Search in sources :

Example 36 with LockedReachable

use of core.action.reachable.LockedReachable 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);
    }
}
Also used : Action(common.datastore.action.Action) ArrayList(java.util.ArrayList) Field(core.field.Field) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) ArrayList(java.util.ArrayList) List(java.util.List) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LongTest(module.LongTest)

Example 37 with LockedReachable

use of core.action.reachable.LockedReachable 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);
    }
}
Also used : Action(common.datastore.action.Action) ArrayList(java.util.ArrayList) Field(core.field.Field) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) ArrayList(java.util.ArrayList) List(java.util.List) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LongTest(module.LongTest) LongTest(module.LongTest)

Example 38 with LockedReachable

use of core.action.reachable.LockedReachable 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);
        });
    }
}
Also used : Action(common.datastore.action.Action) LockedCandidate(core.action.candidate.LockedCandidate) Field(core.field.Field) ColumnSmallField(core.column_field.ColumnSmallField) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) InOutPairField(searcher.pack.InOutPairField) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) PerfectValidator(searcher.common.validator.PerfectValidator) BuildUpStream(common.buildup.BuildUpStream) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) HashSet(java.util.HashSet) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) FilterOnDemandBasicSolutions(searcher.pack.solutions.FilterOnDemandBasicSolutions) MappedBasicSolutions(searcher.pack.solutions.MappedBasicSolutions) BasicSolutions(searcher.pack.calculator.BasicSolutions) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Randoms(lib.Randoms) CheckerNoHold(searcher.checker.CheckerNoHold) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) LockedReachable(core.action.reachable.LockedReachable)

Example 39 with LockedReachable

use of core.action.reachable.LockedReachable 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);
        });
    }
}
Also used : Action(common.datastore.action.Action) LockedCandidate(core.action.candidate.LockedCandidate) Field(core.field.Field) ColumnSmallField(core.column_field.ColumnSmallField) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) InOutPairField(searcher.pack.InOutPairField) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) PerfectValidator(searcher.common.validator.PerfectValidator) BuildUpStream(common.buildup.BuildUpStream) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) HashSet(java.util.HashSet) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) FilterOnDemandBasicSolutions(searcher.pack.solutions.FilterOnDemandBasicSolutions) MappedBasicSolutions(searcher.pack.solutions.MappedBasicSolutions) BasicSolutions(searcher.pack.calculator.BasicSolutions) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Randoms(lib.Randoms) CheckerNoHold(searcher.checker.CheckerNoHold) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) LockedReachable(core.action.reachable.LockedReachable)

Example 40 with LockedReachable

use of core.action.reachable.LockedReachable in project solution-finder by knewjade.

the class CheckmateUsingHoldTest method testMultiPath1.

@Test
void testMultiPath1() throws Exception {
    // Field
    String marks = "" + "X________X" + "XX__XX__XX" + "XX__XX__XX" + "";
    Field field = FieldFactory.createField(marks);
    int maxClearLine = 3;
    int maxDepth = 4;
    // Initialize
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
    LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, maxClearLine);
    // Assertion
    // Set test case
    List<Piece> pieces = Arrays.asList(J, L, S, Z);
    int expectedCount = 4;
    // 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);
}
Also used : Field(core.field.Field) LockedCandidate(core.action.candidate.LockedCandidate) Action(common.datastore.action.Action) Piece(core.mino.Piece) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LongTest(module.LongTest)

Aggregations

LockedReachable (core.action.reachable.LockedReachable)42 Field (core.field.Field)41 LongTest (module.LongTest)38 Test (org.junit.jupiter.api.Test)35 Piece (core.mino.Piece)34 Action (common.datastore.action.Action)31 LockedCandidate (core.action.candidate.LockedCandidate)30 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)24 MinoRotation (core.srs.MinoRotation)18 MinoFactory (core.mino.MinoFactory)17 MinoShifter (core.mino.MinoShifter)17 ColumnField (core.column_field.ColumnField)16 InOutPairField (searcher.pack.InOutPairField)16 Randoms (lib.Randoms)13 List (java.util.List)11 FieldFactory (core.field.FieldFactory)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 PermutationIterable (common.iterable.PermutationIterable)8 OperationTransform (common.parser.OperationTransform)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8