Search in sources :

Example 1 with Pair

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

the class PercentEntryPoint 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.maxClearLineUnder12(maxClearLine);
    // Output field
    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");
    ExecutorService executorService = createExecutorService();
    output("Version = " + FinderConstant.VERSION);
    output("Necessary Pieces = " + maxDepth);
    output();
    // ========================================
    // Holdができるときは必要なミノ分(maxDepth + 1)だけを取り出す。maxDepth + 1だけないときはブロックの個数をそのまま指定
    output("# Enumerate pieces");
    int piecesDepth = generator.getDepth();
    int popCount = settings.isUsingHold() && maxDepth < piecesDepth ? maxDepth + 1 : maxDepth;
    output("Piece pop count = " + popCount);
    if (popCount < piecesDepth) {
        output();
        output("####################################################################");
        output("WARNING: Inputted pieces is more than 'necessary blocks'.");
        output("         Because reduce unnecessary pieces,");
        output("         there is a possibility of getting no expected percentages.");
        output("####################################################################");
        output();
    }
    // 探索パターンの列挙
    NormalEnumeratePieces normalEnumeratePieces = new NormalEnumeratePieces(generator, maxDepth, settings.isUsingHold());
    Set<LongPieces> searchingPieces = normalEnumeratePieces.enumerate();
    output("Searching pattern size (duplicate) = " + normalEnumeratePieces.getCounter());
    output("Searching pattern size ( no dup. ) = " + searchingPieces.size());
    output();
    // ========================================
    // 探索を行う
    output("# Search");
    output("  -> Stopwatch start");
    Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
    ThreadLocal<Candidate<Action>> candidateThreadLocal = createCandidateThreadLocal(settings.getDropType(), maxClearLine);
    ThreadLocal<? extends Reachable> reachableThreadLocal = createReachableThreadLocal(settings.getDropType(), maxClearLine);
    MinoFactory minoFactory = new MinoFactory();
    PercentCore percentCore = new PercentCore(executorService, candidateThreadLocal, settings.isUsingHold(), reachableThreadLocal, minoFactory);
    percentCore.run(field, searchingPieces, maxClearLine, maxDepth);
    AnalyzeTree tree = percentCore.getResultTree();
    List<Pair<Pieces, Boolean>> resultPairs = percentCore.getResultPairs();
    stopwatch.stop();
    output("  -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
    output();
    // ========================================
    // Output tree
    output("# Output");
    output(tree.show());
    output();
    // Output failed patterns
    int treeDepth = settings.getTreeDepth();
    if (piecesDepth < treeDepth)
        treeDepth = piecesDepth;
    output(String.format("Success pattern tree [Head %d pieces]:", treeDepth));
    output(tree.tree(treeDepth));
    output("-------------------");
    int failedMaxCount = settings.getFailedCount();
    // skip if failedMaxCount == 0
    if (0 < failedMaxCount) {
        output(String.format("Fail pattern (max. %d)", failedMaxCount));
        List<Pair<Pieces, Boolean>> failedPairs = resultPairs.stream().filter(pair -> !pair.getValue()).limit(failedMaxCount).collect(Collectors.toList());
        outputFailedPatterns(failedPairs);
    } else if (failedMaxCount < 0) {
        output("Fail pattern (all)");
        List<Pair<Pieces, Boolean>> failedPairs = resultPairs.stream().filter(pair -> !pair.getValue()).collect(Collectors.toList());
        outputFailedPatterns(failedPairs);
    }
    output();
    // ========================================
    output("# Finalize");
    if (executorService != null)
        executorService.shutdown();
    output("done");
}
Also used : Candidate(core.action.candidate.Candidate) PatternGenerator(common.pattern.PatternGenerator) Stopwatch(lib.Stopwatch) EntryPoint(entry.EntryPoint) AnalyzeTree(common.tree.AnalyzeTree) Field(core.field.Field) LongPieces(common.datastore.blocks.LongPieces) NormalEnumeratePieces(entry.searching_pieces.NormalEnumeratePieces) ExecutorService(java.util.concurrent.ExecutorService) MinoFactory(core.mino.MinoFactory) List(java.util.List) Pair(common.datastore.Pair) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) NormalEnumeratePieces(entry.searching_pieces.NormalEnumeratePieces)

Example 2 with Pair

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

the class CheckerUsingHoldInvokerTest method runTestCase.

private AnalyzeTree runTestCase(ConcurrentCheckerInvoker invoker, String marks, PatternGenerator blocksGenerator, int maxClearLine, int maxDepth) throws FinderExecuteException {
    List<Pieces> searchingPieces = blocksGenerator.blocksStream().collect(Collectors.toList());
    Field field = FieldFactory.createField(marks);
    List<Pair<Pieces, Boolean>> resultPairs = invoker.search(field, searchingPieces, maxClearLine, maxDepth);
    // 結果を集計する
    AnalyzeTree tree = new AnalyzeTree();
    for (Pair<Pieces, Boolean> resultPair : resultPairs) {
        Pieces pieces = resultPair.getKey();
        Boolean result = resultPair.getValue();
        tree.set(result, pieces);
    }
    System.out.println(tree.show());
    return tree;
}
Also used : Field(core.field.Field) Pieces(common.datastore.blocks.Pieces) Pair(common.datastore.Pair) AnalyzeTree(common.tree.AnalyzeTree)

Example 3 with Pair

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

the class CheckerUsingHoldInvokerTest method random.

@LongTest
@ParameterizedTest
@ArgumentsSource(InvokerTestCase.class)
void random(IntFunction<ConcurrentCheckerInvoker> invokerGenerator) throws FinderExecuteException, SyntaxException {
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    PerfectValidator validator = new PerfectValidator();
    CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
    for (int count = 0; count < 20; count++) {
        int maxClearLine = randoms.nextInt(3, 6);
        int maxDepth = randoms.nextIntClosed(3, 5);
        Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
        Field field = randoms.field(maxClearLine, maxDepth);
        PatternGenerator blocksGenerator = createPiecesGenerator(maxDepth);
        List<Pieces> searchingPieces = blocksGenerator.blocksStream().collect(Collectors.toList());
        Injector injector = Guice.createInjector(new BasicModule(maxClearLine));
        ExecutorService executorService = injector.getInstance(ExecutorService.class);
        ConcurrentCheckerInvoker invoker = invokerGenerator.apply(maxClearLine);
        List<Pair<Pieces, Boolean>> resultPairs = invoker.search(field, searchingPieces, maxClearLine, maxDepth);
        // 結果を集計する
        AnalyzeTree tree1 = new AnalyzeTree();
        for (Pair<Pieces, Boolean> resultPair : resultPairs) {
            Pieces pieces1 = resultPair.getKey();
            Boolean result = resultPair.getValue();
            tree1.set(result, pieces1);
        }
        System.out.println(tree1.show());
        executorService.shutdown();
        AnalyzeTree tree = tree1;
        for (Pieces pieces : searchingPieces) {
            boolean check = checker.check(field, pieces.getPieces(), candidate, maxClearLine, maxDepth);
            assertThat(tree.isSucceed(pieces)).isEqualTo(check);
        }
    }
}
Also used : Action(common.datastore.action.Action) BasicModule(module.BasicModule) LockedCandidate(core.action.candidate.LockedCandidate) Field(core.field.Field) Injector(com.google.inject.Injector) MinoFactory(core.mino.MinoFactory) PerfectValidator(searcher.common.validator.PerfectValidator) Pieces(common.datastore.blocks.Pieces) Pair(common.datastore.Pair) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) AnalyzeTree(common.tree.AnalyzeTree) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) CheckerUsingHold(searcher.checker.CheckerUsingHold) ExecutorService(java.util.concurrent.ExecutorService) MinoShifter(core.mino.MinoShifter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LongTest(module.LongTest) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource)

Example 4 with Pair

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

the class SingleCheckerNoHoldInvoker 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);
    try {
        ArrayList<Pair<Pieces, Boolean>> results = new ArrayList<>();
        for (Pieces target : searchingPieces) {
            Task task = new Task(obj, commonObj, target);
            results.add(task.call());
        }
        return results;
    } catch (Exception e) {
        throw new FinderExecuteException(e);
    }
}
Also used : CheckerCommonObj(concurrent.checker.invoker.CheckerCommonObj) ArrayList(java.util.ArrayList) FinderExecuteException(exceptions.FinderExecuteException) ConcurrentVisitedTree(common.tree.ConcurrentVisitedTree) FinderExecuteException(exceptions.FinderExecuteException) Pair(common.datastore.Pair) Pieces(common.datastore.blocks.Pieces)

Example 5 with Pair

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

the class Task method call.

@Override
public List<Pair<List<Piece>, List<Result>>> call() throws Exception {
    Checkmate<Action> checkmate = obj.checkmateThreadLocal.get();
    Candidate<Action> candidate = obj.candidateThreadLocal.get();
    // 探索
    List<Pair<List<Piece>, List<Result>>> allResults = new ArrayList<>();
    for (ReadOnlyListPieces piece : targets) {
        List<Piece> pieces = piece.getPieces();
        List<Result> results = checkmate.search(obj.field, pieces, candidate, obj.maxClearLine, obj.maxDepth);
        allResults.add(new Pair<>(pieces, results));
    }
    return allResults;
}
Also used : ReadOnlyListPieces(common.datastore.blocks.ReadOnlyListPieces) Action(common.datastore.action.Action) Piece(core.mino.Piece) ArrayList(java.util.ArrayList) Pair(common.datastore.Pair) Result(common.datastore.Result)

Aggregations

Pair (common.datastore.Pair)8 Pieces (common.datastore.blocks.Pieces)6 AnalyzeTree (common.tree.AnalyzeTree)4 Field (core.field.Field)4 FinderExecuteException (exceptions.FinderExecuteException)3 ArrayList (java.util.ArrayList)3 Action (common.datastore.action.Action)2 LongPieces (common.datastore.blocks.LongPieces)2 PatternGenerator (common.pattern.PatternGenerator)2 ConcurrentVisitedTree (common.tree.ConcurrentVisitedTree)2 CheckerCommonObj (concurrent.checker.invoker.CheckerCommonObj)2 MinoFactory (core.mino.MinoFactory)2 Piece (core.mino.Piece)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Injector (com.google.inject.Injector)1 Result (common.datastore.Result)1 ReadOnlyListPieces (common.datastore.blocks.ReadOnlyListPieces)1 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)1 ColorType (common.tetfu.common.ColorType)1