use of lib.Stopwatch in project solution-finder by knewjade.
the class Main3 method main.
public static void main(String[] args) {
Field field = FieldFactory.createField("" + "X_______XX" + "X_______XX" + "X_______XX" + "X_______XX");
int maxClearLine = 4;
Field verifyField = field.freeze(maxClearLine);
System.out.println(FieldView.toString(field, maxClearLine));
Stopwatch stopwatch = Stopwatch.createStoppedStopwatch();
stopwatch.start();
Set<List<Piece>> sets = new HashSet<>();
List<Piece> allPieces = Piece.valueList();
List<Piece> pieces = new ArrayList<>(allPieces);
int popCount = (maxClearLine * 10 - field.getNumOfAllBlocks()) / 4;
CombinationIterable<Piece> combinationIterable = new CombinationIterable<>(pieces, popCount);
for (List<Piece> pieceList : combinationIterable) {
pieceList.sort(Comparator.comparingInt(allPieces::indexOf));
sets.add(pieceList);
}
TreeSet<Obj> allObjSet = new TreeSet<>();
int counter = 0;
for (List<Piece> usedPieces : sets) {
counter++;
System.out.println(usedPieces);
System.out.println(counter + " / " + sets.size());
List<List<MinoOperationWithKey>> operationsWithKey = search(usedPieces, field, maxClearLine, verifyField);
List<Obj> objs = operationsWithKey.stream().map(operationWithKeys -> {
boolean isDeleted = false;
BlockField blockField = new BlockField(maxClearLine);
for (MinoOperationWithKey key : operationWithKeys) {
Field test = FieldFactory.createField(maxClearLine);
Mino mino = key.getMino();
test.put(mino, key.getX(), key.getY());
test.insertWhiteLineWithKey(key.getNeedDeletedKey());
blockField.merge(test, mino.getPiece());
if (key.getNeedDeletedKey() != 0L)
isDeleted = true;
}
return new Obj(usedPieces, blockField, isDeleted, operationWithKeys);
}).collect(Collectors.toList());
System.out.println(objs.size());
objs.stream().map(obj -> OperationWithKeyInterpreter.parseToString(obj.operations)).sorted().sequential().forEach(System.out::println);
allObjSet.addAll(objs);
}
System.out.println(allObjSet.size());
System.exit(0);
ArrayList<Obj> allObjs = new ArrayList<>(allObjSet);
allObjs.sort(Main3::blockListComparator);
// System.out.println(operationsWithKey.size());
ColorConverter colorConverter = new ColorConverter();
System.out.println("<h3>各ミノ最大1つずつ & ライン消去なし</h3>");
Predicate<Obj> objPredicate = obj -> !obj.isDouble && !obj.isDeleted;
List<TetfuElement> oneNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate);
viewTetfu(oneNoDelete);
System.out.println("<h3>同一ミノを2つ利用 & ライン消去なし</h3>");
Predicate<Obj> objPredicate1 = obj -> obj.isDouble && !obj.isDeleted;
List<TetfuElement> doubleNoDelete = createTetfuElements(field, allObjs, colorConverter, objPredicate1);
viewTetfu(doubleNoDelete);
System.out.println("<h3>各ミノ最大1つずつ & ライン消去あり</h3>");
Predicate<Obj> objPredicate2 = obj -> !obj.isDouble && obj.isDeleted;
List<TetfuElement> oneDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate2);
viewTetfu(oneDeleted);
System.out.println("<h3>同一ミノを2つ利用 & ライン消去あり</h3>");
Predicate<Obj> objPredicate3 = obj -> obj.isDouble && obj.isDeleted;
List<TetfuElement> doubleDeleted = createTetfuElements(field, allObjs, colorConverter, objPredicate3);
viewTetfu(doubleDeleted);
// List<List<FullLimitedMino>> lists = Arrays.asList(
// singletonList(create(minoFactory, Piece.I, Rotate.Left, PositionLimit.OddX, 0L, 0, 3)),
// singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
// singletonList(create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.OddX, 0L, 2, 3)),
// singletonList(create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
// singletonList(create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.OddX, 0L, 0, 1)),
// singletonList(create(minoFactory, Piece.Z, Rotate.Spawn, PositionLimit.EvenX, 0L, 2, 3)),
// singletonList(create(minoFactory, Piece.S, Rotate.Left, PositionLimit.OddX, 1024L, 0, 3)),
// singletonList(create(minoFactory, Piece.Z, Rotate.Left, PositionLimit.EvenX, 1024L, 0, 3)),
// singletonList(create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.OddX, 0L, 0, 1)),
// singletonList(create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.EvenX, 0L, 2, 3))
// );
// CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField(maxClearLine), maxClearLine);
// List<List<OperationWithKey>> lists1 = crossBuilder.create();
// System.out.println(lists1.size());
// List<List<OperationWithKey>> search = new Search(FieldFactory.createField(4), Arrays.asList(
// create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
// create(minoFactory, Piece.O, Rotate.Spawn, PositionLimit.NoLimit, 0L, 2, 3),
// create(minoFactory, Piece.I, Rotate.Right, PositionLimit.NoLimit, 0L, 0, 3),
// create(minoFactory, Piece.L, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
// create(minoFactory, Piece.J, Rotate.Reverse, PositionLimit.NoLimit, 0L, 0, 1),
// create(minoFactory, Piece.T, Rotate.Spawn, PositionLimit.NoLimit, 0L, 0, 1),
// create(minoFactory, Piece.T, Rotate.Reverse, PositionLimit.NoLimit, 0L, 2, 3),
// create(minoFactory, Piece.Z, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3),
// create(minoFactory, Piece.S, Rotate.Right, PositionLimit.NoLimit, 0x400L, 0, 3)
// ), maxClearLine).search();
// System.out.println("--");
// for (List<OperationWithKey> operationWithKeys : search) {
// System.out.println(operationWithKeys);
// }
// MinoFactory minoFactory = new MinoFactory();
// List<List<FullLimitedMino>> lists = Arrays.asList(
// singletonList(create(minoFactory, Piece.J, Rotate.Right, PositionLimit.OddX, 0L, 0, 2)),
// singletonList(create(minoFactory, Piece.L, Rotate.Left, PositionLimit.EvenX, 1048576, 0, 4))
// );
// CrossBuilder crossBuilder = new CrossBuilder(lists, FieldFactory.createField("" +
// "__XXXXXXXX" +
// "__XXXXXXXX" +
// "__XXXXXXXX" +
// "__XXXXXXXX"
// ), maxClearLine);
// List<List<OperationWithKey>> lists1 = crossBuilder.create();
// for (List<OperationWithKey> operationWithKeys : lists1) {
// System.out.println(operationWithKeys);
// }
// System.out.println(lists1.size());
stopwatch.stop();
System.out.println(stopwatch.toMessage(TimeUnit.MILLISECONDS));
}
use of lib.Stopwatch in project solution-finder by knewjade.
the class CheckmateNoHoldReuseTest method randomCheckmateWithJustBlock.
@Test
@LongTest
void randomCheckmateWithJustBlock() {
Randoms randoms = new Randoms();
for (int count = 0; count < 100; count++) {
int maxClearLine = randoms.nextInt(3, 8);
int maxDepth = randoms.nextIntClosed(5, 7);
List<Piece> pieces = randoms.blocks(maxDepth);
Field field = randoms.field(maxClearLine, maxDepth);
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
Stopwatch stopwatchReuse = Stopwatch.createStoppedStopwatch();
for (int swap = 0; swap < 250; swap++) {
int index = randoms.nextInt(3, pieces.size());
Piece pop = pieces.remove(index);
pieces.add(pop);
stopwatchNoUse.start();
List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchNoUse.stop();
stopwatchReuse.start();
List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchReuse.stop();
assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
}
// 実行時間の比較がうまくいかないためskipする(戻り値のチェックのみ)
// assertThat(stopwatchReuse.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
}
}
use of lib.Stopwatch in project solution-finder by knewjade.
the class CheckmateNoHoldReuseTest method randomCheckmateWithJustBlockTwice.
@Test
void randomCheckmateWithJustBlockTwice() {
Randoms randoms = new Randoms();
for (int count = 0; count < 100; count++) {
int maxClearLine = randoms.nextInt(3, 8);
int maxDepth = randoms.nextIntClosed(5, 7);
List<Piece> pieces = randoms.blocks(maxDepth);
Field field = randoms.field(maxClearLine, maxDepth);
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
Stopwatch stopwatchReuse1 = Stopwatch.createStoppedStopwatch();
Stopwatch stopwatchReuse2 = Stopwatch.createStoppedStopwatch();
for (int swap = 0; swap < 250; swap++) {
int index = randoms.nextInt(3, pieces.size());
Piece pop = pieces.remove(index);
pieces.add(pop);
stopwatchNoUse.start();
List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchNoUse.stop();
stopwatchReuse1.start();
List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchReuse1.stop();
assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
stopwatchReuse2.start();
List<Result> result3 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchReuse2.stop();
assertThat(result3).hasSameSizeAs(result1).containsAll(result1);
}
// 実行時間の比較がうまくいかないためskipする(戻り値のチェックのみ)
// assertThat(stopwatchReuse1.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
// assertThat(stopwatchReuse2.getNanoAverageTime()).isLessThan(stopwatchReuse1.getNanoAverageTime());
}
}
use of lib.Stopwatch in project solution-finder by knewjade.
the class CheckmateUsingHoldReuseTest method randomCheckmateWithJustBlock.
@Test
@LongTest
void randomCheckmateWithJustBlock() {
Randoms randoms = new Randoms();
for (int count = 0; count < 25; count++) {
int maxClearLine = randoms.nextInt(3, 8);
int maxDepth = randoms.nextIntClosed(5, 7);
List<Piece> pieces = randoms.blocks(maxDepth);
Field field = randoms.field(maxClearLine, maxDepth);
Candidate<Action> candidate = new LockedCandidate(minoFactory, minoShifter, minoRotation, maxClearLine);
Stopwatch stopwatchNoUse = Stopwatch.createStoppedStopwatch();
Stopwatch stopwatchReuse = Stopwatch.createStoppedStopwatch();
for (int swap = 0; swap < 250; swap++) {
int index = randoms.nextInt(3, pieces.size());
Piece pop = pieces.remove(index);
pieces.add(pop);
stopwatchNoUse.start();
List<Result> result1 = checkmate.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchNoUse.stop();
stopwatchReuse.start();
List<Result> result2 = checkmateReuse.search(field, pieces, candidate, maxClearLine, maxDepth);
stopwatchReuse.stop();
assertThat(result2).hasSameSizeAs(result1).containsAll(result1);
}
// TODO:
// assertThat(stopwatchReuse.getNanoAverageTime()).isLessThan(stopwatchNoUse.getNanoAverageTime());
}
}
use of lib.Stopwatch in project solution-finder by knewjade.
the class LockedNeighborCandidateTest method random.
@Test
void random() {
Injector injector = Guice.createInjector(new BasicModule());
int maxClearLine = 3;
LockedCandidate candidate1 = createLockedCandidate(injector, maxClearLine);
LockedNeighborCandidate candidate2 = createLockedNeighborCandidate(injector, maxClearLine);
MinoShifter minoShifter = injector.getInstance(MinoShifter.class);
Stopwatch stopwatch1 = Stopwatch.createStartedStopwatch();
Stopwatch stopwatch2 = Stopwatch.createStartedStopwatch();
Randoms randoms = new Randoms();
for (int count = 0; count < 10000; count++) {
Field field = randoms.field(maxClearLine, 7);
for (Piece piece : Piece.values()) {
// LockedCandidate
stopwatch1.start();
Set<Action> search1 = candidate1.search(field, piece, maxClearLine);
stopwatch1.stop();
// LockedNeighborCandidate
stopwatch2.start();
Set<Neighbor> neighbors = candidate2.search(field, piece, maxClearLine);
stopwatch2.stop();
Set<Action> search2 = neighbors.stream().map(Neighbor::getPiece).map(this::createMinimalAction).map(action -> minoShifter.createTransformedAction(piece, action)).collect(Collectors.toSet());
assertThat(search2).isEqualTo(search1);
}
}
System.out.println(stopwatch1.toMessage(TimeUnit.NANOSECONDS));
System.out.println(stopwatch2.toMessage(TimeUnit.NANOSECONDS));
}
Aggregations