use of lib.Randoms in project solution-finder by knewjade.
the class CombinationIterableTest method iteratorRandom.
@Test
void iteratorRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 35; size++) {
int pop = randoms.nextInt(1, size <= 10 ? size : 10);
List<Integer> list = IntStream.range(0, size).boxed().collect(Collectors.toList());
CombinationIterable<Integer> iterable = new CombinationIterable<>(list, pop);
long actual = StreamSupport.stream(iterable.spliterator(), false).count();
long upper = 1L;
long bottom = 1L;
for (int count = 0; count < (pop < (size - pop) ? pop : size - pop); count++) {
upper *= size - count;
bottom *= count + 1;
}
assertThat(actual).as("size=%d, pop=%d", size, pop).isEqualTo(upper / bottom);
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class PermutationIterableTest method iteratorRandom.
@Test
void iteratorRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 15; size++) {
int pop = randoms.nextInt(1, size <= 8 ? size : 8);
List<Integer> list = IntStream.range(0, size).boxed().collect(Collectors.toList());
PermutationIterable<Integer> iterable = new PermutationIterable<>(list, pop);
long actual = StreamSupport.stream(iterable.spliterator(), false).count();
long upper = 1L;
for (int count = 0; count < pop; count++) upper *= size - count;
assertThat(actual).as("size=%d, pop=%d", size, pop).isEqualTo(upper);
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class ForwardOrderLookUpTest method parseJustBlocksRandom.
@Test
void parseJustBlocksRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 15; size++) {
List<Piece> pieces = randoms.blocks(size);
int toDepth = pieces.size();
ForwardOrderLookUp lookUp = new ForwardOrderLookUp(toDepth, pieces.size());
HashSet<LongPieces> forward = lookUp.parse(pieces).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
for (int count = 0; count < 10000; count++) {
ArrayList<Piece> sample = new ArrayList<>();
int holdIndex = 0;
for (int index = 1; index < size; index++) {
if (randoms.nextBoolean(0.3)) {
// そのまま追加
sample.add(pieces.get(index));
} else {
// ホールドを追加
sample.add(pieces.get(holdIndex));
holdIndex = index;
}
}
// ホールドを追加
sample.add(pieces.get(holdIndex));
assertThat(new LongPieces(sample)).isIn(forward);
}
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class BuildUpTest method cansBuildRandomLongByCheck.
@Test
@LongTest
void cansBuildRandomLongByCheck() {
Randoms randoms = new Randoms();
// Create field
int height = 4;
// Initialize
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
PerfectValidator validator = new PerfectValidator();
CheckerUsingHold<Action> checker = new CheckerUsingHold<>(minoFactory, validator);
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
AtomicInteger counter = new AtomicInteger();
for (int count = 0; count < 100; count++) {
// Pickup solution from checker
int numOfMinos = randoms.nextIntClosed(7, 10);
Field field = randoms.field(height, numOfMinos);
List<Piece> pieces = randoms.blocks(numOfMinos);
boolean check = checker.check(field, pieces, candidate, height, numOfMinos);
if (check) {
counter.incrementAndGet();
Stream<Operation> operationStream = ResultHelper.createOperationStream(checker.getResult());
Operations operations = new Operations(operationStream);
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, height);
assertThat(BuildUp.cansBuild(field, operationWithKeys, height, reachable)).as(FieldView.toString(field) + pieces).isTrue();
}
}
System.out.println(counter);
}
use of lib.Randoms in project solution-finder by knewjade.
the class BuildUpTest method randomShortByPacking.
@Test
void randomShortByPacking() 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 = 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);
}
Aggregations