distance
Thedistance filter is a numeric filter that takes two position filters enclosed in parentheses as arguments. It returns the number of moves in the game tree that separate the positions represented by these arguments:
distance(x y)
The value distance(x y) can be computed as follows:
- If either the filter
xor the filterydoes not match the position, then neither does thedistancefilter - otherwise, let L be the LCA of
xandy. Then the value is the sum ofdistance(x L)anddistance(y L)
Note that:
- if
x==ythendistance(x y)==0 - if
xis an ancestor ofythen their distance is the number of moves between them.
In CQL, this could be written as a function:
function(x y){
L= LCA(x y)
(x:ply - L:ply) + (y:ply - L:ply)}
This works because x:ply is the value of the ply filter at the position x, which is the same as saying the ply value of position x.
Don't confuse distance with the Euclidean distance between two squares on the chessboard. The concepts aren't related.
Examples
Thedistance filter is mainly used in echo filters , which looks for pairs of positions that have certain similarities. Often we want to make sure the
positions are not "too close" in the game tree, when the resulting patterns is boring. Sometimes we want to sort by this distance as well, since how far apart
they are.
distance is used in with the echo filters in fatamorgana.cql, queenpawnpinecho.cql, and underpromotionecho.cql in this way.