Permalink
Browse files

Replaced non-empty-list with (head, tail)

  • Loading branch information...
ckoster22 committed Jun 10, 2018
1 parent f5e6e0f commit b269207f5116d96e52982830bbb0f9bf6e3284b8
Showing with 32 additions and 72 deletions.
  1. +2 −3 elm-package.json
  2. +2 −3 examples/helloworld/elm-package.json
  3. +2 −3 examples/imageevolver/elm-package.json
  4. +26 −22 src/Genetic.elm
  5. +0 −41 src/NonemptyHelper.elm
View
@@ -11,8 +11,7 @@
"Genetic"
],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"mgold/elm-nonempty-list": "3.0.0 <= v < 4.0.0"
"elm-lang/core": "5.1.1 <= v < 6.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
}
@@ -10,8 +10,7 @@
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"mgold/elm-nonempty-list": "3.1.0 <= v < 4.0.0"
"elm-lang/html": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
}
@@ -12,8 +12,7 @@
"dependencies": {
"elm-community/random-extra": "2.0.0 <= v < 3.0.0",
"elm-lang/core": "5.1.1 <= v < 6.0.0",
"elm-lang/html": "2.0.0 <= v < 3.0.0",
"mgold/elm-nonempty-list": "3.1.0 <= v < 4.0.0"
"elm-lang/html": "2.0.0 <= v < 3.0.0"
},
"elm-version": "0.18.0 <= v < 0.19.0"
}
}
View
@@ -14,8 +14,6 @@ See the `examples` directory for an example of each.
-}
import List.Nonempty as NonemptyList exposing (Nonempty)
import NonemptyHelper
import Random exposing (Generator)
@@ -49,7 +47,7 @@ type alias PointedDna dna =
type alias Population dna =
Nonempty (PointedDna dna)
( PointedDna dna, List (PointedDna dna) )
{-| There are a handful of functions required because the algorithm needs the following information:
@@ -62,13 +60,13 @@ type alias Population dna =
These details are captured in the following record:
{ randomDnaGenerator : Generator dna
, evaluateSolution : dna -> Float
, crossoverDnas : dna -> dna -> dna
, mutateDna : dna -> Generator dna
, isDoneEvolving : dna -> Float -> Int -> Bool
, method : Method
}
{ randomDnaGenerator : Generator dna
, evaluateSolution : dna -> Float
, crossoverDnas : dna -> dna -> dna
, mutateDna : dna -> Generator dna
, isDoneEvolving : dna -> Float -> Int -> Bool
, method : Method
}
-}
type alias Options dna =
@@ -147,20 +145,23 @@ toPointedDna method dna =
{-| Executes subsequent iterations of the genetic algorithm. See `Options` for more information.
-}
executeStep : Options dna -> IntermediateValue dna -> Generator (IntermediateValue dna)
executeStep options (IntermediateValue population best numGenerations) =
executeStep options (IntermediateValue (( popHead, popTail ) as population) best numGenerations) =
let
sortedPopulation =
NonemptyList.sortBy .points population
List.sortBy .points (popHead :: popTail)
bestSolution =
case options.method of
MaximizeScore ->
sortedPopulation
|> NonemptyList.reverse
|> NonemptyList.head
case List.reverse popTail of
[] ->
popHead
tail :: _ ->
tail
MinimizePenalty ->
NonemptyList.head sortedPopulation
popHead
in
nextGenerationGenerator options population
|> Random.map
@@ -184,7 +185,7 @@ toStepValue pointedDna =
case pointedDna of
head :: rest ->
IntermediateValue
(NonemptyHelper.fromHeadRest head rest)
( head, rest )
-- Arbitrarily choose the first element as the best
head
1
@@ -194,12 +195,10 @@ toStepValue pointedDna =
nextGenerationGenerator : Options dna -> Population dna -> Generator (Population dna)
nextGenerationGenerator options currPopulation =
nextGenerationGenerator options (( popHead, popTail ) as currPopulation) =
let
sortedPopulation =
currPopulation
|> NonemptyList.sortBy .points
|> NonemptyList.toList
List.sortBy .points (popHead :: popTail)
bestHalfOfPopulation =
case options.method of
@@ -212,7 +211,12 @@ nextGenerationGenerator options currPopulation =
bestOrganismsGenerator options bestHalfOfPopulation
|> Random.map
(\organismList ->
organismList |> NonemptyList.fromList |> Maybe.withDefault currPopulation
case organismList of
head :: tail ->
( head, tail )
_ ->
currPopulation
)
View
@@ -1,41 +0,0 @@
module NonemptyHelper exposing (fromHeadRest, randomNonemptyList)
import List.Nonempty as Nonempty exposing (Nonempty)
import Random exposing (Generator, Seed)
randomNonemptyList : Int -> Seed -> Generator a -> ( Nonempty a, Seed )
randomNonemptyList size seed generator =
let
( head, seed2 ) =
Random.step generator seed
in
Random.step (Random.list (size - 1) generator) seed2
|> constructNonemptyFromHead head size
constructNonemptyFromHead : a -> Int -> ( List a, Seed ) -> ( Nonempty a, Seed )
constructNonemptyFromHead thing size ( things, seed ) =
let
headList =
Nonempty.fromElement thing
entireList =
List.foldl
(\item nonemptyList ->
Nonempty.cons item nonemptyList
)
headList
things
in
( entireList, seed )
fromHeadRest : a -> List a -> Nonempty a
fromHeadRest head rest =
List.foldl
(\item nonemptyList ->
Nonempty.cons item nonemptyList
)
(Nonempty.fromElement head)
rest

0 comments on commit b269207

Please sign in to comment.