00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "Config.h"
00034 #include "FichierMPS.h"
00035 #include <iostream>
00036 #include <fstream>
00037 #include <sstream>
00038 #include <list>
00039 #include <string>
00040 #include <algorithm>
00041 #include <cctype>
00042 #include <map>
00043 #include <utility>
00044 #include <vector>
00045
00046 namespace Modelib {
00047
00048 using namespace std;
00049
00050 FichierMPS::FichierMPS()
00051 {
00052 objectifName="";
00053 isMax=false;
00054 integerMarker=false;
00055 }
00056
00057 bool FichierMPS::Open(const string& nomFile)
00058 {
00059 ifstream ifile(nomFile.c_str());
00060
00061 if (!ifile.is_open())
00062 return false;
00063
00064 int section = -1;
00065 string s;
00066
00067 map<string,int> sectionMap;
00068 map<string,int>::iterator sectionMapIter;
00069
00070 sectionMap["NAME"] = 0;
00071 sectionMap["ROWS"] = 1;
00072 sectionMap["COLUMNS"] = 2;
00073 sectionMap["RHS"] = 3;
00074 sectionMap["BOUNDS"] = 4;
00075 sectionMap["ENDATA"] = 5;
00076
00077 while (getline(ifile,s,'\n'))
00078 {
00079 istringstream ligne(s);
00080 string first;
00081
00082 ligne >> first;
00083
00084 sectionMapIter = sectionMap.find( first );
00085 if( sectionMapIter != sectionMap.end() && s[0] != ' ' )
00086 {
00087 if( section < sectionMapIter->second )
00088 {
00089 section = sectionMapIter->second;
00090 }
00091 if( section == 0 )
00092 {
00093 ligne >> nom;
00094 commentaires.push_back(pair<long,string>(0,"\\" + nom));
00095 }
00096 }
00097 else
00098 {
00099 switch(section)
00100 {
00101 case 0:
00102 break;
00103 case 1:
00104 ParseROWS(ligne.str());
00105 break;
00106 case 2:
00107 ParseCOLUMNS(ligne.str());
00108 break;
00109 case 3:
00110 ParseRHS(ligne.str());
00111 break;
00112 case 4:
00113 ParseBOUNDS(ligne.str());
00114 break;
00115 case 5:
00116 break;
00117 default:
00118 return false;
00119 break;
00120 }
00121 }
00122 }
00123 nom = objectifName;
00124 return true;
00125 }
00126
00127 void FichierMPS::ParseROWS(const std::string& row)
00128 {
00129 istringstream s(row);
00130
00131 string type;
00132 string name;
00133 unsigned comp=99;
00134
00135 s >> type >> name;
00136
00137 if( type == "N" )
00138 {
00139 if( objectifName == "" )
00140 {
00141 objectifName = name;
00142 }
00143 }
00144 else
00145 {
00146 if( type == "G" )
00147 {
00148 comp=2;
00149 }
00150 if( type == "L" )
00151 {
00152 comp=0;
00153 }
00154 if( type == "E" )
00155 {
00156 comp=5;
00157 }
00158
00159 std::map< std::string, plGlobalNode * >::iterator constraintMapIter = constraintMap.find(name);
00160 if( constraintMapIter == constraintMap.end() )
00161 {
00162 plGlobalNode * newCons = new plGlobalNode(name,list<plExprNode *>(), plSimpleConstraint("",comp));
00163 exprConstraint.push_back( newCons );
00164 constraintMap[name]=newCons;
00165 }
00166 }
00167 }
00168
00169 void FichierMPS::ParseCOLUMNS(const std::string& col)
00170 {
00171 istringstream s(col);
00172 string variable;
00173 string constraint;
00174 string coefficient;
00175
00176
00177 s >> variable;
00178
00179 while( s >> constraint >> coefficient )
00180 {
00181 if( constraint == "'MARKER'" )
00182 {
00183 if( coefficient == "'INTORG'")
00184 integerMarker = true;
00185 else
00186 integerMarker = false;
00187 return;
00188 }
00189 else
00190 {
00191 if( constraint == objectifName )
00192 {
00193 exprObj.push_back( new plExprNode(variable, coefficient, PLUS ) );
00194 }
00195 else
00196 {
00197 std::map< std::string, plGlobalNode * >::iterator constraintMapIter = constraintMap.find(constraint);
00198 if( constraintMapIter != constraintMap.end() )
00199 {
00200 constraintMapIter->second->expr.push_back( new plExprNode(variable, coefficient, PLUS ) );
00201 }
00202 }
00203 }
00204 }
00205
00206 if( integerMarker )
00207 {
00208 exprGeneral.push_back( variable );
00209 }
00210 }
00211
00212 void FichierMPS::ParseRHS(const std::string& rhs)
00213 {
00214 istringstream s(rhs);
00215 string constraint;
00216 string coefficient;
00217 string rhsName;
00218
00219 s >> rhsName;
00220 while( s >> constraint >> coefficient )
00221 {
00222 std::map< std::string, plGlobalNode * >::iterator constraintMapIter = constraintMap.find(constraint);
00223 if( constraintMapIter != constraintMap.end() )
00224 {
00225 constraintMapIter->second->cont.bound = coefficient;
00226 }
00227 }
00228 }
00229
00230 void FichierMPS::ParseBOUNDS(const std::string& bnd )
00231 {
00232 istringstream s(bnd);
00233
00234 string variable;
00235 string coefficient = "-Inf";
00236 string bndName;
00237 string bndType;
00238
00239 s >> bndType >> bndName >> variable >> coefficient;
00240
00241 if( bndType == "BV" )
00242 {
00243 exprBinary.push_back(variable);
00244 return;
00245 }
00246
00247 plConstraintNode * tmp;
00248 if( bndType == "MI" )
00249 {
00250 tmp = new plConstraintNode("1",variable,"-inf",LOWER_THAN );
00251 }
00252
00253 if( bndType == "LO" || bndType == "LI")
00254 {
00255 tmp = new plConstraintNode("1",variable,coefficient,LOWER_THAN );
00256 }
00257
00258 if( bndType == "UP" || bndType == "UI" )
00259 {
00260 tmp = new plConstraintNode(variable,"1",coefficient,"inf",2,99);
00261 }
00262
00263 if( bndType == "FX")
00264 {
00265 tmp = new plConstraintNode("1",variable,EQUAL,coefficient);
00266 }
00267
00268 if( bndType == "FR")
00269 {
00270 tmp = new plConstraintNode (variable,"1","-inf","inf",LOWER_THAN,LOWER_THAN);
00271 }
00272
00273 exprBounds.push_back( tmp );
00274 }
00275
00276 }