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 if (osresult.general == NULL) {
00034 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00035 gmoSolveStatSet(gmo, SolveStat_SolverErr);
00036 gevLogStat(gev, "Error: OS result does not have header.");
00037 return;
00038 } else if (osresult.getGeneralStatusType() == "error") {
00039 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00040 gmoSolveStatSet(gmo, SolveStat_SolverErr);
00041 gevLogStatPChar(gev, "Error: OS result reports error: ");
00042 gevLogStat(gev, osresult.getGeneralMessage().c_str());
00043 return;
00044 } else if (osresult.getGeneralStatusType() == "warning") {
00045 gevLogStatPChar(gev, "Warning: OS result reports warning: ");
00046 gevLogStat(gev, osresult.getGeneralMessage().c_str());
00047 }
00048
00049 gmoSolveStatSet(gmo, SolveStat_Normal);
00050
00051 if (osresult.getSolutionNumber() == 0) {
00052 gmoModelStatSet(gmo, ModelStat_NoSolutionReturned);
00053 } else if (osresult.getSolutionStatusType(0) == "unbounded") {
00054 gmoModelStatSet(gmo, ModelStat_Unbounded);
00055 } else if (osresult.getSolutionStatusType(0) == "globallyOptimal") {
00056 gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
00057 } else if (osresult.getSolutionStatusType(0) == "locallyOptimal") {
00058 gmoModelStatSet(gmo, ModelStat_OptimalLocal);
00059 } else if (osresult.getSolutionStatusType(0) == "optimal") {
00060 gmoModelStatSet(gmo, ModelStat_OptimalGlobal);
00061 } else if (osresult.getSolutionStatusType(0) == "bestSoFar") {
00062 gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed);
00063 } else if (osresult.getSolutionStatusType(0) == "feasible") {
00064 gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed);
00065 } else if (osresult.getSolutionStatusType(0) == "infeasible") {
00066 gmoModelStatSet(gmo, ModelStat_InfeasibleGlobal);
00067 } else if (osresult.getSolutionStatusType(0) == "stoppedByLimit") {
00068 gmoSolveStatSet(gmo, SolveStat_Iteration);
00069 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00070 } else if (osresult.getSolutionStatusType(0) == "unsure") {
00071 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00072 } else if (osresult.getSolutionStatusType(0) == "error") {
00073 gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
00074 } else if (osresult.getSolutionStatusType(0) == "other") {
00075 gmoModelStatSet(gmo, ModelStat_InfeasibleIntermed);
00076 } else {
00077 gmoModelStatSet(gmo, ModelStat_ErrorUnknown);
00078 }
00079
00080 if (osresult.getVariableNumber() != gmoN(gmo)) {
00081 gevLogStat(gev, "Error: Number of variables in OS result does not match with gams model.");
00082 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00083 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00084 return;
00085 }
00086 if (osresult.getConstraintNumber() != gmoM(gmo)) {
00087 gevLogStat(gev, "Error: Number of constraints in OS result does not match with gams model.");
00088 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00089 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00090 return;
00091 }
00092
00093 OptimizationSolution* sol = osresult.optimization->solution[0];
00094
00095 #define SMAG_DBL_NA -1E20 // there is a gmoNAinteger, but no gmoNAdouble
00096 int* colBasStat = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Bstat_Super);
00097 int* colIndic = CoinCopyOfArray((int*)NULL, gmoN(gmo), (int)Cstat_OK);
00098 double* colMarg = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
00099 double* colLev = CoinCopyOfArray((double*)NULL, gmoN(gmo), SMAG_DBL_NA);
00100
00101 int* rowBasStat = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Bstat_Super);
00102 int* rowIndic = CoinCopyOfArray((int*)NULL, gmoM(gmo), (int)Cstat_OK);
00103 double* rowLev = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
00104 double* rowMarg = CoinCopyOfArray((double*)NULL, gmoM(gmo), SMAG_DBL_NA);
00105
00106
00107
00108
00109
00110
00111 if (sol->constraints && sol->constraints->dualValues)
00112
00113
00114
00115 for (int i=0; i < sol->constraints->dualValues->numberOfCon; i++)
00116 rowMarg[sol->constraints->dualValues->con[i]->idx] = sol->constraints->dualValues->con[i]->value;
00117 if (sol->variables && sol->variables->values)
00118
00119
00120
00121 for (int i=0; i < sol->variables->values->numberOfVar; i++)
00122 colLev[sol->variables->values->var[i]->idx] = sol->variables->values->var[i]->value;
00123 if (sol->variables)
00124 for (int i=0; i<sol->variables->numberOfOtherVariableResults; ++i) {
00125 if (sol->variables->other[i]->name=="reduced costs") {
00126
00127
00128
00129 for (int j=0; j < sol->variables->other[i]->numberOfVar; j++)
00130 colLev[sol->variables->other[i]->var[j]->idx] = atof(sol->variables->other[i]->var[j]->value.c_str());
00131 break;
00132 }
00133 }
00134
00135 gmoSetSolution8(gmo, colLev, colMarg, rowMarg, rowLev, colBasStat, colIndic, rowBasStat, rowIndic);
00136
00137 delete[] rowLev;
00138 delete[] rowMarg;
00139 delete[] rowBasStat;
00140 delete[] rowIndic;
00141 delete[] colLev;
00142 delete[] colMarg;
00143 delete[] colBasStat;
00144 delete[] colIndic;
00145
00146 if (sol->objectives && sol->objectives->values && sol->objectives->values->obj[0])
00147 gmoSetHeadnTail(gmo, HobjVal, sol->objectives->values->obj[0]->value);
00148 }
00149
00150 void OSrL2Gams::writeSolution(std::string& osrl) {
00151 OSResult* osresult = NULL;
00152 OSrLReader osrl_reader;
00153 try {
00154 osresult = osrl_reader.readOSrL(osrl);
00155 } catch(const ErrorClass& error) {
00156 gevLogStat(gev, "Error parsing the OS result string:");
00157 gevLogStat(gev, error.errormsg.c_str());
00158 gmoModelStatSet(gmo, ModelStat_ErrorNoSolution);
00159 gmoSolveStatSet(gmo, SolveStat_SystemErr);
00160 return;
00161 }
00162 if (osresult)
00163 writeSolution(*osresult);
00164 }