Source code for cplex._internal._multiobjsoln

# --------------------------------------------------------------------------
# File: _multiobjsoln.py
# ---------------------------------------------------------------------------
# Licensed Materials - Property of IBM
# 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55 5655-Y21
# Copyright IBM Corporation 2008, 2024. All Rights Reserved.
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with
# IBM Corp.
# ------------------------------------------------------------------------
"""Multi-Objective Solution API"""
from . import _constants as _const
from . import _procedural as _proc
from ._baseinterface import BaseInterface
from ..constant_class import ConstantClass


[docs] class MultiObjFloatInfo(ConstantClass): """Types of floating point information that can be queried for multi-objective optimization. This class contains the types of solution information of type float that can be retreived from the solution of a sub-problem solved during multi-objective optimization. This information can be querried for each priority level with method `MultiObjSolnInterface.get_info`. """ time = _const.CPX_MULTIOBJ_TIME """See `CPX_MULTIOBJ_TIME <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_TIME.html>`_ in the C API.""" dettime = _const.CPX_MULTIOBJ_DETTIME """See `CPX_MULTIOBJ_DETTIME <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_DETTIME.html>`_ in the C API.""" objective = _const.CPX_MULTIOBJ_OBJVAL """See `CPX_MULTIOBJ_OBJVAL <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_OBJVAL.html>`_ in the C API.""" best_objective = _const.CPX_MULTIOBJ_BESTOBJVAL """See `CPX_MULTIOBJ_BESTOBJVAL <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_BESTOBJVAL.html>`_ in the C API."""
[docs] class MultiObjIntInfo(ConstantClass): """Types of integer information that can be queried for multi-objective optimization. This class contains the types of solution information of type float that can be retreived from the solution of a sub-problem solved during multi-objective optimization. This information can be querried for each priority level with method `MultiObjSolnInterface.get_info`. """ error = _const.CPX_MULTIOBJ_ERROR """See `CPX_MULTIOBJ_ERROR <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_ERROR.html>`_ in the C API.""" status = _const.CPX_MULTIOBJ_STATUS """See `CPX_MULTIOBJ_STATUS <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_STATUS.html>`_ in the C API.""" method = _const.CPX_MULTIOBJ_METHOD """See `CPX_MULTIOBJ_METHOD <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_METHOD.html>`_ in the C API.""" priority = _const.CPX_MULTIOBJ_PRIORITY """See `CPX_MULTIOBJ_PRIORITY <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_PRIORITY.html>`_ in the C API.""" blend = _const.CPX_MULTIOBJ_BLEND """See `CPX_MULTIOBJ_BLEND <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_BLEND.html>`_ in the C API.""" pfeas = _const.CPX_MULTIOBJ_PFEAS """See `CPX_MULTIOBJ_PFEAS <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_PFEAS.html>`_ in the C API.""" dfeas = _const.CPX_MULTIOBJ_DFEAS """See `CPX_MULTIOBJ_DFEAS <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_DFEAS.html>`_ in the C API."""
[docs] class MultiObjLongInfo(ConstantClass): """Types of long integer information that can be queried for multi-objective optimization. This class contains the types of solution information of type float that can be retreived from the solution of a sub-problem solved during multi-objective optimization. This information can be querried for each priority level with method `MultiObjSolnInterface.get_info`. """ num_barrier_iterations = _const.CPX_MULTIOBJ_BARITCNT """See `CPX_MULTIOBJ_BARITCNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_BARITCNT.html>`_ in the C API.""" num_sifting_iterations = _const.CPX_MULTIOBJ_SIFTITCNT """See `CPX_MULTIOBJ_SIFTITCNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_SIFTITCNT.html>`_ in the C API.""" num_sifting_phase1_iterations = _const.CPX_MULTIOBJ_SIFTPHASE1CNT """See `CPX_MULTIOBJ_SIFTPHASE1CNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_SIFTPHASE1CNT.html>`_ in the C API.""" num_degenerate_iterations = _const.CPX_MULTIOBJ_DEGCNT """See `CPX_MULTIOBJ_DEGCNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_DEGCNT.html>`_ in the C API.""" num_iterations = _const.CPX_MULTIOBJ_ITCNT """See `CPX_MULTIOBJ_ITCNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_ITCNT.html>`_ in the C API.""" num_phase1_iterations = _const.CPX_MULTIOBJ_PHASE1CNT """See `CPX_MULTIOBJ_PHASE1CNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_PHASE1CNT.html>`_ in the C API.""" num_primal_pushes = _const.CPX_MULTIOBJ_PPUSH """See `CPX_MULTIOBJ_PPUSH <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_PPUSH.html>`_ in the C API.""" num_primal_exchanges = _const.CPX_MULTIOBJ_PEXCH """See `CPX_MULTIOBJ_PEXCH <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_PEXCH.html>`_ in the C API.""" num_dual_pushes = _const.CPX_MULTIOBJ_DPUSH """See `CPX_MULTIOBJ_DPUSH <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_DPUSH.html>`_ in the C API.""" num_dual_exchanges = _const.CPX_MULTIOBJ_DEXCH """See `CPX_MULTIOBJ_DEXCH <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_DEXCH.html>`_ in the C API.""" num_nodes = _const.CPX_MULTIOBJ_NODECNT """See `CPX_MULTIOBJ_NODECNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_NODECNT.html>`_ in the C API.""" num_nodes_left = _const.CPX_MULTIOBJ_NODELEFTCNT """See `CPX_MULTIOBJ_NODELEFTCNT <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/macros/CPX_MULTIOBJ_NODELEFTCNT.html>`_ in the C API."""
[docs] class MultiObjSolnInterface(BaseInterface): """Methods for accessing solutions for multi-objective models. The methods in this interface can be used to query solutions for multi-objective models. See also `MultiObjInterface` where methods for adding, querying, and modifying multiple objectives can be found. """ float_info = MultiObjFloatInfo() """See `MultiObjFloatInfo()` """ int_info = MultiObjIntInfo() """See `MultiObjIntInfo()` """ long_info = MultiObjLongInfo() """See `MultiObjLongInfo()` """
[docs] def __init__(self, parent): """Creates a new MIPSolutionInterface. The multi-objective solution interface is exposed by the top-level `Cplex` class as Cplex.solution.multiobj. This constructor is not meant to be used externally. """ super().__init__(cplex=parent._cplex, advanced=True)
[docs] def get_objective_value(self, objidx): """Returns the value of an objective function. objidx is the name or index of the objective to be accessed. See `CPXmultiobjgetobjval <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetobjval.html>`_ in the Callable Library Reference Manual for more detail. Example usage: >>> import cplex >>> c = cplex.Cplex() >>> out = c.set_results_stream(None) >>> out = c.set_log_stream(None) >>> c.read("example.mps") >>> c.multiobj.set_num(2) >>> c.multiobj.set_linear( ... 1, ... [(i, o) for i, o in enumerate(c.multiobj.get_linear(0))] ... ) >>> c.solve() >>> c.solution.multiobj.get_objective_value(0) -202.5 >>> c.solution.multiobj.get_objective_value(1) -202.5 """ objidx = self._cplex.multiobj._conv(objidx) return _proc.multiobjgetobjval( self._env._e, self._cplex._lp, objidx)
[docs] def get_objval_by_priority(self, priority): """Returns the value of an objective function by priority. After multi-objective optimization, returns the blended objective value for the specified priority. See `CPXmultiobjgetobjvalbypriority <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetobjvalbypriority.html>`_ in the Callable Library Reference Manual for more detail. Example usage: >>> import cplex >>> c = cplex.Cplex() >>> out = c.set_results_stream(None) >>> out = c.set_log_stream(None) >>> c.read("example.mps") >>> c.multiobj.set_num(2) >>> c.multiobj.set_linear( ... 1, ... [(i, o) for i, o in enumerate(c.multiobj.get_linear(0))] ... ) >>> c.multiobj.set_priority(0, 1) >>> c.multiobj.set_priority(1, 2) >>> c.solve() >>> c.solution.multiobj.get_objval_by_priority(1) -202.5 >>> c.solution.multiobj.get_objval_by_priority(2) -202.5 """ return _proc.multiobjgetobjvalbypriority( self._env._e, self._cplex._lp, priority)
[docs] def get_num_solves(self): """Returns the number of sub-problems that where successfully solved during the last optimization of a multi-objective problem. See `CPXmultiobjgetnumsolves <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetnumsolves.html>`_ in the Callable Library Reference Manual for more detail. Example usage: >>> import cplex >>> c = cplex.Cplex() >>> out = c.set_results_stream(None) >>> out = c.set_log_stream(None) >>> c.read("example.mps") >>> c.multiobj.set_num(2) >>> c.multiobj.set_linear( ... 1, ... [(i, o) for i, o in enumerate(c.multiobj.get_linear(0))] ... ) >>> c.multiobj.set_priority(0, 1) >>> c.multiobj.set_priority(1, 2) >>> c.solve() >>> c.solution.multiobj.get_num_solves() 2 """ return _proc.multiobjgetnumsolves(self._env._e, self._cplex._lp)
@staticmethod def _isintinfo(what): try: _ = MultiObjSolnInterface.int_info[what] return True except KeyError: return False @staticmethod def _islonginfo(what): try: _ = MultiObjSolnInterface.long_info[what] return True except KeyError: return False @staticmethod def _isdblinfo(what): try: _ = MultiObjSolnInterface.float_info[what] return True except KeyError: return False
[docs] def get_info(self, subprob, what): """Returns the solution information of a sub-problem of a multi-objective optimization. subprob is the sub-problem of a multi-objective optimization. what is an attribute from Cplex.solution.multiobj.float_info (see `MultiObjFloatInfo`), Cplex.solution.multiobj.int_info (see `MultiObjIntInfo`), or Cplex.solution.multiobj.long_info (see `MultiObjLongInfo`). See `CPXmultiobjgetdblinfo <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetdblinfo.html>`_, `CPXmultiobjgetintinfo <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetintinfo.html>`_, `CPXmultiobjgetlonginfo <https://www.ibm.com/docs/en/SSSA5P_22.1.2/ilog.odms.cplex.help/refcallablelibrary/cpxapi/multiobjgetlonginfo.html>`_, etc. in the Callable Library Reference Manual for more detail. Example usage: >>> import cplex >>> c = cplex.Cplex() >>> out = c.set_results_stream(None) >>> out = c.set_log_stream(None) >>> c.read("example.mps") >>> c.multiobj.set_num(2) >>> c.multiobj.set_linear( ... 1, ... [(i, o) for i, o in enumerate(c.multiobj.get_linear(0))] ... ) >>> c.multiobj.set_priority(0, 1) >>> c.multiobj.set_priority(1, 2) >>> c.solve() >>> num = c.solution.multiobj.get_num_solves() >>> for i in range(num): ... priority = c.solution.multiobj.get_info( ... i, ... c.solution.multiobj.int_info.priority ... ) """ if MultiObjSolnInterface._isintinfo(what): return _proc.multiobjgetintinfo(self._env._e, self._cplex._lp, subprob, what) if MultiObjSolnInterface._islonginfo(what): return _proc.multiobjgetlonginfo(self._env._e, self._cplex._lp, subprob, what) if MultiObjSolnInterface._isdblinfo(what): return _proc.multiobjgetdblinfo(self._env._e, self._cplex._lp, subprob, what) raise ValueError(what)