use of searcher.pack.SizedBit in project solution-finder by knewjade.
the class MinoPackingTaskWidthForWidth3 method compute.
@Override
public Stream<Result> compute() {
if (searcher.isFilled(innerField, index)) {
// innerFieldが埋まっている
List<InOutPairField> inOutPairFields = searcher.getInOutPairFields();
if (index == searcher.getLastIndex()) {
// 最後の計算
SizedBit sizedBit = searcher.getSizedBit();
ColumnField lastOuterField = inOutPairFields.get(index).getOuterField();
long innerFieldBoard = lastOuterField.getBoard(0) >> sizedBit.getMaxBitDigit();
MinoFieldMemento nextMemento = memento.skip();
return searcher.getTaskResultHelper().fixResult(searcher, innerFieldBoard, nextMemento);
} else {
// 途中の計算 // 自分で計算する
int nextIndex = index + 1;
ColumnField nextInnerField = inOutPairFields.get(nextIndex).getInnerField();
MinoFieldMemento nextMemento = memento.skip();
return createTask(searcher, nextInnerField, nextMemento, nextIndex).compute();
}
} else {
MinoFields minoFields = searcher.getSolutions(index).parse(innerField);
// innerFieldが埋まっていない
if (index == searcher.getLastIndex()) {
// 最後の計算
return minoFields.stream().parallel().flatMap(this::splitAndFixResult);
} else {
// 途中の計算
return minoFields.stream().parallel().map(this::split).filter(this::isValidTask).flatMap(PackingTask::compute);
}
}
}
use of searcher.pack.SizedBit in project solution-finder by knewjade.
the class MinoPackingTaskWidthForWidth3 method splitAndFixResult.
private Stream<Result> splitAndFixResult(MinoField minoField) {
ColumnField outerField = searcher.getInOutPairFields().get(index).getOuterField();
ColumnField minoOuterField = minoField.getOuterField();
// 注目範囲外outerで重なりがないか確認
if (outerField.canMerge(minoOuterField)) {
// 有効なおきかた
SizedBit sizedBit = searcher.getSizedBit();
ColumnField mergedOuterField = outerField.freeze(searcher.getSizedBit().getHeight());
mergedOuterField.merge(minoOuterField);
long innerFieldBoard = mergedOuterField.getBoard(0) >> sizedBit.getMaxBitDigit();
MinoFieldMemento nextMemento = memento.concat(minoField);
return searcher.getTaskResultHelper().fixResult(searcher, innerFieldBoard, nextMemento);
}
return Stream.empty();
}
use of searcher.pack.SizedBit 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);
}
use of searcher.pack.SizedBit 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);
}
use of searcher.pack.SizedBit in project solution-finder by knewjade.
the class MappedBasicSolutionsFactoryTest method create3x2.
@Test
void create3x2() throws Exception {
SizedBit sizedBit = new SizedBit(3, 2);
int expectedSolutions = 28;
int expectedSolutionItems = 88;
assertCache(sizedBit, expectedSolutions, expectedSolutionItems);
}
Aggregations