Source code for naginterfaces.library.examples.mip.handle_solve_milp_ex
#!/usr/bin/env python3
"``naginterfaces.library.mip.handle_solve_milp`` Python Example."
# NAG Copyright 2018-2020.
# pylint: disable=invalid-name
from naginterfaces.base import utils
from naginterfaces.library import opt, mip
[docs]def main():
"""
Example for :func:`naginterfaces.library.mip.handle_solve_milp`.
Large-scale mixed integer linear programming.
>>> main()
naginterfaces.library.mip.handle_solve_milp Python Example Results.
Solve a small MILP problem.
H02BK, Solver for MILP problems
Status: converged, an optimal solution found
Final primal objective value 1.390000E+01
Final dual objective bound 1.390000E+01
"""
print(
'naginterfaces.library.mip.handle_solve_milp Python Example Results.'
)
print('Solve a small MILP problem.')
# The problem size:
n = 2
# 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=[5.5, 2.1],
)
# Set box constraints:
opt.handle_set_simplebounds(
handle,
bl=[0., 0.],
bu=[1.e20, 1.e20],
)
# Set linear constraints:
opt.handle_set_linconstr(
handle,
bl=[-1.e20, -1.e20],
bu=[2., 17.],
irowb=[
1, 1,
2, 2,
],
icolb=[
1, 2,
1, 2,
],
b=[
-1., 0.5,
8., 2.,
],
)
# Set integer variables
opt.handle_set_property(handle=handle, ptype='Int', idx=[1, 2])
# Set some algorithmic options.
for option in [
'Print Options = NO',
'Print Level = 1',
'Task = Maximize',
]:
opt.handle_opt_set(handle, option)
# Use an explicit I/O manager for abbreviated iteration output:
iom = utils.FileObjManager(locus_in_output=False)
# Solve the problem.
mip.handle_solve_milp(handle, io_manager=iom)
# 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
)