Search in sources :

Example 6 with Stopwatch

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

the class CheckmateUsingHoldReuseTest method randomCheckmateOverMoreBlock.

@Test
@LongTest
void randomCheckmateOverMoreBlock() {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 25; 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)

Example 7 with Stopwatch

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

the class CheckmateUsingHoldReuseTest method randomCheckmateOverBlock.

@Test
@LongTest
void randomCheckmateOverBlock() {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 25; 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 8 with Stopwatch

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

the class CheckmateUsingHoldReuseTest method randomCheckmateWithJustBlockTwice.

@Disabled
@Test
void randomCheckmateWithJustBlockTwice() {
    Randoms randoms = new Randoms();
    for (int count = 0; count < 25; count++) {
        int maxClearLine = randoms.nextInt(3, 8);
        int maxDepth = randoms.nextIntClosed(5, 7);
        List<Piece> pieces = randoms.blocks(maxDepth);
        Field field = randoms.field(maxClearLine, maxDepth);
        Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
        Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
        Stopwatch stopwatchReuse1 = Stopwatch.createStoppedStopwatch();
        Stopwatch stopwatchReuse2 = 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();
            stopwatchReuse1.start();
            List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchReuse1.stop();
            assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
            stopwatchReuse2.start();
            List<Result> result3 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
            stopwatchReuse2.stop();
            assertThat(result3).hasSameSizeAs(result1).containsAll(result1);
        }
        assertThat(stopwatchReuse1.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
        assertThat(stopwatchReuse2.getNanoAverageTime()).isLessThan(stopwatchReuse1.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) Disabled(org.junit.jupiter.api.Disabled)

Example 9 with Stopwatch

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

the class SetupEntryPoint method run.

@Override
public void run() throws FinderException {
    output("# Setup Field");
    // Setup init field
    Field initField = settings.getInitField();
    Verify.field(initField);
    // Setup need filled field
    Field needFilledField = settings.getNeedFilledField();
    Verify.needFilledField(needFilledField);
    // Setup not filled field
    Field notFilledField = settings.getNotFilledField();
    // Setup max height
    int maxHeight = settings.getMaxHeight();
    Verify.maxClearLineUnder10(maxHeight);
    // Setup reserved blocks
    BlockField reservedBlocks = settings.getReservedBlock();
    if (settings.isReserved()) {
        Verify.reservedBlocks(reservedBlocks);
        for (int y = maxHeight - 1; 0 <= y; y--) {
            StringBuilder builder = new StringBuilder();
            for (int x = 0; x < 10; x++) {
                if (reservedBlocks.getBlock(x, y) != null)
                    builder.append(reservedBlocks.getBlock(x, y).getName());
                else if (!initField.isEmpty(x, y))
                    builder.append('X');
                else if (!needFilledField.isEmpty(x, y))
                    builder.append('*');
                else if (!notFilledField.isEmpty(x, y))
                    builder.append('_');
                else
                    builder.append('.');
            }
            output(builder.toString());
        }
    } else {
        for (int y = maxHeight - 1; 0 <= y; y--) {
            StringBuilder builder = new StringBuilder();
            for (int x = 0; x < 10; x++) {
                if (!initField.isEmpty(x, y))
                    builder.append('X');
                else if (!needFilledField.isEmpty(x, y))
                    builder.append('*');
                else if (!notFilledField.isEmpty(x, y))
                    builder.append('_');
                else
                    builder.append('.');
            }
            output(builder.toString());
        }
    }
    // Setup min depth
    // 最低でも必要なミノ数
    int minDepth = Verify.minDepth(needFilledField);
    output();
    // ========================================
    // Output user-defined
    DropType dropType = settings.getDropType();
    output("# Initialize / User-defined");
    output("Max height: " + maxHeight);
    output("Drop: " + dropType.name().toLowerCase());
    output("Searching patterns:");
    // Setup patterns
    List<String> patterns = settings.getPatterns();
    PatternGenerator generator = Verify.patterns(patterns, minDepth);
    // Output patterns
    for (String pattern : patterns) output("  " + pattern);
    // Setup output file
    MyFile base = new MyFile(settings.getOutputBaseFilePath());
    base.mkdirs();
    base.verify();
    output();
    // ========================================
    // Setup core
    output("# Initialize / System");
    int core = Runtime.getRuntime().availableProcessors();
    // Output system-defined
    output("Version = " + FinderConstant.VERSION);
    output("Available processors = " + core);
    output("Need Pieces = " + minDepth);
    output();
    // ========================================
    output("# Search");
    output("  -> Stopwatch start");
    Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
    // Initialize
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    ColorConverter colorConverter = new ColorConverter();
    SizedBit sizedBit = decideSizedBitSolutionWidth(maxHeight);
    TaskResultHelper taskResultHelper = new BasicMinoPackingHelper();
    SolutionFilter solutionFilter = new ForPathSolutionFilter(generator, maxHeight);
    ThreadLocal<BuildUpStream> buildUpStreamThreadLocal = createBuildUpStreamThreadLocal(dropType, maxHeight);
    OneFumenParser fumenParser = new OneFumenParser(minoFactory, colorConverter);
    // ミノリストの作成
    long deleteKeyMask = getDeleteKeyMask(notFilledField, maxHeight);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit, deleteKeyMask);
    // 絶対に置かないブロック
    List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, notFilledField);
    // 絶対に置く必要があるブロック
    ArrayList<BasicSolutions> basicSolutions = new ArrayList<>();
    List<ColumnField> needFillFields = InOutPairField.createInnerFields(sizedBit, needFilledField);
    {
        Field freeze = needFilledField.freeze(sizedBit.getHeight());
        for (ColumnField innerField : needFillFields) {
            // 最小限の部分だけを抽出する
            Field freeze1 = freeze.freeze(sizedBit.getHeight());
            for (int y = 0; y < sizedBit.getHeight(); y++) {
                int width = sizedBit.getWidth();
                for (int x = 0; x < width; x++) freeze1.removeBlock(x, y);
                for (int x = width + 3; x < 10; x++) freeze1.removeBlock(x, y);
            }
            OnDemandBasicSolutions solutions = new OnDemandBasicSolutions(separableMinos, sizedBit, innerField.getBoard(0), freeze1);
            basicSolutions.add(solutions);
            freeze.slideLeft(sizedBit.getWidth());
        }
    }
    // 探索
    SetupPackSearcher searcher = new SetupPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper, needFillFields);
    List<Result> results = getResults(initField, sizedBit, buildUpStreamThreadLocal, searcher);
    output("     Found solution = " + results.size());
    stopwatch.stop();
    output("  -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
    output();
    // ========================================
    output("# Output file");
    HTMLBuilder<FieldHTMLColumn> htmlBuilder = new HTMLBuilder<>("Setup result");
    results.parallelStream().forEach(result -> {
        List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
        Field allField = initField.freeze(maxHeight);
        Operations operations = OperationTransform.parseToOperations(allField, operationWithKeys, sizedBit.getHeight());
        List<? extends Operation> operationList = operations.getOperations();
        for (Operation operation : operationList) {
            Mino mino = minoFactory.create(operation.getPiece(), operation.getRotate());
            int x = operation.getX();
            int y = operation.getY();
            allField.put(mino, x, y);
        }
        // 譜面の作成
        String encode = fumenParser.parse(operationWithKeys, initField, maxHeight);
        String name = operationWithKeys.stream().map(OperationWithKey::getPiece).map(Piece::getName).collect(Collectors.joining());
        String link = String.format("<a href='http://fumen.zui.jp/?v115@%s' target='_blank'>%s</a>", encode, name);
        String line = String.format("<div>%s</div>", link);
        htmlBuilder.addColumn(new FieldHTMLColumn(allField, maxHeight), line);
    });
    ArrayList<FieldHTMLColumn> columns = new ArrayList<>(htmlBuilder.getRegisteredColumns());
    columns.sort(Comparator.comparing(FieldHTMLColumn::getTitle).reversed());
    try (BufferedWriter bufferedWriter = base.newBufferedWriter()) {
        for (String line : htmlBuilder.toList(columns, true)) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
    } catch (IOException e) {
        throw new FinderExecuteException(e);
    }
    output();
    // ========================================
    output("# Finalize");
    output("done");
}
Also used : OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) MyFile(entry.path.output.MyFile) Stopwatch(lib.Stopwatch) ForPathSolutionFilter(entry.path.ForPathSolutionFilter) TaskResultHelper(searcher.pack.task.TaskResultHelper) Result(searcher.pack.task.Result) BufferedWriter(java.io.BufferedWriter) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) ColorConverter(common.tetfu.common.ColorConverter) SeparableMino(searcher.pack.separable_mino.SeparableMino) Mino(core.mino.Mino) MinoFactory(core.mino.MinoFactory) BuildUpStream(common.buildup.BuildUpStream) PatternGenerator(common.pattern.PatternGenerator) OneFumenParser(entry.path.output.OneFumenParser) HTMLBuilder(output.HTMLBuilder) ColumnField(core.column_field.ColumnField) BasicMinoPackingHelper(searcher.pack.task.BasicMinoPackingHelper) IOException(java.io.IOException) EntryPoint(entry.EntryPoint) BasicSolutions(searcher.pack.calculator.BasicSolutions) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) SetupPackSearcher(searcher.pack.task.SetupPackSearcher) SizedBit(searcher.pack.SizedBit) ForPathSolutionFilter(entry.path.ForPathSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) MinoShifter(core.mino.MinoShifter) DropType(entry.DropType) FinderExecuteException(exceptions.FinderExecuteException)

Example 10 with Stopwatch

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

the class FigUtilEntryPoint method run.

@Override
public void run() throws FinderException {
    output("# Setup");
    output("Version = " + FinderConstant.VERSION);
    MinoFactory minoFactory = new MinoFactory();
    ColorConverter colorConverter = new ColorConverter();
    FrameType frameType = settings.getFrameType();
    File outputFile = new File(settings.getOutputFilePath());
    output();
    output("# Generate");
    output("  -> Stopwatch start");
    Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
    List<TetfuPage> usingTetfuPages = useTetfuPages();
    FigFormat figFormat = settings.getFigFormat();
    FigWriter figWriter = createFigWriter(minoFactory, colorConverter, frameType, outputFile, figFormat, usingTetfuPages);
    generate(figWriter, usingTetfuPages);
    stopwatch.stop();
    output("  -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
}
Also used : TetfuPage(common.tetfu.TetfuPage) FigWriter(util.fig.output.FigWriter) FrameType(util.fig.FrameType) Stopwatch(lib.Stopwatch) ColorConverter(common.tetfu.common.ColorConverter) MinoFactory(core.mino.MinoFactory) File(java.io.File)

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