forked from aeroimperial-optimization/mpYALMIP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcallsdpagmp_neos.m
251 lines (198 loc) · 6.88 KB
/
callsdpagmp_neos.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
function output = callsdpagmp_neos(interfacedata)
% CALLSDPAGMP_NEOS.m Send SDPA-GMP problem to NEOS solver from YALMIP
% ----------------------------------------------------------------------- %
%
% Copyright (C) 2017 Hugo Tadashi
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% ----------------------------------------------------------------------- %
% Check for xmlrpc-client-1.1.1.jar
if ~exist('xmlrpc-client-1.1.1.jar','file')
error('Please add xmlrpc-client-1.1.1.jar to your dynamic class path');
end
% Retrieve needed data
options = interfacedata.options;
F_struc = interfacedata.F_struc;
c = interfacedata.c;
K = interfacedata.K;
x0 = interfacedata.x0;
ub = interfacedata.ub;
lb = interfacedata.lb;
% Check if param.sdpa file exists in pwd
cleanup = 0;
if ~exist([pwd,filesep,'param.sdpa'],'file')
cleanup = 1;
% write it with default parameters, otherwise failure!
fID = fopen([pwd,filesep,'param.sdpa'],'w');
fprintf(fID,'200 unsigned int maxIteration; \n');
fprintf(fID,'1.0E-25 double 0.0 < epsilonStar; \n');
fprintf(fID,'1.0E6 double 0.0 < lambdaStar; \n');
fprintf(fID,'2.0 double 1.0 < omegaStar; \n');
fprintf(fID,'-1.0E25 double lowerBound; \n');
fprintf(fID,'1.0E25 double upperBound; \n');
fprintf(fID,'0.1 double 0.0 <= betaStar < 1.0; \n');
fprintf(fID,'0.2 double 0.0 <= betaBar < 1.0, betaStar <= betaBar;\n');
fprintf(fID,'0.7 double 0.0 < gammaStar < 1.0; \n');
fprintf(fID,'1.0E-25 double 0.0 < epsilonDash; \n');
fprintf(fID,'200 precision; \n');
fclose(fID);
end
% Bounded variables converted to constraints
if ~isempty(ub)
% addbounds was renamed somewhere along the way in YALMIP?
try
[F_struc,K] = addbounds(F_struc,K,ub,lb);
catch
[F_struc,K] = addStructureBounds(F_struc,K,ub,lb);
end
end
% Convert from internal (sedumi) format
[mDIM,nBLOCK,bLOCKsTRUCT,c,F] = sedumi2sdpa(F_struc,c,K);
if options.verbose==0
options.sdpa.print = 'no';
else
options.sdpa.print = 'display';
end
if options.savedebug
ops = options.sdpa;
save sdpadebug mDIM nBLOCK bLOCKsTRUCT c F ops
end
if options.showprogress
showprogress(['Calling ' interfacedata.solver.tag],options.showprogress);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CALL SDPA-GMP
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Export to SDPA-GMP, solve and inport results
FF = cellfun(@full,F,'UniformOutput',false);
[nmax,mmax] = size(FF);
BS = abs(bLOCKsTRUCT);
for nn=1:nmax % loop to construc SDPA problem
for mm=1:mmax
if isempty(FF{nn,mm})
FF{nn,mm} = zeros(BS(nn));
end
end
end
tic
inputSDPA = 'sdpagmp_in.dat-s';
outputSDPA = 'sdpagmp_out.out';
header = 'Input from YALMIP';
gensdpagmpfile(inputSDPA,mDIM,nBLOCK,bLOCKsTRUCT,c,FF,header); % write SDPA-GMP input file
% Send job to NEOS server and write solution to outputSDPA file
disp('Creating NEOS server interface')
disp('')
neos = NeosSDPAInterface();
queue = neos.get_queue();
disp('Job queue:')
disp('')
disp(queue)
precision = 'var';
xml_string = build_xml_string(inputSDPA,'param.sdpa',precision);
outputSDPA_filepath = [pwd filesep outputSDPA];
neos.submit_job(xml_string,outputSDPA_filepath);
% import result
[objVal,x,X,Y,INFO] = sdpagmp_read_output(outputSDPA,full(mDIM),full(nBLOCK),full(bLOCKsTRUCT));
solvertime = toc;
% Clean up tmp files created in this directory
delete(inputSDPA);
delete(outputSDPA);
if cleanup
delete('param.sdpa');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% From here onwards, like in YALMIP native callsdpa
% Create variables in YALMIP internal format
Primal = x;
Dual = [];
for i = 1:length(Y)
Dual = [Dual;Y{i}(:)];
end
Slack = [];
if options.saveduals
for i = 1:length(X)
Slack = [Slack;X{i}(:)];
end
end
switch (INFO.phasevalue)
case 'pdOPT'
problem = 0;
case {'noINFO','pFEAS','dFEAS'}
problem = 3;
case {'pdFEAS'}
problem = 4;
case 'pFEAS_dINF'
problem = 2;
case 'pINF_dFEAS'
problem = 1;
case 'pUNBD'
problem = 2;
case 'dUNBD'
problem = 1;
case 'pdINF'
problem = 12;
otherwise
problem = -1;
end
infostr = yalmiperror(problem,interfacedata.solver.tag);
if options.savesolveroutput
solveroutput.objVal = objVal;
solveroutput.x = x;
solveroutput.X = X;
solveroutput.Y = Y;
solveroutput.INFO = INFO;
else
solveroutput = [];
end
if options.savesolverinput
solverinput.mDIM = mDIM;
solverinput.nBLOCK=nBLOCK;
solverinput.bLOCKsTRUCT=bLOCKsTRUCT;
solverinput.c=c;
solverinput.F=F;
else
solverinput = [];
end
% Standard interface
output = createOutputStructure(Primal,Dual,[],problem,infostr,solverinput,solveroutput,solvertime);
end
function xml_string = build_xml_string(sdpa_file,param_sdpa,precision)
% XML header
xml_string = sprintf('<document>\n<category>%s</category>\n<solver>%s</solver>\n<inputType>%s</inputType>\n','sdp','SDPA','SPARSE_SDPA');
% Add email
email = fileread('neos_email.dat');
insert = sprintf('<email> %s </email>',email);
xml_string = strcat(xml_string,insert);
% File in sparse SDPA format
dat = fileread(sdpa_file);
insert = sprintf('<dat><![CDATA[%s]]></dat>\n',dat);
xml_string = strcat(xml_string,insert);
% MATLAB binary file (not used)
insert = sprintf('<mat><![CDATA[]]></mat>\n');
xml_string = strcat(xml_string,insert);
% param.sdpa file
param = fileread(param_sdpa);
insert = sprintf('<param><![CDATA[%s]]></param>\n',param);
xml_string = strcat(xml_string,insert);
% Precision
insert = sprintf('<PRECISION><![CDATA[%s]]></PRECISION>\n',precision);
xml_string = strcat(xml_string,insert);
% Comments (not used)
insert = sprintf('<comment><![CDATA[]]></comment>\n');
xml_string = strcat(xml_string,insert);
% End of xml
xml_string = strcat(xml_string,'</document>');
% Parse XML
xml_string = xmlwrite(xmlreadstring(xml_string));
end