Modelib: Model.h Source File

Page principale | Hiérarchie des classes | Liste des composants | Liste des fichiers | Composants

# Model.h

    00001 #ifndef __MODELE_H
    00002 #define __MODELE_H
    00003
    00004 #include "Config.h"
    00005 #include "Fraction.h"
    00006 #include "Constant.h"
    00007 #include "MuteVar.h"
    00008 #include "Paire.h"
    00009 #include "MatriceCreuse.h"
    00010 #include "Expr.h"
    00011 #include "ConstraintBuilder.h"
    00012 #include "InternalConstraint.h"
    00013 #include "Constraint.h"
    00014 #include "SolverAPI.h"
    00015 #include "LagrangianParam.h"
    00016 #include "Nom.h"
    00017
    00018 #ifdef USE_LATEX_FORMAT
    00019        #include "latexFile.h"
    00020 #endif
    00021
    00022 #ifdef USE_HTML_FORMAT
    00023        #include "htmlFile.h"
    00024 #endif
    00025
    00026 #include <map>
    00027 #include <string>
    00028 #include <vector>
    00029 #include <iostream>
    00030
    00032
    00058 class Model
    00059 {
    00060     VarId hacheVarId;
    00061     VarId hacheConstraintId;
    00062     std::map< VarId, Paire > unknowMap;
    00063     MatriceCreuse<float> A; // AX <= B, A*X = B, A*X < B
    00064     map<VarId, InternalConstraint> constraintMap;
    00065     map<VarId, std::string> comments;
    00066
    00067     bool isMax;
    00068     bool isRelaxed;
    00069     bool isFract;
    00070     bool isOptimal;
    00071     bool isSolved;
    00072     bool isNormalized;
    00073     unsigned MIPVar;
    00074
    00075     Expr objectif;
    00076     float objectifActivity;
    00077     std::string objectifName;
    00078     std::string problemName;
    00079
    00080     // gestion des solvers
    00081     SolverAPI solverAPI;
    00082     // Fraction...
    00083     FractionConvert _fract;
    00084
    00085 // Pointeurs de fonctions pour le polymorphisme
    00086 // pour la definission de Python
    00087
    00088 #ifdef COMPILE_FOR_PYTHON
    00089   public:
    00090         Constraint GetConstraint_1(VarId id) {return GetConstraint(id);}
    00091         Constraint GetConstraint_2(const std::string &_str) {return GetConstraint(_str);}
    00092 #endif
    00093
    00094   protected:
    00095     friend class LagrangianParam;
    00096     float GetLagrangianParamValue( VarId ) const;
    00097     void SetLagrangianParamValue(VarId , float);
    00098
    00099     friend class Constraint;
    00100
    00101
    00102     friend class NumVar;
    00103
    00104     VarId AddVar( const MuteVar & muteVar, const std::string &);
    00105     VarId GetVarId(const std::string &) const;
    00106     MuteVar GetMuteVar( VarId varId) const;
    00107     NumVar GetNumVar( VarId varId ) const;
    00108     std::string GetName( VarId varId ) const;
    00109     void SetName( VarId varId, const std::string & name);
    00110     float GetMuteVarLB( VarId varId) const;
    00111     void SetMuteVarLB( VarId varId, float _value);
    00112     float GetMuteVarUB( VarId varId) const;
    00113     void SetMuteVarUB( VarId varId, float _value);
    00114     void SetMuteVarLBStrict( VarId varId, bool _value);
    00115     void SetMuteVarUBStrict( VarId varId, bool _value);
    00116     MuteVar::VarType GetType( VarId varId ) const;
    00117     void SetType( VarId varId, MuteVar::VarType _type);
    00118
    00119     VarId AddExpr( Expr & expr, MatriceCreuse<float> & A);
    00120     Expr GetRelaxedConstraintExpr( VarId id, bool relaxed = true );
    00121
    00122     void ComputeObjectif();
    00123
    00124     std::ostream & SurchargeFlux(std::ostream&) const;
    00125     std::ostream & SurchargeFluxRelaxed(std::ostream&) const;
    00126
    00127     VarId GetConstraintId(const std::string &);
    00128
    00129   public:
    00131     Model()
    00132     : hacheVarId(1),hacheConstraintId(1),
    00133       isMax(true),isRelaxed(false),isFract(false),
    00134       isOptimal(false),isSolved(false),isNormalized(false),
    00135       MIPVar(0),objectifActivity(0)
    00136       {}
    00137     Model(const Model &);
    00138     Model & operator=(const Model &);
    00139
    00140     Constraint Add( const ConstraintBuilder &, const std::string & nom = "");
    00141     Constraint Add( const ConstraintBuilder & constraint, const Nom & _nom)
    00142     {
    00143         return Add(constraint,_nom());
    00144     }
    00145
    00146     void Maximize(const Expr & expr, const std::string & nom = "");
    00147     void Minimize(const Expr & expr, const std::string & nom = "");
    00148     Expr GetObjectif() const { return objectif;}
    00149
    00150     void SetProblemName( const std::string name = "") { problemName = name;}
    00151     string GetProblemName( ) const { return problemName;}
    00152
    00153     inline bool IsRelaxed() const {return isRelaxed;}
    00154     inline void SetRelaxed(bool _isRelaxed) { isRelaxed = _isRelaxed; }
    00155
    00156     bool RelaxInt2Float ( NumVar numVar = NumVar() );
    00157     bool RelaxBool2Float( NumVar numVar = NumVar() );
    00158
    00159     void SetNumVarValues(const std::list<std::string> & noms, const std::list<float> & values);
    00160     void SetConstraintValues(const std::list<std::string> & noms, const std::list<float> & values);
    00161     void GetInfluentLagrangianParam( std::list<LagrangianParam> & listViolated, std::list<LagrangianParam> & listSlacked);
    00162     std::vector<LagrangianParam> GetAllLagrangianParams();
    00163     float EvalueConstraint(VarId id ) const;
    00164
    00165     void UnrelaxModel();
    00166
    00167     //void Standardize(bool withoutStrictConstraintConversion = true,bool withOnlyPositiveVariable = true);
    00168
    00169     std::ostream & Out( std::ostream & flux = std::cout, const std::string & newline = "" );
    00170     std::ostream & OutDimensions( std::ostream & flux = std::cout, const std::string & newline = "" );
    00171     std::ostream & OutNumVarValues( bool withoutNullValue = true,std::ostream & flux = std::cout, const std::string & newline = "" ) const;
    00172     std::ostream & OutConstraintValues(std::ostream & flux = std::cout, const std::string & newline = "" ) const;
    00173     friend std::ostream& operator<<(std::ostream&,const Model&);
    00174
    00175     Constraint GetConstraint(VarId id) const;
    00176     Constraint GetConstraint(const std::string &);
    00177         bool RelaxConstraint( VarId id, float lagrangianValue = -Infinity );
    00178     bool Relax(const std::string & name, float lagrangianValue = -Infinity);
    00179     void RenameConstraint( VarId id, const std::string & name);
    00180
    00181     VarId GetMaxVarId() const { return hacheVarId;}
    00182     VarId GetMaxConstraintId() const { return hacheConstraintId;}
    00183
    00184     // Chargement d'un fichier LP
    00185     bool LoadLP(const std::string& , bool verbose = false);
    00186
    00187     void RemoveVariable(VarId varId);
    00188     void RemoveConstraint(VarId varId);
    00189     void AddToConstraint(VarId,const Expr &);
    00190     void SetSecondMember(VarId,float);
    00191     float GetSecondMember(VarId) const;
    00192     void Clear();
    00193     void Normalize(bool withoutStrict = true);
    00194     void PrintDual(const std::string&) const;
    00195     Model GetDual() const;
    00196
    00197     // interface du solver
    00198     void  InitSolver(API _API=GLPK,const std::string& _fLP="./tmp.solver.lp", const std::string& _path="")
    00199     {
    00200         solverAPI.Init(_fLP,_path);
    00201         SetAPI(_API);
    00202     }
    00203
    00204     float Solve();
    00205                  // Retourner les informations (Minimisation/Maximisation) et la phrase que sort GLPSOL
    00206                  // genre: 'INTEGER OPTIMAL'
    00207     void  SolveInfos(std::string& _operation, std::string& _informations)
    00208     {
    00209       solverAPI.Infos(_operation,_informations);
    00210     }
    00211
    00212     // Retourne l'occupation de la matrice
    00213     double  MatriceOccupation()
    00214     {
    00215       return (double(A.GetSize()) / double(A.GetNbCols() * A.GetNbLignes()));
    00216     }
    00217
    00218
    00219
    00220     void  SetAPI(API _API);
    00221     void  PushOptions(const std::string& _str) { solverAPI.Options(_str); }
    00222     void  RemoveOptions() {solverAPI.RemoveOptions();}
    00223
    00224     void  ShowMe(  bool withoutNullValue = true );
    00225
    00226     void  PrintFraction() {isFract = true;}
    00227     void  EndFraction()   {isFract = false;}
    00228
    00229     std::string AddComment( const std::string & comment );
    00230     void SetComment( VarId id, const std::string &  comment);
    00231     std::string GetComment( VarId id ) const;
    00232
    00233 #ifdef USE_HTML_FORMAT
    00234     void SortieHTML( const std::string & fileName, const std::string& matrixPNG="");
    00235 #endif
    00236
    00237 #ifdef USE_LATEX_FORMAT
    00238     void SortieLATEX( const std::string & fileName);
    00239 #endif
    00240
    00241 #ifdef EXPORT_SPARSE_MATRIX_PNG
    00242     void ExportSparseMatrixPNG(const std::string& _nom="./MatriceCreuse.png") const;
    00243 #endif
    00244
    00245
    00246 };
    00247
    00248 #ifdef COMPILE_FOR_PYTHON
    00249         void export_Model();
    00250 #endif
    00251
    00252 #endif