#!/usr/bin/env python3
"""
``naginterfaces.library.opt.handle_solve_pennon`` Python Example,
with linear matrix inequality constraints.
"""
# NAG Copyright 2018-2019.
# pylint: disable=invalid-name,too-many-arguments,too-many-locals,too-many-statements
import numpy as np
from naginterfaces.library import opt
from naginterfaces.base import utils as b_utils
[docs]def main():
"""
Example for :func:`naginterfaces.library.opt.handle_solve_pennon`,
with linear matrix inequality constraints.
Semidefinite programming using Pennon.
>>> main()
naginterfaces.library.opt.handle_solve_pennon Python Example Results.
Find the Lovasz theta number for a Petersen Graph.
Lovasz theta number of the given graph is 4.00.
"""
print(
'naginterfaces.library.opt.handle_solve_pennon Python Example Results.'
)
print('Find the Lovasz theta number for a Petersen Graph.')
# The graph structure:
# Number of vertices in the graph:
nv = 10
# Number of edges in the graph:
ne = 15
va = np.empty(ne, dtype=b_utils.EngineIntNumPyType)
vb = np.empty(ne, dtype=b_utils.EngineIntNumPyType)
maxe = ne
ne = 0
ij = [
(1, 2),
(2, 3),
(3, 4),
(4, 5),
(1, 5),
(1, 6),
(2, 7),
(3, 8),
(4, 9),
(5, 10),
(6, 8),
(6, 9),
(7, 9),
(7, 10),
(8, 10),
]
for idx in range(maxe):
i = ij[idx][0]
j = ij[idx][1]
if 1 <= i < j <= nv:
va[ne] = i
vb[ne] = j
ne += 1
# Initial estimate of the solution:
nvar = ne + 1
x = [0.]*nvar
# Create a handle for the problem:
handle = opt.handle_init(nvar)
# Define the quadratic objective:
opt.handle_set_quadobj(
handle, idxc=[1], c=[1.],
)
# Define the linear matrix constraint:
nnzasum = ne + nv + (nv + 1) * nv // 2
nnza = np.empty(nvar + 1, dtype=b_utils.EngineIntNumPyType)
irowa = np.empty(nnzasum, dtype=b_utils.EngineIntNumPyType)
icola = np.empty(nnzasum, dtype=b_utils.EngineIntNumPyType)
a = np.empty(nnzasum)
idx = 0
nnza[0] = (nv + 1) * nv // 2
for i in range(1, nv+1):
for j in range(i, nv+1):
irowa[idx] = i
icola[idx] = j
a[idx] = 1.0
idx += 1
nnza[1] = nv
for i in range(1, nv+1):
irowa[idx] = i
icola[idx] = i
a[idx] = 1.0
idx += 1
for i in range(0, ne):
nnza[2 + i] = 1
irowa[idx] = va[i]
icola[idx] = vb[i]
a[idx] = 1.0
idx += 1
opt.handle_set_linmatineq(
handle, nv, nnza, irowa, icola, a,
)
opt.handle_opt_set(
handle, 'Initial X = Automatic',
)
opt.handle_opt_set(
handle, 'Print Level = 0',
)
opt.handle_opt_set(
handle, 'Print Options = No',
)
# Solve the problem:
theta = opt.handle_solve_pennon(handle, x, inform=0).rinfo[0]
print(
'Lovasz theta number of the given graph is {:7.2f}.'.format(theta)
)
# 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
)