Skip to content

Commit

Permalink
Merge pull request #2326 from rmtfleming/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
rmtfleming authored Sep 29, 2024
2 parents 46b2dc7 + f99b1cc commit ef80589
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 35 deletions.
6 changes: 3 additions & 3 deletions src/analysis/FBA/optimizeCbModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@

% Solve initial LP
if allowLoops
solution = solveCobraLP(optProblem);
solution = solveCobraLP(optProblem, param);
else
MILPproblem = addLoopLawConstraints(optProblem, model, 1:nRxns);
solution = solveCobraMILP(MILPproblem);
Expand Down Expand Up @@ -566,7 +566,7 @@
optProblem2.osense = 1;
% Re-solve the problem
if allowLoops
solution = solveCobraLP(optProblem2);
solution = solveCobraLP(optProblem2, param);
else
MILPproblem2 = addLoopLawConstraints(optProblem2, model, 1:nRxns);
solution = solveCobraMILP(MILPproblem2);
Expand Down Expand Up @@ -622,7 +622,7 @@
optProblem2.osense = 1;

% Re-solve the problem
solution = solveCobraLP(optProblem2);
solution = solveCobraLP(optProblem2, param);


elseif length(minNorm)> 1 || minNorm > 0
Expand Down
4 changes: 2 additions & 2 deletions src/analysis/wholeBody/PSCMToolbox/optimizeWBModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
% parts of the objective are always assumed to be
% minimised.
%
% * solverName: Solver name {'tomlab_cplex','ibm_cplex','cplex_direct'}
% * solverName: Solver name {'tomlab_cplex','ibm_cplex','cplex_direct','mosek'}
%
% * printLevel: verbose level
% * if `0`, warnings and errors are silenced. (default: 0)
Expand Down Expand Up @@ -126,7 +126,7 @@
param.verify = 0;
end

validatedSolvers={'tomlab_cplex','ibm_cplex','cplex_direct', 'gurobi'};
validatedSolvers={'tomlab_cplex','ibm_cplex','cplex_direct', 'gurobi','cplex','mosek'};

if 1
%mlb = magnitude of a large bound
Expand Down
2 changes: 1 addition & 1 deletion src/base/io/utilities/getDefinedFieldProperties.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
relrows = cellfun(@(x) ischar(x) && ~isempty(x),raw.Model_Field);
relarray = [raw.Model_Field(relrows),raw.Xdim(relrows),raw.Ydim(relrows),raw.Evaluator(relrows),raw.Default_Value(relrows),raw.BasicFields(relrows),raw.FieldBasisType,raw.FBAFields(relrows)];
progInfo = cell(0,8);
for i = 1:size(relarray)
for i = 1:length(relarray)
xval = relarray{i,2};
if ~isnumeric(xval)
xnumval = str2num(xval);
Expand Down
29 changes: 29 additions & 0 deletions src/base/solvers/cplex/setCplexParametersForProblem.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
% solverParams: the solver specific parameter structure has to be compatible with `setCplexParam`
% problemType: The type of Problem ('LP','MILP','QP','MIQP').
%
% see https://www.ibm.com/docs/en/icos/12.10.0?topic=cplex-list-parameters
% see https://www.ibm.com/docs/en/icos/12.10.0

%set the default parameters so we can see what they are
cplexProblem.setDefault;
Expand Down Expand Up @@ -84,6 +86,7 @@
cplexProblem.Param.timelimit.Cur = cobraParams.timeLimit;
end


if strcmp(problemType,'QP') || strcmp(problemType,'MIQP')
switch cobraParams.method
case -1 % automatic
Expand All @@ -103,6 +106,32 @@
end
end

if isfield(solverParams,'lpmethod')
%https://www.ibm.com/docs/en/icos/12.10.0?topic=parameters-algorithm-continuous-linear-problems
% Value Symbol Meaning
% 0 CPX_ALG_AUTOMATIC Automatic: let CPLEX choose; default
% 1 CPX_ALG_PRIMAL Primal simplex
% 2 CPX_ALG_DUAL Dual simplex
% 3 CPX_ALG_NET Network simplex
% 4 CPX_ALG_BARRIER Barrier
% 5 CPX_ALG_SIFTING Sifting
% 6 CPX_ALG_CONCURRENT Concurrent (Dual, Barrier, and Primal in opportunistic parallel mode; Dual and Barrier in deterministic parallel mode)
cplexProblem.Param.lpmethod.Cur=solverParams.lpmethod;
%cplexProblem.Param.qpmethod.Cur='CPX_ALG_PRIMAL';
else
cplexProblem.Param.lpmethod.Cur=3;%best benchmark performance on Harvetta
end

if isfield(solverParams,'timelimit')
%https://www.ibm.com/docs/en/icos/12.10.0?topic=parameters-optimizer-time-limit-in-seconds
cplexProblem.Param.timelimit.Cur = solverParams.timelimit;
end


if isfield(solverParams,'printLevel')
solverParams=rmfield(solverParams,'printLevel');
end

% Set IBM-Cplex-specific parameters. Will overide Cobra solver parameters
cplexProblem = setCplexParam(cplexProblem, solverParams);

Expand Down
4 changes: 3 additions & 1 deletion src/base/solvers/getSetSolver/changeCobraSolver.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@
% (usually matlabinstall/toolboxes/local/startup.m)
%


if strcmp(solverName,'cplex')
solverName='ibm_cplex';
end
global SOLVERS;
global CBTDIR;
global OPT_PROB_TYPES;
Expand Down
7 changes: 3 additions & 4 deletions src/base/solvers/msk/parseMskResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
origStat = res.sol.itr.solsta;
%disp(origStat)
switch origStat
case {'OPTIMAL','MSK_SOL_STA_OPTIMAL','MSK_SOL_STA_NEAR_OPTIMAL','UNKNOWN'}
case {'OPTIMAL','MSK_SOL_STA_OPTIMAL','MSK_SOL_STA_NEAR_OPTIMAL'}
if strcmp(res.rcodestr,'MSK_RES_TRM_STALL')
warning('Mosek stalling, returning solution as it may be almost optimal')
else
Expand Down Expand Up @@ -130,9 +130,8 @@
warning(['Unrecognised solsta: ' origStat])
stat=-1; %some other problem
end
end

if isfield(res.sol,'bas') && ~isequal(res.sol.bas.solsta,'UNKNOWN') %dont overwite interior point solution
elseif isfield(res.sol,'bas')
%&& ~isequal(res.sol.bas.solsta,'UNKNOWN') %dont overwite interior point solution
origStat = res.sol.bas.solsta;
switch origStat
case {'OPTIMAL','MSK_SOL_STA_OPTIMAL','MSK_SOL_STA_NEAR_OPTIMAL'}
Expand Down
46 changes: 25 additions & 21 deletions src/base/solvers/solveCobraLP.m
Original file line number Diff line number Diff line change
Expand Up @@ -642,44 +642,45 @@

param = solverParams;
% only set the print level if not already set via solverParams structure
if ~isfield(param, 'MSK_IPAR_LOG')
if ~isfield(solverParams, 'MSK_IPAR_LOG')
switch problemTypeParams.printLevel
case 0
echolev = 0;
case 1
echolev = 3;
case 2
param.MSK_IPAR_LOG_INTPNT = 1;
param.MSK_IPAR_LOG_SIM = 1;
solverParams.MSK_IPAR_LOG_INTPNT = 1;
solverParams.MSK_IPAR_LOG_SIM = 1;
echolev = 3;
otherwise
echolev = 0;
end
if echolev == 0
param.MSK_IPAR_LOG = 0;
solverParams.MSK_IPAR_LOG = 0;
cmd = ['minimize echo(' int2str(echolev) ')'];
else
cmd = 'minimize';
end
end

%https://docs.mosek.com/8.1/toolbox/solving-linear.html
if ~isfield(param, 'MSK_DPAR_INTPNT_TOL_PFEAS')
param.MSK_DPAR_INTPNT_TOL_PFEAS=problemTypeParams.feasTol;
if ~isfield(solverParams, 'MSK_DPAR_INTPNT_TOL_PFEAS')
solverParams.MSK_DPAR_INTPNT_TOL_PFEAS=problemTypeParams.feasTol;
end
if ~isfield(param, 'MSK_DPAR_INTPNT_TOL_DFEAS.')
param.MSK_DPAR_INTPNT_TOL_DFEAS=problemTypeParams.feasTol;
if ~isfield(solverParams, 'MSK_DPAR_INTPNT_TOL_DFEAS.')
solverParams.MSK_DPAR_INTPNT_TOL_DFEAS=problemTypeParams.feasTol;
end
%If the feasibility tolerance is changed by the solverParams
%struct, this needs to be forwarded to the cobra Params for the
%final consistency test!
if isfield(param,'MSK_DPAR_INTPNT_TOL_PFEAS')
problemTypeParams.feasTol = param.MSK_DPAR_INTPNT_TOL_PFEAS;
if isfield(solverParams,'MSK_DPAR_INTPNT_TOL_PFEAS')
problemTypeParams.feasTol = solverParams.MSK_DPAR_INTPNT_TOL_PFEAS;
end

% basis reuse - TODO
% http://docs.mosek.com/7.0/toolbox/A_guided_tour.html#section-node-_A%20guided%20tour_Advanced%20start%20%28hot-start%29

% Syntax: [res] = msklpopt(c,a,blc,buc,blx,bux,param,cmd)
% Syntax: [res] = msklpopt(c,a,blc,buc,blx,bux,solverParams,cmd)
%
% Purpose: Solves the optimization problem
%
Expand All @@ -696,7 +697,7 @@
% buc Upper bounds on constraints.
% blx Lower bounds on variables.
% bux Upper bounds on variables.
% param New MOSEK parameters.
% solverParams New MOSEK parameters.
% cmd MOSEK commands.
%
% blc=[] and buc=[] means that the
Expand All @@ -712,7 +713,7 @@
blc(csense == 'L') = -inf;

if 0
[res] = msklpopt(osense * c, A, blc, buc, lb, ub, param, cmd);
[res] = msklpopt(osense * c, A, blc, buc, lb, ub, solverParams, cmd);
% res.sol.itr
% min(buc(csense == 'E')-A((csense == 'E'),:)*res.sol.itr.xx)
% min(A((csense == 'E'),:)*res.sol.itr.xx-blc(csense == 'E'))
Expand All @@ -733,9 +734,8 @@
prob.sol.bas.xx = basis.xx;
end

% Use the primal simplex optimizer.
param.MSK_IPAR_OPTIMIZER = 'MSK_OPTIMIZER_PRIMAL_SIMPLEX';
[rcode,res] = mosekopt(cmd,prob,param);

[rcode,res] = mosekopt(cmd,prob,solverParams);
end

%parse mosek result structure
Expand Down Expand Up @@ -1374,11 +1374,14 @@
else
stat = -1;
end

% cplexStatus analyzes the CPLEX output Inform code and returns
% the CPLEX solution status message in ExitText and the TOMLAB exit flag
% in ExitFlag
[origStatText, ~] = cplexStatus(origStat);
if 0
% cplexStatus analyzes the CPLEX output Inform code and returns
% the CPLEX solution status message in ExitText and the TOMLAB exit flag
% in ExitFlag
[origStatText, ~] = cplexStatus(origStat);
else
origStat = CplexLPproblem.Solution.statusstring;
end

switch CplexLPproblem.Param.lpmethod.Cur
case 0
Expand Down Expand Up @@ -1560,6 +1563,7 @@
fprintf(' > The interface to ''mps'' from solveCobraLP is not supported anymore.\n -> Instead use >> writeCbModel(model, ''mps'');\n');
% temporary legacy support
writeLPProblem(LPproblem,'fileName','LP.mps','solverParams',solverParams);
stat = 1;
otherwise
if isempty(solver)
error('There is no solver for LP problems available');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
% * .biomassBool - Boolean of biomass reaction
% * .DMRxnBool - Boolean of demand reactions. Prefix `DM_` (optional field)
% * .SinkRxnBool - Boolean of sink reactions. Prefix `sink_` (optional field)
% * .ExchRxnBool - Boolean of exchange reactions. Prefix `EX_` or `Exch_` or `Ex_` (optional field)
% * .ExchRxnBool - Boolean of exchange reactions. Prefix `EX_` or `Exch_` or `Ex_` or 'Excretion_EX' (optional field)
%
% .. Author: - Ronan Fleming

Expand Down Expand Up @@ -129,10 +129,12 @@
model.ExchRxnBool=model.ExchRxnBool | strcmp('x',model.rxnComps);
end
% models with typical COBRA abbreviations - heuristic
model.ExchRxnBool=strncmp('EX_', model.rxns, 3)==1 | strncmp('Exch_', model.rxns, 5)==1 | strncmp('Ex_', model.rxns, 5)==1 | biomassBool | model.ExchRxnBool;
model.ExchRxnBool=strncmp('EX_', model.rxns, 3)==1 | strncmp('Exch_', model.rxns, 5)==1 | strncmp('Ex_', model.rxns, 5)==1 ...
| strncmp('Diet_EX_', model.rxns, 8)==1 | strncmp('Excretion_EX_', model.rxns, 12)==1 | biomassBool | model.ExchRxnBool;
else
% models with typical COBRA abbreviations - heuristic
model.ExchRxnBool=strncmp('EX_', model.rxns, 3)==1 | strncmp('Exch_', model.rxns, 5)==1 | strncmp('Ex_', model.rxns, 5)==1 | biomassBool;
model.ExchRxnBool=strncmp('EX_', model.rxns, 3)==1 | strncmp('Exch_', model.rxns, 5)==1 | strncmp('Ex_', model.rxns, 5)==1 ...
| strncmp('Diet_EX_', model.rxns, 8)==1 | strncmp('Excretion_EX_', model.rxns, 12)==1 | biomassBool;
end
%demand reactions going out of model
model.DMRxnBool=strncmp('DM_', model.rxns, 3)==1;
Expand Down

0 comments on commit ef80589

Please sign in to comment.