Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
Synopsis
- solve :: (Indexed sh, Index sh ~ ix) => Method -> Bounds ix -> Constraints Double ix -> (Direction, Objective sh) -> Result sh
- data Direction
- data Term a ix = Term a ix
- (.*) :: a -> ix -> Term a ix
- type Constraints a ix = [Inequality [Term a ix]]
- free :: x -> Inequality x
- (<=.) :: x -> Double -> Inequality x
- (>=.) :: x -> Double -> Inequality x
- (==.) :: x -> Double -> Inequality x
- (>=<.) :: x -> (Double, Double) -> Inequality x
- data Method
- simplex :: Method
- choose :: Method
- ipm :: Method
- data ModelStatus
- type Result sh = (ModelStatus, Maybe (Double, Array sh Double))
- solveWith :: (Indexed sh, Index sh ~ ix) => Query sh result -> Method -> Bounds ix -> Constraints Double ix -> (Direction, Objective sh) -> (ModelStatus, Maybe result)
- data Query sh a
- getObjectiveValue :: Query sh Double
- getOptimalVector :: C sh => Query sh (Array sh Double)
- getSolutionVectors :: C sh => Query sh ((Array sh Double, Array sh Double), (Array ShapeInt Double, Array ShapeInt Double))
- getBasisStatus :: C sh => Query sh (Array sh BasisStatus, Array ShapeInt BasisStatus)
- data BasisStatus
simple solver
solve :: (Indexed sh, Index sh ~ ix) => Method -> Bounds ix -> Constraints Double ix -> (Direction, Objective sh) -> Result sh Source #
>>>
case Shape.indexTupleFromShape tripletShape of (x,y,z) -> fmap (mapSnd Array.toTuple) $ snd $ LP.solve LP.simplex [] [[2.*x, 1.*y] <=. 10, [1.*y, (5::Double).*z] <=. 20] (LP.Maximize, Array.fromTuple (4,-3,2) :: Array.Array TripletShape Double)
Just (28.0,(5.0,0.0,4.0))
>>>
case Shape.indexTupleFromShape tripletShape of (x,y,z) -> fmap (mapSnd Array.toTuple) $ snd $ LP.solve LP.choose [y >=<. (-12,12)] [[1.*x, (-1).*y] <=. 10, [(-1).*y, (1::Double).*z] <=. 20] (LP.Maximize, Array.fromTuple (4,-3,2) :: Array.Array TripletShape Double)
Just (116.0,(22.0,12.0,32.0))
>>>
case Shape.indexTupleFromShape tripletShape of (x,y,z) -> mapSnd (fmap (mapSnd Array.toTuple)) $ LP.solve LP.choose [y >=<. (-12,12)] [[1.*x, 1.*y] <=. 10, [1.*y, (-1::Double).*z] >=. 20] (LP.Maximize, Array.fromTuple (4,3,2) :: Array.Array TripletShape Double)
(ModelStatusInfeasible,...)
>>>
case Shape.indexTupleFromShape tripletShape of (x,y,z) -> mapSnd (fmap (mapSnd Array.toTuple)) $ LP.solve LP.choose [y >=<. (-12,12)] [[1.*x, 1.*y] <=. 10, [1.*y, (1::Double).*z] >=. 20] (LP.Maximize, Array.fromTuple (4,3,2) :: Array.Array TripletShape Double)
(ModelStatusUnbounded,...)
forAllMethod $ \method (QC.Positive posWeight) (QC.Positive negWeight) target -> case Shape.indexTupleFromShape pairShape of (pos,neg) -> case fmap (mapSnd Array.toTuple) $ snd $ LP.solve method [] [[1.*pos, (-1::Double).*neg] ==. target] (LP.Minimize, Array.fromTuple (posWeight,negWeight) :: Array.Array PairShape Double) of Nothing -> QC.property False Just (absol,(posResult,negResult)) -> QC.property (absol>=0) .&&. (posResult === 0 .||. negResult === 0)
forAllMethod $ \method target -> case Shape.indexTupleFromShape pairShape of (pos,neg) -> case fmap (mapSnd Array.toTuple) $ snd $ LP.solve method [] [[1.*pos, (-1::Double).*neg] ==. target] (LP.Minimize, Array.fromTuple (1,1) :: Array.Array PairShape Double) of Nothing -> QC.property False Just (absol,(posResult,negResult)) -> QC.counterexample (show(absol,(posResult,negResult))) $ QC.property (approxReal 0.001 absol (abs target)) .&&. (posResult === 0 .||. negResult === 0)
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case snd $ LP.solve method bounds constrs (dir,obj) of Nothing -> False Just _ -> True
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case snd $ LP.solve method bounds constrs (dir,obj) of Nothing -> QC.property False Just (_,sol) -> TestLP.checkFeasibility 0.1 bounds constrs sol
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case snd $ LP.solve method bounds constrs (dir,obj) of Nothing -> QC.property False Just (_,sol) -> QC.forAll (QC.choose (0,1)) $ \lambda -> TestLP.checkFeasibility 0.1 bounds constrs $ TestLP.affineCombination lambda sol (Array.map fromIntegral origin)
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case snd $ LP.solve method bounds constrs (dir,obj) of Nothing -> QC.property False Just (opt,sol) -> QC.forAll (QC.choose (0,1)) $ \lambda -> let val = TestLP.scalarProduct obj $ TestLP.affineCombination lambda sol $ Array.map fromIntegral origin in QC.counterexample (show (dir,opt,val)) $ case dir of LP.Minimize -> opt-0.01 <= val LP.Maximize -> opt+0.01 >= val
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllBoundedProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \dirObjA -> QC.forAll (TestLP.genObjective origin) $ \dirObjB -> let solA = snd $ LP.solve method bounds constrs dirObjA in let solB = snd $ LP.solve method bounds constrs dirObjB in QC.counterexample (show (fmap fst solA, fmap fst solB)) $ case (solA, solB) of (Just _, Just _) -> True (Nothing, Nothing) -> True _ -> False
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(_dir,obj) -> case (snd $ LP.solve method bounds constrs (LP.Minimize,obj), snd $ LP.solve method bounds constrs (LP.Maximize,obj)) of (Just (optMin,_), Just (optMax,_)) -> QC.counterexample (show (optMin, optMax)) $ optMin <= optMax + 0.01 _ -> QC.property False
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds allConstrs -> QC.forAll (QC.sublistOf allConstrs) $ \someConstrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case (snd $ LP.solve method bounds allConstrs (dir,obj), snd $ LP.solve method bounds someConstrs (dir,obj)) of (Just (optAll,_), Just (optSome,_)) -> QC.counterexample (show (optAll, optSome)) $ case dir of LP.Minimize -> optAll >= optSome-0.01 LP.Maximize -> optAll <= optSome+0.01 _ -> QC.property False
forAllMethod $ \methodA -> forAllMethod $ \methodB -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \dirObj -> case (snd $ LP.solve methodA bounds constrs dirObj, snd $ LP.solve methodB bounds constrs dirObj) of (Just (optA,_), Just (optB,_)) -> QC.counterexample (show (optA, optB)) $ approxReal 0.01 optA optB _ -> QC.property False
Instances
Bounded Direction | |
Enum Direction | |
Defined in Numeric.LinearProgramming.Common succ :: Direction -> Direction # pred :: Direction -> Direction # fromEnum :: Direction -> Int # enumFrom :: Direction -> [Direction] # enumFromThen :: Direction -> Direction -> [Direction] # enumFromTo :: Direction -> Direction -> [Direction] # enumFromThenTo :: Direction -> Direction -> Direction -> [Direction] # | |
Show Direction | |
Eq Direction | |
Term a ix |
type Constraints a ix = [Inequality [Term a ix]] #
free :: x -> Inequality x #
(<=.) :: x -> Double -> Inequality x infix 4 #
(>=.) :: x -> Double -> Inequality x infix 4 #
(==.) :: x -> Double -> Inequality x infix 4 #
(>=<.) :: x -> (Double, Double) -> Inequality x infix 4 #
data ModelStatus Source #
Instances
Show ModelStatus Source # | |
Defined in Numeric.HiGHS.LP.Enumeration showsPrec :: Int -> ModelStatus -> ShowS # show :: ModelStatus -> String # showList :: [ModelStatus] -> ShowS # | |
Eq ModelStatus Source # | |
Defined in Numeric.HiGHS.LP.Enumeration (==) :: ModelStatus -> ModelStatus -> Bool # (/=) :: ModelStatus -> ModelStatus -> Bool # |
solve with extra queries on the result
solveWith :: (Indexed sh, Index sh ~ ix) => Query sh result -> Method -> Bounds ix -> Constraints Double ix -> (Direction, Objective sh) -> (ModelStatus, Maybe result) Source #
>>>
case Shape.indexTupleFromShape tripletShape of (x,y,z) -> fmap (mapSnd (mapPair (Array.toTuple, Array.toList))) $ snd $ LP.solveWith (liftA2 (,) LP.getObjectiveValue LP.getBasisStatus) LP.simplex [] [[2.*x, 1.*y] <=. 10, [1.*y, (5::Double).*z] <=. 20] (LP.Maximize, Array.fromTuple (4,-3,2) :: Array.Array TripletShape Double)
Just (28.0,((Highs.basisStatusBasic,Highs.basisStatusLower,Highs.basisStatusBasic),[Highs.basisStatusUpper,Highs.basisStatusUpper]))
forAllMethod $ \method -> TestLP.forAllOrigin $ \origin -> TestLP.forAllProblem origin $ \bounds constrs -> QC.forAll (TestLP.genObjective origin) $ \(dir,obj) -> case (snd $ LP.solve method bounds constrs (dir,obj), snd $ LP.solveWith LP.getSolutionVectors method bounds constrs (dir,obj)) of (Just (_,sol0), Just ((sol1,_),_)) -> sol0 == sol1 _ -> False
getObjectiveValue :: Query sh Double Source #
getSolutionVectors :: C sh => Query sh ((Array sh Double, Array sh Double), (Array ShapeInt Double, Array ShapeInt Double)) Source #
getBasisStatus :: C sh => Query sh (Array sh BasisStatus, Array ShapeInt BasisStatus) Source #
data BasisStatus Source #
Instances
Storable BasisStatus Source # | |
Defined in Numeric.HiGHS.LP.FFI sizeOf :: BasisStatus -> Int # alignment :: BasisStatus -> Int # peekElemOff :: Ptr BasisStatus -> Int -> IO BasisStatus # pokeElemOff :: Ptr BasisStatus -> Int -> BasisStatus -> IO () # peekByteOff :: Ptr b -> Int -> IO BasisStatus # pokeByteOff :: Ptr b -> Int -> BasisStatus -> IO () # peek :: Ptr BasisStatus -> IO BasisStatus # poke :: Ptr BasisStatus -> BasisStatus -> IO () # | |
Show BasisStatus Source # | |
Defined in Numeric.HiGHS.LP.FFI showsPrec :: Int -> BasisStatus -> ShowS # show :: BasisStatus -> String # showList :: [BasisStatus] -> ShowS # |