8 #include "CoinHelperFunctions.hpp"
18 #define MC_PRINT_ITER_COUNT 0
20 #define MC_TEST_COST_AFTER_EVERY_SWITCH 0
53 for (
int i = 0; i <
m; ++i) {
66 const int* sig,
const int this_sig)
69 for (
int j = degree - 1;
j >= 0; --
j) {
74 if (sig[adj_list[
j].neighbor] == this_sig) {
75 sum += adj_list[
j].
cost;
77 sum -= adj_list[
j].
cost;
92 double newcost =
cost;
93 for (iter = 0; iter < maxiter && flag; ++iter) {
95 for (i = 0; i <
n; ++i) {
103 #if MC_TEST_COST_AFTER_EVERY_SWITCH
111 #if MC_PRINT_ITER_COUNT
112 printf(
"MC_solution: switch_improve: %i.\n", iter);
134 double newcost =
cost;
137 for (iter = 0; iter < maxiter && flag; ++iter) {
139 for (k = 0; k <
m; ++
k) {
140 const int i = edges[
k].
tail;
141 const int j = edges[
k].
head;
142 const double costk = 2 *
sig[i] *
sig[
j] * edges[
k].
cost;
159 #if MC_TEST_COST_AFTER_EVERY_SWITCH
167 #if MC_PRINT_ITER_COUNT
168 printf(
"MC_solution: edge_switch_improve: %i.\n", iter);
188 const int grid_edge_num = m - mc.
num_nodes + 1;
191 double newcost =
cost;
197 for (iter = 0; iter < maxiter && flag; ++iter) {
200 for (k = 0; k < grid_edge_num; ++
k) {
201 const int i = edges[
k].
tail;
202 const int j = edges[
k].
head;
203 const double costk = 2 *
sig[i] *
sig[
j] * edges[
k].
cost;
220 #if MC_TEST_COST_AFTER_EVERY_SWITCH
229 double switch_cost_of_plus_node =
232 for ( ; k <
m; ++
k) {
233 const int i = edges[
k].
tail;
234 const double costk = 2 *
sig[i] *
sig[plus_node] * edges[
k].
cost;
243 switch_cost_of_plus_node -
247 sig[plus_node] *= -1;
248 switch_cost_of_plus_node = - switch_cost_of_plus_node + costk;
251 #if MC_TEST_COST_AFTER_EVERY_SWITCH
259 #if MC_PRINT_ITER_COUNT
260 printf(
"MC_solution: edge_switch_improve: %i.\n", iter);
274 const int struct_ind,
const int maxiter)
284 double newcost =
cost;
287 for (iter = 0; iter < maxiter && flag; ++iter) {
289 for (k = 0; k <
s; ++
k) {
298 const int * nodes = sw_struct.
nodes;
299 for (i = sw_struct.
num_nodes - 1; i >= 0; --i) {
304 #if MC_TEST_COST_AFTER_EVERY_SWITCH
312 #if MC_PRINT_ITER_COUNT
313 printf(
"MC_solution: sw_struct_improve: %i.\n", iter);
329 int i, start,
end, iter = 0;
334 for (iter = 0; iter < 1000 *
maxiter; ++iter) {
339 for (i = start; i <
n; ++i) {
346 for (i = 0; i < start; ++i) {
363 for (i = start+1; i <
n; ++i) {
369 if (sum < best_sum) {
375 for (i = 0; i < start; ++i) {
381 if (sum < best_sum) {
393 for (--i; i >= 0; --i)
397 for (--i; i >
end; --i)
400 const double newcost =
cost + best_sum;
405 start = (end + 1) %n;
407 #if MC_PRINT_ITER_COUNT
408 printf(
"MC_solution: lk_switch_improve: %i.\n", iter);
419 const int num_nodes =
sig.
size();
422 printf(
"MC_solution:\n");
423 printf(
"%i\n", objval);
424 for (
int i = 0; i < num_nodes; ++i) {
425 printf(
"%2i\n",
sig[i]);
429 if (fname.
length() != 0) {
430 std::ofstream ofile(fname.
c_str());
431 ofile <<
"MC_solution:\n";
432 ofile << objval <<
"\n";
433 for (
int i = 0; i < num_nodes; ++i) {
434 ofile <<
sig[i] <<
"\n";
443 const int heurswitchround,
444 const bool do_edge_switch_heur,
445 const int struct_switch_heur) :
452 const double percentage[3] = {0.05, 0.01, 0.002};
458 bool do_plain_switch =
true;
459 bool do_edge_switch =
true;
460 bool* do_structure_switch =
new bool[num_struct_type];
461 bool do_switch =
true;
463 printf(
"MC: Heur Sol Improvement: ");
464 for (
int k = 0;
k < 1; ++
k) {
473 do_plain_switch =
true;
474 do_edge_switch =
true;
475 CoinFillN(do_structure_switch, num_struct_type,
true);
479 if (do_plain_switch) {
485 do_edge_switch =
true;
486 CoinFillN(do_structure_switch, num_struct_type,
true);
488 do_plain_switch =
false;
490 if (do_edge_switch) {
491 if (do_edge_switch_heur) {
500 do_plain_switch =
true;
501 CoinFillN(do_structure_switch, num_struct_type,
true);
504 do_edge_switch =
false;
506 for (i = 0; i < num_struct_type; ++i) {
507 if (do_structure_switch[i]) {
508 if ((struct_switch_heur & (1 << i)) != 0) {
514 do_plain_switch =
true;
515 do_edge_switch =
true;
516 CoinFillN(do_structure_switch, num_struct_type,
true);
520 do_structure_switch[i] =
false;
523 do_switch = do_plain_switch || do_edge_switch;
524 for (i = 0; i < num_struct_type; ++i)
525 do_switch |= do_structure_switch[i];
527 printf(
" %.0f", obj[0]);
528 const int rounds = obj.
size();
529 for (i = 1; i < rounds; ++i) {
530 printf(
" / %c %.0f", type[i], obj[i]);
534 if (
cost < bestcost) {
539 for (i =
sig.
size() - 1; i >= 0; --i) {
540 if (CoinDrand48() < percentage[
k])
548 delete[] do_structure_switch;
double structure_switch_improve(const MC_problem &mc, const int struct_ind, const int maxiter)
BCP_buffer & pack(const T &value)
Pack a single object of type T.
BCP_buffer & unpack(T &value)
Unpack a single object of type T.
const char * c_str() const
iterator begin()
Return an iterator to the beginning of the object.
double edge_switch_improve(const MC_problem &mc, const int maxiter)
reference back()
Return a reference to the last entry.
This class is a very simple impelementation of a constant length string.
void push_back(const_reference x)
Append x to the end of the vector.
MC_solution & operator=(const MC_solution &sol)
MC_switch_structure ** switch_structures
double compute_cost(const int m, const MC_graph_edge *edges)
void fint fint fint real fint real real real real real real real real real * e
void fint fint fint fint fint fint fint fint fint fint real real real real real real real real * s
virtual double objective_value() const
The method returning the objective value of the solution.
size_t size() const
Return the current number of entries.
double switch_improve(const MC_problem &mc, const int maxiter)
BCP_buffer & unpack(BCP_buffer &buf)
This class describes the message buffer used for all processes of BCP.
double ising_with_external_edge_switch_improve(const MC_problem &mc, const int maxiter)
MC_adjacency_entry * adj_list
int * num_switch_structures
static double MC_switch_cost(const MC_adjacency_entry *adj_list, const int degree, const int *sig, const int this_sig)
double lk_switch_improve(const MC_problem &mc, const int maxiter)
void fint fint fint fint fint fint fint fint fint fint real real real real real real real real real fint real fint real char real fint fint * maxiter
void display(const BCP_string &fname) const
BCP_buffer & pack(BCP_buffer &buf) const