00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "OSResult.h"
00010 #include "OSosrl2gams.hpp"
00011 #include "OSrLReader.h"
00012 #include "OSErrorClass.h"
00013 #include "CoinHelperFunctions.hpp"
00014
00015 #ifdef HAVE_CSTRING
00016 #include <cstring>
00017 #else
00018 #ifdef HAVE_STRING_H
00019 #include <string.h>
00020 #else
00021 #error "don't have header file for string"
00022 #endif
00023 #endif
00024
00025 #include "gmomcc.h"
00026 #include "gevmcc.h"
00027
00028 OSrL2Gams::OSrL2Gams(gmoHandle_t gmo_)
00029 : gmo(gmo_), gev(gmo_ ? (gevHandle_t)gmoEnvironment(gmo_) : NULL)
00030 { }
00031
00032 void OSrL2Gams::writeSolution(OSResult& osresult)
00033 {
00034 if (osresult.general == NULL)
00035 {
00036 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00037 gmoSolveStatSet(gmo, SolveStat_SolverErr);
00038 gevLogStat(gev, "Error: OS result does not have header.");
00039 return;
00040 }
00041 else if (osresult.getGeneralStatusType() == "error")
00042 {
00043 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00044 gmoSolveStatSet(gmo, SolveStat_SolverErr);
00045 gevLogStatPChar(gev, "Error: OS result reports error: ");
00046 gevLogStat(gev, osresult.getGeneralMessage().c_str());
00047 return;
00048 }
00049 else if (osresult.getGeneralStatusType() == "warning")
00050 {
00051 gevLogStatPChar(gev, "Warning: OS result reports warning: ");
00052 gevLogStat(gev, osresult.getGeneralMessage().c_str());
00053 }
00054
00055 gmoSolveStatSet(gmo, SolveStat_Normal);
00056
00057 if (osresult.getSolutionNumber() == 0)
00058 {
00059 gmoModelStatSet(gmo, ModelStat_NoSolutionReturned);
00060 }
00061 else if (osresult.getSolutionStatusType(0) == "unbounded")
00062 {
00063 gmoModelStatSet(gmo, ModelStat_Unbounded);
00064 }
00065 else if (osresult.getSolutionStatusType(0) == "globallyOptimal")
00066 {
00067 gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
00068 }
00069 else if (osresult.getSolutionStatusType(0) == "locallyOptimal")
00070 {
00071 gmoModelStatSet(gmo, ModelStat_OptimalLocal);
00072 }
00073 else if (osresult.getSolutionStatusType(0) == "optimal")
00074 {
00075 gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
00076 }
00077 else if (osresult.getSolutionStatusType(0) == "bestSoFar")
00078 {
00079 gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed);
00080 }
00081 else if (osresult.getSolutionStatusType(0) == "feasible")
00082 {
00083 gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed);
00084 }
00085 else if (osresult.getSolutionStatusType(0) == "infeasible")
00086 {
00087 gmoModelStatSet(gmo, ModelStat_InfeasibleGlobal);
00088 }
00089 else if (osresult.getSolutionStatusType(0) == "stoppedByLimit")
00090 {
00091 gmoSolveStatSet(gmo, SolveStat_Iteration);
00092 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00093 }
00094 else if (osresult.getSolutionStatusType(0) == "unsure")
00095 {
00096 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00097 }
00098 else if (osresult.getSolutionStatusType(0) == "error")
00099 {
00100 gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
00101 }
00102 else if (osresult.getSolutionStatusType(0) == "other")
00103 {
00104 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00105 }
00106 else
00107 {
00108 gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
00109 }
00110
00111 if (osresult.getVariableNumber() != gmoN(gmo))
00112 {
00113 gevLogStat(gev, "Error: Number of variables in OS result does not match with gams model.");
00114 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00115 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00116 return;
00117 }
00118 if (osresult.getConstraintNumber() != gmoM(gmo))
00119 {
00120 gevLogStat(gev, "Error: Number of constraints in OS result does not match with gams model.");
00121 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00122 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00123 return;
00124 }
00125
00126 OptimizationSolution* sol = osresult.optimization->solution[0];
00127
00128 #define SMAG_DBL_NA -1E20 // there is a gmoNAinteger, but no gmoNAdouble
00129 int* colBasStat = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Bstat_Super);
00130 int* colIndic = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Cstat_OK);
00131 double* colMarg = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
00132 double* colLev = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
00133
00134 int* rowBasStat = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Bstat_Super);
00135 int* rowIndic = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Cstat_OK);
00136 double* rowLev = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
00137 double* rowMarg = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
00138
00139
00140
00141
00142
00143
00144 if (sol->constraints && sol->constraints->dualValues)
00145
00146
00147
00148 for (int i=0; i < sol->constraints->dualValues->numberOfCon; i++)
00149 rowMarg[sol->constraints->dualValues->con[i]->idx] = sol->constraints->dualValues->con[i]->value;
00150 if (sol->variables && sol->variables->values)
00151
00152
00153
00154 for (int i=0; i < sol->variables->values->numberOfVar; i++)
00155 colLev[sol->variables->values->var[i]->idx] = sol->variables->values->var[i]->value;
00156 if (sol->variables)
00157 for (int i=0; i<sol->variables->numberOfOtherVariableResults; ++i)
00158 {
00159 if (sol->variables->other[i]->name=="reduced costs")
00160 {
00161
00162
00163
00164 for (int j=0; j < sol->variables->other[i]->numberOfVar; j++)
00165 colLev[sol->variables->other[i]->var[j]->idx] = atof(sol->variables->other[i]->var[j]->value.c_str());
00166 break;
00167 }
00168 }
00169
00170 gmoSetSolution8(gmo, colLev, colMarg, rowMarg, rowLev, colBasStat, colIndic, rowBasStat, rowIndic);
00171
00172 delete[] rowLev;
00173 delete[] rowMarg;
00174 delete[] rowBasStat;
00175 delete[] rowIndic;
00176 delete[] colLev;
00177 delete[] colMarg;
00178 delete[] colBasStat;
00179 delete[] colIndic;
00180
00181 if (sol->objectives && sol->objectives->values && sol->objectives->values->obj[0])
00182 gmoSetHeadnTail(gmo, HobjVal, sol->objectives->values->obj[0]->value);
00183 }
00184
00185 void OSrL2Gams::writeSolution(std::string& osrl)
00186 {
00187 OSResult* osresult = NULL;
00188 OSrLReader osrl_reader;
00189 try
00190 {
00191 osresult = osrl_reader.readOSrL(osrl);
00192 }
00193 catch(const ErrorClass& error)
00194 {
00195 gevLogStat(gev, "Error parsing the OS result string:");
00196 gevLogStat(gev, error.errormsg.c_str());
00197 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00198 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00199 return;
00200 }
00201 if (osresult)
00202 writeSolution(*osresult);
00203 }