Search in sources :

Example 1 with OperationWithKey

use of common.datastore.OperationWithKey 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 2 with OperationWithKey

use of common.datastore.OperationWithKey in project solution-finder by knewjade.

the class BuildUpStreamTest method buildUp1.

@Test
void buildUp1() {
    // Create LockedReachable
    int height = 4;
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
    // Create OperationWithKey List
    Field field = FieldFactory.createField("" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX" + "____XXXXXX");
    Operations operations = OperationInterpreter.parseToOperations("J,0,1,0;S,L,1,2;O,0,2,1;J,2,2,1");
    List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, height);
    // Create Pieces
    Set<String> valid = new BuildUpStream(reachable, height).existsValidBuildPattern(field, operationWithKeys).map(op -> op.stream().map(OperationWithKey::getPiece).map(Piece::name).collect(Collectors.joining())).collect(Collectors.toSet());
    // Assertion
    assertThat(valid).hasSize(2).contains("JSOJ", "JOSJ");
}
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) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationWithKey(common.datastore.OperationWithKey) MinoRotation(core.srs.MinoRotation) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) LockedReachable(core.action.reachable.LockedReachable) Operations(common.datastore.Operations) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 3 with OperationWithKey

use of common.datastore.OperationWithKey in project solution-finder by knewjade.

the class OperationWithKeyComparatorTest method compareDiffX.

@Test
void compareDiffX() throws Exception {
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    int x = randoms.nextInt(10);
    int y = randoms.nextInt(20);
    long deleteKey = 0L;
    long usingKey = 1049600L;
    OperationWithKey operationWithKey1 = new FullOperationWithKey(minoFactory.create(Piece.I, Rotate.Spawn), x, y, deleteKey, usingKey);
    int newX = randoms.nextInt(10);
    if (newX == x)
        newX += 1;
    Mino newMino = new MinoFactory().create(Piece.I, Rotate.Spawn);
    OperationWithKey operationWithKey2 = createNewOperationWithKey(newMino, newX, y, deleteKey, usingKey);
    // assert is not 0 & sign reversed
    OperationWithKeyComparator comparator = new OperationWithKeyComparator();
    assertThat(comparator.compare(operationWithKey1, operationWithKey2) * comparator.compare(operationWithKey2, operationWithKey1)).as(operationWithKey2.toString()).isLessThan(0);
}
Also used : Randoms(lib.Randoms) OperationWithKey(common.datastore.OperationWithKey) FullOperationWithKey(common.datastore.FullOperationWithKey) Mino(core.mino.Mino) MinoFactory(core.mino.MinoFactory) FullOperationWithKey(common.datastore.FullOperationWithKey) Test(org.junit.jupiter.api.Test)

Example 4 with OperationWithKey

use of common.datastore.OperationWithKey in project solution-finder by knewjade.

the class UseCSVPathOutput method output.

@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
    this.lastException = null;
    outputLog("Found path = " + pathPairs.size());
    AtomicInteger allCounter = new AtomicInteger();
    Map<PieceCounter, List<PathPair>> groupingByClockCounter = pathPairs.parallelStream().collect(Collectors.groupingBy(pathPair -> {
        List<OperationWithKey> operations = pathPair.getSampleOperations();
        return new PieceCounter(operations.stream().map(OperationWithKey::getPiece));
    }));
    List<PathPair> emptyValidList = Collections.emptyList();
    try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
        writer.writeAndNewLine("使用ミノ,対応地形数,対応ツモ数 (対パターン),テト譜,ツモ (対パターン)");
        generator.blockCountersStream().parallel().map(blockCounter -> {
            // カウンターをインクリメント
            allCounter.incrementAndGet();
            // 組み合わせ名を取得
            String blockCounterName = blockCounter.getBlockStream().map(Piece::getName).collect(Collectors.joining());
            // パフェ可能な地形を抽出
            List<PathPair> valid = groupingByClockCounter.getOrDefault(blockCounter, emptyValidList);
            // パフェ可能な地形数
            int possibleSize = valid.size();
            // パフェ可能な地形のテト譜を連結
            String fumens = valid.stream().sorted(Comparator.comparing(PathPair::getPatternSize).reversed()).map(pathPair -> "v115@" + pathPair.getFumen()).collect(Collectors.joining(";"));
            // 対応できるパターンを重複なく抽出
            Set<LongPieces> possiblePatternSet = valid.stream().flatMap(PathPair::blocksStreamForPattern).collect(Collectors.toSet());
            // 対応できるパターン数
            int possiblePatternSize = possiblePatternSet.size();
            // パターンを連結
            String patterns = possiblePatternSet.stream().map(LongPieces::getPieces).map(blocks -> blocks.stream().map(Piece::getName).collect(Collectors.joining(""))).collect(Collectors.joining(";"));
            return String.format("%s,%d,%d,%s,%s", blockCounterName, possibleSize, possiblePatternSize, fumens, patterns);
        }).forEach(writer::writeAndNewLine);
        writer.flush();
    } catch (IOException e) {
        throw new FinderExecuteException("Failed to output file", e);
    }
    outputLog("Found piece combinations = " + allCounter.get());
    if (lastException != null)
        throw new FinderExecuteException("Error to output file", lastException);
}
Also used : OperationWithKey(common.datastore.OperationWithKey) Piece(core.mino.Piece) java.util(java.util) LongPieces(common.datastore.blocks.LongPieces) PatternGenerator(common.pattern.PatternGenerator) ReducePatternGenerator(entry.path.ReducePatternGenerator) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) Field(core.field.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) OperationWithKey(common.datastore.OperationWithKey) PieceCounter(common.datastore.PieceCounter) IOException(java.io.IOException) PathPair(entry.path.PathPair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) Piece(core.mino.Piece) FinderExecuteException(exceptions.FinderExecuteException)

Example 5 with OperationWithKey

use of common.datastore.OperationWithKey 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)

Aggregations

OperationWithKey (common.datastore.OperationWithKey)16 MinoFactory (core.mino.MinoFactory)9 Field (core.field.Field)8 Mino (core.mino.Mino)8 Randoms (lib.Randoms)8 Test (org.junit.jupiter.api.Test)8 FullOperationWithKey (common.datastore.FullOperationWithKey)7 Piece (core.mino.Piece)7 MinoOperationWithKey (common.datastore.MinoOperationWithKey)6 Collectors (java.util.stream.Collectors)6 SizedBit (searcher.pack.SizedBit)6 SeparableMino (searcher.pack.separable_mino.SeparableMino)6 FieldFactory (core.field.FieldFactory)5 ExecutionException (java.util.concurrent.ExecutionException)5 PerfectPackSearcher (searcher.pack.task.PerfectPackSearcher)5 Result (searcher.pack.task.Result)5 LongPieces (common.datastore.blocks.LongPieces)4 ColumnField (core.column_field.ColumnField)4 List (java.util.List)4 Stream (java.util.stream.Stream)4