Search in sources :

Example 46 with LongPieces

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

the class PackSearcherComparingParityBasedOnDemandTest method testAllSRSValidPacksHeight6.

// 高さ6: パリティベースとの探索結果を比較する (同一ミノは2つまで)
@Test
@LongTest
void testAllSRSValidPacksHeight6() throws Exception {
    int width = 2;
    int height = 6;
    String resultPath = ClassLoader.getSystemResource("perfects/pack_height6.txt").getPath();
    List<TestData> testCases = Files.lines(Paths.get(resultPath)).map(line -> line.split("//")[0]).map(String::trim).filter(line -> !line.isEmpty()).map(line -> line.split("=")).map(split -> {
        Stream<Piece> blocks = BlockInterpreter.parse(split[0]);
        LongPieces pieces = new LongPieces(blocks);
        int count = Integer.valueOf(split[1]);
        return new TestData(pieces, count);
    }).collect(Collectors.toList());
    compareCount(width, height, testCases);
}
Also used : Pieces(common.datastore.blocks.Pieces) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LongPieces(common.datastore.blocks.LongPieces) ColumnField(core.column_field.ColumnField) FilterWrappedBasicSolutions(searcher.pack.solutions.FilterWrappedBasicSolutions) HashSet(java.util.HashSet) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) CombinationIterable(common.iterable.CombinationIterable) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) InOutPairField(searcher.pack.InOutPairField) Set(java.util.Set) BlockInterpreter(common.parser.BlockInterpreter) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) UsingBlockAndValidKeySolutionFilter(searcher.pack.memento.UsingBlockAndValidKeySolutionFilter) ExecutionException(java.util.concurrent.ExecutionException) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) BasicSolutions(searcher.pack.calculator.BasicSolutions) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) Collections(java.util.Collections) LongPieces(common.datastore.blocks.LongPieces) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 47 with LongPieces

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

the class PackSearcherComparingParityBasedTest method testAllSRSValidPacksHeight4.

// 高さ4: パリティベースとの探索結果を比較する (同一ミノは2つまで)
@Test
void testAllSRSValidPacksHeight4() throws Exception {
    int width = 3;
    int height = 4;
    String resultPath = ClassLoader.getSystemResource("perfects/pack_height4.txt").getPath();
    List<TestData> testCases = Files.lines(Paths.get(resultPath)).map(line -> line.split("//")[0]).map(String::trim).filter(line -> !line.isEmpty()).map(line -> line.split("=")).map(split -> {
        Stream<Piece> blocks = BlockInterpreter.parse(split[0]);
        LongPieces pieces = new LongPieces(blocks);
        int count = Integer.valueOf(split[1]);
        return new TestData(pieces, count);
    }).collect(Collectors.toList());
    compareCount(width, height, testCases);
}
Also used : java.util(java.util) Pieces(common.datastore.blocks.Pieces) RecursiveMinoFields(searcher.pack.mino_fields.RecursiveMinoFields) BasicSolutionsCalculator(searcher.pack.solutions.BasicSolutionsCalculator) LongPieces(common.datastore.blocks.LongPieces) ColumnField(core.column_field.ColumnField) PieceCounter(common.datastore.PieceCounter) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) CombinationIterable(common.iterable.CombinationIterable) FieldFactory(core.field.FieldFactory) AssertionsForClassTypes.assertThat(org.assertj.core.api.AssertionsForClassTypes.assertThat) MinoShifter(core.mino.MinoShifter) MappedBasicSolutions(searcher.pack.solutions.MappedBasicSolutions) Piece(core.mino.Piece) Files(java.nio.file.Files) InOutPairField(searcher.pack.InOutPairField) BlockInterpreter(common.parser.BlockInterpreter) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) UsingBlockAndValidKeySolutionFilter(searcher.pack.memento.UsingBlockAndValidKeySolutionFilter) ExecutionException(java.util.concurrent.ExecutionException) SolutionFilter(searcher.pack.memento.SolutionFilter) Field(core.field.Field) BasicSolutions(searcher.pack.calculator.BasicSolutions) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongPieces(common.datastore.blocks.LongPieces) Stream(java.util.stream.Stream) Test(org.junit.jupiter.api.Test)

Example 48 with LongPieces

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

the class PackSearcherTest method assertHeight5.

void assertHeight5(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
    assert sizedBit.getWidth() == 2;
    assert sizedBit.getHeight() == 5;
    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 BasicMinoPackingHelper();
    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 49 with LongPieces

use of common.datastore.blocks.LongPieces 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 50 with LongPieces

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

the class AnalyzeTreeTest method randomLong.

@Test
@LongTest
void randomLong() throws SyntaxException {
    Randoms randoms = new Randoms();
    for (int size = 8; size <= 11; size++) {
        PatternGenerator generator = new LoadedPatternGenerator("*p7, *p" + (size - 7));
        AnalyzeTree tree = new AnalyzeTree();
        HashSet<LongPieces> success = new HashSet<>();
        HashSet<LongPieces> failed = new HashSet<>();
        generator.blocksStream().forEach(blocks -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> pieceList = blocks.getPieces();
            tree.set(flag, pieceList);
            LongPieces longPieces = new LongPieces(pieceList);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && tree.isSucceed(blocks);
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isVisited(blocks) && !tree.isSucceed(blocks);
        });
        assertThat(isFailed).isTrue();
        double percent = (double) success.size() / (success.size() + failed.size());
        assertThat(tree.getSuccessPercent()).isCloseTo(percent, offset(0.0001));
    }
}
Also used : Randoms(lib.Randoms) PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Aggregations

LongPieces (common.datastore.blocks.LongPieces)71 Test (org.junit.jupiter.api.Test)58 Piece (core.mino.Piece)42 PatternGenerator (common.pattern.PatternGenerator)38 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)36 Randoms (lib.Randoms)27 Collectors (java.util.stream.Collectors)22 Field (core.field.Field)19 Pieces (common.datastore.blocks.Pieces)16 LongTest (module.LongTest)16 List (java.util.List)14 Stream (java.util.stream.Stream)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)13 FieldFactory (core.field.FieldFactory)12 ArrayList (java.util.ArrayList)12 SizedBit (searcher.pack.SizedBit)12 MinoFactory (core.mino.MinoFactory)11 HashSet (java.util.HashSet)11 ExecutionException (java.util.concurrent.ExecutionException)10 BlockInterpreter (common.parser.BlockInterpreter)9