summaryrefslogtreecommitdiff
path: root/excitation.cpp
blob: 05cb8c65116a5be5df2a31a2d562847ed7b44146 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>

#include "rbmp.hpp"

int main(int argc, char* argv[]) {
  unsigned n = 100;

  int opt;

  while ((opt = getopt(argc, argv, "n:")) != -1) {
    switch (opt) {
    case 'n':
      n = atoi(optarg);
      break;
    default:
      exit(1);
    }
  }

  Rng r;
  Graph G(n, r);

  PerfectMatching pm(G.vertices.size(), G.edges.size());

  for (const Edge& e : G.edges) {
    pm.AddEdge(e.halfedges[0].getHead().index, e.halfedges[0].getTail().index, e.weight);
  }

  pm.options.verbose = false;
  pm.Solve();

  std::cout << n << std::endl;

  for (unsigned i = 0; i < G.vertices.size() / 2; i++) {
    unsigned j1 = pm.GetMatch(i);

    std::cout
      << G.vertices[i].coordinate[0] << " "
      << G.vertices[i].coordinate[1] << " "
      << G.vertices[j1].coordinate[0] << " "
      << G.vertices[j1].coordinate[1] << std::endl;
  }

  std::vector<bool> matching(G.edges.size());

  for (unsigned i = 0; i < G.edges.size(); i++) {
    matching[i] = edgeMatched(pm, G.edges[i]);
  }


  while (true) {
    unsigned eFlip = r.variate<unsigned, std::uniform_int_distribution>(0, G.edges.size() - 1);

    while (!matching[eFlip]) {
      eFlip = r.variate<unsigned, std::uniform_int_distribution>(0, G.edges.size() - 1);
    }

    pm.StartUpdate();
    pm.UpdateCost(eFlip, 1e10);
    pm.FinishUpdate();

    pm.Solve();

    unsigned m = 0;
    for (unsigned i = 0; i < G.edges.size(); i++) {
      if (!matching[i] && edgeMatched(pm, G.edges[i])) {
        m++;
      }
    }

    if (m > 4 * n) {
      std::cerr << "Size of excitation: " << m << std::endl;
      break;
    } else {
      pm.StartUpdate();
      pm.UpdateCost(eFlip, -1e10);
      pm.FinishUpdate();

      pm.Solve();
    }
  }

  for (unsigned i = 0; i < G.edges.size(); i++) {
    if (!matching[i] && edgeMatched(pm, G.edges[i])) {
      std::cout
        << G.edges[i].halfedges[0].getTail().coordinate[0] << " "
        << G.edges[i].halfedges[0].getTail().coordinate[1] << " "
        << G.edges[i].halfedges[0].getHead().coordinate[0] << " "
        << G.edges[i].halfedges[0].getHead().coordinate[1] << std::endl;
    }
  }

  return 0;
}