-
Notifications
You must be signed in to change notification settings - Fork 139
/
yalmiptest.m
504 lines (445 loc) · 13.5 KB
/
yalmiptest.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
function out = yalmiptest(prefered_solver,auto)
%YALMIPTEST Runs a number of test problems.
%
% YALMIPTEST is recommended when a new solver or a new version
% of YALMIP installed.
%
% EXAMPLES
% YALMIPTEST % Without argument, default solver used
% YALMIPTEST('solver tag') % Test with specified solver
% YALMIPTEST(options) % Test with specific options structure from
%
% See also SDPSETTINGS
if ~exist('sedumi2pen.m')
disp('Add /yalmip/extras etc to your path first...')
disp('Read the <a href="https://yalmip.github.io/tutorial/installation/">Installation notes</a>.')
return
end
if ~exist('callsedumi.m')
disp('Still missing paths...Just do an addpath(genpath(''yalmiprootdirectory''));')
disp('Read the <a href="https://yalmip.github.io/tutorial/installation/">Installation notes</a>.')
return
end
% SDPT3 has a function called constraint.m which causes issues
detected = which('constraint.m');
if isa(detected,'cell')
if length(detected)>0
if isempty(strfind(detected{1},'extras\@constraint'))
clc
disp('You seem to have some other toolbox with a function called constraint.m');
disp('Delete that toolbox, or delete the function/class, or change path so that YALMIP is on top.');
disp(detected{1})
end
return
end
end
detected = which('yalmip.m','-all');
% Will not work in Octave as Octave only reports first item found?
if isa(detected,'cell')
if length(detected)>1
clc
disp('You seem to have multiple installations of YALMIP in your path.')
disp('Please correct this...');
disp(detected)
return
end
end
% Pagination really doesn't work well with solvers
more off
if exist('OCTAVE_VERSION', 'builtin')
OctaveRunning = 1;
else
OctaveRunning = 0;
end
donttest = 0;
if (nargin==1) && isa(prefered_solver,'char') && strcmp(prefered_solver,'test')
donttest = 0;
prefered_solver = '';
else
donttest = 1;
end
if nargin==0
prefered_solver = '';
else
if ~(isa(prefered_solver,'struct') | isa(prefered_solver,'char'))
error('Argument should be a solver tag, or a sdpsettings structure');
end
if isa(prefered_solver,'char')
donttest = 1;
end
end
if ~(exist('callsedumi')==2)
clc
disp('The directory yalmip/solvers is not in your path.')
disp('These must be in path:')
disp(' yalmip/');
disp(' yalmip/extras');
disp(' yalmip/operators');
disp(' yalmip/modules');
disp(' yalmip/solvers');
disp('See <a href="https://yalmip.github.io/tutorial/installation/">installation guide</a>')
return
end
foundstring = {'not found','found','internal'};
teststring = {'-failed','+passed'};
if ~donttest
header = {'Solver','Version','Status','Unit test'};
else
header = {'Solver','Version','Status'};
end
[solvers,found] = getavailablesolvers(0);
j = 1;
status = ones(length(solvers),1);
s = {solvers.tag};
for i = 1:length(solvers)
if solvers(i).show
same = find(strcmpi(solvers(i).tag,s));
% Find all instances of same solver different versions
for k = setdiff(same,i)
% No reason to show for all versions
solvers(k).show = 0;
end
data{j,1} = upper(solvers(i).tag);
found_versions = same(find(found(same)));
if ~isempty(found_versions)
idx = min(found_versions);
version = solvers(idx).version;
if ~any(strcmpi(version,{'geometric','standard'}))
data{j,2} = [solvers(idx).version ' ' solvers(idx).subversion];
end
else
idx = i;
end
status(j) = found(idx)+1+solvers(idx).builtin;
data{j,3} = foundstring{found(idx)+1+solvers(idx).builtin};
j = j+1;
end
end
if isa(prefered_solver,'char')
ops = sdpsettings('Solver',prefered_solver);
else
ops = prefered_solver;
end
ops.saveyalmipmodel = 1;
if ~((nargin==2) & (ops.verbose==0))
[sortedName,loc] = sort({data{:,1}});
loc = [loc(find(status(loc)==3)) loc(find(status(loc)==2)) loc(find(status(loc)==1))];
dataSorted = reshape({data{loc,:}},[],3);
yalmiptable({'Searching for installed solvers'},header,dataSorted);
disp(' ')
end
if nargin<2
disp('Press any key to continue test')
pause
end
i=1;
test{i}.fcn = 'test_core';
test{i}.desc = 'Core functionalities';
i = i+1;
test{i}.fcn = 'test_linear_programming';
test{i}.desc = 'Linear programming (LP)';
i = i+1;
test{i}.fcn = 'test_quadratic_programming';
test{i}.desc = 'Quadratic programming (QP)';
i = i+1;
test{i}.fcn = 'test_socp_programming';
test{i}.desc = 'Second-order cone programming (SOCP)';
i = i+1;
test{i}.fcn = 'test_semidefinite_programming';
test{i}.desc = 'Semidefinite programming (SDP)';
i = i+1;
test{i}.fcn = 'test_geometric_programming';
test{i}.desc = 'Geometric programming (GP)';
i = i+1;
test{i}.fcn = 'test_nonlinear_programming';
test{i}.desc = 'Nonlinear programming (NLP)';
i = i+1;
test{i}.fcn = 'test_nonlinear_semidefinite_programming';
test{i}.desc = 'Nonlinear SDP (NLSDP)';
i = i+1;
test{i}.fcn = 'test_exponential_cone_programming';
test{i}.desc = 'Exponential cone programming (ECP)';
i = i+1;
test{i}.fcn = 'test_milinear_programming';
test{i}.desc = 'Mixed-integer LP (MIQP)';
i = i+1;
test{i}.fcn = 'test_miquadratic_programming';
test{i}.desc = 'Mixed-integer QP (MIQP)';
i = i+1;
test{i}.fcn = 'test_misocp_programming';
test{i}.desc = 'Mixed-integer SOCP (MISOCP)';
i = i+1;
test{i}.fcn = 'test_nonconvex_quadratic_programming';
test{i}.desc = 'Global nonconvex quadratic programming';
i = i+1;
test{i}.fcn = 'test_nonconvex_global_programming';
test{i}.desc = 'Global nonconvex programming';
i = i+1;
pass_strings = {'Error','Passed','Solver not available'};
% Run test-problems
for i = 1:length(test)
try
if ops.verbose
disp(' ');
disp(['Testing function ' test{i}.fcn]);
disp(' ');
end
% First make call to figure out solver
info = eval([test{i}.fcn '(ops)']);
if ~OctaveRunning
sols{i} = addLink(upper(cleanversion(info.yalmipmodel.solver.tag)));
else
sols{i} = cleanversion(info.yalmipmodel.solver.tag);
end
pass(i) = info.problem == 0;
if pass(i)
results{i}='Success';
else
results{i}='Failed';
end
catch
pass(i) = 0;
results{i} = 'Failed';
sols{i} = '';
end
end
clear data;
header = {'Test','Status', 'Solver'};
for i = 1:length(pass)
data{i,1} = test{i}.desc;
data{i,2} = results{i};
data{i,3} = sols{i};
end
if ops.verbose
disp(' ');
end
formats{1}.data.just = 'right';
formats{2}.data.just = 'right';
formats{3}.data.just = 'right';
formats{1}.header.just = 'right';
formats{2}.header.just = 'right';
formats{3}.header.just = 'right';
clc
yalmiptable([],header,data,formats)
% Test if any LMI solver is installed.
x = sdpvar(2);[p,aux1,aux2,m] = export(x>=0,[],[],[],[],0);
if ~isempty(m)
only_lmilab = strcmpi(m.solver.tag,'lmilab');
only_fmincon = strcmpi(m.solver.tag,'fmincon-standard');
else
only_lmilab = 0;
only_fmincon = 0;
end
if isempty(m)
disp('You do not have any LMI solver installed')
disp(' If you intend to solve LMIs you must install a solver.')
else
if only_lmilab
disp('You do not have any good LMI solver installed')
disp(' (only found <a href="https://yalmip.github.io/solver/lmilab/">LMILAB which should be avoided in YALMIP</a>).')
disp('If you intend to solve LMIs, please install a better solver.')
elseif only_fmincon
disp('You do not have any LMI solver installed')
disp(' (YALMIP will use a nonlinear solver which cannot be expected to work)')
disp(' If you intend to solve LMIs you must install a solver.')
end
end
x = binvar(1);[p,aux1,aux2,m] = export(x>=0,x,[],[],[],0);
if isempty(m)
disp('You do not have any LP/MILP solver installed')
disp(' If you intend to solve LPs/MILPs, you have to install one.')
else
only_bnb = strcmpi(m.solver.tag,'bnb');
if only_bnb
disp('You do not have any MILP solver installed')
disp(' (only found internal <a href="https://yalmip.github.io/solver/bnb/">BNB</a>).')
disp(' If you intend to solve MILP, please install a better solver.')
end
end
x = binvar(1);[p,aux1,aux2,m] = export(x>=0,x^2,[],[],[],0);
if isempty(m)
disp('You do not have any QP/SOCP/MIQP/MISOCP solver installed')
disp(' If you intend to solve QP/SOCP/MIQP/MISOCP you must install solver.')
else
only_bnb = strcmpi(m.solver.tag,'bnb');
if only_bnb
disp('You do not have any MIQP/MISOCP solver installed (only found internal <a href="https://yalmip.github.io/solver/bnb/">BNB</a>)')
disp(' If you intend to solve MIQP/MISOCP, please install a better solver.')
end
end
disp('See <a href="https://yalmip.github.io/allsolvers">guide on interfaced solvers</a>')
function sol = test_core(ops)
% Fake
sol.yalmipmodel.solver.tag = '';
sol.problem = 0;
try
x = sdpvar(2,2);
x = sdpvar(2,2,'symmetric');
x = sdpvar(2,2,'full');
x = sdpvar(2,2,'toeplitz');
x = sdpvar(2,2,'hankel');
x = sdpvar(2,2,'skew');
if ~ishermitian(sdpvar(2,2,'hermitian','complex'))
error('bug')
end
if ~issymmetric(sdpvar(2,2,'symmetric','complex'))
error('bug')
end
if ~isreal(real(sdpvar(2,2,'symmetric','complex')))
error('bug')
end
if isreal(sqrt(-1)*real(sdpvar(2,2,'symmetric','complex')))
error('bug')
end
x = sdpvar(2,1,'','co');
if ~isreal(x'*x)
error('bug')
end
x = sdpvar(2,2,'','co');
if ~isreal(diag(x'*x))
error('bug')
end
x = sdpvar(1,1);
y = sdpvar(2,2);
x*eye(2);
eye(2)*x;
y*3;
3*y;
x = sdpvar(2,3);
y = sdpvar(2,3);
assign(x,randn(2,3));
z = replace(x,x(1,1:2),[8 9]);
z = x+y;
z = x-y;
z = x+1;
z = x-1;
z = x+ones(2,3);
z = x-ones(2,3);
z = ones(2,3)-x;
z = ones(2,3)-x;
z = eye(2)*x;
z = x*eye(3);
z = diag(x);
z = trace(x(1:2,1:2));
z = diff(x);
z = fliplr(x);
z = flipud(x);
z = kron(x,eye(3));
z = kron(eye(3),x);
z = rot90(x);
z = sum(x);
z = diff(x);
z = x';
z = x.';
z = tril(x);
z = triu(x);
z = [x y];
z = [x;y];
sdpvar x y
diag([x y])*[x^-1;y^-1];
assert(isequal([x x;x x]*x-[x x;x x].*x,zeros(2)))
assert(isequal(trace([x x;x x]*[x y;y x])-(x*x+x*y+y*x+x*x),0))
% Regression ??
yalmip('clear')
sdpvar x
(1+x+x^4)*(1-x^2);
% Regression complex multiplcation
A = randn(10,5)+sqrt(-1)*randn(10,5);
b = randn(10,1)+sqrt(-1)*randn(10,1);
x = sdpvar(5,1);
res = A*x-b;
assert(nnz(clean([res res]'*[res res]-res'*res,1e-8))==0)
assert(isreal(clean(res'*res,1e-8)))
assert(isreal(x*x'))
catch
sol.problem = 9;
sol.info = 'Problems';
end
function sol = test_semidefinite_programming(ops)
t = sdpvar(1,1);
Y = sdpvar(2,2);
F = [Y<=t*eye(2), Y>=[1 0.2;0.2 1]];
sol = optimize(F,t,ops);
function sol = test_linear_programming(ops)
N = 5;
A = [2 -1;1 0];
B = [1;0];
C = [0.5 0.5];
[H,S] = create_CHS(A,B,C,N);
x = [2;0];
t = sdpvar(2*N,1);
U = sdpvar(N,1);
Y = H*x+S*U;
F = (U<=1)+(U>=-1);
F = F+(Y(N)>=-1);
F = F+(Y(N)<=1);
F = F+([Y;U]<=t)+([Y;U]>=-t);
sol = optimize(F,sum(t),ops);
function sol = test_socp_programming(ops)
x = sdpvar(2,1);
a = [0;1];
b = [1;1];
F = norm(x-a)<=1;
F = F+[norm(x-b) <= 1];
sol = optimize(F,sum(x),ops);
function sol = test_misdp_programming(ops)
x = intvar(4,1);
e = magic(4)*x-1;
sdpvar t
obj = t;
sol = optimize([t e';e eye(4)]>=0,obj,ops);
function sol = test_misocp_programming(ops)
x = intvar(4,1);
obj = norm(magic(4)*x-1,2);
sol = optimize([-5 <= x <= 5],obj,ops);
function sol = test_miquadratic_programming(ops)
x = intvar(4,1);
obj = norm(magic(4)*x-1,2)^2;
sol = optimize([-5 <= x <= 5],obj,ops);
function sol = test_milinear_programming(ops)
x = intvar(4,1);
obj = norm(magic(4)*x-1,1);
sol = optimize([-5 <= x <= 5],obj,ops);
function sol = test_quadratic_programming(ops)
x = sdpvar(10,1);
sol = optimize([sum(x)==2, -1 <= x <= 1],x'*x,ops);
function sol = test_nonconvex_quadratic_programming(ops)
x = sdpvar(10,1);
ops.forceglobal = 1;
sol = optimize([sum(x)==2, -1 <= x <= 1],-x'*x,ops);
function sol = test_nonconvex_global_programming(ops)
x = sdpvar(3,1);
ops.forceglobal = 1;
sol = optimize([sum(x.^3)==2, -1 <= x <= 1],-x'*x,ops);
function sol = test_nonlinear_semidefinite_programming(ops)
A = [-1 2;-3 -4];
P = sdpvar(2,2);
alpha = sdpvar(1,1);
F = (P>=eye(2))+(A'*P+P*A <= -2*alpha*P)+(alpha >= 0);
sol = optimize([F,P(:) <= 100],-alpha,ops);
function sol = test_geometric_programming(ops)
t1 = sdpvar(1,1);
t2 = sdpvar(1,1);
t3 = sdpvar(1,1);
t = [t1 t2 t3];
obj = (40*t1^-1*t2^-0.5*t3^-1)+(20*t1*t3)+(40*t1*t2*t3);
F = ((1/3)*t1^-2*t2^-2+(4/3)*t2^0.5*t3^-1 <= 1);
F = [F, t>=0];
sol = optimize(F,obj,ops);
function sol = test_nonlinear_programming(ops)
sdpvar x y
sol = optimize(x^2 + x^4 + exp(x) <= 1, x^2+y^2,ops);
function sol = test_exponential_cone_programming(ops)
sdpvar x y z
sol = optimize([expcone([x;2;z]),x==1],z,ops);
function html = addLink(x)
if length(x)>0
html = ['<a href="https://yalmip.github.io/solver/' lower(x) '">' upper(x) '</a>'];
else
html = '';
end
function x = cleanversion(x)
s = strfind(x,'-');
if ~isempty(s)
x=x(1:s(1)-1);
end