-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatticeMaker.cgs
261 lines (208 loc) · 6.16 KB
/
latticeMaker.cgs
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
/*
User Methods:
nextDay(games): return all next day games based on the given list of games
makeDot(games): return a list of all pairs (g1,g2) such that g1<g2 and g1,g2 belongs to 'games'
Eg. create a lattice for the second day of game 0:
games = [0];
d1 = nextDay(games);
d2 = nextDay(d1) >> out;
makeDot(d2) >> out;
The script must produce exactly two lines.
The first with the list of games
The second with the entire lattice information
This must be placed at the end of this script!
*/
/**********************************************************************************************
* returns the antichains of the given game list
* an antichain is a subset of a partially ordered set such that any two elements
* in the subset are incomparable
*
* This is a NP-hard implementation, but it will be used for small sets
* 1) Create all game permutations
* 2) For each permutation p: insert it if it is an antichain
****/
antichains := proc (games)
local nGames, permSize, perm, p, result;
nGames = Length(games);
permSize = power2(nGames);
perm = seq(0, n=0..nGames);
result = [];
for i from 1 to permSize do
/* get subset of games for this permutation */
p = subset(perm, games);
if isAntichain(p) then
Add(result, p);
fi;
perm = nextPermutation(perm);
od;
result
end;
/************************************************************************
* check if a list of games is an antichain
*/
isAntichain := proc (games)
local i, j, result;
result = true;
for i from 1 to Length(games) do
for j from i+1 to Length(games) do
if not (games[i]<>games[j]) then
result = false;
fi;
od;
od;
result
end;
/************************************************************************
* Compute next permutation ([0,0,..,0] -> [1,0...] -> [0,1,...] -> ... -> [1,1,...1]
* pre: never receives permutation [1,1,...,1]
*/
nextPermutation := proc (perm)
local i, result;
i = 1;
result = perm;
if result[i] == 0 then
result[i] = 1;
else
while result[i] == 1 do
result[i] = 0;
i = i +1;
od;
result[i] = 1;
fi;
result
end;
/************************************************************************
* return a list of games included on a given permutation
*/
subset := proc (perm, games)
local i, result;
result = [];
for i from 1 to Length(perm) do
if perm[i] == 1 then
Add(result, games[i]);
fi;
od;
result
end;
/********************* 2^n ***********************/
power2 := proc (n)
local i, result;
result = 1;
for i from 1 to n do
result = result + result;
od;
result
end;
/*************************************************************************
* Create game matrix given options (for both Left & Right)
****/
matrixGames := proc (games)
local i, j, table, header, line, nGames;
nGames = Length(games);
/* make matrix header */
header = [];
Add(header, "{row | col}");
for i from 1 to nGames do
Add(header, games[i]);
od;
table = CreateTable(1+nGames);
AddTableRow(table, header);
for i from 1 to nGames do /* make matrix results, line by line */
line = [games[i]];
for j from 1 to nGames do
Add(line, C({games[i] | games[j]}) );
od;
AddTableRow(table, line);
od;
table
end;
/*************************************************************************
* Given a list of games, compare each other and make a table of results
* e.g.: matrixCompare([0,*,3,^,vv,^*])
****/
matrixCompare := proc (games)
local i, j, table, header, line, nGames;
nGames = Length(games);
/* make matrix header */
header = [];
Add(header, "row 'op' col");
for i from 1 to nGames do
Add(header, games[i]);
od;
table = CreateTable(1+nGames);
AddTableRow(table, header);
for i from 1 to nGames do /* make matrix results, line by line */
line = [games[i]];
for j from 1 to i do
if games[i] == games[j] then
Add(line, "=");
elif games[i] < games[j] then
Add(line, "<");
elif games[i] > games[j] then
Add(line, ">");
elif games[i] <> games[j] then
Add(line, "||");
fi;
od;
AddTableRow(table, line);
od;
table
end;
/*************************************************************************
* return a list of all pairs (g1,g2) such that g1<g2 and g1,g2 belongs to 'games'
****/
makeDot := proc (games)
local i, nGames, pairs;
nGames = Length(games);
pairs = [];
for i from 1 to nGames do /* make matrix results, line by line */
for j from 1 to nGames do
if games[i] < games[j] then
Add(pairs, games[i]);
Add(pairs, games[j]);
fi;
od;
od;
pairs
end;
/*************************************************************************
* Remove duplicates from canonical list of games
****/
removeDuplicates := proc (games)
local result, find;
result = [];
for i from 1 to Length(games) do
find = false;
for j from i+1 to Length(games) do
if games[i] == games[j] then
find = true;
fi;
od;
if not find then
Add(result, games[i]);
fi;
od;
result
end;
/*************************************************************************
* Return all next day games based on the given games
****/
nextDay := proc (games)
local options;
options = antichains(games);
allGames = [];
for i from 1 to Length(options) do
for j from 1 to Length(options) do
Add(allGames, C({options[i] | options[j]}) );
od;
od;
/* This line, if uncommented, includes all games from the previous day */
/* Append(allGames, games); */
removeDuplicates(allGames)
end;
/***************************************************************************/
games = [0];
/*games = [*,*2,*3,1*,1*2,1*3];*/
d1 = nextDay(games);
d2 = nextDay(d1) >> out;
makeDot(d2) >> out;