FlopCpp  trunk
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;
141  messenger = new VerboseMessenger;
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;
241  static MP_model& default_model;
242  static MP_model* current_model;
243  MP_model(const MP_model&);
244  MP_model& operator=(const MP_model&);
245 
246  Messenger* messenger;
247 
248 
249  static void assemble(std::vector<Coef>& v, std::vector<Coef>& av);
250  void add(MP_constraint* c);
251  MP_expression Objective;
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;
270  MP_status mSolverState;
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
static MP_model & getDefaultModel()
Can be used to get the default model.
A solver is placed in the constructor, but it is not yet attached or solved.
Definition: MP_model.hpp:109
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
void detach()
detaches an OsiSolverInterface object from the model.
Symbolic representation of a linear expression.
MP_model(OsiSolverInterface *s, Messenger *m=new NormalMessenger)
Constructs an MP_model from an OsiSolverInterface *.
std::ostream & operator<<(std::ostream &os, const MP_model::MP_status &condition)
allows print of result from call to solve();
virtual ~Messenger()
Definition: MP_model.hpp:45
Messenger * getMessenger()
Gets the current messenger.
Definition: MP_model.hpp:235
Inteface for hooking up to internal flopc++ message handling.
Definition: MP_model.hpp:35
MP_model & add(MP_constraint &c)
Adds a constrataint block to the model.
const double * rowPrice
Definition: MP_model.hpp:213
Semantic representation of a constraint in a Math Program.
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...
const double * rowActivity
Definition: MP_model.hpp:214
MP_model::MP_status solve(const MP_model::MP_direction &dir)
calls the appropriate solving methods in the OsiSolverInterface.
void maximize()
Binds the data and calls the solver to maximize the current objective expression.
void verbose()
used to help understanding and debugging FlopC++'s behavior.
Definition: MP_model.hpp:139
A solver is attached, but not yet solved.
Definition: MP_model.hpp:111
void addRow(const Constraint &c)
Adds a constraint to the MP_model.
void setObjective(const MP_expression &o)
sets the "current objective" to the parameter o
MP_status
Reflects the state of the solution from solve()
Definition: MP_model.hpp:97
This is the anchor point for all constructs in a FlopC++ model.
Definition: MP_model.hpp:89
void attach(OsiSolverInterface *solver=NULL)
attaches the symantic representation of a model and data to a particular OsiSolverInterface
All flopc++ code is contained within the flopc namespace.
Definition: flopc.hpp:49
OsiSolverInterface * Solver
Definition: MP_model.hpp:256
void minimize()
Binds the data and calls the solver to minimize the current objective expression.
if solve is called and solver finds model primal infeasible.
Definition: MP_model.hpp:101
Internal use: used when Verbose output is selected.
Definition: MP_model.hpp:61
static MP_model * getCurrentModel()
Can be used to get the current model.
void silent()
used to silence FlopC++
Definition: MP_model.hpp:134
Symantic representation of a variable.
Definition: MP_variable.hpp:75
if solve is called and solver finds the model dual infeasible.
Definition: MP_model.hpp:103
Representation of a set for indexing into some other construct.
Definition: MP_set.hpp:78
MP_status getStatus() const
Returns the current status of the model-solver interaction.
Definition: MP_model.hpp:130
const double * reducedCost
Definition: MP_model.hpp:212
Internal use: used when Normal output is selected.
Definition: MP_model.hpp:51
const double * solution
Accessors for the results after a call to maximize()/minimize()
Definition: MP_model.hpp:211
double getInfinity() const
Useful for getting an appropriate value to pass in as "infinity".
No solver is attached.
Definition: MP_model.hpp:113
if the solve method is called and the optimal solution found.
Definition: MP_model.hpp:99
void setSolver(OsiSolverInterface *s)
allows for replacement of the solver used.
Definition: MP_model.hpp:145
virtual void logMessage(int level, const char *const msg)
Definition: MP_model.hpp:37
MP_direction
used when calling the solve() method.
Definition: MP_model.hpp:93
Semantic representation of a linear constraint.