Search in sources :

Example 11 with Stopwatch

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

the class Main3 method main.

public static void main(String[] args) {
    Field field = FieldFactory.createField("" + "X_______XX" + "X_______XX" + "X_______XX" + "X_______XX");
    int maxClearLine = 4;
    Field verifyField = field.freeze(maxClearLine);
    System.out.println(FieldView.toString(field, maxClearLine));
    Stopwatch stopwatch = Stopwatch.createStoppedStopwatch();
    stopwatch.start();
    Set<List<Piece>> sets = new HashSet<>();
    List<Piece> allPieces = Piece.valueList();
    List<Piece> pieces = new ArrayList<>(allPieces);
    int popCount = (maxClearLine * 10 - field.getNumOfAllBlocks()) / 4;
    CombinationIterable<Piece> combinationIterable = new CombinationIterable<>(pieces, popCount);
    for (List<Piece> pieceList : combinationIterable) {
        pieceList.sort(Comparator.comparingInt(allPieces::indexOf));
        sets.add(pieceList);
    }
    TreeSet<Obj> allObjSet = new TreeSet<>();
    int counter = 0;
    for (List<Piece> usedPieces : sets) {
        counter++;
        System.out.println(usedPieces);
        System.out.println(counter + " / " + sets.size());
        List<List<MinoOperationWithKey>> operationsWithKey = search(usedPieces, field, maxClearLine, verifyField);
        List<Obj> objs = operationsWithKey.stream().map(operationWithKeys -> {
            boolean isDeleted = false;
            BlockField blockField = new BlockField(maxClearLine);
            for (MinoOperationWithKey key : operationWithKeys) {
                Field test = FieldFactory.createField(maxClearLine);
                Mino mino = key.getMino();
                test.put(mino, key.getX(), key.getY());
                test.insertWhiteLineWithKey(key.getNeedDeletedKey());
                blockField.merge(test, mino.getPiece());
                if (key.getNeedDeletedKey() != 0L)
                    isDeleted = true;
            }
            return new Obj(usedPieces, blockField, isDeleted, operationWithKeys);
        }).collect(Collectors.toList());
        System.out.println(objs.size());
        objs.stream().map(obj -> OperationWithKeyInterpreter.parseToString(obj.operations)).sorted().sequential().forEach(System.out::println);
        allObjSet.addAll(objs);
    }
    System.out.println(allObjSet.size());
    System.exit(0);
    ArrayList<Obj> allObjs = new ArrayList<>(allObjSet);
    allObjs.sort(Main3::blockListComparator);
    // System.out.println(operationsWithKey.size());
    ColorConverter colorConverter = new ColorConverter();
    System.out.println("<h3>各ミノ最大1つずつ & ライン消去なし</h3>");
    Predicate<Obj> objPredicate = obj -> !obj.isDouble && !obj.isDeleted;
    List<TetfuElement> oneNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate);
    viewTetfu(oneNoDelete);
    System.out.println("<h3>同一ミノを2つ利用 & ライン消去なし</h3>");
    Predicate<Obj> objPredicate1 = obj -> obj.isDouble && !obj.isDeleted;
    List<TetfuElement> doubleNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate1);
    viewTetfu(doubleNoDelete);
    System.out.println("<h3>各ミノ最大1つずつ & ライン消去あり</h3>");
    Predicate<Obj> objPredicate2 = obj -> !obj.isDouble && obj.isDeleted;
    List<TetfuElement> oneDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate2);
    viewTetfu(oneDeleted);
    System.out.println("<h3>同一ミノを2つ利用 & ライン消去あり</h3>");
    Predicate<Obj> objPredicate3 = obj -> obj.isDouble && obj.isDeleted;
    List<TetfuElement> doubleDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate3);
    viewTetfu(doubleDeleted);
    // List<List<FullLimitedMino>> lists = Arrays.asList(
    // singletonList(create(minoFactory, Piece.I, Rotate.Left, PositionLimit.OddX, 0L, 0, 3)),
    // singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 2, 3)),
    // singletonList(create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.Z, Rotate.Spawn, PositionLimit.EvenX, 0L, 2, 3)),
    // singletonList(create(minoFactory, Piece.S, Rotate.Left, PositionLimit.OddX, 1024L, 0, 3)),
    // singletonList(create(minoFactory, Piece.Z, Rotate.Left, PositionLimit.EvenX, 1024L, 0, 3)),
    // singletonList(create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
    // singletonList(create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.EvenX, 0L, 2, 3))
    // );
    // CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField(maxClearLine), maxClearLine);
    // List<List<OperationWithKey>> lists1 = crossBuilder.create();
    // System.out.println(lists1.size());
    // List<List<OperationWithKey>> search = new Search(FieldFactory.createField(4), Arrays.asList(
    // create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 2, 3),
    // create(minoFactory, Piece.I, Rotate.Right, PositionLimit.NoLimit, 0L, 0, 3),
    // create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
    // create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.NoLimit, 0L, 2, 3),
    // create(minoFactory, Piece.Z, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3),
    // create(minoFactory, Piece.S, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3)
    // ), maxClearLine).search();
    // System.out.println("--");
    // for (List<OperationWithKey> operationWithKeys : search) {
    // System.out.println(operationWithKeys);
    // }
    // MinoFactory minoFactory = new MinoFactory();
    // List<List<FullLimitedMino>> lists = Arrays.asList(
    // singletonList(create(minoFactory, Piece.J, Rotate.Right, PositionLimit.OddX, 0L, 0, 2)),
    // singletonList(create(minoFactory, Piece.L, Rotate.Left, PositionLimit.EvenX, 1048576, 0, 4))
    // );
    // CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField("" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX" +
    // "__XXXXXXXX"
    // ), maxClearLine);
    // List<List<OperationWithKey>> lists1 = crossBuilder.create();
    // for (List<OperationWithKey> operationWithKeys : lists1) {
    // System.out.println(operationWithKeys);
    // }
    // System.out.println(lists1.size());
    stopwatch.stop();
    System.out.println(stopwatch.toMessage(TimeUnit.MILLISECONDS));
}
Also used : java.util(java.util) Stopwatch(lib.Stopwatch) ColorType(common.tetfu.common.ColorType) OperationWithKeyComparator(common.comparator.OperationWithKeyComparator) common.datastore(common.datastore) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Tetfu(common.tetfu.Tetfu) FieldView(core.field.FieldView) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) CombinationIterable(common.iterable.CombinationIterable) FieldFactory(core.field.FieldFactory) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) Predicate(java.util.function.Predicate) BuildUp(common.buildup.BuildUp) TetfuElement(common.tetfu.TetfuElement) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) TimeUnit(java.util.concurrent.TimeUnit) Field(core.field.Field) Stream(java.util.stream.Stream) OperationWithKeyInterpreter(common.parser.OperationWithKeyInterpreter) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColoredField(common.tetfu.field.ColoredField) Mino(core.mino.Mino) Stopwatch(lib.Stopwatch) Field(core.field.Field) ColoredField(common.tetfu.field.ColoredField) CombinationIterable(common.iterable.CombinationIterable) Piece(core.mino.Piece) DeltaLimitedMino(_implements.parity_based_pack.step1.DeltaLimitedMino) FullLimitedMino(_implements.parity_based_pack.step2.FullLimitedMino) Mino(core.mino.Mino) ColorConverter(common.tetfu.common.ColorConverter) TetfuElement(common.tetfu.TetfuElement)

Example 12 with Stopwatch

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

the class CheckmateNoHoldReuseTest method randomCheckmateWithJustBlock.

@Test
@LongTest
void randomCheckmateWithJustBlock() {
    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);
        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);
        }
    // 実行時間の比較がうまくいかないためskipする(戻り値のチェックのみ)
    // 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 13 with Stopwatch

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

the class CheckmateNoHoldReuseTest method randomCheckmateWithJustBlockTwice.

@Test
void randomCheckmateWithJustBlockTwice() {
    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);
        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);
        }
    // 実行時間の比較がうまくいかないためskipする(戻り値のチェックのみ)
    // 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)

Example 14 with Stopwatch

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

the class CheckmateUsingHoldReuseTest method randomCheckmateWithJustBlock.

@Test
@LongTest
void randomCheckmateWithJustBlock() {
    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 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);
        }
    // TODO:
    // 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 15 with Stopwatch

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

the class LockedNeighborCandidateTest method random.

@Test
void random() {
    Injector injector = Guice.createInjector(new BasicModule());
    int maxClearLine = 3;
    LockedCandidate candidate1 = createLockedCandidate(injector, maxClearLine);
    LockedNeighborCandidate candidate2 = createLockedNeighborCandidate(injector, maxClearLine);
    MinoShifter minoShifter = injector.getInstance(MinoShifter.class);
    Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
    Stopwatch stopwatch2 = Stopwatch.createStartedStopwatch();
    Randoms randoms = new Randoms();
    for (int count = 0; count < 10000; count++) {
        Field field = randoms.field(maxClearLine, 7);
        for (Piece piece : Piece.values()) {
            // LockedCandidate
            stopwatch1.start();
            Set<Action> search1 = candidate1.search(field, piece, maxClearLine);
            stopwatch1.stop();
            // LockedNeighborCandidate
            stopwatch2.start();
            Set<Neighbor> neighbors = candidate2.search(field, piece, maxClearLine);
            stopwatch2.stop();
            Set<Action> search2 = neighbors.stream().map(Neighbor::getPiece).map(this::createMinimalAction).map(action -> minoShifter.createTransformedAction(piece, action)).collect(Collectors.toSet());
            assertThat(search2).isEqualTo(search1);
        }
    }
    System.out.println(stopwatch1.toMessage(TimeUnit.NANOSECONDS));
    System.out.println(stopwatch2.toMessage(TimeUnit.NANOSECONDS));
}
Also used : MinimalAction(common.datastore.action.MinimalAction) Randoms(lib.Randoms) Stopwatch(lib.Stopwatch) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BasicModule(module.BasicModule) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Action(common.datastore.action.Action) Neighbor(core.neighbor.Neighbor) MinoFactory(core.mino.MinoFactory) FieldFactory(core.field.FieldFactory) ArgumentsProvider(org.junit.jupiter.params.provider.ArgumentsProvider) MinoRotation(core.srs.MinoRotation) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) OriginalPieceFactory(core.neighbor.OriginalPieceFactory) Set(java.util.Set) Arguments(org.junit.jupiter.params.provider.Arguments) Collectors(java.util.stream.Collectors) Injector(com.google.inject.Injector) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Field(core.field.Field) Stream(java.util.stream.Stream) Guice(com.google.inject.Guice) OriginalPiece(core.neighbor.OriginalPiece) ArgumentsSource(org.junit.jupiter.params.provider.ArgumentsSource) MinimalAction(common.datastore.action.MinimalAction) Action(common.datastore.action.Action) BasicModule(module.BasicModule) Stopwatch(lib.Stopwatch) Neighbor(core.neighbor.Neighbor) Field(core.field.Field) Randoms(lib.Randoms) Injector(com.google.inject.Injector) Piece(core.mino.Piece) OriginalPiece(core.neighbor.OriginalPiece) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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