packages feed

dynobud-1.3.0.0: src/Dyno/Nlp.hs

{-# OPTIONS_GHC -Wall #-}
{-# Language FlexibleInstances #-}
{-# Language DeriveFunctor #-}
{-# Language DeriveGeneric #-}

module Dyno.Nlp
       ( Bounds
       , Nlp(..),  NlpOut(..)
       , KKT(..)
       ) where

import GHC.Generics ( Generic )

import Casadi.DMatrix ( DMatrix )
import qualified Data.Vector as V
import Data.Binary ( Binary )

import Dyno.Vectorize ( Id )
import Dyno.View.View ( View(..), J )
import Dyno.View.Viewable ( Viewable )
import Dyno.View.JV ( JV )
import Dyno.View.M ( M )

type Bounds = (Maybe Double, Maybe Double)

-- | nonlinear program (NLP)
--
-- >  minimize         f(x,p)
-- >     x
-- >
-- > subject to   xlb <=  x   <= xub
-- >              glb <= g(x) <= gub
--
-- where p is some parameter
--
data Nlp x p g a =
  Nlp
  { nlpFG :: J x a -> J p a -> (J (JV Id) a, J g a)
  , nlpBX :: J x (V.Vector Bounds)
  , nlpBG :: J g (V.Vector Bounds)
  , nlpX0 :: J x (V.Vector Double)
  , nlpP  :: J p (V.Vector Double)
  , nlpLamX0 :: Maybe (J x (V.Vector Double))
  , nlpLamG0 :: Maybe (J g (V.Vector Double))
  , nlpScaleF :: Maybe Double
  , nlpScaleX :: Maybe (J x (V.Vector Double))
  , nlpScaleG :: Maybe (J g (V.Vector Double))
  }

-- | NLP output
data NlpOut x g a =
  NlpOut
  { fOpt :: J (JV Id) a
  , xOpt :: J x a
  , gOpt :: J g a
  , lambdaXOpt :: J x a
  , lambdaGOpt :: J g a
  } deriving (Eq, Show, Generic)
instance (View x, View g, Binary a, Viewable a) => Binary (NlpOut x g a)


-- | Karush–Kuhn–Tucker (KKT) matrix
data KKT x g =
  KKT
  { kktHessLag :: M x x DMatrix -- ^ unscaled version only valid at solution
  , kktHessF :: M x x DMatrix
  , kktHessLambdaG :: M x x DMatrix -- ^ unscaled version only valid at solution
  , kktJacG :: M g x DMatrix
  , kktG :: J g DMatrix
  , kktGradF :: J x DMatrix
  , kktF :: J (JV Id) DMatrix
  } deriving (Generic, Eq, Show)
instance (View x, View g) => Binary (KKT x g)