use of common.buildup.BuildUpStream in project solution-finder by knewjade.
the class TetfuTest method random.
@Test
@LongTest
void random() throws Exception {
// Initialize
Randoms randoms = new Randoms();
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
ColorConverter colorConverter = new ColorConverter();
// 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 < 20; count++) {
System.out.println(count);
// Create field
int numOfMinos = randoms.nextIntClosed(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();
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<TetfuElement> elements = operations.getOperations().stream().map(operation -> {
ColorType colorType = colorConverter.parseToColorType(operation.getPiece());
Rotate rotate = operation.getRotate();
int x = operation.getX();
int y = operation.getY();
String comment = randoms.string() + randoms.string() + randoms.string();
return new TetfuElement(colorType, rotate, x, y, comment);
}).collect(Collectors.toList());
String encode = new Tetfu(minoFactory, colorConverter).encode(elements);
List<TetfuPage> decode = decodeTetfu(minoFactory, colorConverter, encode);
assertThat(decode).hasSize(elements.size());
for (int index = 0; index < decode.size(); index++) {
TetfuElement element = elements.get(index);
assertThat(decode.get(index)).returns(element.getColorType(), TetfuPage::getColorType).returns(element.getRotate(), TetfuPage::getRotate).returns(element.getX(), TetfuPage::getX).returns(element.getY(), TetfuPage::getY).returns(element.getComment(), TetfuPage::getComment);
}
});
});
}
}
use of common.buildup.BuildUpStream in project solution-finder by knewjade.
the class Task method call.
@Override
public Pair<Pieces, Boolean> call() throws Exception {
List<Piece> pieceList = target.getPieces();
// すでに探索済みならそのまま結果を追加
int succeed = obj.visitedTree.isSucceed(pieceList);
if (succeed != VisitedTree.NO_RESULT)
return new Pair<>(target, succeed == VisitedTree.SUCCEED);
// 探索準備
Checker<Action> checker = commonObj.checkerThreadLocal.get();
Candidate<Action> candidate = commonObj.candidateThreadLocal.get();
// 探索
boolean checkResult = checker.check(obj.field, target, candidate, obj.maxClearLine, obj.maxDepth);
obj.visitedTree.set(checkResult, pieceList);
// パフェが見つかったツモ順(≠探索時のツモ順)へと、ホールドを使ってできるパターンを逆算
if (checkResult) {
Result result = checker.getResult();
Operations operations = new Operations(ResultHelper.createOperationStream(result));
List<MinoOperationWithKey> operationWithKeys = OperationTransform.parseToOperationWithKeys(obj.field, operations, commonObj.minoFactory, obj.maxClearLine);
BuildUpStream buildUpStream = new BuildUpStream(commonObj.reachableThreadLocal.get(), obj.maxClearLine);
buildUpStream.existsValidBuildPattern(obj.field, operationWithKeys).forEach(minoOperationWithKeys -> {
List<Piece> pieces = minoOperationWithKeys.stream().map(OperationWithKey::getPiece).collect(Collectors.toList());
obj.visitedTree.set(true, pieces);
});
}
return new Pair<>(target, checkResult);
}
use of common.buildup.BuildUpStream in project solution-finder by knewjade.
the class LockedBuildUpListUpThreadLocal method initialValue.
@Override
protected BuildUpStream initialValue() {
MinoFactory minoFactory = new MinoFactory();
MinoShifter minoShifter = new MinoShifter();
MinoRotation minoRotation = new MinoRotation();
LockedReachable reachable = new LockedReachable(minoFactory, minoShifter, minoRotation, height);
return new BuildUpStream(reachable, height);
}
use of common.buildup.BuildUpStream in project solution-finder by knewjade.
the class PathCore method run.
List<PathPair> run(Field field, SizedBit sizedBit) throws ExecutionException, InterruptedException {
List<Result> candidates = searcher.toList();
int maxClearLine = sizedBit.getHeight();
return candidates.parallelStream().map(result -> {
LinkedList<MinoOperationWithKey> operations = result.getMemento().getSeparableMinoStream(sizedBit.getWidth()).map(SeparableMino::toMinoOperationWithKey).collect(Collectors.toCollection(LinkedList::new));
// 地形の中で組むことができるoperationsをすべてリスト化する
BuildUpStream buildUpStream2 = buildUpStreamThreadLocal.get();
List<List<MinoOperationWithKey>> validOperaions = buildUpStream2.existsValidBuildPatternDirectly(field, operations).collect(Collectors.toList());
// 地形の中で組むことができるものがないときはスキップ
if (validOperaions.isEmpty())
return PathPair.EMPTY_PAIR;
// 地形の中で組むことができるSetを作成
HashSet<LongPieces> piecesSolution = validOperaions.stream().map(operationWithKeys -> operationWithKeys.stream().map(OperationWithKey::getPiece)).map(LongPieces::new).collect(Collectors.toCollection(HashSet::new));
// 探索シーケンスの中で組むことができるSetを作成
HashSet<LongPieces> piecesPattern = getPiecesPattern(piecesSolution);
// 探索シーケンスの中で組むことができるものがないときはスキップ
if (piecesPattern.isEmpty())
return PathPair.EMPTY_PAIR;
// 探索シーケンスの中でテト譜にするoperationsを選択する
List<MinoOperationWithKey> operationsToUrl = validOperaions.stream().filter(o -> validPieces.contains(new LongPieces(o.stream().map(Operation::getPiece)))).findFirst().orElse(Collections.emptyList());
// 譜面の作成
String fumen = fumenParser.parse(operationsToUrl, field, maxClearLine);
return new PathPair(result, piecesSolution, piecesPattern, fumen, new ArrayList<>(operationsToUrl), validPieces);
}).filter(pathPair -> pathPair != PathPair.EMPTY_PAIR).collect(Collectors.toList());
}
use of common.buildup.BuildUpStream in project solution-finder by knewjade.
the class PathEntryPoint method createPathCore.
private PathCore createPathCore(Field field, int maxClearLine, int maxDepth, List<String> patterns, MinoFactory minoFactory, ColorConverter colorConverter, SizedBit sizedBit, SolutionFilter solutionFilter, boolean isUsingHold, BasicSolutions basicSolutions, int threadCount) throws FinderInitializeException, FinderExecuteException {
assert 1 <= threadCount;
List<InOutPairField> inOutPairFields = InOutPairField.createInOutPairFields(sizedBit, field);
TaskResultHelper taskResultHelper = createTaskResultHelper(maxClearLine);
PerfectPackSearcher searcher = new PerfectPackSearcher(inOutPairFields, basicSolutions, sizedBit, solutionFilter, taskResultHelper, threadCount != 1);
FumenParser fumenParser = createFumenParser(settings.isTetfuSplit(), minoFactory, colorConverter);
ThreadLocal<BuildUpStream> threadLocalBuildUpStream = createBuildUpStreamThreadLocal(settings.getDropType(), maxClearLine);
try {
return new PathCore(patterns, searcher, maxDepth, isUsingHold, fumenParser, threadLocalBuildUpStream);
} catch (SyntaxException e) {
output("Pattern syntax error");
output(e.getMessage());
throw new FinderInitializeException("Pattern syntax error", e);
}
}
Aggregations