OSInstanceGenerator.cpp
Go to the documentation of this file.
1 /* $Id: OSInstanceGenerator.cpp 2698 2009-06-09 04:14:07Z kmartin $ */
34 #include <iostream>
35 #include <vector>
36 
37 #include "CoinHelperFunctions.hpp"
38 #include "OSConfig.h"
39 #include "OSInstance.h"
40 #include "OSiLWriter.h"
41 #include "OSParameters.h"
42 #include "OSnLNode.h"
43 #include "OSErrorClass.h"
44 
45 
46 
47 
48 
49 
50 
51 
52 using std::cout;
53 using std::endl;
54 int main(){
55  WindowsErrorPopupBlocker();
56  cout << "Start Building the Model" << endl;
57  try{
59  osinstance = new OSInstance();
60  //
61  // put in some of the OSInstance <instanceHeader> information
62  osinstance->setInstanceSource("An example from the LINDO API samples directory");
63  osinstance->setInstanceDescription("A good example of a hard nonlinear program");
64  //
65  // now put in the OSInstance <instanceData> information
66  //
67  // first the variables
68  osinstance->setVariableNumber( 2);
69  //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
70  // we could use setVariables() and add all the variable with one method call -- below is easier
71  osinstance->addVariable(0, "x0", -100, 100, 'C');
72  osinstance->addVariable(1, "x1", 0, 1, 'B');
73  //
74  // now add the objective function
75  osinstance->setObjectiveNumber( 1);
76  // now the single coefficient in the linear part
77  SparseVector *objcoeff;
78  objcoeff = new SparseVector(1);
79  objcoeff->indexes[ 0] = 1;
80  objcoeff->values[ 0] = .4;
81  //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
82  osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
83  objcoeff->bDeleteArrays = true;
84  delete objcoeff;
85  //
86  // now the constraints
87  osinstance->setConstraintNumber( 6);
88  //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
89  // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
90  osinstance->addConstraint(0, "row0", -OSDBL_MAX, 4, 0);
91  osinstance->addConstraint(1, "row1", -OSDBL_MAX, 6, 0);
92  osinstance->addConstraint(2, "row2", -OSDBL_MAX, 0, 0);
93  osinstance->addConstraint(3, "row3", 0 , OSDBL_MAX, 0);
94  osinstance->addConstraint(4, "row4", -OSDBL_MAX, 0, 0);
95  osinstance->addConstraint(5, "row5", -OSDBL_MAX, 0, 0);
96  //
97  //
98  // now add the <linearConstraintCoefficients>
99  //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
100  //double* values, int valuesBegin, int valuesEnd,
101  //int* indexes, int indexesBegin, int indexesEnd,
102  //int* starts, int startsBegin, int startsEnd);
103  double *values = new double[ 3];
104  int *indexes = new int[ 3];
105  int *starts = new int[ 3];
106  values[ 0] = 1.0;
107  values[ 1] = 1.0;
108  values[ 2] = 1.0;
109  indexes[ 0] = 0;
110  indexes[ 1] = 0;
111  indexes[ 2] = 1;
112  starts[ 0] = 0;
113  starts[ 1] = 1;
114  starts[ 2] = 3;
115  osinstance->setLinearConstraintCoefficients(3, true, values, 0, 2,
116  indexes, 0, 2, starts, 0, 2);
117  //
118  // finally the nonlinear part, not as nice since we don't have any set() methods
119  // yet, we must work directly with the data structures
120  //
121  // we have 6 nonlinear expressions
123  osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 6 ];
124  // define the vectors
125  OSnLNode *nlNodePoint;
126  OSnLNodeVariable *nlNodeVariablePoint;
127  OSnLNodeNumber *nlNodeNumberPoint;
128  OSnLNodeMax *nlNodeMaxPoint;
129  std::vector<OSnLNode*> nlNodeVec;
130  //
131  //
132  // the objective function nonlinear term abs( x0 + 1)
133  osinstance->instanceData->nonlinearExpressions->nl[ 0] = new Nl();
134  osinstance->instanceData->nonlinearExpressions->nl[ 0]->idx = -1;
136  // create a variable nl node for x0
137  nlNodeVariablePoint = new OSnLNodeVariable();
138  nlNodeVariablePoint->idx=0;
139  nlNodeVec.push_back( nlNodeVariablePoint);
140  // create the nl node for number 1
141  nlNodeNumberPoint = new OSnLNodeNumber();
142  nlNodeNumberPoint->value = 1.0;
143  nlNodeVec.push_back( nlNodeNumberPoint);
144  // create the nl node for +
145  nlNodePoint = new OSnLNodePlus();
146  nlNodeVec.push_back( nlNodePoint);
147  // create the nl node for max
148  nlNodePoint = new OSnLNodeAbs();
149  nlNodeVec.push_back( nlNodePoint);
150  // the vectors are in postfix format
151  // now the expression tree
153  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
154  nlNodeVec.clear();
155  //
156  //
157  // constraint 0 has no nonlinear terms
158  // generate the x0*x1 term in constraint 1
159  //
160  osinstance->instanceData->nonlinearExpressions->nl[ 1] = new Nl();
161  osinstance->instanceData->nonlinearExpressions->nl[ 1]->idx = 1;
163  // create a variable nl node for x0
164  nlNodeVariablePoint = new OSnLNodeVariable();
165  nlNodeVariablePoint->idx=0;
166  nlNodeVec.push_back( nlNodeVariablePoint);
167  // create the nl node for x1
168  nlNodeVariablePoint = new OSnLNodeVariable();
169  nlNodeVariablePoint->idx=1;
170  nlNodeVec.push_back( nlNodeVariablePoint);
171  // create the nl node for *
172  nlNodePoint = new OSnLNodeTimes();
173  nlNodeVec.push_back( nlNodePoint);
174  // the vectors are in postfix format
175  // now the expression tree
177  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
178  nlNodeVec.clear();
179  //
180  //
181  // generate the x0*x1 term in constraint 2
182  osinstance->instanceData->nonlinearExpressions->nl[ 2] = new Nl();
183  osinstance->instanceData->nonlinearExpressions->nl[ 2]->idx = 2;
185  // create a variable nl node for x0
186  nlNodeVariablePoint = new OSnLNodeVariable();
187  nlNodeVariablePoint->idx=0;
188  nlNodeVec.push_back( nlNodeVariablePoint);
189  // create the nl node for x0
190  nlNodeVariablePoint = new OSnLNodeVariable();
191  nlNodeVariablePoint->idx=1;
192  nlNodeVec.push_back( nlNodeVariablePoint);
193  // create the nl node for *
194  nlNodePoint = new OSnLNodeTimes();
195  nlNodeVec.push_back( nlNodePoint);
196  // the vectors are in postfix format
197  // now the expression tree
199  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
200  nlNodeVec.clear();
201  //
202  //
203  //
204  // generate the max(x0 , x1 + 1) term in constraint 3
205  osinstance->instanceData->nonlinearExpressions->nl[ 3] = new Nl();
206  osinstance->instanceData->nonlinearExpressions->nl[ 3]->idx = 3;
208  // create a variable nl node for x1
209  nlNodeVariablePoint = new OSnLNodeVariable();
210  nlNodeVariablePoint->idx=1;
211  nlNodeVec.push_back( nlNodeVariablePoint);
212  // create the nl node for number 1
213  nlNodeNumberPoint = new OSnLNodeNumber();
214  nlNodeNumberPoint->value = 1.0;
215  nlNodeVec.push_back( nlNodeNumberPoint);
216  // create the nl node for +
217  nlNodePoint = new OSnLNodePlus();
218  nlNodeVec.push_back( nlNodePoint);
219  // now push x0 to the stack
220  nlNodeVariablePoint = new OSnLNodeVariable();
221  nlNodeVariablePoint->idx=0;
222  nlNodeVec.push_back( nlNodeVariablePoint);
223  // create the nl node for max
224  nlNodeMaxPoint = new OSnLNodeMax();
225  nlNodeMaxPoint->inumberOfChildren = 2;
226  nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
227  nlNodeVec.push_back( nlNodeMaxPoint);
228  // the vectors are in postfix format
229  // now the expression tree
231  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
232  nlNodeVec.clear();
233  //
234  //
235  //
236  // generate the if(x1, 1, x1) term in constraint 4
237  osinstance->instanceData->nonlinearExpressions->nl[ 4] = new Nl();
238  osinstance->instanceData->nonlinearExpressions->nl[ 4]->idx = 4;
240  // create a variable nl node for x1
241  nlNodeVariablePoint = new OSnLNodeVariable();
242  nlNodeVariablePoint->idx=1;
243  nlNodeVec.push_back( nlNodeVariablePoint);
244  // create the nl node for number 1
245  nlNodeNumberPoint = new OSnLNodeNumber();
246  nlNodeNumberPoint->value = 1.0;
247  nlNodeVec.push_back( nlNodeNumberPoint);
248  // now push x1 to the stack
249  nlNodeVariablePoint = new OSnLNodeVariable();
250  nlNodeVariablePoint->idx=1;
251  nlNodeVec.push_back( nlNodeVariablePoint);
252  // create the nl node for If
253  nlNodePoint = new OSnLNodeIf();
254  nlNodeVec.push_back( nlNodePoint);
255  // the vectors are in postfix format
256  // now the expression tree
258  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
259  nlNodeVec.clear();
260  //
261  //
262  //
263  // generate the (x1 * 2 * x1 - x1) * x0 term in constraint 5
264  osinstance->instanceData->nonlinearExpressions->nl[ 5] = new Nl();
265  osinstance->instanceData->nonlinearExpressions->nl[ 5]->idx = 5;
267  // create a variable nl node for x1
268  nlNodeVariablePoint = new OSnLNodeVariable();
269  nlNodeVariablePoint->idx=1;
270  nlNodeVec.push_back( nlNodeVariablePoint);
271  // create the nl node for number 1
272  nlNodeNumberPoint = new OSnLNodeNumber();
273  nlNodeNumberPoint->value = 2.0;
274  nlNodeVec.push_back( nlNodeNumberPoint);
275  // create an nl node for *
276  nlNodePoint = new OSnLNodeTimes();
277  nlNodeVec.push_back( nlNodePoint);
278  // now push x1 to the stack
279  nlNodeVariablePoint = new OSnLNodeVariable();
280  nlNodeVariablePoint->idx=1;
281  nlNodeVec.push_back( nlNodeVariablePoint);
282  // create an nl node for *
283  nlNodePoint = new OSnLNodeTimes();
284  nlNodeVec.push_back( nlNodePoint);
285  // create a variable nl node for x1
286  nlNodeVariablePoint = new OSnLNodeVariable();
287  nlNodeVariablePoint->idx=1;
288  nlNodeVec.push_back( nlNodeVariablePoint);
289  // create an nl node for -
290  nlNodePoint = new OSnLNodeMinus();
291  nlNodeVec.push_back( nlNodePoint);
292  // create a variable nl node for x0
293  nlNodeVariablePoint = new OSnLNodeVariable();
294  nlNodeVariablePoint->idx=0;
295  nlNodeVec.push_back( nlNodeVariablePoint);
296  // create an nl node for *
297  nlNodePoint = new OSnLNodeTimes();
298  nlNodeVec.push_back( nlNodePoint);
299  // the vectors are in postfix format
300  // now the expression tree
302  nlNodeVec[ 0]->createExpressionTreeFromPostfix( nlNodeVec);
303  nlNodeVec.clear();
304  //
305  //
306  //
307  cout << "End Building the Model: Here is What you built" << endl;
308  // Write out the model
309  OSiLWriter *osilwriter;
310  osilwriter = new OSiLWriter();
311  cout << osilwriter->writeOSiL( osinstance);
312  std::cout << osinstance->printModel( ) << std::endl;
313  // done writing the model
314 
315  cout << "Done writing the Model" << endl;
316  delete osinstance;
317  osinstance = NULL;
318  delete osilwriter;
319  osilwriter = NULL;
320  cout << "Done with garbage collection" << endl;
321  cout << "Program terminates normally" << endl;
322  return 0;
323  }
324  catch(const ErrorClass& eclass){
325  cout << eclass.errormsg << endl;
326  }
327 }
328 
The OSnLNodeTimes Class.
Definition: OSnLNode.h:617
double * values
This file defines the OSnLNode class along with its derived classes.
bool addVariable(int index, std::string name, double lowerBound, double upperBound, char type)
add a variable.
NonlinearExpressions * nonlinearExpressions
nonlinearExpressions is a pointer to a NonlinearExpressions object
Definition: OSInstance.h:2206
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 printModel()
Print the infix representation of the problem.
The OSnLNodeIf Class.
Definition: OSnLNode.h:1212
unsigned int inumberOfChildren
inumberOfChildren is the number of OSnLNode child elements If this number is not fixed, e.g., for a sum node, it is temporarily set to 0
Definition: OSnLNode.h:74
int idx
idx is the index of the variable
Definition: OSnLNode.h:1488
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
The OSnLNodeNumber Class.
Definition: OSnLNode.h:1262
bool setConstraintNumber(int number)
set the number of constraints.
Nl ** nl
nl is pointer to an array of Nl object pointers
Definition: OSInstance.h:469
bool bDeleteArrays
bDeleteArrays is true if we delete the arrays in garbage collection set to true by default ...
Definition: OSGeneral.h:149
The OSnLNodePlus Class.
Definition: OSnLNode.h:315
OSnLNode * createExpressionTreeFromPostfix(std::vector< ExprNode * > nlNodeVec)
Take a vector of ExprNodes (OSnLNodes and OSnLMNodes) in postfix format and create a scalar-valued OS...
Definition: OSnLNode.cpp:413
The OSnLNodeVariable Class.
Definition: OSnLNode.h:1478
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
The OSnLNodeMax Class.
Definition: OSnLNode.h:414
double value
value is the value of the number
Definition: OSnLNode.h:1266
a sparse vector data structure
Definition: OSGeneral.h:122
OSnLNode ** m_mChildren
m_mChildren holds all the operands, that is, nodes that the current node operates on...
Definition: OSnLNode.h:84
OSnLNode * m_treeRoot
m_treeRoot holds the root node (of OSnLNode type) of the expression tree.
InstanceData * instanceData
A pointer to an InstanceData object.
Definition: OSInstance.h:2278
The in-memory representation of the &lt;nl&gt; element.
Definition: OSInstance.h:410
int idx
idx holds the row index of the nonlinear expression
Definition: OSInstance.h:414
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
ScalarExpressionTree * osExpressionTree
osExpressionTree contains the root of the ScalarExpressionTree
Definition: OSInstance.h:430
double * values
values holds a double array of nonzero values.
Definition: OSGeneral.h:164
int numberOfNonlinearExpressions
numberOfNonlinearExpressions is the number of &lt;nl&gt; elements in the &lt;nonlinearExpressions&gt; element...
Definition: OSInstance.h:466
int * indexes
indexes holds an integer array of indexes whose corresponding values are nonzero. ...
Definition: OSGeneral.h:159
The OSnLNodeAbs Class.
Definition: OSnLNode.h:1112
bool setVariableNumber(int number)
set the number of variables.
The in-memory representation of an OSiL instance..
Definition: OSInstance.h:2262
OSInstance * osinstance
The OSnLNode Class for nonlinear expressions.
Definition: OSnLNode.h:179
Used to hold the instance in memory.
used for throwing exceptions.
Definition: OSErrorClass.h:31
The OSnLNodeMinus Class.
Definition: OSnLNode.h:515
Take an OSInstance object and write a string that validates against the OSiL schema.
Definition: OSiLWriter.h:29