Search in sources :

Example 1 with Stopwatch

use of lib.Stopwatch in project solution-finder by knewjade.

the class MappedBasicSolutionsFactoryTest method assertCache.

private void assertCache(SizedBit sizedBit, long expectedSolutions, long expectedSolutionItems) throws IOException {
    SeparableMinos separableMinos = createSeparableMinos(sizedBit);
    BasicSolutionsCalculator calculator = new BasicSolutionsCalculator(separableMinos, sizedBit);
    Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
    Map<ColumnField, RecursiveMinoFields> calculate = calculator.calculate();
    stopwatch1.stop();
    System.out.println("create only: " + stopwatch1.toMessage(TimeUnit.MILLISECONDS));
    AllPassedSolutionFilter solutionFilter = new AllPassedSolutionFilter();
    MappedBasicSolutions solutions = new MappedBasicSolutions(calculate, solutionFilter);
    assertThat(countValidKey(solutions)).isEqualTo(expectedSolutions);
    assertThat(countValidItem(solutions)).isEqualTo(expectedSolutionItems);
}
Also used : SeparableMinos(searcher.pack.SeparableMinos) ColumnField(core.column_field.ColumnField) Stopwatch(lib.Stopwatch) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields)

Example 2 with Stopwatch

use of lib.Stopwatch 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 3 with Stopwatch

use of lib.Stopwatch in project solution-finder by knewjade.

the class OnDemandBasicSolutionsFactoryTest method assertCache.

private void assertCache(SizedBit sizedBit, long expectedSolutions, long expectedSolutionItems) throws IOException {
    SeparableMinos separableMinos = createSeparableMinos(sizedBit);
    int minMemorizedBit = (int) (sizedBit.getMaxBitDigit() * 0.2);
    Predicate<ColumnField> memorizedPredicate = BasicSolutions.createBitCountPredicate(minMemorizedBit);
    OnDemandBasicSolutions solutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
    LongStream.rangeClosed(0, sizedBit.getFillBoard()).parallel().boxed().sorted(Comparator.comparingLong(Long::bitCount).reversed()).map(ColumnFieldFactory::createField).forEach(solutions::parse);
    stopwatch1.stop();
    System.out.println("create only: " + stopwatch1.toMessage(TimeUnit.MILLISECONDS));
    assertThat(countValidKey(solutions)).isEqualTo(expectedSolutions);
    assertThat(countValidItem(solutions)).isEqualTo(expectedSolutionItems);
}
Also used : SeparableMinos(searcher.pack.SeparableMinos) ColumnField(core.column_field.ColumnField) Stopwatch(lib.Stopwatch)

Example 4 with Stopwatch

use of lib.Stopwatch in project solution-finder by knewjade.

the class CheckmateNoHoldReuseTest method randomCheckmateOverBlock.

@Test
@LongTest
void randomCheckmateOverBlock() {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 100; count++) {
        int maxClearLine = randoms.nextInt(3, 8);
        int maxDepth = randoms.nextIntClosed(5, 7);
        List<Piece> pieces = randoms.blocks(maxDepth + 1);
        Field field = randoms.field(maxClearLine, maxDepth);
        Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
        Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
        Stopwatch stopwatchReuse = Stopwatch.createStoppedStopwatch();
        for (int swap = 0; swap < 250; swap++) {
            int index = randoms.nextInt(3, pieces.size());
            Piece pop = pieces.remove(index);
            pieces.add(pop);
            stopwatchNoUse.start();
            List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchNoUse.stop();
            stopwatchReuse.start();
            List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchReuse.stop();
            assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
        }
    // assertThat(stopwatchReuse.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
    }
}
Also used : Field(core.field.Field) LockedCandidate(core.action.candidate.LockedCandidate) Randoms(lib.Randoms) Action(common.datastore.action.Action) Piece(core.mino.Piece) Stopwatch(lib.Stopwatch) Result(common.datastore.Result) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 5 with Stopwatch

use of lib.Stopwatch in project solution-finder by knewjade.

the class CheckmateNoHoldReuseTest method randomCheckmateOverMoreBlock.

@Test
@LongTest
void randomCheckmateOverMoreBlock() {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 100; count++) {
        int maxClearLine = randoms.nextInt(3, 8);
        int maxDepth = randoms.nextIntClosed(5, 7);
        List<Piece> pieces = randoms.blocks(maxDepth + 10);
        Field field = randoms.field(maxClearLine, maxDepth);
        Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
        Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
        Stopwatch stopwatchReuse = Stopwatch.createStoppedStopwatch();
        for (int swap = 0; swap < 250; swap++) {
            int index = randoms.nextInt(3, pieces.size());
            Piece pop = pieces.remove(index);
            pieces.add(pop);
            stopwatchNoUse.start();
            List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchNoUse.stop();
            stopwatchReuse.start();
            List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchReuse.stop();
            assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
        }
        assertThat(stopwatchReuse.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
    }
}
Also used : Field(core.field.Field) LockedCandidate(core.action.candidate.LockedCandidate) Randoms(lib.Randoms) Action(common.datastore.action.Action) Piece(core.mino.Piece) Stopwatch(lib.Stopwatch) Result(common.datastore.Result) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Aggregations

Stopwatch (lib.Stopwatch)16 Field (core.field.Field)13 Piece (core.mino.Piece)10 Action (common.datastore.action.Action)9 Randoms (lib.Randoms)9 Test (org.junit.jupiter.api.Test)9 Result (common.datastore.Result)8 LockedCandidate (core.action.candidate.LockedCandidate)8 LongTest (module.LongTest)8 MinoFactory (core.mino.MinoFactory)6 ColorConverter (common.tetfu.common.ColorConverter)4 ColumnField (core.column_field.ColumnField)4 PatternGenerator (common.pattern.PatternGenerator)3 MinoShifter (core.mino.MinoShifter)3 EntryPoint (entry.EntryPoint)3 FieldFactory (core.field.FieldFactory)2 Mino (core.mino.Mino)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2