MCF2_tm.cpp
Go to the documentation of this file.
1 #include "BCP_math.hpp"
2 #include "BCP_solution.hpp"
3 #include "MCF2_tm.hpp"
4 
5 using std::make_pair;
6 
7 //#############################################################################
8 
9 void
11  BCP_column_generation& colgen,
12  CoinSearchTreeBase*& candidates)
13 {
14  // Let BCP initialize things (like creating the search tree)
15  BCP_tm_user::init_new_phase(phase, colgen, candidates);
16  // but override colgen
17  colgen = BCP_GenerateColumns;
18 }
19 
20 /*---------------------------------------------------------------------------*/
21 
24  BCP_lp_relax*& matrix)
25 {
26  int i;
27  // The core cuts are the cuts that specify that the sum of the flows must
28  // not be out of bounds, and the convexity constraints for the generated
29  // algo variables (flows). However, since there are no core variables, the
30  // core matrix is empty.
31  for (int i = 0; i < data.numarcs; ++i) {
32  cuts.push_back(new BCP_cut_core(-BCP_DBL_MAX, data.arcs[i].ub));
33  }
34  for (i = 0; i < data.numcommodities; ++i) {
35  cuts.push_back(new BCP_cut_core(1.0, 1.0));
36  }
37  matrix = new BCP_lp_relax;
38 }
39 
40 /*---------------------------------------------------------------------------*/
41 
43  BCP_vec<BCP_cut*>& added_cuts,
44  BCP_user_data*& user_data)
45 {
46  // create a column (a flow) for each commodity
48  // In this case just send all the demand through the artificial arc
49  // for each commodity. That'll get the algorithm started.
50  for (int i = 0; i < data.numcommodities; ++i) {
51  int ind = data.numarcs-data.numcommodities+i;
52  double val = data.commodities[i].demand;
53  double w = data.commodities[i].demand * data.arcs[ind].weight;
54  added_vars.push_back(new MCF2_var(i, CoinPackedVector(1, &ind, &val,
55  false), w));
56  }
57  } else {
58  // Generate a flow for each commodity independently
59  // *** excercise ***
60 
61  // Think of the consequences of not having the artificial arcs. After
62  // branching the master problem may become infeasible, subproblems can
63  // be infeasible, etc...
64  }
65 }
66 
67 /*---------------------------------------------------------------------------*/
68 
70 {
71  const BCP_solution_generic* gsol =
72  dynamic_cast<const BCP_solution_generic*>(sol);
73  const BCP_vec<BCP_var*>& vars = gsol->_vars;
74  for (int i = vars.size()-1; i >= 0; --i) {
75  const MCF2_var* v = dynamic_cast<const MCF2_var*>(vars[i]);
76  printf("Flow of commodity %i:\n", v->commodity);
77  const CoinPackedVector& f = v->flow;
78  const int* ind = f.getIndices();
79  const double* val = f.getElements();
80  for (int j = 0; j < f.getNumElements(); ++j) {
81  printf(" arc %6i ( %5i -> %5i ) flow %i\n",
82  ind[j], data.arcs[ind[j]].tail, data.arcs[ind[j]].head,
83  static_cast<int>(val[j]));
84  }
85  }
86 }
87 
88 /*---------------------------------------------------------------------------*/
89 
91 {
92  switch (ptype) {
93  case BCP_ProcessType_LP:
94  par.pack(buf);
95  data.pack(buf);
96  break;
97  default:
98  throw BCP_fatal_error("In this example we have only an LP process!\n");
99  }
100 }
BCP_vec< BCP_var * > _vars
Vector of variables that are at nonzero level in the solution.
virtual void display_feasible_solution(const BCP_solution *sol)
Display a feasible solution.
Definition: MCF2_tm.cpp:69
virtual void create_root(BCP_vec< BCP_var * > &added_vars, BCP_vec< BCP_cut * > &added_cuts, BCP_user_data *&user_data)
Create the set of extra variables and cuts that should be added to the formulation in the root node...
Definition: MCF2_tm.cpp:42
int numarcs
Definition: MCF2_data.hpp:28
int numcommodities
Definition: MCF2_data.hpp:30
virtual void initialize_core(BCP_vec< BCP_var_core * > &vars, BCP_vec< BCP_cut_core * > &cuts, BCP_lp_relax *&matrix)
Create the core of the problem by filling out the last three arguments.
Definition: MCF2_tm.cpp:22
BCP_process_t
This enumerative constant describes the various process types.
char entry(const chr_params key) const
void pack(BCP_buffer &buf) const
Definition: MCF2_data.cpp:7
Core cuts are the cuts that always stay in the LP formulation.
Definition: BCP_cut.hpp:195
BCP_parameter_set< MCF2_par > par
Definition: MCF2_tm.hpp:30
void push_back(const_reference x)
Append x to the end of the vector.
static char * j
Definition: OSdtoa.cpp:3622
void fint fint fint real fint real real real real * f
Attempt column generation.
Definition: BCP_enum.hpp:73
#define BCP_DBL_MAX
Definition: BCP_math.hpp:6
void fint fint fint * phase
commodity * commodities
Definition: MCF2_data.hpp:27
virtual void init_new_phase(int phase, BCP_column_generation &colgen, CoinSearchTreeBase *&candidates)
Do whatever initialization is necessary before the phase-th phase.
virtual void init_new_phase(int phase, BCP_column_generation &colgen, CoinSearchTreeBase *&candidates)
Do whatever initialization is necessary before the phase-th phase.
Definition: MCF2_tm.cpp:10
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
arc * arcs
Definition: MCF2_data.hpp:26
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
MCF2_data data
Definition: MCF2_tm.hpp:31
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
CoinPackedVector flow
Definition: MCF2_var.hpp:24
virtual void pack_module_data(BCP_buffer &buf, BCP_process_t ptype)
Pack the initial information (info that the user wants to send over) for the process specified by the...
Definition: MCF2_tm.cpp:90
BCP_column_generation
This enumerative constant describes what to do when a search tree node becomes fathomable for the cur...
Definition: BCP_enum.hpp:65
int commodity
Definition: MCF2_var.hpp:23
void fint fint fint real fint real real real real real real real real * w
This class holds a MIP feasible primal solution.
void pack(BCP_buffer &buf)
Pack the parameter set into the buffer.
An object of type BCP_lp_relax holds the description of an lp relaxation.
Definition: BCP_matrix.hpp:267
This is the abstract base class for a solution to a Mixed Integer Programming problem.