Search in sources :

Example 16 with ColumnField

use of core.column_field.ColumnField in project solution-finder by knewjade.

the class SetupPackSearcher method forEach.

public void forEach(Consumer<Result> callback) throws InterruptedException, ExecutionException {
    // 探索準備
    MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
    ColumnField innerField = inOutPairFields.get(0).getInnerField();
    PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
    // 探索
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    ForkJoinTask<Boolean> submitTask = forkJoinPool.submit(() -> {
        // Streamは終端操作を実行するまで実際には計算を行わない
        // そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
        // (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
        task.compute().parallel().forEach(callback);
        // 計算を最後まで行ったことを伝えるため true を返却
        return true;
    });
    // 結果を取得するまで待つ
    Boolean result = submitTask.get();
    assert result;
    // 終了処理
    forkJoinPool.shutdown();
}
Also used : ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 17 with ColumnField

use of core.column_field.ColumnField 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 18 with ColumnField

use of core.column_field.ColumnField 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 19 with ColumnField

use of core.column_field.ColumnField 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 20 with ColumnField

use of core.column_field.ColumnField in project solution-finder by knewjade.

the class FullOperationSeparableMino method create.

public static SeparableMino create(FullOperationWithKey operationWithKey, int upperY, int fieldHeight) {
    assert upperY <= 10 : upperY;
    Mino mino = operationWithKey.getMino();
    long deleteKey = operationWithKey.getNeedDeletedKey();
    int y = operationWithKey.getY();
    MinoMask minoMask = MinoMaskFactory.create(fieldHeight, mino, y, deleteKey);
    int x = operationWithKey.getX();
    Field mask = minoMask.getMinoMask(x);
    int lowerY = operationWithKey.getY() + operationWithKey.getMino().getMinY();
    ColumnSmallField columnField = ColumnFieldFactory.createField();
    for (int ny = lowerY; ny <= upperY; ny++) {
        for (int nx = x + mino.getMinX(); nx <= x + mino.getMaxX(); nx++) {
            if (!mask.isEmpty(nx, ny))
                columnField.setBlock(nx, ny, fieldHeight);
        }
    }
    Field field = FieldFactory.createField(fieldHeight);
    field.put(operationWithKey.getMino(), operationWithKey.getX(), operationWithKey.getY());
    field.insertWhiteLineWithKey(operationWithKey.getNeedDeletedKey());
    return new FullOperationSeparableMino(operationWithKey, columnField, field);
}
Also used : Field(core.field.Field) ColumnField(core.column_field.ColumnField) ColumnSmallField(core.column_field.ColumnSmallField) ColumnSmallField(core.column_field.ColumnSmallField) MinoMask(searcher.pack.separable_mino.mask.MinoMask) Mino(core.mino.Mino)

Aggregations

ColumnField (core.column_field.ColumnField)62 SeparableMinos (searcher.pack.SeparableMinos)33 SizedBit (searcher.pack.SizedBit)26 Field (core.field.Field)19 Test (org.junit.jupiter.api.Test)19 MinoFieldMemento (searcher.pack.memento.MinoFieldMemento)17 MinoFactory (core.mino.MinoFactory)14 MinoShifter (core.mino.MinoShifter)14 ForkJoinPool (java.util.concurrent.ForkJoinPool)12 Predicate (java.util.function.Predicate)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 RecursiveMinoFields (searcher.pack.mino_fields.RecursiveMinoFields)12 SeparableMino (searcher.pack.separable_mino.SeparableMino)12 Stream (java.util.stream.Stream)11 ColumnSmallField (core.column_field.ColumnSmallField)10 InOutPairField (searcher.pack.InOutPairField)10 OnDemandBasicSolutions (searcher.pack.solutions.OnDemandBasicSolutions)10 BasicSolutions (searcher.pack.calculator.BasicSolutions)9 SolutionFilter (searcher.pack.memento.SolutionFilter)9 Piece (core.mino.Piece)7