template.cpp
Go to the documentation of this file.
1 /* $Id: template.cpp 2710 2009-06-10 21:13:43Z kmartin $ */
15 //#include <cppad/cppad.hpp>
16 #include "OSConfig.h"
17 #include "OSCoinSolver.h"
18 #include "OSIpoptSolver.h"
19 #include "OSResult.h"
20 #include "OSiLReader.h"
21 #include "OSiLWriter.h"
22 #include "OSrLReader.h"
23 #include "OSrLWriter.h"
24 #include "OSoLWriter.h"
25 #include "OSInstance.h"
26 #include "OSFileUtil.h"
27 #include "OSDefaultSolver.h"
28 #include "OShL.h"
29 #include "OSErrorClass.h"
30 #include "OSmps2OS.h"
31 #include "OSBase64.h"
32 #include "OSErrorClass.h"
33 #include "OSMathUtil.h"
34 #include "CoinFinite.hpp"
35 
36 #include "ClpSimplex.hpp"
37 #include "ClpSolve.hpp"
38 #include "CbcOrClpParam.hpp"
39 #include "ClpInterior.hpp"
40 #include "ClpCholeskyBase.hpp"
41 #include "ClpQuadraticObjective.hpp"
42 
43 #include<iostream>
44 #include <ostream>
45 #include <sstream>
46 #include <streambuf>
47 
48 #include<map>
49 #include<stdio.h>
50 
51 using std::cout;
52 using std::endl;
53 
54 
55 
56 
57 int main(int argC, char* argV[]){
58  WindowsErrorPopupBlocker();
59  FileUtil *fileUtil = NULL;
60  fileUtil = new FileUtil();
61 
62 
63  //demonstrate use of Clp
64  ClpSolve solveOptions;
65  ClpSolve::SolveType method;
66  method = ClpSolve::useBarrier;
67  //or primal simplex
68  method = ClpSolve::usePrimal;
69  solveOptions.setSolveType( method);
70 
71  ClpSimplex* qpClpModel;
72  //ClpInterior* qpClpModel;
73  qpClpModel = NULL;
74  // template -- add your code here -- //
75  std::cout << "Hello World" << std::endl;
76 
77 
79 
80  CbcOrClpParam parameters[CBCMAXPARAMETERS];
81  int numberParameters ;
82  establishParams(numberParameters, parameters) ;
83  std::cout << "NUMBER OF PARAMETERS = " << numberParameters<< std::endl;
84 
85  /*
86  parameters[whichParam(CLP_PARAM_ACTION_BASISIN, numberParameters, parameters)].setStringValue(importBasisFile);
87  parameters[whichParam(CLP_PARAM_ACTION_BASISOUT, numberParameters, parameters)].setStringValue(exportBasisFile);
88  parameters[whichParam(CLP_PARAM_ACTION_PRINTMASK, numberParameters, parameters)].setStringValue(printMask);
89  parameters[whichParam(CLP_PARAM_ACTION_DIRECTORY, numberParameters, parameters)].setStringValue(directory);
90  parameters[whichParam(CLP_PARAM_ACTION_DIRSAMPLE, numberParameters, parameters)].setStringValue(dirSample);
91  parameters[whichParam(CLP_PARAM_ACTION_DIRNETLIB, numberParameters, parameters)].setStringValue(dirNetlib);
92  parameters[whichParam(CBC_PARAM_ACTION_DIRMIPLIB, numberParameters, parameters)].setStringValue(dirMiplib);
93  parameters[whichParam(CLP_PARAM_DBL_DUALBOUND, numberParameters, parameters)].setDoubleValue(models->dualBound());
94  parameters[whichParam(CLP_PARAM_DBL_DUALTOLERANCE, numberParameters, parameters)].setDoubleValue(models->dualTolerance());
95  parameters[whichParam(CLP_PARAM_ACTION_EXPORT, numberParameters, parameters)].setStringValue(exportFile);
96  parameters[whichParam(CLP_PARAM_INT_IDIOT, numberParameters, parameters)].setIntValue(doIdiot);
97  parameters[whichParam(CLP_PARAM_ACTION_IMPORT, numberParameters, parameters)].setStringValue(importFile);
98  parameters[whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].setIntValue(models->logLevel());
99  parameters[whichParam(CLP_PARAM_INT_MAXFACTOR, numberParameters, parameters)].setIntValue(models->factorizationFrequency());
100  parameters[whichParam(CLP_PARAM_INT_MAXITERATION, numberParameters, parameters)].setIntValue(models->maximumIterations());
101  parameters[whichParam(CLP_PARAM_INT_OUTPUTFORMAT, numberParameters, parameters)].setIntValue(outputFormat);
102  parameters[whichParam(CLP_PARAM_INT_PRESOLVEPASS, numberParameters, parameters)].setIntValue(preSolve);
103  parameters[whichParam(CLP_PARAM_INT_PERTVALUE, numberParameters, parameters)].setIntValue(models->perturbation());
104  parameters[whichParam(CLP_PARAM_DBL_PRIMALTOLERANCE, numberParameters, parameters)].setDoubleValue(models->primalTolerance());
105  parameters[whichParam(CLP_PARAM_DBL_PRIMALWEIGHT, numberParameters, parameters)].setDoubleValue(models->infeasibilityCost());
106  parameters[whichParam(CLP_PARAM_ACTION_RESTORE, numberParameters, parameters)].setStringValue(restoreFile);
107  parameters[whichParam(CLP_PARAM_ACTION_SAVE, numberParameters, parameters)].setStringValue(saveFile);
108  parameters[whichParam(CLP_PARAM_DBL_TIMELIMIT, numberParameters, parameters)].setDoubleValue(models->maximumSeconds());
109  parameters[whichParam(CLP_PARAM_ACTION_SOLUTION, numberParameters, parameters)].setStringValue(solutionFile);
110  parameters[whichParam(CLP_PARAM_ACTION_SAVESOL, numberParameters, parameters)].setStringValue(solutionSaveFile);
111  parameters[whichParam(CLP_PARAM_INT_SPRINT, numberParameters, parameters)].setIntValue(doSprint);
112  parameters[whichParam(CLP_PARAM_INT_SUBSTITUTION, numberParameters, parameters)].setIntValue(substitution);
113  parameters[whichParam(CLP_PARAM_INT_DUALIZE, numberParameters, parameters)].setIntValue(dualize);
114  parameters[whichParam(CLP_PARAM_DBL_PRESOLVETOLERANCE, numberParameters, parameters)].setDoubleValue(1.0e-8);
115  */
116 
118 
119 
120  qpClpModel = new ClpSimplex();
121  //qpClpModel = new ClpInterior();
122 
123 
124  //set a parameter
125 
126 
127  qpClpModel->setIntParam(ClpMaxNumIteration, 100) ;
128 
129 
130  //parameters[whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].setIntValue(qpClpModel->logLevel());
131 
132  std::cout << "Parameter number for: CLP_PARAM_INT_SOLVERLOGLEVEL " << whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters) << std::endl;
133  std::cout << "Parameter number for: CLP_PARAM_INT_MAXITERATION " << whichParam(CLP_PARAM_INT_MAXITERATION, numberParameters, parameters) << std::endl;
134  parameters[ whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].setIntParameter(qpClpModel, 0);
135  parameters[ whichParam(CLP_PARAM_INT_MAXITERATION, numberParameters, parameters)].setIntParameter(qpClpModel, 10);
136 
137  std::cout << "CLP_PARAM_INT_SOLVERLOGLEVEL = " << parameters[whichParam(CLP_PARAM_INT_SOLVERLOGLEVEL, numberParameters, parameters)].intValue() << std::endl;
138 
139  try{
140 
141  int i;
142 
143 
144  const char dirsep = CoinFindDirSeparator();
145  std::string osil;
151  std::string dataDir;
152  std::string osilFileName;
153  dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
154 
155  // first declare a generic solver
156  DefaultSolver *solver = NULL;
157 
158  OSiLReader *osilreader = NULL;
159  OSInstance *osinstance = NULL;
160  OSoLWriter *osolwriter = NULL;
161  OSOption* osoption = NULL;
162 
163 
164 
165  /******************** Start Clp Example *************************/
166  std::cout << std::endl << std::endl;
167  std::cout << "CLP EXAMPLE" << std::endl;
168 
169  /******************** STEP 1 ************************
170  * Get an instance in mps format, and create an OSInstance object
171  */
172  std::string qpFileName;
173  qpFileName = dataDir + "osilFiles" + dirsep + "parincQuadratic.osil";
174  // convert to the OS native format
175  osil = fileUtil->getFileAsString( qpFileName.c_str() );
176  osilreader = new OSiLReader();
177  osinstance = osilreader->readOSiL( osil);
178 
179 
180  /******************** STEP 2 ************************
181  * Create an OSOption object and give the solver options
182  */
183  osoption = new OSOption();
189  // normally most output is turned off, here we turn it back on
190  osoption->setAnotherSolverOption("OsiHintTry","","osi","","OsiHintStrength","");
191  osoption->setAnotherSolverOption("OsiDoReducePrint","false","osi","","OsiHintParam","");
192  osolwriter = new OSoLWriter();
193  std::cout << osolwriter-> writeOSoL( osoption);
194 
195 
196  /******************** STEP 3 ************************
197  * Create the solver object -- for a CoinSolver we must specify
198  * which solver to use
199  */
200  //solver = new CoinSolver();
201  //solver->sSolverName ="clp";
202 
203 
204  CoinPackedMatrix* matrix;
205  bool columnMajor = true;
206  double maxGap = 0;
207  matrix = new CoinPackedMatrix(
208  columnMajor, //Column or Row Major
209  columnMajor? osinstance->getConstraintNumber() : osinstance->getVariableNumber(), //Minor Dimension
210  columnMajor? osinstance->getVariableNumber() : osinstance->getConstraintNumber(), //Major Dimension
211  osinstance->getLinearConstraintCoefficientNumber(), //Number of nonzeroes
212  columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->values : osinstance->getLinearConstraintCoefficientsInRowMajor()->values, //Pointer to matrix nonzeroes
213  columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->indexes : osinstance->getLinearConstraintCoefficientsInRowMajor()->indexes, //Pointer to start of minor dimension indexes -- change to allow for row storage
214  columnMajor? osinstance->getLinearConstraintCoefficientsInColumnMajor()->starts : osinstance->getLinearConstraintCoefficientsInRowMajor()->starts, //Pointers to start of columns.
215  0, 0, maxGap );
216 
217  qpClpModel->setOptimizationDirection( -1);
218  qpClpModel->loadProblem( *matrix, osinstance->getVariableLowerBounds(),
219  osinstance->getVariableUpperBounds(),
220  osinstance->getDenseObjectiveCoefficients()[0],
221  osinstance->getConstraintLowerBounds(), osinstance->getConstraintUpperBounds()
222  );
223 
224 
225  //now for the quadratic part
226  //get the quadratic terms
227  std::map<int, std::map<int, double> > varIndexMap;
228  std::map<int, std::map<int, double> >::iterator mit1;
229  std::map<int, double> tmpMap;
230  std::map<int, double>::iterator mit2;
231 
232  int numNonz;
233  numNonz = 0;
234 
235  QuadraticTerms* quadTerms = NULL;
236  quadTerms = osinstance->getQuadraticTerms();
237  int i1;
238  int j1;
239  int tmpint;
240 
241  for(i = 0; i < osinstance->getNumberOfQuadraticTerms(); i++){
242 
243  if( quadTerms->rowIndexes[ i] == -1){
244 
245  i1 = quadTerms->varOneIndexes[ i] ;
246  j1 = quadTerms->varTwoIndexes[ i] ;
247 
248  if(i1 == j1) quadTerms->coefficients[i] = 2*quadTerms->coefficients[i];
249 
250  //does Clp expect j1 >= i1?????
251  //try this
252 
253  if(j1 < i1){ //switch
254 
255  tmpint = j1;
256  j1 = i1;
257  i1 = tmpint;
258 
259 
260  }
261 
262  mit1 = varIndexMap.find( i1 );
263 
264  if( mit1 == varIndexMap.end() ){ //add new index
265 
266  tmpMap.insert( std::pair<int, double>( j1,
267  quadTerms->coefficients[i]) );
268  numNonz++;
269 
270  varIndexMap[ i1 ] = tmpMap;
271 
272  tmpMap.clear();
273  }else{
274 
275  //map index already exists
276  //insert second index if not alreay there
277  mit2 = mit1->second.find( j1) ;
278 
279  if( mit2 == mit1->second.end() ){
280  //add the new index and coefficient
281  mit1->second.insert( std::pair<int, double>( j1,
282  quadTerms->coefficients[i]) );
283  numNonz++;
284 
285  }else{
286 
287  mit2->second += quadTerms->coefficients[i];
288 
289  }
290 
291  }
292 
293  }//end if on test for objective function index
294 
295  }//end loop over quadratic terms
296 
297  std::cout << "numNonz = " << numNonz << std::endl;
298 
299 
300  int *start = NULL;
301  int *idx = NULL;
302  double *nonz = NULL;
303 
304  start = new int[ varIndexMap.size() + 1];
305  idx = new int[ numNonz]; //index the columns
306  nonz = new double[ numNonz];
307 
308  int kount;
309  kount = 0;
310  numNonz = 0;
311  start[ kount++] = numNonz;
312 
313  for( mit1 = varIndexMap.begin(); mit1 != varIndexMap.end(); mit1++){
314 
315  std::cout << std::endl;
316  std::cout << "FIRST INDEX = " << mit1->first << std::endl;
317  for( mit2 = mit1->second.begin(); mit2 != mit1->second.end(); mit2++){
318  std::cout << "SECOND INDEX = " << mit2->first << std::endl;
319  std::cout << "COEFFICIENT = " << mit2->second << std::endl;
320  idx[ numNonz] = mit2->first;
321  //we multiply by 2 for Clp
322  nonz[ numNonz++ ] = mit2->second;
323  }
324  start[ kount++] = numNonz ;
325 
326  }
327 
345  qpClpModel->loadQuadraticObjective( qpClpModel->numberColumns(), start, idx, nonz);
346 
347  qpClpModel->writeMps("quad.mps");
348 
349 
350  //call solver
351  //qpClpModel->primal();
352  ClpCholeskyBase * cholesky = new ClpCholeskyBase();
353  cholesky->setKKT(true);
354  // qpClpModel->setCholesky(cholesky);
355  // qpClpModel->primalDual();
356 
357  qpClpModel->initialSolve( solveOptions);
358 
359  double *primal;
360  double *dual;
361  primal = qpClpModel->primalColumnSolution();
362  dual = qpClpModel->dualRowSolution();
363 
364 
365  int numberColumns = qpClpModel->numberColumns();
366  int numberRows = qpClpModel->numberRows();
367  for (i = 0; i < numberColumns; i++) {
368  if (fabs(primal[i]) > 1.0e-8)
369  printf("%d primal %g\n", i, primal[i]);
370  }
371  for (i = 0; i < numberRows; i++) {
372  if (fabs(dual[i]) > 1.0e-8)
373  printf("%d dual %g\n", i, dual[i]);
374  }
375 
376 
377  std::cout << osinstance->printModel();
378 
379 
380  /******************** STEP 4 ************************
381  * Give the solver the instance and options and solve
382  */
383  //solver->osinstance = osinstance;
384  //solver->osoption = osoption;
385  //solver->solve();
386 
387 
388  /******************** STEP 5 ************************
389  * Create a result object and get the optimal objective
390  * and primal variable values
391  */
392  //getOSResult( solver->osrl);
393 
394  //OSResult *osr = solver->osresult;
395  //int numOfBasisVar = osr->getNumberOfBasisVar(0);
396  //std::cout << "NUMBER OF BASIS VARS = " << numOfBasisVar << std::endl;
397 
398 
399 
400  //do garbage collection
401  delete osilreader;
402  osilreader = NULL;
403  delete solver;
404  solver = NULL;
405  delete osoption;
406  osoption = NULL;
407  delete osolwriter;
408  osolwriter = NULL;
409  delete qpClpModel;
410  qpClpModel = NULL;
411  //finish garbage collection
412 
413 
414  /******************** End Clp Example *************************/
415 
416 
417 
418  delete fileUtil;
419  fileUtil = NULL;
420  std::cout << std::endl << "Execution terminated normally" << std::endl;
421 
422  return 0;
423 
424  }
425  catch(const ErrorClass& eclass){
426  delete fileUtil;
427  fileUtil = NULL;
428  delete qpClpModel;
429  qpClpModel = NULL;
430  std::cout << eclass.errormsg << std::endl;
431  return 0;
432  }
433 
434 }// end main
435 
a data structure for holding quadratic terms
Definition: OSGeneral.h:431
double * getConstraintLowerBounds()
Get constraint lower bounds.
double * getVariableLowerBounds()
Get variable lower bounds.
double * getConstraintUpperBounds()
Get constraint upper bounds.
int getVariableNumber()
Get number of variables.
std::string printModel()
Print the infix representation of the problem.
bool setAnotherSolverOption(std::string name, std::string value, std::string solver, std::string category, std::string type, std::string description)
Definition: OSOption.cpp:9509
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
int getLinearConstraintCoefficientNumber()
Get number of specified (usually nonzero) linear constraint coefficient values.
OSOption * osoption
Take an OSOption object and write a string that validates against the OSoL schema.
Definition: OSoLWriter.h:29
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
The Option Class.
Definition: OSOption.h:3564
int * indexes
indexes holds an integer array of rowIdx (or colIdx) elements in coefMatrix (AMatrix).
Definition: OSGeneral.h:258
double ** getDenseObjectiveCoefficients()
getDenseObjectiveCoefficients.
void fint fint fint real fint real real real real real real real real real * e
Used to read an OSiL string.
Definition: OSiLReader.h:37
int * varOneIndexes
varOneIndexes holds an integer array of the first variable indexes of all the quadratic terms...
Definition: OSGeneral.h:445
SparseMatrix * getLinearConstraintCoefficientsInRowMajor()
Get linear constraint coefficients in row major.
The Default Solver Class.
SparseMatrix * getLinearConstraintCoefficientsInColumnMajor()
Get linear constraint coefficients in column major.
double * values
values holds a double array of value elements in coefMatrix (AMatrix), which contains nonzero element...
Definition: OSGeneral.h:264
int getConstraintNumber()
Get number of constraints.
int getNumberOfQuadraticTerms()
Get the number of specified (usually nonzero) qTerms in the quadratic coefficients.
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
Definition: OSFileUtil.cpp:35
int * rowIndexes
rowIndexes holds an integer array of row indexes of all the quadratic terms.
Definition: OSGeneral.h:440
int * starts
starts holds an integer array of start elements in coefMatrix (AMatrix), which points to the start of...
Definition: OSGeneral.h:252
QuadraticTerms * getQuadraticTerms()
Get all the quadratic terms in the instance.
double * getVariableUpperBounds()
Get variable upper bounds.
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
class used to make it easy to read and write files.
Definition: OSFileUtil.h:37
double * coefficients
coefficients holds a double array all the quadratic term coefficients.
Definition: OSGeneral.h:455
int * varTwoIndexes
varTwoIndexes holds an integer array of the second variable indexes of all the quadratic terms...
Definition: OSGeneral.h:450
used for throwing exceptions.
Definition: OSErrorClass.h:31