Source code for naginterfaces.library.examples.opt.nlp1_solve_ex

#!/usr/bin/env python3
"``naginterfaces.library.opt.nlp1_solve`` Python Example."

# NAG Copyright 2017-2019.

# pylint: disable=invalid-name,too-many-locals

import numpy as np

from naginterfaces.library import opt

[docs]def main(): """ Example for :func:`naginterfaces.library.opt.nlp1_solve`. Dense NLP. Demonstrates handling optional algorithmic parameters. >>> main() naginterfaces.library.opt.nlp1_solve Python Example Results. Solve Hock and Schittkowski Problem 71. Final objective value is 1.7014017e+01 """ print( 'naginterfaces.library.opt.nlp1_solve Python Example Results.' ) print('Solve Hock and Schittkowski Problem 71.') def cb_confun(mode, needc, x, cjac, _nstate): """The nonlinear constraints.""" c = np.zeros(len(needc)) if needc[0] > 0: if mode in [0, 2]: c[0] = (x[0]**2 + x[1]**2 + x[2]**2 + x[3]**2) if mode == 2: cjac[0, :] = 2*x if needc[1] > 0: if mode in [0, 2]: c[1] = x[0]*x[1]*x[2]*x[3] if mode == 2: cjac[1, :] = [ x[1]*x[2]*x[3], x[0]*x[2]*x[3], x[0]*x[1]*x[3], x[0]*x[1]*x[2], ] return c, cjac def cb_objfun(mode, x, objgrd, _nstate): """The objective function.""" if mode in [0, 2]: objf = x[0]*x[3]*(x[0] + x[1] + x[2]) + x[2] else: objf = 0. if mode == 2: objgrd[:] = [ x[3]*(2*x[0] + x[1] + x[2]), x[0]*x[3], x[0]*x[3] + 1.0, x[0]*(x[0] + x[1] + x[2]), ] return objf, objgrd # Initialize the solver: comm = opt.nlp1_init('nlp1_solve') # The initial guess: x = [1., 5., 5., 1.] # The linear constraints: a = np.array([[1.]*len(x)]) # The bounds: bl = [1., 1., 1., 1., -1.0E+25, -1.0E+25, 25.] bu = [5., 5., 5., 5., 20., 40., 1.0E+25] # To set algorithmic options: opt.nlp1_option_string('Infinite Bound Size = 1.0e20', comm) objf = opt.nlp1_solve( a, bl, bu, cb_objfun, x, comm, confun=cb_confun, ).objf print('Final objective value is {:.7e}'.format(objf))
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )