-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlpsw.cpp
More file actions
123 lines (103 loc) · 2.89 KB
/
lpsw.cpp
File metadata and controls
123 lines (103 loc) · 2.89 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <bits/stdc++.h>
using namespace std;
int d;
random_device rd;
mt19937 mt(rd() ^ chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> dist;
int64_t basisChanges;
// given a Basis B, return the optimal solution X to the constraints in B
vector<int> basisToX(set<int> B) {
assert(B.size() == d);
vector<int> X(d, 0);
bool h1 = B.find(1) != B.end();
if(h1) X[0] = 1;
for(int i = 1; i < d; i++) {
bool h0 = B.find(2 * i) != B.end();
bool h1 = B.find(2 * i + 1) != B.end();
if(h0 && h1) X[i] = max(X[i - 1], 1 - X[i - 1]);
else if(h0) X[i] = 1 - X[i - 1];
else if(h1) X[i] = X[i - 1];
}
return X;
}
// Randomly choose an element from H \ T
int choose(set<int> &H, set<int> &T) {
vector<int> diff;
set_difference(H.begin(), H.end(), T.begin(), T.end(), back_inserter(diff)); // diff = H \ T
if (diff.empty()) return -1;
dist = uniform_int_distribution<int>(0, (int)diff.size() - 1);
return diff[dist(mt)];
}
// Returns Basis(B \cup h)
set <int> UpdateBasis(set <int> B, int h) {
assert(B.find(h) == B.end());
int cmpl;
if(h % 2 == 0) cmpl = h + 1;
if(h % 2 == 1) cmpl = h - 1;
B.erase(cmpl);
B.insert(h);
assert(B.size() == d);
return B;
}
// Returns True if h violates constraints in B
bool violates(int h, set<int> B) {
assert(B.find(h) == B.end());
int cmpl;
if(h == 1) return true;
else if(h == 0) return false;
vector <int> X = basisToX(B);
int idx = h / 2;
if((h % 2 == 0) ^ (X[idx - 1] == 1)) return true;
return false;
}
set<int> LPType(set<int> G, set<int> T) {
if (G.empty() || G == T) return T; // ?
// for(auto x: G) cout << x << " ";
// cout << endl;
int h = choose(G, T);
G.erase(h);
set<int> B = LPType(G, T); // G \ {h} and T
if(violates(h, B)) {
basisChanges++;
G.insert(h);
B = UpdateBasis(B, h);
B = LPType(G, B);
}
return B;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cerr << "Usage: " << argv[0] << " <d>" << endl;
return 1;
}
// Define the problem constraints:
// 0: h^0_1: x1 >= 0
// 1: h^1_1: x1 >= 1
// 2: h^0_2: x2 >= 1 - x1
// 3: h^1_2: x2 >= x1
// 4: h^0_3: x3 >= 1 - x2
// 5: h^1_3: x3 >= x2
// ...
d = stoi(argv[1]);
set<int> H;
for (int i = 0; i < 2 * d; i++) {
H.insert(i);
}
basisChanges = 0;
// random initial regular basis
set<int> T;
dist = uniform_int_distribution<int>(0, 1);
for(int i = 0; i < d; i++) {
if(dist(mt) == 0) T.insert(2 * i);
else T.insert(2 * i + 1);
}
// random initial regular basis
assert(T.size() == d);
set<int> B = LPType(H, T);
vector<int> x = basisToX(B);
for(int i = 0; i < d; i++) {
assert(x[i] == 1);
}
cout << basisChanges << endl;
return 0;
}