#!/usr/bin/env python3
"``naginterfaces.library.opt.handle_solve_lp_simplex`` Python Example."
# NAG Copyright 2018-2020.
# pylint: disable=invalid-name
from naginterfaces.base import utils
from naginterfaces.library import opt
[docs]def main():
"""
Example for :func:`naginterfaces.library.opt.handle_solve_lp_simplex`.
Large-scale linear programming based on a simplex method.
>>> main()
naginterfaces.library.opt.handle_solve_lp_simplex Python Example Results.
Solve a small LP problem.
E04MK, Simplex method for LP problems
Status: converged, an optimal solution found
Final objective value 2.359648E-02
"""
print(
'naginterfaces.library.opt.handle_solve_lp_simplex Python Example Results.'
)
print('Solve a small LP problem.')
# The problem size:
n = 7
# Create a handle for the problem:
handle = opt.handle_init(nvar=n)
# Set the objective function:
opt.handle_set_quadobj(
handle,
idxc=list(range(1, n+1)),
c=[-0.02, -0.2, -0.2, -0.2, -0.2, 0.04, 0.04],
)
# Set box constraints:
opt.handle_set_simplebounds(
handle,
bl=[-0.01, -0.1, -0.01, -0.04, -0.1, -0.01, -0.01],
bu=[0.01, 0.15, 0.03, 0.02, 0.05, 1.e20, 1.e20],
)
# Set linear constraints:
opt.handle_set_linconstr(
handle,
bl=[-0.13, -1.e20, -1.e20, -1.e20, -1.e20, -0.0992, -0.003],
bu=[-0.13, -0.0049, -0.0064, -0.0037, -0.0012, 1.e20, 0.002],
irowb=[
1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4,
5, 5, 5,
6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7,
],
icolb=[
1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6, 7,
1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5,
1, 2, 5,
1, 2, 3, 4, 5, 6,
1, 2, 3, 4, 5, 6, 7,
],
b=[
1., 1., 1., 1., 1., 1., 1.,
0.15, 0.04, 0.02, 0.04, 0.02, 0.01, 0.03,
0.03, 0.05, 0.08, 0.02, 0.06, 0.01,
0.02, 0.04, 0.01, 0.02, 0.02,
0.02, 0.03, 0.01,
0.7, 0.75, 0.8, 0.75, 0.8, 0.97,
0.02, 0.06, 0.08, 0.12, 0.02, 0.01, 0.97,
],
)
# Set some algorithmic options.
for option in [
'Print Options = NO',
'Print Level = 1',
]:
opt.handle_opt_set(handle, option)
# Use an explicit I/O manager for abbreviated iteration output:
iom = utils.FileObjManager(locus_in_output=False)
# The initial guess:
x = [0.0, 0.1, 0.02, 0.01, 0.02, 0.0, 0.2]
# Solve the problem.
opt.handle_solve_lp_simplex(handle, x, io_manager=iom)
# Retrieve Basis information
_, basis = opt.handle_set_get_integer(handle, "Basis")
# 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
)