use of lib.Randoms 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 lib.Randoms in project solution-finder by knewjade.
the class ColumnFieldComparatorTest method compare.
@Test
void compare() throws Exception {
Randoms randoms = new Randoms();
ColumnFieldComparator comparator = new ColumnFieldComparator();
for (int count = 0; count < 10000; count++) {
// same field
int height = randoms.nextInt(1, 10);
ColumnSmallField field1 = ColumnFieldFactory.createField();
ColumnSmallField field2 = ColumnFieldFactory.createField();
int maxBlock = randoms.nextInt(1, 15);
for (int block = 0; block < maxBlock; block++) {
int x = randoms.nextInt(10);
int y = randoms.nextInt(height);
field1.setBlock(x, y, height);
field2.setBlock(x, y, height);
}
assertThat(comparator.compare(field1, field2)).isEqualTo(0);
assertThat(comparator.compare(field2, field1)).isEqualTo(0);
// 1block different field
int x = randoms.nextInt(10);
int y = randoms.nextInt(height);
if (field1.isEmpty(x, y, height))
field1.setBlock(x, y, height);
else
field1.removeBlock(x, y, height);
// assert is not 0 & reversed sign
assertThat(comparator.compare(field1, field2) * comparator.compare(field2, field1)).isLessThan(0);
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class OperationWithKeyComparatorTest method compareDiffDeleteKey.
@Test
void compareDiffDeleteKey() throws Exception {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
int x = randoms.nextInt(10);
int y = randoms.nextInt(20);
long deleteKey = 0L;
long usingKey = 1049600L;
OperationWithKey operationWithKey1 = new FullOperationWithKey(minoFactory.create(Piece.I, Rotate.Spawn), x, y, deleteKey, usingKey);
Mino newMino = new MinoFactory().create(Piece.I, Rotate.Spawn);
Long choose = randoms.key();
while (choose == deleteKey) choose = randoms.key();
OperationWithKey operationWithKey2 = createNewOperationWithKey(newMino, x, y, choose, usingKey);
// assert is not 0 & sign reversed
OperationWithKeyComparator comparator = new OperationWithKeyComparator();
assertThat(comparator.compare(operationWithKey1, operationWithKey2) * comparator.compare(operationWithKey2, operationWithKey1)).as(operationWithKey2.toString()).isLessThan(0);
}
use of lib.Randoms in project solution-finder by knewjade.
the class OperationWithKeyComparatorTest method compare.
@Test
void compare() throws Exception {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
int x = randoms.nextInt(10);
int y = randoms.nextInt(20);
long deleteKey = 0L;
long usingKey = 1049600L;
OperationWithKey operationWithKey1 = new FullOperationWithKey(minoFactory.create(Piece.I, Rotate.Spawn), x, y, deleteKey, usingKey);
Mino newMino = new MinoFactory().create(Piece.I, Rotate.Spawn);
OperationWithKey operationWithKey2 = createNewOperationWithKey(newMino, x, y, deleteKey, usingKey);
OperationWithKeyComparator comparator = new OperationWithKeyComparator();
assertThat(comparator.compare(operationWithKey1, operationWithKey2)).as(operationWithKey1.toString()).isEqualTo(0);
assertThat(comparator.compare(operationWithKey2, operationWithKey1)).as(operationWithKey2.toString()).isEqualTo(0);
}
use of lib.Randoms in project solution-finder by knewjade.
the class OperationWithKeyComparatorTest method compareDiffUsingKey.
@Test
void compareDiffUsingKey() throws Exception {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
int x = randoms.nextInt(10);
int y = randoms.nextInt(20);
long deleteKey = 0L;
long usingKey = 1049600L;
OperationWithKey operationWithKey1 = new FullOperationWithKey(minoFactory.create(Piece.I, Rotate.Spawn), x, y, deleteKey, usingKey);
Mino newMino = new MinoFactory().create(Piece.I, Rotate.Spawn);
Long choose = randoms.key();
OperationWithKey operationWithKey2 = createNewOperationWithKey(newMino, x, y, deleteKey, choose);
// assert is 0
OperationWithKeyComparator<OperationWithKey> comparator = new OperationWithKeyComparator<>();
int compare1 = comparator.compare(operationWithKey1, operationWithKey2);
assertThat(compare1).as(operationWithKey1.toString()).isEqualTo(0);
int compare2 = comparator.compare(operationWithKey2, operationWithKey1);
assertThat(compare2).as(operationWithKey2.toString()).isEqualTo(0);
}
Aggregations