{-# LANGUAGE CPP, FlexibleContexts #-}
{-
	Copyright (C) 2018 Dr. Alistair Ward

	This file is part of BishBosh.

	BishBosh is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	BishBosh is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with BishBosh.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]

	* Quantifies the fitness of a game.

	* By measuring the fitness from the perspective of the player who just moved (rather than the next player to move),
	an automated player can test various /move/s & select the fittest.
-}

module BishBosh.Evaluation.Fitness(
-- * Types
-- * Constants
--	maximumDestinations,
	maximumDefended,
-- * Functions
	measurePieceSquareValue,
	measurePieceSquareValueIncrementally,
	measureValueOfMaterial,
--	measureValueOfMobility,
	measureValueOfCastlingPotential,
	measureValueOfDefence,
	measureValueOfDoubledPawns,
	measureValueOfIsolatedPawns,
	measureValueOfPassedPawns,
	evaluateFitness
) where

import			Control.Applicative((<|>))
import			Control.Arrow((&&&))
import			Data.Array.IArray((!))
import qualified	BishBosh.Attribute.LogicalColour			as Attribute.LogicalColour
import qualified	BishBosh.Attribute.MoveType				as Attribute.MoveType
import qualified	BishBosh.Cartesian.Abscissa				as Cartesian.Abscissa
import qualified	BishBosh.Cartesian.Coordinates				as Cartesian.Coordinates
import qualified	BishBosh.Cartesian.Ordinate				as Cartesian.Ordinate
import qualified	BishBosh.Component.Move					as Component.Move
import qualified	BishBosh.Component.Piece				as Component.Piece
import qualified	BishBosh.Component.PieceSquareByCoordinatesByRank	as Component.PieceSquareByCoordinatesByRank
import qualified	BishBosh.Component.QualifiedMove			as Component.QualifiedMove
import qualified	BishBosh.Component.Turn					as Component.Turn
import qualified	BishBosh.Input.CriteriaWeights				as Input.CriteriaWeights
import qualified	BishBosh.Input.EvaluationOptions			as Input.EvaluationOptions
import qualified	BishBosh.Input.RankValues				as Input.RankValues
import qualified	BishBosh.Metric.CriterionValue				as Metric.CriterionValue
import qualified	BishBosh.Metric.WeightedMeanAndCriterionValues		as Metric.WeightedMeanAndCriterionValues
import qualified	BishBosh.Model.Game					as Model.Game
import qualified	BishBosh.Property.Opposable				as Property.Opposable
import qualified	BishBosh.Rule.GameTerminationReason			as Rule.GameTerminationReason
import qualified	BishBosh.State.Board					as State.Board
import qualified	BishBosh.State.CastleableRooksByLogicalColour		as State.CastleableRooksByLogicalColour
import qualified	BishBosh.Type.Count					as Type.Count
import qualified	BishBosh.Type.Mass					as Type.Mass
import qualified	Control.Monad.Reader
import qualified	Data.Array.IArray
import qualified	Data.Foldable
import qualified	Data.List
import qualified	Data.Map.Strict						as Map
import qualified	Data.Maybe

#ifdef USE_UNBOXED_ARRAYS
import qualified	Data.Array.Unboxed
#endif

-- | Measures the piece-square value from the perspective of the last player to move.
measurePieceSquareValue :: (
#ifdef USE_UNBOXED_ARRAYS
	Data.Array.Unboxed.IArray	Data.Array.Unboxed.UArray pieceSquareValue,	-- Requires 'FlexibleContexts'. The unboxed representation of the array-element must be defined (& therefore must be of fixed size).
#endif
	Num				pieceSquareValue
 )
	=> Component.PieceSquareByCoordinatesByRank.PieceSquareByCoordinatesByRank pieceSquareValue
	-> Model.Game.Game
	-> pieceSquareValue
{-# SPECIALISE measurePieceSquareValue :: Component.PieceSquareByCoordinatesByRank.PieceSquareByCoordinatesByRank Type.Mass.PieceSquareValue -> Model.Game.Game -> Type.Mass.PieceSquareValue #-}
measurePieceSquareValue :: PieceSquareByCoordinatesByRank pieceSquareValue
-> Game -> pieceSquareValue
measurePieceSquareValue PieceSquareByCoordinatesByRank pieceSquareValue
pieceSquareByCoordinatesByRank Game
game	= (
	if LogicalColour -> Bool
Attribute.LogicalColour.isBlack (LogicalColour -> Bool) -> LogicalColour -> Bool
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game
		then pieceSquareValue -> pieceSquareValue
forall a. a -> a
id
		else pieceSquareValue -> pieceSquareValue
forall a. Num a => a -> a
negate	-- Represent the piece-square value from Black's perspective.
 ) (pieceSquareValue -> pieceSquareValue)
-> pieceSquareValue -> pieceSquareValue
forall a b. (a -> b) -> a -> b
$ pieceSquareValue
whitesPieceSquareValue pieceSquareValue -> pieceSquareValue -> pieceSquareValue
forall a. Num a => a -> a -> a
- pieceSquareValue
blacksPieceSquareValue where
	[pieceSquareValue
blacksPieceSquareValue, pieceSquareValue
whitesPieceSquareValue]	= Array LogicalColour pieceSquareValue -> [pieceSquareValue]
forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> [e]
Data.Array.IArray.elems (Array LogicalColour pieceSquareValue -> [pieceSquareValue])
-> (Board -> Array LogicalColour pieceSquareValue)
-> Board
-> [pieceSquareValue]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PieceSquareByCoordinatesByRank pieceSquareValue
-> Board -> Array LogicalColour pieceSquareValue
forall pieceSquareValue.
Num pieceSquareValue =>
PieceSquareByCoordinatesByRank pieceSquareValue
-> Board -> ArrayByLogicalColour pieceSquareValue
State.Board.sumPieceSquareValueByLogicalColour PieceSquareByCoordinatesByRank pieceSquareValue
pieceSquareByCoordinatesByRank (Board -> [pieceSquareValue]) -> Board -> [pieceSquareValue]
forall a b. (a -> b) -> a -> b
$ Game -> Board
Model.Game.getBoard Game
game

{- |
	* Measures the piece-square value from the perspective of the last player to move.

	* The previous value is provided, to enable calculation by difference.

	* N.B.: because of diminishing returns, the piece-square value for everything but quiet moves is calculated from scratch.
-}
measurePieceSquareValueIncrementally :: (
#ifdef USE_UNBOXED_ARRAYS
	Data.Array.Unboxed.IArray	Data.Array.Unboxed.UArray pieceSquareValue,	-- Requires 'FlexibleContexts'. The unboxed representation of the array-element must be defined (& therefore must be of fixed size).
#endif
	Num				pieceSquareValue
 )
	=> pieceSquareValue	-- ^ The value before the last move was applied, & therefore also from the perspective of the previous player.
	-> Component.PieceSquareByCoordinatesByRank.PieceSquareByCoordinatesByRank pieceSquareValue
	-> Model.Game.Game
	-> pieceSquareValue
{-# SPECIALISE measurePieceSquareValueIncrementally :: Type.Mass.PieceSquareValue -> Component.PieceSquareByCoordinatesByRank.PieceSquareByCoordinatesByRank Type.Mass.PieceSquareValue -> Model.Game.Game -> Type.Mass.PieceSquareValue #-}
measurePieceSquareValueIncrementally :: pieceSquareValue
-> PieceSquareByCoordinatesByRank pieceSquareValue
-> Game
-> pieceSquareValue
measurePieceSquareValueIncrementally pieceSquareValue
previousPieceSquareValue PieceSquareByCoordinatesByRank pieceSquareValue
pieceSquareByCoordinatesByRank Game
game
	| MoveType -> Bool
Attribute.MoveType.isSimple (MoveType -> Bool) -> MoveType -> Bool
forall a b. (a -> b) -> a -> b
$ QualifiedMove -> MoveType
Component.QualifiedMove.getMoveType QualifiedMove
qualifiedMove	= let
		findPieceSquareValue :: Coordinates -> pieceSquareValue
findPieceSquareValue	= (NPieces
 -> LogicalColour -> Rank -> Coordinates -> pieceSquareValue)
-> (NPieces, LogicalColour)
-> Rank
-> Coordinates
-> pieceSquareValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (
			PieceSquareByCoordinatesByRank pieceSquareValue
-> NPieces
-> LogicalColour
-> Rank
-> Coordinates
-> pieceSquareValue
forall pieceSquareValue.
PieceSquareByCoordinatesByRank pieceSquareValue
-> NPieces
-> LogicalColour
-> Rank
-> Coordinates
-> pieceSquareValue
Component.PieceSquareByCoordinatesByRank.findPieceSquareValue PieceSquareByCoordinatesByRank pieceSquareValue
pieceSquareByCoordinatesByRank
		 ) (
			Board -> NPieces
State.Board.getNPieces {- N.B.: no capture occurred-} (Board -> NPieces) -> (Game -> Board) -> Game -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Game -> Board
Model.Game.getBoard (Game -> NPieces)
-> (Game -> LogicalColour) -> Game -> (NPieces, LogicalColour)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite (LogicalColour -> LogicalColour)
-> (Game -> LogicalColour) -> Game -> LogicalColour
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Game -> LogicalColour
Model.Game.getNextLogicalColour (Game -> (NPieces, LogicalColour))
-> Game -> (NPieces, LogicalColour)
forall a b. (a -> b) -> a -> b
$ Game
game	{-the last player to move-}
		 ) (
			Turn -> Rank
Component.Turn.getRank Turn
turn	-- N.B.: no promotion occurred.
		 )
	in (pieceSquareValue -> pieceSquareValue -> pieceSquareValue)
-> (pieceSquareValue, pieceSquareValue) -> pieceSquareValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) (
		Coordinates -> pieceSquareValue
findPieceSquareValue (Coordinates -> pieceSquareValue)
-> (Move -> Coordinates) -> Move -> pieceSquareValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Move -> Coordinates
Component.Move.getDestination (Move -> pieceSquareValue)
-> (Move -> pieceSquareValue)
-> Move
-> (pieceSquareValue, pieceSquareValue)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& Coordinates -> pieceSquareValue
findPieceSquareValue (Coordinates -> pieceSquareValue)
-> (Move -> Coordinates) -> Move -> pieceSquareValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Move -> Coordinates
Component.Move.getSource (Move -> (pieceSquareValue, pieceSquareValue))
-> Move -> (pieceSquareValue, pieceSquareValue)
forall a b. (a -> b) -> a -> b
$ QualifiedMove -> Move
Component.QualifiedMove.getMove QualifiedMove
qualifiedMove
	) pieceSquareValue -> pieceSquareValue -> pieceSquareValue
forall a. Num a => a -> a -> a
- pieceSquareValue
previousPieceSquareValue {-from the previous player's perspective-}
	| Bool
otherwise	= PieceSquareByCoordinatesByRank pieceSquareValue
-> Game -> pieceSquareValue
forall pieceSquareValue.
Num pieceSquareValue =>
PieceSquareByCoordinatesByRank pieceSquareValue
-> Game -> pieceSquareValue
measurePieceSquareValue PieceSquareByCoordinatesByRank pieceSquareValue
pieceSquareByCoordinatesByRank Game
game	-- N.B.: though non-simple (Castling, En-passant, promotion) can be calculated, the returns don't justify the effort.
	where
		Just Turn
turn	= Game -> Maybe Turn
Model.Game.maybeLastTurn Game
game
		qualifiedMove :: QualifiedMove
qualifiedMove	= Turn -> QualifiedMove
Component.Turn.getQualifiedMove Turn
turn

-- | Measure the arithmetic difference between the total /rank-value/ of the /piece/s currently held by either side; <https://www.chessprogramming.org/Material>.
measureValueOfMaterial
	:: Input.RankValues.RankValues
	-> Type.Mass.RankValue	-- ^ Maximum total rank-value.
	-> Model.Game.Game
	-> Metric.CriterionValue.CriterionValue
measureValueOfMaterial :: RankValues -> RankValue -> Game -> RankValue
measureValueOfMaterial RankValues
rankValues RankValue
maximumTotalRankValue Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (Board -> RankValue) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ RankValue
maximumTotalRankValue	-- Normalise.
 ) (RankValue -> RankValue)
-> (Board -> RankValue) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	if LogicalColour -> Bool
Attribute.LogicalColour.isBlack (LogicalColour -> Bool) -> LogicalColour -> Bool
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game
		then RankValue -> RankValue
forall a. a -> a
id		-- White just moved.
		else RankValue -> RankValue
forall a. Num a => a -> a
negate	-- Black just moved.
 ) (RankValue -> RankValue)
-> (Board -> RankValue) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RankValue -> (Rank, NPieces) -> RankValue)
-> RankValue -> [(Rank, NPieces)] -> RankValue
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.List.foldl' (
	\RankValue
acc (Rank
rank, NPieces
nPiecesDifference) -> if NPieces
nPiecesDifference NPieces -> NPieces -> Bool
forall a. Eq a => a -> a -> Bool
== NPieces
0
		then RankValue
acc	-- Avoid calling 'Input.RankValues.findRankValue'.
		else RankValue
acc RankValue -> RankValue -> RankValue
forall a. Num a => a -> a -> a
+ RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (
			Rank -> RankValues -> RankValue
Input.RankValues.findRankValue Rank
rank RankValues
rankValues
		) RankValue -> RankValue -> RankValue
forall a. Num a => a -> a -> a
* NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral NPieces
nPiecesDifference
 ) RankValue
0 ([(Rank, NPieces)] -> RankValue)
-> (Board -> [(Rank, NPieces)]) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Rank NPieces -> [(Rank, NPieces)]
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> [(i, e)]
Data.Array.IArray.assocs (Array Rank NPieces -> [(Rank, NPieces)])
-> (Board -> Array Rank NPieces) -> Board -> [(Rank, NPieces)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Board -> Array Rank NPieces
State.Board.getNPiecesDifferenceByRank {-which arbitrarily counts White pieces as positive & Black as negative-} (Board -> RankValue) -> Board -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> Board
Model.Game.getBoard Game
game

{- |
	* Count the difference between the reciprocals (cf. <https://www.chessprogramming.org/Mobility>), of the total number of /move/s available to each player.

	* Using the reciprocal facilitates mapping into the /closed unit-interval/, & also emphasises the difference between having just one available move & having zero (i.e. mate).
	In consequence, it is more about restricting the opponent's mobility (particularly the @King@) rather than increasing one's own.
	This metric drives the game towards check-mate, rather than merely fighting a war of attrition.

	* CAVEAT: avoiding a reduction of one's mobility to zero (i.e. mate) must be paramount => losing one's @Queen@ should be preferable.
	measureValueOfMobility = 1 when mobility = 0, whereas loss of a @Queen@ = @ (rankValues ! Queen) / maximumTotalRankValue @,
	=> getWeightOfMobility * 1 > weightOfMaterial * (8.8 / 102.47)
	=> getWeightOfMobility > weightOfMaterial / 11.6

	The corollary is that one probably shouldn't sacrifice even a @Knight@ to temporarily reduce one's opponent's mobility to one.
	measureValueOfMobility = 0.5 when mobility = 1,
	=> getWeightOfMobility * 0.5 < weightOfMaterial * (3.2 / 102.47)
	=> getWeightOfMobility < weightOfMaterial / 16.0
	CAVEAT: the loss of a @Knight@ occurs on the subsequent turn & is therefore downgraded, so even this represents too high a weighting.

	This presents a paradox !
-}
measureValueOfMobility :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfMobility :: Game -> RankValue
measureValueOfMobility Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RankValue -> RankValue -> RankValue)
-> (RankValue, RankValue) -> RankValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((RankValue, RankValue) -> RankValue)
-> (LogicalColour -> (RankValue, RankValue))
-> LogicalColour
-> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	LogicalColour -> RankValue
measureConstriction (LogicalColour -> RankValue)
-> (LogicalColour -> RankValue)
-> LogicalColour
-> (RankValue, RankValue)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> RankValue
measureConstriction (LogicalColour -> RankValue)
-> (LogicalColour -> LogicalColour) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-}
 ) (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game where
	measureConstriction :: Attribute.LogicalColour.LogicalColour -> Type.Mass.CriterionValue
	measureConstriction :: LogicalColour -> RankValue
measureConstriction LogicalColour
logicalColour	= RankValue -> RankValue
forall a. Fractional a => a -> a
recip (RankValue -> RankValue)
-> (NPieces -> RankValue) -> NPieces -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-NPlies-} (NPieces -> RankValue)
-> (NPieces -> NPieces) -> NPieces -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> NPieces
forall a. Enum a => a -> a
succ {-avoid divide-by-zero-} (NPieces -> RankValue) -> NPieces -> RankValue
forall a b. (a -> b) -> a -> b
$ LogicalColour -> Game -> NPieces
Model.Game.countPliesAvailableTo LogicalColour
logicalColour Game
game

-- | Measure the arithmetic difference between the potential to /Castle/, on either side.
measureValueOfCastlingPotential :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfCastlingPotential :: Game -> RankValue
measureValueOfCastlingPotential Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RankValue -> RankValue -> RankValue)
-> (RankValue, RankValue) -> RankValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((RankValue, RankValue) -> RankValue)
-> (LogicalColour -> (RankValue, RankValue))
-> LogicalColour
-> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	LogicalColour -> RankValue
castlingPotential (LogicalColour -> RankValue)
-> (LogicalColour -> LogicalColour) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-} (LogicalColour -> RankValue)
-> (LogicalColour -> RankValue)
-> LogicalColour
-> (RankValue, RankValue)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> RankValue
castlingPotential
 ) (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game where
	castlingPotential :: Attribute.LogicalColour.LogicalColour -> Type.Mass.CriterionValue
	castlingPotential :: LogicalColour -> RankValue
castlingPotential	= RankValue
-> ([NPieces] -> RankValue) -> Maybe [NPieces] -> RankValue
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe RankValue
1 {-have Castled-} (
		(RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ RankValue
2) (RankValue -> RankValue)
-> ([NPieces] -> RankValue) -> [NPieces] -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NPieces -> RankValue)
-> ([NPieces] -> NPieces) -> [NPieces] -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NPieces] -> NPieces
forall (t :: * -> *) a. Foldable t => t a -> NPieces
length
	 ) (Maybe [NPieces] -> RankValue)
-> (LogicalColour -> Maybe [NPieces]) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
		LogicalColour -> CastleableRooksByLogicalColour -> Maybe [NPieces]
`State.CastleableRooksByLogicalColour.locateForLogicalColour` Game -> CastleableRooksByLogicalColour
Model.Game.getCastleableRooksByLogicalColour Game
game
	 )

{- |
	* Measure the arithmetic difference between the number of /doubled/ @Pawn@s on either side; <https://www.chessprogramming.org/Doubled_Pawn>.

	* N.B.: measures tripled @Pawn@s as equivalent to two doubled @Pawn@s.

	* CAVEAT: this is a negative attribute, so the weighted normalised value shouldn't exceed the reduction due to 'measureValueOfMaterial' resulting from a @Pawn@-sacrifice.
-}
measureValueOfDoubledPawns :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfDoubledPawns :: Game -> RankValue
measureValueOfDoubledPawns Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ (
		RankValue
6	:: Type.Mass.CriterionValue	-- Normalise to [-1 .. 1]; the optimal scenario is all files containing one Pawn; the worst scenario is two files each containing four Pawns, all but one per file of which are counted as doubled.
	)
 ) (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-NPieces-} (NPieces -> RankValue)
-> (LogicalColour -> NPieces) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NPieces -> NPieces -> NPieces) -> (NPieces, NPieces) -> NPieces
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((NPieces, NPieces) -> NPieces)
-> (LogicalColour -> (NPieces, NPieces))
-> LogicalColour
-> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	LogicalColour -> NPieces
countDoubledPawns (LogicalColour -> NPieces)
-> (LogicalColour -> NPieces)
-> LogicalColour
-> (NPieces, NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> NPieces
countDoubledPawns (LogicalColour -> NPieces)
-> (LogicalColour -> LogicalColour) -> LogicalColour -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-}
 ) (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game where
	countDoubledPawns :: Attribute.LogicalColour.LogicalColour -> Type.Count.NPieces
	countDoubledPawns :: LogicalColour -> NPieces
countDoubledPawns LogicalColour
logicalColour	= (NPieces -> NPieces -> NPieces) -> (NPieces, NPieces) -> NPieces
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((NPieces, NPieces) -> NPieces)
-> (NPiecesByFile -> (NPieces, NPieces))
-> NPiecesByFile
-> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
		(NPieces -> NPieces -> NPieces)
-> NPieces -> NPiecesByFile -> NPieces
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.Foldable.foldl' NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
(+) NPieces
0 (NPiecesByFile -> NPieces)
-> (NPiecesByFile -> NPieces)
-> NPiecesByFile
-> (NPieces, NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& NPieces -> NPieces
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NPieces -> NPieces)
-> (NPiecesByFile -> NPieces) -> NPiecesByFile -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPiecesByFile -> NPieces
forall (t :: * -> *) a. Foldable t => t a -> NPieces
Data.Foldable.length {-one Pawn can't be considered to be doubled, so substract one Pawn per column-}
	 ) (NPiecesByFile -> NPieces) -> NPiecesByFile -> NPieces
forall a b. (a -> b) -> a -> b
$ Board -> NPiecesByFileByLogicalColour
State.Board.getNPawnsByFileByLogicalColour (Game -> Board
Model.Game.getBoard Game
game) NPiecesByFileByLogicalColour -> LogicalColour -> NPiecesByFile
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
logicalColour

{- |
	* Measure the arithmetic difference between the number of /isolated/ @Pawn@s on either side; <https://www.chessprogramming.org/Isolated_Pawn>.

	* CAVEAT: this is a negative attribute, so the weighted normalised value shouldn't exceed the reduction due to 'measureValueOfMaterial' resulting from a @Pawn@-sacrifice.
-}
measureValueOfIsolatedPawns :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfIsolatedPawns :: Game -> RankValue
measureValueOfIsolatedPawns Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ (
		NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-X-} NPieces
Cartesian.Abscissa.xLength	:: Type.Mass.CriterionValue	-- Normalise to [-1 .. 1]; the optimal scenario is eight files each containing one Pawn & the worst scenario is all Pawns isolated (e.g. 4 alternate files of 2, 2 separate files or 4, ...).
	)
 ) (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-NPieces-} (NPieces -> RankValue)
-> (LogicalColour -> NPieces) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NPieces -> NPieces -> NPieces) -> (NPieces, NPieces) -> NPieces
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((NPieces, NPieces) -> NPieces)
-> (LogicalColour -> (NPieces, NPieces))
-> LogicalColour
-> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	LogicalColour -> NPieces
countIsolatedPawns (LogicalColour -> NPieces)
-> (LogicalColour -> NPieces)
-> LogicalColour
-> (NPieces, NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> NPieces
countIsolatedPawns (LogicalColour -> NPieces)
-> (LogicalColour -> LogicalColour) -> LogicalColour -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-}
 ) (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game where
	countIsolatedPawns :: Attribute.LogicalColour.LogicalColour -> Type.Count.NPieces
	countIsolatedPawns :: LogicalColour -> NPieces
countIsolatedPawns LogicalColour
logicalColour	= (NPieces -> NPieces -> NPieces -> NPieces)
-> NPieces -> NPiecesByFile -> NPieces
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey' (
		\NPieces
acc NPieces
x NPieces
nPawns -> if (NPieces -> NPiecesByFile -> Bool
forall k a. Ord k => k -> Map k a -> Bool
`Map.member` NPiecesByFile
nPawnsByFile) (NPieces -> Bool) -> [NPieces] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
`any` NPieces -> [NPieces]
Cartesian.Abscissa.getAdjacents NPieces
x
			then NPieces
acc		-- This file has at least one neighbouring Pawn which can (if at a suitable rank) be used to protect any of those in this file.
			else NPieces
acc NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
+ NPieces
nPawns	-- All the Pawns on this file are isolated & thus lack the protection that may be offered by adjacent Pawns.
	 ) NPieces
0 NPiecesByFile
nPawnsByFile where
		nPawnsByFile :: NPiecesByFile
nPawnsByFile	= Board -> NPiecesByFileByLogicalColour
State.Board.getNPawnsByFileByLogicalColour (Game -> Board
Model.Game.getBoard Game
game) NPiecesByFileByLogicalColour -> LogicalColour -> NPiecesByFile
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
logicalColour

-- | Measure the arithmetic difference between the number of /passed/ @Pawn@s on either side; <https://www.chessprogramming.org/Passed_Pawn>.
measureValueOfPassedPawns :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfPassedPawns :: Game -> RankValue
measureValueOfPassedPawns Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-X-} NPieces
Cartesian.Abscissa.xLength	-- Normalise to [-1 .. 1]; the optimal scenario is all files containing exactly one Pawn, of one's own logical colour, on the 7th rank.
 ) (RankValue -> RankValue)
-> (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RankValue -> RankValue -> RankValue)
-> (RankValue, RankValue) -> RankValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((RankValue, RankValue) -> RankValue)
-> (LogicalColour -> (RankValue, RankValue))
-> LogicalColour
-> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	LogicalColour -> RankValue
valuePassedPawns (LogicalColour -> RankValue)
-> (LogicalColour -> LogicalColour) -> LogicalColour -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-} (LogicalColour -> RankValue)
-> (LogicalColour -> RankValue)
-> LogicalColour
-> (RankValue, RankValue)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& LogicalColour -> RankValue
valuePassedPawns
 ) (LogicalColour -> RankValue) -> LogicalColour -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game where
	valuePassedPawns :: Attribute.LogicalColour.LogicalColour -> Type.Mass.CriterionValue
	valuePassedPawns :: LogicalColour -> RankValue
valuePassedPawns LogicalColour
logicalColour	= (RankValue -> Coordinates -> RankValue)
-> RankValue -> [Coordinates] -> RankValue
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Data.List.foldl' (
		\RankValue
acc -> (RankValue
acc RankValue -> RankValue -> RankValue
forall a. Num a => a -> a -> a
+) (RankValue -> RankValue)
-> (Coordinates -> RankValue) -> Coordinates -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RankValue -> RankValue
forall a. Fractional a => a -> a
recip {-value increases exponentially as distance to promotion decreases-} (RankValue -> RankValue)
-> (Coordinates -> RankValue) -> Coordinates -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral (NPieces -> RankValue)
-> (Coordinates -> NPieces) -> Coordinates -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> NPieces
forall a. Num a => a -> a
abs (NPieces -> NPieces)
-> (Coordinates -> NPieces) -> Coordinates -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> NPieces -> NPieces
forall a. Num a => a -> a -> a
subtract (
			LogicalColour -> NPieces
Cartesian.Ordinate.lastRank LogicalColour
logicalColour
		) (NPieces -> NPieces)
-> (Coordinates -> NPieces) -> Coordinates -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coordinates -> NPieces
Cartesian.Coordinates.getY	-- Measure the distance to promotion.
	 ) RankValue
0 ([Coordinates] -> RankValue) -> [Coordinates] -> RankValue
forall a b. (a -> b) -> a -> b
$ Board -> CoordinatesByLogicalColour
State.Board.getPassedPawnCoordinatesByLogicalColour (Game -> Board
Model.Game.getBoard Game
game) CoordinatesByLogicalColour -> LogicalColour -> [Coordinates]
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
logicalColour

{- |
	* The constant maximum total number of times the /piece/s of either side, can be defended.

	* Assumes all Pawns have been Queened.

	* CAVEAT: assuming the optimal arrangement of pieces:

	RQQB	= 3 + 7 + 3 + 2	= 15
	QQQN	= 4 + 6 + 8 + 4	= 22
	NQQK	= 4 + 8 + 6 + 0	= 18
	BQQR	= 2 + 3 + 7 + 3	= 15
				= 70
-}
maximumDefended :: Type.Count.NPieces
maximumDefended :: NPieces
maximumDefended	= NPieces
70

{- |
	* Measure the normalised arithmetic difference between the number of /piece/s defending each of one's own, on either side.

	* N.B. the /rank-value/ of the defended /piece/ is irrelevant because; it's the unknown value of the attacker that counts, since that's what the defender has the opportunity to counter-strike.
	CAVEAT: the validity of this depends on the duration of the battle.

	* N.B. defence of the @King@ is irrelevent, because it can't be taken.

	* N.B. it's the total number of defenders which is relevant, rather than whether each piece has some protection, since it's not the individual battles but the war which counts.

	* CAVEAT: this criterion competes with /mobility/, since each defended /piece/ blocks the path of the defender.
-}
measureValueOfDefence :: Model.Game.Game -> Metric.CriterionValue.CriterionValue
measureValueOfDefence :: Game -> RankValue
measureValueOfDefence Game
game	= RankValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (RankValue -> RankValue)
-> (Board -> RankValue) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	RankValue -> RankValue -> RankValue
forall a. Fractional a => a -> a -> a
/ (
		NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-NPieces-} NPieces
maximumDefended	:: Type.Mass.CriterionValue	-- Normalise.
	)
 ) (RankValue -> RankValue)
-> (Board -> RankValue) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NPieces -> RankValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral {-NPieces-} (NPieces -> RankValue) -> (Board -> NPieces) -> Board -> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NPieces -> NPieces -> NPieces) -> (NPieces, NPieces) -> NPieces
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (-) ((NPieces, NPieces) -> NPieces)
-> (Board -> (NPieces, NPieces)) -> Board -> NPieces
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (
	(Array LogicalColour NPieces -> LogicalColour -> NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour -> LogicalColour
forall a. Opposable a => a -> a
Property.Opposable.getOpposite {-recent mover-} LogicalColour
nextLogicalColour) (Array LogicalColour NPieces -> NPieces)
-> (Array LogicalColour NPieces -> NPieces)
-> Array LogicalColour NPieces
-> (NPieces, NPieces)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& (Array LogicalColour NPieces -> LogicalColour -> NPieces
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
! LogicalColour
nextLogicalColour)
 ) (Array LogicalColour NPieces -> (NPieces, NPieces))
-> (Board -> Array LogicalColour NPieces)
-> Board
-> (NPieces, NPieces)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Board -> Array LogicalColour NPieces
State.Board.summariseNDefendersByLogicalColour (Board -> RankValue) -> Board -> RankValue
forall a b. (a -> b) -> a -> b
$ Game -> Board
Model.Game.getBoard Game
game where
	nextLogicalColour :: LogicalColour
nextLogicalColour	= Game -> LogicalColour
Model.Game.getNextLogicalColour Game
game

{- |
	* Evaluates the fitness of the /board/ from the perspective of the last player to move.
	If the game has ended, the fitness is maximum for checkmate or zero for a draw,
	but otherwise is the /weighted mean/ of various criteria; <https://www.chessprogramming.org/Evaluation>.

	* Also returns the break-down of those /criterion-value/s with a non-zero /criterion-weight/.

	* Besides measuring the difference between the total /rank-value/ on either side, other criteria are selected to represent known attributes of a good position.

	* Many possible criteria aren't measured because they're, either currently or imminently, represented by those that are, typically by 'measureValueOfMaterial'.
-}
evaluateFitness :: (
#ifdef USE_UNBOXED_ARRAYS
	Data.Array.Unboxed.IArray	Data.Array.Unboxed.UArray pieceSquareValue,	-- Requires 'FlexibleContexts'. The unboxed representation of the array-element must be defined (& therefore must be of fixed size).
#endif
	Fractional			pieceSquareValue,
	Real				pieceSquareValue
 )
	=> Maybe pieceSquareValue	-- ^ An optional value for the specified game.
	-> Model.Game.Game
	-> Input.EvaluationOptions.Reader pieceSquareValue Metric.WeightedMeanAndCriterionValues.WeightedMeanAndCriterionValues
{-# SPECIALISE evaluateFitness :: Maybe Type.Mass.PieceSquareValue -> Model.Game.Game -> Input.EvaluationOptions.Reader Type.Mass.PieceSquareValue Metric.WeightedMeanAndCriterionValues.WeightedMeanAndCriterionValues #-}
evaluateFitness :: Maybe pieceSquareValue
-> Game -> Reader pieceSquareValue WeightedMeanAndCriterionValues
evaluateFitness Maybe pieceSquareValue
maybePieceSquareValue Game
game
	| Just GameTerminationReason
gameTerminationReason <- Game -> Maybe GameTerminationReason
Model.Game.getMaybeTerminationReason Game
game	= WeightedMeanAndCriterionValues
-> Reader pieceSquareValue WeightedMeanAndCriterionValues
forall (m :: * -> *) a. Monad m => a -> m a
return {-to Reader-monad-} (WeightedMeanAndCriterionValues
 -> Reader pieceSquareValue WeightedMeanAndCriterionValues)
-> WeightedMeanAndCriterionValues
-> Reader pieceSquareValue WeightedMeanAndCriterionValues
forall a b. (a -> b) -> a -> b
$ RankValue -> [RankValue] -> WeightedMeanAndCriterionValues
Metric.WeightedMeanAndCriterionValues.mkWeightedMeanAndCriterionValues (
		if GameTerminationReason -> Bool
Rule.GameTerminationReason.isCheckMate GameTerminationReason
gameTerminationReason
			then RankValue
1	-- The last player to move, has won.
			else RankValue
0	-- A draw.
	) []
	| Bool
otherwise	= do
		CriteriaWeights
criteriaWeights				<- (EvaluationOptions pieceSquareValue -> CriteriaWeights)
-> ReaderT
     (EvaluationOptions pieceSquareValue) Identity CriteriaWeights
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
Control.Monad.Reader.asks EvaluationOptions pieceSquareValue -> CriteriaWeights
forall pieceSquareValue.
EvaluationOptions pieceSquareValue -> CriteriaWeights
Input.EvaluationOptions.getCriteriaWeights
		(RankValues, RankValue)
rankValuePair				<- (EvaluationOptions pieceSquareValue -> (RankValues, RankValue))
-> ReaderT
     (EvaluationOptions pieceSquareValue)
     Identity
     (RankValues, RankValue)
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
Control.Monad.Reader.asks ((EvaluationOptions pieceSquareValue -> (RankValues, RankValue))
 -> ReaderT
      (EvaluationOptions pieceSquareValue)
      Identity
      (RankValues, RankValue))
-> (EvaluationOptions pieceSquareValue -> (RankValues, RankValue))
-> ReaderT
     (EvaluationOptions pieceSquareValue)
     Identity
     (RankValues, RankValue)
forall a b. (a -> b) -> a -> b
$ EvaluationOptions pieceSquareValue -> RankValues
forall pieceSquareValue.
EvaluationOptions pieceSquareValue -> RankValues
Input.EvaluationOptions.getRankValues (EvaluationOptions pieceSquareValue -> RankValues)
-> (EvaluationOptions pieceSquareValue -> RankValue)
-> EvaluationOptions pieceSquareValue
-> (RankValues, RankValue)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& EvaluationOptions pieceSquareValue -> RankValue
forall pieceSquareValue.
EvaluationOptions pieceSquareValue -> RankValue
Input.EvaluationOptions.getMaximumTotalRankValue
		Maybe (PieceSquareByCoordinatesByRank pieceSquareValue)
maybePieceSquareByCoordinatesByRank	<- (EvaluationOptions pieceSquareValue
 -> Maybe (PieceSquareByCoordinatesByRank pieceSquareValue))
-> ReaderT
     (EvaluationOptions pieceSquareValue)
     Identity
     (Maybe (PieceSquareByCoordinatesByRank pieceSquareValue))
forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
Control.Monad.Reader.asks EvaluationOptions pieceSquareValue
-> Maybe (PieceSquareByCoordinatesByRank pieceSquareValue)
forall pieceSquareValue.
EvaluationOptions pieceSquareValue
-> Maybe (PieceSquareByCoordinatesByRank pieceSquareValue)
Input.EvaluationOptions.getMaybePieceSquareByCoordinatesByRank

		WeightedMeanAndCriterionValues
-> Reader pieceSquareValue WeightedMeanAndCriterionValues
forall (m :: * -> *) a. Monad m => a -> m a
return {-to Reader-monad-} (WeightedMeanAndCriterionValues
 -> Reader pieceSquareValue WeightedMeanAndCriterionValues)
-> WeightedMeanAndCriterionValues
-> Reader pieceSquareValue WeightedMeanAndCriterionValues
forall a b. (a -> b) -> a -> b
$ CriteriaWeights
-> RankValue
-> RankValue
-> RankValue
-> RankValue
-> RankValue
-> RankValue
-> RankValue
-> RankValue
-> WeightedMeanAndCriterionValues
Input.CriteriaWeights.calculateWeightedMean CriteriaWeights
criteriaWeights (
			(RankValues -> RankValue -> Game -> RankValue)
-> (RankValues, RankValue) -> Game -> RankValue
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry RankValues -> RankValue -> Game -> RankValue
measureValueOfMaterial (RankValues, RankValue)
rankValuePair Game
game
		 ) (
			Game -> RankValue
measureValueOfMobility Game
game
		 ) (
			RankValue
-> (pieceSquareValue -> RankValue)
-> Maybe pieceSquareValue
-> RankValue
forall b a. b -> (a -> b) -> Maybe a -> b
Data.Maybe.maybe RankValue
0 (
				pieceSquareValue -> RankValue
forall a b. (Real a, Fractional b) => a -> b
realToFrac (pieceSquareValue -> RankValue)
-> (pieceSquareValue -> pieceSquareValue)
-> pieceSquareValue
-> RankValue
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (pieceSquareValue -> pieceSquareValue -> pieceSquareValue
forall a. Fractional a => a -> a -> a
/ NPieces -> pieceSquareValue
forall a b. (Integral a, Num b) => a -> b
fromIntegral NPieces
Component.Piece.nPiecesPerSide)
			) (Maybe pieceSquareValue -> RankValue)
-> Maybe pieceSquareValue -> RankValue
forall a b. (a -> b) -> a -> b
$ Maybe pieceSquareValue
maybePieceSquareValue Maybe pieceSquareValue
-> Maybe pieceSquareValue -> Maybe pieceSquareValue
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (PieceSquareByCoordinatesByRank pieceSquareValue
 -> pieceSquareValue)
-> Maybe (PieceSquareByCoordinatesByRank pieceSquareValue)
-> Maybe pieceSquareValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (PieceSquareByCoordinatesByRank pieceSquareValue
-> Game -> pieceSquareValue
forall pieceSquareValue.
Num pieceSquareValue =>
PieceSquareByCoordinatesByRank pieceSquareValue
-> Game -> pieceSquareValue
`measurePieceSquareValue` Game
game) Maybe (PieceSquareByCoordinatesByRank pieceSquareValue)
maybePieceSquareByCoordinatesByRank
		 ) (
			Game -> RankValue
measureValueOfCastlingPotential Game
game
		 ) (
			Game -> RankValue
measureValueOfDefence Game
game
		 ) (
			Game -> RankValue
measureValueOfDoubledPawns Game
game
		 ) (
			Game -> RankValue
measureValueOfIsolatedPawns Game
game
		 ) (
			Game -> RankValue
measureValueOfPassedPawns Game
game
		 )