OSosrl2gams.cpp
Go to the documentation of this file.
1 // Copyright (C) GAMS Development and others 2008-2009
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // $Id: OSosrl2gams.cpp 4540 2012-10-03 12:08:09Z Gassmann $
6 //
7 // Authors: Stefan Vigerske
8 
9 #include "OSResult.h"
10 #include "OSosrl2gams.hpp"
11 #include "OSrLReader.h"
12 #include "OSErrorClass.h"
13 #include "CoinHelperFunctions.hpp"
14 
15 #ifdef HAVE_CSTRING
16 #include <cstring>
17 #else
18 #ifdef HAVE_STRING_H
19 #include <string.h>
20 #else
21 #error "don't have header file for string"
22 #endif
23 #endif
24 
25 #include "gmomcc.h"
26 #include "gevmcc.h"
27 
28 OSrL2Gams::OSrL2Gams(gmoHandle_t gmo_)
29  : gmo(gmo_), gev(gmo_ ? (gevHandle_t)gmoEnvironment(gmo_) : NULL)
30 { }
31 
33 {
34  if (osresult.general == NULL)
35  {
36  gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
37  gmoSolveStatSet(gmo, SolveStat_SolverErr);
38  gevLogStat(gev, "Error: OS result does not have header.");
39  return;
40  }
41  else if (osresult.getGeneralStatusType() == "error")
42  {
43  gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
44  gmoSolveStatSet(gmo, SolveStat_SolverErr);
45  gevLogStatPChar(gev, "Error: OS result reports error: ");
46  gevLogStat(gev, osresult.getGeneralMessage().c_str());
47  return;
48  }
49  else if (osresult.getGeneralStatusType() == "warning")
50  {
51  gevLogStatPChar(gev, "Warning: OS result reports warning: ");
52  gevLogStat(gev, osresult.getGeneralMessage().c_str());
53  }
54 
55  gmoSolveStatSet(gmo, SolveStat_Normal);
56 
57  if (osresult.getSolutionNumber() == 0)
58  {
59  gmoModelStatSet(gmo, ModelStat_NoSolutionReturned);
60  }
61  else if (osresult.getSolutionStatusType(0) == "unbounded")
62  {
63  gmoModelStatSet(gmo, ModelStat_Unbounded);
64  }
65  else if (osresult.getSolutionStatusType(0) == "globallyOptimal")
66  {
67  gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
68  }
69  else if (osresult.getSolutionStatusType(0) == "locallyOptimal")
70  {
71  gmoModelStatSet(gmo, ModelStat_OptimalLocal);
72  }
73  else if (osresult.getSolutionStatusType(0) == "optimal")
74  {
75  gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
76  }
77  else if (osresult.getSolutionStatusType(0) == "bestSoFar")
78  {
79  gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed); // or should we report integer solution if integer var.?
80  }
81  else if (osresult.getSolutionStatusType(0) == "feasible")
82  {
83  gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed); // or should we report integer solution if integer var.?
84  }
85  else if (osresult.getSolutionStatusType(0) == "infeasible")
86  {
87  gmoModelStatSet(gmo, ModelStat_InfeasibleGlobal);
88  }
89  else if (osresult.getSolutionStatusType(0) == "stoppedByLimit")
90  {
91  gmoSolveStatSet(gmo, SolveStat_Iteration); // just a guess
92  gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
93  }
94  else if (osresult.getSolutionStatusType(0) == "unsure")
95  {
96  gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
97  }
98  else if (osresult.getSolutionStatusType(0) == "error")
99  {
100  gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
101  }
102  else if (osresult.getSolutionStatusType(0) == "other")
103  {
104  gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
105  }
106  else
107  {
108  gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
109  }
110 
111  if (osresult.getVariableNumber() != gmoN(gmo))
112  {
113  gevLogStat(gev, "Error: Number of variables in OS result does not match with gams model.");
114  gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
115  gmoSolveStatSet(gmo, SolveStat_SystemErr);
116  return;
117  }
118  if (osresult.getConstraintNumber() != gmoM(gmo))
119  {
120  gevLogStat(gev, "Error: Number of constraints in OS result does not match with gams model.");
121  gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
122  gmoSolveStatSet(gmo, SolveStat_SystemErr);
123  return;
124  }
125 
126  OptimizationSolution* sol = osresult.optimization->solution[0];
127 
128 #define SMAG_DBL_NA -1E20 // there is a gmoNAinteger, but no gmoNAdouble
129  int* colBasStat = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Bstat_Super);
130  int* colIndic = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Cstat_OK);
131  double* colMarg = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
132  double* colLev = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
133 
134  int* rowBasStat = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Bstat_Super);
135  int* rowIndic = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Cstat_OK);
136  double* rowLev = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
137  double* rowMarg = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
138 
139  //TODO
140 // if (sol->constraints && sol->constraints->values) // set row levels, if available
141 // for (std::vector<ConValue*>::iterator it(sol->constraints->values->con.begin());
142 // it!=sol->constraints->values->con.end(); ++it)
143 // rowLev[(*it)->idx]=(*it)->value;
144  if (sol->constraints && sol->constraints->dualValues) // set row dual values, if available
145 // for (std::vector<DualVarValue*>::iterator it(sol->constraints->dualValues->con.begin());
146 // it!=sol->constraints->dualValues->con.end(); ++it)
147 // rowMarg[(*it)->idx]=(*it)->value; // what are it->lbValue and it->ubValue ?
148  for (int i=0; i < sol->constraints->dualValues->numberOfCon; i++)
149  rowMarg[sol->constraints->dualValues->con[i]->idx] = sol->constraints->dualValues->con[i]->value;
150  if (sol->variables && sol->variables->values) // set var values, if available
151 // for (std::vector<VarValue*>::const_iterator it(sol->variables->values->var.begin());
152 // it!=sol->variables->values->var.end(); ++it)
153 // colLev[(*it)->idx]=(*it)->value;
154  for (int i=0; i < sol->variables->values->numberOfVar; i++)
155  colLev[sol->variables->values->var[i]->idx] = sol->variables->values->var[i]->value;
156  if (sol->variables)
157  for (int i=0; i<sol->variables->numberOfOtherVariableResults; ++i)
158  {
159  if (sol->variables->other[i]->name=="reduced_costs")
160  {
161 // for (std::vector<OtherVarResult*>::const_iterator it(sol->variables->other[i]->var.begin());
162 // it!=sol->variables->other[i]->var.end(); ++it)
163 // colMarg[(*it)->idx]=atof((*it)->value.c_str());
164  for (int j=0; j < sol->variables->other[i]->numberOfVar; j++)
165  colLev[sol->variables->other[i]->var[j]->idx] = atof(sol->variables->other[i]->var[j]->value.c_str());
166  break;
167  }
168  }
169 
170  gmoSetSolution8(gmo, colLev, colMarg, rowMarg, rowLev, colBasStat, colIndic, rowBasStat, rowIndic);
171 
172  delete[] rowLev;
173  delete[] rowMarg;
174  delete[] rowBasStat;
175  delete[] rowIndic;
176  delete[] colLev;
177  delete[] colMarg;
178  delete[] colBasStat;
179  delete[] colIndic;
180 
181  if (sol->objectives && sol->objectives->values && sol->objectives->values->obj[0])
182  gmoSetHeadnTail(gmo, HobjVal, sol->objectives->values->obj[0]->value);
183 }
184 
185 void OSrL2Gams::writeSolution(std::string& osrl)
186 {
187  OSResult* osresult = NULL;
188  OSrLReader osrl_reader;
189  try
190  {
191  osresult = osrl_reader.readOSrL(osrl);
192  }
193  catch(const ErrorClass& error)
194  {
195  gevLogStat(gev, "Error parsing the OS result string:");
196  gevLogStat(gev, error.errormsg.c_str());
197  gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
198  gmoSolveStatSet(gmo, SolveStat_SystemErr);
199  return;
200  }
201  if (osresult)
202  writeSolution(*osresult);
203 }
std::string getGeneralMessage()
Get the general message.
Definition: OSResult.cpp:1520
int numberOfVar
the number of variable values that are in the solution
Definition: OSResult.h:907
std::string getGeneralStatusType()
Get the general status type, which can be: success, error, warning.
Definition: OSResult.cpp:1479
struct gevRec * gev
Definition: OSosrl2gams.hpp:24
std::string getSolutionStatusType(int solIdx)
Get the [i]th optimization solution status type, where i equals the given solution index...
Definition: OSResult.cpp:2051
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
int idx
idx is the index on variable in the solution
Definition: OSResult.h:852
std::string errormsg
errormsg is the error that is causing the exception to be thrown
Definition: OSErrorClass.h:42
DualVarValue ** con
con is a vector of DualVarValue objects that give an index and dual variable value for each constrain...
Definition: OSResult.h:1666
DualVariableValues * dualValues
a pointer to an array of DualVariableValues objects
Definition: OSResult.h:1871
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
GeneralResult * general
general holds the first child of the OSResult specified by the OSrL Schema.
Definition: OSResult.h:2561
if(!yyg->yy_init)
The OptimizationSolution Class.
Definition: OSResult.h:2263
OptimizationResult * optimization
optimization holds the fifth child of the OSResult specified by the OSrL Schema.
Definition: OSResult.h:2581
ObjectiveSolution * objectives
objectives holds the solution information for the objectives
Definition: OSResult.h:2300
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
void writeSolution(OSResult &osresult)
Writes a solution into a GMO with the result given as OSResult object.
Definition: OSosrl2gams.cpp:32
OtherVarResult ** var
Definition: OSResult.h:1162
int numberOfCon
record the number of constraints for which values are given
Definition: OSResult.h:1660
VarValue ** var
a vector of VarValue objects, there will be one for each variable in the solution ...
Definition: OSResult.h:912
VariableSolution * variables
variables holds the solution information for the variables
Definition: OSResult.h:2291
struct gmoRec * gmo
Definition: OSosrl2gams.hpp:23
std::string name
the name of the result the user is defining
Definition: OSResult.h:1139
VariableValues * values
a pointer to a VariableValues object
Definition: OSResult.h:1227
OSResult * osresult
int idx
the index of a variable in the solution
Definition: OSResult.h:1070
int getSolutionNumber()
get the number of solutions.
Definition: OSResult.cpp:2033
#define SMAG_DBL_NA
OptimizationSolution ** solution
solution is an array of pointers to OptimizationSolution objects
Definition: OSResult.h:2500
ConstraintSolution * constraints
constraints holds the solution information for the constraints
Definition: OSResult.h:2295
ObjectiveValues * values
a pointer to an array of ObjectiveValues objects
Definition: OSResult.h:1547
double value
Definition: OSResult.h:860
int idx
idx is the index on a constraint
Definition: OSResult.h:1603
OtherVariableResult ** other
a pointer to an array of other pointer objects for variables
Definition: OSResult.h:1238
The OSrLReader Class.
Definition: OSrLReader.h:42
OSrL2Gams(struct gmoRec *gmo_)
Constructor.
Definition: OSosrl2gams.cpp:28
int numberOfOtherVariableResults
the number of types of variable results other than the value of the variable
Definition: OSResult.h:1224
double value
the value of the objective indexed by idx
Definition: OSResult.h:1292
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
int getVariableNumber()
Get variable number.
Definition: OSResult.cpp:2003
double value
value of dual variable on the constraint indexed by idx
Definition: OSResult.h:1611
int getConstraintNumber()
Get constraint number.
Definition: OSResult.cpp:2023