-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transition.java
376 lines (341 loc) · 13.9 KB
/
Transition.java
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package siteswapsuite;
import java.util.List;
import java.util.ArrayList;
class ImpossibleTransitionException extends SiteswapException {
String message = "ERROR: cannot compute transition between non-finite states";
public String getMessage() {
return this.message;
}
}
abstract class Transition extends Siteswap {
int eventualPeriod = 0;
static Transition compute(State from, State to, int minLength, boolean allowExtraSqueezeCatches, boolean generateBallAntiballPairs) throws ImpossibleTransitionException {
Util.printf(from, Util.DebugLevel.DEBUG);
Util.printf(to, Util.DebugLevel.DEBUG);
Util.printf("", Util.DebugLevel.DEBUG);
// first check that the states are finite, otherwise there won't be a transition
if(!from.isFinite() || !to.isFinite()) {
throw new ImpossibleTransitionException();
}
// make copies of the states, so as not to muck up the originals
State fromCopy = from.deepCopy();
State toCopy = to.deepCopy();
// see if either state is empty
if(fromCopy.finiteLength() == 0 || toCopy.finiteLength() == 0) {
return new EmptyTransition(from.numHands());
}
// equalize the state lengths
if(fromCopy.finiteLength() < toCopy.finiteLength())
fromCopy.getFiniteNode(toCopy.finiteLength() - 1);
else if (fromCopy.finiteLength() > toCopy.finiteLength())
toCopy.getFiniteNode(fromCopy.finiteLength() - 1);
// determine which subclass constructor to call
if(allowExtraSqueezeCatches) {
if(generateBallAntiballPairs)
return new OneBeatTransition(fromCopy, toCopy); // minLength is irrelevant here, as it will always end up being one beat, then unAntitossified
else
return new AllowExtraSqueezeCatches(fromCopy, toCopy, minLength);
} else {
if(generateBallAntiballPairs)
return new GenerateBallAntiballPairs(fromCopy, toCopy, minLength);
else
return new StandardTransition(fromCopy, toCopy, minLength);
}
}
// link to Siteswap() constructor for subclasses
private Transition(int numHands) { super(numHands); }
private static class EmptyTransition extends Transition {
private EmptyTransition(int numHands) {
super(numHands);
}
}
private static class StandardTransition extends Transition {
private StandardTransition(State from, State to, int minLength) {
super(from.numHands());
int b = 0; // index of beat in output siteswap
Util.printf("s1: " + from.toString(), Util.DebugLevel.DEBUG);
Util.printf("s2: " + to.toString(), Util.DebugLevel.DEBUG);
State.DiffSum diffs;
int futureCatches = 0;
int futureAnticatches = 0;
diffs = from.diffSums(to); // compute difference sum
Util.printf(diffs, Util.DebugLevel.DEBUG);
int ballNumDiff = (diffs.catches - diffs.antiCatches) - (diffs.tosses - diffs.antiTosses);
Util.printf("ballNumDiff: " + ballNumDiff, Util.DebugLevel.DEBUG);
int ballNumDiffPositive = (ballNumDiff > 0 ? ballNumDiff : 0);
int ballNumDiffNegative = (ballNumDiff < 0 ? ballNumDiff : 0);
Util.printf("this: ", Util.DebugLevel.DEBUG);
Util.printf(this, Util.DebugLevel.DEBUG);
Util.printf("", Util.DebugLevel.DEBUG);
int debugCounter = 20;
// find the transition!
while(b < minLength || diffs.tosses != 0 || diffs.antiTosses != 0 || futureCatches + ballNumDiffNegative != diffs.catches || futureAnticatches + ballNumDiffPositive != diffs.antiCatches) {
Util.printf(">>>>> b: " + b, Util.DebugLevel.DEBUG);
this.appendEmptyBeat();
// see if we can catch new balls/antiballs
for(int h=0; h<numHands; h++) {
if(from.getChargeAtBeatAtHand(0,h) == 0) {
Util.printf(to.getChargeAtBeatAtHand(0,h), Util.DebugLevel.DEBUG);
if(ballNumDiffNegative < 0 && to.getChargeAtBeatAtHand(0,h) < 0) {
Util.printf("catching new antiball at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteAntitoss(b, h, InfinityType.NEGATIVE_INFINITY);
from.decChargeOfNowNodeAtHand(h);
ballNumDiffNegative++;
} else if(ballNumDiffPositive > 0 && to.getChargeAtBeatAtHand(0,h) > 0) {
Util.printf("catching new ball at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteToss(b, h, InfinityType.NEGATIVE_INFINITY);
from.incChargeOfNowNodeAtHand(h);
ballNumDiffPositive--;
}
}
}
// shift goal state backward by one beat, and match lengths
Util.printf("shifting", Util.DebugLevel.DEBUG);
to.shiftBackward();
from.getFiniteNode(to.finiteLength() - 1);
Util.printf("s1: " + from.toString(), Util.DebugLevel.DEBUG);
Util.printf("s2: " + to.toString(), Util.DebugLevel.DEBUG);
// make tosses to match charges in nodes between states
for(int h=0; h<numHands; h++) {
int chargeAtHand = from.getChargeAtBeatAtHand(0, h);
while(chargeAtHand > 0) {
Util.printf("performing toss at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteToss(b, h, InfinityType.POSITIVE_INFINITY);
chargeAtHand--;
if(ballNumDiffNegative < 0 && diffs.catches == 0)
ballNumDiffNegative++;
else
futureCatches++;
}
while(chargeAtHand < 0) {
Util.printf("performing antitoss at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteAntitoss(b, h, InfinityType.POSITIVE_INFINITY);
chargeAtHand++;
if(ballNumDiffPositive > 0 && diffs.antiCatches == 0)
ballNumDiffPositive--;
else
futureAnticatches++;
}
}
Util.printf("advancing time", Util.DebugLevel.DEBUG);
from.advanceTime();
to.advanceTime();
b++;
Util.printf("s1: " + from.toString(), Util.DebugLevel.DEBUG);
Util.printf("s2: " + to.toString(), Util.DebugLevel.DEBUG);
diffs = from.diffSums(to);
Util.printf(diffs, Util.DebugLevel.DEBUG);
Util.printf("futureCatches: " + futureCatches, Util.DebugLevel.DEBUG);
Util.printf("futureAnticatches: " + futureAnticatches, Util.DebugLevel.DEBUG);
Util.printf("ballNumDiffPositive: " + ballNumDiffPositive, Util.DebugLevel.DEBUG);
Util.printf("ballNumDiffNegative: " + ballNumDiffNegative, Util.DebugLevel.DEBUG);
Util.printf(this, Util.DebugLevel.DEBUG);
debugCounter--;
if(debugCounter == 0) {
Util.printf("debug counter threshhold reached; aborting", Util.DebugLevel.DEBUG);
break;
}
}
this.eventualPeriod = b;
this.appendEmptyBeat();
Util.printf(this, Util.DebugLevel.DEBUG);
Util.printf("FINDING CATCHES!", Util.DebugLevel.DEBUG);
// find catches!
while(from.finiteLength() > 0) {
for(int h=0; h<numHands; h++) {
int diff = to.getChargeAtBeatAtHand(0, h) - from.getChargeAtBeatAtHand(0, h);
if(diff > 0) {
Util.printf("catching ball at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteToss(b, h, InfinityType.NEGATIVE_INFINITY);
} else if(diff < 0) {
Util.printf("catching antiball at beat " + b, Util.DebugLevel.DEBUG);
this.addInfiniteAntitoss(b, h, InfinityType.NEGATIVE_INFINITY);
}
}
b++;
this.appendEmptyBeat();
from.advanceTime();
to.advanceTime();
}
Util.printf("found general transition:", Util.DebugLevel.DEBUG);
Util.printf(this, Util.DebugLevel.DEBUG);
Util.printf("-", Util.DebugLevel.DEBUG);
}
}
private static class AllowExtraSqueezeCatches extends Transition {
private AllowExtraSqueezeCatches(State from, State to, int minLength) {
super(from.numHands());
}
}
private static class GenerateBallAntiballPairs extends Transition {
private GenerateBallAntiballPairs(State from, State to, int minLength) {
super(from.numHands());
}
}
private static class OneBeatTransition extends Transition {
private OneBeatTransition(State from, State to) {
super(from.numHands());
}
}
public List<Siteswap> unInfinitize(int maxTransitions) {
int numTosses = 0;
int numAntitosses = 0;
// count [anti]tosses
for(int tossBeat=0; tossBeat<eventualPeriod; tossBeat++) {
// loop through hands
for(int tossHand=0; tossHand<numHands; tossHand++) {
// loop through tosses in hand
for(int tossToss=0; tossToss<this.numTossesAtSite(tossBeat,tossHand); tossToss++) {
// see if toss at this index is a real toss
Toss curToss = this.getToss(tossBeat,tossHand,tossToss);
if(curToss.height().sign() > 0) {
if(!curToss.isAntitoss())
numTosses++;
else
numAntitosses++;
}
}
}
}
int numCatches = 0;
int numAnticatches = 0;
// count catches/anticatches
for(int catchBeat=eventualPeriod; catchBeat<period(); catchBeat++) {
// loop through hands
for(int catchHand=0; catchHand<this.numHands; catchHand++) {
// loop through tosses in hand
for(int catchToss=0; catchToss<this.numTossesAtSite(catchBeat,catchHand); catchToss++) {
Toss curCatch = this.getToss(catchBeat, catchHand, catchToss);
// make sure it's actually a catch, not a zero-toss
if(curCatch.height().sign() < 0) {
if(!curCatch.isAntitoss())
numCatches++;
else
numAnticatches++;
}
}
}
}
int extraTosses = numTosses - numCatches;
int extraAntitosses = numAntitosses - numAnticatches;
Util.printf(" numTosses: " + numTosses, Util.DebugLevel.DEBUG);
Util.printf(" numCatches: " + numCatches, Util.DebugLevel.DEBUG);
Util.printf(" numAntitosses: " + numAntitosses, Util.DebugLevel.DEBUG);
Util.printf("numAnticatches: " + numAnticatches, Util.DebugLevel.DEBUG);
Util.printf("> extraTosses: " + extraTosses, Util.DebugLevel.DEBUG);
Util.printf("> extraAntitosses: " + extraAntitosses, Util.DebugLevel.DEBUG);
// get list of all options for tosses, and null where non-tosses are
List<List<Toss>> tossOptionsList = new ArrayList<List<Toss>>();
for(int tossBeat=0; tossBeat<eventualPeriod; tossBeat++) {
// loop through hands
for(int tossHand=0; tossHand<numHands; tossHand++) {
// loop through tosses in hand
for(int tossToss=0; tossToss<this.numTossesAtSite(tossBeat,tossHand); tossToss++) {
// see if toss at this index is a real toss
Toss curToss = this.getToss(tossBeat,tossHand,tossToss);
if(curToss.height().sign() <= 0) {
tossOptionsList.add(null);
} else {
// add the appropriate number of infinite-height tosses of appropriate charge
ArrayList<Toss> tossOptions = new ArrayList<Toss>();
if(!curToss.isAntitoss()) {
for(int i=0; i<extraTosses; i++) {
tossOptions.add(new Toss(InfinityType.POSITIVE_INFINITY, false));
}
} else {
for(int i=0; i<extraAntitosses; i++) {
tossOptions.add(new Toss(InfinityType.POSITIVE_INFINITY, true));
}
}
// loop through catches to get all other possible tosses
for(int catchBeat=eventualPeriod; catchBeat<period(); catchBeat++) {
// loop through hands
for(int catchHand=0; catchHand<this.numHands; catchHand++) {
// loop through tosses in hand
for(int catchToss=0; catchToss<this.numTossesAtSite(catchBeat,catchHand); catchToss++) {
Toss curCatch = this.getToss(catchBeat, catchHand, catchToss);
// make sure it's a catch of matching charge
if(curCatch.height().sign() < 0 && curCatch.isAntitoss() == curToss.isAntitoss()) {
int height = catchBeat - tossBeat;
tossOptions.add(new Toss(height, catchHand, curToss.isAntitoss()));
}
}
}
}
tossOptionsList.add(tossOptions);
}
}
}
}
Util.printf("tossOptionsList", Util.DebugLevel.DEBUG);
Util.printf(tossOptionsList, Util.DebugLevel.DEBUG);
Util.printf("toss perms: " + (numCatches + extraTosses), Util.DebugLevel.DEBUG);
Util.printf("antiToss perms: " + (numAnticatches + extraAntitosses), Util.DebugLevel.DEBUG);
List<List<Integer>> tossPerms = findAllPermutations(numCatches + extraTosses);
List<List<Integer>> antiTossPerms = findAllPermutations(numAnticatches + extraAntitosses);
// combine into final list of transitions! (to be processed by a different class shortly...)
List<Siteswap> ret = new ArrayList<Siteswap>();
for(int t1=0; t1<tossPerms.size(); t1++) {
for(int t2=0; t2<antiTossPerms.size(); t2++) {
int totalFlatAnyTossIndex = 0;
List<Integer> curTossPerm = tossPerms.get(t1);
int flatTossIndex = 0;
List<Integer> curAntitossPerm = antiTossPerms.get(t2);
int flatAntitossIndex = 0;
Siteswap curSS = new Siteswap(numHands);
if(maxTransitions != -1 && maxTransitions <= 0)
return ret;
for(int b=0; b<eventualPeriod; b++) {
curSS.appendEmptyBeat();
for(int h=0; h<numHands; h++) {
for(int t=0; t<numTossesAtSite(b, h); t++) {
Toss curToss = this.getToss(b, h, t);
if(tossOptionsList.get(totalFlatAnyTossIndex) == null) {
curSS.addToss(b, h, curToss);
} else {
if(!curToss.isAntitoss()) {
curSS.addToss(b, h, tossOptionsList.get(totalFlatAnyTossIndex).get(curTossPerm.get(flatTossIndex)));
flatTossIndex++;
} else {
curSS.addToss(b, h, tossOptionsList.get(totalFlatAnyTossIndex).get(curAntitossPerm.get(flatAntitossIndex)));
flatAntitossIndex++;
}
}
totalFlatAnyTossIndex++;
}
}
}
ret.add(curSS);
if(maxTransitions != -1)
maxTransitions--;
}
}
Util.printf(ret, Util.DebugLevel.DEBUG);
return ret;
}
static List<List<Integer>> findAllPermutations(int numTosses) {
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i=0; i<numTosses; i++) {
list.add(i);
}
return findAllPermutationsHelper(list);
}
static List<List<Integer>> findAllPermutationsHelper(List<Integer> list) {
if(list.size() == 0) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(new ArrayList<Integer>());
return result;
}
Integer firstElement = list.remove(0);
List<List<Integer>> returnValue = new ArrayList<List<Integer>>();
List<List<Integer>> permutations = findAllPermutationsHelper(list);
for(List<Integer> smallerPermutated : permutations) {
for(int index=0; index <= smallerPermutated.size(); index++) {
List<Integer> temp = new ArrayList<Integer>(smallerPermutated);
temp.add(index, firstElement);
returnValue.add(temp);
}
}
return returnValue;
}
}