Copyright | (c) David Llorens and Juan Miguel Vilar 2020 |
---|---|
License | BSD-3-Clause |
Stability | experimental |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
HyloDP.Semiring
Description
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
Methods
(<+>) :: 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.
Constructors
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.
Constructors
TMax v |
Min product semiring
Constructors
MinProd v |
Instances
Ord v => Opt (MinProd v) Source # | |
(Num v, Ord v, Bounded v) => Semiring (MinProd v) Source # | |
Show v => Show (MinProd v) Source # | |
Eq v => Eq (MinProd v) Source # | |
Ord v => Ord (MinProd v) Source # | |
Max product semiring
Constructors
MaxProd v |
Instances
DPTypes sc d (MaxProd sc) Source # | |
Defined in HyloDP.Base | |
Ord v => Opt (MaxProd v) Source # | |
(Num v, Ord v, Bounded v) => Semiring (MaxProd v) Source # | |
Show v => Show (MaxProd v) Source # | |
Eq v => Eq (MaxProd v) Source # | |
Ord v => Ord (MaxProd v) Source # | |
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.
Constructors
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.
Constructors
AllSolutions [([d], sc)] |
Instances
DPTypes sc d sol => DPTypes sc d (AllSolutions d sol) Source # | |
Defined in HyloDP.Base Methods combine :: sc -> d -> AllSolutions d sol Source # | |
DPTypes sc (Maybe d) sol => DPTypes sc (Maybe d) (AllSolutions d sol) Source # | |
Defined in HyloDP.Base Methods combine :: sc -> Maybe d -> AllSolutions d sol Source # | |
Semiring sc => Semiring (AllSolutions d sc) Source # | |
Defined in HyloDP.Semiring Methods (<+>) :: 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 Methods 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.
Constructors
AllBestSolutions ([[d]], s) |
Instances
DPTypes sc d sol => DPTypes sc d (AllBestSolutions d sol) Source # | |
Defined in HyloDP.Base Methods combine :: sc -> d -> AllBestSolutions d sol Source # | |
DPTypes sc (Maybe d) sol => DPTypes sc (Maybe d) (AllBestSolutions d sol) Source # | |
Defined in HyloDP.Base Methods combine :: sc -> Maybe d -> AllBestSolutions d sol Source # | |
(Semiring sc, Opt sc, Eq sc) => Semiring (AllBestSolutions d sc) Source # | |
Defined in HyloDP.Semiring Methods (<+>) :: 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 Methods 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