Search in sources :

Example 1 with FinderExecuteException

use of exceptions.FinderExecuteException in project solution-finder by knewjade.

the class PatternCSVPathOutput method output.

@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
    this.lastException = null;
    outputLog("Found path = " + pathPairs.size());
    AtomicInteger validCounter = new AtomicInteger();
    AtomicInteger allCounter = new AtomicInteger();
    try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
        writer.writeAndNewLine("ツモ,対応地形数,使用ミノ,未使用ミノ,テト譜");
        generator.blocksStream().parallel().map(blocks -> {
            // シーケンス名を取得
            String sequenceName = blocks.blockStream().map(Piece::getName).collect(Collectors.joining());
            // パフェ可能な地形を抽出
            List<PathPair> valid = pathPairs.stream().filter(pathPair -> {
                HashSet<? extends Pieces> buildBlocks = pathPair.blocksHashSetForPattern();
                return buildBlocks.contains(blocks);
            }).collect(Collectors.toList());
            // パフェ可能な地形数
            int possibleSize = valid.size();
            // パフェ可能ならカウンターをインクリメント
            allCounter.incrementAndGet();
            if (0 < possibleSize)
                validCounter.incrementAndGet();
            // パフェ可能な地形のテト譜を連結
            String fumens = valid.stream().sorted(Comparator.comparing(PathPair::getPatternSize).reversed()).map(pathPair -> "v115@" + pathPair.getFumen()).collect(Collectors.joining(";"));
            // 使うミノ一覧を抽出
            Set<PieceCounter> usesSet = valid.stream().map(PathPair::getBlockCounter).collect(Collectors.toSet());
            String uses = usesSet.stream().map(blockCounter -> {
                return blockCounter.getBlockStream().sorted().map(Piece::getName).collect(Collectors.joining());
            }).collect(Collectors.joining(";"));
            // 残せるミノ一覧を抽出
            PieceCounter orderPieceCounter = new PieceCounter(blocks.blockStream());
            String noUses = usesSet.stream().map(orderPieceCounter::removeAndReturnNew).distinct().map(blockCounter -> {
                return blockCounter.getBlockStream().sorted().map(Piece::getName).collect(Collectors.joining());
            }).collect(Collectors.joining(";"));
            return String.format("%s,%d,%s,%s,%s", sequenceName, possibleSize, uses, noUses, fumens);
        }).forEach(writer::writeAndNewLine);
        writer.flush();
    } catch (IOException e) {
        throw new FinderExecuteException("Failed to output file", e);
    }
    outputLog("");
    outputLog("perfect clear percent");
    outputLog(String.format("  -> success = %.2f%% (%d/%d)", 100.0 * validCounter.get() / allCounter.get(), validCounter.get(), allCounter.get()));
    if (lastException != null)
        throw new FinderExecuteException("Error to output file", lastException);
}
Also used : Piece(core.mino.Piece) Pieces(common.datastore.blocks.Pieces) PatternGenerator(common.pattern.PatternGenerator) Set(java.util.Set) ReducePatternGenerator(entry.path.ReducePatternGenerator) IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderInitializeException(exceptions.FinderInitializeException) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) List(java.util.List) Field(core.field.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AsyncBufferedFileWriter(lib.AsyncBufferedFileWriter) PathPair(entry.path.PathPair) Comparator(java.util.Comparator) PathEntryPoint(entry.path.PathEntryPoint) PathSettings(entry.path.PathSettings) Set(java.util.Set) HashSet(java.util.HashSet) 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) List(java.util.List) FinderExecuteException(exceptions.FinderExecuteException)

Example 2 with FinderExecuteException

use of exceptions.FinderExecuteException 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 3 with FinderExecuteException

use of exceptions.FinderExecuteException in project solution-finder by knewjade.

the class PercentEntryPoint method close.

@Override
public void close() throws FinderTerminateException {
    try {
        flush();
        logWriter.close();
    } catch (IOException | FinderExecuteException e) {
        throw new FinderTerminateException(e);
    }
}
Also used : IOException(java.io.IOException) FinderExecuteException(exceptions.FinderExecuteException) FinderTerminateException(exceptions.FinderTerminateException)

Example 4 with FinderExecuteException

use of exceptions.FinderExecuteException 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 5 with FinderExecuteException

use of exceptions.FinderExecuteException in project solution-finder by knewjade.

the class ConcurrentCheckerNoHoldInvoker method search.

@Override
public List<Pair<Pieces, Boolean>> search(Field field, List<Pieces> searchingPieces, int maxClearLine, int maxDepth) throws FinderExecuteException {
    ConcurrentVisitedTree visitedTree = new ConcurrentVisitedTree();
    Obj obj = new Obj(field, maxClearLine, maxDepth, visitedTree);
    ArrayList<Task> tasks = new ArrayList<>();
    for (Pieces target : searchingPieces) tasks.add(new Task(obj, commonObj, target));
    try {
        return execute(tasks);
    } catch (InterruptedException | ExecutionException e) {
        throw new FinderExecuteException(e);
    }
}
Also used : CheckerCommonObj(concurrent.checker.invoker.CheckerCommonObj) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) FinderExecuteException(exceptions.FinderExecuteException) ConcurrentVisitedTree(common.tree.ConcurrentVisitedTree) Pieces(common.datastore.blocks.Pieces)

Aggregations

FinderExecuteException (exceptions.FinderExecuteException)15 IOException (java.io.IOException)10 Pieces (common.datastore.blocks.Pieces)6 Field (core.field.Field)6 FinderInitializeException (exceptions.FinderInitializeException)5 SizedBit (searcher.pack.SizedBit)5 PatternGenerator (common.pattern.PatternGenerator)4 ConcurrentVisitedTree (common.tree.ConcurrentVisitedTree)4 CheckerCommonObj (concurrent.checker.invoker.CheckerCommonObj)4 BufferedWriter (java.io.BufferedWriter)4 ArrayList (java.util.ArrayList)4 LongPieces (common.datastore.blocks.LongPieces)3 Piece (core.mino.Piece)3 PathEntryPoint (entry.path.PathEntryPoint)3 PathPair (entry.path.PathPair)3 PathSettings (entry.path.PathSettings)3 FinderTerminateException (exceptions.FinderTerminateException)3 File (java.io.File)3 ExecutionException (java.util.concurrent.ExecutionException)3 Collectors (java.util.stream.Collectors)3