Search in sources :

Example 1 with SeparableMinos

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

the class ConnectionsToStreamCallable method parseWhenNext.

private Stream<RecursiveMinoField> parseWhenNext(ColumnField outerColumnField, SeparableMino currentMino, RecursiveMinoFields minoFields) {
    SeparableMinos separableMinos = calculator.getSeparableMinos();
    int index = separableMinos.toIndex(currentMino);
    return minoFields.recursiveStream().filter(minoField -> minoField.getMaxIndex() <= index).map(minoField -> {
        // outerで、最終的に使用されるブロック と すでに使っているブロックが重ならないことを確認
        ColumnField lastOuterField = minoField.getOuterField();
        if (!lastOuterField.canMerge(outerColumnField))
            return null;
        OperationWithKey currentOperations = currentMino.toMinoOperationWithKey();
        long currentDeleteKey = currentOperations.getNeedDeletedKey();
        long currentUsingKey = currentOperations.getUsingKey();
        // いま置こうとしているミノと、それまでの結果に矛盾があるか確認
        boolean isContradiction = currentDeleteKey != 0L && minoField.getOperationsStream().anyMatch(operationWithKey -> {
            long deletedKey = operationWithKey.getNeedDeletedKey();
            long usingKey = operationWithKey.getUsingKey();
            return (currentUsingKey & deletedKey) != 0L && (usingKey & currentDeleteKey) != 0L;
        });
        // 矛盾があるときはスキップ
        if (isContradiction)
            return null;
        // 使用されるブロックを算出
        ColumnField usingBlock = lastOuterField.freeze(calculator.getHeight());
        usingBlock.merge(outerColumnField);
        return new RecursiveMinoField(currentMino, minoField, usingBlock, separableMinos);
    }).filter(Objects::nonNull);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields) ColumnField(core.column_field.ColumnField) Callable(java.util.concurrent.Callable) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects) List(java.util.List) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) SeparableMinos(searcher.pack.SeparableMinos) ColumnFieldConnection(searcher.pack.connections.ColumnFieldConnection) ColumnFieldConnections(searcher.pack.connections.ColumnFieldConnections) OperationWithKey(common.datastore.OperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects)

Example 2 with SeparableMinos

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

the class ConnectionsToStreamCallable method over.

private Stream<RecursiveMinoField> over(ColumnField columnField, ColumnField outerColumnField, List<SeparableMino> minos, SeparableMinos separableMinos, RecursiveMinoField result) {
    Stream.Builder<RecursiveMinoField> builder = Stream.builder();
    builder.accept(result);
    StreamColumnFieldConnections connections = new StreamColumnFieldConnections(minos, columnField, calculator.getSizedBit());
    Stream<RecursiveMinoField> stream = connections.getConnectionStream().filter(connection -> {
        ColumnField nextOuterField = connection.getOuterField();
        SeparableMino mino = connection.getMino();
        return // 次に埋めるべき場所を埋めてある
        !needFilledField.canMerge(mino.getField()) && // 次のフィールドの制限範囲と重なっていない
        nextOuterField.canMerge(limitOuterField) && // 次のフィールドにあるブロックと重なっていない
        nextOuterField.canMerge(outerColumnField);
    }).flatMap(connection -> {
        // 使用されるブロックを算出
        ColumnField usingBlock = connection.getOuterField().freeze(calculator.getHeight());
        usingBlock.merge(outerColumnField);
        RecursiveMinoField t = new RecursiveMinoField(connection.getMino(), result, usingBlock, separableMinos);
        List<SeparableMino> allMinos = separableMinos.getMinos();
        List<SeparableMino> minos2 = allMinos.subList(separableMinos.toIndex(connection.getMino()) + 1, allMinos.size());
        return over(connection.getInnerField(), usingBlock, minos2, separableMinos, t);
    });
    return Stream.concat(Stream.of(result), stream);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields) ColumnField(core.column_field.ColumnField) Callable(java.util.concurrent.Callable) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Objects(java.util.Objects) List(java.util.List) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) SeparableMinos(searcher.pack.SeparableMinos) ColumnFieldConnection(searcher.pack.connections.ColumnFieldConnection) ColumnFieldConnections(searcher.pack.connections.ColumnFieldConnections) SeparableMino(searcher.pack.separable_mino.SeparableMino) ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Stream(java.util.stream.Stream) StreamColumnFieldConnections(searcher.pack.connections.StreamColumnFieldConnections)

Example 3 with SeparableMinos

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

the class ConnectionsToStreamCallable method parseWhenFilled.

private Stream<RecursiveMinoField> parseWhenFilled(ColumnField columnField, ColumnField outerColumnField, Field wallField, SeparableMino currentMino) {
    // これからブロックをおく場所以外を、すでにブロックで埋めたフィールドを作成
    Field freeze = wallField.freeze(calculator.getHeight());
    Field invertedOuterField = calculator.parseInvertedOuterField(outerColumnField);
    freeze.merge(invertedOuterField);
    // 置くブロック以外がすでに埋まっていると仮定したとき、正しく接着できる順があるか確認
    SeparableMinos separableMinos = calculator.getSeparableMinos();
    RecursiveMinoField result = new RecursiveMinoField(currentMino, outerColumnField.freeze(calculator.getHeight()), separableMinos);
    // 次に埋めるべき場所がないときは結果をそのまま返す
    if (needFilledField.isPerfect()) {
        return Stream.of(result);
    }
    // 現在のフィールドに埋めるべきところがないが、次以降のフィールドに残っている場合をケアする
    List<SeparableMino> allMinos = separableMinos.getMinos();
    return over(columnField, outerColumnField, allMinos, separableMinos, result);
}
Also used : ColumnField(core.column_field.ColumnField) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField) Field(core.field.Field) SeparableMinos(searcher.pack.SeparableMinos) SeparableMino(searcher.pack.separable_mino.SeparableMino) RecursiveMinoField(searcher.pack.mino_field.RecursiveMinoField)

Example 4 with SeparableMinos

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

the class BuildUpStreamTest method randomLong.

@Test
@LongTest
void randomLong() 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 < 10; count++) {
        // Create field
        int numOfMinos = randoms.nextIntClosed(7, 9);
        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) LongTest(module.LongTest)

Example 5 with SeparableMinos

use of searcher.pack.SeparableMinos 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)

Aggregations

ColumnField (core.column_field.ColumnField)33 SeparableMinos (searcher.pack.SeparableMinos)33 SizedBit (searcher.pack.SizedBit)21 Test (org.junit.jupiter.api.Test)18 MinoFactory (core.mino.MinoFactory)13 MinoShifter (core.mino.MinoShifter)13 Field (core.field.Field)12 Predicate (java.util.function.Predicate)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 RecursiveMinoFields (searcher.pack.mino_fields.RecursiveMinoFields)12 Stream (java.util.stream.Stream)10 SeparableMino (searcher.pack.separable_mino.SeparableMino)10 OnDemandBasicSolutions (searcher.pack.solutions.OnDemandBasicSolutions)10 InOutPairField (searcher.pack.InOutPairField)9 BasicSolutions (searcher.pack.calculator.BasicSolutions)9 SolutionFilter (searcher.pack.memento.SolutionFilter)9 Piece (core.mino.Piece)7 Result (searcher.pack.task.Result)7 TaskResultHelper (searcher.pack.task.TaskResultHelper)7 LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)6