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);
}
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);
}
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);
}
});
});
}
}
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);
});
});
}
}
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());
}
Aggregations