Copyright | (c) David Llorens and Juan Miguel Vilar 2020 |
---|---|
License | BSD-3-Clause |
Stability | experimental |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
This module declares the Semiring
class and various instances.
Synopsis
- class Semiring s where
- class Opt t where
- optimum :: t -> t -> t
- newtype TMin v = TMin v
- newtype TMax v = TMax v
- newtype MinProd v = MinProd v
- newtype MaxProd v = MaxProd v
- newtype Count = Count Integer
- data BestSolution d sc = BestSolution (Maybe [d]) sc
- newtype AllSolutions d sc = AllSolutions [([d], sc)]
- newtype AllBestSolutions d s = AllBestSolutions ([[d]], s)
- decisions :: BestSolution d sc -> [d]
Type Classes
class Semiring s where Source #
A Semiring
is a type with two operations <+>
and <.>
and two
distinguished elements, zero
and one
, which satisfy the following
axioms:
- Conmutativity:
a <+> b == b <+> a a <.> b == b <.> a
- Associativity:
a <+> (b <+> c) == (a <+> b) <+> c a <.> (b <.> c) == (a <.> b) <.> c
- Identity:
a <+> zero = zero <+> a == a a <.> one = one <.> a == a
- Distributive property:
a <.> (b<+>c) == (a<.>b) <+> (a<.>c) (a<+>b) <.>c == (a<.>c) <+> (b<.>c)
- Anhiliation of multiplication by zero:
a <.> zero = zero <.> a = zero
(<+>) :: s -> s -> s infixl 6 Source #
(<.>) :: s -> s -> s infixl 7 Source #
Neutral element for <+>
.
Neutral element for <.>
.
Instances
This typeclass is used in optimization semirings. It is expected
that optimum a b
returns either a
or b
.
Semiring Helpers
Min tropical semiring
The tropical min semiring, a semiring that uses min
as <+>
and
sum as <.>
. It is used in problems that ask for minimizing a sum of
values.
TMin v |
Max tropical semiring
The tropical max semiring, a semiring that uses max
as <+>
and
sum as <.>
. It is used in problems that ask for maximizing a sum of
values.
TMax v |
Min product semiring
MinProd v |
Max product semiring
MaxProd v |
Count semiring
The Count
semiring is used for counting the number of different
solutions.
Best solution semiring
data BestSolution d sc Source #
The BestSolution
semiring is used for recovering the best sequence
of decisions together with its score. The score must be an instance of
Opt
to be able to decide which is the best of two scores.
BestSolution (Maybe [d]) sc |
Instances
All solutions semiring
newtype AllSolutions d sc Source #
With the AllSolutions
semiring it is possible to recover all possible
solutions to a problem, regardless of their scores.
AllSolutions [([d], sc)] |
Instances
DPTypes sc d sol => DPTypes sc d (AllSolutions d sol) Source # | |
Defined in HyloDP.Base combine :: sc -> d -> AllSolutions d sol Source # | |
DPTypes sc (Maybe d) sol => DPTypes sc (Maybe d) (AllSolutions d sol) Source # | |
Defined in HyloDP.Base combine :: sc -> Maybe d -> AllSolutions d sol Source # | |
Semiring sc => Semiring (AllSolutions d sc) Source # | |
Defined in HyloDP.Semiring (<+>) :: AllSolutions d sc -> AllSolutions d sc -> AllSolutions d sc Source # (<.>) :: AllSolutions d sc -> AllSolutions d sc -> AllSolutions d sc Source # zero :: AllSolutions d sc Source # one :: AllSolutions d sc Source # | |
(Show d, Show sc) => Show (AllSolutions d sc) Source # | |
Defined in HyloDP.Semiring showsPrec :: Int -> AllSolutions d sc -> ShowS # show :: AllSolutions d sc -> String # showList :: [AllSolutions d sc] -> ShowS # |
All best solutions semiring
newtype AllBestSolutions d s Source #
With the AllBestSolutions
semiring it is possible to recover all the
solutions to a problem that reach the optimum score.
AllBestSolutions ([[d]], s) |
Instances
DPTypes sc d sol => DPTypes sc d (AllBestSolutions d sol) Source # | |
Defined in HyloDP.Base combine :: sc -> d -> AllBestSolutions d sol Source # | |
DPTypes sc (Maybe d) sol => DPTypes sc (Maybe d) (AllBestSolutions d sol) Source # | |
Defined in HyloDP.Base combine :: sc -> Maybe d -> AllBestSolutions d sol Source # | |
(Semiring sc, Opt sc, Eq sc) => Semiring (AllBestSolutions d sc) Source # | |
Defined in HyloDP.Semiring (<+>) :: AllBestSolutions d sc -> AllBestSolutions d sc -> AllBestSolutions d sc Source # (<.>) :: AllBestSolutions d sc -> AllBestSolutions d sc -> AllBestSolutions d sc Source # zero :: AllBestSolutions d sc Source # one :: AllBestSolutions d sc Source # | |
(Show d, Show s) => Show (AllBestSolutions d s) Source # | |
Defined in HyloDP.Semiring showsPrec :: Int -> AllBestSolutions d s -> ShowS # show :: AllBestSolutions d s -> String # showList :: [AllBestSolutions d s] -> ShowS # |
Other functions
decisions :: BestSolution d sc -> [d] Source #
Auxiliary function to recover the sequence of decisions from a BestSolution