Skip to content

Commit

Permalink
use shannon entropy instead of weight
Browse files Browse the repository at this point in the history
way more interesting results!
  • Loading branch information
BrianHicks committed Feb 21, 2020
1 parent a42cda2 commit 24f22d9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Main.elm
Expand Up @@ -333,7 +333,7 @@ view model =

Just { coords, entropy } ->
Html.span []
[ Html.text (String.fromInt entropy)
[ Html.text (String.fromFloat entropy)
, Html.text " @ "
, Html.text (String.fromInt coords.row)
, Html.text ","
Expand Down Expand Up @@ -374,7 +374,7 @@ view model =
(\value ->
Html.td [ css [ Css.padding (Css.px 2) ] ]
[ value
|> Maybe.map (String.fromInt << .entropy)
|> Maybe.map (String.fromFloat << .entropy)
|> Maybe.withDefault "-"
|> Html.text
]
Expand Down
26 changes: 18 additions & 8 deletions src/Wave.elm
Expand Up @@ -12,7 +12,7 @@ import Set exposing (Set)

type alias Entropy =
{ coords : { row : Int, column : Int }
, entropy : Int
, entropy : Float
}


Expand Down Expand Up @@ -250,15 +250,25 @@ propagateInDirection source sourceCell direction ( Wave wave, todo ) =
)


entropy : Dict comparable Int -> Set comparable -> Int
entropy probabilities possibilities =
possibilities
|> Set.toList
entropy : Dict comparable Int -> Set comparable -> Float
entropy weights possibilities =
let
total =
Dict.values weights |> List.sum |> toFloat
in
Set.toList possibilities
|> List.map
(\item ->
probabilities
|> Dict.get item
|> Maybe.withDefault 0
case Dict.get item weights of
Just weight ->
let
prob =
toFloat weight / total
in
-(prob * logBase 2 prob)

Nothing ->
0.0
)
|> List.sum

Expand Down

0 comments on commit 24f22d9

Please sign in to comment.