module BishBosh.Search.Transpositions (
Transformation,
Transpositions(),
find,
insert
) where
import qualified BishBosh.Property.Empty as Property.Empty
import qualified BishBosh.Search.EphemeralData as Search.EphemeralData
import qualified BishBosh.Search.TranspositionValue as Search.TranspositionValue
import qualified Data.Map
import qualified Data.Maybe
newtype Transpositions move positionHash = MkTranspositions {
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct :: Data.Map.Map positionHash (Search.TranspositionValue.Value move)
}
instance Property.Empty.Empty (Transpositions move positionHash) where
empty :: Transpositions move positionHash
empty = Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions Map positionHash (Value move)
forall k a. Map k a
Data.Map.empty
instance Search.EphemeralData.EphemeralData (Transpositions move positionHash) where
getSize :: Transpositions move positionHash -> NPlies
getSize MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash } = Map positionHash (Value move) -> NPlies
forall k a. Map k a -> NPlies
Data.Map.size Map positionHash (Value move)
byPositionHash
euthanise :: NPlies
-> Transpositions move positionHash
-> Transpositions move positionHash
euthanise NPlies
nPlies MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash } = Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions (Map positionHash (Value move) -> Transpositions move positionHash)
-> Map positionHash (Value move)
-> Transpositions move positionHash
forall a b. (a -> b) -> a -> b
$ (Value move -> Bool)
-> Map positionHash (Value move) -> Map positionHash (Value move)
forall a k. (a -> Bool) -> Map k a -> Map k a
Data.Map.filter ((NPlies -> NPlies -> Bool
forall a. Ord a => a -> a -> Bool
> NPlies
nPlies) (NPlies -> Bool) -> (Value move -> NPlies) -> Value move -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value move -> NPlies
forall move. Value move -> NPlies
Search.TranspositionValue.getNPlies) Map positionHash (Value move)
byPositionHash
find
:: Ord positionHash
=> positionHash
-> Transpositions move positionHash
-> Maybe (Search.TranspositionValue.Value move)
find :: positionHash
-> Transpositions move positionHash -> Maybe (Value move)
find positionHash
positionHash MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash } = positionHash -> Map positionHash (Value move) -> Maybe (Value move)
forall k a. Ord k => k -> Map k a -> Maybe a
Data.Map.lookup positionHash
positionHash Map positionHash (Value move)
byPositionHash
type Transformation move positionHash = Transpositions move positionHash -> Transpositions move positionHash
insert
:: (Ord positionHash, Ord weightedMean)
=> Search.TranspositionValue.FindFitness move weightedMean
-> positionHash
-> Search.TranspositionValue.Value move
-> Transformation move positionHash
insert :: FindFitness move weightedMean
-> positionHash -> Value move -> Transformation move positionHash
insert FindFitness move weightedMean
findFitness positionHash
positionHash Value move
proposedValue MkTranspositions { deconstruct :: forall move positionHash.
Transpositions move positionHash -> Map positionHash (Value move)
deconstruct = Map positionHash (Value move)
byPositionHash } = Map positionHash (Value move) -> Transpositions move positionHash
forall move positionHash.
Map positionHash (Value move) -> Transpositions move positionHash
MkTranspositions (Map positionHash (Value move) -> Transpositions move positionHash)
-> Map positionHash (Value move)
-> Transpositions move positionHash
forall a b. (a -> b) -> a -> b
$ (Maybe (Value move) -> Maybe (Value move))
-> positionHash
-> Map positionHash (Value move)
-> Map positionHash (Value move)
forall k a.
Ord k =>
(Maybe a -> Maybe a) -> k -> Map k a -> Map k a
Data.Map.alter (
Maybe (Value move)
-> (Value move -> Maybe (Value move))
-> Maybe (Value move)
-> Maybe (Value move)
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe (Value move -> Maybe (Value move)
forall a. a -> Maybe a
Just Value move
proposedValue) ((Value move -> Maybe (Value move))
-> Maybe (Value move) -> Maybe (Value move))
-> (Value move -> Maybe (Value move))
-> Maybe (Value move)
-> Maybe (Value move)
forall a b. (a -> b) -> a -> b
$ \Value move
incumbentValue -> if FindFitness move weightedMean -> Value move -> Value move -> Bool
forall weightedMean move.
Ord weightedMean =>
FindFitness move weightedMean -> Value move -> Value move -> Bool
Search.TranspositionValue.isBetter FindFitness move weightedMean
findFitness Value move
proposedValue Value move
incumbentValue
then Value move -> Maybe (Value move)
forall a. a -> Maybe a
Just Value move
proposedValue
else Maybe (Value move)
forall a. Maybe a
Nothing
) positionHash
positionHash Map positionHash (Value move)
byPositionHash