My Project
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$
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
28OSrL2Gams::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
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
185void 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)
203}
#define SMAG_DBL_NA
DualVariableValues * dualValues
a pointer to an array of DualVariableValues objects
Definition OSResult.h:1871
double value
value of dual variable on the constraint indexed by idx
Definition OSResult.h:1611
int idx
idx is the index on a constraint
Definition OSResult.h:1603
DualVarValue ** con
con is a vector of DualVarValue objects that give an index and dual variable value for each constrain...
Definition OSResult.h:1666
int numberOfCon
record the number of constraints for which values are given
Definition OSResult.h:1660
used for throwing exceptions.
std::string errormsg
errormsg is the error that is causing the exception to be thrown
The Result Class.
Definition OSResult.h:2549
std::string getSolutionStatusType(int solIdx)
Get the [i]th optimization solution status type, where i equals the given solution index.
GeneralResult * general
general holds the first child of the OSResult specified by the OSrL Schema.
Definition OSResult.h:2561
OptimizationResult * optimization
optimization holds the fifth child of the OSResult specified by the OSrL Schema.
Definition OSResult.h:2581
std::string getGeneralMessage()
Get the general message.
int getSolutionNumber()
get the number of solutions.
int getConstraintNumber()
Get constraint number.
int getVariableNumber()
Get variable number.
std::string getGeneralStatusType()
Get the general status type, which can be: success, error, warning.
struct gmoRec * gmo
OSrL2Gams(struct gmoRec *gmo_)
Constructor.
struct gevRec * gev
void writeSolution(OSResult &osresult)
Writes a solution into a GMO with the result given as OSResult object.
The OSrLReader Class.
Definition OSrLReader.h:43
OSResult * readOSrL(const std::string &posrl)
Get an OSResult object from an OSrL string.
double value
the value of the objective indexed by idx
Definition OSResult.h:1292
ObjectiveValues * values
a pointer to an array of ObjectiveValues objects
Definition OSResult.h:1547
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
OptimizationSolution ** solution
solution is an array of pointers to OptimizationSolution objects
Definition OSResult.h:2500
The OptimizationSolution Class.
Definition OSResult.h:2264
VariableSolution * variables
variables holds the solution information for the variables
Definition OSResult.h:2291
ObjectiveSolution * objectives
objectives holds the solution information for the objectives
Definition OSResult.h:2300
ConstraintSolution * constraints
constraints holds the solution information for the constraints
Definition OSResult.h:2295
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
int idx
the index of a variable in the solution
Definition OSResult.h:1070
std::string name
the name of the result the user is defining
Definition OSResult.h:1139
OtherVarResult ** var
Definition OSResult.h:1162
int numberOfVar
the number of variables which have values for this particular type of result
Definition OSResult.h:1131
int idx
idx is the index on variable in the solution
Definition OSResult.h:852
double value
Definition OSResult.h:860
OtherVariableResult ** other
a pointer to an array of other pointer objects for variables
Definition OSResult.h:1238
int numberOfOtherVariableResults
the number of types of variable results other than the value of the variable
Definition OSResult.h:1224
VariableValues * values
a pointer to a VariableValues object
Definition OSResult.h:1227
VarValue ** var
a vector of VarValue objects, there will be one for each variable in the solution
Definition OSResult.h:912
int numberOfVar
the number of variable values that are in the solution
Definition OSResult.h:907
OSResult * osresult