Search in sources :

Example 6 with SeparableMino

use of searcher.pack.separable_mino.SeparableMino in project solution-finder by knewjade.

the class BuildUpStreamTest method randomShort.

@Test
void randomShort() 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 = createRandomSolutionFilter(randoms, sizedBit, lockedReachableThreadLocal, field);
        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));
            // Create Pieces
            LockedReachable reachable = lockedReachableThreadLocal.get();
            Set<List<MinoOperationWithKey>> valid = new BuildUpStream(reachable, height).existsValidBuildPattern(field, operationWithKeys).collect(Collectors.toSet());
            PermutationIterable<MinoOperationWithKey> permutations = new PermutationIterable<>(operationWithKeys, operationWithKeys.size());
            for (List<MinoOperationWithKey> permutation : permutations) {
                boolean canBuild = BuildUp.cansBuild(field, permutation, height, reachable);
                if (canBuild) {
                    assertThat(valid).as(FieldView.toString(field)).contains(permutation);
                } else {
                    assertThat(valid).as(FieldView.toString(field)).doesNotContain(permutation);
                }
            }
        });
    }
    System.out.println(counter);
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationInterpreter(common.parser.OperationInterpreter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) FieldView(core.field.FieldView) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SeparableMinos(searcher.pack.SeparableMinos) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) LinkedList(java.util.LinkedList) MinoRotation(core.srs.MinoRotation) LockedReachable(core.action.reachable.LockedReachable) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) OperationWithKey(common.datastore.OperationWithKey) 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) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) 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) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) MinoFactory(core.mino.MinoFactory) LinkedList(java.util.LinkedList) List(java.util.List) 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) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 7 with SeparableMino

use of searcher.pack.separable_mino.SeparableMino in project solution-finder by knewjade.

the class FullOperationSeparableMinoComparatorTest method compareMinoFieldEqual.

@Test
void compareMinoFieldEqual() {
    List<SeparableMino> minos = separableMinos.getMinos();
    Randoms randoms = new Randoms();
    int index = randoms.nextInt(minos.size());
    SeparableMino mino = minos.get(index);
    FullOperationSeparableMinoComparator comparator = new FullOperationSeparableMinoComparator();
    assertThat(comparator.compare(mino, mino)).isEqualTo(0);
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) Test(org.junit.jupiter.api.Test)

Example 8 with SeparableMino

use of searcher.pack.separable_mino.SeparableMino in project solution-finder by knewjade.

the class MinoFieldComparatorTest method compareMinoFieldDiffOuterField.

@Test
void compareMinoFieldDiffOuterField() {
    Randoms randoms = new Randoms();
    List<SeparableMino> minos = separableMinos.getMinos();
    int index = randoms.nextInt(0, minos.size() - 1);
    SeparableMino mino = minos.get(index);
    RecursiveMinoField recursiveMinoField1 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    RecursiveMinoField recursiveMinoField2 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(1L), separableMinos);
    MinoFieldComparator comparator = new MinoFieldComparator();
    assertThat(comparator.compare(recursiveMinoField1, recursiveMinoField2)).isEqualTo(0);
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Test(org.junit.jupiter.api.Test)

Example 9 with SeparableMino

use of searcher.pack.separable_mino.SeparableMino in project solution-finder by knewjade.

the class MinoFieldComparatorTest method compareMinoFieldEqual.

@Test
void compareMinoFieldEqual() {
    Randoms randoms = new Randoms();
    List<SeparableMino> minos = separableMinos.getMinos();
    SeparableMino mino = minos.get(0);
    RecursiveMinoField recursiveMinoField1 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    RecursiveMinoField recursiveMinoField2 = new RecursiveMinoField(mino, ColumnFieldFactory.createField(), separableMinos);
    for (int count = 0; count < 10; count++) {
        int index = randoms.nextInt(0, minos.size() - 1);
        SeparableMino randomMino = minos.get(index);
        recursiveMinoField1 = new RecursiveMinoField(randomMino, recursiveMinoField1, ColumnFieldFactory.createField(), separableMinos);
        recursiveMinoField2 = new RecursiveMinoField(randomMino, recursiveMinoField2, ColumnFieldFactory.createField(), separableMinos);
    }
    MinoFieldComparator comparator = new MinoFieldComparator();
    assertThat(comparator.compare(recursiveMinoField1, recursiveMinoField2)).isEqualTo(0);
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Test(org.junit.jupiter.api.Test)

Example 10 with SeparableMino

use of searcher.pack.separable_mino.SeparableMino in project solution-finder by knewjade.

the class MinoFieldComparatorTest method compareMinoFieldDiffMino.

@Test
void compareMinoFieldDiffMino() {
    Randoms randoms = new Randoms();
    List<SeparableMino> minos = separableMinos.getMinos();
    MinoFieldComparator comparator = new MinoFieldComparator();
    for (int count = 0; count < 100000; count++) {
        int index1 = randoms.nextInt(0, minos.size());
        SeparableMino mino1 = minos.get(index1);
        RecursiveMinoField recursiveMinoField1 = new RecursiveMinoField(mino1, ColumnFieldFactory.createField(), separableMinos);
        int index2 = randoms.nextInt(0, minos.size() - 1);
        if (index1 == index2)
            index2 += 1;
        SeparableMino mino2 = minos.get(index2);
        RecursiveMinoField recursiveMinoField2 = new RecursiveMinoField(mino2, ColumnFieldFactory.createField(), separableMinos);
        // assert is not 0 & sign reversed
        assertThat(comparator.compare(recursiveMinoField1, recursiveMinoField2) * comparator.compare(recursiveMinoField2, recursiveMinoField1)).isLessThan(0);
    }
}
Also used : Randoms(lib.Randoms) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Test(org.junit.jupiter.api.Test)

Aggregations

SeparableMino (searcher.pack.separable_mino.SeparableMino)24 Field (core.field.Field)14 ColumnField (core.column_field.ColumnField)13 Test (org.junit.jupiter.api.Test)13 Randoms (lib.Randoms)12 SeparableMinos (searcher.pack.SeparableMinos)11 SizedBit (searcher.pack.SizedBit)11 Result (searcher.pack.task.Result)11 Collectors (java.util.stream.Collectors)10 PerfectPackSearcher (searcher.pack.task.PerfectPackSearcher)9 FieldFactory (core.field.FieldFactory)8 MinoFactory (core.mino.MinoFactory)8 MinoShifter (core.mino.MinoShifter)8 Piece (core.mino.Piece)8 List (java.util.List)8 Stream (java.util.stream.Stream)8 InOutPairField (searcher.pack.InOutPairField)8 SolutionFilter (searcher.pack.memento.SolutionFilter)8 TaskResultHelper (searcher.pack.task.TaskResultHelper)8 BuildUpStream (common.buildup.BuildUpStream)7