Search in sources :

Example 51 with SizedBit

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

the class MappedBasicSolutionsFactoryTest method create2x5.

@Test
void create2x5() throws Exception {
    SizedBit sizedBit = new SizedBit(2, 5);
    int expectedSolutions = 822;
    int expectedSolutionItems = 321978;
    assertCache(sizedBit, expectedSolutions, expectedSolutionItems);
}
Also used : SizedBit(searcher.pack.SizedBit) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 52 with SizedBit

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

the class PathCore method run.

List<PathPair> run(Field field, SizedBit sizedBit, BlockField blockField) throws ExecutionException, InterruptedException {
    int maxClearLine = sizedBit.getHeight();
    List<Result> candidates = searcher.stream(resultStream -> {
        return resultStream.filter(result -> {
            LinkedList<MinoOperationWithKey> operations = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
            BlockField mergedField = new BlockField(maxClearLine);
            operations.forEach(operation -> {
                Field operationField = createField(operation, maxClearLine);
                mergedField.merge(operationField, operation.getPiece());
            });
            return mergedField.containsAll(blockField);
        }).collect(Collectors.toList());
    });
    return candidates.stream().map(result -> {
        LinkedList<MinoOperationWithKey> operations = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
        // 地形の中で組むことができるoperationsを一つ作成
        BuildUpStream buildUpStream = buildUpStreamThreadLocal.get();
        List<MinoOperationWithKey> sampleOperations = buildUpStream.existsValidBuildPatternDirectly(field, operations).findFirst().orElse(Collections.emptyList());
        // 地形の中で組むことができるものがないときはスキップ
        if (sampleOperations.isEmpty())
            return PathPair.EMPTY_PAIR;
        // 地形の中で組むことができるSetを作成
        HashSet<LongPieces> piecesSolution = buildUpStream.existsValidBuildPatternDirectly(field, operations).map(operationWithKeys -> operationWithKeys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList())).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
        // 探索シーケンスの中で組むことができるSetを作成
        HashSet<LongPieces> piecesPattern = getPiecesPattern(piecesSolution);
        // 探索シーケンスの中で組むことができるものがないときはスキップ
        if (piecesPattern.isEmpty())
            return PathPair.EMPTY_PAIR;
        // 譜面の作成
        String fumen = fumenParser.parse(sampleOperations, field, maxClearLine);
        return new PathPair(result, piecesSolution, piecesPattern, fumen, new ArrayList<>(sampleOperations), validPieces);
    }).filter(pathPair -> pathPair != PathPair.EMPTY_PAIR).collect(Collectors.toList());
}
Also used : SyntaxException(common.SyntaxException) java.util(java.util) Pieces(common.datastore.blocks.Pieces) BlockField(common.datastore.BlockField) LongPieces(common.datastore.blocks.LongPieces) PatternGenerator(common.pattern.PatternGenerator) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SizedBit(searcher.pack.SizedBit) OrderLookup(common.order.OrderLookup) Operation(common.datastore.Operation) FieldFactory(core.field.FieldFactory) StackOrder(common.order.StackOrder) BuildUpStream(common.buildup.BuildUpStream) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) OperationWithKey(common.datastore.OperationWithKey) ReverseOrderLookUp(common.order.ReverseOrderLookUp) Piece(core.mino.Piece) Result(searcher.pack.task.Result) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) Field(core.field.Field) Stream(java.util.stream.Stream) SeparableMino(searcher.pack.separable_mino.SeparableMino) FumenParser(entry.path.output.FumenParser) Mino(core.mino.Mino) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationWithKey(common.datastore.OperationWithKey) SeparableMino(searcher.pack.separable_mino.SeparableMino) Result(searcher.pack.task.Result) BlockField(common.datastore.BlockField) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) BlockField(common.datastore.BlockField) LongPieces(common.datastore.blocks.LongPieces) BuildUpStream(common.buildup.BuildUpStream)

Example 53 with SizedBit

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

the class PathEntryPoint method run.

@Override
public void run() throws FinderException {
    output("# Setup Field");
    // Setup field
    Field field = settings.getField();
    Verify.field(field);
    // Setup max clear line
    int maxClearLine = settings.getMaxClearLine();
    Verify.maxClearLineUnder10(maxClearLine);
    // Setup reserved blocks
    BlockField reservedBlocks = settings.getReservedBlock();
    if (settings.isReserved()) {
        Verify.reservedBlocks(reservedBlocks);
        for (int y = maxClearLine - 1; 0 <= y; y--) {
            StringBuilder builder = new StringBuilder();
            for (int x = 0; x < 10; x++) {
                if (reservedBlocks.getBlock(x, y) != null)
                    builder.append(reservedBlocks.getBlock(x, y).getName());
                else if (!field.isEmpty(x, y))
                    builder.append('X');
                else
                    builder.append('_');
            }
            output(builder.toString());
        }
    } else {
        output(FieldView.toString(field, maxClearLine));
    }
    // Setup max depth
    // パフェに必要なミノ数
    int maxDepth = Verify.maxDepth(field, maxClearLine);
    output();
    // ========================================
    // Output user-defined
    output("# Initialize / User-defined");
    output("Max clear lines: " + maxClearLine);
    output("Using hold: " + (settings.isUsingHold() ? "use" : "avoid"));
    output("Drop: " + settings.getDropType().name().toLowerCase());
    output("Searching patterns:");
    // Setup patterns
    List<String> patterns = settings.getPatterns();
    PatternGenerator generator = Verify.patterns(patterns, maxDepth);
    // Output patterns
    for (String pattern : patterns) output("  " + pattern);
    output();
    // ========================================
    // Setup core
    output("# Initialize / System");
    int threadCount = getThreadCount();
    // Output system-defined
    output("Version = " + FinderConstant.VERSION);
    output("Threads = " + threadCount);
    output("Need Pieces = " + maxDepth);
    output();
    // ========================================
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    ColorConverter colorConverter = new ColorConverter();
    SizedBit sizedBit = decideSizedBitSolutionWidth(maxClearLine);
    SolutionFilter solutionFilter = new ForPathSolutionFilter(generator, maxClearLine);
    // Holdができるときは必要なミノ分(maxDepth + 1)だけを取り出す。maxDepth + 1だけないときはブロックの個数をそのまま指定
    output("# Enumerate pieces");
    boolean isUsingHold = settings.isUsingHold();
    int piecesDepth = generator.getDepth();
    output("Piece pop count = " + (isUsingHold && maxDepth < piecesDepth ? maxDepth + 1 : maxDepth));
    output();
    // ========================================
    output("# Cache");
    output("  -> Stopwatch start");
    Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
    BasicSolutions basicSolutions = calculateBasicSolutions(field, minoFactory, minoShifter, sizedBit, solutionFilter);
    stopwatch1.stop();
    output("     ... done");
    output("  -> Stopwatch stop : " + stopwatch1.toMessage(TimeUnit.MILLISECONDS));
    output();
    // ========================================
    output("# Search");
    output("  -> Stopwatch start");
    output("     ... searching");
    Stopwatch stopwatch2 = Stopwatch.createStartedStopwatch();
    PathCore pathCore = createPathCore(field, maxClearLine, maxDepth, patterns, minoFactory, colorConverter, sizedBit, solutionFilter, isUsingHold, basicSolutions, threadCount);
    List<PathPair> pathPairs = run(pathCore, field, sizedBit, reservedBlocks);
    stopwatch2.stop();
    output("     ... done");
    output("  -> Stopwatch stop : " + stopwatch2.toMessage(TimeUnit.MILLISECONDS));
    output();
    // ========================================
    output("# Output file");
    OutputType outputType = settings.getOutputType();
    PathOutput pathOutput = createOutput(outputType, generator, maxDepth);
    pathOutput.output(pathPairs, field, sizedBit);
    output();
    // ========================================
    output("# Finalize");
    output("done");
}
Also used : PatternGenerator(common.pattern.PatternGenerator) Stopwatch(lib.Stopwatch) EntryPoint(entry.EntryPoint) FilterOnDemandBasicSolutions(searcher.pack.solutions.FilterOnDemandBasicSolutions) BasicSolutions(searcher.pack.calculator.BasicSolutions) BlockField(common.datastore.BlockField) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) ColumnSmallField(core.column_field.ColumnSmallField) BlockField(common.datastore.BlockField) SizedBit(searcher.pack.SizedBit) SolutionFilter(searcher.pack.memento.SolutionFilter) ColorConverter(common.tetfu.common.ColorConverter) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter)

Aggregations

SizedBit (searcher.pack.SizedBit)53 Test (org.junit.jupiter.api.Test)32 ColumnField (core.column_field.ColumnField)29 SeparableMinos (searcher.pack.SeparableMinos)22 Field (core.field.Field)18 LongTest (module.LongTest)18 MinoFactory (core.mino.MinoFactory)15 MinoShifter (core.mino.MinoShifter)15 SolutionFilter (searcher.pack.memento.SolutionFilter)15 InOutPairField (searcher.pack.InOutPairField)14 Piece (core.mino.Piece)13 Collectors (java.util.stream.Collectors)13 BasicSolutions (searcher.pack.calculator.BasicSolutions)13 Predicate (java.util.function.Predicate)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 SeparableMino (searcher.pack.separable_mino.SeparableMino)11 PerfectPackSearcher (searcher.pack.task.PerfectPackSearcher)11 Result (searcher.pack.task.Result)11 TaskResultHelper (searcher.pack.task.TaskResultHelper)10 Stream (java.util.stream.Stream)9