use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class CheckmateUsingHoldTest method assertResult.
private void assertResult(Result result, Field field, int maxClearLine, LockedReachable reachable, List<Piece> blocks) {
// Check blocks is same
List<Piece> resultPieces = parseToBlocks(result);
Piece lastHoldPiece = result.getLastHold();
HashSet<LongPieces> pieces = OrderLookup.reverseBlocks(resultPieces, blocks.size()).stream().map(StackOrder::toStream).map(stream -> stream.map(block -> block != null ? block : lastHoldPiece)).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
assertThat(pieces).contains(new LongPieces(blocks));
// Check can build result
Operations operations = parseToOperations(result);
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(field, operations, minoFactory, maxClearLine);
boolean cansBuild = BuildUp.cansBuild(field, operationWithKeys, maxClearLine, reachable);
assertThat(cansBuild).isTrue();
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class UseCSVPathOutput method output.
@Override
public void output(List<PathPair> pathPairs, Field field, SizedBit sizedBit) throws FinderExecuteException {
this.lastException = null;
outputLog("Found path = " + pathPairs.size());
AtomicInteger allCounter = new AtomicInteger();
Map<PieceCounter, List<PathPair>> groupingByClockCounter = pathPairs.parallelStream().collect(Collectors.groupingBy(pathPair -> {
List<OperationWithKey> operations = pathPair.getSampleOperations();
return new PieceCounter(operations.stream().map(OperationWithKey::getPiece));
}));
List<PathPair> emptyValidList = Collections.emptyList();
try (AsyncBufferedFileWriter writer = outputBaseFile.newAsyncWriter()) {
writer.writeAndNewLine("使用ミノ,対応地形数,対応ツモ数 (対パターン),テト譜,ツモ (対パターン)");
generator.blockCountersStream().parallel().map(blockCounter -> {
// カウンターをインクリメント
allCounter.incrementAndGet();
// 組み合わせ名を取得
String blockCounterName = blockCounter.getBlockStream().map(Piece::getName).collect(Collectors.joining());
// パフェ可能な地形を抽出
List<PathPair> valid = groupingByClockCounter.getOrDefault(blockCounter, emptyValidList);
// パフェ可能な地形数
int possibleSize = valid.size();
// パフェ可能な地形のテト譜を連結
String fumens = valid.stream().sorted(Comparator.comparing(PathPair::getPatternSize).reversed()).map(pathPair -> "v115@" + pathPair.getFumen()).collect(Collectors.joining(";"));
// 対応できるパターンを重複なく抽出
Set<LongPieces> possiblePatternSet = valid.stream().flatMap(PathPair::blocksStreamForPattern).collect(Collectors.toSet());
// 対応できるパターン数
int possiblePatternSize = possiblePatternSet.size();
// パターンを連結
String patterns = possiblePatternSet.stream().map(LongPieces::getPieces).map(blocks -> blocks.stream().map(Piece::getName).collect(Collectors.joining(""))).collect(Collectors.joining(";"));
return String.format("%s,%d,%d,%s,%s", blockCounterName, possibleSize, possiblePatternSize, fumens, patterns);
}).forEach(writer::writeAndNewLine);
writer.flush();
} catch (IOException e) {
throw new FinderExecuteException("Failed to output file", e);
}
outputLog("Found piece combinations = " + allCounter.get());
if (lastException != null)
throw new FinderExecuteException("Error to output file", lastException);
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PercentCore method run.
void run(Field field, Set<LongPieces> searchingPiecesSet, int maxClearLine, int maxDepth) throws FinderExecuteException {
List<Pieces> searchingPieces = new ArrayList<>(searchingPiecesSet);
this.resultPairs = invoker.search(field, searchingPieces, maxClearLine, maxDepth);
// 最低限の探索結果を集計する
this.resultTree = new AnalyzeTree();
for (Pair<Pieces, Boolean> resultPair : resultPairs) {
Pieces pieces = resultPair.getKey();
Boolean result = resultPair.getValue();
resultTree.set(result, pieces);
}
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PercentEntryPoint 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.maxClearLineUnder12(maxClearLine);
// Output field
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");
ExecutorService executorService = createExecutorService();
output("Version = " + FinderConstant.VERSION);
output("Necessary Pieces = " + maxDepth);
output();
// ========================================
// Holdができるときは必要なミノ分(maxDepth + 1)だけを取り出す。maxDepth + 1だけないときはブロックの個数をそのまま指定
output("# Enumerate pieces");
int piecesDepth = generator.getDepth();
int popCount = settings.isUsingHold() && maxDepth < piecesDepth ? maxDepth + 1 : maxDepth;
output("Piece pop count = " + popCount);
if (popCount < piecesDepth) {
output();
output("####################################################################");
output("WARNING: Inputted pieces is more than 'necessary blocks'.");
output(" Because reduce unnecessary pieces,");
output(" there is a possibility of getting no expected percentages.");
output("####################################################################");
output();
}
// 探索パターンの列挙
NormalEnumeratePieces normalEnumeratePieces = new NormalEnumeratePieces(generator, maxDepth, settings.isUsingHold());
Set<LongPieces> searchingPieces = normalEnumeratePieces.enumerate();
output("Searching pattern size (duplicate) = " + normalEnumeratePieces.getCounter());
output("Searching pattern size ( no dup. ) = " + searchingPieces.size());
output();
// ========================================
// 探索を行う
output("# Search");
output(" -> Stopwatch start");
Stopwatch stopwatch = Stopwatch.createStartedStopwatch();
ThreadLocal<Candidate<Action>> candidateThreadLocal = createCandidateThreadLocal(settings.getDropType(), maxClearLine);
ThreadLocal<? extends Reachable> reachableThreadLocal = createReachableThreadLocal(settings.getDropType(), maxClearLine);
MinoFactory minoFactory = new MinoFactory();
PercentCore percentCore = new PercentCore(executorService, candidateThreadLocal, settings.isUsingHold(), reachableThreadLocal, minoFactory);
percentCore.run(field, searchingPieces, maxClearLine, maxDepth);
AnalyzeTree tree = percentCore.getResultTree();
List<Pair<Pieces, Boolean>> resultPairs = percentCore.getResultPairs();
stopwatch.stop();
output(" -> Stopwatch stop : " + stopwatch.toMessage(TimeUnit.MILLISECONDS));
output();
// ========================================
// Output tree
output("# Output");
output(tree.show());
output();
// Output failed patterns
int treeDepth = settings.getTreeDepth();
if (piecesDepth < treeDepth)
treeDepth = piecesDepth;
output(String.format("Success pattern tree [Head %d pieces]:", treeDepth));
output(tree.tree(treeDepth));
output("-------------------");
int failedMaxCount = settings.getFailedCount();
// skip if failedMaxCount == 0
if (0 < failedMaxCount) {
output(String.format("Fail pattern (max. %d)", failedMaxCount));
List<Pair<Pieces, Boolean>> failedPairs = resultPairs.stream().filter(pair -> !pair.getValue()).limit(failedMaxCount).collect(Collectors.toList());
outputFailedPatterns(failedPairs);
} else if (failedMaxCount < 0) {
output("Fail pattern (all)");
List<Pair<Pieces, Boolean>> failedPairs = resultPairs.stream().filter(pair -> !pair.getValue()).collect(Collectors.toList());
outputFailedPatterns(failedPairs);
}
output();
// ========================================
output("# Finalize");
if (executorService != null)
executorService.shutdown();
output("done");
}
use of common.datastore.blocks.LongPieces in project solution-finder by knewjade.
the class PackSearcherComparingParityBasedTest method testAllSRSValidPacksHeight6.
// 高さ6: パリティベースとの探索結果を比較する (同一ミノは2つまで)
@Test
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);
}
Aggregations