1 | // Copyright (C) 2002, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | |
---|
4 | /* |
---|
5 | Authors |
---|
6 | |
---|
7 | John Forrest |
---|
8 | |
---|
9 | */ |
---|
10 | #ifndef ClpSimplex_H |
---|
11 | #define ClpSimplex_H |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | #include <cfloat> |
---|
15 | #include "ClpModel.hpp" |
---|
16 | #include "ClpMatrixBase.hpp" |
---|
17 | #include "ClpSolve.hpp" |
---|
18 | class ClpDualRowPivot; |
---|
19 | class ClpPrimalColumnPivot; |
---|
20 | class ClpFactorization; |
---|
21 | class CoinIndexedVector; |
---|
22 | class ClpNonLinearCost; |
---|
23 | class ClpNodeStuff; |
---|
24 | class CoinStructuredModel; |
---|
25 | class OsiClpSolverInterface; |
---|
26 | class CoinWarmStartBasis; |
---|
27 | class ClpDisasterHandler; |
---|
28 | class ClpConstraint; |
---|
29 | |
---|
30 | /** This solves LPs using the simplex method |
---|
31 | |
---|
32 | It inherits from ClpModel and all its arrays are created at |
---|
33 | algorithm time. Originally I tried to work with model arrays |
---|
34 | but for simplicity of coding I changed to single arrays with |
---|
35 | structural variables then row variables. Some coding is still |
---|
36 | based on old style and needs cleaning up. |
---|
37 | |
---|
38 | For a description of algorithms: |
---|
39 | |
---|
40 | for dual see ClpSimplexDual.hpp and at top of ClpSimplexDual.cpp |
---|
41 | for primal see ClpSimplexPrimal.hpp and at top of ClpSimplexPrimal.cpp |
---|
42 | |
---|
43 | There is an algorithm data member. + for primal variations |
---|
44 | and - for dual variations |
---|
45 | |
---|
46 | */ |
---|
47 | |
---|
48 | class ClpSimplex : public ClpModel { |
---|
49 | friend void ClpSimplexUnitTest(const std::string & mpsDir); |
---|
50 | |
---|
51 | public: |
---|
52 | /** enums for status of various sorts. |
---|
53 | First 4 match CoinWarmStartBasis, |
---|
54 | isFixed means fixed at lower bound and out of basis |
---|
55 | */ |
---|
56 | enum Status { |
---|
57 | isFree = 0x00, |
---|
58 | basic = 0x01, |
---|
59 | atUpperBound = 0x02, |
---|
60 | atLowerBound = 0x03, |
---|
61 | superBasic = 0x04, |
---|
62 | isFixed = 0x05 |
---|
63 | }; |
---|
64 | // For Dual |
---|
65 | enum FakeBound { |
---|
66 | noFake = 0x00, |
---|
67 | bothFake = 0x01, |
---|
68 | upperFake = 0x02, |
---|
69 | lowerFake = 0x03 |
---|
70 | }; |
---|
71 | |
---|
72 | /**@name Constructors and destructor and copy */ |
---|
73 | //@{ |
---|
74 | /// Default constructor |
---|
75 | ClpSimplex (bool emptyMessages = false ); |
---|
76 | |
---|
77 | /** Copy constructor. May scale depending on mode |
---|
78 | -1 leave mode as is |
---|
79 | 0 -off, 1 equilibrium, 2 geometric, 3, auto, 4 dynamic(later) |
---|
80 | */ |
---|
81 | ClpSimplex(const ClpSimplex & rhs, int scalingMode =-1); |
---|
82 | /** Copy constructor from model. May scale depending on mode |
---|
83 | -1 leave mode as is |
---|
84 | 0 -off, 1 equilibrium, 2 geometric, 3, auto, 4 dynamic(later) |
---|
85 | */ |
---|
86 | ClpSimplex(const ClpModel & rhs, int scalingMode=-1); |
---|
87 | /** Subproblem constructor. A subset of whole model is created from the |
---|
88 | row and column lists given. The new order is given by list order and |
---|
89 | duplicates are allowed. Name and integer information can be dropped |
---|
90 | Can optionally modify rhs to take into account variables NOT in list |
---|
91 | in this case duplicates are not allowed (also see getbackSolution) |
---|
92 | */ |
---|
93 | ClpSimplex (const ClpModel * wholeModel, |
---|
94 | int numberRows, const int * whichRows, |
---|
95 | int numberColumns, const int * whichColumns, |
---|
96 | bool dropNames=true, bool dropIntegers=true, |
---|
97 | bool fixOthers=false); |
---|
98 | /** Subproblem constructor. A subset of whole model is created from the |
---|
99 | row and column lists given. The new order is given by list order and |
---|
100 | duplicates are allowed. Name and integer information can be dropped |
---|
101 | Can optionally modify rhs to take into account variables NOT in list |
---|
102 | in this case duplicates are not allowed (also see getbackSolution) |
---|
103 | */ |
---|
104 | ClpSimplex (const ClpSimplex * wholeModel, |
---|
105 | int numberRows, const int * whichRows, |
---|
106 | int numberColumns, const int * whichColumns, |
---|
107 | bool dropNames=true, bool dropIntegers=true, |
---|
108 | bool fixOthers=false); |
---|
109 | /** This constructor modifies original ClpSimplex and stores |
---|
110 | original stuff in created ClpSimplex. It is only to be used in |
---|
111 | conjunction with originalModel */ |
---|
112 | ClpSimplex (ClpSimplex * wholeModel, |
---|
113 | int numberColumns, const int * whichColumns); |
---|
114 | /** This copies back stuff from miniModel and then deletes miniModel. |
---|
115 | Only to be used with mini constructor */ |
---|
116 | void originalModel(ClpSimplex * miniModel); |
---|
117 | /** Array persistence flag |
---|
118 | If 0 then as now (delete/new) |
---|
119 | 1 then only do arrays if bigger needed |
---|
120 | 2 as 1 but give a bit extra if bigger needed |
---|
121 | */ |
---|
122 | void setPersistenceFlag(int value); |
---|
123 | #ifdef CLP_AUXILIARY_MODEL |
---|
124 | /** |
---|
125 | If you are re-using the same matrix again and again then the setup time |
---|
126 | to do scaling may be significant. Also you may not want to initialize all values |
---|
127 | or return all values (especially if infeasible). While an auxiliary model exists |
---|
128 | it will be faster. If options -1 then model is switched off. Otherwise switched on |
---|
129 | with following options. |
---|
130 | 1 - rhs is constant |
---|
131 | 2 - bounds are constant |
---|
132 | 4 - objective is constant |
---|
133 | 8 - solution in by basis and no djs etc in |
---|
134 | 16 - no duals out (but reduced costs) |
---|
135 | 32 - no output if infeasible |
---|
136 | */ |
---|
137 | void auxiliaryModel(int options); |
---|
138 | /// Switch off e.g. if people using presolve |
---|
139 | void deleteAuxiliaryModel(); |
---|
140 | /// See if we have auxiliary model |
---|
141 | inline bool usingAuxiliaryModel() const |
---|
142 | { return auxiliaryModel_!=NULL;} |
---|
143 | #endif |
---|
144 | /// Save a copy of model with certain state - normally without cuts |
---|
145 | void makeBaseModel(); |
---|
146 | /// Switch off base model |
---|
147 | void deleteBaseModel(); |
---|
148 | /// See if we have base model |
---|
149 | inline ClpSimplex * baseModel() const |
---|
150 | { return baseModel_;} |
---|
151 | /** Reset to base model (just size and arrays needed) |
---|
152 | If model NULL use internal copy |
---|
153 | */ |
---|
154 | void setToBaseModel(ClpSimplex * model=NULL); |
---|
155 | /// Assignment operator. This copies the data |
---|
156 | ClpSimplex & operator=(const ClpSimplex & rhs); |
---|
157 | /// Destructor |
---|
158 | ~ClpSimplex ( ); |
---|
159 | // Ones below are just ClpModel with some changes |
---|
160 | /** Loads a problem (the constraints on the |
---|
161 | rows are given by lower and upper bounds). If a pointer is 0 then the |
---|
162 | following values are the default: |
---|
163 | <ul> |
---|
164 | <li> <code>colub</code>: all columns have upper bound infinity |
---|
165 | <li> <code>collb</code>: all columns have lower bound 0 |
---|
166 | <li> <code>rowub</code>: all rows have upper bound infinity |
---|
167 | <li> <code>rowlb</code>: all rows have lower bound -infinity |
---|
168 | <li> <code>obj</code>: all variables have 0 objective coefficient |
---|
169 | </ul> |
---|
170 | */ |
---|
171 | void loadProblem ( const ClpMatrixBase& matrix, |
---|
172 | const double* collb, const double* colub, |
---|
173 | const double* obj, |
---|
174 | const double* rowlb, const double* rowub, |
---|
175 | const double * rowObjective=NULL); |
---|
176 | void loadProblem ( const CoinPackedMatrix& matrix, |
---|
177 | const double* collb, const double* colub, |
---|
178 | const double* obj, |
---|
179 | const double* rowlb, const double* rowub, |
---|
180 | const double * rowObjective=NULL); |
---|
181 | |
---|
182 | /** Just like the other loadProblem() method except that the matrix is |
---|
183 | given in a standard column major ordered format (without gaps). */ |
---|
184 | void loadProblem ( const int numcols, const int numrows, |
---|
185 | const CoinBigIndex* start, const int* index, |
---|
186 | const double* value, |
---|
187 | const double* collb, const double* colub, |
---|
188 | const double* obj, |
---|
189 | const double* rowlb, const double* rowub, |
---|
190 | const double * rowObjective=NULL); |
---|
191 | /// This one is for after presolve to save memory |
---|
192 | void loadProblem ( const int numcols, const int numrows, |
---|
193 | const CoinBigIndex* start, const int* index, |
---|
194 | const double* value,const int * length, |
---|
195 | const double* collb, const double* colub, |
---|
196 | const double* obj, |
---|
197 | const double* rowlb, const double* rowub, |
---|
198 | const double * rowObjective=NULL); |
---|
199 | /** This loads a model from a coinModel object - returns number of errors. |
---|
200 | If keepSolution true and size is same as current then |
---|
201 | keeps current status and solution |
---|
202 | */ |
---|
203 | int loadProblem ( CoinModel & modelObject,bool keepSolution=false); |
---|
204 | /// Read an mps file from the given filename |
---|
205 | int readMps(const char *filename, |
---|
206 | bool keepNames=false, |
---|
207 | bool ignoreErrors = false); |
---|
208 | /// Read GMPL files from the given filenames |
---|
209 | int readGMPL(const char *filename,const char * dataName, |
---|
210 | bool keepNames=false); |
---|
211 | /// Read file in LP format from file with name filename. |
---|
212 | /// See class CoinLpIO for description of this format. |
---|
213 | int readLp(const char *filename, const double epsilon = 1e-5); |
---|
214 | /** Borrow model. This is so we dont have to copy large amounts |
---|
215 | of data around. It assumes a derived class wants to overwrite |
---|
216 | an empty model with a real one - while it does an algorithm. |
---|
217 | This is same as ClpModel one, but sets scaling on etc. */ |
---|
218 | void borrowModel(ClpModel & otherModel); |
---|
219 | void borrowModel(ClpSimplex & otherModel); |
---|
220 | /// Pass in Event handler (cloned and deleted at end) |
---|
221 | void passInEventHandler(const ClpEventHandler * eventHandler); |
---|
222 | /// Puts solution back into small model |
---|
223 | void getbackSolution(const ClpSimplex & smallModel,const int * whichRow, const int * whichColumn); |
---|
224 | /** Load nonlinear part of problem from AMPL info |
---|
225 | Returns 0 if linear |
---|
226 | 1 if quadratic objective |
---|
227 | 2 if quadratic constraints |
---|
228 | 3 if nonlinear objective |
---|
229 | 4 if nonlinear constraints |
---|
230 | -1 on failure |
---|
231 | */ |
---|
232 | int loadNonLinear(void * info, int & numberConstraints, |
---|
233 | ClpConstraint ** & constraints); |
---|
234 | //@} |
---|
235 | |
---|
236 | /**@name Functions most useful to user */ |
---|
237 | //@{ |
---|
238 | /** General solve algorithm which can do presolve. |
---|
239 | See ClpSolve.hpp for options |
---|
240 | */ |
---|
241 | int initialSolve(ClpSolve & options); |
---|
242 | /// Default initial solve |
---|
243 | int initialSolve(); |
---|
244 | /// Dual initial solve |
---|
245 | int initialDualSolve(); |
---|
246 | /// Primal initial solve |
---|
247 | int initialPrimalSolve(); |
---|
248 | /// Barrier initial solve |
---|
249 | int initialBarrierSolve(); |
---|
250 | /// Barrier initial solve, not to be followed by crossover |
---|
251 | int initialBarrierNoCrossSolve(); |
---|
252 | /** Dual algorithm - see ClpSimplexDual.hpp for method. |
---|
253 | ifValuesPass==2 just does values pass and then stops. |
---|
254 | |
---|
255 | startFinishOptions - bits |
---|
256 | 1 - do not delete work areas and factorization at end |
---|
257 | 2 - use old factorization if same number of rows |
---|
258 | 4 - skip as much initialization of work areas as possible |
---|
259 | (based on whatsChanged in clpmodel.hpp) ** work in progress |
---|
260 | maybe other bits later |
---|
261 | */ |
---|
262 | int dual(int ifValuesPass=0, int startFinishOptions=0); |
---|
263 | // If using Debug |
---|
264 | int dualDebug(int ifValuesPass=0, int startFinishOptions=0); |
---|
265 | /** Primal algorithm - see ClpSimplexPrimal.hpp for method. |
---|
266 | ifValuesPass==2 just does values pass and then stops. |
---|
267 | |
---|
268 | startFinishOptions - bits |
---|
269 | 1 - do not delete work areas and factorization at end |
---|
270 | 2 - use old factorization if same number of rows |
---|
271 | 4 - skip as much initialization of work areas as possible |
---|
272 | (based on whatsChanged in clpmodel.hpp) ** work in progress |
---|
273 | maybe other bits later |
---|
274 | */ |
---|
275 | int primal(int ifValuesPass=0, int startFinishOptions=0); |
---|
276 | /** Solves nonlinear problem using SLP - may be used as crash |
---|
277 | for other algorithms when number of iterations small. |
---|
278 | Also exits if all problematical variables are changing |
---|
279 | less than deltaTolerance |
---|
280 | */ |
---|
281 | int nonlinearSLP(int numberPasses,double deltaTolerance); |
---|
282 | /** Solves problem with nonlinear constraints using SLP - may be used as crash |
---|
283 | for other algorithms when number of iterations small. |
---|
284 | Also exits if all problematical variables are changing |
---|
285 | less than deltaTolerance |
---|
286 | */ |
---|
287 | int nonlinearSLP(int numberConstraints, ClpConstraint ** constraints, |
---|
288 | int numberPasses,double deltaTolerance); |
---|
289 | /** Solves using barrier (assumes you have good cholesky factor code). |
---|
290 | Does crossover to simplex if asked*/ |
---|
291 | int barrier(bool crossover=true); |
---|
292 | /** Solves non-linear using reduced gradient. Phase = 0 get feasible, |
---|
293 | =1 use solution */ |
---|
294 | int reducedGradient(int phase=0); |
---|
295 | /// Solve using structure of model and maybe in parallel |
---|
296 | int solve(CoinStructuredModel * model); |
---|
297 | /** This loads a model from a CoinStructuredModel object - returns number of errors. |
---|
298 | If originalOrder then keep to order stored in blocks, |
---|
299 | otherwise first column/rows correspond to first block - etc. |
---|
300 | If keepSolution true and size is same as current then |
---|
301 | keeps current status and solution |
---|
302 | */ |
---|
303 | int loadProblem ( CoinStructuredModel & modelObject, |
---|
304 | bool originalOrder=true,bool keepSolution=false); |
---|
305 | /** |
---|
306 | When scaling is on it is possible that the scaled problem |
---|
307 | is feasible but the unscaled is not. Clp returns a secondary |
---|
308 | status code to that effect. This option allows for a cleanup. |
---|
309 | If you use it I would suggest 1. |
---|
310 | This only affects actions when scaled optimal |
---|
311 | 0 - no action |
---|
312 | 1 - clean up using dual if primal infeasibility |
---|
313 | 2 - clean up using dual if dual infeasibility |
---|
314 | 3 - clean up using dual if primal or dual infeasibility |
---|
315 | 11,12,13 - as 1,2,3 but use primal |
---|
316 | |
---|
317 | return code as dual/primal |
---|
318 | */ |
---|
319 | int cleanup(int cleanupScaling); |
---|
320 | /** Dual ranging. |
---|
321 | This computes increase/decrease in cost for each given variable and corresponding |
---|
322 | sequence numbers which would change basis. Sequence numbers are 0..numberColumns |
---|
323 | and numberColumns.. for artificials/slacks. |
---|
324 | For non-basic variables the information is trivial to compute and the change in cost is just minus the |
---|
325 | reduced cost and the sequence number will be that of the non-basic variables. |
---|
326 | For basic variables a ratio test is between the reduced costs for non-basic variables |
---|
327 | and the row of the tableau corresponding to the basic variable. |
---|
328 | The increase/decrease value is always >= 0.0 |
---|
329 | |
---|
330 | Up to user to provide correct length arrays where each array is of length numberCheck. |
---|
331 | which contains list of variables for which information is desired. All other |
---|
332 | arrays will be filled in by function. If fifth entry in which is variable 7 then fifth entry in output arrays |
---|
333 | will be information for variable 7. |
---|
334 | |
---|
335 | If valueIncrease/Decrease not NULL (both must be NULL or both non NULL) then these are filled with |
---|
336 | the value of variable if such a change in cost were made (the existing bounds are ignored) |
---|
337 | |
---|
338 | Returns non-zero if infeasible unbounded etc |
---|
339 | */ |
---|
340 | int dualRanging(int numberCheck,const int * which, |
---|
341 | double * costIncrease, int * sequenceIncrease, |
---|
342 | double * costDecrease, int * sequenceDecrease, |
---|
343 | double * valueIncrease=NULL, double * valueDecrease=NULL); |
---|
344 | /** Primal ranging. |
---|
345 | This computes increase/decrease in value for each given variable and corresponding |
---|
346 | sequence numbers which would change basis. Sequence numbers are 0..numberColumns |
---|
347 | and numberColumns.. for artificials/slacks. |
---|
348 | This should only be used for non-basic variabls as otherwise information is pretty useless |
---|
349 | For basic variables the sequence number will be that of the basic variables. |
---|
350 | |
---|
351 | Up to user to provide correct length arrays where each array is of length numberCheck. |
---|
352 | which contains list of variables for which information is desired. All other |
---|
353 | arrays will be filled in by function. If fifth entry in which is variable 7 then fifth entry in output arrays |
---|
354 | will be information for variable 7. |
---|
355 | |
---|
356 | Returns non-zero if infeasible unbounded etc |
---|
357 | */ |
---|
358 | int primalRanging(int numberCheck,const int * which, |
---|
359 | double * valueIncrease, int * sequenceIncrease, |
---|
360 | double * valueDecrease, int * sequenceDecrease); |
---|
361 | /** Write the basis in MPS format to the specified file. |
---|
362 | If writeValues true writes values of structurals |
---|
363 | (and adds VALUES to end of NAME card) |
---|
364 | |
---|
365 | Row and column names may be null. |
---|
366 | formatType is |
---|
367 | <ul> |
---|
368 | <li> 0 - normal |
---|
369 | <li> 1 - extra accuracy |
---|
370 | <li> 2 - IEEE hex (later) |
---|
371 | </ul> |
---|
372 | |
---|
373 | Returns non-zero on I/O error |
---|
374 | */ |
---|
375 | int writeBasis(const char *filename, |
---|
376 | bool writeValues=false, |
---|
377 | int formatType=0) const; |
---|
378 | /** Read a basis from the given filename, |
---|
379 | returns -1 on file error, 0 if no values, 1 if values */ |
---|
380 | int readBasis(const char *filename); |
---|
381 | /// Returns a basis (to be deleted by user) |
---|
382 | CoinWarmStartBasis * getBasis() const; |
---|
383 | /// Passes in factorization |
---|
384 | void setFactorization( ClpFactorization & factorization); |
---|
385 | /// Copies in factorization to existing one |
---|
386 | void copyFactorization( ClpFactorization & factorization); |
---|
387 | /** Tightens primal bounds to make dual faster. Unless |
---|
388 | fixed or doTight>10, bounds are slightly looser than they could be. |
---|
389 | This is to make dual go faster and is probably not needed |
---|
390 | with a presolve. Returns non-zero if problem infeasible. |
---|
391 | |
---|
392 | Fudge for branch and bound - put bounds on columns of factor * |
---|
393 | largest value (at continuous) - should improve stability |
---|
394 | in branch and bound on infeasible branches (0.0 is off) |
---|
395 | */ |
---|
396 | int tightenPrimalBounds(double factor=0.0,int doTight=0,bool tightIntegers=false); |
---|
397 | /** Crash - at present just aimed at dual, returns |
---|
398 | -2 if dual preferred and crash basis created |
---|
399 | -1 if dual preferred and all slack basis preferred |
---|
400 | 0 if basis going in was not all slack |
---|
401 | 1 if primal preferred and all slack basis preferred |
---|
402 | 2 if primal preferred and crash basis created. |
---|
403 | |
---|
404 | if gap between bounds <="gap" variables can be flipped |
---|
405 | ( If pivot -1 then can be made super basic!) |
---|
406 | |
---|
407 | If "pivot" is |
---|
408 | -1 No pivoting - always primal |
---|
409 | 0 No pivoting (so will just be choice of algorithm) |
---|
410 | 1 Simple pivoting e.g. gub |
---|
411 | 2 Mini iterations |
---|
412 | */ |
---|
413 | int crash(double gap,int pivot); |
---|
414 | /// Sets row pivot choice algorithm in dual |
---|
415 | void setDualRowPivotAlgorithm(ClpDualRowPivot & choice); |
---|
416 | /// Sets column pivot choice algorithm in primal |
---|
417 | void setPrimalColumnPivotAlgorithm(ClpPrimalColumnPivot & choice); |
---|
418 | /** For strong branching. On input lower and upper are new bounds |
---|
419 | while on output they are change in objective function values |
---|
420 | (>1.0e50 infeasible). |
---|
421 | Return code is 0 if nothing interesting, -1 if infeasible both |
---|
422 | ways and +1 if infeasible one way (check values to see which one(s)) |
---|
423 | Solutions are filled in as well - even down, odd up - also |
---|
424 | status and number of iterations |
---|
425 | */ |
---|
426 | int strongBranching(int numberVariables,const int * variables, |
---|
427 | double * newLower, double * newUpper, |
---|
428 | double ** outputSolution, |
---|
429 | int * outputStatus, int * outputIterations, |
---|
430 | bool stopOnFirstInfeasible=true, |
---|
431 | bool alwaysFinish=false, |
---|
432 | int startFinishOptions=0); |
---|
433 | /// Fathom - 1 if solution |
---|
434 | int fathom(void * stuff); |
---|
435 | /** Do up to N deep - returns |
---|
436 | -1 - no solution nNodes_ valid nodes |
---|
437 | >= if solution and that node gives solution |
---|
438 | ClpNode array is 2**N long. Values for N and |
---|
439 | array are in stuff (nNodes_ also in stuff) */ |
---|
440 | int fathomMany(void * stuff); |
---|
441 | /// Double checks OK |
---|
442 | double doubleCheck(); |
---|
443 | /// Starts Fast dual2 |
---|
444 | int startFastDual2(ClpNodeStuff * stuff); |
---|
445 | /// Like Fast dual |
---|
446 | int fastDual2(ClpNodeStuff * stuff); |
---|
447 | /// Stops Fast dual2 |
---|
448 | void stopFastDual2(ClpNodeStuff * stuff); |
---|
449 | /** Deals with crunch aspects |
---|
450 | mode 0 - in |
---|
451 | 1 - out with solution |
---|
452 | 2 - out without solution |
---|
453 | returns small model or NULL |
---|
454 | */ |
---|
455 | ClpSimplex * fastCrunch(ClpNodeStuff * stuff, int mode); |
---|
456 | //@} |
---|
457 | |
---|
458 | /**@name Needed for functionality of OsiSimplexInterface */ |
---|
459 | //@{ |
---|
460 | /** Pivot in a variable and out a variable. Returns 0 if okay, |
---|
461 | 1 if inaccuracy forced re-factorization, -1 if would be singular. |
---|
462 | Also updates primal/dual infeasibilities. |
---|
463 | Assumes sequenceIn_ and pivotRow_ set and also directionIn and Out. |
---|
464 | */ |
---|
465 | int pivot(); |
---|
466 | |
---|
467 | /** Pivot in a variable and choose an outgoing one. Assumes primal |
---|
468 | feasible - will not go through a bound. Returns step length in theta |
---|
469 | Returns ray in ray_ (or NULL if no pivot) |
---|
470 | Return codes as before but -1 means no acceptable pivot |
---|
471 | */ |
---|
472 | int primalPivotResult(); |
---|
473 | |
---|
474 | /** Pivot out a variable and choose an incoing one. Assumes dual |
---|
475 | feasible - will not go through a reduced cost. |
---|
476 | Returns step length in theta |
---|
477 | Returns ray in ray_ (or NULL if no pivot) |
---|
478 | Return codes as before but -1 means no acceptable pivot |
---|
479 | */ |
---|
480 | int dualPivotResult(); |
---|
481 | |
---|
482 | /** Common bits of coding for dual and primal. Return 0 if okay, |
---|
483 | 1 if bad matrix, 2 if very bad factorization |
---|
484 | |
---|
485 | startFinishOptions - bits |
---|
486 | 1 - do not delete work areas and factorization at end |
---|
487 | 2 - use old factorization if same number of rows |
---|
488 | 4 - skip as much initialization of work areas as possible |
---|
489 | (based on whatsChanged in clpmodel.hpp) ** work in progress |
---|
490 | maybe other bits later |
---|
491 | |
---|
492 | */ |
---|
493 | int startup(int ifValuesPass,int startFinishOptions=0); |
---|
494 | void finish(int startFinishOptions=0); |
---|
495 | |
---|
496 | /** Factorizes and returns true if optimal. Used by user */ |
---|
497 | bool statusOfProblem(bool initial=false); |
---|
498 | /// If user left factorization frequency then compute |
---|
499 | void defaultFactorizationFrequency(); |
---|
500 | //@} |
---|
501 | |
---|
502 | /**@name most useful gets and sets */ |
---|
503 | //@{ |
---|
504 | /// If problem is primal feasible |
---|
505 | inline bool primalFeasible() const |
---|
506 | { return (numberPrimalInfeasibilities_==0);} |
---|
507 | /// If problem is dual feasible |
---|
508 | inline bool dualFeasible() const |
---|
509 | { return (numberDualInfeasibilities_==0);} |
---|
510 | /// factorization |
---|
511 | inline ClpFactorization * factorization() const |
---|
512 | { return factorization_;} |
---|
513 | /// Sparsity on or off |
---|
514 | bool sparseFactorization() const; |
---|
515 | void setSparseFactorization(bool value); |
---|
516 | /// Factorization frequency |
---|
517 | int factorizationFrequency() const; |
---|
518 | void setFactorizationFrequency(int value); |
---|
519 | /// Dual bound |
---|
520 | inline double dualBound() const |
---|
521 | { return dualBound_;} |
---|
522 | void setDualBound(double value); |
---|
523 | /// Infeasibility cost |
---|
524 | inline double infeasibilityCost() const |
---|
525 | { return infeasibilityCost_;} |
---|
526 | void setInfeasibilityCost(double value); |
---|
527 | /** Amount of print out: |
---|
528 | 0 - none |
---|
529 | 1 - just final |
---|
530 | 2 - just factorizations |
---|
531 | 3 - as 2 plus a bit more |
---|
532 | 4 - verbose |
---|
533 | above that 8,16,32 etc just for selective debug |
---|
534 | */ |
---|
535 | /** Perturbation: |
---|
536 | 50 - switch on perturbation |
---|
537 | 100 - auto perturb if takes too long (1.0e-6 largest nonzero) |
---|
538 | 101 - we are perturbed |
---|
539 | 102 - don't try perturbing again |
---|
540 | default is 100 |
---|
541 | others are for playing |
---|
542 | */ |
---|
543 | inline int perturbation() const |
---|
544 | { return perturbation_;} |
---|
545 | void setPerturbation(int value); |
---|
546 | /// Current (or last) algorithm |
---|
547 | inline int algorithm() const |
---|
548 | {return algorithm_; } |
---|
549 | /// Set algorithm |
---|
550 | inline void setAlgorithm(int value) |
---|
551 | {algorithm_=value; } |
---|
552 | /// Return true if the objective limit test can be relied upon |
---|
553 | bool isObjectiveLimitTestValid() const ; |
---|
554 | /// Sum of dual infeasibilities |
---|
555 | inline double sumDualInfeasibilities() const |
---|
556 | { return sumDualInfeasibilities_;} |
---|
557 | inline void setSumDualInfeasibilities(double value) |
---|
558 | { sumDualInfeasibilities_=value;} |
---|
559 | /// Sum of relaxed dual infeasibilities |
---|
560 | inline double sumOfRelaxedDualInfeasibilities() const |
---|
561 | { return sumOfRelaxedDualInfeasibilities_;} |
---|
562 | inline void setSumOfRelaxedDualInfeasibilities(double value) |
---|
563 | { sumOfRelaxedDualInfeasibilities_=value;} |
---|
564 | /// Number of dual infeasibilities |
---|
565 | inline int numberDualInfeasibilities() const |
---|
566 | { return numberDualInfeasibilities_;} |
---|
567 | inline void setNumberDualInfeasibilities(int value) |
---|
568 | { numberDualInfeasibilities_=value;} |
---|
569 | /// Number of dual infeasibilities (without free) |
---|
570 | inline int numberDualInfeasibilitiesWithoutFree() const |
---|
571 | { return numberDualInfeasibilitiesWithoutFree_;} |
---|
572 | /// Sum of primal infeasibilities |
---|
573 | inline double sumPrimalInfeasibilities() const |
---|
574 | { return sumPrimalInfeasibilities_;} |
---|
575 | inline void setSumPrimalInfeasibilities(double value) |
---|
576 | { sumPrimalInfeasibilities_=value;} |
---|
577 | /// Sum of relaxed primal infeasibilities |
---|
578 | inline double sumOfRelaxedPrimalInfeasibilities() const |
---|
579 | { return sumOfRelaxedPrimalInfeasibilities_;} |
---|
580 | inline void setSumOfRelaxedPrimalInfeasibilities(double value) |
---|
581 | { sumOfRelaxedPrimalInfeasibilities_=value;} |
---|
582 | /// Number of primal infeasibilities |
---|
583 | inline int numberPrimalInfeasibilities() const |
---|
584 | { return numberPrimalInfeasibilities_;} |
---|
585 | inline void setNumberPrimalInfeasibilities(int value) |
---|
586 | { numberPrimalInfeasibilities_=value;} |
---|
587 | /** Save model to file, returns 0 if success. This is designed for |
---|
588 | use outside algorithms so does not save iterating arrays etc. |
---|
589 | It does not save any messaging information. |
---|
590 | Does not save scaling values. |
---|
591 | It does not know about all types of virtual functions. |
---|
592 | */ |
---|
593 | int saveModel(const char * fileName); |
---|
594 | /** Restore model from file, returns 0 if success, |
---|
595 | deletes current model */ |
---|
596 | int restoreModel(const char * fileName); |
---|
597 | |
---|
598 | /** Just check solution (for external use) - sets sum of |
---|
599 | infeasibilities etc. |
---|
600 | If setToBounds 0 then primal column values not changed |
---|
601 | and used to compute primal row activity values. If 1 or 2 |
---|
602 | then status used - so all nonbasic variables set to |
---|
603 | indicated bound and if any values changed (or ==2) basic values re-computed. |
---|
604 | */ |
---|
605 | void checkSolution(int setToBounds=false); |
---|
606 | /** Just check solution (for internal use) - sets sum of |
---|
607 | infeasibilities etc. */ |
---|
608 | void checkSolutionInternal(); |
---|
609 | /// Useful row length arrays (0,1,2,3,4,5) |
---|
610 | inline CoinIndexedVector * rowArray(int index) const |
---|
611 | { return rowArray_[index];} |
---|
612 | /// Useful column length arrays (0,1,2,3,4,5) |
---|
613 | inline CoinIndexedVector * columnArray(int index) const |
---|
614 | { return columnArray_[index];} |
---|
615 | //@} |
---|
616 | |
---|
617 | /******************** End of most useful part **************/ |
---|
618 | /**@name Functions less likely to be useful to casual user */ |
---|
619 | //@{ |
---|
620 | /** Given an existing factorization computes and checks |
---|
621 | primal and dual solutions. Uses input arrays for variables at |
---|
622 | bounds. Returns feasibility states */ |
---|
623 | int getSolution ( const double * rowActivities, |
---|
624 | const double * columnActivities); |
---|
625 | /** Given an existing factorization computes and checks |
---|
626 | primal and dual solutions. Uses current problem arrays for |
---|
627 | bounds. Returns feasibility states */ |
---|
628 | int getSolution (); |
---|
629 | /** Constructs a non linear cost from list of non-linearities (columns only) |
---|
630 | First lower of each column is taken as real lower |
---|
631 | Last lower is taken as real upper and cost ignored |
---|
632 | |
---|
633 | Returns nonzero if bad data e.g. lowers not monotonic |
---|
634 | */ |
---|
635 | int createPiecewiseLinearCosts(const int * starts, |
---|
636 | const double * lower, const double * gradient); |
---|
637 | /// dual row pivot choice |
---|
638 | ClpDualRowPivot * dualRowPivot() const |
---|
639 | { return dualRowPivot_;} |
---|
640 | /// Returns true if model looks OK |
---|
641 | inline bool goodAccuracy() const |
---|
642 | { return (largestPrimalError_<1.0e-7&&largestDualError_<1.0e-7);} |
---|
643 | /** Return model - updates any scalars */ |
---|
644 | void returnModel(ClpSimplex & otherModel); |
---|
645 | /** Factorizes using current basis. |
---|
646 | solveType - 1 iterating, 0 initial, -1 external |
---|
647 | If 10 added then in primal values pass |
---|
648 | Return codes are as from ClpFactorization unless initial factorization |
---|
649 | when total number of singularities is returned. |
---|
650 | Special case is numberRows_+1 -> all slack basis. |
---|
651 | */ |
---|
652 | int internalFactorize(int solveType); |
---|
653 | /// Save data |
---|
654 | ClpDataSave saveData() ; |
---|
655 | /// Restore data |
---|
656 | void restoreData(ClpDataSave saved); |
---|
657 | /// Clean up status |
---|
658 | void cleanStatus(); |
---|
659 | /// Factorizes using current basis. For external use |
---|
660 | int factorize(); |
---|
661 | /** Computes duals from scratch. If givenDjs then |
---|
662 | allows for nonzero basic djs */ |
---|
663 | void computeDuals(double * givenDjs); |
---|
664 | /// Computes primals from scratch |
---|
665 | void computePrimals ( const double * rowActivities, |
---|
666 | const double * columnActivities); |
---|
667 | /** Adds multiple of a column into an array */ |
---|
668 | void add(double * array, |
---|
669 | int column, double multiplier) const; |
---|
670 | /** |
---|
671 | Unpacks one column of the matrix into indexed array |
---|
672 | Uses sequenceIn_ |
---|
673 | Also applies scaling if needed |
---|
674 | */ |
---|
675 | void unpack(CoinIndexedVector * rowArray) const ; |
---|
676 | /** |
---|
677 | Unpacks one column of the matrix into indexed array |
---|
678 | Slack if sequence>= numberColumns |
---|
679 | Also applies scaling if needed |
---|
680 | */ |
---|
681 | void unpack(CoinIndexedVector * rowArray,int sequence) const; |
---|
682 | /** |
---|
683 | Unpacks one column of the matrix into indexed array |
---|
684 | ** as packed vector |
---|
685 | Uses sequenceIn_ |
---|
686 | Also applies scaling if needed |
---|
687 | */ |
---|
688 | void unpackPacked(CoinIndexedVector * rowArray) ; |
---|
689 | /** |
---|
690 | Unpacks one column of the matrix into indexed array |
---|
691 | ** as packed vector |
---|
692 | Slack if sequence>= numberColumns |
---|
693 | Also applies scaling if needed |
---|
694 | */ |
---|
695 | void unpackPacked(CoinIndexedVector * rowArray,int sequence); |
---|
696 | protected: |
---|
697 | /** |
---|
698 | This does basis housekeeping and does values for in/out variables. |
---|
699 | Can also decide to re-factorize |
---|
700 | */ |
---|
701 | int housekeeping(double objectiveChange); |
---|
702 | /** This sets largest infeasibility and most infeasible and sum |
---|
703 | and number of infeasibilities (Primal) */ |
---|
704 | void checkPrimalSolution(const double * rowActivities=NULL, |
---|
705 | const double * columnActivies=NULL); |
---|
706 | /** This sets largest infeasibility and most infeasible and sum |
---|
707 | and number of infeasibilities (Dual) */ |
---|
708 | void checkDualSolution(); |
---|
709 | /** This sets sum and number of infeasibilities (Dual and Primal) */ |
---|
710 | void checkBothSolutions(); |
---|
711 | public: |
---|
712 | /** For advanced use. When doing iterative solves things can get |
---|
713 | nasty so on values pass if incoming solution has largest |
---|
714 | infeasibility < incomingInfeasibility throw out variables |
---|
715 | from basis until largest infeasibility < allowedInfeasibility |
---|
716 | or incoming largest infeasibility. |
---|
717 | If allowedInfeasibility>= incomingInfeasibility this is |
---|
718 | always possible altough you may end up with an all slack basis. |
---|
719 | |
---|
720 | Defaults are 1.0,10.0 |
---|
721 | */ |
---|
722 | void setValuesPassAction(double incomingInfeasibility, |
---|
723 | double allowedInfeasibility); |
---|
724 | //@} |
---|
725 | /**@name most useful gets and sets */ |
---|
726 | //@{ |
---|
727 | public: |
---|
728 | /// Initial value for alpha accuracy calculation (-1.0 off) |
---|
729 | inline double alphaAccuracy() const |
---|
730 | { return alphaAccuracy_;} |
---|
731 | inline void setAlphaAccuracy(double value) |
---|
732 | { alphaAccuracy_ = value;} |
---|
733 | public: |
---|
734 | /// Set disaster handler |
---|
735 | inline void setDisasterHandler(ClpDisasterHandler * handler) |
---|
736 | { disasterArea_= handler;} |
---|
737 | /// Get disaster handler |
---|
738 | inline ClpDisasterHandler * disasterHandler() const |
---|
739 | { return disasterArea_;} |
---|
740 | /// Large bound value (for complementarity etc) |
---|
741 | inline double largeValue() const |
---|
742 | { return largeValue_;} |
---|
743 | void setLargeValue( double value) ; |
---|
744 | /// Largest error on Ax-b |
---|
745 | inline double largestPrimalError() const |
---|
746 | { return largestPrimalError_;} |
---|
747 | /// Largest error on basic duals |
---|
748 | inline double largestDualError() const |
---|
749 | { return largestDualError_;} |
---|
750 | /// Largest error on Ax-b |
---|
751 | inline void setLargestPrimalError(double value) |
---|
752 | { largestPrimalError_=value;} |
---|
753 | /// Largest error on basic duals |
---|
754 | inline void setLargestDualError(double value) |
---|
755 | { largestDualError_=value;} |
---|
756 | /// Basic variables pivoting on which rows |
---|
757 | inline int * pivotVariable() const |
---|
758 | { return pivotVariable_;} |
---|
759 | /// If automatic scaling on |
---|
760 | inline bool automaticScaling() const |
---|
761 | { return automaticScale_!=0;} |
---|
762 | inline void setAutomaticScaling(bool onOff) |
---|
763 | { automaticScale_ = onOff ? 1: 0;} |
---|
764 | /// Current dual tolerance |
---|
765 | inline double currentDualTolerance() const |
---|
766 | { return dualTolerance_;} |
---|
767 | inline void setCurrentDualTolerance(double value) |
---|
768 | { dualTolerance_ = value;} |
---|
769 | /// Current primal tolerance |
---|
770 | inline double currentPrimalTolerance() const |
---|
771 | { return primalTolerance_;} |
---|
772 | inline void setCurrentPrimalTolerance(double value) |
---|
773 | { primalTolerance_ = value;} |
---|
774 | /// How many iterative refinements to do |
---|
775 | inline int numberRefinements() const |
---|
776 | { return numberRefinements_;} |
---|
777 | void setNumberRefinements( int value) ; |
---|
778 | /// Alpha (pivot element) for use by classes e.g. steepestedge |
---|
779 | inline double alpha() const { return alpha_;} |
---|
780 | inline void setAlpha(double value) { alpha_ = value;} |
---|
781 | /// Reduced cost of last incoming for use by classes e.g. steepestedge |
---|
782 | inline double dualIn() const { return dualIn_;} |
---|
783 | /// Pivot Row for use by classes e.g. steepestedge |
---|
784 | inline int pivotRow() const{ return pivotRow_;} |
---|
785 | inline void setPivotRow(int value) { pivotRow_=value;} |
---|
786 | /// value of incoming variable (in Dual) |
---|
787 | double valueIncomingDual() const; |
---|
788 | //@} |
---|
789 | |
---|
790 | protected: |
---|
791 | /**@name protected methods */ |
---|
792 | //@{ |
---|
793 | /** May change basis and then returns number changed. |
---|
794 | Computation of solutions may be overriden by given pi and solution |
---|
795 | */ |
---|
796 | int gutsOfSolution ( double * givenDuals, |
---|
797 | const double * givenPrimals, |
---|
798 | bool valuesPass=false); |
---|
799 | /// Does most of deletion (0 = all, 1 = most, 2 most + factorization) |
---|
800 | void gutsOfDelete(int type); |
---|
801 | /// Does most of copying |
---|
802 | void gutsOfCopy(const ClpSimplex & rhs); |
---|
803 | /** puts in format I like (rowLower,rowUpper) also see StandardMatrix |
---|
804 | 1 bit does rows (now and columns), (2 bit does column bounds), 4 bit does objective(s). |
---|
805 | 8 bit does solution scaling in |
---|
806 | 16 bit does rowArray and columnArray indexed vectors |
---|
807 | and makes row copy if wanted, also sets columnStart_ etc |
---|
808 | Also creates scaling arrays if needed. It does scaling if needed. |
---|
809 | 16 also moves solutions etc in to work arrays |
---|
810 | On 16 returns false if problem "bad" i.e. matrix or bounds bad |
---|
811 | If startFinishOptions is -1 then called by user in getSolution |
---|
812 | so do arrays but keep pivotVariable_ |
---|
813 | */ |
---|
814 | bool createRim(int what,bool makeRowCopy=false,int startFinishOptions=0); |
---|
815 | /// Does rows and columns |
---|
816 | void createRim1(bool initial); |
---|
817 | /// Does objective |
---|
818 | void createRim4(bool initial); |
---|
819 | /// Does rows and columns and objective |
---|
820 | void createRim5(bool initial); |
---|
821 | /** releases above arrays and does solution scaling out. May also |
---|
822 | get rid of factorization data - |
---|
823 | 0 get rid of nothing, 1 get rid of arrays, 2 also factorization |
---|
824 | */ |
---|
825 | void deleteRim(int getRidOfFactorizationData=2); |
---|
826 | /// Sanity check on input rim data (after scaling) - returns true if okay |
---|
827 | bool sanityCheck(); |
---|
828 | //@} |
---|
829 | public: |
---|
830 | /**@name public methods */ |
---|
831 | //@{ |
---|
832 | /** Return row or column sections - not as much needed as it |
---|
833 | once was. These just map into single arrays */ |
---|
834 | inline double * solutionRegion(int section) const |
---|
835 | { if (!section) return rowActivityWork_; else return columnActivityWork_;} |
---|
836 | inline double * djRegion(int section) const |
---|
837 | { if (!section) return rowReducedCost_; else return reducedCostWork_;} |
---|
838 | inline double * lowerRegion(int section) const |
---|
839 | { if (!section) return rowLowerWork_; else return columnLowerWork_;} |
---|
840 | inline double * upperRegion(int section) const |
---|
841 | { if (!section) return rowUpperWork_; else return columnUpperWork_;} |
---|
842 | inline double * costRegion(int section) const |
---|
843 | { if (!section) return rowObjectiveWork_; else return objectiveWork_;} |
---|
844 | /// Return region as single array |
---|
845 | inline double * solutionRegion() const |
---|
846 | { return solution_;} |
---|
847 | inline double * djRegion() const |
---|
848 | { return dj_;} |
---|
849 | inline double * lowerRegion() const |
---|
850 | { return lower_;} |
---|
851 | inline double * upperRegion() const |
---|
852 | { return upper_;} |
---|
853 | inline double * costRegion() const |
---|
854 | { return cost_;} |
---|
855 | inline Status getStatus(int sequence) const |
---|
856 | {return static_cast<Status> (status_[sequence]&7);} |
---|
857 | inline void setStatus(int sequence, Status status) |
---|
858 | { |
---|
859 | unsigned char & st_byte = status_[sequence]; |
---|
860 | st_byte = static_cast<unsigned char>(st_byte & ~7); |
---|
861 | st_byte = static_cast<unsigned char>(st_byte | status); |
---|
862 | } |
---|
863 | /// Start or reset using maximumRows_ and Columns_ - true if change |
---|
864 | bool startPermanentArrays(); |
---|
865 | /** Normally the first factorization does sparse coding because |
---|
866 | the factorization could be singular. This allows initial dense |
---|
867 | factorization when it is known to be safe |
---|
868 | */ |
---|
869 | void setInitialDenseFactorization(bool onOff); |
---|
870 | bool initialDenseFactorization() const; |
---|
871 | /** Return sequence In or Out */ |
---|
872 | inline int sequenceIn() const |
---|
873 | {return sequenceIn_;} |
---|
874 | inline int sequenceOut() const |
---|
875 | {return sequenceOut_;} |
---|
876 | /** Set sequenceIn or Out */ |
---|
877 | inline void setSequenceIn(int sequence) |
---|
878 | { sequenceIn_=sequence;} |
---|
879 | inline void setSequenceOut(int sequence) |
---|
880 | { sequenceOut_=sequence;} |
---|
881 | /** Return direction In or Out */ |
---|
882 | inline int directionIn() const |
---|
883 | {return directionIn_;} |
---|
884 | inline int directionOut() const |
---|
885 | {return directionOut_;} |
---|
886 | /** Set directionIn or Out */ |
---|
887 | inline void setDirectionIn(int direction) |
---|
888 | { directionIn_=direction;} |
---|
889 | inline void setDirectionOut(int direction) |
---|
890 | { directionOut_=direction;} |
---|
891 | /// Value of Out variable |
---|
892 | inline double valueOut() const |
---|
893 | { return valueOut_;} |
---|
894 | /// Returns 1 if sequence indicates column |
---|
895 | inline int isColumn(int sequence) const |
---|
896 | { return sequence<numberColumns_ ? 1 : 0;} |
---|
897 | /// Returns sequence number within section |
---|
898 | inline int sequenceWithin(int sequence) const |
---|
899 | { return sequence<numberColumns_ ? sequence : sequence-numberColumns_;} |
---|
900 | /// Return row or column values |
---|
901 | inline double solution(int sequence) |
---|
902 | { return solution_[sequence];} |
---|
903 | /// Return address of row or column values |
---|
904 | inline double & solutionAddress(int sequence) |
---|
905 | { return solution_[sequence];} |
---|
906 | inline double reducedCost(int sequence) |
---|
907 | { return dj_[sequence];} |
---|
908 | inline double & reducedCostAddress(int sequence) |
---|
909 | { return dj_[sequence];} |
---|
910 | inline double lower(int sequence) |
---|
911 | { return lower_[sequence];} |
---|
912 | /// Return address of row or column lower bound |
---|
913 | inline double & lowerAddress(int sequence) |
---|
914 | { return lower_[sequence];} |
---|
915 | inline double upper(int sequence) |
---|
916 | { return upper_[sequence];} |
---|
917 | /// Return address of row or column upper bound |
---|
918 | inline double & upperAddress(int sequence) |
---|
919 | { return upper_[sequence];} |
---|
920 | inline double cost(int sequence) |
---|
921 | { return cost_[sequence];} |
---|
922 | /// Return address of row or column cost |
---|
923 | inline double & costAddress(int sequence) |
---|
924 | { return cost_[sequence];} |
---|
925 | /// Return original lower bound |
---|
926 | inline double originalLower(int iSequence) const |
---|
927 | { if (iSequence<numberColumns_) return columnLower_[iSequence]; else |
---|
928 | return rowLower_[iSequence-numberColumns_];} |
---|
929 | /// Return original lower bound |
---|
930 | inline double originalUpper(int iSequence) const |
---|
931 | { if (iSequence<numberColumns_) return columnUpper_[iSequence]; else |
---|
932 | return rowUpper_[iSequence-numberColumns_];} |
---|
933 | /// Theta (pivot change) |
---|
934 | inline double theta() const |
---|
935 | { return theta_;} |
---|
936 | /// Return pointer to details of costs |
---|
937 | inline ClpNonLinearCost * nonLinearCost() const |
---|
938 | { return nonLinearCost_;} |
---|
939 | /** Return more special options |
---|
940 | 1 bit - if presolve says infeasible in ClpSolve return |
---|
941 | 2 bit - if presolved problem infeasible return |
---|
942 | 4 bit - keep arrays like upper_ around |
---|
943 | 8 bit - if factorization kept can still declare optimal at once |
---|
944 | 16 bit - if checking replaceColumn accuracy before updating |
---|
945 | 32 bit - say optimal if primal feasible! |
---|
946 | */ |
---|
947 | inline int moreSpecialOptions() const |
---|
948 | { return moreSpecialOptions_;} |
---|
949 | /** Set more special options |
---|
950 | 1 bit - if presolve says infeasible in ClpSolve return |
---|
951 | 2 bit - if presolved problem infeasible return |
---|
952 | 4 bit - keep arrays like upper_ around |
---|
953 | */ |
---|
954 | inline void setMoreSpecialOptions(int value) |
---|
955 | { moreSpecialOptions_ = value;} |
---|
956 | //@} |
---|
957 | /**@name status methods */ |
---|
958 | //@{ |
---|
959 | inline void setFakeBound(int sequence, FakeBound fakeBound) |
---|
960 | { |
---|
961 | unsigned char & st_byte = status_[sequence]; |
---|
962 | st_byte = static_cast<unsigned char>(st_byte & ~24); |
---|
963 | st_byte = static_cast<unsigned char>(st_byte | (fakeBound<<3)); |
---|
964 | } |
---|
965 | inline FakeBound getFakeBound(int sequence) const |
---|
966 | {return static_cast<FakeBound> ((status_[sequence]>>3)&3);} |
---|
967 | inline void setRowStatus(int sequence, Status status) |
---|
968 | { |
---|
969 | unsigned char & st_byte = status_[sequence+numberColumns_]; |
---|
970 | st_byte = static_cast<unsigned char>(st_byte & ~7); |
---|
971 | st_byte = static_cast<unsigned char>(st_byte | status); |
---|
972 | } |
---|
973 | inline Status getRowStatus(int sequence) const |
---|
974 | {return static_cast<Status> (status_[sequence+numberColumns_]&7);} |
---|
975 | inline void setColumnStatus(int sequence, Status status) |
---|
976 | { |
---|
977 | unsigned char & st_byte = status_[sequence]; |
---|
978 | st_byte = static_cast<unsigned char>(st_byte & ~7); |
---|
979 | st_byte = static_cast<unsigned char>(st_byte | status); |
---|
980 | } |
---|
981 | inline Status getColumnStatus(int sequence) const |
---|
982 | {return static_cast<Status> (status_[sequence]&7);} |
---|
983 | inline void setPivoted( int sequence) |
---|
984 | { status_[sequence] = static_cast<unsigned char>(status_[sequence] | 32);} |
---|
985 | inline void clearPivoted( int sequence) |
---|
986 | { status_[sequence] = static_cast<unsigned char>(status_[sequence] & ~32);} |
---|
987 | inline bool pivoted(int sequence) const |
---|
988 | {return (((status_[sequence]>>5)&1)!=0);} |
---|
989 | /// To flag a variable (not inline to allow for column generation) |
---|
990 | void setFlagged( int sequence); |
---|
991 | inline void clearFlagged( int sequence) |
---|
992 | { |
---|
993 | status_[sequence] = static_cast<unsigned char>(status_[sequence] & ~64); |
---|
994 | } |
---|
995 | inline bool flagged(int sequence) const |
---|
996 | {return ((status_[sequence]&64)!=0);} |
---|
997 | /// To say row active in primal pivot row choice |
---|
998 | inline void setActive( int iRow) |
---|
999 | { |
---|
1000 | status_[iRow] = static_cast<unsigned char>(status_[iRow] | 128); |
---|
1001 | } |
---|
1002 | inline void clearActive( int iRow) |
---|
1003 | { |
---|
1004 | status_[iRow] = static_cast<unsigned char>(status_[iRow] & ~128); |
---|
1005 | } |
---|
1006 | inline bool active(int iRow) const |
---|
1007 | {return ((status_[iRow]&128)!=0);} |
---|
1008 | /** Set up status array (can be used by OsiClp). |
---|
1009 | Also can be used to set up all slack basis */ |
---|
1010 | void createStatus() ; |
---|
1011 | /** Sets up all slack basis and resets solution to |
---|
1012 | as it was after initial load or readMps */ |
---|
1013 | void allSlackBasis(bool resetSolution=false); |
---|
1014 | |
---|
1015 | /// So we know when to be cautious |
---|
1016 | inline int lastBadIteration() const |
---|
1017 | {return lastBadIteration_;} |
---|
1018 | /// Progress flag - at present 0 bit says artificials out |
---|
1019 | inline int progressFlag() const |
---|
1020 | {return progressFlag_;} |
---|
1021 | /// Force re-factorization early |
---|
1022 | inline void forceFactorization(int value) |
---|
1023 | { forceFactorization_ = value;} |
---|
1024 | /// Raw objective value (so always minimize in primal) |
---|
1025 | inline double rawObjectiveValue() const |
---|
1026 | { return objectiveValue_;} |
---|
1027 | /// Compute objective value from solution and put in objectiveValue_ |
---|
1028 | void computeObjectiveValue(bool useWorkingSolution=false); |
---|
1029 | /// Compute minimization objective value from internal solution without perturbation |
---|
1030 | double computeInternalObjectiveValue(); |
---|
1031 | /** Number of extra rows. These are ones which will be dynamically created |
---|
1032 | each iteration. This is for GUB but may have other uses. |
---|
1033 | */ |
---|
1034 | inline int numberExtraRows() const |
---|
1035 | { return numberExtraRows_;} |
---|
1036 | /** Maximum number of basic variables - can be more than number of rows if GUB |
---|
1037 | */ |
---|
1038 | inline int maximumBasic() const |
---|
1039 | { return maximumBasic_;} |
---|
1040 | /// Iteration when we entered dual or primal |
---|
1041 | inline int baseIteration() const |
---|
1042 | { return baseIteration_;} |
---|
1043 | /// Create C++ lines to get to current state |
---|
1044 | void generateCpp( FILE * fp,bool defaultFactor=false); |
---|
1045 | /// Gets clean and emptyish factorization |
---|
1046 | ClpFactorization * getEmptyFactorization(); |
---|
1047 | /// May delete or may make clean and emptyish factorization |
---|
1048 | void setEmptyFactorization(); |
---|
1049 | /// Move status and solution across |
---|
1050 | void moveInfo(const ClpSimplex & rhs, bool justStatus=false); |
---|
1051 | //@} |
---|
1052 | |
---|
1053 | ///@name Basis handling |
---|
1054 | // These are only to be used using startFinishOptions (ClpSimplexDual, ClpSimplexPrimal) |
---|
1055 | // *** At present only without scaling |
---|
1056 | // *** Slacks havve -1.0 element (so == row activity) - take care |
---|
1057 | ///Get a row of the tableau (slack part in slack if not NULL) |
---|
1058 | void getBInvARow(int row, double* z, double * slack=NULL); |
---|
1059 | |
---|
1060 | ///Get a row of the basis inverse |
---|
1061 | void getBInvRow(int row, double* z); |
---|
1062 | |
---|
1063 | ///Get a column of the tableau |
---|
1064 | void getBInvACol(int col, double* vec); |
---|
1065 | |
---|
1066 | ///Get a column of the basis inverse |
---|
1067 | void getBInvCol(int col, double* vec); |
---|
1068 | |
---|
1069 | /** Get basic indices (order of indices corresponds to the |
---|
1070 | order of elements in a vector retured by getBInvACol() and |
---|
1071 | getBInvCol()). |
---|
1072 | */ |
---|
1073 | void getBasics(int* index); |
---|
1074 | |
---|
1075 | //@} |
---|
1076 | //------------------------------------------------------------------------- |
---|
1077 | /**@name Changing bounds on variables and constraints */ |
---|
1078 | //@{ |
---|
1079 | /** Set an objective function coefficient */ |
---|
1080 | void setObjectiveCoefficient( int elementIndex, double elementValue ); |
---|
1081 | /** Set an objective function coefficient */ |
---|
1082 | inline void setObjCoeff( int elementIndex, double elementValue ) |
---|
1083 | { setObjectiveCoefficient( elementIndex, elementValue);} |
---|
1084 | |
---|
1085 | /** Set a single column lower bound<br> |
---|
1086 | Use -DBL_MAX for -infinity. */ |
---|
1087 | void setColumnLower( int elementIndex, double elementValue ); |
---|
1088 | |
---|
1089 | /** Set a single column upper bound<br> |
---|
1090 | Use DBL_MAX for infinity. */ |
---|
1091 | void setColumnUpper( int elementIndex, double elementValue ); |
---|
1092 | |
---|
1093 | /** Set a single column lower and upper bound */ |
---|
1094 | void setColumnBounds( int elementIndex, |
---|
1095 | double lower, double upper ); |
---|
1096 | |
---|
1097 | /** Set the bounds on a number of columns simultaneously<br> |
---|
1098 | The default implementation just invokes setColLower() and |
---|
1099 | setColUpper() over and over again. |
---|
1100 | @param indexFirst,indexLast pointers to the beginning and after the |
---|
1101 | end of the array of the indices of the variables whose |
---|
1102 | <em>either</em> bound changes |
---|
1103 | @param boundList the new lower/upper bound pairs for the variables |
---|
1104 | */ |
---|
1105 | void setColumnSetBounds(const int* indexFirst, |
---|
1106 | const int* indexLast, |
---|
1107 | const double* boundList); |
---|
1108 | |
---|
1109 | /** Set a single column lower bound<br> |
---|
1110 | Use -DBL_MAX for -infinity. */ |
---|
1111 | inline void setColLower( int elementIndex, double elementValue ) |
---|
1112 | { setColumnLower(elementIndex, elementValue);} |
---|
1113 | /** Set a single column upper bound<br> |
---|
1114 | Use DBL_MAX for infinity. */ |
---|
1115 | inline void setColUpper( int elementIndex, double elementValue ) |
---|
1116 | { setColumnUpper(elementIndex, elementValue);} |
---|
1117 | |
---|
1118 | /** Set a single column lower and upper bound */ |
---|
1119 | inline void setColBounds( int elementIndex, |
---|
1120 | double lower, double upper ) |
---|
1121 | { setColumnBounds(elementIndex, lower, upper);} |
---|
1122 | |
---|
1123 | /** Set the bounds on a number of columns simultaneously<br> |
---|
1124 | @param indexFirst,indexLast pointers to the beginning and after the |
---|
1125 | end of the array of the indices of the variables whose |
---|
1126 | <em>either</em> bound changes |
---|
1127 | @param boundList the new lower/upper bound pairs for the variables |
---|
1128 | */ |
---|
1129 | inline void setColSetBounds(const int* indexFirst, |
---|
1130 | const int* indexLast, |
---|
1131 | const double* boundList) |
---|
1132 | { setColumnSetBounds(indexFirst, indexLast, boundList);} |
---|
1133 | |
---|
1134 | /** Set a single row lower bound<br> |
---|
1135 | Use -DBL_MAX for -infinity. */ |
---|
1136 | void setRowLower( int elementIndex, double elementValue ); |
---|
1137 | |
---|
1138 | /** Set a single row upper bound<br> |
---|
1139 | Use DBL_MAX for infinity. */ |
---|
1140 | void setRowUpper( int elementIndex, double elementValue ) ; |
---|
1141 | |
---|
1142 | /** Set a single row lower and upper bound */ |
---|
1143 | void setRowBounds( int elementIndex, |
---|
1144 | double lower, double upper ) ; |
---|
1145 | |
---|
1146 | /** Set the bounds on a number of rows simultaneously<br> |
---|
1147 | @param indexFirst,indexLast pointers to the beginning and after the |
---|
1148 | end of the array of the indices of the constraints whose |
---|
1149 | <em>either</em> bound changes |
---|
1150 | @param boundList the new lower/upper bound pairs for the constraints |
---|
1151 | */ |
---|
1152 | void setRowSetBounds(const int* indexFirst, |
---|
1153 | const int* indexLast, |
---|
1154 | const double* boundList); |
---|
1155 | |
---|
1156 | //@} |
---|
1157 | |
---|
1158 | ////////////////// data ////////////////// |
---|
1159 | protected: |
---|
1160 | |
---|
1161 | /**@name data. Many arrays have a row part and a column part. |
---|
1162 | There is a single array with both - columns then rows and |
---|
1163 | then normally two arrays pointing to rows and columns. The |
---|
1164 | single array is the owner of memory |
---|
1165 | */ |
---|
1166 | //@{ |
---|
1167 | /// Worst column primal infeasibility |
---|
1168 | double columnPrimalInfeasibility_; |
---|
1169 | /// Worst row primal infeasibility |
---|
1170 | double rowPrimalInfeasibility_; |
---|
1171 | /// Sequence of worst (-1 if feasible) |
---|
1172 | int columnPrimalSequence_; |
---|
1173 | /// Sequence of worst (-1 if feasible) |
---|
1174 | int rowPrimalSequence_; |
---|
1175 | /// Worst column dual infeasibility |
---|
1176 | double columnDualInfeasibility_; |
---|
1177 | /// Worst row dual infeasibility |
---|
1178 | double rowDualInfeasibility_; |
---|
1179 | /// More special options - see set for details |
---|
1180 | int moreSpecialOptions_; |
---|
1181 | /// Iteration when we entered dual or primal |
---|
1182 | int baseIteration_; |
---|
1183 | /// Primal tolerance needed to make dual feasible (<largeTolerance) |
---|
1184 | double primalToleranceToGetOptimal_; |
---|
1185 | /// Remaining largest dual infeasibility |
---|
1186 | double remainingDualInfeasibility_; |
---|
1187 | /// Large bound value (for complementarity etc) |
---|
1188 | double largeValue_; |
---|
1189 | /// Largest error on Ax-b |
---|
1190 | double largestPrimalError_; |
---|
1191 | /// Largest error on basic duals |
---|
1192 | double largestDualError_; |
---|
1193 | /// For computing whether to re-factorize |
---|
1194 | double alphaAccuracy_; |
---|
1195 | /// Dual bound |
---|
1196 | double dualBound_; |
---|
1197 | /// Alpha (pivot element) |
---|
1198 | double alpha_; |
---|
1199 | /// Theta (pivot change) |
---|
1200 | double theta_; |
---|
1201 | /// Lower Bound on In variable |
---|
1202 | double lowerIn_; |
---|
1203 | /// Value of In variable |
---|
1204 | double valueIn_; |
---|
1205 | /// Upper Bound on In variable |
---|
1206 | double upperIn_; |
---|
1207 | /// Reduced cost of In variable |
---|
1208 | double dualIn_; |
---|
1209 | /// Lower Bound on Out variable |
---|
1210 | double lowerOut_; |
---|
1211 | /// Value of Out variable |
---|
1212 | double valueOut_; |
---|
1213 | /// Upper Bound on Out variable |
---|
1214 | double upperOut_; |
---|
1215 | /// Infeasibility (dual) or ? (primal) of Out variable |
---|
1216 | double dualOut_; |
---|
1217 | /// Current dual tolerance for algorithm |
---|
1218 | double dualTolerance_; |
---|
1219 | /// Current primal tolerance for algorithm |
---|
1220 | double primalTolerance_; |
---|
1221 | /// Sum of dual infeasibilities |
---|
1222 | double sumDualInfeasibilities_; |
---|
1223 | /// Sum of primal infeasibilities |
---|
1224 | double sumPrimalInfeasibilities_; |
---|
1225 | /// Weight assigned to being infeasible in primal |
---|
1226 | double infeasibilityCost_; |
---|
1227 | /// Sum of Dual infeasibilities using tolerance based on error in duals |
---|
1228 | double sumOfRelaxedDualInfeasibilities_; |
---|
1229 | /// Sum of Primal infeasibilities using tolerance based on error in primals |
---|
1230 | double sumOfRelaxedPrimalInfeasibilities_; |
---|
1231 | /// Acceptable pivot value just after factorization |
---|
1232 | double acceptablePivot_; |
---|
1233 | /// Working copy of lower bounds (Owner of arrays below) |
---|
1234 | double * lower_; |
---|
1235 | /// Row lower bounds - working copy |
---|
1236 | double * rowLowerWork_; |
---|
1237 | /// Column lower bounds - working copy |
---|
1238 | double * columnLowerWork_; |
---|
1239 | /// Working copy of upper bounds (Owner of arrays below) |
---|
1240 | double * upper_; |
---|
1241 | /// Row upper bounds - working copy |
---|
1242 | double * rowUpperWork_; |
---|
1243 | /// Column upper bounds - working copy |
---|
1244 | double * columnUpperWork_; |
---|
1245 | /// Working copy of objective (Owner of arrays below) |
---|
1246 | double * cost_; |
---|
1247 | /// Row objective - working copy |
---|
1248 | double * rowObjectiveWork_; |
---|
1249 | /// Column objective - working copy |
---|
1250 | double * objectiveWork_; |
---|
1251 | /// Useful row length arrays |
---|
1252 | CoinIndexedVector * rowArray_[6]; |
---|
1253 | /// Useful column length arrays |
---|
1254 | CoinIndexedVector * columnArray_[6]; |
---|
1255 | /// Sequence of In variable |
---|
1256 | int sequenceIn_; |
---|
1257 | /// Direction of In, 1 going up, -1 going down, 0 not a clude |
---|
1258 | int directionIn_; |
---|
1259 | /// Sequence of Out variable |
---|
1260 | int sequenceOut_; |
---|
1261 | /// Direction of Out, 1 to upper bound, -1 to lower bound, 0 - superbasic |
---|
1262 | int directionOut_; |
---|
1263 | /// Pivot Row |
---|
1264 | int pivotRow_; |
---|
1265 | /// Last good iteration (immediately after a re-factorization) |
---|
1266 | int lastGoodIteration_; |
---|
1267 | /// Working copy of reduced costs (Owner of arrays below) |
---|
1268 | double * dj_; |
---|
1269 | /// Reduced costs of slacks not same as duals (or - duals) |
---|
1270 | double * rowReducedCost_; |
---|
1271 | /// Possible scaled reduced costs |
---|
1272 | double * reducedCostWork_; |
---|
1273 | /// Working copy of primal solution (Owner of arrays below) |
---|
1274 | double * solution_; |
---|
1275 | /// Row activities - working copy |
---|
1276 | double * rowActivityWork_; |
---|
1277 | /// Column activities - working copy |
---|
1278 | double * columnActivityWork_; |
---|
1279 | #ifdef CLP_AUXILIARY_MODEL |
---|
1280 | /// Auxiliary model |
---|
1281 | ClpSimplex * auxiliaryModel_; |
---|
1282 | #endif |
---|
1283 | /// Number of dual infeasibilities |
---|
1284 | int numberDualInfeasibilities_; |
---|
1285 | /// Number of dual infeasibilities (without free) |
---|
1286 | int numberDualInfeasibilitiesWithoutFree_; |
---|
1287 | /// Number of primal infeasibilities |
---|
1288 | int numberPrimalInfeasibilities_; |
---|
1289 | /// How many iterative refinements to do |
---|
1290 | int numberRefinements_; |
---|
1291 | /// dual row pivot choice |
---|
1292 | ClpDualRowPivot * dualRowPivot_; |
---|
1293 | /// primal column pivot choice |
---|
1294 | ClpPrimalColumnPivot * primalColumnPivot_; |
---|
1295 | /// Basic variables pivoting on which rows |
---|
1296 | int * pivotVariable_; |
---|
1297 | /// factorization |
---|
1298 | ClpFactorization * factorization_; |
---|
1299 | /// Saved version of solution |
---|
1300 | double * savedSolution_; |
---|
1301 | /// Number of times code has tentatively thought optimal |
---|
1302 | int numberTimesOptimal_; |
---|
1303 | /// Disaster handler |
---|
1304 | ClpDisasterHandler * disasterArea_; |
---|
1305 | /// If change has been made (first attempt at stopping looping) |
---|
1306 | int changeMade_; |
---|
1307 | /// Algorithm >0 == Primal, <0 == Dual |
---|
1308 | int algorithm_; |
---|
1309 | /** Now for some reliability aids |
---|
1310 | This forces re-factorization early */ |
---|
1311 | int forceFactorization_; |
---|
1312 | /** Perturbation: |
---|
1313 | -50 to +50 - perturb by this power of ten (-6 sounds good) |
---|
1314 | 100 - auto perturb if takes too long (1.0e-6 largest nonzero) |
---|
1315 | 101 - we are perturbed |
---|
1316 | 102 - don't try perturbing again |
---|
1317 | default is 100 |
---|
1318 | */ |
---|
1319 | int perturbation_; |
---|
1320 | /// Saved status regions |
---|
1321 | unsigned char * saveStatus_; |
---|
1322 | /** Very wasteful way of dealing with infeasibilities in primal. |
---|
1323 | However it will allow non-linearities and use of dual |
---|
1324 | analysis. If it doesn't work it can easily be replaced. |
---|
1325 | */ |
---|
1326 | ClpNonLinearCost * nonLinearCost_; |
---|
1327 | /// So we know when to be cautious |
---|
1328 | int lastBadIteration_; |
---|
1329 | /// So we know when to open up again |
---|
1330 | int lastFlaggedIteration_; |
---|
1331 | /// Can be used for count of fake bounds (dual) or fake costs (primal) |
---|
1332 | int numberFake_; |
---|
1333 | /// Can be used for count of changed costs (dual) or changed bounds (primal) |
---|
1334 | int numberChanged_; |
---|
1335 | /// Progress flag - at present 0 bit says artificials out, 1 free in |
---|
1336 | int progressFlag_; |
---|
1337 | /// First free/super-basic variable (-1 if none) |
---|
1338 | int firstFree_; |
---|
1339 | /** Number of extra rows. These are ones which will be dynamically created |
---|
1340 | each iteration. This is for GUB but may have other uses. |
---|
1341 | */ |
---|
1342 | int numberExtraRows_; |
---|
1343 | /** Maximum number of basic variables - can be more than number of rows if GUB |
---|
1344 | */ |
---|
1345 | int maximumBasic_; |
---|
1346 | /// If may skip final factorize then allow up to this pivots (default 20) |
---|
1347 | int dontFactorizePivots_; |
---|
1348 | /** For advanced use. When doing iterative solves things can get |
---|
1349 | nasty so on values pass if incoming solution has largest |
---|
1350 | infeasibility < incomingInfeasibility throw out variables |
---|
1351 | from basis until largest infeasibility < allowedInfeasibility. |
---|
1352 | if allowedInfeasibility>= incomingInfeasibility this is |
---|
1353 | always possible altough you may end up with an all slack basis. |
---|
1354 | |
---|
1355 | Defaults are 1.0,10.0 |
---|
1356 | */ |
---|
1357 | double incomingInfeasibility_; |
---|
1358 | double allowedInfeasibility_; |
---|
1359 | /// Automatic scaling of objective and rhs and bounds |
---|
1360 | int automaticScale_; |
---|
1361 | /// A copy of model with certain state - normally without cuts |
---|
1362 | ClpSimplex * baseModel_; |
---|
1363 | /// For dealing with all issues of cycling etc |
---|
1364 | ClpSimplexProgress progress_; |
---|
1365 | public: |
---|
1366 | /// Spare int array for passing information [0]!=0 switches on |
---|
1367 | mutable int spareIntArray_[4]; |
---|
1368 | /// Spare double array for passing information [0]!=0 switches on |
---|
1369 | mutable double spareDoubleArray_[4]; |
---|
1370 | protected: |
---|
1371 | /// Allow OsiClp certain perks |
---|
1372 | friend class OsiClpSolverInterface; |
---|
1373 | //@} |
---|
1374 | }; |
---|
1375 | //############################################################################# |
---|
1376 | /** A function that tests the methods in the ClpSimplex class. The |
---|
1377 | only reason for it not to be a member method is that this way it doesn't |
---|
1378 | have to be compiled into the library. And that's a gain, because the |
---|
1379 | library should be compiled with optimization on, but this method should be |
---|
1380 | compiled with debugging. |
---|
1381 | |
---|
1382 | It also does some testing of ClpFactorization class |
---|
1383 | */ |
---|
1384 | void |
---|
1385 | ClpSimplexUnitTest(const std::string & mpsDir); |
---|
1386 | |
---|
1387 | // For Devex stuff |
---|
1388 | #define DEVEX_TRY_NORM 1.0e-4 |
---|
1389 | #define DEVEX_ADD_ONE 1.0 |
---|
1390 | #endif |
---|