diff --git a/src/Main.elm b/src/Main.elm index cb84cec..7a863f3 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -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 "," @@ -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 ] diff --git a/src/Wave.elm b/src/Wave.elm index 8e1f90a..485535c 100644 --- a/src/Wave.elm +++ b/src/Wave.elm @@ -12,7 +12,7 @@ import Set exposing (Set) type alias Entropy = { coords : { row : Int, column : Int } - , entropy : Int + , entropy : Float } @@ -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