Search in sources :

Example 16 with MinoFieldMemento

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

the class BasicMinoPackingHelper method fixResult.

@Override
public Stream<Result> fixResult(PackSearcher searcher, long innerFieldBoard, MinoFieldMemento nextMemento) {
    SizedBit sizedBit = searcher.getSizedBit();
    SolutionFilter solutionFilter = searcher.getSolutionFilter();
    long fillBoard = sizedBit.getFillBoard();
    long board = innerFieldBoard & fillBoard;
    int resultIndex = searcher.getLastIndex() + 1;
    ColumnSmallField nextInnerField = ColumnFieldFactory.createField(board);
    if (searcher.isFilled(nextInnerField, resultIndex)) {
        if (solutionFilter.testLast(nextMemento))
            return Stream.of(createResult(nextMemento));
        return Stream.empty();
    } else {
        ColumnSmallField over = ColumnFieldFactory.createField(innerFieldBoard & ~fillBoard);
        MinoFields minoFields = searcher.getSolutions(resultIndex).parse(nextInnerField);
        return minoFields.stream().filter(minoField -> over.canMerge(minoField.getOuterField())).map(nextMemento::concat).filter(solutionFilter::testLast).map(this::createResult);
    }
}
Also used : SolutionFilter(searcher.pack.memento.SolutionFilter) SizedBit(searcher.pack.SizedBit) Stream(java.util.stream.Stream) ColumnFieldFactory(core.column_field.ColumnFieldFactory) ColumnSmallField(core.column_field.ColumnSmallField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) MinoFields(searcher.pack.mino_fields.MinoFields) ColumnSmallField(core.column_field.ColumnSmallField) MinoFields(searcher.pack.mino_fields.MinoFields) SizedBit(searcher.pack.SizedBit) SolutionFilter(searcher.pack.memento.SolutionFilter)

Example 17 with MinoFieldMemento

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

the class PerfectPackSearcher method collect.

public <A, R> R collect(Collector<? super Result, A, R> 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<R> submitTask = forkJoinPool.submit(() -> {
        // Streamは終端操作を実行するまで実際には計算を行わない
        // そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
        // (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
        Stream<Result> compute = toStream(task);
        return compute.collect(callback);
    });
    // 結果を取得するまで待つ
    R result = submitTask.get();
    assert result != null;
    // 終了処理
    forkJoinPool.shutdown();
    return result;
}
Also used : ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 18 with MinoFieldMemento

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

the class SetupPackSearcher method count.

public Long count() throws InterruptedException, ExecutionException {
    // 探索準備
    MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
    ColumnField innerField = inOutPairFields.get(0).getInnerField();
    PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
    // 探索
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    ForkJoinTask<Long> submitTask = forkJoinPool.submit(() -> {
        return task.compute().parallel().count();
    });
    // 結果を取得するまで待つ
    Long result = submitTask.get();
    assert result != null;
    // 終了処理
    forkJoinPool.shutdown();
    return result;
}
Also used : ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 19 with MinoFieldMemento

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

the class SetupPackSearcher method collect.

public <A, R> R collect(Collector<? super Result, A, R> 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<R> submitTask = forkJoinPool.submit(() -> {
        return task.compute().parallel().collect(callback);
    });
    // 結果を取得するまで待つ
    R result = submitTask.get();
    assert result != null;
    // 終了処理
    forkJoinPool.shutdown();
    return result;
}
Also used : ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Example 20 with MinoFieldMemento

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

the class SetupPackSearcher method findAny.

public Optional<Result> findAny() throws InterruptedException, ExecutionException {
    // 探索準備
    MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
    ColumnField innerField = inOutPairFields.get(0).getInnerField();
    PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
    // 探索
    ForkJoinPool forkJoinPool = new ForkJoinPool();
    ForkJoinTask<Optional<Result>> submitTask = forkJoinPool.submit(() -> {
        return task.compute().parallel().findAny();
    });
    // 結果を取得するまで待つ
    Optional<Result> result = submitTask.get();
    assert result != null;
    // 終了処理
    forkJoinPool.shutdown();
    return result;
}
Also used : Optional(java.util.Optional) ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento) ForkJoinPool(java.util.concurrent.ForkJoinPool)

Aggregations

MinoFieldMemento (searcher.pack.memento.MinoFieldMemento)20 ColumnField (core.column_field.ColumnField)17 ForkJoinPool (java.util.concurrent.ForkJoinPool)12 SizedBit (searcher.pack.SizedBit)8 MinoFields (searcher.pack.mino_fields.MinoFields)3 ColumnSmallField (core.column_field.ColumnSmallField)2 List (java.util.List)2 Optional (java.util.Optional)2 SolutionFilter (searcher.pack.memento.SolutionFilter)2 ColumnFieldFactory (core.column_field.ColumnFieldFactory)1 Stream (java.util.stream.Stream)1 InOutPairField (searcher.pack.InOutPairField)1