0001 function solution = solveCobraNLP(NLPproblem,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 global CBT_NLP_SOLVER
0089 if (~isempty(CBT_NLP_SOLVER))
0090 solver = CBT_NLP_SOLVER;
0091 else
0092 error('No solver found. call changeCobraSolver(solverName)');
0093 end
0094
0095 optParamNames = {'printLevel','warning','checkNaN','PbName', ...
0096 'iterationLimit', 'logFile'};
0097 parameters = '';
0098 if nargin ~=1
0099 if mod(length(varargin),2)==0
0100 for i=1:2:length(varargin)-1
0101 if ismember(varargin{i},optParamNames)
0102 parameters.(varargin{i}) = varargin{i+1};
0103 else
0104 error([varargin{i} ' is not a valid optional parameter']);
0105 end
0106 end
0107 elseif strcmp(varargin{1},'default')
0108 parameters = 'default';
0109 elseif isstruct(varargin{1})
0110 parameters = varargin{1};
0111 else
0112 display('Warning: Invalid number of parameters/values')
0113 solution=[];
0114 return;
0115 end
0116 end
0117 [printLevel warning] = getCobraSolverParams('NLP',{'printLevel','warning'},parameters);
0118
0119
0120 [A,lb,ub] = deal(NLPproblem.A,NLPproblem.lb,NLPproblem.ub);
0121 if isfield(NLPproblem,'csense')
0122 [b, csense] = deal(NLPproblem.b, NLPproblem.csense);
0123 elseif isfield(NLPproblem,'b_U')
0124 [b_L,b_U] = deal(NLPproblem.b_L,NLPproblem.b_U);
0125 else
0126 display('either .b_U, .b_L or .b, .csense must be defined')
0127 end
0128
0129 if isfield(NLPproblem, 'objFunction')
0130 objFunction = NLPproblem.objFunction;
0131 elseif isfield(NLPproblem, 'c')
0132 c = NLPproblem.c;
0133 else
0134 display('either .objFunction or .c must be defined')
0135 end
0136 if isfield(NLPproblem,'x0')
0137 x0 = NLPproblem.x0;
0138 else
0139 x0 = [];
0140 end
0141
0142
0143 t_start = clock;
0144
0145 switch solver
0146 case 'matlab'
0147
0148 A1 = [A(csense == 'L',:);-A(csense == 'G',:)];
0149 b1 = [b(csense == 'L'),-b(csense == 'G')];
0150
0151 A2 = A(csense == 'E',:);
0152 b2 = b(csense == 'E');
0153
0154 options.nIter = 100000;
0155
0156 [x,f,origStat,output,lambda] = fmincon(objFunction,x0,A1,b1,A2,b2,lb,ub,[],options,varargin);
0157
0158
0159 if (origStat > 0)
0160 stat = 1;
0161 y = lambda.eqlin;
0162 elseif (origStat < 0)
0163 stat = 0;
0164 else
0165 stat = -1;
0166 end
0167 case 'tomlab_snopt'
0168
0169
0170
0171 [checkNaN, PbName, iterationLimit, logFile] = ...
0172 getCobraSolverParams('NLP',{'checkNaN','PbName', 'iterationLimit', 'logFile'},parameters);
0173 if isfield(NLPproblem,'gradFunction')
0174 gradFunction = NLPproblem.gradFunction;
0175 else
0176 gradFunction = [];
0177 end
0178 if isfield(NLPproblem,'H')
0179 H = NLPproblem.H;
0180 else
0181 H = [];
0182 end
0183 if isfield(NLPproblem,'fLowBnd')
0184 fLowBnd = NLPproblem.fLowBnd;
0185 else
0186 fLowBnd = [];
0187 end
0188 if isfield(NLPproblem,'d')
0189 d = NLPproblem.d;
0190 else
0191 d = [];
0192 end
0193 if isfield(NLPproblem,'dd')
0194 dd = NLPproblem.dd;
0195 else
0196 dd = [];
0197 end
0198 if isfield(NLPproblem,'d2d')
0199 d2d = NLPproblem.d2c;
0200 else
0201 d2d = [];
0202 end
0203 if isfield(NLPproblem,'d_L')
0204 d_L = NLPproblem.d_L;
0205 else
0206 d_L = [];
0207 end
0208 if isfield(NLPproblem,'d_U')
0209 d_U = NLPproblem.d_U;
0210 else
0211 d_U = [];
0212 end
0213 if isfield(NLPproblem,'userParams')
0214 userParams = NLPproblem.userParams;
0215 else
0216 userParams = [];
0217 end
0218 if isfield(NLPproblem,'optParams')
0219 optParams = NLPproblem.optParams;
0220 else
0221 optParams = [];
0222 end
0223 if isfield(NLPproblem,'SOL'), Prob.SOL = NLPproblem.SOL; end
0224
0225 x_L = lb;
0226 x_U = ub;
0227 if ~exist('b_L','var')
0228 if (~isempty(csense))
0229 b_L(csense == 'E') = b(csense == 'E');
0230 b_U(csense == 'E') = b(csense == 'E');
0231 b_L(csense == 'G') = b(csense == 'G');
0232 b_U(csense == 'G') = 1e6;
0233 b_L(csense == 'L') = -1e6;
0234 b_U(csense == 'L') = b(csense == 'L');
0235 else
0236 b_L = b;
0237 b_U = b;
0238 end
0239 end
0240
0241
0242 HessPattern = [];
0243 pSepFunc = [];
0244 ConsPattern = [];
0245 x_min = []; x_max = [];
0246 f_opt = []; x_opt = [];
0247
0248 if exist('c', 'var')
0249 Prob = lpconAssign(c, x_L, x_U, PbName, x0,...
0250 A, b_L, b_U,...
0251 d, dd, d2d, ConsPattern, d_L, d_U,...
0252 fLowBnd, x_min, x_max, f_opt, x_opt);
0253 else
0254 f = objFunction;
0255 g = gradFunction;
0256 Prob = conAssign(f, g, H, HessPattern, x_L, x_U, PbName, x0, ...
0257 pSepFunc, fLowBnd, ...
0258 A, b_L, b_U, d, dd, d2d, ConsPattern, d_L, d_U, ...
0259 x_min, x_max, f_opt, x_opt);
0260 end
0261 Prob.user = userParams;
0262 Prob.optParam = optParams;
0263 Prob.Warning = warning;
0264 Prob.SOL.optPar(35) = iterationLimit;
0265 Prob.SOL.optPar(30) = 1e9;
0266 Prob.CheckNaN = checkNaN;
0267
0268 Prob.SOL.PrintFile = strcat(logFile, '_iterations.txt');
0269 Prob.SOL.SummFile = strcat(logFile, '_summary.txt');
0270
0271 if printLevel >= 1
0272 Prob.optParam.IterPrint = 1;
0273 end
0274 if printLevel >=3
0275 Prob.PriLevOpt = 1;
0276
0277 end
0278
0279
0280 Result = tomRun('snopt', Prob, printLevel);
0281
0282
0283 x = Result.x_k;
0284 f = Result.f_k;
0285
0286 origStat = Result.Inform;
0287 w = Result.v_k(1:length(lb));
0288 y = Result.v_k((length(lb)+1):end);
0289 if (origStat >= 1) && (origStat <= 3)
0290 stat = 1;
0291 elseif (origStat >= 11) && (origStat <= 14)
0292 stat = 0;
0293 elseif (origStat == 21 || origStat == 22)
0294 stat = 2;
0295 else
0296 stat = -1;
0297 end
0298 save ttt
0299 otherwise
0300 error(['Unknown solver: ' solver]);
0301 end
0302
0303
0304 t = etime(clock, t_start);
0305 [solution.full,solution.obj,solution.rcost,solution.dual, ...
0306 solution.solver,solution.stat,solution.origStat,solution.time] = ...
0307 deal(x,f,w,y,solver,stat,origStat,t);
0308 if strcmp(solver,'tomlab_snopt')
0309 solution.origSolStruct = Result;
0310 end