Skip to content

Commit 859f717

Browse files
committed
Merge pull request #94 from tpfau/CPLEXMILP
Added functionality to use ibm CPLEX as MILP Solver and fixed an issue with the clone log file of ibm_cplex
2 parents 8a8689c + 0921982 commit 859f717

File tree

3 files changed

+92
-10
lines changed

3 files changed

+92
-10
lines changed

solvers/changeCobraSolver.m

+7
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@
198198
warning('MILP solver CPLEX through Tomlab not usable: tomRun.m not in Matlab path');
199199
solverOK = false;
200200
end
201+
case 'ibm_cplex'
202+
try
203+
ILOGcplex = Cplex('fba');% Initialize the CPLEX object
204+
catch ME
205+
solverOK = false;
206+
warning('MILP solver CPLEX from IBM not usable: IBM CPLEX not installed or licence server not up');
207+
end
201208
case 'glpk'
202209
if (~exist('glpkmex'))
203210
warning('MILP solver glpk not usable: glpkmex not in Matlab path');

solvers/solveCobraLP.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,9 @@
10601060
end
10611061
%1 = (Simplex or Barrier) Optimal solution is available.
10621062
stat=origStat;
1063-
if exist('clone1.log','file')
1064-
delete('clone1.log')
1065-
end
1063+
if exist([pwd filesep 'clone1.log'],'file')
1064+
delete('clone1.log')
1065+
end
10661066
case 'lindo'
10671067
%%
10681068
error('Solver type lindo is obsolete - use lindo_new or lindo_old instead');

solvers/solveCobraMILP.m

+82-7
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@
5959

6060
% Markus Herrgard 1/23/07
6161
% Tim Harrington 05/18/12 Added support for the Gurobi 5.0 solver
62-
% <<<<<<< HEAD
6362
% Ronan (16/07/2013) default MPS parameters are no longer global variables
64-
% =======
6563
% Meiyappan Lakshmanan 11/14/14 Added support for the cplex_direct solver
6664
% cplex_direct solver accesible through CPLEX m-file and CPLEX C-interface
67-
% >>>>>>> 987a38606db2c6c47ed5421155258bb27fd34cbe
65+
% Thomas Pfau (12/11/2015) Added support for ibm_cplex (the IBM Matlab
66+
% interface) to the solvers.
6867

6968
%% Process options
7069

@@ -161,6 +160,8 @@
161160
parametersStructureFlag=0;
162161
[minNorm, printLevel, primalOnlyFlag, saveInput, feasTol, optTol] = ...
163162
getCobraSolverParams('LP',optParamNames(1:6));
163+
%parameters will later be accessed and should be initialized.
164+
parameters = '';
164165
end
165166

166167

@@ -348,8 +349,76 @@
348349
else
349350
solStat = -1; % Solution not optimal or solver problem
350351
end
352+
case 'ibm_cplex'
353+
cplexlp = Cplex();
354+
if (~isempty(csense))
355+
b_L(csense == 'E') = b(csense == 'E');
356+
b_U(csense == 'E') = b(csense == 'E');
357+
b_L(csense == 'G') = b(csense == 'G');
358+
b_U(csense == 'G') = inf;
359+
b_L(csense == 'L') = -inf;
360+
b_U(csense == 'L') = b(csense == 'L');
361+
elseif isfield(MILPproblem, 'b_L') && isfield(MILPproblem, 'b_U')
362+
b_L = MILPproblem.b_L;
363+
b_U = MILPproblem.b_U;
364+
else
365+
b_L = b;
366+
b_U = b;
367+
end
368+
intVars = (vartype == 'B') | (vartype == 'I');
369+
%intVars
370+
%pause;
371+
cplexlp.Model.A = A;
372+
cplexlp.Model.rhs = b_U;
373+
cplexlp.Model.lhs = b_L;
374+
cplexlp.Model.ub = ub;
375+
cplexlp.Model.lb = lb;
376+
cplexlp.Model.obj = osense * c;
377+
cplexlp.Model.name = 'CobraMILP';
378+
cplexlp.Model.ctype = vartype';
379+
cplexlp.Start.x = x0;
380+
cplexlp.Param.mip.tolerances.mipgap.Cur = solverParams.relMipGapTol;
381+
cplexlp.Param.mip.tolerances.integrality.Cur = solverParams.intTol;
382+
cplexlp.Param.timelimit.Cur = solverParams.timeLimit;
383+
cplexlp.Param.output.writelevel.Cur = solverParams.printLevel;
351384

352-
case 'gurobi5'
385+
outputfile = fopen(solverParams.logFile,'a');
386+
cplexlp.DisplayFunc = @redirect;
387+
388+
cplexlp.Param.simplex.tolerances.optimality.Cur = solverParams.optTol;
389+
cplexlp.Param.mip.tolerances.absmipgap.Cur = solverParams.absMipGapTol;
390+
cplexlp.Param.simplex.tolerances.feasibility.Cur = solverParams.feasTol;
391+
% Strict numerical tolerances
392+
cplexlp.Param.emphasis.numerical.Cur = solverParams.NUMERICALEMPHASIS;
393+
save('MILPProblem','cplexlp')
394+
395+
% Set up callback to print out intermediate solutions
396+
% only set this up if you know that you actually need these
397+
% results. Otherwise do not specify intSolInd and contSolInd
398+
399+
% Solve problem
400+
Result = cplexlp.solve();
401+
402+
% Get results
403+
x = Result.x;
404+
f = osense*Result.objval;
405+
stat = Result.status;
406+
if (stat == 101 || stat == 102 || stat == 1)
407+
solStat = 1; % Opt integer within tolerance
408+
elseif (stat == 103 || stat == 3)
409+
solStat = 0; % Integer infeas
410+
elseif (stat == 118 || stat == 119 || stat == 2)
411+
solStat = 2; % Unbounded
412+
elseif (stat == 106 || stat == 106 || stat == 108 || stat == 110 || stat == 112 || stat == 114 || stat == 117)
413+
solStat = -1; % No integer solution exists
414+
else
415+
solStat = 3; % Other problem, but integer solution exists
416+
end
417+
if exist([pwd filesep 'clone1.log'],'file')
418+
delete('clone1.log')
419+
end
420+
421+
case {'gurobi5','gurobi6'}
353422
%% gurobi 5
354423
% Free academic licenses for the Gurobi solver can be obtained from
355424
% http://www.gurobi.com/html/academic.html
@@ -417,9 +486,6 @@
417486
solStat = 2; % Unbounded
418487
elseif strcmp(resultgurobi.status,'INF_OR_UNBD')
419488
solStat = 0; % Gurobi reports infeasible *or* unbounded
420-
elseif strcmp(resultgurobi.status,'SUBOPTIMAL')
421-
solStat = -1; % Suboptimal solution
422-
[x,f] = deal(resultgurobi.x,resultgurobi.objval);
423489
else
424490
solStat = -1; % Solution not optimal or solver problem
425491
end
@@ -589,6 +655,7 @@
589655
%[solution] = BuildMPS(Ale, ble, Aeq, beq, c, lb, ub, PbName,'MPSfilename',MPSfilename,'EleNames',EleNames,'EqtNames',EqtNames,'VarNames',VarNames);
590656

591657
return
658+
592659
otherwise
593660
error(['Unknown solver: ' solver]);
594661
end
@@ -616,3 +683,11 @@
616683
end
617684
end
618685

686+
%% Redirection function such that cplex redirects its output to the defined outputfile.
687+
function redirect(l)
688+
% Write the line of log output
689+
fprintf(outputfile, '%s\n', l);
690+
end
691+
692+
end
693+

0 commit comments

Comments
 (0)