1 | function output = calllindo_nlp(interfacedata) |
---|
2 | |
---|
3 | global MY_LICENSE_FILE |
---|
4 | |
---|
5 | persistent iEnv |
---|
6 | |
---|
7 | % Instead of calling lindo, we define the parameters we need. This is |
---|
8 | % needed to speed up repeated calls |
---|
9 | %lindo |
---|
10 | LSERR_NO_ERROR = 0000; |
---|
11 | LS_IPARAM_NLP_PRINTLEVEL = 203; |
---|
12 | LS_IPARAM_NLP_SOLVER = 201; |
---|
13 | LS_IPARAM_NLP_MAXLOCALSEARCH = 221; |
---|
14 | LS_STATUS_OPTIMAL = 1; |
---|
15 | LS_STATUS_BASIC_OPTIMAL = 2; |
---|
16 | LS_STATUS_INFEASIBLE = 3; |
---|
17 | LS_STATUS_UNBOUNDED = 4; |
---|
18 | LS_STATUS_FEASIBLE = 5; |
---|
19 | LS_STATUS_INFORUNB = 6; |
---|
20 | LS_STATUS_NEAR_OPTIMAL = 7; |
---|
21 | LS_STATUS_LOCAL_OPTIMAL = 8; |
---|
22 | LS_STATUS_LOCAL_INFEASIBLE = 9; |
---|
23 | LS_STATUS_CUTOFF = 10; |
---|
24 | LS_STATUS_NUMERICAL_ERROR = 11; |
---|
25 | LS_STATUS_UNKNOWN = 12; |
---|
26 | LS_STATUS_UNLOADED = 13; |
---|
27 | LS_STATUS_LOADED = 14; |
---|
28 | LS_METHOD_FREE = 0; |
---|
29 | LS_METHOD_PSIMPLEX = 1; |
---|
30 | LS_METHOD_DSIMPLEX = 2; |
---|
31 | LS_METHOD_BARRIER = 3; |
---|
32 | LS_METHOD_NLP = 4; |
---|
33 | LS_NMETHOD_FREE = 4; |
---|
34 | LS_NMETHOD_CONOPT = 7; |
---|
35 | LS_NMETHOD_MSW_GRG = 9; |
---|
36 | |
---|
37 | if isempty(iEnv) |
---|
38 | % This call is mighty slow, so we do it only once, unless uses clears |
---|
39 | % everything |
---|
40 | [MY_LICENSE_KEY,Err] = mxlindo('LSloadLicenseString',MY_LICENSE_FILE); |
---|
41 | [iEnv,nErr]=mxlindo('LScreateEnv',MY_LICENSE_KEY); |
---|
42 | if nErr ~= LSERR_NO_ERROR;output = returnempty(-5); return; end; |
---|
43 | end |
---|
44 | |
---|
45 | % Retrieve needed data |
---|
46 | options = interfacedata.options; |
---|
47 | F_struc = interfacedata.F_struc; |
---|
48 | c = interfacedata.c; |
---|
49 | K = interfacedata.K; |
---|
50 | x0 = interfacedata.x0; |
---|
51 | Q = interfacedata.Q; |
---|
52 | lb = interfacedata.lb; |
---|
53 | ub = interfacedata.ub; |
---|
54 | monomtable = interfacedata.monomtable; |
---|
55 | |
---|
56 | % Do some pre-calc to be used in callbacks |
---|
57 | nonlinearindicies = find(interfacedata.variabletype~=0); |
---|
58 | nonlinearindicies = unionstripped(nonlinearindicies,interfacedata.evalVariables); |
---|
59 | linearindicies = find(interfacedata.variabletype==0); |
---|
60 | linearindicies = setdiff1D(linearindicies,nonlinearindicies); |
---|
61 | interfacedata.nonlinearindicies = nonlinearindicies; |
---|
62 | interfacedata.linearindicies = linearindicies; |
---|
63 | |
---|
64 | % Init model size |
---|
65 | m = K.l + K.f; |
---|
66 | n = length(c); |
---|
67 | |
---|
68 | % Specifying variable types... |
---|
69 | vtype = repmat('C',1,length(c(linearindicies))); |
---|
70 | vtype(interfacedata.integer_variables) = 'I'; |
---|
71 | |
---|
72 | oshift = interfacedata.f; |
---|
73 | |
---|
74 | if m == 0 |
---|
75 | interfacedata.F_struc = [1e6 -ones(1,length(c))]; |
---|
76 | K.l = 1; |
---|
77 | F_struc = [1e6 -ones(1,length(c))]; |
---|
78 | m = 1; |
---|
79 | csense = [repmat('E',1,K.f) repmat('L',1,K.l)]; |
---|
80 | end |
---|
81 | |
---|
82 | [Nbegcol,Nlencol,Nrowndx] = lindosparse(ones(m,length(linearindicies))); |
---|
83 | oJacobian = ones(length(linearindicies),1); |
---|
84 | Nobjndx = find(oJacobian) - 1; |
---|
85 | Nobjcnt = length(Nobjndx); |
---|
86 | if isempty(Nobjndx) |
---|
87 | Nobjndx = []; |
---|
88 | end |
---|
89 | |
---|
90 | %[Nbegcol,Nlencol,Nrowndx,Nobjcnt,Nobjndx,Apatt] = jacSparsityGeometric(interfacedata); |
---|
91 | % A = -F_struc(:,1+linearindicies); |
---|
92 | % b = full(F_struc(:,1)); |
---|
93 | csense = [repmat('E',1,K.f) repmat('L',1,K.l)]; |
---|
94 | % A = A.*(~Apatt); |
---|
95 | % b(any(Apatt,2)) = 0; |
---|
96 | |
---|
97 | [iModel,nErr]=mxlindo('LScreateModel',iEnv); |
---|
98 | if nErr ~= LSERR_NO_ERROR;output = returnempty(11); return; end; |
---|
99 | |
---|
100 | % SETUP data for callbacks |
---|
101 | mt = interfacedata.monomtable; |
---|
102 | linear_variables = find((sum(abs(mt),2)==1) & (any(mt==1,2))); |
---|
103 | nonlinear_variables = setdiff((1:size(mt,1))',linear_variables); |
---|
104 | sigmonial_variables = find(any(0>mt,2) | any(mt-fix(mt),2)); |
---|
105 | extended_variables = interfacedata.extended_variables; |
---|
106 | |
---|
107 | [prob,problem] = yalmip2geometric(options,F_struc,c,Q,K,ub,lb,mt,linear_variables,extended_variables); |
---|
108 | prob.interfacedata = interfacedata; |
---|
109 | if problem |
---|
110 | output = createoutput([],[],[],problem,'LINDO',[],[],0); |
---|
111 | return |
---|
112 | end |
---|
113 | lindo_fungp([],[],[],[],[],[],prob); |
---|
114 | |
---|
115 | [nErr] = mxlindo('LSsetFuncalc', iModel, 'lindo_fungp',prob); |
---|
116 | if nErr ~= LSERR_NO_ERROR;output = returnempty(11); return; end; |
---|
117 | |
---|
118 | [nErr] = mxlindo('LSsetModelIntParameter', iModel, LS_IPARAM_NLP_PRINTLEVEL, options.verbose+1); |
---|
119 | if nErr ~= LSERR_NO_ERROR;output = returnempty(11); return; end; |
---|
120 | |
---|
121 | % Set NLP solver |
---|
122 | [nErr] = mxlindo('LSsetModelIntParameter', iModel, LS_IPARAM_NLP_SOLVER, eval(options.lindo.LS_IPARAM_NLP_SOLVER)); |
---|
123 | [nErr] = mxlindo('LSsetModelIntParameter', iModel, LS_IPARAM_NLP_MAXLOCALSEARCH,options.lindo.LS_IPARAM_NLP_MAXLOCALSEARCH); |
---|
124 | |
---|
125 | % Load the LP portion of model |
---|
126 | if ~isempty(lb) |
---|
127 | lb = lb(linear_variables); |
---|
128 | ind = find(lb<1e-2); |
---|
129 | lb(ind) = exp(log(1e-2)+(lb(ind)-1e-2)/1e-2); |
---|
130 | lb = log(lb+sqrt(eps)); |
---|
131 | end |
---|
132 | if ~isempty(ub) |
---|
133 | ub = ub(linear_variables); |
---|
134 | ind = find(ub<1e-2); |
---|
135 | ub(ind) = exp(log(1e-2)+(ub(ind)-1e-2)/1e-2); |
---|
136 | ub = log(ub+sqrt(eps)); |
---|
137 | end |
---|
138 | b = zeros(m,1); |
---|
139 | A = spalloc(m,length(linearindicies),0); |
---|
140 | [nErr] = mxlindo('LSXloadLPData', iModel, 1, 0, zeros(length(linearindicies),1),b, csense,A, lb, ub); |
---|
141 | if nErr ~= LSERR_NO_ERROR;output = createoutput(11); return; end; |
---|
142 | |
---|
143 | nErr = mxlindo('LSloadVarType',iModel,vtype); |
---|
144 | if nErr ~= LSERR_NO_ERROR;output = createoutput(11); return; end; |
---|
145 | |
---|
146 | % Load the NLP portion of the model |
---|
147 | [nErr] = mxlindo('LSloadNLPData', iModel, Nbegcol, Nlencol,[], Nrowndx, Nobjcnt,Nobjndx,[]); |
---|
148 | if nErr ~= LSERR_NO_ERROR;output = createoutput(11); return; end; |
---|
149 | |
---|
150 | % Optimize model |
---|
151 | solvertime = clock; |
---|
152 | |
---|
153 | if isempty(interfacedata.integer_variables) |
---|
154 | solver = 2; |
---|
155 | else |
---|
156 | solver = 1; |
---|
157 | end |
---|
158 | solvertime = clock; |
---|
159 | switch solver |
---|
160 | case 1 |
---|
161 | [solstat,nErr] = mxlindo('LSsolveMIP', iModel); |
---|
162 | if ~ismember(solstat,[2009 LS_STATUS_INFEASIBLE]) |
---|
163 | [x,nErr] = mxlindo('LSgetMIPPrimalSolution',iModel); |
---|
164 | else |
---|
165 | x = zeros(length(linearindicies),1); |
---|
166 | end |
---|
167 | case 2 |
---|
168 | [solstat,nErr] = mxlindo('LSoptimize', iModel, eval(options.lindo.LS_METHOD)); |
---|
169 | if ~ismember(solstat,[2009 LS_STATUS_INFEASIBLE]) |
---|
170 | [x,nErr] = mxlindo('LSgetPrimalSolution',iModel); |
---|
171 | else |
---|
172 | x = zeros(length(linearindicies),1); |
---|
173 | end |
---|
174 | case 3 |
---|
175 | [solStatus,nErr] = mxlindo('LSsolveGOP', iModel); |
---|
176 | [x,nErr] = mxlindo('LSgetPrimalSolution',iModel); |
---|
177 | otherwise |
---|
178 | end |
---|
179 | if interfacedata.getsolvertime solvertime = etime(clock,solvertime);else solvertime = 0;end |
---|
180 | |
---|
181 | w = zeros(length(c),1);w(linearindicies) = exp(x); |
---|
182 | y = []; |
---|
183 | |
---|
184 | %[nErr]=mxlindo('LSdeleteEnv',iEnv); |
---|
185 | [nErr]=mxlindo('LSdeleteModel',iModel); |
---|
186 | |
---|
187 | switch solstat |
---|
188 | case {LS_STATUS_OPTIMAL,LS_STATUS_BASIC_OPTIMAL,7,8} |
---|
189 | problem = 0; |
---|
190 | case {LS_STATUS_INFEASIBLE} |
---|
191 | problem = 1; |
---|
192 | case {LS_STATUS_UNBOUNDED} |
---|
193 | problem = 2; |
---|
194 | otherwise |
---|
195 | problem = 11; |
---|
196 | end |
---|
197 | infostr = yalmiperror(problem,'LINDO-QP'); |
---|
198 | |
---|
199 | % Save all data sent to solver? |
---|
200 | if options.savesolverinput |
---|
201 | solverinput.solstat = solstat; |
---|
202 | solverinput.nErr = nErr; |
---|
203 | solverinput.x = x; |
---|
204 | else |
---|
205 | solverinput = []; |
---|
206 | end |
---|
207 | |
---|
208 | % Save all data from the solver? |
---|
209 | if options.savesolveroutput |
---|
210 | solveroutput.x = x; |
---|
211 | solveroutput.fmin = fmin; |
---|
212 | solveroutput.flag = flag; |
---|
213 | solveroutput.output=output; |
---|
214 | solveroutput.lambda=lambda; |
---|
215 | else |
---|
216 | solveroutput = []; |
---|
217 | end |
---|
218 | |
---|
219 | % Standard interface |
---|
220 | output = createoutput(w,y,[],problem,'LINDO',solverinput,solveroutput,solvertime); |
---|