-
Notifications
You must be signed in to change notification settings - Fork 0
/
matlabSCIPSDPInterface_install.m
589 lines (497 loc) · 19.5 KB
/
matlabSCIPSDPInterface_install.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
582
583
584
585
586
587
588
589
function matlabSCIPSDPInterface_install(runTests, specifyCPP)
% OPTI Toolbox Installation File
%
% matlabSCIPSDPInterface_install(runTests, specifyCPP)
%
% runTests: Run the post-installation tests
% specifyCPP: Specify a non-default C++-compiler version
%
% All arguments are optional and if not supplied, the user will be prompted
% to enter their selection in the MATLAB Command Window. True is the
% default option for each argument.
%
% Based on code by Jonathan Currie:
% Copyright (C) 2018 Jonathan Currie (Inverse Problem Limited)
% https://inverseproblem.co.nz/OPTI/
% Adpated by Nicolai Simon, Copyright (C) 2021 TU Darmstadt
% options:
opts = [];
opts.verb = false; % verbose logging
opts.debug = false; % debug for mex files
opts.pp = []; % cell array of preprocessor options, i.e. -D flags
opts.expre = ''; % string to insert further commands before source file
opts.quiet = false; % set mex compilation to quiet
if (strcmp(computer, "PCWIN"))
error("Win32 systems are not supported");
end
% set paths needed for the interface install
cd(fileparts(which(mfilename)));
addpath([pwd() '/Source']);
addpath([pwd() '/Source/scip']);
addpath([pwd() '/Source/scip/Include']);
addpath([pwd() '/Utilities/Install']);
addpath([pwd() '/Utilities/opti']);
addpath([pwd() '/Examples']);
% handle missing input args
if ~(exist('runTests', 'var')), runTests = []; end
if ~(exist('specifyCPP', 'var')), specifyCPP = []; end
% MEX interface source files
src = {'scip/scipsdpmex.cpp scip/scipeventmex.cpp'};
% set path to SCIP files
scippath = setSCIPPath();
scipsdppath = setSCIPSDPPath();
% set include files needed for mex-file
[incscip, installversion] = setIncludeSCIP(scippath);
[incscipsdp, sdpinstallversion] = setIncludeSCIPSDP(scipsdppath);
inc = [incscip incscipsdp];
% set libraries needed for mex-file & specify the corresponding LDflags
[lib, opts.expre] = setLibraries(scippath, scipsdppath, opts.expre, installversion, sdpinstallversion);
% allow for custom CXX Versions for Matlab on linux systems
if strcmp(computer, 'GLNXA64')
cxx_custom = setCXXVersion(specifyCPP);
else
cxx_custom=[];
end
opti_solverMex('scipsdp',src, cxx_custom, inc,lib, opts);
fclose all;
cpath = pwd();
try
cd('Utilities/opti');
catch %#ok<CTCH>
error('You do not appear to be in the interface directory.');
end
% get current versions
localVer = optiver();
fprintf('\n------------------------------------------------\n')
fprintf('INSTALLING Matlab-SCIPSDP Interface ...\n')
cd(cpath);
% check ML ver
if(~isOctave())
matlabVerCheck();
end
% perform MEX file check (also checks pre-reqs)
if (~mexFileCheck(localVer))
return;
end
% update files in cache
rehash;
% post install test if requested
if (isempty(runTests))
fprintf('\n');
in = input('Would you like to run post installation tests? (Recommended) (y/n): ','s');
else
in = bool2yn(runTests);
end
if(strcmpi(in,'y'))
opti_Install_Test(1);
end
% finished
fprintf('\n\nMatlab-SCIPSDP Interface installation complete!\n');
optiSolver;
fprintf('To be able to use the Matlab-SDIPSDP Interface after restarting Matlab, you need to save path changes.\n')
fprintf('or add them manually every time you launch matlab (the script addPaths.m can be used to do so).\n')
fprintf('Paths can be saved using the command:\n')
fprintf(' savepath() \n')
fprintf('If you are unable to save path changes we refer to the ''Common Problems'' section in the README\n')
fprintf('or directly to the Matlab documentation.\n')
% The function setSCIPPath determines the path to the SCIP installation and
% prompts user input, if no path is found.
function scippath = setSCIPPath()
scippath = [];
% include directories
fprintf('\nSearching for SCIP install ...\n');
% check environment variables
fprintf('Checking environment variables SCIPOPTDIR and SCIPDIR ...\n');
if (~isempty(getenv('SCIPOPTDIR')))
fprintf('SCIPOPTDIR variable found.\n')
scipoptpath = getenv('SCIPOPTDIR');
scipoptpath = strrep(scipoptpath, '\', '/');
if(exist([scipoptpath '/scip'], 'dir'))
scippath = checkScipDir([scipoptpath '/scip']);
else
scippath = checkScipDir(scipoptpath);
end
elseif (~isempty(getenv('SCIPDIR')))
fprintf('SCIPDIR variable found.\n')
scipdirpath = getenv('SCIPDIR');
scipdirpath = strrep(scipdirpath, '\', '/');
scippath = checkScipDir(scipdirpath);
end
if (isempty(scippath))
yesno = input('SCIP not found! Would you like to specify a custom install location? (y/n)', 's');
if (strcmp(yesno,'y'))
fprintf('Please specify SCIP directory:');
scipdirpath = uigetdir();
scipdirpath = strrep(scipdirpath, '\', '/');
scippath = checkScipDir(scipdirpath);
end
end
if(isempty(scippath))
error('No SCIP installation found!');
end
% This function checks if the specified SCIP directory can be used as a valid SCIP
% installation.
function scippath = checkScipDir(scipdirpath)
if (~(exist([scipdirpath '/src'], 'dir') || exist([scipdirpath '/include'], 'dir')))
fprintf('\nCould not identify src or include directory: \n');
fprintf('Structure of specified SCIP directory ''')
fprintf(scipdirpath)
fprintf(''' not recognized.\n')
scippath = [];
if (exist([scipdirpath '/scip'], 'dir') && exist([scipdirpath '/soplex'], 'dir') && exist([scipdirpath '/papilo'], 'dir'))
yesno = input('Specified directory seems to contain scipoptsuite root. Use subfolder /scip as scip directory? (y/n)', 's');
if (strcmp(yesno,'y'))
scippath = [scipdirpath '/scip'];
end
end
else
fprintf(['\nSetting SCIP directory to ''' scipdirpath '''.\n']);
scippath = scipdirpath;
end
% The function setSCIPSDPPath determines the path to the SCIPSDP installation and
% prompts user input, if no path is found.
function scipsdppath = setSCIPSDPPath()
scipsdppath = [];
% include directories
fprintf('\nSearching for SCIPSDP install ...\n');
% check environment variables
fprintf('Checking environment variable SCIPSDPDIR ...\n');
if (~isempty(getenv('SCIPSDPDIR')))
fprintf('SCIPSDPDIR variable found.\n')
scipsdpdirpath = getenv('SCIPSDPDIR');
scipsdpdirpath = strrep(scipsdpdirpath, '\', '/');
scipsdppath = checkScipSDPDir(scipsdpdirpath);
end
if (isempty(scipsdppath))
yesno = input('SCIPSDP not found! Would you like to specify a custom install location? (y/n)', 's');
if (strcmp(yesno,'y'))
fprintf('Please specify SCIPSDP directory:');
scipsdpdirpath = uigetdir();
scipsdpdirpath = strrep(scipsdpdirpath, '\', '/');
scipsdppath = checkScipSDPDir(scipsdpdirpath);
end
end
if(isempty(scipsdppath))
error('No SCIPSDP installation found!');
end
% This function checks if the specified SCIPSDP directory can be used as a valid SCIPSDP
% installtion.
function scipsdppath = checkScipSDPDir(scipsdpdirpath)
if (~(exist([scipsdpdirpath '/src'], 'dir')))
fprintf('\nCould not identify src directory: \n');
fprintf('Structure of specified SCIPSDP directory ''')
fprintf(scipdirpath)
fprintf(''' not recognized.\n')
scipsdppath = [];
else
fprintf(['\nSetting SCIPSDP directory to ''' scipsdpdirpath '''.\n']);
scipsdppath = scipsdpdirpath;
end
% The function setIncludeSCIP uses the SCIP path to set the correct include
% directory and determine the installation method used.
function [inc, installversion] = setIncludeSCIP(scippath)
% include directories
if (exist([scippath '/obj'], 'dir'))
fprintf('\nSCIP build using ''make'' detected.\n');
installversion = 1;
if (strcmp(computer, 'GLNXA64'))
inc = {'scip/Include',[scippath '/src'],[scippath '/src/blockmemshell'],[scippath '/obj/shared/O.linux.x86_64.gnu.opt/include']};
else
inc = {'scip/Include',[scippath '/src'],[scippath '/src/blockmemshell']};
end
elseif (exist([scippath '/build'], 'dir'))
fprintf('\nSCIP build using ''cmake'' detected.\n');
installversion = 2;
inc = {'scip/Include',[scippath '/src'],[scippath '/src/blockmemshell']};
elseif (exist([scippath '/../build'], 'dir'))
fprintf('\nSCIPOPT build using ''cmake'' detected.\n');
installversion = 3;
inc = {'scip/Include',[scippath '/src'],[scippath '/src/blockmemshell']};
elseif (exist([scippath '/lib'], 'dir'))
fprintf('\nSCIP install using ''make install'' or ''.exe'' detected.\n');
installversion = 4;
inc = {'scip/Include',[scippath '/include'],[scippath '/include/blockmemshell']};
end
% Check if the automatically chosen directory contains the include files and prompt for user input if not
if ~checkDir(inc(2:length(inc)))
yesno = input('\nNo include files found in default location. Would you like to manually specify the location of the SCIP include directory? (y/n)', 's');
if strcmp(yesno, 'y')
scippath = uigetdir();
installversion = 5;
inc = {'scip/Include',scippath,[scippath '/blockmemshell']};
if ~checkDir(inc(2:length(inc)))
error('No include files found in specified directory.');
end
else
error('No include files found in default directory.');
end
end
% Ensure that the specified paths cannot break due to whitespaces
for i = 2:length(inc)
inc{i} = ['''' inc{i} ''''];
end
% The function setIncludeSCIPSDP uses the SCIPSDP path to set the correct include
% directory and determine the installation method used.
function [inc, sdpinstallversion] = setIncludeSCIPSDP(scipsdppath)
%Include Directories
if (exist([scipsdppath '/obj'], 'dir'))
fprintf('SCIPSDP install using ''make'' detected.\n');
sdpinstallversion = 1;
inc = {'scip/Include',[scipsdppath '/src'],[scipsdppath '/src/scipsdp'],[scipsdppath '/src/sdpi']};
elseif (exist([scipsdppath '/build'], 'dir'))
fprintf('SCIPSDP build using ''cmake'' detected.\n');
sdpinstallversion = 2;
inc = {'scip/Include',[scipsdppath '/src'],[scipsdppath '/src/scipsdp'],[scipsdppath '/src/sdpi']};
end
% Check if the automatically chosen directory contains the include files and prompt for user input if not
if ~checkDir(inc(2:length(inc)))
yesno = input('\nNo include files found in default location. Would you like to manually specify the location of the SCIPSDP include directory? (y/n)', 's');
if strcmp(yesno, 'y')
scipsdppath = uigetdir();
sdpinstallversion = 3;
inc = {'scip/Include', scipsdppath, [scipsdppath '/scipsdp'], [scipsdppath '/sdpi']};
if ~checkDir(inc(2:length(inc)))
error('No include files found in specified directory.');
end
else
error('No include files found in default directory.');
end
end
% Ensure that the specified paths cannot break due to whitespaces
for i = 2:length(inc)
inc{i} = ['''' inc{i} ''''];
end
% This functions checks if the given path contains all directories we expect
function [defaultFound] = checkDir(dirLoc)
defaultFound = false;
if (~(isempty(dirLoc)))
defaultFound = true;
for i = 1:length(dirLoc)
if ~(exist(sprintf('%s', dirLoc{i}), 'dir'))
defaultFound = false;
end
end
end
% The function setLibraries uses the scippath and the identified
% installation version to setup the correct libraries for the installation
% process.
function [lib, expre] = setLibraries(scippath, scipsdppath, expre, installversion, sdpinstallversion)
% lib names [static libraries to link against]
switch(installversion)
case 1
scipLibPath = [scippath '/lib/shared/'];
case 2
scipLibPath = [scippath '/build/lib/'];
case 3
scipLibPath = [scippath '/../build/lib/'];
case {4,5}
scipLibPath = [scippath '/lib/'];
end
% Check if the automatically chosen directory contains the library folder and prompt for user input if not
[defaultFound, installversion] = checkScipLib(installversion, scipLibPath);
if ~defaultFound
yesno = input('\nNo lib folder found in default location. Would you like to manually specify the location of the SCIP library? (y/n)', 's');
if strcmp(yesno, 'y')
scipLibPath = uigetdir();
[defaultFound, installversion] = checkScipLib(installversion, scipLibPath);
if ~defaultFound
error(['No SCIP library found in' scipLibPath])
end
else
error('No SCIP library found in default location.')
end
end
% Format the library link
if isOctave()
scipLibPath = ['"' scipLibPath '"'];
scipLibOctPath = ['''' scipLibPath ''''];
lib = ['-L' scipLibOctPath];
else
scipLibPath = ['''' scipLibPath ''''];
lib = ['-L' scipLibPath];
end
% Append the appropriate library name
lib = sprintf('%s -l%s',lib,'scip');
switch(sdpinstallversion)
case 1
scipsdpLibPath = [scipsdppath '/lib/shared/'];
case 2
scipsdpLibPath = [scipsdppath '/build/lib/'];
case 3
scipsdpLibPath = [scippath '/lib/'];
end
% Check if the automatically chosen directory contains the library folder and prompt for user input if not
[defaultFound, installversion] = checkScipsdpLib(sdpinstallversion, scipsdpLibPath);
if ~defaultFound
yesno = input('\nNo lib folder found in default location. Would you like to manually specify the location of the SCIPSDP library? (y/n)', 's');
if strcmp(yesno, 'y')
scipsdpLibPath = uigetdir();
[defaultFound, ~] = checkScipsdpLib(installversion, scipsdpLibPath);
if ~defaultFound
error(['No SCIPSDP library found in' scipsdpLibPath])
end
else
error('No SCIPSDP library found in default location.')
end
end
% Format the library link
if isOctave()
scipsdpLibPath = ['"' scipsdpLibPath '"'];
scipsdpLibOctPath = ['''' scipsdpLibPath ''''];
lib = [lib ' -L' scipsdpLibOctPath];
else
scipsdpLibPath = ['''' scipsdpLibPath ''''];
lib = [lib ' -L' scipsdpLibPath];
end
lib = sprintf('%s -l%s',lib,'scipsdp');
if strcmp(mexext, 'mexw64')
% as a work around for a problem with freezing code on windows, we compile a debug version
expre = [expre ' -g'];
end
% add define for old SCIP versions
expre = [expre ' -DNO_CONFIG_HEADER'];
lib = [lib ' '];
switch(computer)
case 'GLNXA64'
expre = [expre ' LDFLAGS=''-Wl,-rpath,' scipLibPath ' -Wl,-rpath,' scipsdpLibPath ''' '];
case {'x86_64-pc-linux-gnu', 'x86_64-w64-mingw32'}
expre = [expre ' ''-Wl,-rpath,' scipLibPath ' -Wl,-rpath,' scipsdpLibPath ''' '];
end
% This functions checks if the given path contains all directories we expect
function [defaultFound, installversion] = checkScipLib(installversion, libLoc)
defaultFound = false;
if installversion == 5
yesno = input('\nWas the specified library build using cmake? (y/n)', 's');
if (strcmp(yesno,'n'))
installversion = 1;
end
end
if (exist([libLoc '/libscip.so'], 'file') || exist([libLoc '/libscip.lib'], 'file') || exist([libLoc '/libscip.dll'], 'file'))
defaultFound = true;
end
% This functions checks if the given path contains all directories we expect
function [defaultFound, installversion] = checkScipsdpLib(installversion, libLoc)
defaultFound = false;
if (exist( [libLoc '/libscipsdp.so'], 'file') || exist([libLoc '/libscipsdp.dll'], 'file'))
defaultFound = true;
end
% The function setCXXVersion allows users to specify a gcc compiler on linux
% versions of the installation.
function cxx_custom = setCXXVersion(specifyCPP)
cxx_custom='';
if specifyCPP
cxx_custom = input('Please specify which compiler to use: ', 's');
cxx_custom = [ ' GCC=''' cxx_custom ''' '];
fprintf(['Setting ''' cxx_custom '''\n'])
elseif(isempty(specifyCPP))
fprintf('\n');
yesno = input('Would you like to specify a non-default GNU C++-Compiler (e.g. ''/usr/bin/gcc-8'')? (y/n)', 's');
if (strcmp(yesno,'y'))
cxx_custom = input('Please specify which compiler to use: ', 's');
cxx_custom = [ ' GCC=''' cxx_custom ''' '];
fprintf(['Setting ''' cxx_custom '''\n'])
else
fprintf('Using default.')
end
end
% This function checks the Matlab version.
function matlabVerCheck()
fprintf('\n- Checking MATLAB version and operating system ...\n');
mver = ver('MATLAB');
% Sometimes we get multiple products here (no idea why), ensure we have MATLAB
if (length(mver) > 1)
for i = 1:length(mver)
if (strcmp(mver(i).Name, 'MATLAB'))
mver = mver(i);
break;
end
end
end
vv = regexp(mver.Version,'\.','split');
if(str2double(vv{1}) < 8)
if(str2double(vv{2}) < 12)
if(str2double(vv{2}) < 10)
error('MATLAB 2011a or above is required to run the Interface - sorry!');
else %2010a/b/sp1
fprintf(2,'Matlab-SCIP Interface is designed for MATLAB 2011a or above.\nIt will install into 2010a, but you may experience reliability problems.\nPlease upgrade to R2011a or later.\n');
end
end
end
switch(mexext)
case 'mexw32'
error('32bit Windows Installations are not supported.');
case 'mexw64'
fprintf('MATLAB %s 64bit (Windows x64) detected.\n',mver.Release);
case 'mexa64'
fprintf('MATLAB %s 64bit (Linux x64) detected.\n',mver.Release);
otherwise
error('The interface cannot be compiled for your operating system - sorry!');
end
% This function checks release versions.
function OK = mexFileCheck(localVer)
fprintf('\n- Checking MEX file release information ...\n');
% check whether the current OPTI version matches the mex files
[mexFilesOK, mexBuildVer] = checkMexFileVersion(localVer, false);
if (mexFilesOK == false)
% one or more mex files are out of date, report to the user
if (isnan(mexBuildVer))
fprintf(2,'MEX file is not compatible with this version of the Interface.\n');
else
fprintf(2,'MEX file is not compatible with this version of the Interface (MEX v%.2f vs Interface v%.2f).\n', mexBuildVer, localVer);
end
error('MEX file incompatible.')
else
% All up to date, nothing to check
fprintf('MEX files match interface release.\n');
OK = true;
end
% This function checks the version of the MEX file.
function [OK,buildVer] = checkMexFileVersion(localVer, verbose)
OK = true;
buildVer = localVer;
mexFile = optiSolver('onlyscip');
% Now search through and compare version info
for i = 1:length(mexFile)
if (~any(strcmpi(mexFile{i},{'matlab'})))
try
[~,optiBuildVer] = feval(lower(mexFile{i}));
catch
% Initially lots of mex files in the wild which don't support
% the second arg, assume out of date.
OK = false;
buildVer = NaN;
if (verbose)
fprintf('Error evaluating MEX file: %s.\n', lower(mexFile{i}));
end
return;
end
if (optiBuildVer ~= localVer)
if (optiBuildVer > localVer) % unusual case where user has newer mex files than opti source
fprintf(2,'MEX files are for a newer version of the interface.');
fprintf(2,'Please update your version of the interface.');
error('Install Error: Interface is out of date.');
else
OK = false;
buildVer = optiBuildVer;
if (verbose)
fprintf('MEX File ''%s.%s'' is out of date (MEX v%.2f vs Interface v%.2f).\n',mexFile{i},mexext, optiBuildVer, localVer);
else
break; % no point continuing check if not displaying
end
end
end
end
end
function in = bool2yn(val)
if (isempty(val) || val == true)
in = 'y';
else
in = 'n';
end
% This function returns true if we are running Octave.
function retval = isOctave()
persistent cacheval; % speeds up repeated calls
if isempty (cacheval)
cacheval = (exist ("OCTAVE_VERSION", "builtin") > 0);
end
retval = cacheval;