prashant007 / Dynamic-Programming-With-Semirings

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Our code base uses a pared down, slightly modified version of Sasha Rush's Pragmatic DP library (http://hackage.haskell.org/package/DP-0.1.1/). The library essentially allows programmers to get a DP out a semiring formulation of a recurrence relation.

1) One noticable addition to the code is that we use our formulation of Semirings rather than use the default semiring formulation used in the original implementation. The original semiring formulation used in the code was clunky and hadn't been maintained in some time. 

2) We are not using all the different solvers (botton up solvers) provided by the library, and are just restricting ourself to the top down solver. Consequently, we are not using the files for these additional solvers in this code base.

3) The code from on the library is in the folder DPSolver and is spread across multiple files. We list all the changes we made to these file below. We also mention the relevant changes at the top of each of these files. 

-- ==========================================================================================================
-- ==========================================================================================================

The DP.hs file make some changes from Sasha Rush's original file of the same name in the aforementioned 
library. These changes are:

1) Functions memo and inj defined here are not part of the original code. 
2) The DPGen type synonym is called DP in the orifinal file. This type change propagates to
   the fromSimple function which is of DPGen now instead of DP. We made this change as we wanted
   to use DP type synonym elsewhere.

-- | A dynamic program. 
type DPGen index cell = index -> DPCell index ce
-- | Convert a Simple DP to a General DP 

fromSimple :: SimpleDP a b -> DPGen a (Identity b)
fromSimple simple i = mkCell [mkItem () (simple i)] 

-- ==========================================================================================================
-- ==========================================================================================================

The Solvers.hs file make some changes from Sasha Rush's original file of the same name in the aforementioned 
library. These changes are:

1) Only a very specific DP solver (used in solving the recurrence relation), top down solver, is used. 
In comparison Sasha Rush's original file exported bottom up solvers as well.

2) runDP function defined here isn't part of the original code of the library. 


-- ==========================================================================================================
-- ==========================================================================================================

The Internals.hs module make some changes to the original file in the library of the same name. The specific changes 
made to this code are provided as comments here. These are changes owing to the different semiring 
formulation that we have.  

Our code has 

optFunc Plus = (<+>)
optFunc Times = (<.>)

--- =========================instead of=============================================================== 

optFunc Plus = mappend
optFunc Times = times

Our code has 

instance (Semiring (CellVal cell)) => Semiring (DPSubValue index cell ) where 
    zero  = Constant zero 
    one   = Constant one   
    (<+>) = Opt Plus
    (<.>) = Opt Times

--- =========================instead of=============================================================== 

instance (Monoid (CellVal cell)) => Monoid (DPSubValue index cell ) where 
    mappend = Opt Plus
    mempty  = Constant mempty

instance (Multiplicative (CellVal cell)) => Multiplicative (DPSubValue index  cell ) where 
    times = Opt Times
    one  = Constant one
instance Semiring (CellVal cell) =>  Semiring (DPSubValue index  cell)

data DPState m chart ind cell = DPState { 
     dpLookup :: ind -> StateT (DPState m chart ind cell) m cell,
     dpData :: chart 
}

-- ==========================================================================================================
-- ==========================================================================================================

About

License:MIT License


Languages

Language:Haskell 100.0%