use of core.column_field.ColumnField in project solution-finder by knewjade.
the class BuildUpTest method randomLongByPacking.
@Test
@LongTest
void randomLongByPacking() 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 < 100; count++) {
// Create field
int numOfMinos = randoms.nextIntClosed(7, 10);
Field field = randoms.field(height, numOfMinos);
// Search
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(basicWidth, height, field);
SolutionFilter solutionFilter = new AllPassedSolutionFilter();
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));
LockedReachable reachable = lockedReachableThreadLocal.get();
boolean exists = BuildUp.existsValidBuildPattern(field, operationWithKeys, height, reachable);
if (exists) {
// cansBuildでtrueとなるものがあることを確認
Optional<List<MinoOperationWithKey>> valid = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).filter(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get())).findAny();
assertThat(valid.isPresent()).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// checksKeyは必ずtrueとなる
assertThat(BuildUp.checksKey(operationWithKeys, 0L, height)).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずtrueになる
assert valid.isPresent();
List<MinoOperationWithKey> keys = valid.get();
List<Piece> pieces = keys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, keys.stream(), pieces, height, reachable)).isTrue();
} else {
// cansBuildですべてがfalseとなることを確認
boolean noneMatch = StreamSupport.stream(new PermutationIterable<>(operationWithKeys, operationWithKeys.size()).spliterator(), false).noneMatch(combination -> BuildUp.cansBuild(field, combination, height, lockedReachableThreadLocal.get()));
assertThat(noneMatch).as(FieldView.toString(field) + OperationWithKeyInterpreter.parseToString(operationWithKeys)).isTrue();
// existsValidByOrderは必ずfalseになる
List<Piece> pieces = operationWithKeys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
assertThat(BuildUp.existsValidByOrder(field, operationWithKeys.stream(), pieces, height, reachable)).isFalse();
}
});
}
System.out.println(counter);
}
use of core.column_field.ColumnField in project solution-finder by knewjade.
the class EasyPath method createMappedBasicSolutions.
private BasicSolutions createMappedBasicSolutions(SizedBit sizedBit) {
SeparableMinos separableMinos = SeparableMinos.createSeparableMinos(minoFactory, minoShifter, sizedBit);
BasicSolutionsCalculator calculator = new BasicSolutionsCalculator(separableMinos, sizedBit);
Map<ColumnField, RecursiveMinoFields> calculate = calculator.calculate();
return new MappedBasicSolutions(calculate);
}
use of core.column_field.ColumnField in project solution-finder by knewjade.
the class InOutPairField method createInnerFields.
private static List<ColumnField> createInnerFields(int width, int height, Field initField, int max) {
ArrayList<ColumnField> fields = new ArrayList<>();
Field field = initField.freeze(height);
for (int count = 0; count < max; count++) {
ColumnSmallField innerField = ColumnFieldFactory.createField();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (!field.isEmpty(x, y))
innerField.setBlock(x, y, height);
}
}
fields.add(innerField);
field.slideLeft(width);
}
return fields;
}
use of core.column_field.ColumnField in project solution-finder by knewjade.
the class PerfectPackSearcher method collect.
public <A, R> R collect(Collector<? super Result, A, R> callback) throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<R> submitTask = forkJoinPool.submit(() -> {
// Streamは終端操作を実行するまで実際には計算を行わない
// そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
// (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
Stream<Result> compute = toStream(task);
return compute.collect(callback);
});
// 結果を取得するまで待つ
R result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
use of core.column_field.ColumnField in project solution-finder by knewjade.
the class SetupPackSearcher method count.
public Long count() throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<Long> submitTask = forkJoinPool.submit(() -> {
return task.compute().parallel().count();
});
// 結果を取得するまで待つ
Long result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
Aggregations