use of lib.Randoms in project solution-finder by knewjade.
the class PackSearcherTest method assertHeight4.
private void assertHeight4(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
assert sizedBit.getWidth() == 3;
assert sizedBit.getHeight() == 4;
int width = sizedBit.getWidth();
int height = sizedBit.getHeight();
Randoms randoms = new Randoms();
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
TaskResultHelper taskResultHelper = new Field4x10MinoPackingHelper();
for (int count = 0; count < maxCount; count++) {
// Field
int maxDepth = randoms.nextIntClosed(3, 6);
Field initField = randoms.field(height, maxDepth);
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, initField);
SolutionFilter solutionFilter = createSRSSolutionFilter(sizedBit, initField);
// Pack
BasicSolutions basicSolutions = basicSolutionSupplier.apply(initField, solutionFilter);
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper);
List<Result> results = searcher.toList();
// Possible
HashSet<Pieces> possiblePieces = new HashSet<>();
for (Result result : results) {
// result to possible pieces
List<MinoOperationWithKey> operationWithKeys = result.getMemento().getSeparableMinoStream(width).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toList());
Set<LongPieces> sets = new BuildUpStream(reachable, height).existsValidBuildPattern(initField, operationWithKeys).map(keys -> keys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toSet());
possiblePieces.addAll(sets);
}
// Checker
PerfectValidator validator = new PerfectValidator();
CheckerNoHold<Action> checker = new CheckerNoHold<>(minoFactory, validator);
// Assert generator
PatternGenerator generator = createPiecesGenerator(maxDepth);
generator.blocksStream().forEach(blocks -> {
List<Piece> pieceList = blocks.getPieces();
boolean check = checker.check(initField, pieceList, candidate, height, maxDepth);
assertThat(possiblePieces.contains(blocks)).as(pieceList.toString()).isEqualTo(check);
});
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class DeepdropCandidateTest method testRandom.
@Test
void testRandom() throws Exception {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new PassedMinoShifter();
DeepdropCandidate candidate = new DeepdropCandidate(minoFactory, minoShifter);
for (int count = 0; count < 10000; count++) {
int height = randoms.nextIntClosed(2, 12);
int numOfMinos = randoms.nextIntClosed(4, height * 10 / 4 - 1);
Field field = randoms.field(height, numOfMinos);
height -= field.clearLine();
Piece piece = randoms.block();
Set<Action> actions = candidate.search(field, piece, height);
for (Rotate rotate : Rotate.values()) {
Coordinates.walk(minoFactory.create(piece, rotate), height).map(coordinate -> MinimalAction.create(coordinate.x, coordinate.y, rotate)).forEach(action -> {
int x = action.getX();
int y = action.getY();
Mino mino = minoFactory.create(piece, action.getRotate());
if (actions.contains(action)) {
// おける
assertThat(field.canPut(mino, x, y)).isTrue();
assertThat(field.isOnGround(mino, x, y)).isTrue();
} else {
// おけない
assertThat(field.canPut(mino, x, y) && field.isOnGround(mino, x, y)).isFalse();
}
});
}
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class LimitIterationCandidateTest method testRandomLocked.
@Disabled
@Test
@LongTest
// TODO: mesure time, 移動回数をチェックしていないためテストに失敗することがある
void testRandomLocked() {
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
LimitIterationCandidate limitIterationCandidate = new LimitIterationCandidate(minoFactory, minoShifter, minoRotation, 12);
for (int count = 0; count < 10; count++) {
int randomHeight = randoms.nextIntClosed(10, 12);
int numOfMinos = randoms.nextIntClosed(4, randomHeight * 10 / 4 - 1);
Field field = randoms.field(randomHeight, numOfMinos);
int clearLine = field.clearLine();
int height = randomHeight - clearLine;
Piece piece = randoms.block();
String description = FieldView.toString(field, height) + piece;
Set<Action> actions1 = limitIterationCandidate.search(field, piece, height);
LockedCandidate lockedCandidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, height);
Set<Action> actions2 = lockedCandidate.search(field, piece, height);
assertThat(actions2).as(description).isEqualTo(actions1);
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class LockedNeighborCandidateTest method random.
@Test
void random() {
Injector injector = Guice.createInjector(new BasicModule());
int maxClearLine = 3;
LockedCandidate candidate1 = createLockedCandidate(injector, maxClearLine);
LockedNeighborCandidate candidate2 = createLockedNeighborCandidate(injector, maxClearLine);
MinoShifter minoShifter = injector.getInstance(MinoShifter.class);
Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
Stopwatch stopwatch2 = Stopwatch.createStartedStopwatch();
Randoms randoms = new Randoms();
for (int count = 0; count < 10000; count++) {
Field field = randoms.field(maxClearLine, 7);
for (Piece piece : Piece.values()) {
// LockedCandidate
stopwatch1.start();
Set<Action> search1 = candidate1.search(field, piece, maxClearLine);
stopwatch1.stop();
// LockedNeighborCandidate
stopwatch2.start();
Set<Neighbor> neighbors = candidate2.search(field, piece, maxClearLine);
stopwatch2.stop();
Set<Action> search2 = neighbors.stream().map(Neighbor::getPiece).map(this::createMinimalAction).map(action -> minoShifter.createTransformedAction(piece, action)).collect(Collectors.toSet());
assertThat(search2).isEqualTo(search1);
}
}
System.out.println(stopwatch1.toMessage(TimeUnit.NANOSECONDS));
System.out.println(stopwatch2.toMessage(TimeUnit.NANOSECONDS));
}
use of lib.Randoms in project solution-finder by knewjade.
the class LockedNeighborReachableTest method randoms.
@Test
void randoms() {
int maxClearLine = 4;
Injector injector = Guice.createInjector(new BasicModule(maxClearLine));
LockedReachable reachable1 = createLockedReachable(injector, maxClearLine);
LockedNeighborReachable reachable2 = createLockedNeighborReachable(injector, maxClearLine);
OriginalPieceFactory pieceFactory = injector.getInstance(OriginalPieceFactory.class);
Set<OriginalPiece> pieces = pieceFactory.create();
Randoms randoms = new Randoms();
for (int count = 0; count < 10000; count++) {
Field field = randoms.field(4, 5);
for (OriginalPiece piece : pieces) {
// フィールドにそもそも置けないときは前提条件が合わないためスキップ
if (!field.canPut(piece))
continue;
Mino mino = piece.getMino();
int x = piece.getX();
int y = piece.getY();
boolean checks1 = reachable1.checks(field, mino, x, y, maxClearLine);
boolean checks2 = reachable2.checks(field, mino, x, y, maxClearLine);
assertThat(checks2).as(piece.toString()).isEqualTo(checks1);
}
}
}
Aggregations