Modelib: Fichier source de Model.h

Page principale | Liste des namespaces | Hiérarchie des classes | Liste alphabétique | Liste des classes | Liste des fichiers | Membres de namespace | Membres de classe | Membres de fichier

# Model.h

Aller à la documentation de ce fichier.

    00001 /*
    00002     Copyright (c) 2005, Quentin Lequy and Romain Gaucher
    00003     All rights reserved.
    00004
    00005     Redistribution and use in source and binary forms, with or
    00006     without modification, are permitted provided that the following
    00007     conditions are met:
    00008
    00009         * Redistributions of source code must retain the above copyright
    00010           notice, this list of conditions and the following disclaimer.
    00011
    00012         * Redistributions in binary form must reproduce the above copyright
    00013           notice, this list of conditions and the following disclaimer in the
    00014           documentation and/or other materials provided with the distribution.
    00015
    00016         * Neither the name of Quentin Lequy or Romain Gaucher nor the names
    00017           of its contributors may be used to endorse or promote products
    00018           derived from this software without specific prior written permission.
    00019
    00020     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    00021     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00022     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00023     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    00024     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00025     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    00026     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
    00027     AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00028     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00029     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00030 */
    00031
    00032 #ifndef __MODELE_H
    00033 #define __MODELE_H
    00034
    00035 #include "Config.h"
    00036 #include "Fraction.h"
    00037 #include "Constant.h"
    00038 #include "MuteVar.h"
    00039 #include "Paire.h"
    00040 #include "MatriceCreuse.h"
    00041 #include "Expr.h"
    00042 #include "ConstraintBuilder.h"
    00043 #include "InternalConstraint.h"
    00044 #include "Constraint.h"
    00045 #include "SolverAPI.h"
    00046 #include "LagrangianParam.h"
    00047 #include "Nom.h"
    00048
    00049 #ifdef USE_LATEX_FORMAT
    00050        #include "latexFile.h"
    00051 #endif
    00052
    00053 #ifdef USE_HTML_FORMAT
    00054        #include "htmlFile.h"
    00055 #endif
    00056
    00057 #include <map>
    00058 #include <string>
    00059 #include <vector>
    00060 #include <iostream>
    00061
    00062 namespace Modelib {
    00063
    00065
    00091 class Model
    00092 {
    00093     VarId hacheVarId;
    00094     VarId hacheConstraintId;
    00095     std::map< VarId, Paire > unknowMap;
    00096     MatriceCreuse<float> A; // AX <= B, A*X = B, A*X < B
    00097     map<VarId, InternalConstraint> constraintMap;
    00098     map<VarId, std::string> comments;
    00099
    00100     bool isMax;
    00101     bool isRelaxed;
    00102     bool isFract;
    00103     bool isOptimal;
    00104     bool isSolved;
    00105     bool isNormalized;
    00106     unsigned MIPVar;
    00107
    00108     Expr objectif;
    00109     float objectifActivity;
    00110     std::string objectifName;
    00111     std::string problemName;
    00112
    00113     // gestion des solvers
    00114     SolverAPI solverAPI;
    00115     // Fraction...
    00116     FractionConvert _fract;
    00117
    00118 // Pointeurs de fonctions pour le polymorphisme
    00119 // pour la definission de Python
    00120
    00121 #ifdef COMPILE_FOR_PYTHON
    00122   public:
    00123     Constraint GetConstraint_1(VarId id) {return GetConstraint(id);}
    00124     Constraint GetConstraint_2(const std::string &_str) {return GetConstraint(_str);}
    00125 #endif
    00126
    00127   protected:
    00128     friend class LagrangianParam;
    00129     float GetLagrangianParamValue( VarId ) const;
    00130     void SetLagrangianParamValue(VarId , float);
    00131
    00132     friend class Constraint;
    00133
    00134
    00135     friend class NumVar;
    00136
    00137     VarId AddVar( const MuteVar & muteVar, const std::string &);
    00138     VarId GetVarId(const std::string &) const;
    00139     MuteVar GetMuteVar( VarId varId) const;
    00140     NumVar GetNumVar( VarId varId ) const;
    00141     std::string GetName( VarId varId ) const;
    00142     void SetName( VarId varId, const std::string & name);
    00143     float GetMuteVarLB( VarId varId) const;
    00144     void SetMuteVarLB( VarId varId, float _value);
    00145     float GetMuteVarUB( VarId varId) const;
    00146     void SetMuteVarUB( VarId varId, float _value);
    00147     void SetMuteVarLBStrict( VarId varId, bool _value);
    00148     void SetMuteVarUBStrict( VarId varId, bool _value);
    00149     MuteVar::VarType GetType( VarId varId ) const;
    00150     void SetType( VarId varId, MuteVar::VarType _type);
    00151
    00152     VarId AddExpr( Expr & expr, MatriceCreuse<float> & A);
    00153     Expr GetRelaxedConstraintExpr( VarId id, bool relaxed = true );
    00154
    00155     void ComputeObjectif();
    00156
    00157     std::ostream & SurchargeFlux(std::ostream&) const;
    00158     std::ostream & SurchargeFluxRelaxed(std::ostream&) const;
    00159
    00160     std::ostream & SurchargeMPSFlux(std::ostream&,bool verbose) const;
    00161
    00162     VarId GetConstraintId(const std::string &);
    00163
    00164   public:
    00166     Model()
    00167     : hacheVarId(1),hacheConstraintId(1),
    00168       isMax(true),isRelaxed(false),isFract(false),
    00169       isOptimal(false),isSolved(false),isNormalized(false),
    00170       MIPVar(0),objectifActivity(0)
    00171       {}
    00172     Model(const Model &);
    00173     Model & operator=(const Model &);
    00174
    00175     Constraint Add( const ConstraintBuilder &, const std::string & nom = "");
    00176     Constraint Add( const ConstraintBuilder & constraint, const Nom & _nom)
    00177     {
    00178         return Add(constraint,_nom());
    00179     }
    00180
    00181     void Maximize(const Expr & expr, const std::string & nom = "");
    00182     void Minimize(const Expr & expr, const std::string & nom = "");
    00183     Expr GetObjectif() const { return objectif;}
    00184
    00185     void SetProblemName( const std::string name = "") { problemName = name;}
    00186     string GetProblemName( ) const { return problemName;}
    00187
    00188     inline bool IsRelaxed() const {return isRelaxed;}
    00189     inline void SetRelaxed(bool _isRelaxed) { isRelaxed = _isRelaxed; }
    00190
    00191     bool RelaxInt2Float ( NumVar numVar = NumVar() );
    00192     bool RelaxBool2Float( NumVar numVar = NumVar() );
    00193
    00194     void SetNumVarValues(const std::list<std::string> & noms, const std::list<float> & values);
    00195     void SetConstraintValues(const std::list<std::string> & noms, const std::list<float> & values);
    00196     void GetInfluentLagrangianParam( std::list<LagrangianParam> & listViolated, std::list<LagrangianParam> & listSlacked);
    00197     std::vector<LagrangianParam> GetAllLagrangianParams();
    00198     float EvalConstraint(VarId id ) const;
    00199
    00200     void UnrelaxModel();
    00201
    00202     //void Standardize(bool withoutStrictConstraintConversion = true,bool withOnlyPositiveVariable = true);
    00203
    00204     std::ostream & Out( std::ostream & flux = std::cout, const std::string & newline = "" );
    00205     std::ostream & OutDimensions( std::ostream & flux = std::cout, const std::string & newline = "" );
    00206     std::ostream & OutNumVarValues( bool withoutNullValue = true,std::ostream & flux = std::cout, const std::string & newline = "" ) const;
    00207     std::ostream & OutConstraintValues(std::ostream & flux = std::cout, const std::string & newline = "" ) const;
    00208     friend std::ostream& operator<<(std::ostream&, const Model&);
    00209     friend std::ostream & ToMPS(std::ostream&, const Model&,bool verbose=true);
    00210
    00211     Constraint GetConstraint(VarId id) const;
    00212     Constraint GetConstraint(const std::string &);
    00213     bool RelaxConstraint( VarId id, float lagrangianValue = -Infinity );
    00214     bool Relax(const std::string & name, float lagrangianValue = -Infinity);
    00215     void RenameConstraint( VarId id, const std::string & name);
    00216
    00217     VarId GetMaxVarId() const { return hacheVarId;}
    00218     VarId GetMaxConstraintId() const { return hacheConstraintId;}
    00219
    00220     // Chargement d'un fichier LP
    00221     bool LoadLP(const std::string& , bool verbose = false);
    00222     bool LoadMPS(const std::string& , bool verbose = false);
    00223
    00224     void RemoveVariable(VarId varId);
    00225     void RemoveConstraint(VarId varId);
    00226     void AddToConstraint(VarId,const Expr &);
    00227     void SetSecondMember(VarId,float);
    00228     float GetSecondMember(VarId) const;
    00229     void Clear();
    00230     void Normalize(bool withoutStrict = true);
    00231     void PrintDual(const std::string&) const;
    00232     Model GetDual() const;
    00233
    00234     // interface du solver
    00235     void  InitSolver(API _API=GLPK,const std::string& _fLP="./tmp.solver.lp", const std::string& _path="")
    00236     {
    00237         solverAPI.Init(_fLP,_path);
    00238         SetAPI(_API);
    00239     }
    00240
    00241     float Solve();
    00242          // Retourner les informations (Minimisation/Maximisation) et la phrase que sort GLPSOL
    00243          // genre: 'INTEGER OPTIMAL'
    00244     void  SolveInfos(std::string& _operation, std::string& _informations)
    00245     {
    00246       solverAPI.Infos(_operation,_informations);
    00247     }
    00248
    00249     // Retourne l'occupation de la matrice
    00250     double  MatriceOccupation()
    00251     {
    00252       return (double(A.GetSize()) / double(A.GetNbCols() * A.GetNbLignes()));
    00253     }
    00254
    00255
    00256
    00257     void  SetAPI(API _API);
    00258     void  PushOptions(const std::string& _str) { solverAPI.Options(_str); }
    00259     void  RemoveOptions() {solverAPI.RemoveOptions();}
    00260
    00261     void  ShowMe(  bool withoutNullValue = true );
    00262
    00263     void  PrintFraction() {isFract = true;}
    00264     void  EndFraction()   {isFract = false;}
    00265
    00266     std::string AddComment( const std::string & comment );
    00267     void SetComment( VarId id, const std::string &  comment);
    00268     std::string GetComment( VarId id ) const;
    00269
    00270 #ifdef USE_HTML_FORMAT
    00271     void SortieHTML( const std::string & fileName, const std::string& matrixPNG="");
    00272 #endif
    00273
    00274 #ifdef USE_LATEX_FORMAT
    00275     void SortieLATEX( const std::string & fileName);
    00276 #endif
    00277
    00278 #ifdef EXPORT_SPARSE_MATRIX_PNG
    00279     void ExportSparseMatrixPNG(const std::string& _nom="./MatriceCreuse.png") const;
    00280 #endif
    00281
    00282
    00283 };
    00284
    00285 #ifdef COMPILE_FOR_PYTHON
    00286     void export_Model();
    00287 #endif
    00288
    00289 }
    00290
    00291 #endif