OSModificationDemo.cpp
Go to the documentation of this file.
1 /* $Id: OSModificationDemo.cpp 2831 2009-07-21 07:34:44Z kmartin $ */
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 "OSInstance.h"
25 #include "OSoLWriter.h"
26 #include "OSFileUtil.h"
27 #include "OSDefaultSolver.h"
28 //#include "OSWSUtil.h"
29 //#include "OSSolverAgent.h"
30 #include "OShL.h"
31 #include "OSErrorClass.h"
32 #include "OSmps2OS.h"
33 #include "OSBase64.h"
34 #include "OSErrorClass.h"
35 #include "OSMathUtil.h"
36 
37 #include "OsiClpSolverInterface.hpp"
38 #include "OsiCbcSolverInterface.hpp"
39 #include "CbcModel.hpp"
40 
41 #include<iostream>
42 using std::cout;
43 using std::endl;
44 
45 //int main(int argC, char* argV[]){
46 int main( ){
47 // test OS code samples here
48 
49  FileUtil *fileUtil = NULL;
50  fileUtil = new FileUtil();
51  cout << "Start Building the Model" << endl;
52  try{
54 
55  // put in some of the OSInstance <instanceHeader> information
56  osinstance->setInstanceSource("From Anderson, Sweeney, Williams, and Martin");
57  osinstance->setInstanceDescription("The Par Inc. Problem");
58  //
59  // now put in the OSInstance <instanceData> information
60  //
61  // first the variables
62  osinstance->setVariableNumber( 2);
63  //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
64  // we could use setVariables() and add all the variable with one method call -- below is easier
65  osinstance->addVariable(0, "x0", 0, OSDBL_MAX, 'C');
66  osinstance->addVariable(1, "x1", 0, OSDBL_MAX, 'C');
67  //
68  // now add the objective function
69  osinstance->setObjectiveNumber( 1);
70  // now the coefficient
71  SparseVector *objcoeff = new SparseVector(2);
72  objcoeff->indexes[ 0] = 0;
73  objcoeff->values[ 0] = 10;
74  objcoeff->indexes[ 1] = 1;
75  objcoeff->values[ 1] = 9;
76  //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
77  osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
78  objcoeff->bDeleteArrays = true;
79  delete objcoeff;
80  //
81  // now the constraints
82  osinstance->setConstraintNumber( 4);
83  //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
84  // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
85  osinstance->addConstraint(0, "row0", -OSDBL_MAX, 630, 0);
86  osinstance->addConstraint(1, "row1", -OSDBL_MAX, 600, 0);
87  osinstance->addConstraint(2, "row2", -OSDBL_MAX, 708, 0);
88  osinstance->addConstraint(3, "row3", -OSDBL_MAX, 135, 0);
89  /*
90  now add the <linearConstraintCoefficients>
91  first do column major
92  bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
93  double* values, int valuesBegin, int valuesEnd,
94  int* indexes, int indexesBegin, int indexesEnd,
95  int* starts, int startsBegin, int startsEnd);
96  double *values = new double[ 8];
97  int *indexes = new int[ 8];
98  int *starts = new int[ 3];
99  values[ 0] = .7;
100  values[ 1] = .5;
101  values[ 2] = 1.0;
102  values[ 3] = .1;
103  values[ 4] = 1.0;
104  values[ 5] = 5./6.;
105  values[ 6] = 2./3.;
106  values[ 7] = .25;
107  indexes[ 0] = 0;
108  indexes[ 1] = 1;
109  indexes[ 2] = 2;
110  indexes[ 3] = 3;
111  indexes[ 4] = 0;
112  indexes[ 5] = 1;
113  indexes[ 6] = 2;
114  indexes[ 7] = 3;
115  starts[ 0] = 0;
116  starts[ 1] = 4;
117  starts[ 2] = 8;
118  cout << "Call setLinearConstraintCoefficients" << endl;
119  osinstance->setLinearConstraintCoefficients(8, true, values, 0, 7,
120  indexes, 0, 7, starts, 0, 2);
121 
122  now row major
123  */
124  double *values = new double[ 8];
125  int *indexes = new int[ 8];
126  int *starts = new int[ 5];
127  values[ 0] = .7;
128  values[ 1] = 1;
129  values[ 2] = .5;
130  values[ 3] = 5./6.;
131  values[ 4] = 1.0;
132  values[ 5] = 2./3.;
133  values[ 6] = .1;
134  values[ 7] = .25;
135  indexes[ 0] = 0;
136  indexes[ 1] = 1;
137  indexes[ 2] = 0;
138  indexes[ 3] = 1;
139  indexes[ 4] = 0;
140  indexes[ 5] = 1;
141  indexes[ 6] = 0;
142  indexes[ 7] = 1;
143  starts[ 0] = 0;
144  starts[ 1] = 2;
145  starts[ 2] = 4;
146  starts[ 3] = 6;
147  starts[ 4] = 8;
148  cout << "Call setLinearConstraintCoefficients" << endl;
149  osinstance->setLinearConstraintCoefficients(8, false, values, 0, 7,
150  indexes, 0, 7, starts, 0, 4);
151  cout << "End Building the Model" << endl;
152  // Write out the model
153  OSiLWriter *osilwriter;
154  osilwriter = new OSiLWriter();
155  cout << osilwriter->writeOSiL( osinstance);
156  // done writing the model
157  cout << "Done writing the Model" << endl;
158  // now solve the model
159  CoinSolver *solver = new CoinSolver();
160  solver->osinstance = osinstance;
161  solver->sSolverName ="clp";
162  solver->buildSolverInstance();
163  solver->solve();
164  std::cout << solver->osrl << std::endl;
165  // write the answer to a file
166  //fileUtil->writeFileFromString("../result.xml", solver->osrl);
167  // work with the OSResult object
168  //OSResult *result = NULL;
169  //result = solver->osresult;
170  //std::cout << result->resultData->optimization->solution[0]->objectives->values->obj[0]->value << std::endl;
171  // do garbage collection
172 
173  // illustrate changing an objective function coefficient
174  delete solver;
175  osinstance->bObjectivesModified = true;
176  osinstance->instanceData->objectives->obj[0]->coef[0]->value = 5;
177  solver = new CoinSolver();
178  solver->osinstance = osinstance;
179  solver->sSolverName ="clp";
180  solver->buildSolverInstance();
181  solver->solve();
182  std::cout << solver->osrl << std::endl;
183  delete solver;
184  // change objective function coefficient a second time
185  osinstance->bObjectivesModified = true;
186  osinstance->instanceData->objectives->obj[0]->coef[0]->value = 0;
187  // make the upper bound of the second variable 500
188  // first print out the current value -- should be "infinity"
189  std::cout << "Variable upper bound = "<< osinstance->instanceData->variables->var[1]->ub << std::endl;
190  // now change it to 500;
191  osinstance->bVariablesModified = true;
192  osinstance->instanceData->variables->var[1]->ub = 500;
193  std::cout << "Variable upper bound = "<< osinstance->instanceData->variables->var[1]->ub << std::endl;
194  solver = new CoinSolver();
195  solver->osinstance = osinstance;
196  solver->sSolverName ="clp";
197  solver->buildSolverInstance();
198  solver->solve();
199  std::cout << solver->osrl << std::endl;
200  std::cout << "Obj value = " << solver->osresult->optimization->solution[0]->objectives->values->obj[0]->value << endl;
201  std::cout << "Obj value = " << solver->osresult->getOptimalObjValue( -1, 0) << endl; // use a get
202  delete osinstance;
203  osinstance = NULL;
204  delete osilwriter;
205  osilwriter = NULL;
206  delete solver;
207  solver = NULL;
208  delete fileUtil;
209  fileUtil = NULL;
210  cout << "Done with garbage collection" << endl;
211  return 0;
212  //
213  }
214  catch(const ErrorClass& eclass){
215  delete fileUtil;
216  std::cout << eclass.errormsg << std::endl;
217  return 0;
218  }
219 }// end main
220 
double * values
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type)
add a variable.
bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor, double *values, int valuesBegin, int valuesEnd, int *indexes, int indexesBegin, int indexesEnd, int *starts, int startsBegin, int startsEnd)
set linear constraint coefficients
std::string osrl
osrl holds the solution or result of the model
ObjValue ** obj
obj is a pointer to an array of ObjValue objects that give an index and objective function value for ...
Definition: OSResult.h:1344
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
ObjCoef ** coef
coef is pointer to an array of ObjCoef object pointers
Definition: OSInstance.h:176
OptimizationResult * optimization
optimization holds the fifth child of the OSResult specified by the OSrL Schema.
Definition: OSResult.h:2581
OSResult * osresult
osresult holds the solution or result of the model in-memory as an OSResult object ...
double getOptimalObjValue(int objIdx, int solIdx)
Get one solution&#39;s optimal objective value.
Definition: OSResult.cpp:3065
ObjectiveSolution * objectives
objectives holds the solution information for the objectives
Definition: OSResult.h:2300
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
bool bObjectivesModified
bObjectivesModified is true if the objective function data has been modified.
Definition: OSInstance.h:2293
bool setConstraintNumber(int number)
set the number of constraints.
bool bDeleteArrays
bDeleteArrays is true if we delete the arrays in garbage collection set to true by default ...
Definition: OSGeneral.h:149
Variable ** var
Here we define a pointer to an array of var pointers.
Definition: OSInstance.h:97
double value
value is the value of the objective function coefficient corresponding to the variable with index idx...
Definition: OSInstance.h:128
bool setObjectiveNumber(int number)
set the number of objectives.
bool addConstraint(int index, std::string name, double lowerBound, double upperBound, double constant)
add a constraint.
bool addObjective(int index, std::string name, std::string maxOrMin, double constant, double weight, SparseVector *objectiveCoefficients)
add an objective.
const double OSDBL_MAX
Definition: OSParameters.h:93
double ub
ub corresponds to the optional attribute that holds the variable upper bound.
Definition: OSInstance.h:61
a sparse vector data structure
Definition: OSGeneral.h:122
Variables * variables
variables is a pointer to a Variables object
Definition: OSInstance.h:2185
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
InstanceData * instanceData
A pointer to an InstanceData object.
Definition: OSInstance.h:2278
OptimizationSolution ** solution
solution is an array of pointers to OptimizationSolution objects
Definition: OSResult.h:2500
ObjectiveValues * values
a pointer to an array of ObjectiveValues objects
Definition: OSResult.h:1547
Objective ** obj
coef is pointer to an array of ObjCoef object pointers
Definition: OSInstance.h:205
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
bool setInstanceSource(std::string source)
set the instance source.
bool setInstanceDescription(std::string description)
set the instance description.
std::string writeOSiL(const OSInstance *theosinstance)
create an osil string from an OSInstance object
Definition: OSiLWriter.cpp:40
Objectives * objectives
objectives is a pointer to a Objectives object
Definition: OSInstance.h:2188
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
double * values
values holds a double array of nonzero values.
Definition: OSGeneral.h:164
bool bVariablesModified
bVariablesModified is true if the variables data has been modified.
Definition: OSInstance.h:2288
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero. ...
Definition: OSGeneral.h:159
double value
the value of the objective indexed by idx
Definition: OSResult.h:1292
bool setVariableNumber(int number)
set the number of variables.
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
virtual void solve()
The implementation of the corresponding virtual function.
used for throwing exceptions.
Definition: OSErrorClass.h:31
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition: OSiLWriter.h:29