Search in sources :

Example 26 with LongPieces

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

the class OrderLookupTest method forwardJustBlocksRandom.

@Test
void forwardJustBlocksRandom() throws Exception {
    Randoms randoms = new Randoms();
    for (int size = 2; size <= 13; size++) {
        List<Piece> pieceList = randoms.blocks(size);
        int toDepth = pieceList.size();
        PiecesNumberComparator comparator = new PiecesNumberComparator();
        List<LongPieces> forward1 = OrderLookup.forwardBlocks(pieceList, toDepth).stream().map(StackOrder::toList).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
        ForwardOrderLookUp lookUp = new ForwardOrderLookUp(toDepth, pieceList.size());
        List<LongPieces> forward2 = lookUp.parse(pieceList).map(blockStream -> blockStream.collect(Collectors.toList())).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
        assertThat(forward2).isEqualTo(forward1);
    }
}
Also used : Test(org.junit.jupiter.api.Test) Piece(core.mino.Piece) Randoms(lib.Randoms) Arrays(java.util.Arrays) List(java.util.List) ListComparator(lib.ListComparator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LongPieces(common.datastore.blocks.LongPieces) PiecesNumberComparator(common.comparator.PiecesNumberComparator) Comparator(java.util.Comparator) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Randoms(lib.Randoms) Piece(core.mino.Piece) LongPieces(common.datastore.blocks.LongPieces) PiecesNumberComparator(common.comparator.PiecesNumberComparator) Test(org.junit.jupiter.api.Test)

Example 27 with LongPieces

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

the class FumenCSVPathOutput method output.

@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
    this.lastException = null;
    outputLog("Found path = " + pathPairs.size());
    try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
        writer.writeAndNewLine("テト譜,使用ミノ,対応ツモ数 (対地形),対応ツモ数 (対パターン),ツモ (対地形),ツモ (対パターン)");
        pathPairs.parallelStream().map(pathPair -> {
            // テト譜
            String encode = pathPair.getFumen();
            // パターンに対する有効なミノ順をまとめる
            List<LongPieces> patternBuildBlocks = pathPair.blocksStreamForPattern().collect(Collectors.toList());
            // 対応ツモ数 (対パターン)
            int pattern = patternBuildBlocks.size();
            // 使用ミノをまとめる
            String usingPieces = pathPair.getUsingBlockName();
            // パターンに対する有効なミノ順をまとめる
            String validOrdersPattern = patternBuildBlocks.stream().map(longBlocks -> longBlocks.blockStream().map(Piece::getName).collect(Collectors.joining())).collect(Collectors.joining(";"));
            // 地形に対する有効なミノ順をまとめる
            HashSet<LongPieces> solutionBuildBlocks = pathPair.blocksHashSetForSolution();
            String validOrdersSolution = solutionBuildBlocks.stream().map(longBlocks -> longBlocks.blockStream().map(Piece::getName).collect(Collectors.joining())).collect(Collectors.joining(";"));
            // 対応ツモ数 (対地形)
            int solution = solutionBuildBlocks.size();
            return String.format("http://fumen.zui.jp/?v115@%s,%s,%d,%d,%s,%s", encode, usingPieces, solution, pattern, validOrdersSolution, validOrdersPattern);
        }).forEach(writer::writeAndNewLine);
        writer.flush();
    } catch (IOException e) {
        throw new FinderExecuteException("Failed to output file", e);
    }
    if (lastException != null)
        throw new FinderExecuteException("Error to output file", lastException);
}
Also used : Piece(core.mino.Piece) LongPieces(common.datastore.blocks.LongPieces) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) SizedBit(searcher.pack.SizedBit) List(java.util.List) Field(core.field.Field) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) Piece(core.mino.Piece) List(java.util.List) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) HashSet(java.util.HashSet)

Example 28 with LongPieces

use of common.datastore.blocks.LongPieces 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 29 with LongPieces

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

the class PathCore method getPiecesPattern.

private HashSet<LongPieces> getPiecesPattern(HashSet<LongPieces> piecesSolution) {
    if (isHoldReduced) {
        // allとvalidが異なる
        ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(maxDepth, maxDepth + 1);
        return piecesSolution.stream().filter(validPieces::contains).flatMap(blocks -> {
            return reverseOrderLookUp.parse(blocks.getPieces()).map(stream -> stream.collect(Collectors.toCollection(ArrayList::new))).flatMap(blocksWithHold -> {
                int nullIndex = blocksWithHold.indexOf(null);
                if (nullIndex < 0)
                    return Stream.of(new LongPieces(blocksWithHold));
                Stream.Builder<LongPieces> builder = Stream.builder();
                for (Piece piece : Piece.values()) {
                    blocksWithHold.set(nullIndex, piece);
                    builder.accept(new LongPieces(blocksWithHold));
                }
                return builder.build();
            });
        }).filter(allPieces::contains).collect(Collectors.toCollection(HashSet::new));
    } else if (isUsingHold) {
        // allとvalidが同じだが、ホールドが使える
        ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(maxDepth, maxDepth);
        return piecesSolution.stream().filter(validPieces::contains).flatMap(blocks -> {
            return reverseOrderLookUp.parse(blocks.getPieces()).map(stream -> stream.collect(Collectors.toCollection(ArrayList::new))).flatMap(blocksWithHold -> {
                int nullIndex = blocksWithHold.indexOf(null);
                if (nullIndex < 0)
                    return Stream.of(new LongPieces(blocksWithHold));
                Stream.Builder<LongPieces> builder = Stream.builder();
                for (Piece piece : Piece.values()) {
                    blocksWithHold.set(nullIndex, piece);
                    builder.accept(new LongPieces(blocksWithHold));
                }
                return builder.build();
            });
        }).filter(allPieces::contains).collect(Collectors.toCollection(HashSet::new));
    } else {
        // そのまま絞り込みだけ実施
        return piecesSolution.stream().filter(validPieces::contains).collect(Collectors.toCollection(HashSet::new));
    }
}
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) ReverseOrderLookUp(common.order.ReverseOrderLookUp) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) BuildUpStream(common.buildup.BuildUpStream) Stream(java.util.stream.Stream)

Example 30 with LongPieces

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

the class PermutationIterableTest method iteratorRandomBlock.

@Test
@LongTest
void iteratorRandomBlock() throws Exception {
    Randoms randoms = new Randoms();
    ArrayList<Piece> allPieces = Lists.newArrayList(Iterables.concat(Piece.valueList(), Piece.valueList()));
    for (int pop = 1; pop <= 7; pop++) {
        PermutationIterable<Piece> iterable = new PermutationIterable<>(allPieces, pop);
        HashSet<LongPieces> sets = StreamSupport.stream(iterable.spliterator(), false).map(Collection::stream).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
        // ランダムにサンプルを選択し、必ず列挙したセットの中にあることを確認
        for (int count = 0; count < 1000; count++) {
            List<Piece> sample = randoms.sample(allPieces, pop);
            LongPieces pieces = new LongPieces(sample);
            assertThat(pieces).isIn(sets);
        }
    }
}
Also used : Randoms(lib.Randoms) Piece(core.mino.Piece) LongPieces(common.datastore.blocks.LongPieces) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Aggregations

LongPieces (common.datastore.blocks.LongPieces)71 Test (org.junit.jupiter.api.Test)58 Piece (core.mino.Piece)42 PatternGenerator (common.pattern.PatternGenerator)38 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)36 Randoms (lib.Randoms)27 Collectors (java.util.stream.Collectors)22 Field (core.field.Field)19 Pieces (common.datastore.blocks.Pieces)16 LongTest (module.LongTest)16 List (java.util.List)14 Stream (java.util.stream.Stream)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 FieldFactory (core.field.FieldFactory)12 ArrayList (java.util.ArrayList)12 SizedBit (searcher.pack.SizedBit)12 MinoFactory (core.mino.MinoFactory)11 HashSet (java.util.HashSet)11 ExecutionException (java.util.concurrent.ExecutionException)10 BlockInterpreter (common.parser.BlockInterpreter)9