Prioritize computing nodes with few children, prioritize takes, move 1 second earlier to avoid exceeding the move time
parent
943aab1a14
commit
00f371b913
|
|
@ -98,10 +98,15 @@ class BestMoveSearcher {
|
|||
return value;
|
||||
}
|
||||
|
||||
static int getPositionBasePriority(Position position, int value, Iterable<(String, Position)> legalMoves, int depth) =>
|
||||
legalMoves.isEmpty
|
||||
? Numbers.maxInteger
|
||||
: 0;
|
||||
static int getPositionBasePriority(Position position, int value, Iterable<(String, Position)> legalMoves, int depth) {
|
||||
if (legalMoves.isEmpty) {
|
||||
return Numbers.maxInteger;
|
||||
}
|
||||
if (legalMoves.length < 10) {
|
||||
return (-2520 / legalMoves.length).toInt() >> 2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Future<String?> search(Position position, int? moveTime) async {
|
||||
_isRunning = true;
|
||||
|
|
@ -114,7 +119,7 @@ class BestMoveSearcher {
|
|||
while (!_isStopped && positionTree.priority < Numbers.maxInteger) {
|
||||
positionTree.computeStep();
|
||||
time = DateTime.now().millisecondsSinceEpoch - startTime;
|
||||
if (null != moveTime && time >= moveTime) {
|
||||
if (null != moveTime && time >= moveTime - 1000) {
|
||||
_isStopped = true;
|
||||
}
|
||||
if (positionTree.childrenByValue.isNotEmpty || bestValue != positionTree.value) {
|
||||
|
|
|
|||
|
|
@ -363,4 +363,11 @@ class Position {
|
|||
move.length > 4 ? move.substring(4).toLowerCase() : null,
|
||||
);
|
||||
|
||||
static bool isTake(Position position, String move) {
|
||||
final (int from, int to, String? promotingPiece) = moveStringToIndices(move);
|
||||
return
|
||||
(position.board[to] != Piece.emptySquare) ||
|
||||
(position.enPassantTargetSquare == to && (position.board[from] == Piece.whitePawn || position.board[from] == Piece.blackPawn));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -63,27 +63,30 @@ class PositionTreeNode {
|
|||
_computeChildren();
|
||||
return;
|
||||
}
|
||||
final PositionTreeNode firstChild = children.first;
|
||||
final int initialFirstChildNodeCount = firstChild.nodeCount;
|
||||
firstChild.computeStep();
|
||||
children.updateElement(firstChild.move);
|
||||
childrenByValue.updateElement(firstChild.move);
|
||||
nodeCount += firstChild.nodeCount - initialFirstChildNodeCount;
|
||||
if (firstChild.depth + 1 > depth) {
|
||||
depth = firstChild.depth + 1;
|
||||
final PositionTreeNode computedChild = children.first;
|
||||
final int initialFirstChildNodeCount = computedChild.nodeCount;
|
||||
computedChild.computeStep();
|
||||
children.updateElement(computedChild.move);
|
||||
childrenByValue.updateElement(computedChild.move);
|
||||
nodeCount += computedChild.nodeCount - initialFirstChildNodeCount;
|
||||
if (computedChild.depth + 1 > depth) {
|
||||
depth = computedChild.depth + 1;
|
||||
}
|
||||
priority = (children.first.priority == Numbers.maxInteger) ? children.first.priority : children.first.priority + 1;
|
||||
priority = (children.first.priority == Numbers.maxInteger) ? children.first.priority : children.first.priority + 100;
|
||||
value = childrenByValue.first.value;
|
||||
}
|
||||
|
||||
void _computeChildren() {
|
||||
for (final (String, Position) move in legalMoves) {
|
||||
final PositionTreeNode positionTreeNode = PositionTreeNode.fromPosition(move.$2, move.$1);
|
||||
if (Position.isTake(position, move.$1)) {
|
||||
positionTreeNode.priority -= 200;
|
||||
}
|
||||
children.add(positionTreeNode);
|
||||
childrenByValue.add(positionTreeNode);
|
||||
}
|
||||
nodeCount = legalMoves.length;
|
||||
priority = (children.first.priority == Numbers.maxInteger) ? children.first.priority : children.first.priority + 1;
|
||||
priority = (children.first.priority == Numbers.maxInteger) ? children.first.priority : children.first.priority + 100;
|
||||
depth = 1;
|
||||
value = childrenByValue.first.value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue