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

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

# NAG Copyright 2017-2019.

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

from naginterfaces.library import opt

[docs]def main(): """ Example for :func:`naginterfaces.library.opt.qpconvex2_sparse_solve`. Sparse QP/LP. >>> main() naginterfaces.library.opt.qpconvex2_sparse_solve Python Example Results. Sparse QP/LP. Function value at lowest point found is -1847784.67712. The corresponding x is: (0.00, 349.40, 648.85, 172.85, 407.52, 271.36, 150.02). """ print( 'naginterfaces.library.opt.qpconvex2_sparse_solve ' 'Python Example Results.' ) print('Sparse QP/LP.') # Cold start: start = 'Cold' cb_qphx = lambda x, _nstate: ( [ 2*x[0], 2*x[1], 2*(x[2] + x[3]), 2*(x[2] + x[3]), 2*x[4], 2*(x[5] + x[6]), 2*(x[5] + x[6]), ] ) # The sparse A stored by column: a = [ 0.02, 0.02, 0.03, 1.00, 0.70, 0.02, 0.15, -200.00, 0.06, 0.75, 0.03, 0.04, 0.05, 0.04, 1.00, -2000.00, 0.02, 1.00, 0.01, 0.08, 0.08, 0.80, -2000.00, 1.00, 0.12, 0.02, 0.02, 0.75, 0.04, -2000.00, 0.01, 0.80, 0.02, 1.00, 0.02, 0.06, 0.02, -2000.00, 1.00, 0.01, 0.01, 0.97, 0.01, 400.00, 0.97, 0.03, 1.00, 400.00, ] # The index information for the packed A: loca = [1, 9, 17, 24, 31, 39, 45, 49] inda = [ 7, 5, 3, 1, 6, 4, 2, 8, 7, 6, 5, 4, 3, 2, 1, 8, 2, 1, 4, 3, 7, 6, 8, 1, 7, 3, 4, 6, 2, 8, 5, 6, 7, 1, 2, 3, 4, 8, 1, 2, 3, 6, 7, 8, 7, 2, 1, 8, ] # In this example, we make the objective vector the final column of A: c = [0.] iobj = 8 # The number of leading nonzero columns of the Hessian matrix: ncolh = 7 # The bounds on x and on the linear constraints: m = 8 bl = [ 0., 0., 400., 100., 0., 0., 0., 2000., -1.e20, -1.e20, -1.e20, -1.e20, 1500., 250., -1.e20, ] bu = [ 200., 2500., 800., 700., 1500., 1.e20, 1.e20, 2000., 60., 100., 40., 30., 1.e20, 300., 1.e20, ] # No constant objective term: objadd = 0. # Row and column names: names = [ '...X1...', '...X2...', '...X3...', '...X4...', '...X5...', '...X6...', '...X7...', '..ROW1..', '..ROW2..', '..ROW3..', '..ROW4..', '..ROW5..', '..ROW6..', '..ROW7..', '..COST..', ] # Blank problem name: prob = '' # No elastic variables: n = len(loca) - 1 helast = [0]*(n+m) # Initial state for cold start: hs = [0]*(n+m) x = [0.]*(n+m) # No superbasics to specify: ns = 0 # Initialize the communication structure: comm = opt.qpconvex2_sparse_init() qp_soln = opt.qpconvex2_sparse_solve( start, m, ncolh, iobj, objadd, prob, a, inda, loca, bl, bu, names, helast, hs, x, ns, comm, qphx=cb_qphx, c=c, ) print( 'Function value at lowest point found is {:.5f}.'.format(qp_soln.obj) ) print('The corresponding x is:') print('(' + ', '.join(['{:.2f}']*n).format(*qp_soln.x[:n]) + ').')
if __name__ == '__main__': import doctest import sys sys.exit( doctest.testmod( None, verbose=True, report=False, optionflags=doctest.REPORT_NDIFF, ).failed )