use of lib.Randoms in project solution-finder by knewjade.
the class OrderLookupTest method forwardOverBlocksRandom.
@Test
void forwardOverBlocksRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 3; size <= 13; size++) {
List<Piece> pieceList = randoms.blocks(size);
int toDepth = pieceList.size() - 1;
PiecesNumberComparator comparator = new PiecesNumberComparator();
List<LongPieces> forward1 = OrderLookup.forwardBlocks(pieceList, toDepth).stream().map(StackOrder::toList).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
ForwardOrderLookUp lookUp = new ForwardOrderLookUp(toDepth, pieceList.size());
List<LongPieces> forward2 = lookUp.parse(pieceList).map(blockStream -> blockStream.collect(Collectors.toList())).map(LongPieces::new).sorted(comparator).collect(Collectors.toList());
assertThat(forward2).isEqualTo(forward1);
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class ReverseOrderLookUpTest method parseJustRandom.
@Test
void parseJustRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 13; size++) {
List<Piece> blocks = randoms.blocks(size);
int fromDepth = blocks.size();
ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(blocks.size(), fromDepth);
List<LongPieces> reverse = reverseOrderLookUp.parse(blocks).map(LongPieces::new).collect(Collectors.toList());
LongPieces target = new LongPieces(blocks);
ForwardOrderLookUp forwardOrderLookUp = new ForwardOrderLookUp(blocks.size(), fromDepth);
for (LongPieces pieces : reverse) {
boolean isFound = forwardOrderLookUp.parse(pieces.getPieces()).map(LongPieces::new).anyMatch(target::equals);
assertThat(isFound).isTrue();
}
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class ReverseOrderLookUpTest method parseOverRandom.
@Test
@LongTest
void parseOverRandom() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 13; size++) {
List<Piece> pieces = randoms.blocks(size);
int fromDepth = pieces.size() + 1;
ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(pieces.size(), fromDepth);
List<Stream<Piece>> reverse = reverseOrderLookUp.parse(pieces).collect(Collectors.toList());
LongPieces target = new LongPieces(pieces);
ForwardOrderLookUp forwardOrderLookUp = new ForwardOrderLookUp(pieces.size(), fromDepth);
for (Stream<Piece> stream : reverse) {
List<Piece> sample = stream.map(block -> block != null ? block : randoms.block()).collect(Collectors.toList());
boolean isFound = forwardOrderLookUp.parse(sample).map(LongPieces::new).anyMatch(target::equals);
assertThat(isFound).isTrue();
}
}
}
use of lib.Randoms in project solution-finder by knewjade.
the class ReverseOrderLookUpTest method parseOver2Random.
@Test
void parseOver2Random() throws Exception {
Randoms randoms = new Randoms();
for (int size = 2; size <= 12; size++) {
List<Piece> pieces = randoms.blocks(size);
int fromDepth = pieces.size() + 2;
ReverseOrderLookUp reverseOrderLookUp = new ReverseOrderLookUp(pieces.size(), fromDepth);
List<Stream<Piece>> reverse = reverseOrderLookUp.parse(pieces).collect(Collectors.toList());
LongPieces target = new LongPieces(pieces);
ForwardOrderLookUp forwardOrderLookUp = new ForwardOrderLookUp(pieces.size(), fromDepth);
for (Stream<Piece> stream : reverse) {
List<Piece> sample = stream.map(block -> block != null ? block : randoms.block()).collect(Collectors.toList());
boolean isFound = forwardOrderLookUp.parse(sample).map(LongPieces::new).anyMatch(target::equals);
assertThat(isFound).isTrue();
}
}
}
use of lib.Randoms 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);
});
});
}
}
Aggregations