Search in sources :

Example 6 with PermutationIterable

use of common.iterable.PermutationIterable 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);
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) java.util(java.util) OperationTransform(common.parser.OperationTransform) common.datastore(common.datastore) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) ResultHelper(common.ResultHelper) OperationInterpreter(common.parser.OperationInterpreter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) LockedCandidate(core.action.candidate.LockedCandidate) FieldView(core.field.FieldView) Action(common.datastore.action.Action) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PerfectValidator(searcher.common.validator.PerfectValidator) SeparableMinos(searcher.pack.SeparableMinos) Candidate(core.action.candidate.Candidate) StreamSupport(java.util.stream.StreamSupport) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) MinoRotation(core.srs.MinoRotation) LockedReachable(core.action.reachable.LockedReachable) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) PermutationIterable(common.iterable.PermutationIterable) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) InOutPairField(searcher.pack.InOutPairField) Rotate(core.srs.Rotate) CheckerUsingHold(searcher.checker.CheckerUsingHold) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) SolutionFilter(searcher.pack.memento.SolutionFilter) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) OperationWithKeyInterpreter(common.parser.OperationWithKeyInterpreter) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) Piece(core.mino.Piece) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) MinoFactory(core.mino.MinoFactory) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) PermutationIterable(common.iterable.PermutationIterable) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SizedBit(searcher.pack.SizedBit) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) MinoShifter(core.mino.MinoShifter) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 7 with PermutationIterable

use of common.iterable.PermutationIterable in project solution-finder by knewjade.

the class CheckerUsingHoldCountTest method testTemplate.

@Test
void testTemplate() throws Exception {
    // Invoker
    List<Piece> pieces = Arrays.asList(I, T, S, Z, J, L, O);
    int popCount = 4;
    int maxClearLine = 4;
    int maxDepth = 3;
    // Field
    String marks = "" + "XXXXX____X" + "XXXXXX___X" + "XXXXXXX__X" + "XXXXXX___X" + "";
    Field field = FieldFactory.createField(marks);
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    PerfectValidator validator = new PerfectValidator();
    CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
    // Measure
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
    AnalyzeTree tree = new AnalyzeTree();
    Iterable<List<Piece>> combinations = new PermutationIterable<>(pieces, popCount);
    for (List<Piece> combination : combinations) {
        boolean result = checker.check(field, combination, candidate, maxClearLine, maxDepth);
        tree.set(result, combination);
    }
    // Source: Nilgiri: https://docs.google.com/spreadsheets/d/1bVY3t_X96xRmUL0qdgB9tViSIGenu6RMKX4RW7qWg8Y/edit#gid=0
    assertThat(tree.getSuccessPercent()).isEqualTo(514 / 840.0);
}
Also used : Action(common.datastore.action.Action) PermutationIterable(common.iterable.PermutationIterable) AnalyzeTree(common.tree.AnalyzeTree) Field(core.field.Field) MinoRotation(core.srs.MinoRotation) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) List(java.util.List) PerfectValidator(searcher.common.validator.PerfectValidator) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 8 with PermutationIterable

use of common.iterable.PermutationIterable in project solution-finder by knewjade.

the class CheckerUsingHoldCountTest method testBT4_5.

@Test
@LongTest
void testBT4_5() throws Exception {
    // Invoker
    List<Piece> pieces = Arrays.asList(I, T, S, Z, J, L, O);
    int popCount = 7;
    int maxClearLine = 6;
    int maxDepth = 7;
    // Field
    String marks = "" + "XX________" + "XX________" + "XXX______X" + "XXXXXXX__X" + "XXXXXX___X" + "XXXXXXX_XX" + "";
    Field field = FieldFactory.createField(marks);
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    PerfectValidator validator = new PerfectValidator();
    CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
    // Measure
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
    AnalyzeTree tree = new AnalyzeTree();
    Iterable<List<Piece>> combinations = new PermutationIterable<>(pieces, popCount);
    for (List<Piece> combination : combinations) {
        boolean result = checker.check(field, combination, candidate, maxClearLine, maxDepth);
        tree.set(result, combination);
    }
    // Source: myself 20170415
    assertThat(tree.getSuccessPercent()).isEqualTo(5038 / 5040.0);
}
Also used : Action(common.datastore.action.Action) PermutationIterable(common.iterable.PermutationIterable) AnalyzeTree(common.tree.AnalyzeTree) Field(core.field.Field) MinoRotation(core.srs.MinoRotation) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) List(java.util.List) PerfectValidator(searcher.common.validator.PerfectValidator) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 9 with PermutationIterable

use of common.iterable.PermutationIterable in project solution-finder by knewjade.

the class CheckerUsingHoldCountTest method runTestCase.

private AnalyzeTree runTestCase(List<Piece> pieces, int popCount, int maxClearLine, int maxDepth, String marks) {
    Field field = FieldFactory.createField(marks);
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    PerfectValidator validator = new PerfectValidator();
    CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
    // Measure
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
    AnalyzeTree tree = new AnalyzeTree();
    Iterable<List<Piece>> combinations = new PermutationIterable<>(pieces, popCount);
    for (List<Piece> combination : combinations) {
        // Hold分の追加
        combination.add(0, Piece.T);
        boolean result = checker.check(field, combination, candidate, maxClearLine, maxDepth);
        tree.set(result, combination);
    }
    return tree;
}
Also used : Action(common.datastore.action.Action) PermutationIterable(common.iterable.PermutationIterable) AnalyzeTree(common.tree.AnalyzeTree) Field(core.field.Field) MinoRotation(core.srs.MinoRotation) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) List(java.util.List) PerfectValidator(searcher.common.validator.PerfectValidator)

Example 10 with PermutationIterable

use of common.iterable.PermutationIterable in project solution-finder by knewjade.

the class CheckerUsingHoldCountTest method testAfter4Line.

@Test
@LongTest
void testAfter4Line() throws Exception {
    // Invoker
    List<Piece> pieces = Arrays.asList(I, T, S, Z, J, L, O);
    int popCount = 7;
    int maxClearLine = 4;
    int maxDepth = 6;
    // Field
    String marks = "" + "XXXX______" + "XXXX______" + "XXXX______" + "XXXX______" + "";
    Field field = FieldFactory.createField(marks);
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    PerfectValidator validator = new PerfectValidator();
    CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
    // Measure
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
    AnalyzeTree tree = new AnalyzeTree();
    Iterable<List<Piece>> combinations = new PermutationIterable<>(pieces, popCount);
    for (List<Piece> combination : combinations) {
        boolean result = checker.check(field, combination, candidate, maxClearLine, maxDepth);
        tree.set(result, combination);
    }
    // Source: myself 20170415
    assertThat(tree.getSuccessPercent()).isEqualTo(5040 / 5040.0);
}
Also used : Action(common.datastore.action.Action) PermutationIterable(common.iterable.PermutationIterable) AnalyzeTree(common.tree.AnalyzeTree) Field(core.field.Field) MinoRotation(core.srs.MinoRotation) LockedCandidate(core.action.candidate.LockedCandidate) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) List(java.util.List) PerfectValidator(searcher.common.validator.PerfectValidator) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Aggregations

PermutationIterable (common.iterable.PermutationIterable)10 Field (core.field.Field)10 MinoFactory (core.mino.MinoFactory)10 MinoShifter (core.mino.MinoShifter)10 MinoRotation (core.srs.MinoRotation)10 LongTest (module.LongTest)9 Test (org.junit.jupiter.api.Test)9 Piece (core.mino.Piece)8 Action (common.datastore.action.Action)6 LockedCandidate (core.action.candidate.LockedCandidate)6 LockedReachable (core.action.reachable.LockedReachable)6 ColumnField (core.column_field.ColumnField)6 List (java.util.List)6 PerfectValidator (searcher.common.validator.PerfectValidator)6 InOutPairField (searcher.pack.InOutPairField)6 OperationInterpreter (common.parser.OperationInterpreter)4 OperationTransform (common.parser.OperationTransform)4 AnalyzeTree (common.tree.AnalyzeTree)4 LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)4 FieldFactory (core.field.FieldFactory)4