module IT.Algorithms where
import IT
import IT.Random
import IT.Metrics
import Control.DeepSeq
import qualified Numeric.LinearAlgebra as LA
data Solution = Sol { Solution -> Expr
_expr :: Expr
, Solution -> [Double]
_fit :: [Double]
, Solution -> Double
_constr :: Double
, Solution -> Int
_len :: Int
, Solution -> Double
_penalty :: Double
, Solution -> [Vector]
_weights :: [Vector]
}
instance Show Solution where
show :: Solution -> String
show (Sol Expr
e [Double]
f Double
c Int
l Double
_ [Vector]
w) = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [String
"Expression: " , String
expr, String
"\n"
, String
"Fitness: " , String
fit, String
"\n"
, String
"Weights: " , String
weights, String
"\n"
, String
"Constraints: ", String
constr, String
"\n"
, String
"Length: " , String
len, String
"\n"]
where
expr :: String
expr = Expr -> [Double] -> String
toExprStr Expr
e (Vector -> [Double]
forall a. Storable a => Vector a -> [a]
LA.toList (Vector -> [Double]) -> Vector -> [Double]
forall a b. (a -> b) -> a -> b
$ [Vector] -> Vector
forall a. [a] -> a
head [Vector]
w)
fit :: String
fit = (Double -> String
forall a. Show a => a -> String
show (Double -> String) -> ([Double] -> Double) -> [Double] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Double] -> Double
forall a. [a] -> a
head) [Double]
f
weights :: String
weights = [Vector] -> String
forall a. Show a => a -> String
show [Vector]
w
constr :: String
constr = Double -> String
forall a. Show a => a -> String
show Double
c
len :: String
len = Int -> String
forall a. Show a => a -> String
show Int
l
instance Eq Solution where
Solution
s1 == :: Solution -> Solution -> Bool
== Solution
s2 = ([Double] -> Double
forall a. [a] -> a
head([Double] -> Double)
-> (Solution -> [Double]) -> Solution -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Solution -> [Double]
_fit) Solution
s1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Solution -> Double
_penalty Solution
s1 Double -> Double -> Bool
forall a. Eq a => a -> a -> Bool
== ([Double] -> Double
forall a. [a] -> a
head([Double] -> Double)
-> (Solution -> [Double]) -> Solution -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Solution -> [Double]
_fit) Solution
s2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Solution -> Double
_penalty Solution
s2
instance Ord Solution where
Solution
s1 <= :: Solution -> Solution -> Bool
<= Solution
s2 = ([Double] -> Double
forall a. [a] -> a
head([Double] -> Double)
-> (Solution -> [Double]) -> Solution -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Solution -> [Double]
_fit) Solution
s1 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Solution -> Double
_penalty Solution
s1 Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
<= ([Double] -> Double
forall a. [a] -> a
head([Double] -> Double)
-> (Solution -> [Double]) -> Solution -> Double
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Solution -> [Double]
_fit) Solution
s2 Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Solution -> Double
_penalty Solution
s2
type Population = [Solution]
instance NFData Solution where
rnf :: Solution -> ()
rnf Solution
_ = ()
type Fitness = Expr -> Maybe Solution
type Constraint = Expr -> [Double] -> Double
type Mutation = Expr -> Rnd Expr