OSResultDemo.cpp
Go to the documentation of this file.
1 /* $Id: OSResultDemo.cpp 2710 2009-06-10 21:13:43Z kmartin $ */
15 #include "OSConfig.h"
16 #include "OSCoinSolver.h"
17 #include "OSIpoptSolver.h"
18 #include "OSResult.h"
19 #include "OSiLReader.h"
20 #include "OSiLWriter.h"
21 #include "OSrLReader.h"
22 #include "OSrLWriter.h"
23 #include "OSInstance.h"
24 #include "OSOption.h"
25 #include "OSoLWriter.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 
35 
36 #include "CoinError.hpp"
37 #include "CoinHelperFunctions.hpp"
38 #include<iostream>
39 
40 
41 using std::string;
42 using std::cout;
43 using std::endl;
44 
45 //int main(int argC, char* argV[]){
46 int main( ){
47  WindowsErrorPopupBlocker();
48  FileUtil *fileUtil = NULL;
49  fileUtil = new FileUtil();
50  cout << "Start Building the Model" << endl;
51  try{
52 
53 
54  const char dirsep = CoinFindDirSeparator();
55  // Set directory containing mps data files.
56  std::string dataDir;
57  std::string osilFileName;
58  dataDir = dirsep == '/' ? "../data/" : "..\\data\\";
59 
60  osilFileName = dataDir + "osilFiles" + dirsep + "parincLinear.osil";
61  std::cout << "Try to read a sample file" << std::endl;
62  std::cout << "The file is: " ;
63  std::cout << osilFileName << std::endl;
64  std::string osil = fileUtil->getFileAsString( osilFileName.c_str() );
65  //now create some options
66  OSiLReader *osilreader = NULL;
67  osilreader = new OSiLReader();
69  osinstance = osilreader->readOSiL( osil);
70  CoinSolver *solver = new CoinSolver();
71  solver->osinstance = osinstance;
72  solver->sSolverName ="clp";
73  solver->buildSolverInstance();
74  solver->solve();
75 
76  // we demonstrate the OSResult API
77  // first read the result in OSResult object
78  // creat an OSResult object from the string
79  OSrLReader *osrlreader = NULL;
80  OSResult *osresult = NULL;
81  osrlreader = new OSrLReader();
82  osresult = osrlreader->readOSrL( solver->osrl);
83 
84  //now use the OSResult API -- first make sure we got an optimal solution
85  //get the status
86  std::string solStatus;
87  double optSolValue;
88  // the argument is the solution index
89  solStatus = osresult->getSolutionStatusType( 0 );
90  // if solStatus is optimal get the optimal solution value
91  if( solStatus.find("ptimal") != string::npos ){
92  //first index is objIdx, second is solution index
93  optSolValue = osresult->getOptimalObjValue( -1, 0);
94  std::cout << "OPTIMAL SOLUTION VALUE " << optSolValue << std::endl;
95  }else{
96  std::cout << "NO OPTIMAL SOLUTION FOUND " << std::endl;
97  }
98 
99  int i;
100  int j;
101  int k;
102  int vecSize;
103  // now get the primal solution
104  std::vector<IndexValuePair*> primalValPair;
105  primalValPair = osresult->getOptimalPrimalVariableValues( 0);
106  vecSize = primalValPair.size();
107  for(i = 0; i < vecSize; i++){
108  std::cout << "index = " << primalValPair[ i]->idx << std::endl;
109  std::cout << "value = " << primalValPair[ i]->value << std::endl;
110  }
111  // now get the dual solution
112  std::vector<IndexValuePair*> dualValPair;
113  dualValPair = osresult->getOptimalDualVariableValues( 0);
114  vecSize = dualValPair.size();
115  for(i = 0; i < vecSize; i++){
116  std::cout << "index = " << dualValPair[ i]->idx << std::endl;
117  std::cout << "value = " << dualValPair[ i]->value << std::endl;
118  }
119  //the OSResult API is currently somewhat limited, but you can get at the
120  //OSResult object directly -- let's get all the otherVar stuff
121  int numSolutions;
122  int numberOfOtherVariableResults;
123  int numberOfOtherVar;
124  numSolutions =osresult-> getSolutionNumber();
125  for(i = 0; i < numSolutions; i++){
126  numberOfOtherVariableResults = osresult->
127  getNumberOfOtherVariableResults( i);
128  for(j = 0; j < numberOfOtherVariableResults; j++){
129  std::cout << "Other Name = " << osresult->optimization->solution[ i]->variables->other[ j]->name << std::endl;
130  std::cout << "Other Description = " << osresult->optimization->solution[ i]->variables->other[ j]->description << std::endl;
131  std::cout << "Other NumberOfVar = " << osresult->optimization->solution[ i]->variables->other[ j]->numberOfVar << std::endl;
132  numberOfOtherVar = osresult->optimization->solution[ i]->variables->other[ j]->numberOfVar;
133  for(k = 0; k < numberOfOtherVar; k++){
134  std::cout << "Other Variable index = " << osresult->optimization->solution[ i]->variables->other[ j]->var[ k]->idx << std::endl;
135  std::cout << "Other Variable value = " << osresult->optimization->solution[ i]->variables->other[ j]->var[ k]->value << std::endl;
136  }
137  }
138  }
139  //garbage collection
140  delete solver;
141  solver = NULL;
142  delete fileUtil;
143  fileUtil = NULL;
144  delete osrlreader;
145  osrlreader = NULL;
146  delete osilreader;
147  osilreader = NULL;
148  cout << "Done with garbage collection" << endl;
149  cout << "Program terminates normally" << endl;
150  return 0;
151  //
152  }
153  catch(const ErrorClass& eclass){
154  delete fileUtil;
155  std::cout << eclass.errormsg << std::endl;
156  return 0;
157  }
158 }// end main
std::string getSolutionStatusType(int solIdx)
Get the [i]th optimization solution status type, where i equals the given solution index...
Definition: OSResult.cpp:2051
std::string osrl
osrl holds the solution or result of the model
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
std::vector< IndexValuePair * > getOptimalPrimalVariableValues(int solIdx)
Get one solution of optimal primal variable values.
Definition: OSResult.cpp:2215
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
Definition: OSrLReader.cpp:97
The Result Class.
Definition: OSResult.h:2548
OptimizationResult * optimization
optimization holds the fifth child of the OSResult specified by the OSrL Schema.
Definition: OSResult.h:2581
double getOptimalObjValue(int objIdx, int solIdx)
Get one solution&#39;s optimal objective value.
Definition: OSResult.cpp:3065
std::vector< IndexValuePair * > getOptimalDualVariableValues(int solIdx)
Get one solution of optimal dual variable values.
Definition: OSResult.cpp:3504
std::string description
a brief description of the type of result
Definition: OSResult.h:1150
OSInstance * readOSiL(const std::string &osil)
parse the OSiL model instance.
Definition: OSiLReader.cpp:53
virtual void buildSolverInstance()
The implementation of the corresponding virtual function.
static char * j
Definition: OSdtoa.cpp:3622
int numberOfVar
the number of variables which have values for this particular type of result
Definition: OSResult.h:1131
OtherVarResult ** var
Definition: OSResult.h:1162
VariableSolution * variables
variables holds the solution information for the variables
Definition: OSResult.h:2291
Used to read an OSiL string.
Definition: OSiLReader.h:37
std::string name
the name of the result the user is defining
Definition: OSResult.h:1139
OSResult * osresult
int idx
the index of a variable in the solution
Definition: OSResult.h:1070
void fint fint * k
OSInstance * osinstance
osinstance holds the problem instance in-memory as an OSInstance object
OptimizationSolution ** solution
solution is an array of pointers to OptimizationSolution objects
Definition: OSResult.h:2500
std::string sSolverName
sSolverName is the name of the Coin solver used, e.g.
OtherVariableResult ** other
a pointer to an array of other pointer objects for variables
Definition: OSResult.h:1238
Implements a solve method for the Coin solvers.
Definition: OSCoinSolver.h:37
std::string getFileAsString(const char *fname)
read a file and return contents as a string.
Definition: OSFileUtil.cpp:35
The OSrLReader Class.
Definition: OSrLReader.h:42
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.
std::string value
value holds a general value associated with a variable, for example, rather than the value of a varia...
Definition: OSResult.h:1081
used for throwing exceptions.
Definition: OSErrorClass.h:31