use of searcher.pack.calculator.BasicSolutions in project solution-finder by knewjade.
the class MappedBasicSolutionsTest method get3x3.
@Test
void get3x3() throws Exception {
SizedBit sizedBit = new SizedBit(3, 3);
SeparableMinos separableMinos = createSeparableMinos(sizedBit);
BasicSolutionsCalculator calculator = new BasicSolutionsCalculator(separableMinos, sizedBit);
Map<ColumnField, RecursiveMinoFields> calculate = calculator.calculate();
BasicSolutions solutions = new MappedBasicSolutions(calculate);
MinoFields minoFields = solutions.parse(ColumnFieldFactory.createField());
Stream<? extends MinoField> stream = minoFields.stream();
assertThat(stream.count()).isEqualTo(278L);
}
use of searcher.pack.calculator.BasicSolutions in project solution-finder by knewjade.
the class PackSearcherComparingParityBasedOnDemandTest method compareCount.
private void compareCount(int width, int height, List<TestData> testDataList) throws InterruptedException, ExecutionException {
SizedBit sizedBit = new SizedBit(width, height);
SeparableMinos separableMinos = createSeparableMinos(sizedBit);
Predicate<ColumnField> bitCountPredicate = BasicSolutions.createBitCountPredicate(1);
OnDemandBasicSolutions onDemandBasicSolutions = new OnDemandBasicSolutions(separableMinos, sizedBit, bitCountPredicate);
for (TestData data : testDataList) {
// 準備
List<Piece> usingPieces = data.getPieces();
int popCount = usingPieces.size();
Field initField = createSquareEmptyField(height, popCount);
// packで探索
Set<PieceCounter> pieceCounters = Collections.singleton(new PieceCounter(usingPieces));
SolutionFilter solutionFilter = createUsingBlockAndValidKeyMementoFilter(initField, sizedBit, pieceCounters);
BasicSolutions basicSolutions = new FilterWrappedBasicSolutions(onDemandBasicSolutions, solutionFilter);
long packCounter = calculateSRSValidCount(sizedBit, basicSolutions, initField, solutionFilter);
System.out.println(usingPieces);
assertThat(packCounter).isEqualTo(data.getCount());
}
}
use of searcher.pack.calculator.BasicSolutions 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 searcher.pack.calculator.BasicSolutions 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 searcher.pack.calculator.BasicSolutions in project solution-finder by knewjade.
the class PathEntryPoint method run.
@Override
public void run() throws FinderException {
output("# Setup Field");
// Setup field
Field field = settings.getField();
Verify.field(field);
// Setup max clear line
int maxClearLine = settings.getMaxClearLine();
Verify.maxClearLineUnder10(maxClearLine);
// Setup reserved blocks
BlockField reservedBlocks = settings.getReservedBlock();
if (settings.isReserved()) {
Verify.reservedBlocks(reservedBlocks);
for (int y = maxClearLine - 1; 0 <= y; y--) {
StringBuilder builder = new StringBuilder();
for (int x = 0; x < 10; x++) {
if (reservedBlocks.getBlock(x, y) != null)
builder.append(reservedBlocks.getBlock(x, y).getName());
else if (!field.isEmpty(x, y))
builder.append('X');
else
builder.append('_');
}
output(builder.toString());
}
} else {
output(FieldView.toString(field, maxClearLine));
}
// Setup max depth
// パフェに必要なミノ数
int maxDepth = Verify.maxDepth(field, maxClearLine);
output();
// ========================================
// Output user-defined
output("# Initialize / User-defined");
output("Max clear lines: " + maxClearLine);
output("Using hold: " + (settings.isUsingHold() ? "use" : "avoid"));
output("Drop: " + settings.getDropType().name().toLowerCase());
output("Searching patterns:");
// Setup patterns
List<String> patterns = settings.getPatterns();
PatternGenerator generator = Verify.patterns(patterns, maxDepth);
// Output patterns
for (String pattern : patterns) output(" " + pattern);
output();
// ========================================
// Setup core
output("# Initialize / System");
int threadCount = getThreadCount();
// Output system-defined
output("Version = " + FinderConstant.VERSION);
output("Threads = " + threadCount);
output("Need Pieces = " + maxDepth);
output();
// ========================================
// Initialize
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
ColorConverter colorConverter = new ColorConverter();
SizedBit sizedBit = decideSizedBitSolutionWidth(maxClearLine);
SolutionFilter solutionFilter = new ForPathSolutionFilter(generator, maxClearLine);
// Holdができるときは必要なミノ分(maxDepth + 1)だけを取り出す。maxDepth + 1だけないときはブロックの個数をそのまま指定
output("# Enumerate pieces");
boolean isUsingHold = settings.isUsingHold();
int piecesDepth = generator.getDepth();
output("Piece pop count = " + (isUsingHold && maxDepth < piecesDepth ? maxDepth + 1 : maxDepth));
output();
// ========================================
output("# Cache");
output(" -> Stopwatch start");
Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
BasicSolutions basicSolutions = calculateBasicSolutions(field, minoFactory, minoShifter, sizedBit, solutionFilter);
stopwatch1.stop();
output(" ... done");
output(" -> Stopwatch stop : " + stopwatch1.toMessage(TimeUnit.MILLISECONDS));
output();
// ========================================
output("# Search");
output(" -> Stopwatch start");
output(" ... searching");
Stopwatch stopwatch2 = Stopwatch.createStartedStopwatch();
PathCore pathCore = createPathCore(field, maxClearLine, maxDepth, patterns, minoFactory, colorConverter, sizedBit, solutionFilter, isUsingHold, basicSolutions, threadCount);
List<PathPair> pathPairs = run(pathCore, field, sizedBit, reservedBlocks);
stopwatch2.stop();
output(" ... done");
output(" -> Stopwatch stop : " + stopwatch2.toMessage(TimeUnit.MILLISECONDS));
output();
// ========================================
output("# Output file");
OutputType outputType = settings.getOutputType();
PathOutput pathOutput = createOutput(outputType, generator, maxDepth);
pathOutput.output(pathPairs, field, sizedBit);
output();
// ========================================
output("# Finalize");
output("done");
}
Aggregations