use of core.action.candidate.LockedCandidate in project solution-finder by knewjade.
the class CheckmateNoHoldTest method testLong9.
@Test
void testLong9() throws Exception {
List<Pair<List<Piece>, Integer>> testCases = new ArrayList<Pair<List<Piece>, Integer>>() {
{
add(new Pair<>(Arrays.asList(I, S, Z, T, J, I, S, Z, S, Z), 0));
add(new Pair<>(Arrays.asList(T, S, L, I, Z, J, L, O, O, S), 5));
add(new Pair<>(Arrays.asList(L, Z, S, J, Z, Z, Z, I, T, T), 0));
add(new Pair<>(Arrays.asList(T, T, S, S, Z, Z, L, L, J, J), 5));
add(new Pair<>(Arrays.asList(O, S, O, S, Z, L, Z, L, I, I), 0));
add(new Pair<>(Arrays.asList(J, I, T, O, L, S, I, T, Z, O), 7));
add(new Pair<>(Arrays.asList(S, T, J, L, O, O, T, S, L, L), 6));
add(new Pair<>(Arrays.asList(S, T, J, L, O, O, T, S, L), 6));
add(new Pair<>(Arrays.asList(Z, S, T, I, O, J, L, Z, S), 1));
add(new Pair<>(Arrays.asList(S, T, J, L, O, O, T, S, L), 6));
}
};
// Field
String marks = "" + "__________" + "X_________" + "X_________" + "XX________" + "";
Field field = FieldFactory.createField(marks);
int maxClearLine = 4;
int maxDepth = 9;
// Initialize
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, maxClearLine);
// Assertion
for (Pair<List<Piece>, Integer> testCase : testCases) {
// Set test case
List<Piece> pieces = testCase.getKey();
int expectedCount = testCase.getValue();
// Execute
List<Result> results = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
assertThat(results).as(pieces.toString()).hasSize(expectedCount);
// Check result
for (Result result : results) assertResult(result, field, maxClearLine, reachable, pieces);
}
}
use of core.action.candidate.LockedCandidate in project solution-finder by knewjade.
the class CheckmateNoHoldTest method testLong10.
@Test
@LongTest
void testLong10() throws Exception {
List<Pair<List<Piece>, Integer>> testCases = new ArrayList<Pair<List<Piece>, Integer>>() {
{
add(new Pair<>(Arrays.asList(I, S, Z, T, J, I, S, Z, S, Z, T), 3));
add(new Pair<>(Arrays.asList(S, Z, T, L, J, I, O, S, Z, T, L), 6));
add(new Pair<>(Arrays.asList(T, L, J, Z, S, O, O, T, J, L, I), 21));
add(new Pair<>(Arrays.asList(T, L, J, Z, S, O, O, T, J, L, T), 21));
add(new Pair<>(Arrays.asList(T, L, J, Z, S, O, O, T, J, L), 21));
add(new Pair<>(Arrays.asList(I, S, Z, T, J, I, S, Z, S, Z), 3));
add(new Pair<>(Arrays.asList(T, S, L, I, Z, J, L, O, O, S), 2));
add(new Pair<>(Arrays.asList(L, Z, S, J, Z, Z, Z, I, T, T), 7));
add(new Pair<>(Arrays.asList(T, T, S, S, Z, Z, L, L, J, J), 18));
add(new Pair<>(Arrays.asList(O, S, O, S, Z, L, Z, L, I, I), 4));
add(new Pair<>(Arrays.asList(J, I, T, O, L, S, I, T, Z, O), 9));
add(new Pair<>(Arrays.asList(S, T, J, L, O, O, T, S, L, L), 16));
}
};
// Field
int maxClearLine = 4;
int maxDepth = 10;
Field field = FieldFactory.createField(maxClearLine);
// Initialize
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, maxClearLine);
// Assertion
for (Pair<List<Piece>, Integer> testCase : testCases) {
// Set test case
List<Piece> pieces = testCase.getKey();
int expectedCount = testCase.getValue();
// Execute
List<Result> results = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
assertThat(results).as(pieces.toString()).hasSize(expectedCount);
// Check result
for (Result result : results) assertResult(result, field, maxClearLine, reachable, pieces);
}
}
use of core.action.candidate.LockedCandidate in project solution-finder by knewjade.
the class CheckmateUsingHoldReuseTest method randomCheckmateWithJustBlock.
@Test
@LongTest
void randomCheckmateWithJustBlock() {
Randoms randoms = new Randoms();
for (int count = 0; count < 25; count++) {
int maxClearLine = randoms.nextInt(3, 8);
int maxDepth = randoms.nextIntClosed(5, 7);
List<Piece> pieces = randoms.blocks(maxDepth);
Field field = randoms.field(maxClearLine, maxDepth);
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
Stopwatch stopwatchReuse = Stopwatch.createStoppedStopwatch();
for (int swap = 0; swap < 250; swap++) {
int index = randoms.nextInt(3, pieces.size());
Piece pop = pieces.remove(index);
pieces.add(pop);
stopwatchNoUse.start();
List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchNoUse.stop();
stopwatchReuse.start();
List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchReuse.stop();
assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
}
// TODO:
// assertThat(stopwatchReuse.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
}
}
use of core.action.candidate.LockedCandidate in project solution-finder by knewjade.
the class PackSearcherTest method assertHeight5.
void assertHeight5(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
assert sizedBit.getWidth() == 2;
assert sizedBit.getHeight() == 5;
int width = sizedBit.getWidth();
int height = sizedBit.getHeight();
Randoms randoms = new Randoms();
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
TaskResultHelper taskResultHelper = new BasicMinoPackingHelper();
for (int count = 0; count < maxCount; count++) {
// Field
int maxDepth = randoms.nextIntClosed(3, 6);
Field initField = randoms.field(height, maxDepth);
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, initField);
SolutionFilter solutionFilter = createSRSSolutionFilter(sizedBit, initField);
// Pack
BasicSolutions basicSolutions = basicSolutionSupplier.apply(initField, solutionFilter);
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
List<Result> results = searcher.toList();
// Possible
HashSet<Pieces> possiblePieces = new HashSet<>();
for (Result result : results) {
// result to possible pieces
List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(width).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
Set<LongPieces> sets = new BuildUpStream(reachable, height).existsValidBuildPattern(initField, operationWithKeys).map(keys -> keys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toSet());
possiblePieces.addAll(sets);
}
// Checker
PerfectValidator validator = new PerfectValidator();
CheckerNoHold<Action> checker = new CheckerNoHold<>(minoFactory, validator);
// Assert generator
PatternGenerator generator = createPiecesGenerator(maxDepth);
generator.blocksStream().forEach(blocks -> {
List<Piece> pieceList = blocks.getPieces();
boolean check = checker.check(initField, pieceList, candidate, height, maxDepth);
assertThat(possiblePieces.contains(blocks)).as(pieceList.toString()).isEqualTo(check);
});
}
}
use of core.action.candidate.LockedCandidate in project solution-finder by knewjade.
the class PackSearcherTest method assertHeight4.
private void assertHeight4(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
assert sizedBit.getWidth() == 3;
assert sizedBit.getHeight() == 4;
int width = sizedBit.getWidth();
int height = sizedBit.getHeight();
Randoms randoms = new Randoms();
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
for (int count = 0; count < maxCount; count++) {
// Field
int maxDepth = randoms.nextIntClosed(3, 6);
Field initField = randoms.field(height, maxDepth);
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, initField);
SolutionFilter solutionFilter = createSRSSolutionFilter(sizedBit, initField);
// Pack
BasicSolutions basicSolutions = basicSolutionSupplier.apply(initField, solutionFilter);
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
List<Result> results = searcher.toList();
// Possible
HashSet<Pieces> possiblePieces = new HashSet<>();
for (Result result : results) {
// result to possible pieces
List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(width).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
Set<LongPieces> sets = new BuildUpStream(reachable, height).existsValidBuildPattern(initField, operationWithKeys).map(keys -> keys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toSet());
possiblePieces.addAll(sets);
}
// Checker
PerfectValidator validator = new PerfectValidator();
CheckerNoHold<Action> checker = new CheckerNoHold<>(minoFactory, validator);
// Assert generator
PatternGenerator generator = createPiecesGenerator(maxDepth);
generator.blocksStream().forEach(blocks -> {
List<Piece> pieceList = blocks.getPieces();
boolean check = checker.check(initField, pieceList, candidate, height, maxDepth);
assertThat(possiblePieces.contains(blocks)).as(pieceList.toString()).isEqualTo(check);
});
}
}
Aggregations