<commit_before>{-# LANGUAGE DeriveGeneric #-}

module Phenotype
    ( Phenotype (Phenotype)
    , distance
    , phenotypeToVector
    , fitness) where

import Data.Hashable

newtype Phenotype = Phenotype [Double] deriving (Eq, Show, Ord)

instance Hashable Phenotype where
    hashWithSalt salt (Phenotype p) = hashWithSalt salt p

phenotypeToVector :: Phenotype -> [Double]
phenotypeToVector (Phenotype xs) = xs

distance :: Phenotype -> Phenotype -> Double
distance (Phenotype p1) (Phenotype p2) = euclideanDistance p1 p2
  where
    sqr :: Double -> Double
    sqr x = x * x

    euclideanDistance :: [Double] -> [Double] -> Double
    euclideanDistance a b = sqrt $ sum $ map sqr $ zipWith (-) a b

fitness :: Phenotype -> Phenotype -> Double
fitness optimum individual = log $ 25.0 / (individual `distance` optimum + 0.001)
<commit_msg>check that eauclidean distance inputs are of same length<commit_after>{-# LANGUAGE DeriveGeneric #-}

module Phenotype
    ( Phenotype (Phenotype)
    , distance
    , phenotypeToVector
    , fitness) where

import Data.Hashable

newtype Phenotype = Phenotype [Double] deriving (Eq, Show, Ord)

instance Hashable Phenotype where
    hashWithSalt salt (Phenotype p) = hashWithSalt salt p

phenotypeToVector :: Phenotype -> [Double]
phenotypeToVector (Phenotype xs) = xs

distance :: Phenotype -> Phenotype -> Double
distance (Phenotype p1) (Phenotype p2) = euclideanDistance p1 p2
  where
    sqr :: Double -> Double
    sqr x = x * x

    euclideanDistance :: [Double] -> [Double] -> Double
    euclideanDistance a b =
        if length a == length b
        then
            sqrt $ sum $ map sqr $ zipWith (-) a b
        else
            error $ "cant't measure distance of two vectors of different lengths " ++ show a ++ " vs " ++ show b

fitness :: Phenotype -> Phenotype -> Double
fitness optimum individual = log $ 25.0 / (individual `distance` optimum + 0.001)

{'fraction_non_alphanumeric': 0.09738219895287958, 'fraction_numerical': 0.011518324607329843, 'mean_word_length': 3.2751677852348995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1983278', 'n_tokens_mistral': 643, 'n_tokens_neox': 571, 'n_words': 202}