summaryrefslogtreecommitdiff
path: root/excitation.cpp
blob: 8a2c409e6b8661bbd54d008291cc72cbd572ad52 (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
#include <iostream>

#include "aztec.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;
  AztecDiamond G(n);
  G.setWeights(r);

  PerfectMatching pm = findGroundState(G);

  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].tail->coordinate[0] << " " << G.edges[i].tail->coordinate[1] << " "
                << G.edges[i].head->coordinate[0] << " " << G.edges[i].head->coordinate[1]
                << std::endl;
    }
  }

  return 0;
}