solveCobraNLP

PURPOSE ^

solveCobraNLP Solves a COBRA non-linear (objective and/or constraints)

SYNOPSIS ^

function solution = solveCobraNLP(NLPproblem,varargin)

DESCRIPTION ^

solveCobraNLP Solves a COBRA non-linear (objective and/or constraints)
problem.

 solution = solveCobraNLP(NLPproblem,varargin)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function solution = solveCobraNLP(NLPproblem,varargin)
0002 %solveCobraNLP Solves a COBRA non-linear (objective and/or constraints)
0003 %problem.
0004 %
0005 % solution = solveCobraNLP(NLPproblem,varargin)
0006 
0007 % Solves a problem of the following form:
0008 %     min objFunction(x) or c'*x
0009 %     st.       A*x  <=> b   or b_L < A*x < b_U
0010 %        and    d_L < d(x) < d_U
0011 %     where A is a matrix, d(x) is an optional function and the objective
0012 %     is either a general function or a linear function.
0013 %
0014 %INPUT
0015 % NLPproblem  Non-linear optimization problem
0016 %  Required Fields
0017 %   A               LHS matrix
0018 %   b               RHS vector
0019 %   lb              Lower bounds
0020 %   ub              Upper bounds
0021 %   csense          Constraint senses ('L','E','G')
0022 %   objFunction     Function to evaluate as the objective.  Input as string
0023 %       or
0024 %   c               linear objective such that c*x is minimized.
0025 %  Note: 'b_L' and 'b_U' can be used in place of 'b' and 'csense'
0026 %
0027 %  Optional Fields
0028 %   x0              Initial solution
0029 %   gradFunction    Name of the function that computes the n x 1 gradient
0030 %                   vector (ignored if 'd' is set).
0031 %   H               Name of the function that computes the n x n Hessian
0032 %                   matrix
0033 %   fLowBnd         A lower bound on the function value at optimum.
0034 %   d               Name of function that computes the mN nonlinear
0035 %                   constraints
0036 %   dd              Name of function that computes the constraint Jacobian
0037 %                   mN x n
0038 %   d2d             Name of function that computes the second part of the
0039 %                   Lagrangian function (only needed for some solvers)
0040 %   d_L             Lower bound vector in nonlinear constraints
0041 %   d_U             Upper bound vector in nonlinear constraints
0042 %   userParams      Solver specific user parameters structure
0043 %   optParams       Solver specific optional parameters structure
0044 %
0045 %OPTIONAL INPUTS
0046 %(If using matlab solver)
0047 %   varargin Any additional arguments to the 'objFunction' function
0048 %
0049 %(for other solvers)
0050 % Optional parameters can be entered using parameters structure or as
0051 % parameter followed by parameter value: i.e. ,'printLevel',3)
0052 %
0053 % parameters    Structure containing optional parameters as fields.
0054 %               Setting parameters = 'default' uses default setting set in
0055 %               getCobraSolverParameters.
0056 % printLevel    Printing level
0057 %               = 0    Silent (Default)
0058 %               = 1    Warnings and Errors
0059 %               = 2    Summary information
0060 %               = 3    More detailed information
0061 %               > 10   Pause statements, and maximal printing (debug mode)
0062 % checkNaN      Check for NaN elements (Default = false)
0063 % PbName        NLP problem name (Default = NLP problem)
0064 %
0065 %OUTPUT
0066 % solution Structure containing the following fields describing an NLP
0067 % solution
0068 %  full             Full LP solution vector
0069 %  obj              Objective value
0070 %  rcost            Reduced costs
0071 %  dual             Dual solution
0072 %  solver           Solver used to solve LP problem
0073 %
0074 %  stat             Solver status in standardized form
0075 %                   1   Optimal solution
0076 %                   2   Unbounded solution
0077 %                   0   Infeasible
0078 %                   -1  No solution reported (timelimit, numerical
0079 %                       problem etc)
0080 %
0081 %  origStat         Original status returned by the specific solver
0082 %  time             Solve time in seconds
0083 %  origSolStruct    Original solution structure
0084 %
0085 % Markus Herrgard 12/7/07
0086 % Richard Que (02/10/10) Added tomlab_snopt support.
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 %deal variables
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 % Solvers
0145 switch solver
0146     case 'matlab'
0147         %% fmincon
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         %Assign Results
0159         if (origStat > 0)
0160             stat = 1; % Optimal solution found
0161             y = lambda.eqlin;
0162         elseif (origStat < 0)
0163             stat = 0; % Infeasible
0164         else
0165             stat = -1; % Solution did not converge
0166         end
0167     case 'tomlab_snopt'
0168         %% tomlab_snopt
0169         
0170         %get settings
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         %settings
0242         HessPattern = []; 
0243         pSepFunc = [];
0244         ConsPattern = [];
0245         x_min = []; x_max = [];
0246         f_opt = [];  x_opt = [];
0247         
0248         if exist('c', 'var') % linear objective function
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 % general objective function
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; %This is major iteration limit.
0265         Prob.SOL.optPar(30) = 1e9; %this is the minor iteration limit.  Essentially unlimited
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         %Call Solver
0280         Result = tomRun('snopt', Prob, printLevel);
0281         
0282         % Assign results
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 %% Assign Solution
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

Generated on Thu 21-Jun-2012 15:39:23 by m2html © 2003