-
Notifications
You must be signed in to change notification settings - Fork 0
/
EinsteinCNF.cpp
349 lines (255 loc) · 12.6 KB
/
EinsteinCNF.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
Written by Varun Komperla
GT ID: xx3926404
GT Email: [email protected]
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>
using namespace std;
// --------------------- Red, White, Green, Yellow, Blue
vector<string> ColorKeys{"clr_r", "clr_w", "clr_g", "clr_y", "clr_b"};
// --------------------- Brit, Swede, Dane, Norwegian, German
vector<string> NationalityKeys{"ntn_B", "ntn_S", "ntn_D", "ntn_N", "ntn_G"};
// --------------------- Tea, Coffee, Milk, Beer, Water
vector<string> BeverageKeys{"bvg_t", "bvg_c", "bvg_m", "bvg_b", "bvg_w"};
// --------------------- Pall Mall, Dunhill, Blends, Prince, Bluemasters
vector<string> CigarKeys{"cig_p", "cig_d", "cig_b", "cig_c", "cig_s"};
// --------------------- Cats, Horse, Dogs, Fish, Birds
vector<string> PetKeys{"pet_c", "pet_h", "pet_d", "pet_f", "pet_b"};
const int numHouses = 5;
void PrintClause(vector<int> clause)
{
// cout << endl;
// for (int i = 0; i < clause.size(); i++)
// {
// cout << clause[i] << " ";
// }
// cout<<"0";
}
vector<vector<int>> BaseContraintsGeneration(vector<string> propertyKeys, map<pair<int, string>, int> literalMap)
{
vector<vector<int>> clausesList;
vector<vector<int>> houseHasAtLeastOneProp;
vector<vector<vector<int>>> houseHasAtMostOneProp;
// Building constraints for each house, each house should have at least one and at most one property.
for (int houseNumber = 1; houseNumber <= numHouses; houseNumber++)
{
vector<int> houseHasAtLeastOnePropTempClause;
for (int propIndex = 0; propIndex < propertyKeys.size(); propIndex++)
{
int literal_HousePropPair1 = literalMap[{houseNumber, propertyKeys[propIndex]}];
houseHasAtLeastOnePropTempClause.push_back(literal_HousePropPair1);
for (int propIndex2 = propIndex + 1; propIndex2 < propertyKeys.size(); propIndex2++)
{
int literal_HousePropPair2 = literalMap[{houseNumber, propertyKeys[propIndex2]}];
vector<int> houseHasAtMostOnePropTempClause;
houseHasAtMostOnePropTempClause.push_back(-1 * literal_HousePropPair1);
houseHasAtMostOnePropTempClause.push_back(-1 * literal_HousePropPair2);
PrintClause(houseHasAtMostOnePropTempClause);
clausesList.push_back(houseHasAtMostOnePropTempClause);
}
}
PrintClause(houseHasAtLeastOnePropTempClause);
clausesList.push_back(houseHasAtLeastOnePropTempClause);
}
// Building constraints for each property value, where property applies to at least one house and at most one house
vector<vector<int>> atLeastOneHouseHasProp;
vector<vector<vector<int>>> atMostOneHouseHasProp;
for (int propertyIndex = 0; propertyIndex < propertyKeys.size(); propertyIndex++)
{
vector<int> atLeastOneHouseHasPropTempClause;
for (int houseIndex = 1; houseIndex <= numHouses; houseIndex++)
{
int literal_HousePropPair1 = literalMap[{houseIndex, propertyKeys[propertyIndex]}];
atLeastOneHouseHasPropTempClause.push_back(literal_HousePropPair1);
for (int houseIndex2 = houseIndex + 1; houseIndex2 <= numHouses; houseIndex2++)
{
int literal_HousePropPair2 = literalMap[{houseIndex2, propertyKeys[propertyIndex]}];
vector<int> atMostOneHouseHasPropTempClause;
atMostOneHouseHasPropTempClause.push_back(-1 * literal_HousePropPair1);
atMostOneHouseHasPropTempClause.push_back(-1 * literal_HousePropPair2);
PrintClause(atMostOneHouseHasPropTempClause);
clausesList.push_back(atMostOneHouseHasPropTempClause);
}
}
PrintClause(atLeastOneHouseHasPropTempClause);
clausesList.push_back(atLeastOneHouseHasPropTempClause);
}
return clausesList;
}
vector<vector<int>> GenerateNeighbourConstraints(string propertyKey1, string propertyKey2, map<pair<int, string>, int> literalMap)
{
vector<vector<int>> clauses;
for (int houseNum1 = 1; houseNum1 <= numHouses; houseNum1++)
{
for (int houseNum2 = houseNum1 + 2; houseNum2 <= numHouses; houseNum2++)
{
vector<int> clauseHolder1;
vector<int> clauseHolder2;
clauseHolder1.push_back(-1 * literalMap[{houseNum1, propertyKey1}]);
clauseHolder1.push_back(-1 * literalMap[{houseNum2, propertyKey2}]);
clauseHolder2.push_back(-1 * literalMap[{houseNum1, propertyKey2}]);
clauseHolder2.push_back(-1 * literalMap[{houseNum2, propertyKey1}]);
PrintClause(clauseHolder1);
PrintClause(clauseHolder2);
clauses.push_back(clauseHolder1);
clauses.push_back(clauseHolder2);
}
vector<int> clauseHolderNotSameHouse;
clauseHolderNotSameHouse.push_back(-1 * literalMap[{houseNum1, propertyKey1}]);
clauseHolderNotSameHouse.push_back(-1 * literalMap[{houseNum1, propertyKey2}]);
PrintClause(clauseHolderNotSameHouse);
clauses.push_back(clauseHolderNotSameHouse);
}
return clauses;
}
vector<vector<int>> GenerateImplicationConstraints(string propertyKey1, string propertyKey2, map<pair<int, string>, int> literalMap)
{
vector<vector<int>> clauses;
for (int houseNum1 = 1; houseNum1 < numHouses; houseNum1++)
{
for (int houseNum2 = houseNum1 + 1; houseNum2 <= numHouses; houseNum2++)
{
if (houseNum1 != houseNum2)
{
vector<int> clauseHolder1;
vector<int> clauseHolder2;
clauseHolder1.push_back(-1 * literalMap[{houseNum1, propertyKey1}]);
clauseHolder1.push_back(-1 * literalMap[{houseNum2, propertyKey2}]);
clauseHolder2.push_back(-1 * literalMap[{houseNum1, propertyKey2}]);
clauseHolder2.push_back(-1 * literalMap[{houseNum2, propertyKey1}]);
PrintClause(clauseHolder1);
PrintClause(clauseHolder2);
clauses.push_back(clauseHolder1);
clauses.push_back(clauseHolder2);
}
}
}
return clauses;
}
vector<vector<int>> GenerateFactConstraints(int houseNum, string propertyKey, map<pair<int, string>, int> literalMap)
{
vector<vector<int>> clauses;
vector<int> clauseHolder;
clauseHolder.push_back(literalMap[{houseNum, propertyKey}]);
clauses.push_back(clauseHolder);
PrintClause(clauseHolder);
return clauses;
}
vector<vector<int>> GenerateLeftOfConstraint(string propertyKeyLeft, string propertyKeyRight, map<pair<int, string>, int> literalMap)
{
vector<vector<int>> clauses;
vector<int> clauseHolder1;
clauseHolder1.push_back(-1 * literalMap[{5, propertyKeyLeft}]);
clauses.push_back(clauseHolder1);
for (int houseNum1 = 1; houseNum1 <= numHouses - 1; houseNum1++)
{
for (int houseNum2 = 1; houseNum2 <= numHouses; houseNum2++)
{
if (houseNum2 != (houseNum1 + 1) && houseNum2 != houseNum1)
{
vector<int> clauseHolder2;
clauseHolder2.push_back(-1 * literalMap[{houseNum1, propertyKeyLeft}]);
clauseHolder2.push_back(-1 * literalMap[{houseNum2, propertyKeyRight}]);
PrintClause(clauseHolder2);
clauses.push_back(clauseHolder2);
}
}
}
return clauses;
}
int main()
{
vector<vector<string>> propertyKeys{
ColorKeys,
NationalityKeys,
BeverageKeys,
CigarKeys,
PetKeys};
vector<string> propertyNames{"Color", "Nationality", "Beverage", "Cigar", "Pet"};
map<pair<int, string>, int> literalMap;
vector<vector<int>> AllClauses;
int variable = 1;
cout<< "\nGenerating variables...";
for (int property = 0; property < propertyKeys.size(); property++)
{
cout << "\n\nVariables for House - " << propertyNames[property] << "\n\n";
for (int houseNum = 1; houseNum <= numHouses; houseNum++)
{
for (int propertyValueIndex = 0; propertyValueIndex < propertyKeys[property].size(); propertyValueIndex++)
{
cout << "(" << houseNum << ", " << propertyKeys[property][propertyValueIndex] << ") - " << variable << "\t ";
literalMap.insert({{houseNum, propertyKeys[property][propertyValueIndex]}, variable});
variable++;
}
cout << endl;
}
}
for (int property = 0; property < propertyKeys.size(); property++)
{
cout<<"\n\nCreating base constraints for House-"<<propertyNames[property]<<"...\n\n";
vector<vector<int>> baseClauses = BaseContraintsGeneration(propertyKeys[property], literalMap);
AllClauses.insert(AllClauses.end(), begin(baseClauses), end(baseClauses));
}
vector<vector<int>> clauses;
cout<<"\n\nCreating implication constraints for Brit and Red...\n\n";
clauses = GenerateImplicationConstraints(NationalityKeys[0], ColorKeys[0], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Swede and Dogs...\n\n";
clauses = GenerateImplicationConstraints(NationalityKeys[1], PetKeys[2], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Dane and Tea...\n\n";
clauses = GenerateImplicationConstraints(NationalityKeys[2], BeverageKeys[0], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Green and Coffee...\n\n";
clauses = GenerateImplicationConstraints(ColorKeys[2], BeverageKeys[1], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Pall Mall and Birds...\n\n";
clauses = GenerateImplicationConstraints(CigarKeys[0], PetKeys[4], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Yellow and Dunhill...\n\n";
clauses = GenerateImplicationConstraints(ColorKeys[3], CigarKeys[1], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating neighbour constraints for Blends and Cats...\n\n";
clauses = GenerateNeighbourConstraints(CigarKeys[2], PetKeys[0], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating neighbour constraints for Horse and Dunhill...\n\n";
clauses = GenerateNeighbourConstraints(PetKeys[1], CigarKeys[1], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for Bluemasters and Beer...\n\n";
clauses = GenerateImplicationConstraints(CigarKeys[4], BeverageKeys[3], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating implication constraints for German and Prince...\n\n";
clauses = GenerateImplicationConstraints(NationalityKeys[4], CigarKeys[3], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating neighbour constraints for Norwegian and Bluehouse...\n\n";
clauses = GenerateNeighbourConstraints(NationalityKeys[3], ColorKeys[4], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating neighbour constraints for Blends and Water...\n\n";
clauses = GenerateNeighbourConstraints(CigarKeys[2], BeverageKeys[4], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating fact constraints for Milk and Center house...\n\n";
clauses = GenerateFactConstraints(3, BeverageKeys[2], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating fact constraints for Norwegian in Blue house...\n\n";
clauses = GenerateFactConstraints(1, NationalityKeys[3], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout<<"\n\nCreating constraints for Green to the left of White...\n\n";
clauses = GenerateLeftOfConstraint(ColorKeys[2], ColorKeys[1], literalMap);
AllClauses.insert(AllClauses.end(), begin(clauses), end(clauses));
cout << "\n\n--------- Clauses in DIMACS format --------- \n\n";
cout << "p cnf 125 " << AllClauses.size();
for (int i = 0; i < AllClauses.size(); i++)
{
cout << "\n";
for (int j = 0; j < AllClauses[i].size(); j++)
{
cout << AllClauses[i][j] << " ";
}
cout << "0";
}
cout<<endl;
}