- Main Page
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/OS-2.3.5/OS/src/OSModelInterfaces/OSosrl2gams.cpp

Go to the documentation of this file.

    00001 // Copyright (C) GAMS Development and others 2008-2009
    00002 // All Rights Reserved.
    00003 // This code is published under the Common Public License.
    00004 //
    00005 // $Id: OSosrl2gams.cpp 2925 2009-09-18 19:47:25Z Gassmann $
    00006 //
    00007 // Authors: Stefan Vigerske
    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);     // or should we report integer solution if integer var.?
    00063         } else if (osresult.getSolutionStatusType(0) == "feasible") {
    00064                 gmoModelStatSet(gmo, ModelStat_NonOptimalIntermed);     // or should we report integer solution if integer var.?
    00065         } else if (osresult.getSolutionStatusType(0) == "infeasible") {
    00066                 gmoModelStatSet(gmo, ModelStat_InfeasibleGlobal);
    00067         } else if (osresult.getSolutionStatusType(0) == "stoppedByLimit") {
    00068                 gmoSolveStatSet(gmo, SolveStat_Iteration); // just a guess
    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         //TODO
    00107 //      if (sol->constraints && sol->constraints->values) // set row levels, if available
    00108 //              for (std::vector<ConValue*>::iterator it(sol->constraints->values->con.begin());
    00109 //              it!=sol->constraints->values->con.end(); ++it)
    00110 //                      rowLev[(*it)->idx]=(*it)->value;
    00111         if (sol->constraints && sol->constraints->dualValues) // set row dual values, if available
    00112 //              for (std::vector<DualVarValue*>::iterator it(sol->constraints->dualValues->con.begin());
    00113 //              it!=sol->constraints->dualValues->con.end(); ++it)
    00114 //                      rowMarg[(*it)->idx]=(*it)->value; // what are it->lbValue and it->ubValue ?
    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) // set var values, if available
    00118 //              for (std::vector<VarValue*>::const_iterator it(sol->variables->values->var.begin());
    00119 //              it!=sol->variables->values->var.end(); ++it)
    00120 //                      colLev[(*it)->idx]=(*it)->value;
    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 //                              for (std::vector<OtherVarResult*>::const_iterator it(sol->variables->other[i]->var.begin());
    00127 //                              it!=sol->variables->other[i]->var.end(); ++it)
    00128 //                                      colMarg[(*it)->idx]=atof((*it)->value.c_str());
    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 }