forked from MATPOWER/matpower
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhave_fcn.m
581 lines (570 loc) · 25.2 KB
/
have_fcn.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
function rv = have_fcn(tag, rtype)
%HAVE_FCN Test for optional functionality / version info.
% TORF = HAVE_FCN(TAG)
% TORF = HAVE_FCN(TAG, TOGGLE)
% VER_STR = HAVE_FCN(TAG, 'vstr')
% VER_NUM = HAVE_FCN(TAG, 'vnum')
% DATE = HAVE_FCN(TAG, 'date')
% INFO = HAVE_FCN(TAG, 'all')
%
% Returns availability, version and release information for optional
% MATPOWER functionality. All information is cached, and the cached values
% returned on subsequent calls. If the functionality exists, an attempt is
% made to determine the release date and version number. The second
% argument defines which value is returned, as follows:
% <none> 1 = optional functionality is available, 0 = not available
% 'vstr' version number as a string (e.g. '3.11.4')
% 'vnum' version number as numeric value (e.g. 3.011004)
% 'date' release date as a string (e.g. '20-Jan-2015')
% 'all' struct with fields named 'av' (for 'availability'), 'vstr',
% 'vnum' and 'date', and values corresponding to the above,
% respectively.
%
% For functionality that is not available, all calls with a string-valued
% second argument will return an empty value.
%
% Alternatively, the optional functionality specified by TAG can be toggled
% OFF or ON by calling HAVE_FCN with a numeric second argument TOGGLE with
% one of the following values:
% 0 - turn OFF the optional functionality
% 1 - turn ON the optional functionality (if available)
% -1 - toggle the ON/OFF state of the optional functionality
%
% Possible values for input TAG and their meanings:
% bpmpd - BP, BPMPD interior point solver
% clp - CLP, LP/QP solver(http://www.coin-or.org/projects/Clp.xml)
% opti_clp - version of CLP distributed with OPTI Toolbox
% (http://www.i2c2.aut.ac.nz/Wiki/OPTI/)
% cplex - CPLEX, IBM ILOG CPLEX Optimizer
% e4st - E4ST (http://e4st.com/)
% fmincon - FMINCON, solver from Optimization Toolbox 2.x +
% fmincon_ipm - FMINCON with Interior Point solver, from Opt Tbx 4.x +
% glpk - GLPK, GNU Linear Programming Kit
% gurobi - GUROBI, Gurobi solver (http://www.gurobi.com/), 5.x +
% intlinprog - INTLINPROG, MILP solver from Optimization
% Toolbox 7.0 (R2014a)+
% ipopt - IPOPT, NLP solver
% (http://www.coin-or.org/projects/Ipopt.xml)
% linprog - LINPROG, LP solver from Optimization Toolbox 2.x +
% linprog_ds - LINPROG with dual-simplex solver
% from Optimization Toolbox 7.1 (R2014b) +
% knitro - KNITRO, NLP solver (http://www.ziena.com/)
% knitromatlab - KNITRO, version 9.0.0+
% ktrlink - KNITRO, version < 9.0.0 (requires Opt Tbx)
% matlab - code is running under MATLAB, as opposed to Octave
% minopf - MINOPF, MINOPF, MINOS-based OPF solver
% most - MOST, MATPOWER Optimal Scheduling Tool
% mosek - MOSEK, LP/QP solver (http://www.mosek.com/)
% optimoptions - OPTIMOPTIONS, option setting funciton for Optim Tbx 6.3+
% pardiso - PARDISO, Parallel Sparse Direct and Linear Solver
% (http://www.pardiso-project.org)
% quadprog - QUADPROG, QP solver from Optimization Toolbox 2.x +
% quadprog_ls - QUADPROG with large-scale interior point convex solver
% from Optimization Toolbox 6.x +
% pdipmopf - PDIPMOPF, primal-dual interior point method OPF solver
% scpdipmopf - SCPDIPMOPF, step-controlled PDIPM OPF solver
% smartmarket - RUNMARKET and friends, for running an auction
% tralmopf - TRALMOPF, trust region based augmented Langrangian
% OPF solver
% octave - code is running under Octave, as opposed to MATLAB
% sdp_pf - SDP_PF applications of semi-definite programming
% relaxation of power flow equations
% yalmip - YALMIP SDP modeling platform
% sedumi - SeDuMi SDP solver
% sdpt3 - SDPT3 SDP solver
%
% Examples:
% if have_fcn('minopf')
% results = runopf(mpc, mpoption('opf.ac.solver', 'MINOPF'));
% end
%
% Optional functionality can also be toggled OFF and ON by calling HAVE_FCN
% with the following syntax,
% TORF = HAVE_FCN(TAG, TOGGLE)
% where TOGGLE takes a numeric value as follows:
% Private tags for internal use only:
% catchme - support for 'catch me' syntax in try/catch constructs
% evalc - support for evalc() function
% ipopt_auxdata - support for ipopt_auxdata(), required by 3.11 and later
% lu_vec - support for lu(..., 'vector') syntax
% regexp_split - support for 'split' argument to regexp()
% MATPOWER
% Copyright (c) 2004-2016, Power Systems Engineering Research Center (PSERC)
% by Ray Zimmerman, PSERC Cornell
%
% This file is part of MATPOWER.
% Covered by the 3-clause BSD License (see LICENSE file for details).
% See http://www.pserc.cornell.edu/matpower/ for more info.
if nargin > 1 && isnumeric(rtype)
toggle = 1;
on_off = rtype;
if on_off < 0
TorF = have_fcn(tag);
on_off = ~TorF;
end
else
toggle = 0;
end
persistent fcns;
if toggle %% change availability
if on_off %% turn on if available
fcns = rmfield(fcns, tag); %% delete field to force re-check
else %% turn off
if ~isfield(fcns, tag) %% not yet been checked
TorF = have_fcn(tag); %% cache result first
end
fcns.(tag).av = 0; %% then turn off
end
TorF = have_fcn(tag); %% return cached value
else %% detect availability
%% info not yet cached?
if ~isfield(fcns, tag)
%%----- determine installation status, version number, etc. -----
%% initialize default values
TorF = 0;
vstr = '';
rdate = '';
switch tag
%%----- public tags -----
case 'bpmpd'
TorF = exist('bp', 'file') == 3;
if TorF
v = bpver('all');
vstr = v.Version;
rdate = v.Date;
end
case 'clp'
tmp = have_fcn('opti_clp', 'all');
if tmp.av %% have opti_clp
TorF = tmp.av;
vstr = tmp.vstr;
rdate = tmp.date;
elseif exist('clp','file') == 2 && exist('mexclp','file') == 3
TorF = 1;
vstr = '';
end
case 'opti_clp'
TorF = exist('opti_clp', 'file') == 2 && exist('clp', 'file') == 3;
if TorF
str = evalc('clp');
pat = 'CLP: COIN-OR Linear Programming \[v([^\s,]+), Built ([^\],])+(,[^\]]*)*\]'; %% OPTI, Giorgetti/Currie
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
rdate = datestr(t{1}{2}, 'dd-mmm-yyyy');
end
end
case 'cplex'
if exist('cplexqp', 'file')
%% it's installed, but we need to check for MEX for this arch
p = which('cplexqp'); %% get the path
len = length(p) - length('cplexqp.p');
w = what(p(1:len)); %% look for mex files on the path
for k = 1:length(w.mex)
if regexp(w.mex{k}, 'cplexlink[^\.]*');
TorF = 1;
break;
end
end
end
if TorF
try
cplex = Cplex('null');
vstr = cplex.getVersion;
catch
TorF = 0;
end
end
case 'e4st'
TorF = exist('e4st_ver', 'file') == 2;
if TorF
v = e4st_ver('all');
vstr = v.Version;
rdate = v.Date;
end
case {'fmincon', 'fmincon_ipm', 'intlinprog', 'linprog', ...
'linprog_ds', 'optimoptions', 'quadprog', 'quadprog_ls'}
matlab = have_fcn('matlab');
if ~matlab || (matlab && license('test', 'optimization_toolbox'))
v = ver('optim');
if length(v) > 1
warning('The built-in VER command is behaving strangely, probably as a result of installing a 3rd party toolbox in a directory named ''optim'' on your path. Check each element of the output of ver(''optim'') to find the offending toolbox, then move the toolbox to a more appropriately named directory.');
v = v(1);
end
if isempty(v) || isempty(v.Version)
vstr = ''; %% make sure it's a string
rdate = [];
else
vstr = v.Version;
rdate = v.Date;
end
otver = vstr2num(vstr);
switch tag
case 'fmincon'
TorF = (exist('fmincon', 'file') == 2 || ...
exist('fmincon', 'file') == 6) & matlab;
case 'intlinprog'
TorF = exist('intlinprog', 'file') == 2 & matlab;
case 'linprog'
TorF = exist('linprog', 'file') == 2 & matlab; %% don't try to use Octave linprog
case 'quadprog'
TorF = exist('quadprog', 'file') == 2;
%% Octave optim 1.5.0 and earlier, had problems with
%% incorrect lambdas, including opposite sign
%% convention for equality multipliers
if ~matlab && otver <= 1.005
TorF = 0;
end
otherwise
if matlab
switch tag
case 'fmincon_ipm'
if otver >= 4 %% Opt Tbx 4.0+ (R208a+, MATLAB 7.6+)
TorF = 1;
end
case 'linprog_ds'
if otver >= 7.001 %% Opt Tbx 7.1+ (R2014b+, MATLAB 8.4+)
TorF = 1;
end
case 'optimoptions'
if otver >= 6.003 %% Opt Tbx 6.3+ (R2013a+, MATLAB 8.1+)
TorF = 1;
end
case 'quadprog_ls'
if otver >= 6 %% Opt Tbx 6.0+ (R2011a+, MATLAB 7.12+)
TorF = 1;
end
end
else %% octave
TorF = 0;
end
end
end
case 'glpk'
if exist('glpk','file') == 3 %% Windows OPTI install (no glpk.m)
TorF = 1;
str = evalc('glpk');
pat = 'GLPK: GNU Linear Programming Kit \[v([^\s,]+), Built ([^\],])+(,[^\]]*)*\]'; %% OPTI, Giorgetti/Currie
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
rdate = datestr(t{1}{2}, 'dd-mmm-yyyy');
end
elseif exist('glpk','file') == 2 %% others have glpk.m and ...
if exist('__glpk__','file') == 3 %% octave __glpk__ MEX
TorF = 1;
if have_fcn('evalc')
str = evalc('glpk(1, 1, 1, 1, 1, ''U'', ''C'', -1, struct(''msglev'', 3))');
pat = 'GLPK Simplex Optimizer, v([^\s,]+)';
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
end
end
elseif exist('glpkcc','file') == 3 %% MATLAB glpkcc MEX
TorF = 1;
str = evalc('glpk');
pat = 'GLPK Matlab interface\. Version: ([^\s,]+)'; %% glpkccm, Giorgetti/Klitgord
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
end
end
end
case 'gurobi'
TorF = exist('gurobi', 'file') == 3;
if TorF
try
model = struct( ...
'A', sparse(1), ...
'rhs', 1, ...
'sense', '=', ...
'vtype', 'C', ...
'obj', 1, ...
'modelsense', 'min' ...
);
params = struct( ...
'outputflag', 0 ...
);
result = gurobi(model, params);
vstr = sprintf('%d.%d.%d', result.versioninfo.major, result.versioninfo.minor, result.versioninfo.technical);
catch % gurobiError
fprintf('Gurobi Error!\n');
% disp(gurobiError.message);
end
end
case 'ipopt'
TorF = exist('ipopt', 'file') == 3;
if TorF
str = evalc('qps_ipopt([],1,1,1,1,1,1,1,struct(''verbose'', 2))');
pat = 'Ipopt version ([^\s,]+)';
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
if vstr2num(vstr) >= 3.011 && ...
~exist('ipopt_auxdata', 'file')
TorF = 0;
warning('Improper installation of IPOPT. Version %s detected, but IPOPT_AUXDATA.M is missing.', vstr);
end
end
end
case 'knitro' %% any Knitro
tmp = have_fcn('knitromatlab', 'all');
if tmp.av
TorF = tmp.av;
vstr = tmp.vstr;
rdate = tmp.date;
else
tmp = have_fcn('ktrlink', 'all');
if tmp.av
TorF = tmp.av;
vstr = tmp.vstr;
rdate = tmp.date;
end
end
case {'knitromatlab', 'ktrlink'}
%% knitromatlab for Knitro 9.0 or greater
%% ktrlink for pre-Knitro 9.0, requires Optim Toolbox
TorF = exist(tag, 'file') == 2;
if TorF
try
str = evalc(['[x fval] = ' tag '(@(x)1,1);']);
end
TorF = exist('fval', 'var') && fval == 1;
if TorF
pat = 'KNITRO ([^\s]+)\n|Knitro ([^\s]+)\n';
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
end
end
end
case 'matlab'
v = ver('matlab');
if length(v) > 1
warning('The built-in VER command is behaving strangely, probably as a result of installing a 3rd party toolbox in a directory named ''matlab'' on your path. Check each element of the output of ver(''matlab'') to find the offending toolbox, then move the toolbox to a more appropriately named directory.');
v = v(1);
end
if ~isempty(v) && isfield(v, 'Version') && ~isempty(v.Version)
TorF = 1;
vstr = v.Version;
rdate = v.Date;
end
case 'minopf'
TorF = exist('minopf', 'file') == 3;
if TorF
v = minopfver('all');
vstr = v.Version;
rdate = v.Date;
end
case 'most'
TorF = exist('most', 'file') == 2;
if TorF
v = mostver('all');
vstr = v.Version;
rdate = v.Date;
end
case 'mosek'
TorF = exist('mosekopt', 'file') == 3;
if TorF
% MOSEK Version 6.0.0.93 (Build date: 2010-10-26 13:03:27)
% MOSEK Version 6.0.0.106 (Build date: 2011-3-17 10:46:54)
% MOSEK Version 7.0.0.134 (Build date: 2014-10-2 11:10:02)
pat = 'Version (\.*\d)+.*Build date: (\d+-\d+-\d+)';
[s,e,tE,m,t] = regexp(evalc('mosekopt'), pat);
if isempty(t)
[r, res] = mosekopt('version');
v = res.version;
vstr = sprintf('%d.%d.%d.%d', ...
v.major, v.minor, v.build, v.revision);
else
vstr = t{1}{1};
rdate = datestr(t{1}{2}, 'dd-mmm-yyyy');
end
end
case 'smartmarket'
TorF = exist('runmarket', 'file') == 2;
if TorF
v = mpver('all');
vstr = v.Version;
rdate = v.Date;
end
case 'octave'
TorF = exist('OCTAVE_VERSION', 'builtin') == 5;
if TorF
v = ver('octave');
vstr = v.Version;
rdate = v.Date;
end
case 'pardiso'
TorF = exist('pardisoinit', 'file') == 3 && ...
exist('pardisoreorder', 'file') == 3 && ...
exist('pardisofactor', 'file') == 3 && ...
exist('pardisosolve', 'file') == 3 && ...
exist('pardisofree', 'file') == 3;
if TorF
try
A = sparse([1 2; 3 4]);
b = [1;1];
% Summary PARDISO 5.1.0: ( reorder to reorder )
pat = 'Summary PARDISO (\.*\d)+:';
info = pardisoinit(11, 0);
info = pardisoreorder(A, info, false);
% [s,e,tE,m,t] = regexp(evalc('info = pardisoreorder(A, info, true);'), pat);
% if ~isempty(t)
% vstr = t{1}{1};
% end
info = pardisofactor(A, info, false);
[x, info] = pardisosolve(A, b, info, false);
pardisofree(info);
if any(x ~= [-1; 1])
TorF = 0;
end
catch
TorF = 0;
end
end
case {'pdipmopf', 'scpdipmopf', 'tralmopf'}
if have_fcn('matlab')
vn = have_fcn('matlab', 'vnum');
%% requires >= MATLAB 6.5 (R13) (released 20-Jun-2002)
%% older versions do not have mxCreateDoubleScalar() function
%% (they have mxCreateScalarDouble() instead)
if vn >= 6.005
switch tag
case 'pdipmopf'
TorF = exist('pdipmopf', 'file') == 3;
case 'scpdipmopf'
TorF = exist('scpdipmopf', 'file') == 3;
case 'tralmopf'
%% requires >= MATLAB 7.3 (R2006b) (released 03-Aug-2006)
%% older versions do not include the needed form of chol()
if vn >= 7.003
TorF = exist('tralmopf', 'file') == 3;
end
end
end
if TorF
v = feval([tag 'ver'], 'all');
vstr = v.Version;
rdate = v.Date;
end
end
case 'sdp_pf'
TorF = have_fcn('yalmip') && exist('mpoption_info_sdp_pf', 'file') == 2;
if TorF
v = sdp_pf_ver('all');
vstr = v.Version;
rdate = v.Date;
end
case 'yalmip'
TorF = ~have_fcn('octave') && exist('yalmip','file') == 2;
%% YALMIP does not yet work with Octave, rdz 1/6/14
if TorF
vstr = yalmip('version');
if length(vstr) == 8
yr = str2num(vstr(1:4));
mo = str2num(vstr(5:6));
dy = str2num(vstr(7:8));
rdate = datestr([yr mo dy 0 0 0], 'dd-mmm-yyyy');
end
end
case 'sdpt3'
TorF = exist('sdpt3','file') == 2;
if TorF
str = evalc('help sdpt3');
pat = 'version\s+([^\s]+).*Last Modified: ([^\n]+)\n';
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
rdate = datestr(t{1}{2}, 'dd-mmm-yyyy');
end
end
case 'sedumi'
TorF = exist('sedumi','file') == 2;
if TorF
warn_state = warning; %% sedumi turns (and leaves!) off all warnings
str = evalc('x = sedumi([1 1], 1, [1;2])');
warning(warn_state);
pat = 'SeDuMi\s+([^\s]+)';
[s,e,tE,m,t] = regexp(str, pat);
if ~isempty(t)
vstr = t{1}{1};
end
end
%%----- private tags -----
case 'catchme' %% not supported by MATLAB <= 7.4 (R2007a), Octave <= 3.6
if have_fcn('octave')
if have_fcn('octave', 'vnum') > 3.006
TorF = 1;
end
else
if have_fcn('matlab', 'vnum') > 7.004
TorF = 1;
end
end
case 'evalc'
if have_fcn('matlab')
TorF = 1;
end
case 'ipopt_auxdata'
if have_fcn('ipopt')
vn = have_fcn('ipopt', 'vnum');
if ~isempty(vn) && vn >= 3.011
TorF = 1;
end
end
case 'lu_vec' %% lu(..., 'vector') syntax supported?
if have_fcn('matlab') && have_fcn('matlab', 'vnum') < 7.003
TorF = 0; %% lu(..., 'vector') syntax not supported
else
TorF = 1;
end
case 'regexp_split' %% missing for MATLAB < 7.3 & Octave < 3.8
if have_fcn('matlab') && have_fcn('matlab', 'vnum') >= 7.003
TorF = 1;
elseif have_fcn('octave', 'vnum') >= 3.008
TorF = 1;
end
%%----- unknown tag -----
otherwise
warning('have_fcn: unknown functionality ''%s''', tag);
vstr = 'unknown';
end
%% assign values to cache
fcns.(tag).av = TorF;
fcns.(tag).vstr = vstr;
if isempty(vstr)
fcns.(tag).vnum = [];
else
fcns.(tag).vnum = vstr2num(vstr);
end
fcns.(tag).date = rdate;
end
end
%% extract desired values from cache
if nargin < 2 || toggle
rv = fcns.(tag).av;
else
switch lower(rtype)
case 'vstr'
rv = fcns.(tag).vstr;
case 'vnum'
rv = fcns.(tag).vnum;
case 'date'
rv = fcns.(tag).date;
case 'all'
rv = fcns.(tag);
end
end
function num = vstr2num(vstr)
% Converts version string to numerical value suitable for < or > comparisons
% E.g. '3.11.4' --> 3.011004
pat = '\.?(\d+)';
[s,e,tE,m,t] = regexp(vstr, pat);
b = 1;
num = 0;
for k = 1:length(t)
num = num + b * str2num(t{k}{1});
b = b / 1000;
end