Search in sources :

Example 81 with Randoms

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

the class PackSearcherTest method assertHeight4.

private void assertHeight4(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
    assert sizedBit.getWidth() == 3;
    assert sizedBit.getHeight() == 4;
    int width = sizedBit.getWidth();
    int height = sizedBit.getHeight();
    Randoms randoms = new Randoms();
    Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
    LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    for (int count = 0; count < maxCount; count++) {
        // Field
        int maxDepth = randoms.nextIntClosed(3, 6);
        Field initField = randoms.field(height, maxDepth);
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, initField);
        SolutionFilter solutionFilter = createSRSSolutionFilter(sizedBit, initField);
        // Pack
        BasicSolutions basicSolutions = basicSolutionSupplier.apply(initField, solutionFilter);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        List<Result> results = searcher.toList();
        // Possible
        HashSet<Pieces> possiblePieces = new HashSet<>();
        for (Result result : results) {
            // result to possible pieces
            List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(width).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
            Set<LongPieces> sets = new BuildUpStream(reachable, height).existsValidBuildPattern(initField, operationWithKeys).map(keys -> keys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toSet());
            possiblePieces.addAll(sets);
        }
        // Checker
        PerfectValidator validator = new PerfectValidator();
        CheckerNoHold<Action> checker = new CheckerNoHold<>(minoFactory, validator);
        // Assert generator
        PatternGenerator generator = createPiecesGenerator(maxDepth);
        generator.blocksStream().forEach(blocks -> {
            List<Piece> pieceList = blocks.getPieces();
            boolean check = checker.check(initField, pieceList, candidate, height, maxDepth);
            assertThat(possiblePieces.contains(blocks)).as(pieceList.toString()).isEqualTo(check);
        });
    }
}
Also used : Action(common.datastore.action.Action) LockedCandidate(core.action.candidate.LockedCandidate) Field(core.field.Field) ColumnSmallField(core.column_field.ColumnSmallField) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) InOutPairField(searcher.pack.InOutPairField) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) PerfectValidator(searcher.common.validator.PerfectValidator) BuildUpStream(common.buildup.BuildUpStream) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) HashSet(java.util.HashSet) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) FilterOnDemandBasicSolutions(searcher.pack.solutions.FilterOnDemandBasicSolutions) MappedBasicSolutions(searcher.pack.solutions.MappedBasicSolutions) BasicSolutions(searcher.pack.calculator.BasicSolutions) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Randoms(lib.Randoms) CheckerNoHold(searcher.checker.CheckerNoHold) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) LockedReachable(core.action.reachable.LockedReachable)

Example 82 with Randoms

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

the class DeepdropCandidateTest method testRandom.

@Test
void testRandom() throws Exception {
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new PassedMinoShifter();
    DeepdropCandidate candidate = new DeepdropCandidate(minoFactory, minoShifter);
    for (int count = 0; count < 10000; count++) {
        int height = randoms.nextIntClosed(2, 12);
        int numOfMinos = randoms.nextIntClosed(4, height * 10 / 4 - 1);
        Field field = randoms.field(height, numOfMinos);
        height -= field.clearLine();
        Piece piece = randoms.block();
        Set<Action> actions = candidate.search(field, piece, height);
        for (Rotate rotate : Rotate.values()) {
            Coordinates.walk(minoFactory.create(piece, rotate), height).map(coordinate -> MinimalAction.create(coordinate.x, coordinate.y, rotate)).forEach(action -> {
                int x = action.getX();
                int y = action.getY();
                Mino mino = minoFactory.create(piece, action.getRotate());
                if (actions.contains(action)) {
                    // おける
                    assertThat(field.canPut(mino, x, y)).isTrue();
                    assertThat(field.isOnGround(mino, x, y)).isTrue();
                } else {
                    // おけない
                    assertThat(field.canPut(mino, x, y) && field.isOnGround(mino, x, y)).isFalse();
                }
            });
        }
    }
}
Also used : Action(common.datastore.action.Action) MinimalAction(common.datastore.action.MinimalAction) Test(org.junit.jupiter.api.Test) Randoms(lib.Randoms) Field(core.field.Field) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) core.mino(core.mino) Set(java.util.Set) FieldFactory(core.field.FieldFactory) Coordinates(lib.Coordinates) Rotate(core.srs.Rotate) Action(common.datastore.action.Action) MinimalAction(common.datastore.action.MinimalAction) Rotate(core.srs.Rotate) Field(core.field.Field) Randoms(lib.Randoms) Test(org.junit.jupiter.api.Test)

Example 83 with Randoms

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

the class LimitIterationCandidateTest method testRandomLocked.

@Disabled
@Test
@LongTest
// TODO: mesure time, 移動回数をチェックしていないためテストに失敗することがある
void testRandomLocked() {
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    LimitIterationCandidate limitIterationCandidate = new LimitIterationCandidate(minoFactory, minoShifter, minoRotation, 12);
    for (int count = 0; count < 10; count++) {
        int randomHeight = randoms.nextIntClosed(10, 12);
        int numOfMinos = randoms.nextIntClosed(4, randomHeight * 10 / 4 - 1);
        Field field = randoms.field(randomHeight, numOfMinos);
        int clearLine = field.clearLine();
        int height = randomHeight - clearLine;
        Piece piece = randoms.block();
        String description = FieldView.toString(field, height) + piece;
        Set<Action> actions1 = limitIterationCandidate.search(field, piece, height);
        LockedCandidate lockedCandidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
        Set<Action> actions2 = lockedCandidate.search(field, piece, height);
        assertThat(actions2).as(description).isEqualTo(actions1);
    }
}
Also used : MinoRotation(core.srs.MinoRotation) Field(core.field.Field) Randoms(lib.Randoms) MinimalAction(common.datastore.action.MinimalAction) Action(common.datastore.action.Action) Piece(core.mino.Piece) MinoFactory(core.mino.MinoFactory) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest) Disabled(org.junit.jupiter.api.Disabled)

Example 84 with Randoms

use of lib.Randoms 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)

Example 85 with Randoms

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

the class LockedNeighborReachableTest method randoms.

@Test
void randoms() {
    int maxClearLine = 4;
    Injector injector = Guice.createInjector(new BasicModule(maxClearLine));
    LockedReachable reachable1 = createLockedReachable(injector, maxClearLine);
    LockedNeighborReachable reachable2 = createLockedNeighborReachable(injector, maxClearLine);
    OriginalPieceFactory pieceFactory = injector.getInstance(OriginalPieceFactory.class);
    Set<OriginalPiece> pieces = pieceFactory.create();
    Randoms randoms = new Randoms();
    for (int count = 0; count < 10000; count++) {
        Field field = randoms.field(4, 5);
        for (OriginalPiece piece : pieces) {
            // フィールドにそもそも置けないときは前提条件が合わないためスキップ
            if (!field.canPut(piece))
                continue;
            Mino mino = piece.getMino();
            int x = piece.getX();
            int y = piece.getY();
            boolean checks1 = reachable1.checks(field, mino, x, y, maxClearLine);
            boolean checks2 = reachable2.checks(field, mino, x, y, maxClearLine);
            assertThat(checks2).as(piece.toString()).isEqualTo(checks1);
        }
    }
}
Also used : Field(core.field.Field) Randoms(lib.Randoms) Injector(com.google.inject.Injector) BasicModule(module.BasicModule) Mino(core.mino.Mino) OriginalPiece(core.neighbor.OriginalPiece) OriginalPieceFactory(core.neighbor.OriginalPieceFactory) Test(org.junit.jupiter.api.Test)

Aggregations

Randoms (lib.Randoms)108 Test (org.junit.jupiter.api.Test)103 Piece (core.mino.Piece)58 LongTest (module.LongTest)32 Field (core.field.Field)29 Action (common.datastore.action.Action)25 LongPieces (common.datastore.blocks.LongPieces)24 MinoFactory (core.mino.MinoFactory)20 ArrayList (java.util.ArrayList)19 LockedCandidate (core.action.candidate.LockedCandidate)17 Mino (core.mino.Mino)16 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)16 MinoShifter (core.mino.MinoShifter)14 MinoRotation (core.srs.MinoRotation)14 Rotate (core.srs.Rotate)13 Collectors (java.util.stream.Collectors)13 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)12 PatternGenerator (common.pattern.PatternGenerator)12 List (java.util.List)12 SeparableMino (searcher.pack.separable_mino.SeparableMino)12