MP_model.hpp
Go to the documentation of this file.
1 // ******************** FlopCpp **********************************************
2 // File: MP_model.hpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 // ****************************************************************************
8 
9 #ifndef _MP_model_hpp_
10 #define _MP_model_hpp_
11 
12 #include <ostream>
13 #include <vector>
14 #include <set>
15 #include <string>
16 
17 #include "MP_expression.hpp"
18 #include "MP_constraint.hpp"
19 #include <CoinPackedVector.hpp>
20 class OsiSolverInterface;
21 
22 namespace flopc {
23 
24  class MP_variable;
25  class MP_index;
26  class MP_set;
27 
35  class Messenger {
36  public:
37  virtual void logMessage(int level, const char * const msg){}
38  friend class MP_model;
39  private:
40  virtual void constraintDebug(std::string name, const std::vector<Coef>& cfs) {}
41  virtual void objectiveDebug(const std::vector<Coef>& cfs) {}
42  virtual void statistics(int bm, int m, int bn, int n, int nz) {}
43  virtual void generationTime(double t) {}
44  protected:
45  virtual ~Messenger() {}
46  };
47 
51  class NormalMessenger : public Messenger {
52  friend class MP_model;
53  private:
54  virtual void statistics(int bm, int m, int bn, int n, int nz);
55  virtual void generationTime(double t);
56  };
57 
62  friend class MP_model;
63  private:
64  virtual void constraintDebug(std::string name, const std::vector<Coef>& cfs);
65  virtual void objectiveDebug(const std::vector<Coef>& cfs);
66  };
67 
89  class MP_model {
90  friend class MP_constraint;
91  public:
93  typedef enum {MINIMIZE=1, MAXIMIZE=-1} MP_direction;
94 
97  typedef enum {
114  } MP_status;
115 
118 
120  delete messenger;
121  }
122 
131  return mSolverState;
132  }
134  void silent() {
135  delete messenger;
136  messenger = new Messenger;
137  }
139  void verbose() {
140  delete messenger;
142  }
143 
146  Solver = s;
147  }
148 
151  return Solver;
152  }
153 
156 
160  void maximize();
164  void maximize(const MP_expression &obj);
168  void minimize();
172  void minimize(const MP_expression &obj);
173 
177  void minimize_max(MP_set& d, const MP_expression &obj);
178 
180  void setObjective(const MP_expression& o);
191  void attach(OsiSolverInterface *solver=NULL);
198  void detach();
211  const double* solution;
212  const double* reducedCost;
213  const double* rowPrice;
214  const double* rowActivity;
218  double getInfinity() const;
219 
221  void add(MP_variable* v);
223  void addRow(const Constraint& c);
224 
228  static MP_model &getDefaultModel();
232  static MP_model *getCurrentModel();
236  return messenger;
237  }
238  private:
239  typedef std::set<MP_variable* >::iterator varIt;
240  typedef std::set<MP_constraint* >::iterator conIt;
243  MP_model(const MP_model&);
244  MP_model& operator=(const MP_model&);
245 
247 
248 
249  static void assemble(std::vector<Coef>& v, std::vector<Coef>& av);
250  void add(MP_constraint* c);
252  std::set<MP_constraint *> Constraints;
253  std::set<MP_variable *> Variables;
254  public:
257  private:
258  int m;
259  int n;
260  int nz;
261  int *Cst;
262  int *Clg;
263  int *Rnr;
264  double *Elm;
265  double *bl;
266  double *bu;
267  double *c;
268  double *l;
269  double *u;
271  };
272 
274  std::ostream &operator<<(std::ostream &os,
275  const MP_model::MP_status &condition);
277  std::ostream &operator<<(std::ostream &os,
278  const MP_model::MP_direction &direction);
279 
280 } // End of namespace flopc
281 #endif
A solver is placed in the constructor, but it is not yet attached or solved.
Definition: MP_model.hpp:109
This is the anchor point for all constructs in a FlopC++ model.
Definition: MP_model.hpp:89
std::set< MP_variable * > Variables
Definition: MP_model.hpp:253
Messenger * getMessenger()
Gets the current messenger.
Definition: MP_model.hpp:235
void detach()
detaches an OsiSolverInterface object from the model.
virtual void statistics(int bm, int m, int bn, int n, int nz)
static MP_model * current_model
Definition: MP_model.hpp:242
MP_status mSolverState
Definition: MP_model.hpp:270
Internal use: used when Verbose output is selected.
Definition: MP_model.hpp:61
static MP_model & default_model
Definition: MP_model.hpp:241
Internal use: used when Normal output is selected.
Definition: MP_model.hpp:51
virtual void generationTime(double t)
Definition: MP_model.hpp:43
std::ostream & operator<<(std::ostream &os, const MP_model::MP_status &condition)
allows print of result from call to solve();
MP_direction
used when calling the solve() method.
Definition: MP_model.hpp:93
virtual void objectiveDebug(const std::vector< Coef > &cfs)
Definition: MP_model.hpp:41
MP_model::MP_status solve(const MP_model::MP_direction &dir)
calls the appropriate solving methods in the OsiSolverInterface.
void addRow(const Constraint &c)
Adds a constraint to the MP_model.
MP_expression Objective
Definition: MP_model.hpp:251
Semantic representation of a linear constraint.
static MP_model * getCurrentModel()
Can be used to get the current model.
MP_model & operator=(const MP_model &)
virtual void objectiveDebug(const std::vector< Coef > &cfs)
static MP_model & getDefaultModel()
Can be used to get the default model.
std::set< MP_variable * >::iterator varIt
Definition: MP_model.hpp:239
virtual void constraintDebug(std::string name, const std::vector< Coef > &cfs)
void verbose()
used to help understanding and debugging FlopC++&#39;s behavior.
Definition: MP_model.hpp:139
MP_status getStatus() const
Returns the current status of the model-solver interaction.
Definition: MP_model.hpp:130
static void assemble(std::vector< Coef > &v, std::vector< Coef > &av)
if solve is called and solver finds model primal infeasible.
Definition: MP_model.hpp:101
MP_model(OsiSolverInterface *s, Messenger *m=new NormalMessenger)
Constructs an MP_model from an OsiSolverInterface *.
std::set< MP_constraint * > Constraints
Definition: MP_model.hpp:252
A solver is attached, but not yet solved.
Definition: MP_model.hpp:111
No solver is attached.
Definition: MP_model.hpp:113
Representation of a set for indexing into some other construct.
Definition: MP_set.hpp:78
Inteface for hooking up to internal flopc++ message handling.
Definition: MP_model.hpp:35
virtual void generationTime(double t)
Symantic representation of a variable.
Definition: MP_variable.hpp:75
const double * reducedCost
Definition: MP_model.hpp:212
const double * rowActivity
Definition: MP_model.hpp:214
std::set< MP_constraint * >::iterator conIt
Definition: MP_model.hpp:240
Abstract Base Class for describing an interface to a solver.
void silent()
used to silence FlopC++
Definition: MP_model.hpp:134
MP_model & add(MP_constraint &c)
Adds a constrataint block to the model.
double * Elm
Definition: MP_model.hpp:264
virtual ~Messenger()
Definition: MP_model.hpp:45
void setSolver(OsiSolverInterface *s)
allows for replacement of the solver used.
Definition: MP_model.hpp:145
Messenger * messenger
Definition: MP_model.hpp:246
const double * rowPrice
Definition: MP_model.hpp:213
Symbolic representation of a linear expression.
OsiSolverInterface * Solver
Definition: MP_model.hpp:256
virtual void constraintDebug(std::string name, const std::vector< Coef > &cfs)
Definition: MP_model.hpp:40
void attach(OsiSolverInterface *solver=NULL)
attaches the symantic representation of a model and data to a particular OsiSolverInterface ...
void maximize()
Binds the data and calls the solver to maximize the current objective expression. ...
MP_status
Reflects the state of the solution from solve()
Definition: MP_model.hpp:97
if solve is called and solver finds the model dual infeasible.
Definition: MP_model.hpp:103
OsiSolverInterface * operator->()
allows access to the OsiSolverInterface *
Definition: MP_model.hpp:150
if solve is called and solver abandons the problem (time?, iter limit?)
Definition: MP_model.hpp:106
virtual void logMessage(int level, const char *const msg)
Definition: MP_model.hpp:37
virtual void statistics(int bm, int m, int bn, int n, int nz)
Definition: MP_model.hpp:42
void minimize()
Binds the data and calls the solver to minimize the current objective expression. ...
double getInfinity() const
Useful for getting an appropriate value to pass in as &quot;infinity&quot;.
void minimize_max(MP_set &d, const MP_expression &obj)
Binds the data and calls the solver to minimize maximum value of the parameter obj objective expressi...
if the solve method is called and the optimal solution found.
Definition: MP_model.hpp:99
Semantic representation of a constraint in a Math Program.
void setObjective(const MP_expression &o)
sets the &quot;current objective&quot; to the parameter o
const double * solution
Accessors for the results after a call to maximize()/minimize()
Definition: MP_model.hpp:211