OSInstanceGenerator.cpp
Go to the documentation of this file.
1 /* $Id: OSInstanceGenerator.cpp 2698 2009-06-09 04:14:07Z kmartin $ */
35 #include<iostream>
36 #include <vector>
37 
38 #include "CoinHelperFunctions.hpp"
39 #include "OSConfig.h"
40 #include "OSInstance.h"
41 #include "OSiLWriter.h"
42 #include "OSParameters.h"
43 #include "OSnLNode.h"
44 #include "OSErrorClass.h"
45 
46 
47 using std::cout;
48 using std::endl;
49 
50 int main(){
51  WindowsErrorPopupBlocker();
52  cout << "Start Building the Model" << endl;
53  try{
55  osinstance = new OSInstance();
56  //
57  // put in some of the OSInstance <instanceHeader> information
58  osinstance->setInstanceSource("An example from the LINDO API samples directory");
59  osinstance->setInstanceDescription("A good example of a hard nonlinear program");
60  //
61  // now put in the OSInstance <instanceData> information
62  //
63  // first the variables
64  osinstance->setVariableNumber( 3);
65  //addVariable(int index, string name, double lowerBound, double upperBound, char type, double init, string initString);
66  // we could use setVariables() and add all the variable with one method call -- below is easier
67 
68  osinstance->addVariable(0, "x0", -100, 100, 'C');
69  osinstance->addVariable(1, "x1", 0, 1, 'B');
70  osinstance->addVariable(2, "x2", 5, 10, 'I');
71  //
72  // now add the objective function
73  osinstance->setObjectiveNumber( 1);
74  // now the coefficient
75  SparseVector *objcoeff;
76  objcoeff = new SparseVector(1);
77  objcoeff->indexes[ 0] = 1;
78  objcoeff->values[ 0] = .4;
79  //bool addObjective(int index, string name, string maxOrMin, double constant, double weight, SparseVector* objectiveCoefficients);
80  osinstance->addObjective(-1, "objfunction", "max", 0.0, 1.0, objcoeff);
81  objcoeff->bDeleteArrays = true;
82  delete objcoeff;
83  //
84  // now the constraints
85  osinstance->setConstraintNumber( 6);
86  //bool addConstraint(int index, string name, double lowerBound, double upperBound, double constant);
87  // note: we could use setConstraints() and add all the constraints with one method call -- below is easier
88  osinstance->addConstraint(0, "row0", -OSDBL_MAX, 4, 0);
89  osinstance->addConstraint(1, "row1", -OSDBL_MAX, 6, 0);
90  osinstance->addConstraint(2, "row2", -OSDBL_MAX, 0, 0);
91  osinstance->addConstraint(3, "row3", 0 , OSDBL_MAX, 0);
92  osinstance->addConstraint(4, "row4", -OSDBL_MAX, 0, 0);
93  osinstance->addConstraint(5, "row5", -OSDBL_MAX, 0, 0);
94  //
95  //
96  // now add the <linearConstraintCoefficients>
97  //bool setLinearConstraintCoefficients(int numberOfValues, bool isColumnMajor,
98  //double* values, int valuesBegin, int valuesEnd,
99  //int* indexes, int indexesBegin, int indexesEnd,
100  //int* starts, int startsBegin, int startsEnd);
101  double *values = new double[ 3];
102  int *indexes = new int[ 3];
103  int *starts = new int[ 3];
104  values[ 0] = 1.0;
105  values[ 1] = 1.0;
106  values[ 2] = 1.0;
107  indexes[ 0] = 0;
108  indexes[ 1] = 0;
109  indexes[ 2] = 1;
110  starts[ 0] = 0;
111  starts[ 1] = 1;
112  starts[ 2] = 3;
113  osinstance->setLinearConstraintCoefficients(3, true, values, 0, 2,
114  indexes, 0, 2, starts, 0, 2);
115  //
116  // finally the nonlinear part, not as nice since we don't have any set() methods
117  // yet, we must work directly with the data structures
118  //
119  // we use 7 nonlinear expressions
120  // note that constraint 2 contains two expressions
121  // that we choose to model separately.
122  // They will be added together by the system.
123 
126  osinstance->instanceData->nonlinearExpressions->nl = new Nl*[ 7 ];
127  // define the vectors
128  OSnLNode *nlNodePoint;
129  OSnLNodeVariable *nlNodeVariablePoint;
130  OSnLNodeNumber *nlNodeNumberPoint;
131  OSnLNodeMax *nlNodeMaxPoint;
132  std::vector<ExprNode*> nlNodeVec;
133  //
134  //
135  // the objective function nonlinear term abs( x0 + 1)
136  osinstance->instanceData->nonlinearExpressions->nl[ 0] = new Nl();
137  osinstance->instanceData->nonlinearExpressions->nl[ 0]->idx = -1;
139  // create a variable nl node for x0
140  nlNodeVariablePoint = new OSnLNodeVariable();
141  nlNodeVariablePoint->idx=0;
142  nlNodeVec.push_back( nlNodeVariablePoint);
143  // create the nl node for number 1
144  nlNodeNumberPoint = new OSnLNodeNumber();
145  nlNodeNumberPoint->value = 1.0;
146  nlNodeVec.push_back( nlNodeNumberPoint);
147  // create the nl node for +
148  nlNodePoint = new OSnLNodePlus();
149  nlNodeVec.push_back( nlNodePoint);
150  // create the nl node for max
151  nlNodePoint = new OSnLNodeAbs();
152  nlNodeVec.push_back( nlNodePoint);
153  // the vectors are in postfix format
154  // now the expression tree
156  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
157  nlNodeVec.clear();
158  //
159  //
160  // constraint 0 has no nonlinear terms
161  // generate the x1*x2 term in constraint 1
162  //
163  osinstance->instanceData->nonlinearExpressions->nl[ 1] = new Nl();
164  osinstance->instanceData->nonlinearExpressions->nl[ 1]->idx = 1;
166  // create a variable nl node for x1
167  nlNodeVariablePoint = new OSnLNodeVariable();
168  nlNodeVariablePoint->idx=1;
169  nlNodeVec.push_back( nlNodeVariablePoint);
170  // create the nl node for x2
171  nlNodeVariablePoint = new OSnLNodeVariable();
172  nlNodeVariablePoint->idx=2;
173  nlNodeVec.push_back( nlNodeVariablePoint);
174  // create the nl node for *
175  nlNodePoint = new OSnLNodeTimes();
176  nlNodeVec.push_back( nlNodePoint);
177  // the vectors are in postfix format
178  // now the expression tree
180  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
181  nlNodeVec.clear();
182  //
183  //
184  // generate the second term in constraint 1, ln(x2)
185  osinstance->instanceData->nonlinearExpressions->nl[ 2] = new Nl();
186  osinstance->instanceData->nonlinearExpressions->nl[ 2]->idx = 1;
188  // create a variable nl node for x2
189  nlNodeVariablePoint = new OSnLNodeVariable();
190  nlNodeVariablePoint->idx=2;
191  nlNodeVec.push_back( nlNodeVariablePoint);
192  // create the nl node for ln
193  nlNodePoint = new OSnLNodeLn();
194  nlNodeVec.push_back( nlNodePoint);
195  // the vectors are in postfix format
196  // now the expression tree
198  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
199  nlNodeVec.clear();
200  //
201  //
202 
203 
204  // generate the x0*x1 term in constraint 2
205  osinstance->instanceData->nonlinearExpressions->nl[ 3] = new Nl();
206  osinstance->instanceData->nonlinearExpressions->nl[ 3]->idx = 2;
208  // create a variable nl node for x0
209  nlNodeVariablePoint = new OSnLNodeVariable();
210  nlNodeVariablePoint->idx=0;
211  nlNodeVec.push_back( nlNodeVariablePoint);
212  // create the nl node for x0
213  nlNodeVariablePoint = new OSnLNodeVariable();
214  nlNodeVariablePoint->idx=1;
215  nlNodeVec.push_back( nlNodeVariablePoint);
216  // create the nl node for *
217  nlNodePoint = new OSnLNodeTimes();
218  nlNodeVec.push_back( nlNodePoint);
219  // the vectors are in postfix format
220  // now the expression tree
222  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
223  nlNodeVec.clear();
224  //
225  //
226  //
227  // generate the max(x0 , x1 + 1) term in constraint 3
228  osinstance->instanceData->nonlinearExpressions->nl[ 4] = new Nl();
229  osinstance->instanceData->nonlinearExpressions->nl[ 4]->idx = 3;
231  // create a variable nl node for x1
232  nlNodeVariablePoint = new OSnLNodeVariable();
233  nlNodeVariablePoint->idx=1;
234  nlNodeVec.push_back( nlNodeVariablePoint);
235  // create the nl node for number 1
236  nlNodeNumberPoint = new OSnLNodeNumber();
237  nlNodeNumberPoint->value = 1.0;
238  nlNodeVec.push_back( nlNodeNumberPoint);
239  // create the nl node for +
240  nlNodePoint = new OSnLNodePlus();
241  nlNodeVec.push_back( nlNodePoint);
242  // now push x0 to the stack
243  nlNodeVariablePoint = new OSnLNodeVariable();
244  nlNodeVariablePoint->idx=0;
245  nlNodeVec.push_back( nlNodeVariablePoint);
246  // create the nl node for max
247  nlNodeMaxPoint = new OSnLNodeMax();
248  nlNodeMaxPoint->inumberOfChildren = 2;
249  nlNodeMaxPoint->m_mChildren = new OSnLNode*[ nlNodeMaxPoint->inumberOfChildren];
250  nlNodeVec.push_back( nlNodeMaxPoint);
251  // the vectors are in postfix format
252  // now the expression tree
254  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
255  nlNodeVec.clear();
256  //
257  //
258  //
259  // generate the if(x1, 1, x1) term in constraint 4
260  osinstance->instanceData->nonlinearExpressions->nl[ 5] = new Nl();
261  osinstance->instanceData->nonlinearExpressions->nl[ 5]->idx = 4;
263  // create a variable nl node for x1
264  nlNodeVariablePoint = new OSnLNodeVariable();
265  nlNodeVariablePoint->idx=1;
266  nlNodeVec.push_back( nlNodeVariablePoint);
267  // create the nl node for number 1
268  nlNodeNumberPoint = new OSnLNodeNumber();
269  nlNodeNumberPoint->value = 1.0;
270  nlNodeVec.push_back( nlNodeNumberPoint);
271  // now push x1 to the stack
272  nlNodeVariablePoint = new OSnLNodeVariable();
273  nlNodeVariablePoint->idx=1;
274  nlNodeVec.push_back( nlNodeVariablePoint);
275  // create the nl node for If
276  nlNodePoint = new OSnLNodeIf();
277  nlNodeVec.push_back( nlNodePoint);
278  // the vectors are in postfix format
279  // now the expression tree
281  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
282  nlNodeVec.clear();
283  //
284  //
285  //
286  // generate the (x1 * 2 * x1 - x1) * x0 term in constraint 5
287  osinstance->instanceData->nonlinearExpressions->nl[ 6] = new Nl();
288  osinstance->instanceData->nonlinearExpressions->nl[ 6]->idx = 5;
290  // create a variable nl node for x1
291  nlNodeVariablePoint = new OSnLNodeVariable();
292  nlNodeVariablePoint->idx=1;
293  nlNodeVec.push_back( nlNodeVariablePoint);
294  // create the nl node for number 1
295  nlNodeNumberPoint = new OSnLNodeNumber();
296  nlNodeNumberPoint->value = 2.0;
297  nlNodeVec.push_back( nlNodeNumberPoint);
298  // create an nl node for *
299  nlNodePoint = new OSnLNodeTimes();
300  nlNodeVec.push_back( nlNodePoint);
301  // now push x1 to the stack
302  nlNodeVariablePoint = new OSnLNodeVariable();
303  nlNodeVariablePoint->idx=1;
304  nlNodeVec.push_back( nlNodeVariablePoint);
305  // create an nl node for *
306  nlNodePoint = new OSnLNodeTimes();
307  nlNodeVec.push_back( nlNodePoint);
308  // create a variable nl node for x1
309  nlNodeVariablePoint = new OSnLNodeVariable();
310  nlNodeVariablePoint->idx=1;
311  nlNodeVec.push_back( nlNodeVariablePoint);
312  // create an nl node for -
313  nlNodePoint = new OSnLNodeMinus();
314  nlNodeVec.push_back( nlNodePoint);
315  // create a variable nl node for x0
316  nlNodeVariablePoint = new OSnLNodeVariable();
317  nlNodeVariablePoint->idx=0;
318  nlNodeVec.push_back( nlNodeVariablePoint);
319  // create an nl node for *
320  nlNodePoint = new OSnLNodeTimes();
321  nlNodeVec.push_back( nlNodePoint);
322  // the vectors are in postfix format
323  // now the expression tree
325  ((OSnLNode*)nlNodeVec[ 0])->createExpressionTreeFromPostfix( nlNodeVec);
326  nlNodeVec.clear();
327  //
328  //
329  //
330  cout << "End Building the Model: Here is What you built" << endl;
331  // Write out the model
332  OSiLWriter *osilwriter;
333  osilwriter = new OSiLWriter();
334  cout << osilwriter->writeOSiL( osinstance);
335  std::cout << osinstance->printModel( ) << std::endl;
336  // done writing the model
337 
338  cout << "Done writing the Model" << endl;
339  delete osinstance;
340  osinstance = NULL;
341  delete osilwriter;
342  osilwriter = NULL;
343  cout << "Done with garbage collection" << endl;
344  return 0;
345  }
346  catch(const ErrorClass& eclass){
347  cout << eclass.errormsg << endl;
348  }
349 }
350 
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
The OSnLNodeLn Class.
Definition: OSnLNode.h:815
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
The in-memory representation of the &lt;nonlinearExpressions&gt; element.
Definition: OSInstance.h:452
The OSnLNodeNumber Class.
Definition: OSnLNode.h:1262
Used to hold part of the instance in memory.
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 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