#!/usr/bin/env python3
"""
``naginterfaces.library.opt.handle_solve_pennon`` Python Example,
with bilinear matrix inequality constraints.
"""
# NAG Copyright 2018-2019.
# pylint: disable=invalid-name,too-many-arguments,too-many-locals,too-many-statements
from naginterfaces.library import opt
[docs]def main():
"""
Example for :func:`naginterfaces.library.opt.handle_solve_pennon`,
with bilinear matrix inequality constraints.
Semidefinite programming using Pennon.
>>> main()
naginterfaces.library.opt.handle_solve_pennon Python Example Results.
BMI-SDP.
Final objective value is 2.000000
at the point:
(1.000e+00, 1.029e-15, 1.000e+00, 1.314e+03, 1.311e+03).
"""
print(
'naginterfaces.library.opt.handle_solve_pennon Python Example Results.'
)
print('BMI-SDP.')
# The number of decision variables:
nvar = 5
# Create a handle for the problem:
handle = opt.handle_init(nvar)
# Define the linear objective function:
opt.handle_set_linobj(
handle,
cvec=[1., 0., 1., 0., 0.],
)
# Define the first linear matrix constraint:
opt.handle_set_linmatineq(
handle,
dima=2, nnza=[2, 2, 3, 2, 0, 0],
irowa=[1, 2, 1, 1, 1, 1, 2, 1, 2], icola=[1, 2, 1, 2, 1, 2, 2, 2, 2],
a=[1.0, 1.0, 2.0, -2.0, 6.0, 5.0, -4.0, 3.0, 8.0],
)
# Define the bilinear matrix constraint:
opt.handle_set_quadmatineq(
handle,
qi=[1, 2, 3, 1, 2, 3], qj=[4, 4, 4, 5, 5, 5],
dimq=2, nnzq=[1, 2, 1, 1, 2, 1],
irowq=[1, 1, 1, 1, 1, 1, 2, 2], icolq=[1, 1, 2, 2, 2, 2, 2, 2],
q=[2.0, 2.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0], idblk=1,
)
# Define the second linear matrix constraint:
opt.handle_set_linmatineq(
handle,
dima=2, nnza=[2, 1, 1, 1, 0, 0],
irowa=[1, 2, 1, 1, 2], icola=[1, 2, 1, 2, 2],
a=[1.0, 1.0, 1.0, 1.0, 1.0],
)
# Initial estimate of the solution:
x = [0.]*nvar
opt.handle_opt_set(
handle, 'Print Level = 0',
)
opt.handle_opt_set(
handle, 'Print Options = No',
)
# Solve the problem:
pennon_sol = opt.handle_solve_pennon(handle, x, inform=0)
print('Final objective value is {:f}'.format(pennon_sol.rinfo[0]))
print('at the point:')
print('(' + ', '.join(['{:.3e}']*nvar).format(*pennon_sol.x) + ').')
# Destroy the handle:
opt.handle_free(handle)
if __name__ == '__main__':
import doctest
import sys
sys.exit(
doctest.testmod(
None, verbose=True, report=False,
optionflags=doctest.ELLIPSIS | doctest.REPORT_NDIFF,
).failed
)