Skip to content

Commit

Permalink
determenistic initial population for real-coded tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astanin committed May 22, 2014
1 parent b0f0175 commit e7b43cb
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Tests/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,29 @@ undx = unimodalCrossoverRP
sbx = simulatedBinaryCrossover 2


randomGenomesReal :: Int -> [(Double,Double)] -> Rand [Genome Double]
randomGenomesReal popsize ranges = replicateM popsize randomGenome
where
randomGenome = mapM (\varRange -> getRandomR varRange) ranges
realUniformGenomes :: Int -> [(Double,Double)] -> [Genome Double]
realUniformGenomes popsize ranges =
let dims = map (uncurry subtract) ranges :: [Double]
ndims = length dims :: Int
sd = sum dims
vol = product dims
mdim = vol ** (1.0/fromIntegral ndims) :: Double
msamples = (fromIntegral popsize) ** (1.0/fromIntegral ndims) :: Double
ptsPerDim = map (\d -> round $ d*msamples/mdim) dims :: [Int]
ptsInLastDims = product $ drop 1 ptsPerDim :: Int
ptsInFirstDim = popsize `div` ptsInLastDims :: Int
ptsPerDim' = ptsInFirstDim : (drop 1 ptsPerDim) :: [Int]
linspaces = zipWith linspace ranges ptsPerDim' :: [[Double]]
in sproduct [[]] linspaces
where
linspace :: (Double, Double) -> Int -> [Double]
linspace (lo, hi) n = map (\i -> (fromIntegral i)*(hi-lo)/fromIntegral (n-1)) [0..n-1]

sproduct :: [[Double]] -> [[Double]] -> [[Double]]
sproduct gs [] = gs
sproduct gs (l:ls) =
let gs' = [x:g | g<-gs, x<-l]
in sproduct gs' ls


data (ObjectiveFunction objectivefn a) => Solver objectivefn a = Solver {
Expand Down Expand Up @@ -71,7 +90,7 @@ runSolverReal :: RealProblem
-- ^ final population and euclidean distance from the known solution
runSolverReal problem solver = do
let ptype = Minimizing
let init = randomGenomesReal (s'popsize solver) (minimizeVarRange problem)
let init = return $ realUniformGenomes (s'popsize solver) (minimizeVarRange problem)
let step = nextGeneration ptype (s'objective solver)
(s'select solver) (s'elitesize solver)
(s'crossover solver) (s'mutate solver)
Expand Down

0 comments on commit e7b43cb

Please sign in to comment.