Search in sources :

Example 16 with Pieces

use of common.datastore.blocks.Pieces 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 17 with Pieces

use of common.datastore.blocks.Pieces 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 18 with Pieces

use of common.datastore.blocks.Pieces 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 19 with Pieces

use of common.datastore.blocks.Pieces 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 20 with Pieces

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

the class ConcurrentVisitedTreeTest 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));
        ConcurrentVisitedTree tree = new ConcurrentVisitedTree();
        Set<LongPieces> success = Collections.synchronizedSet(new HashSet<>());
        Set<LongPieces> failed = Collections.synchronizedSet(new HashSet<>());
        List<Pieces> piecesList = generator.blocksStream().collect(Collectors.toList());
        piecesList.parallelStream().forEach(pieces -> {
            boolean flag = randoms.nextBoolean();
            List<Piece> blocks = pieces.getPieces();
            tree.set(flag, blocks);
            LongPieces longPieces = new LongPieces(blocks);
            if (flag) {
                success.add(longPieces);
            } else {
                failed.add(longPieces);
            }
        });
        boolean isSucceed = success.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.SUCCEED;
        });
        assertThat(isSucceed).isTrue();
        boolean isFailed = failed.stream().allMatch(pieces -> {
            List<Piece> blocks = pieces.getPieces();
            return tree.isSucceed(blocks) == ConcurrentVisitedTree.FAILED;
        });
        assertThat(isFailed).isTrue();
    }
}
Also used : PatternGenerator(common.pattern.PatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) LoadedPatternGenerator(common.pattern.LoadedPatternGenerator) Randoms(lib.Randoms) LongPieces(common.datastore.blocks.LongPieces) Piece(core.mino.Piece) Pieces(common.datastore.blocks.Pieces) LongPieces(common.datastore.blocks.LongPieces) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Aggregations

Pieces (common.datastore.blocks.Pieces)25 Field (core.field.Field)16 LongPieces (common.datastore.blocks.LongPieces)13 MinoFactory (core.mino.MinoFactory)12 Piece (core.mino.Piece)12 PatternGenerator (common.pattern.PatternGenerator)10 MinoShifter (core.mino.MinoShifter)10 ExecutionException (java.util.concurrent.ExecutionException)9 Test (org.junit.jupiter.api.Test)9 ColumnField (core.column_field.ColumnField)8 Collectors (java.util.stream.Collectors)8 LongTest (module.LongTest)8 PieceCounter (common.datastore.PieceCounter)7 BlockInterpreter (common.parser.BlockInterpreter)7 LoadedPatternGenerator (common.pattern.LoadedPatternGenerator)7 FieldFactory (core.field.FieldFactory)7 Files (java.nio.file.Files)7 Paths (java.nio.file.Paths)7 Stream (java.util.stream.Stream)7 SizedBit (searcher.pack.SizedBit)7