MC_tm.cpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #include <fstream>
4 
5 #include "BCP_tm.hpp"
6 #include "MC_tm.hpp"
7 #include "MC_cut.hpp"
8 #include "MC_init.hpp"
9 
10 //#############################################################################
11 
12 int main(int argc, char* argv[])
13 {
14  MC_initialize mc_init;
15  return bcp_main(argc, argv, &mc_init);
16 }
17 
18 //#############################################################################
19 
20 void
22 {
23  switch (ptype) {
24  case BCP_ProcessType_LP:
25  lp_par.pack(buf);
26  break;
27  default:
28  abort();
29  }
30  mc.pack(buf);
31 }
32 
33 //#############################################################################
34 
37 {
38  MC_solution* new_sol = new MC_solution;
39  new_sol->unpack(buf);
40  if (new_sol->objective_value() > best_soln.objective_value())
41  best_soln = *new_sol;
42  return new_sol;
43 }
44 
45 //#############################################################################
46 
47 void
50  BCP_lp_relax*& matrix)
51 {
52  int i;
53  const int m = mc.num_edges;
54 
55  vars.reserve(m);
56  for (i = 0; i < m; ++i) {
58  mc.edges[i].cost, 0, 1));
59  }
60 }
61 
62 //#############################################################################
63 
64 void
66  BCP_vec<BCP_cut*>& added_cuts,
67  BCP_user_data*& user_data)
68 {
69  if (added_cuts.size() > 0)
70  return;
71  // Do a couple of MST based cut generations with 0 as the primal solution
72  // and with slight perturbations on the costs.
73  int i, j;
74  const int n = mc.num_nodes;
75  const int m = mc.num_edges;
76  double* x = new double[m];
77 
78  // Set x to be the optimal solution to the unconstrainted optimization
79  // problem
80  const MC_graph_edge* edges = mc.edges;
81  for (i = 0; i < m; ++i)
82  x[i] = edges[i].cost > 0.0 ? 0.0 : 1.0;
83 
84  const int improve_round = lp_par.entry(MC_lp_par::HeurSwitchImproveRound);
85  const bool edge_switch = lp_par.entry(MC_lp_par::DoEdgeSwitchHeur);
86  const int struct_switch = ( lp_par.entry(MC_lp_par::StructureSwitchHeur) &
87  ((1 << mc.num_structure_type) - 1) );
88  MC_solution* sol = NULL;
89 
90  BCP_vec<BCP_row*> new_rows;
92  const int grid = static_cast<int>(sqrt(mc.num_nodes + 1.0));
93  const int grid_nodes = grid*grid;
96  x, 0.9, added_cuts, new_rows);
97  if (mc.ising_triangles)
99  x, 0.9, added_cuts, new_rows);
100  getTmProblemPointer()->ub(0.0);
101  BCP_vec<int> sig(n, 1);
102  sol = new MC_solution(sig, mc, improve_round, edge_switch, struct_switch);
103  } else {
104  // Not an Ising problem, use MST cuts to generate initial cuts;
105  sol = MC_mst_cutgen(mc, x, NULL, 1.0, 0.0,
107  improve_round, edge_switch, struct_switch,
108  0.9, COIN_INT_MAX, added_cuts, new_rows);
109  }
110 
111  // update best upper bound
113  getTmProblemPointer()->feas_sol = sol;
114 
115  delete[] x;
116 
117  // get rid of the row representations
118  purge_ptr_vector(new_rows);
119 
120  // keep unique cuts only (delete the duplicates)
121  std::sort(added_cuts.begin(), added_cuts.end(), MC_cycle_cut_less);
122  const int cutnum = added_cuts.size();
123  for (j = 0, i = 1; i < cutnum; ++i) {
124  if (MC_cycle_cut_equal(added_cuts[j], added_cuts[i])) {
125  delete added_cuts[i];
126  added_cuts[i] = NULL;
127  } else {
128  j = i;
129  }
130  }
131 
132  // get rid of the NULL pointers
133  for (j = 0, i = 0; i < cutnum; ++i) {
134  if (added_cuts[i] != NULL) {
135  added_cuts[j++] = added_cuts[i];
136  }
137  }
138  added_cuts.erase(added_cuts.entry(j), added_cuts.end());
139 }
140 
141 //#############################################################################
142 
143 void
145 {
146  const MC_solution* mc_sol = dynamic_cast<const MC_solution*>(sol);
147  if (! mc_sol) {
148  throw BCP_fatal_error("\
149 MC_tm::display_feasible_solution() invoked with non-MC_solution.\n");
150  }
151 
152  if (mc.ising_problem) {
153  const int grid = static_cast<int>(sqrt(mc.num_nodes + 1.0));
154  const int grid_size = grid * grid;
155  const BCP_vec<int>& sig = mc_sol->sig;
156  double field = 0;
157  for (int i = grid_size - 1; i >= 0; --i) {
158  field += sig[i];
159  }
160  if (field < 0)
161  field = -field;
162  field /= grid_size;
163  const double energy =
164  (- mc.sum_edge_weight + 2 * mc_sol->objective_value()) /
165  (mc.scaling_factor * grid_size);
166  printf("MC: field: %.6f energy: %.6f\n", field, energy);
167  }
168 
171  }
172 }
Binary (0-1) variable.
Definition: BCP_enum.hpp:163
int * ising_four_cycles
Definition: MC.hpp:105
double cost
Definition: MC.hpp:31
MC_solution * MC_mst_cutgen(const MC_problem &mc, const double *x, const double *w, const double alpha, const double beta, const MC_EdgeOrdering edge_ordering, const int heurswitchround, const bool do_edge_switch_heur, const int struct_switch_heur, const double minviol, const int maxnewcuts, BCP_vec< BCP_cut * > &new_cuts, BCP_vec< BCP_row * > &new_rows)
Definition: MC_cutgen.cpp:152
bool MC_cycle_cut_equal(const BCP_cut *bc0, const BCP_cut *bc1)
Definition: MC_cut.hpp:122
int num_edges
Definition: MC.hpp:92
int main(int argc, char *argv[])
Definition: BB_tm.cpp:32
BCP_solution * unpack_feasible_solution(BCP_buffer &buf)
Unpack a MIP feasible solution that was packed by the BCP_lp_user::pack_feasible_solution() method...
Definition: MC_tm.cpp:36
int num_nodes
Definition: MC.hpp:91
BCP_process_t
This enumerative constant describes the various process types.
double ub() const
Definition: BCP_tm.hpp:321
char entry(const chr_params key) const
MC_graph_edge * edges
Definition: MC.hpp:95
BCP_parameter_set< MC_lp_par > lp_par
Definition: MC_tm.hpp:19
iterator begin()
Return an iterator to the beginning of the object.
Definition: BCP_vector.hpp:99
BCP_solution * feas_sol
Definition: BCP_tm.hpp:163
void reserve(const size_t n)
Reallocate the object to make space for n entries.
int * ising_triangles
Definition: MC.hpp:107
Core variables are the variables that always stay in the LP formulation.
Definition: BCP_var.hpp:230
static char * j
Definition: OSdtoa.cpp:3622
int bcp_main(int argc, char *argv[], USER_initialize *user_init)
This is the function the user must invoke when (s)he is ready to turn contrl over to BCP...
Definition: BCP_tm_main.cpp:37
bool ising_problem
Definition: MC.hpp:99
double scaling_factor
Definition: MC.hpp:102
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: MC_tm.cpp:21
void erase(iterator pos)
Erase the entry pointed to by pos.
BCP_parameter_set< MC_tm_par > tm_par
Definition: MC_tm.hpp:18
void display_feasible_solution(const BCP_solution *sol)
Display a feasible solution.
Definition: MC_tm.cpp:144
MC_problem mc
Definition: MC_tm.hpp:23
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: MC_tm.cpp:65
virtual double objective_value() const
The method returning the objective value of the solution.
Definition: MC_solution.hpp:27
Currently there isn&#39;t any error handling in BCP.
Definition: BCP_error.hpp:20
size_t size() const
Return the current number of entries.
Definition: BCP_vector.hpp:116
iterator end()
Return an iterator to the end of the object.
Definition: BCP_vector.hpp:104
MC_solution best_soln
Definition: MC_tm.hpp:24
BCP_buffer & unpack(BCP_buffer &buf)
Definition: MC_solution.cpp:33
This class describes the message buffer used for all processes of BCP.
Definition: BCP_buffer.hpp:39
int num_structure_type
Definition: MC.hpp:109
void unchecked_push_back(const_reference x)
Append x to the end of the vector.
BCP_tm_prob * getTmProblemPointer() const
Get the pointer.
Definition: BCP_tm_user.hpp:71
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: MC_tm.cpp:48
void purge_ptr_vector(BCP_vec< T * > &pvec, typename BCP_vec< T * >::iterator first, typename BCP_vec< T * >::iterator last)
This function purges the entries [first,last) from the vector of pointers pvec.
Definition: BCP_vector.hpp:266
void fint * m
double sum_edge_weight
Definition: MC.hpp:101
void MC_test_ising_triangles(const int n, const int *cycles, const double *x, const double minviol, BCP_vec< BCP_cut * > &cuts, BCP_vec< BCP_row * > &rows)
iterator entry(const int i)
Return an iterator to the i-th entry.
Definition: BCP_vector.hpp:109
BCP_vec< int > sig
Definition: MC_solution.hpp:17
void fint * n
void pack(BCP_buffer &buf)
Pack the parameter set into the buffer.
void display(const BCP_string &fname) const
void MC_test_ising_four_cycles(const int n, const int *cycles, const double *x, const double minviol, BCP_vec< BCP_cut * > &cuts, BCP_vec< BCP_row * > &rows)
bool MC_cycle_cut_less(const BCP_cut *bc0, const BCP_cut *bc1)
Definition: MC_cut.hpp:107
An object of type BCP_lp_relax holds the description of an lp relaxation.
Definition: BCP_matrix.hpp:267
BCP_buffer & pack(BCP_buffer &buf)
Definition: MC.cpp:10
void fint fint fint real fint real * x
This is the abstract base class for a solution to a Mixed Integer Programming problem.