-
Notifications
You must be signed in to change notification settings - Fork 0
/
torto.mip.cc
205 lines (186 loc) · 5.09 KB
/
torto.mip.cc
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "easyscip/easyscip.h"
using namespace std;
using namespace easyscip;
bool valid(int i, int j, int w, int h) {
return i >= 0 && i < w && j >= 0 && j < h;
}
int distance(int a, int b) {
int ia = a % 3, ja = a / 3;
int ib = b % 3, jb = b / 3;
return abs(ia - ib) + abs(ja - jb);
}
bool neighbour(int a, int b) {
int ia = a % 3, ja = a / 3;
int ib = b % 3, jb = b / 3;
return abs(ia - ib) <= 1 && abs(ja - jb) <= 1 && a != b;
}
int len(string& s) {
return static_cast<int>(s.size());
}
template<typename T>
int len(vector<T> vec) {
return static_cast<int>(vec.size());
}
struct Position {
int word, start;
};
struct Bigram {
Position a, b;
};
int main() {
int nwords;
cin >> nwords;
vector<string> words(nwords);
map<char, int> hist;
for (int i = 0; i < nwords; i++) {
cin >> words[i];
for (int j = 0; j < len(words[i]); j++) {
int total = 0;
for (int k = 0; k < len(words[i]); k++) {
if (words[i][k] == words[i][j]) {
total++;
}
}
hist[i] = max(hist[i], total);
}
}
// Add variables.
MIPSolver mip;
int npos = 18;
vector<vector<vector<Variable>>> grid(
npos, vector<vector<Variable>>(nwords));
for (int p = 0; p < npos; p++) {
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]); c++) {
grid[p][w].push_back(mip.binary_variable(0));
}
}
}
vector<Variable> bigramvar;
vector<Bigram> bigrams;
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]) - 1; c++) {
for (int ww = 0; ww < nwords; ww++) {
if (w == ww) continue;
for (int cc = 0; cc < len(words[ww]) - 1; cc++) {
if (c == cc) continue;
if (words[w][c] == words[ww][cc] &&
words[w][c + 1] == words[ww][cc + 1]) {
bigramvar.push_back(mip.binary_variable(-1));
bigrams.push_back(Bigram{Position{w, c}, Position{ww,cc}});
}
}
}
}
}
// Enforce bigrams.
for (int i = 0; i < len(bigrams); i++) {
for (int p = 0; p < npos; p++) {
auto& b = bigrams[i];
auto cons1 = mip.constraint();
cons1.add_variable(bigramvar[i], 1);
cons1.add_variable(grid[p][b.a.word][b.a.start], 1);
for (int pp = 0; pp < npos; pp++) {
if (p == pp) continue;
cons1.add_variable(grid[pp][b.b.word][b.b.start], 1);
}
cons1.commit(0, 2);
auto cons2 = mip.constraint();
cons2.add_variable(bigramvar[i], 1);
cons2.add_variable(grid[p][b.a.word][b.a.start + 1], 1);
for (int pp = 0; pp < npos; pp++) {
if (p == pp) continue;
cons2.add_variable(grid[pp][b.b.word][b.b.start + 1], 1);
}
cons2.commit(0, 2);
}
}
// Only one letter for grid position.
for (int p = 0; p < npos; p++) {
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]); c++) {
auto cons = mip.constraint();
int vars = 0;
for (int ww = 0; ww < nwords; ww++) {
for (int cc = 0; cc < len(words[ww]); cc++) {
if (words[w][c] == words[ww][cc]) {
continue;
}
cons.add_variable(grid[p][ww][cc], 1);
vars++;
}
}
cons.add_variable(grid[p][w][c], vars);
cons.commit(0, vars);
}
}
}
// Each pos must have at least one letter.
for (int p = 0; p < npos; p++) {
auto cons = mip.constraint();
int limit = 0;
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]); c++) {
cons.add_variable(grid[p][w][c], 1);
limit++;
}
}
cons.commit(1, limit);
}
// Each letter must appear in exactly one pos.
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]); c++) {
auto cons = mip.constraint();
for (int p = 0; p < npos; p++) {
cons.add_variable(grid[p][w][c], 1);
}
cons.commit(1, 1);
}
}
// Letters must form a sequence.
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]) - 1; c++) {
for (int p = 0; p < npos; p++) {
auto cons = mip.constraint();
cons.add_variable(grid[p][w][c], 1);
for (int pp = 0; pp < npos; pp++) {
if (neighbour(p, pp)) {
cons.add_variable(grid[pp][w][c + 1], -1);
}
}
cons.commit(-1, 0);
}
}
}
// A word must not use the same letter of the grid twice.
for (int w = 0; w < nwords; w++) {
for (int p = 0; p < npos; p++) {
auto cons = mip.constraint();
for (int c = 0; c < len(words[w]); c++) {
cons.add_variable(grid[p][w][c], 1);
}
cons.commit(0, 1);
}
}
// Solve and print.
auto sol = mip.solve();
for (int p = 0; p < npos; p++) {
bool has = false;
for (int w = 0; w < nwords; w++) {
for (int c = 0; c < len(words[w]); c++) {
if (sol.value(grid[p][w][c]) > 0.5 && !has) {
cout << words[w][c] << " ";
has = true;
}
}
}
if (p % 3 == 2) {
cout << "\n";
}
}
cout << "\n";
}