packages feed

ipopt-hs-0.2.0.0: examples/Test2.hs

{-# LANGUAGE RankNTypes #-}
{- | Translation of the "number 71 from the Hock-Schittkowsky test suite"
<http://www.coin-or.org/Ipopt/documentation/node28.html> testing AD

-}
module Main where

import qualified Data.Vector.Storable as VS
import Text.Printf ( printf )
import Data.Vector ((!))
import qualified Data.Vector as V
import qualified Data.Vector.Storable as VS
import Ipopt.Raw
    ( addIpoptNumOption,
      addIpoptStrOption,
      ipoptSolve,
      createIpoptProblemAD )

main = do
    let n = 4 -- number of variables
        fromList = VS.unsafeThaw . VS.fromList

    xL <- fromList (replicate n 1)
    xU <- fromList (replicate n 5)

    gL <- fromList [25, 40]
    gU <- fromList [2.0e19,40]

    let f x = x!0 * x!3 * (x!0 + x!1 + x!2) + x!2
        g x = V.fromList
            [x!0 * x!1 * x!2 * x!3,
            x!0*x!0 + x!1*x!1 + x!2*x!2 + x!3*x!3]

    nlp <- createIpoptProblemAD xL xU gL gU f g

    addIpoptNumOption nlp "tol" 1.0e-9
    addIpoptStrOption nlp "mu_strategy" "adaptive"

    x <- fromList [1,5, 5,1]

    ans <- ipoptSolve nlp x

    let x_official = VS.fromList [1, 4.74299964, 3.82114998, 1.37940829]

    x <- VS.freeze x

    let sse :: Double
        sse = realToFrac $ VS.sum $ VS.zipWith (\a b -> (a-b)^2) x x_official

    printf "\n\n||x - x_official||_2 = %f\n" sse