Search in sources :

Example 1 with Result

use of searcher.pack.task.Result 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 2 with Result

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

Example 3 with Result

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

the class TetfuTest method random.

@Test
@LongTest
void random() throws Exception {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    ColorConverter colorConverter = new ColorConverter();
    // 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);
    for (int count = 0; count < 20; count++) {
        System.out.println(count);
        // Create field
        int numOfMinos = randoms.nextIntClosed(6, 10);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = new SRSValidSolutionFilter(field, lockedReachableThreadLocal, sizedBit);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        BuildUpStream buildUpStream = new BuildUpStream(lockedReachableThreadLocal.get(), height);
        // If found solution
        resultOptional.ifPresent(result -> {
            List<MinoOperationWithKey> list = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
            Optional<List<MinoOperationWithKey>> validOption = buildUpStream.existsValidBuildPattern(field, list).findAny();
            validOption.ifPresent(operationWithKeys -> {
                Operations operations = OperationTransform.parseToOperations(field, operationWithKeys, height);
                List<TetfuElement> elements = operations.getOperations().stream().map(operation -> {
                    ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
                    Rotate rotate = operation.getRotate();
                    int x = operation.getX();
                    int y = operation.getY();
                    String comment = randoms.string() + randoms.string() + randoms.string();
                    return new TetfuElement(colorType, rotate, x, y, comment);
                }).collect(Collectors.toList());
                String encode = new Tetfu(minoFactory, colorConverter).encode(elements);
                List<TetfuPage> decode = decodeTetfu(minoFactory, colorConverter, encode);
                assertThat(decode).hasSize(elements.size());
                for (int index = 0; index < decode.size(); index++) {
                    TetfuElement element = elements.get(index);
                    assertThat(decode.get(index)).returns(element.getColorType(), TetfuPage::getColorType).returns(element.getRotate(), TetfuPage::getRotate).returns(element.getX(), TetfuPage::getX).returns(element.getY(), TetfuPage::getY).returns(element.getComment(), TetfuPage::getComment);
                }
            });
        });
    }
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) FinderParseException(exceptions.FinderParseException) SizedBit(searcher.pack.SizedBit) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) Tag(org.junit.jupiter.api.Tag) MinoRotation(core.srs.MinoRotation) BuildUpStream(common.buildup.BuildUpStream) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) L(core.mino.Piece.L) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) J(core.mino.Piece.J) InOutPairField(searcher.pack.InOutPairField) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) 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) Collections(java.util.Collections) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Rotate(core.srs.Rotate) 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) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) ColorConverter(common.tetfu.common.ColorConverter) ColorType(common.tetfu.common.ColorType) MinoFactory(core.mino.MinoFactory) List(java.util.List) BuildUpStream(common.buildup.BuildUpStream) Operations(common.datastore.Operations) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) SizedBit(searcher.pack.SizedBit) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 4 with Result

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

the class PathCore method run.

List<PathPair> run(Field field, SizedBit sizedBit) throws ExecutionException, InterruptedException {
    List<Result> candidates = searcher.toList();
    int maxClearLine = sizedBit.getHeight();
    return candidates.parallelStream().map(result -> {
        LinkedList<MinoOperationWithKey> operations = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
        // 地形の中で組むことができるoperationsをすべてリスト化する
        BuildUpStream buildUpStream2 = buildUpStreamThreadLocal.get();
        List<List<MinoOperationWithKey>> validOperaions = buildUpStream2.existsValidBuildPatternDirectly(field, operations).collect(Collectors.toList());
        // 地形の中で組むことができるものがないときはスキップ
        if (validOperaions.isEmpty())
            return PathPair.EMPTY_PAIR;
        // 地形の中で組むことができるSetを作成
        HashSet<LongPieces> piecesSolution = validOperaions.stream().map(operationWithKeys -> operationWithKeys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
        // 探索シーケンスの中で組むことができるSetを作成
        HashSet<LongPieces> piecesPattern = getPiecesPattern(piecesSolution);
        // 探索シーケンスの中で組むことができるものがないときはスキップ
        if (piecesPattern.isEmpty())
            return PathPair.EMPTY_PAIR;
        // 探索シーケンスの中でテト譜にするoperationsを選択する
        List<MinoOperationWithKey> operationsToUrl = validOperaions.stream().filter(o -> validPieces.contains(new LongPieces(o.stream().map(Operation::getPiece)))).findFirst().orElse(Collections.emptyList());
        // 譜面の作成
        String fumen = fumenParser.parse(operationsToUrl, field, maxClearLine);
        return new PathPair(result, piecesSolution, piecesPattern, fumen, new ArrayList<>(operationsToUrl), 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) Result(searcher.pack.task.Result) MinoOperationWithKey(common.datastore.MinoOperationWithKey) LongPieces(common.datastore.blocks.LongPieces) BuildUpStream(common.buildup.BuildUpStream)

Example 5 with Result

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

the class SetupEntryPoint method run.

@Override
public void run() throws FinderException {
    output("# Setup Field");
    // Setup init field
    Field initField = settings.getInitField();
    Verify.field(initField);
    // Setup need filled field
    Field needFilledField = settings.getNeedFilledField();
    Verify.needFilledField(needFilledField);
    // Setup not filled field
    Field notFilledField = settings.getNotFilledField();
    // Setup max height
    int maxHeight = settings.getMaxHeight();
    Verify.maxClearLineUnder10(maxHeight);
    // Setup reserved blocks
    BlockField reservedBlocks = settings.getReservedBlock();
    if (settings.isReserved()) {
        Verify.reservedBlocks(reservedBlocks);
        for (int y = maxHeight - 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 (!initField.isEmpty(x, y))
                    builder.append('X');
                else if (!needFilledField.isEmpty(x, y))
                    builder.append('*');
                else if (!notFilledField.isEmpty(x, y))
                    builder.append('_');
                else
                    builder.append('.');
            }
            output(builder.toString());
        }
    } else {
        for (int y = maxHeight - 1; 0 <= y; y--) {
            StringBuilder builder = new StringBuilder();
            for (int x = 0; x < 10; x++) {
                if (!initField.isEmpty(x, y))
                    builder.append('X');
                else if (!needFilledField.isEmpty(x, y))
                    builder.append('*');
                else if (!notFilledField.isEmpty(x, y))
                    builder.append('_');
                else
                    builder.append('.');
            }
            output(builder.toString());
        }
    }
    // Setup min depth
    // 最低でも必要なミノ数
    int minDepth = Verify.minDepth(needFilledField);
    output();
    // ========================================
    // Output user-defined
    DropType dropType = settings.getDropType();
    output("# Initialize / User-defined");
    output("Max height: " + maxHeight);
    output("Drop: " + dropType.name().toLowerCase());
    output("Searching patterns:");
    // Setup patterns
    List<String> patterns = settings.getPatterns();
    PatternGenerator generator = Verify.patterns(patterns, minDepth);
    // Output patterns
    for (String pattern : patterns) output("  " + pattern);
    // Setup output file
    MyFile base = new MyFile(settings.getOutputBaseFilePath());
    base.mkdirs();
    base.verify();
    output();
    // ========================================
    // Setup core
    output("# Initialize / System");
    int core = Runtime.getRuntime().availableProcessors();
    // Output system-defined
    output("Version = " + FinderConstant.VERSION);
    output("Available processors = " + core);
    output("Need Pieces = " + minDepth);
    output();
    // ========================================
    output("# Search");
    output("  -> Stopwatch start");
    Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    ColorConverter colorConverter = new ColorConverter();
    SizedBit sizedBit = decideSizedBitSolutionWidth(maxHeight);
    TaskResultHelper taskResultHelper = new BasicMinoPackingHelper();
    SolutionFilter solutionFilter = new ForPathSolutionFilter(generator, maxHeight);
    ThreadLocal<BuildUpStream> buildUpStreamThreadLocal = createBuildUpStreamThreadLocal(dropType, maxHeight);
    OneFumenParser fumenParser = new OneFumenParser(minoFactory, colorConverter);
    // ミノリストの作成
    long deleteKeyMask = getDeleteKeyMask(notFilledField, maxHeight);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit, deleteKeyMask);
    // 絶対に置かないブロック
    List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, notFilledField);
    // 絶対に置く必要があるブロック
    ArrayList<BasicSolutions> basicSolutions = new ArrayList<>();
    List<ColumnField> needFillFields = InOutPairField.createInnerFields(sizedBit, needFilledField);
    {
        Field freeze = needFilledField.freeze(sizedBit.getHeight());
        for (ColumnField innerField : needFillFields) {
            // 最小限の部分だけを抽出する
            Field freeze1 = freeze.freeze(sizedBit.getHeight());
            for (int y = 0; y < sizedBit.getHeight(); y++) {
                int width = sizedBit.getWidth();
                for (int x = 0; x < width; x++) freeze1.removeBlock(x, y);
                for (int x = width + 3; x < 10; x++) freeze1.removeBlock(x, y);
            }
            OnDemandBasicSolutions solutions = new OnDemandBasicSolutions(separableMinos, sizedBit, innerField.getBoard(0), freeze1);
            basicSolutions.add(solutions);
            freeze.slideLeft(sizedBit.getWidth());
        }
    }
    // 探索
    SetupPackSearcher searcher = new SetupPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper, needFillFields);
    List<Result> results = getResults(initField, sizedBit, buildUpStreamThreadLocal, searcher);
    output("     Found solution = " + results.size());
    stopwatch.stop();
    output("  -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
    output();
    // ========================================
    output("# Output file");
    HTMLBuilder<FieldHTMLColumn> htmlBuilder = new HTMLBuilder<>("Setup result");
    results.parallelStream().forEach(result -> {
        List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
        Field allField = initField.freeze(maxHeight);
        Operations operations = OperationTransform.parseToOperations(allField, operationWithKeys, sizedBit.getHeight());
        List<? extends Operation> operationList = operations.getOperations();
        for (Operation operation : operationList) {
            Mino mino = minoFactory.create(operation.getPiece(), operation.getRotate());
            int x = operation.getX();
            int y = operation.getY();
            allField.put(mino, x, y);
        }
        // 譜面の作成
        String encode = fumenParser.parse(operationWithKeys, initField, maxHeight);
        String name = operationWithKeys.stream().map(OperationWithKey::getPiece).map(Piece::getName).collect(Collectors.joining());
        String link = String.format("<a href='http://fumen.zui.jp/?v115@%s' target='_blank'>%s</a>", encode, name);
        String line = String.format("<div>%s</div>", link);
        htmlBuilder.addColumn(new FieldHTMLColumn(allField, maxHeight), line);
    });
    ArrayList<FieldHTMLColumn> columns = new ArrayList<>(htmlBuilder.getRegisteredColumns());
    columns.sort(Comparator.comparing(FieldHTMLColumn::getTitle).reversed());
    try (BufferedWriter bufferedWriter = base.newBufferedWriter()) {
        for (String line : htmlBuilder.toList(columns, true)) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
    } catch (IOException e) {
        throw new FinderExecuteException(e);
    }
    output();
    // ========================================
    output("# Finalize");
    output("done");
}
Also used : OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) MyFile(entry.path.output.MyFile) Stopwatch(lib.Stopwatch) ForPathSolutionFilter(entry.path.ForPathSolutionFilter) TaskResultHelper(searcher.pack.task.TaskResultHelper) Result(searcher.pack.task.Result) BufferedWriter(java.io.BufferedWriter) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) ColorConverter(common.tetfu.common.ColorConverter) SeparableMino(searcher.pack.separable_mino.SeparableMino) Mino(core.mino.Mino) MinoFactory(core.mino.MinoFactory) BuildUpStream(common.buildup.BuildUpStream) PatternGenerator(common.pattern.PatternGenerator) OneFumenParser(entry.path.output.OneFumenParser) HTMLBuilder(output.HTMLBuilder) ColumnField(core.column_field.ColumnField) BasicMinoPackingHelper(searcher.pack.task.BasicMinoPackingHelper) IOException(java.io.IOException) EntryPoint(entry.EntryPoint) BasicSolutions(searcher.pack.calculator.BasicSolutions) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) SetupPackSearcher(searcher.pack.task.SetupPackSearcher) SizedBit(searcher.pack.SizedBit) ForPathSolutionFilter(entry.path.ForPathSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) MinoShifter(core.mino.MinoShifter) DropType(entry.DropType) FinderExecuteException(exceptions.FinderExecuteException)

Aggregations

Field (core.field.Field)11 SizedBit (searcher.pack.SizedBit)11 SeparableMino (searcher.pack.separable_mino.SeparableMino)11 Result (searcher.pack.task.Result)11 Collectors (java.util.stream.Collectors)10 PerfectPackSearcher (searcher.pack.task.PerfectPackSearcher)9 ColumnField (core.column_field.ColumnField)8 FieldFactory (core.field.FieldFactory)8 MinoFactory (core.mino.MinoFactory)8 MinoShifter (core.mino.MinoShifter)8 Piece (core.mino.Piece)8 InOutPairField (searcher.pack.InOutPairField)8 SeparableMinos (searcher.pack.SeparableMinos)8 SolutionFilter (searcher.pack.memento.SolutionFilter)8 TaskResultHelper (searcher.pack.task.TaskResultHelper)8 BuildUpStream (common.buildup.BuildUpStream)7 MinoOperationWithKey (common.datastore.MinoOperationWithKey)7 LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)7 ExecutionException (java.util.concurrent.ExecutionException)7 OnDemandBasicSolutions (searcher.pack.solutions.OnDemandBasicSolutions)7