Search in sources :

Example 1 with LockedReachableThreadLocal

use of concurrent.LockedReachableThreadLocal in project solution-finder by knewjade.

the class BuildUpStreamTest method randomLong.

@Test
@LongTest
void randomLong() throws ExecutionException, InterruptedException {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    // Define size
    int height = 4;
    int basicWidth = 3;
    SizedBit sizedBit = new SizedBit(basicWidth, height);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
    // Create basic solutions
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
    Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
    OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    AtomicInteger counter = new AtomicInteger();
    for (int count = 0; count < 10; count++) {
        // Create field
        int numOfMinos = randoms.nextIntClosed(7, 9);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = createRandomSolutionFilter(randoms, sizedBit, lockedReachableThreadLocal, field);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        // If found solution
        resultOptional.ifPresent(result -> {
            counter.incrementAndGet();
            LinkedList<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
            // Create Pieces
            LockedReachable reachable = lockedReachableThreadLocal.get();
            Set<List<MinoOperationWithKey>> valid = new BuildUpStream(reachable, height).existsValidBuildPattern(field, operationWithKeys).collect(Collectors.toSet());
            PermutationIterable<MinoOperationWithKey> permutations = new PermutationIterable<>(operationWithKeys, operationWithKeys.size());
            for (List<MinoOperationWithKey> permutation : permutations) {
                boolean canBuild = BuildUp.cansBuild(field, permutation, height, reachable);
                if (canBuild) {
                    assertThat(valid).as(FieldView.toString(field)).contains(permutation);
                } else {
                    assertThat(valid).as(FieldView.toString(field)).doesNotContain(permutation);
                }
            }
        });
    }
    System.out.println(counter);
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationInterpreter(common.parser.OperationInterpreter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) FieldView(core.field.FieldView) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SeparableMinos(searcher.pack.SeparableMinos) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) LinkedList(java.util.LinkedList) MinoRotation(core.srs.MinoRotation) LockedReachable(core.action.reachable.LockedReachable) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) OperationWithKey(common.datastore.OperationWithKey) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) PermutationIterable(common.iterable.PermutationIterable) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) InOutPairField(searcher.pack.InOutPairField) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) MinoFactory(core.mino.MinoFactory) LinkedList(java.util.LinkedList) List(java.util.List) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) PermutationIterable(common.iterable.PermutationIterable) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SizedBit(searcher.pack.SizedBit) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 2 with LockedReachableThreadLocal

use of concurrent.LockedReachableThreadLocal in project solution-finder by knewjade.

the class BuildUpStreamTest method randomShort.

@Test
void randomShort() throws ExecutionException, InterruptedException {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    // Define size
    int height = 4;
    int basicWidth = 3;
    SizedBit sizedBit = new SizedBit(basicWidth, height);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
    // Create basic solutions
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
    Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
    OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    AtomicInteger counter = new AtomicInteger();
    for (int count = 0; count < 10000; count++) {
        // Create field
        int numOfMinos = randoms.nextInt(1, 7);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = createRandomSolutionFilter(randoms, sizedBit, lockedReachableThreadLocal, field);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        // If found solution
        resultOptional.ifPresent(result -> {
            counter.incrementAndGet();
            LinkedList<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
            // Create Pieces
            LockedReachable reachable = lockedReachableThreadLocal.get();
            Set<List<MinoOperationWithKey>> valid = new BuildUpStream(reachable, height).existsValidBuildPattern(field, operationWithKeys).collect(Collectors.toSet());
            PermutationIterable<MinoOperationWithKey> permutations = new PermutationIterable<>(operationWithKeys, operationWithKeys.size());
            for (List<MinoOperationWithKey> permutation : permutations) {
                boolean canBuild = BuildUp.cansBuild(field, permutation, height, reachable);
                if (canBuild) {
                    assertThat(valid).as(FieldView.toString(field)).contains(permutation);
                } else {
                    assertThat(valid).as(FieldView.toString(field)).doesNotContain(permutation);
                }
            }
        });
    }
    System.out.println(counter);
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) OperationInterpreter(common.parser.OperationInterpreter) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) FieldView(core.field.FieldView) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SeparableMinos(searcher.pack.SeparableMinos) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) LinkedList(java.util.LinkedList) MinoRotation(core.srs.MinoRotation) LockedReachable(core.action.reachable.LockedReachable) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) OperationWithKey(common.datastore.OperationWithKey) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) PermutationIterable(common.iterable.PermutationIterable) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) InOutPairField(searcher.pack.InOutPairField) Set(java.util.Set) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) MinoFactory(core.mino.MinoFactory) LinkedList(java.util.LinkedList) List(java.util.List) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) PermutationIterable(common.iterable.PermutationIterable) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SizedBit(searcher.pack.SizedBit) AllPassedSolutionFilter(searcher.pack.memento.AllPassedSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) LockedReachable(core.action.reachable.LockedReachable) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest)

Example 3 with LockedReachableThreadLocal

use of concurrent.LockedReachableThreadLocal in project solution-finder by knewjade.

the class TetfuTest method random.

@Test
@LongTest
void random() throws Exception {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    ColorConverter colorConverter = new ColorConverter();
    // Define size
    int height = 4;
    int basicWidth = 3;
    SizedBit sizedBit = new SizedBit(basicWidth, height);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
    // Create basic solutions
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
    Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
    OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    for (int count = 0; count < 20; count++) {
        System.out.println(count);
        // Create field
        int numOfMinos = randoms.nextIntClosed(6, 10);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = new SRSValidSolutionFilter(field, lockedReachableThreadLocal, sizedBit);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        BuildUpStream buildUpStream = new BuildUpStream(lockedReachableThreadLocal.get(), height);
        // If found solution
        resultOptional.ifPresent(result -> {
            List<MinoOperationWithKey> list = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
            Optional<List<MinoOperationWithKey>> validOption = buildUpStream.existsValidBuildPattern(field, list).findAny();
            validOption.ifPresent(operationWithKeys -> {
                Operations operations = OperationTransform.parseToOperations(field, operationWithKeys, height);
                List<TetfuElement> elements = operations.getOperations().stream().map(operation -> {
                    ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
                    Rotate rotate = operation.getRotate();
                    int x = operation.getX();
                    int y = operation.getY();
                    String comment = randoms.string() + randoms.string() + randoms.string();
                    return new TetfuElement(colorType, rotate, x, y, comment);
                }).collect(Collectors.toList());
                String encode = new Tetfu(minoFactory, colorConverter).encode(elements);
                List<TetfuPage> decode = decodeTetfu(minoFactory, colorConverter, encode);
                assertThat(decode).hasSize(elements.size());
                for (int index = 0; index < decode.size(); index++) {
                    TetfuElement element = elements.get(index);
                    assertThat(decode.get(index)).returns(element.getColorType(), TetfuPage::getColorType).returns(element.getRotate(), TetfuPage::getRotate).returns(element.getX(), TetfuPage::getX).returns(element.getY(), TetfuPage::getY).returns(element.getComment(), TetfuPage::getComment);
                }
            });
        });
    }
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) Arrays(java.util.Arrays) ColorType(common.tetfu.common.ColorType) OperationTransform(common.parser.OperationTransform) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) FinderParseException(exceptions.FinderParseException) SizedBit(searcher.pack.SizedBit) ColoredFieldFactory(common.tetfu.field.ColoredFieldFactory) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) Tag(org.junit.jupiter.api.Tag) MinoRotation(core.srs.MinoRotation) BuildUpStream(common.buildup.BuildUpStream) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Piece(core.mino.Piece) L(core.mino.Piece.L) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) J(core.mino.Piece.J) InOutPairField(searcher.pack.InOutPairField) ColorConverter(common.tetfu.common.ColorConverter) Rotate(core.srs.Rotate) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) Collections(java.util.Collections) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) Rotate(core.srs.Rotate) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) ArrayColoredField(common.tetfu.field.ArrayColoredField) ColoredField(common.tetfu.field.ColoredField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) ColorConverter(common.tetfu.common.ColorConverter) ColorType(common.tetfu.common.ColorType) MinoFactory(core.mino.MinoFactory) List(java.util.List) BuildUpStream(common.buildup.BuildUpStream) Operations(common.datastore.Operations) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) SizedBit(searcher.pack.SizedBit) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 4 with LockedReachableThreadLocal

use of concurrent.LockedReachableThreadLocal in project solution-finder by knewjade.

the class OperationTransformTest method randomParse.

@Test
@LongTest
void randomParse() throws Exception {
    // Initialize
    Randoms randoms = new Randoms();
    MinoFactory minoFactory = new MinoFactory();
    MinoShifter minoShifter = new MinoShifter();
    MinoRotation minoRotation = new MinoRotation();
    // Define size
    int height = 4;
    int basicWidth = 3;
    SizedBit sizedBit = new SizedBit(basicWidth, height);
    SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
    // Create basic solutions
    TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
    LockedReachableThreadLocal lockedReachableThreadLocal = new LockedReachableThreadLocal(minoFactory, minoShifter, minoRotation, height);
    Predicate<ColumnField> memorizedPredicate = (columnField) -> true;
    OnDemandBasicSolutions basicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, memorizedPredicate);
    for (int count = 0; count < 100; count++) {
        // Create field
        int numOfMinos = randoms.nextInt(6, 10);
        Field field = randoms.field(height, numOfMinos);
        // Search
        List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
        SolutionFilter solutionFilter = new SRSValidSolutionFilter(field, lockedReachableThreadLocal, sizedBit);
        PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
        Optional<Result> resultOptional = searcher.findAny();
        OperationWithKeyComparator<MinoOperationWithKey> operationWithKeyComparator = new OperationWithKeyComparator<>();
        ListComparator<MinoOperationWithKey> comparator = new ListComparator<>(operationWithKeyComparator);
        BuildUpStream buildUpStream = new BuildUpStream(lockedReachableThreadLocal.get(), height);
        // If found solution
        resultOptional.ifPresent(result -> {
            List<MinoOperationWithKey> list = result.getMemento().getSeparableMinoStream(basicWidth).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
            Optional<List<MinoOperationWithKey>> validOption = buildUpStream.existsValidBuildPattern(field, list).findAny();
            validOption.ifPresent(operationWithKeys -> {
                Operations operations = OperationTransform.parseToOperations(field, operationWithKeys, height);
                List<MinoOperationWithKey> actual = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, height);
                assertThat(comparator.compare(operationWithKeys, actual)).as("%s%n%s%n %s", FieldView.toString(field, height), OperationWithKeyInterpreter.parseToString(operationWithKeys), OperationWithKeyInterpreter.parseToString(actual)).isEqualTo(0);
            });
        });
    }
}
Also used : TaskResultHelper(searcher.pack.task.TaskResultHelper) Randoms(lib.Randoms) ListComparator(lib.ListComparator) OperationWithKeyComparator(common.comparator.OperationWithKeyComparator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ColumnField(core.column_field.ColumnField) MinoOperationWithKey(common.datastore.MinoOperationWithKey) FieldView(core.field.FieldView) SizedBit(searcher.pack.SizedBit) MinoFactory(core.mino.MinoFactory) SeparableMinos(searcher.pack.SeparableMinos) FieldFactory(core.field.FieldFactory) Tag(org.junit.jupiter.api.Tag) MinoRotation(core.srs.MinoRotation) BuildUpStream(common.buildup.BuildUpStream) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) MinoShifter(core.mino.MinoShifter) Predicate(java.util.function.Predicate) Result(searcher.pack.task.Result) InOutPairField(searcher.pack.InOutPairField) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Operations(common.datastore.Operations) SolutionFilter(searcher.pack.memento.SolutionFilter) List(java.util.List) Field(core.field.Field) SeparableMino(searcher.pack.separable_mino.SeparableMino) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) Optional(java.util.Optional) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) LongTest(module.LongTest) OnDemandBasicSolutions(searcher.pack.solutions.OnDemandBasicSolutions) ListComparator(lib.ListComparator) TaskResultHelper(searcher.pack.task.TaskResultHelper) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) Result(searcher.pack.task.Result) ColumnField(core.column_field.ColumnField) InOutPairField(searcher.pack.InOutPairField) Field(core.field.Field) MinoOperationWithKey(common.datastore.MinoOperationWithKey) SeparableMinos(searcher.pack.SeparableMinos) InOutPairField(searcher.pack.InOutPairField) PerfectPackSearcher(searcher.pack.task.PerfectPackSearcher) OperationWithKeyComparator(common.comparator.OperationWithKeyComparator) MinoFactory(core.mino.MinoFactory) List(java.util.List) BuildUpStream(common.buildup.BuildUpStream) Operations(common.datastore.Operations) Field4x10MinoPackingHelper(searcher.pack.task.Field4x10MinoPackingHelper) ColumnField(core.column_field.ColumnField) MinoRotation(core.srs.MinoRotation) Randoms(lib.Randoms) SizedBit(searcher.pack.SizedBit) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) SolutionFilter(searcher.pack.memento.SolutionFilter) SRSValidSolutionFilter(searcher.pack.memento.SRSValidSolutionFilter) MinoShifter(core.mino.MinoShifter) Test(org.junit.jupiter.api.Test) LongTest(module.LongTest) LongTest(module.LongTest)

Example 5 with LockedReachableThreadLocal

use of concurrent.LockedReachableThreadLocal in project solution-finder by knewjade.

the class Main3 method search.

public static List<List<MinoOperationWithKey>> search(List<Piece> usedPieces, Field field, int maxClearLine, Field verifyField) {
    MinoFactory minoFactory = new MinoFactory();
    PositionLimitParser positionLimitParser = new PositionLimitParser(minoFactory, maxClearLine);
    LockedReachableThreadLocal threadLocal = new LockedReachableThreadLocal(maxClearLine);
    ParityField parityField = new ParityField(field);
    PieceCounter pieceCounter = new PieceCounter(usedPieces);
    ColumnParityLimitation limitation = new ColumnParityLimitation(pieceCounter, parityField, maxClearLine);
    return limitation.enumerate().parallelStream().map(EstimateBuilder::create).flatMap(Collection::stream).flatMap((List<DeltaLimitedMino> deltaLimitedMinos) -> {
        // 変換 DeltaLimitedMinos to FullLimitedMino
        List<List<FullLimitedMino>> collect = deltaLimitedMinos.stream().map(positionLimitParser::parse).collect(Collectors.toList());
        // 候補数が小さい順  // 同種のブロックを固めるため
        List<Piece> priority = collect.stream().sorted(Comparator.comparingInt(List::size)).map(fullLimitedMinos -> fullLimitedMinos.get(0).getMino().getPiece()).collect(Collectors.toList());
        // ソートする
        collect.sort((o1, o2) -> {
            int compare = Integer.compare(priority.indexOf(o1.get(0).getMino().getPiece()), priority.indexOf(o2.get(0).getMino().getPiece()));
            if (compare != 0)
                return compare;
            return -Integer.compare(o1.size(), o2.size());
        });
        return Stream.of(collect);
    }).limit(Long.MAX_VALUE).flatMap(sets -> new CrossBuilder(sets, field, maxClearLine).create().stream()).filter(operationWithKeys -> BuildUp.existsValidBuildPattern(verifyField, operationWithKeys, maxClearLine, threadLocal.get())).collect(Collectors.toList());
}
Also used : PositionLimitParser(_implements.parity_based_pack.step2.PositionLimitParser) 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) LockedReachableThreadLocal(concurrent.LockedReachableThreadLocal) ColumnParityLimitation(_implements.parity_based_pack.step1.ColumnParityLimitation) CrossBuilder(_implements.parity_based_pack.step3.CrossBuilder) Piece(core.mino.Piece) EstimateBuilder(_implements.parity_based_pack.step1.EstimateBuilder) MinoFactory(core.mino.MinoFactory)

Aggregations

LockedReachableThreadLocal (concurrent.LockedReachableThreadLocal)12 Field (core.field.Field)10 MinoFactory (core.mino.MinoFactory)10 Piece (core.mino.Piece)10 Collectors (java.util.stream.Collectors)9 ColumnField (core.column_field.ColumnField)7 FieldFactory (core.field.FieldFactory)7 MinoShifter (core.mino.MinoShifter)7 Predicate (java.util.function.Predicate)7 InOutPairField (searcher.pack.InOutPairField)7 SeparableMinos (searcher.pack.SeparableMinos)7 SizedBit (searcher.pack.SizedBit)7 SolutionFilter (searcher.pack.memento.SolutionFilter)7 SeparableMino (searcher.pack.separable_mino.SeparableMino)7 Field4x10MinoPackingHelper (searcher.pack.task.Field4x10MinoPackingHelper)7 PerfectPackSearcher (searcher.pack.task.PerfectPackSearcher)7 Result (searcher.pack.task.Result)7 TaskResultHelper (searcher.pack.task.TaskResultHelper)7 FieldView (core.field.FieldView)6 MinoRotation (core.srs.MinoRotation)6