use of common.datastore.blocks.Pieces in project solution-finder by knewjade.
the class PackSearcherComparingParityBasedOnDemandTest method testAllSRSValidPacksHeight6.
// 高さ6: パリティベースとの探索結果を比較する (同一ミノは2つまで)
@Test
@LongTest
void testAllSRSValidPacksHeight6() throws Exception {
int width = 2;
int height = 6;
String resultPath = ClassLoader.getSystemResource("perfects/pack_height6.txt").getPath();
List<TestData> testCases = Files.lines(Paths.get(resultPath)).map(line -> line.split("//")[0]).map(String::trim).filter(line -> !line.isEmpty()).map(line -> line.split("=")).map(split -> {
Stream<Piece> blocks = BlockInterpreter.parse(split[0]);
LongPieces pieces = new LongPieces(blocks);
int count = Integer.valueOf(split[1]);
return new TestData(pieces, count);
}).collect(Collectors.toList());
compareCount(width, height, testCases);
}
use of common.datastore.blocks.Pieces in project solution-finder by knewjade.
the class PackSearcherComparingParityBasedTest method testAllSRSValidPacksHeight4.
// 高さ4: パリティベースとの探索結果を比較する (同一ミノは2つまで)
@Test
void testAllSRSValidPacksHeight4() throws Exception {
int width = 3;
int height = 4;
String resultPath = ClassLoader.getSystemResource("perfects/pack_height4.txt").getPath();
List<TestData> testCases = Files.lines(Paths.get(resultPath)).map(line -> line.split("//")[0]).map(String::trim).filter(line -> !line.isEmpty()).map(line -> line.split("=")).map(split -> {
Stream<Piece> blocks = BlockInterpreter.parse(split[0]);
LongPieces pieces = new LongPieces(blocks);
int count = Integer.valueOf(split[1]);
return new TestData(pieces, count);
}).collect(Collectors.toList());
compareCount(width, height, testCases);
}
use of common.datastore.blocks.Pieces in project solution-finder by knewjade.
the class PackSearcherTest method assertHeight5.
void assertHeight5(SizedBit sizedBit, int maxCount, BiFunction<Field, SolutionFilter, BasicSolutions> basicSolutionSupplier) throws ExecutionException, InterruptedException, SyntaxException {
assert sizedBit.getWidth() == 2;
assert sizedBit.getHeight() == 5;
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 BasicMinoPackingHelper();
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 common.datastore.blocks.Pieces 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 common.datastore.blocks.Pieces in project solution-finder by knewjade.
the class ConcurrentVisitedTreeTest method randomLong.
@Test
@LongTest
void randomLong() throws SyntaxException {
Randoms randoms = new Randoms();
for (int size = 8; size <= 11; size++) {
PatternGenerator generator = new LoadedPatternGenerator("*p7, *p" + (size - 7));
ConcurrentVisitedTree tree = new ConcurrentVisitedTree();
Set<LongPieces> success = Collections.synchronizedSet(new HashSet<>());
Set<LongPieces> failed = Collections.synchronizedSet(new HashSet<>());
List<Pieces> piecesList = generator.blocksStream().collect(Collectors.toList());
piecesList.parallelStream().forEach(pieces -> {
boolean flag = randoms.nextBoolean();
List<Piece> blocks = pieces.getPieces();
tree.set(flag, blocks);
LongPieces longPieces = new LongPieces(blocks);
if (flag) {
success.add(longPieces);
} else {
failed.add(longPieces);
}
});
boolean isSucceed = success.stream().allMatch(pieces -> {
List<Piece> blocks = pieces.getPieces();
return tree.isSucceed(blocks) == ConcurrentVisitedTree.SUCCEED;
});
assertThat(isSucceed).isTrue();
boolean isFailed = failed.stream().allMatch(pieces -> {
List<Piece> blocks = pieces.getPieces();
return tree.isSucceed(blocks) == ConcurrentVisitedTree.FAILED;
});
assertThat(isFailed).isTrue();
}
}
Aggregations