#!/usr/bin/env python3
"``naginterfaces.library.opt.nlp2_sparse_solve`` Python Example."
# NAG Copyright 2017-2022.
# pylint: disable=invalid-name,too-many-locals
from math import cos, sin
import warnings
import numpy as np
from naginterfaces.base import utils
from naginterfaces.library import opt
[docs]def main():
"""
Example for :func:`naginterfaces.library.opt.nlp2_sparse_solve`.
Sparse NLP.
>>> main()
naginterfaces.library.opt.nlp2_sparse_solve Python Example Results.
Solve Hock and Schittkowski Problem 74.
Final objective value is 5.1264981e+03
"""
print(
'naginterfaces.library.opt.nlp2_sparse_solve Python Example Results.'
)
print('Solve Hock and Schittkowski Problem 74.')
def cb_usrfun(_status, x, needf, f, needg, g):
"""The nonlinear constraints."""
if needf > 0:
f[:3] = [
1000.*sin(-x[0]-0.25) + 1000.*sin(-x[1]-0.25),
1000.*sin(x[0]-0.25) + 1000.*sin(x[0]-x[1]-0.25),
1000.*sin(x[1]-0.25) + 1000.*sin(x[1]-x[0]-0.25),
]
# There is no need to define the wholly linear
# components f_3(x) and f_4(x)...
f[5] = 1.e-6*x[2]**3 + 2.e-6*x[3]**3/3.
if needg > 0:
g = [
-1000.*cos(-x[0]-0.25),
-1000.*cos(-x[1]-0.25),
1000.*cos(x[0]-0.25) + 1000.*cos(x[0]-x[1]-0.25),
-1000.*cos(x[0]-x[1]-0.25),
-1000.*cos(x[1]-x[0]-0.25),
1000.*cos(x[1]-x[0]-0.25) + 1000.*cos(x[1]-0.25),
3.e-6*x[2]**2,
2.e-6*x[3]**2,
]
return 0, f, g
# Silence deprecation warning:
warnings.simplefilter('ignore', utils.NagDeprecatedWarning)
# Initialize the solver:
comm = opt.nlp2_sparse_init()
# Initial guess:
x = [0., 0., 0., 0.]
n = len(x)
# There are six problem functions:
nf = 6
# The linear part of F and its packing information:
a = [
-1., -1., -1., 1., 1., -1., 3., 2.,
]
nea = 8
iafun = [1, 2, 4, 4, 5, 5, 6, 6]
javar = [3, 4, 1, 2, 1, 2, 3, 4]
# The coordinates of the nonzero elements of G:
neg = 8
igfun = [1, 1, 2, 2, 3, 3, 6, 6]
jgvar = [1, 2, 1, 2, 1, 2, 3, 4]
# Variable names:
xnames = ['X1', 'X2', 'X3', 'X4']
# Function names:
fnames = [
'NlnCon 1', 'NlnCon 2', 'NlnCon 3', 'LinCon 1', 'LinCon 2', 'Objectiv',
]
# No constant objective term:
objadd = 0.
# Use row six of F as the objective:
objrow = 6
# Blank problem name:
prob = ''
# The bounds:
xlow = [
-0.55, -0.55, 0., 0.,
]
flow = [
-894.8, -894.8, -1294.8,
-0.55, -0.55,
-1.e+25,
]
xupp = [
0.55, 0.55, 1200., 1200.,
]
fupp = [
-894.8, -894.8, -1294.8,
1.e+25, 1.e+25,
1.e+25,
]
# Cold start:
start = 0
# Initial state:
xstate = np.zeros(n, dtype=utils.EngineIntNumPyType)
f = np.zeros(nf)
fstate = np.zeros(nf, dtype=utils.EngineIntNumPyType)
fmul = np.zeros(nf)
# No superbasics to specify:
ns = 0
nlp_soln = opt.nlp2_sparse_solve(
start, objadd, objrow, prob,
cb_usrfun, iafun, javar, nea, a, igfun, jgvar, neg,
xlow, xupp, xnames, flow, fupp, fnames, x, xstate, f, fstate,
fmul, ns, comm,
)
print('Final objective value is {:.7e}'.format(nlp_soln.f[objrow-1]))
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.REPORT_NDIFF,
).failed
)