Search in sources :

Example 6 with MinoFieldMemento

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

the class MinoPackingTaskWidthForWidth3 method compute.

@Override
public Stream<Result> compute() {
    if (searcher.isFilled(innerField, index)) {
        // innerFieldが埋まっている
        List<InOutPairField> inOutPairFields = searcher.getInOutPairFields();
        if (index == searcher.getLastIndex()) {
            // 最後の計算
            SizedBit sizedBit = searcher.getSizedBit();
            ColumnField lastOuterField = inOutPairFields.get(index).getOuterField();
            long innerFieldBoard = lastOuterField.getBoard(0) >> sizedBit.getMaxBitDigit();
            MinoFieldMemento nextMemento = memento.skip();
            return searcher.getTaskResultHelper().fixResult(searcher, innerFieldBoard, nextMemento);
        } else {
            // 途中の計算  // 自分で計算する
            int nextIndex = index + 1;
            ColumnField nextInnerField = inOutPairFields.get(nextIndex).getInnerField();
            MinoFieldMemento nextMemento = memento.skip();
            return createTask(searcher, nextInnerField, nextMemento, nextIndex).compute();
        }
    } else {
        MinoFields minoFields = searcher.getSolutions(index).parse(innerField);
        // innerFieldが埋まっていない
        if (index == searcher.getLastIndex()) {
            // 最後の計算
            return minoFields.stream().parallel().flatMap(this::splitAndFixResult);
        } else {
            // 途中の計算
            return minoFields.stream().parallel().map(this::split).filter(this::isValidTask).flatMap(PackingTask::compute);
        }
    }
}
Also used : MinoFields(searcher.pack.mino_fields.MinoFields) InOutPairField(searcher.pack.InOutPairField) SizedBit(searcher.pack.SizedBit) ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento)

Example 7 with MinoFieldMemento

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

the class MinoPackingTaskWidthForWidth3 method splitAndFixResult.

private Stream<Result> splitAndFixResult(MinoField minoField) {
    ColumnField outerField = searcher.getInOutPairFields().get(index).getOuterField();
    ColumnField minoOuterField = minoField.getOuterField();
    // 注目範囲外outerで重なりがないか確認
    if (outerField.canMerge(minoOuterField)) {
        // 有効なおきかた
        SizedBit sizedBit = searcher.getSizedBit();
        ColumnField mergedOuterField = outerField.freeze(searcher.getSizedBit().getHeight());
        mergedOuterField.merge(minoOuterField);
        long innerFieldBoard = mergedOuterField.getBoard(0) >> sizedBit.getMaxBitDigit();
        MinoFieldMemento nextMemento = memento.concat(minoField);
        return searcher.getTaskResultHelper().fixResult(searcher, innerFieldBoard, nextMemento);
    }
    return Stream.empty();
}
Also used : SizedBit(searcher.pack.SizedBit) ColumnField(core.column_field.ColumnField) MinoFieldMemento(searcher.pack.memento.MinoFieldMemento)

Example 8 with MinoFieldMemento

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

the class PerfectPackSearcher 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(() -> {
        // Streamは終端操作を実行するまで実際には計算を行わない
        // そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
        // (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
        Stream<Result> compute = toStream(task);
        return compute.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)

Example 9 with MinoFieldMemento

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

the class PerfectPackSearcher method toList.

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

Example 10 with MinoFieldMemento

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

the class PerfectPackSearcher method stream.

public <T> T stream(Function<Stream<Result>, T> 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<T> submitTask = forkJoinPool.submit(() -> {
        // Streamは終端操作を実行するまで実際には計算を行わない
        // そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
        // (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
        Stream<Result> compute = toStream(task);
        return callback.apply(compute);
    });
    // 結果を取得するまで待つ
    T 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)

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