use of core.action.reachable.LockedReachable in project solution-finder by knewjade.
the class BuildUpTest method randomShortByPacking.
@Test
void randomShortByPacking() throws ExecutionException, InterruptedException {
// Initialize
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
// Define size
int height = 4;
int basicWidth = 3;
SizedBit sizedBit = new SizedBit(basicWidth, height);
SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
// Create basic solutions
TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
AtomicInteger counter = new AtomicInteger();
for (int count = 0; count < 10000; count++) {
// Create field
int numOfMinos = randoms.nextInt(1, 7);
Field field = randoms.field(height, numOfMinos);
// Search
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
SolutionFilter solutionFilter = new AllPassedSolutionFilter();
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
Optional<Result> resultOptional = searcher.findAny();
// If found solution
resultOptional.ifPresent(result -> {
counter.incrementAndGet();
LinkedList<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
LockedReachable reachable = lockedReachableThreadLocal.get();
boolean exists = BuildUp.existsValidBuildPattern(field, operationWithKeys, height, reachable);
if (exists) {
// cansBuildでtrueとなるものがあることを確認
Optional<List<MinoOperationWithKey>> valid = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).filter(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get())).findAny();
assertThat(valid.isPresent()).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// checksKeyは必ずtrueとなる
assertThat(BuildUp.checksKey(operationWithKeys, 0L, height)).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずtrueになる
assert valid.isPresent();
List<MinoOperationWithKey> keys = valid.get();
List<Piece> pieces = keys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, keys.stream(), pieces, height, reachable)).isTrue();
} else {
// cansBuildですべてがfalseとなることを確認
boolean noneMatch = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).noneMatch(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get()));
assertThat(noneMatch).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずfalseになる
List<Piece> pieces = operationWithKeys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, operationWithKeys.stream(), pieces, height, reachable)).isFalse();
}
});
}
System.out.println(counter);
}
use of core.action.reachable.LockedReachable in project solution-finder by knewjade.
the class BuildUpTest method checksAllPatterns1.
@Test
void checksAllPatterns1() throws Exception {
int height = 4;
Field field = FieldFactory.createField("" + "____XXXXXX" + "___XXXXXXX" + "__XXXXXXXX" + "___XXXXXXX");
Operations operations = new Operations(Arrays.asList(new SimpleOperation(Piece.T, Rotate.Right, 0, 1), new SimpleOperation(Piece.S, Rotate.Spawn, 2, 1), new SimpleOperation(Piece.Z, Rotate.Spawn, 1, 0)));
// OperationWithKeyに変換
MinoFactory minoFactory = new MinoFactory();
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, height);
// reachableの準備
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
// existsValidBuildPatternのチェック
boolean exists = BuildUp.existsValidBuildPattern(field, operationWithKeys, height, reachable);
assertThat(exists).isTrue();
// 有効な手順を列挙する
BuildUpStream buildUpStream = new BuildUpStream(reachable, height);
Set<List<MinoOperationWithKey>> validPatterns = buildUpStream.existsValidBuildPattern(field, operationWithKeys).collect(Collectors.toSet());
// すべての組み合わせでチェック
Iterable<List<MinoOperationWithKey>> iterable = new PermutationIterable<>(operationWithKeys, operationWithKeys.size());
for (List<MinoOperationWithKey> withKeys : iterable) {
boolean canBuild = BuildUp.cansBuild(field, withKeys, height, reachable);
assertThat(canBuild).isEqualTo(validPatterns.contains(withKeys));
boolean checksKey = BuildUp.checksKey(withKeys, 0L, height);
assertThat(checksKey).isTrue();
}
}
use of core.action.reachable.LockedReachable in project solution-finder by knewjade.
the class BuildUpTest method randomLongByPacking.
@Test
@LongTest
void randomLongByPacking() throws ExecutionException, InterruptedException {
// Initialize
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
// Define size
int height = 4;
int basicWidth = 3;
SizedBit sizedBit = new SizedBit(basicWidth, height);
SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
// Create basic solutions
TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
AtomicInteger counter = new AtomicInteger();
for (int count = 0; count < 100; count++) {
// Create field
int numOfMinos = randoms.nextIntClosed(7, 10);
Field field = randoms.field(height, numOfMinos);
// Search
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
SolutionFilter solutionFilter = new AllPassedSolutionFilter();
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
Optional<Result> resultOptional = searcher.findAny();
// If found solution
resultOptional.ifPresent(result -> {
counter.incrementAndGet();
LinkedList<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
LockedReachable reachable = lockedReachableThreadLocal.get();
boolean exists = BuildUp.existsValidBuildPattern(field, operationWithKeys, height, reachable);
if (exists) {
// cansBuildでtrueとなるものがあることを確認
Optional<List<MinoOperationWithKey>> valid = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).filter(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get())).findAny();
assertThat(valid.isPresent()).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// checksKeyは必ずtrueとなる
assertThat(BuildUp.checksKey(operationWithKeys, 0L, height)).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずtrueになる
assert valid.isPresent();
List<MinoOperationWithKey> keys = valid.get();
List<Piece> pieces = keys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, keys.stream(), pieces, height, reachable)).isTrue();
} else {
// cansBuildですべてがfalseとなることを確認
boolean noneMatch = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).noneMatch(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get()));
assertThat(noneMatch).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずfalseになる
List<Piece> pieces = operationWithKeys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, operationWithKeys.stream(), pieces, height, reachable)).isFalse();
}
});
}
System.out.println(counter);
}
use of core.action.reachable.LockedReachable in project solution-finder by knewjade.
the class CheckerNoHoldTest method testCase3.
@Test
void testCase3() throws Exception {
List<Pair<List<Piece>, Boolean>> testCases = new ArrayList<Pair<List<Piece>, Boolean>>() {
{
add(new Pair<>(Arrays.asList(T, I, L, S, O, Z, J), false));
add(new Pair<>(Arrays.asList(O, J, I, L, T, S, Z), false));
add(new Pair<>(Arrays.asList(O, J, L, T, I, S, Z), true));
}
};
// Field
String marks = "" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "XXXXXXXX__" + "XXXXXXXX__" + "";
Field field = FieldFactory.createField(marks);
int maxClearLine = 8;
int maxDepth = 7;
// Initialize
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, maxClearLine);
// Assertion
for (Pair<List<Piece>, Boolean> testCase : testCases) {
// Set test case
List<Piece> pieces = testCase.getKey();
Boolean expectedCount = testCase.getValue();
// Execute
boolean isSucceed = checker.check(field, pieces, candidate, maxClearLine, maxDepth);
assertThat(isSucceed).isEqualTo(expectedCount);
// Check result
if (isSucceed)
assertResult(field, maxClearLine, reachable, pieces);
}
}
use of core.action.reachable.LockedReachable in project solution-finder by knewjade.
the class CheckerNoHoldTest method testGraceSystem.
@Test
void testGraceSystem() throws Exception {
List<Pair<List<Piece>, Boolean>> testCases = new ArrayList<Pair<List<Piece>, Boolean>>() {
{
add(new Pair<>(Arrays.asList(T, S, O, J), false));
add(new Pair<>(Arrays.asList(T, O, J, S), false));
add(new Pair<>(Arrays.asList(T, T, L, J), true));
add(new Pair<>(Arrays.asList(T, T, S, Z), true));
add(new Pair<>(Arrays.asList(T, S, Z, T), false));
add(new Pair<>(Arrays.asList(J, S, Z, L), false));
add(new Pair<>(Arrays.asList(Z, I, O, T), false));
add(new Pair<>(Arrays.asList(I, J, J, O), true));
add(new Pair<>(Arrays.asList(T, S, Z, J), false));
add(new Pair<>(Arrays.asList(L, S, Z, T), false));
}
};
// Field
String marks = "" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "XXXXXX____" + "";
Field field = FieldFactory.createField(marks);
int maxClearLine = 4;
int maxDepth = 4;
// Initialize
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, maxClearLine);
// Assertion
for (Pair<List<Piece>, Boolean> testCase : testCases) {
// Set test case
List<Piece> pieces = testCase.getKey();
Boolean expectedCount = testCase.getValue();
// Execute
boolean isSucceed = checker.check(field, pieces, candidate, maxClearLine, maxDepth);
assertThat(isSucceed).isEqualTo(expectedCount);
// Check result
if (isSucceed)
assertResult(field, maxClearLine, reachable, pieces);
}
}
Aggregations