FLOPC++
|
00001 // ******************** FlopCpp ********************************************** 00002 // File: MP_model.hpp 00003 // $Id$ 00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt) 00005 // Copyright (C) 2003 Tim Helge Hultberg 00006 // All Rights Reserved. 00007 // **************************************************************************** 00008 00009 #ifndef _MP_model_hpp_ 00010 #define _MP_model_hpp_ 00011 00012 #include <ostream> 00013 #include <vector> 00014 #include <set> 00015 using std::vector; 00016 using std::set; 00017 00018 #include "MP_expression.hpp" 00019 #include "MP_constraint.hpp" 00020 #include <CoinPackedVector.hpp> 00021 class OsiSolverInterface; 00022 00023 namespace flopc { 00024 00025 class MP_variable; 00026 class MP_index; 00027 class MP_set; 00028 00036 class Messenger { 00037 public: 00038 virtual void logMessage(int level, const char * const msg){} 00039 friend class MP_model; 00040 private: 00041 virtual void constraintDebug(string name, const vector<MP::Coef>& cfs) {} 00042 virtual void objectiveDebug(const vector<MP::Coef>& cfs) {} 00043 virtual void statistics(int bm, int m, int bn, int n, int nz) {} 00044 virtual void generationTime(double t) {} 00045 protected: 00046 virtual ~Messenger() {} 00047 }; 00048 00052 class NormalMessenger : public Messenger { 00053 friend class MP_model; 00054 private: 00055 virtual void statistics(int bm, int m, int bn, int n, int nz); 00056 virtual void generationTime(double t); 00057 }; 00058 00062 class VerboseMessenger : public NormalMessenger { 00063 friend class MP_model; 00064 private: 00065 virtual void constraintDebug(string name, const vector<MP::Coef>& cfs); 00066 virtual void objectiveDebug(const vector<MP::Coef>& cfs); 00067 }; 00068 00090 class MP_model { 00091 friend class MP_constraint; 00092 public: 00094 typedef enum {MINIMIZE=1, MAXIMIZE=-1} MP_direction; 00095 00098 typedef enum { 00100 OPTIMAL, 00102 PRIMAL_INFEASIBLE, 00104 DUAL_INFEASIBLE, 00107 ABANDONED, 00110 SOLVER_ONLY, 00112 ATTACHED, 00114 DETACHED 00115 } MP_status; 00116 00118 MP_model(OsiSolverInterface* s, Messenger* m = new NormalMessenger); 00119 00120 ~MP_model(); 00121 00129 MP_status getStatus()const { 00130 return mSolverState; 00131 } 00133 void silent() { 00134 delete messenger; 00135 messenger = new Messenger; 00136 } 00138 void verbose() { 00139 delete messenger; 00140 messenger = new VerboseMessenger; 00141 } 00142 00144 void setSolver(OsiSolverInterface* s) { 00145 Solver = s; 00146 } 00147 00149 OsiSolverInterface* operator->() { 00150 return Solver; 00151 } 00152 00154 MP_model& add(MP_constraint& constraint); 00155 00159 void maximize(); 00163 void maximize(const MP_expression &obj); 00167 void minimize(); 00171 void minimize(const MP_expression &obj); 00172 00176 void minimize_max(MP_set& d, const MP_expression &obj); 00177 00179 void setObjective(const MP_expression& o); 00190 void attach(OsiSolverInterface *solver = 0); 00197 void detach(); 00205 MP_model::MP_status solve(const MP_model::MP_direction &dir); 00213 double getInfinity() const; 00214 00216 void add(MP_variable* v); 00218 void addRow(const Constraint& constraint); 00219 00223 static MP_model &getDefaultModel(); 00227 static MP_model *getCurrentModel(); 00230 Messenger *getMessenger(){ 00231 return messenger; 00232 } 00233 private: 00234 typedef std::set<MP_variable* >::iterator varIt; 00235 typedef std::set<MP_constraint* >::iterator conIt; 00236 static MP_model& default_model; 00237 static MP_model* current_model; 00238 MP_model(const MP_model&); 00239 MP_model& operator=(const MP_model&); 00240 00241 Messenger* messenger; 00242 00243 00244 static void assemble(vector<MP::Coef>& v, vector<MP::Coef>& av); 00245 void add(MP_constraint* constraint); 00246 MP_expression Objective; 00247 set<MP_constraint *> Constraints; 00248 set<MP_variable *> Variables; 00249 public: 00251 OsiSolverInterface* Solver; 00252 private: 00253 int m; 00254 int n; 00255 int nz; 00256 int *Cst; 00257 int *Clg; 00258 int *Rnr; 00259 double *Elm; 00260 double *bl; 00261 double *bu; 00262 double *c; 00263 double *l; 00264 double *u; 00265 MP_status mSolverState; 00266 00267 }; 00268 00270 std::ostream &operator<<(std::ostream &os, 00271 const MP_model::MP_status &condition); 00273 std::ostream &operator<<(std::ostream &os, 00274 const MP_model::MP_direction &direction); 00275 00276 } // End of namespace flopc 00277 #endif