How to use the mip.OptimizationStatus.OPTIMAL function in mip

To help you get started, we’ve selected a few mip examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github coin-or / python-mip / test / rcpsp_instances.py View on Github external
best_z_ub = None
        best_z_lb = None

        for tr in range(tries):
            inst = gen_inst(nj)
            J = inst["J"]
            dur = inst["d"]
            c = inst["c"]
            r = inst["r"]
            S = inst["S"]
            EST = inst["EST"]
            mip = create_mip("gurobi", J, dur, S, c, r, EST)
            mip.verbose = 0
            mip.relax()
            mip.optimize()
            assert mip.status == OptimizationStatus.OPTIMAL
            z_relax = mip.objective_value
            mip = create_mip("gurobi", J, dur, S, c, r, EST)
            mip.verbose = 0
            mip.max_nodes = 512
            mip.optimize()
            z_lb = mip.objective_bound
            z_ub = mip.objective_value
            assert mip.status in [
                OptimizationStatus.OPTIMAL,
                OptimizationStatus.FEASIBLE,
            ]
            sum_dur = sum(dur[i] for i in J)
            score = (
                (z_ub - z_relax) / sum_dur
                + 2 * (z_ub - z_relax)
                + 5 * (z_ub - z_lb)
github coin-or / python-mip / mip / entities.py View on Github external
def xi(self, i: int) -> Optional[numbers.Real]:
        """Value for this variable in the :math:`i`-th solution from the solution
        pool. Note that None is returned if the solution is not available."""
        if self.__model.status in [
            mip.OptimizationStatus.OPTIMAL,
            mip.OptimizationStatus.FEASIBLE,
        ]:
            return self.__model.solver.var_get_xi(self, i)
        return None
github coin-or / python-mip / mip / cbc.py View on Github external
else:
            cbclib.Cbc_setLogLevel(self._model, 1)

        if relax:
            self.__clear_sol()
            res = Cbc_solveLinearProgram(self._model)
            if res == 0:
                self.__x = cbclib.Cbc_getColSolution(self._model)
                self.__rc = cbclib.Cbc_getReducedCost(self._model)
                self.__pi = cbclib.Cbc_getRowPrice(self._model)
                self.__slack = cbclib.Cbc_getRowSlack(self._model)
                self.__obj_val = cbclib.Cbc_getObjValue(self._model) + self._objconst
                self.__obj_bound = self.__obj_val
                self.__num_solutions = 1

                return OptimizationStatus.OPTIMAL
            if res == 2:
                return OptimizationStatus.UNBOUNDED
            if res == 3:
                return OptimizationStatus.INFEASIBLE
            return OptimizationStatus.ERROR

        # adding cut generators
        m = self.model
        if m.cuts_generator is not None:
            atSol = CHAR_ZERO
            cbclib.Cbc_addCutCallback(
                self._model,
                cbc_cut_callback,
                "UserCuts".encode("utf-8"),
                ffi.NULL,
                1,
github coin-or / python-mip / mip / model.py View on Github external
This method only works with the CBC mip solver, as Gurobi does not
        supports calling only cut generators.

        Args:
            cut_types (List[CutType]): types of cuts that can be generated, if
                an empty list is specified then all available cut generators
                will be called.
            max_cuts(int): cut separation will stop when at least max_cuts
                violated cuts were found.
            min_viol(float): cuts which are not violated by at least min_viol
                will be discarded.


        """
        if self.status != mip.OptimizationStatus.OPTIMAL:
            raise mip.SolutionNotAvailable()

        return self.solver.generate_cuts(cut_types, max_cuts)
github coin-or / python-mip / mip / cbc.py View on Github external
def constr_get_slack(self, constr: Constr) -> Optional[float]:
        if self.model.status not in [
            OptimizationStatus.OPTIMAL,
            OptimizationStatus.FEASIBLE,
        ]:
            return None
        pac = cbclib.Osi_getRowActivity(self.osi)
        if pac == ffi.NULL:
            return None
        rhs = cbclib.Osi_getRowRHS(self.osi, constr.idx)
        activity = pac[constr.idx]

        sense = cbclib.Osi_getRowSense(self.osi, constr.idx).decode("utf-8").upper()

        if sense in "G":
            return activity - rhs
        if sense in "=E":
github coin-or / python-mip / examples / tsp-compact-ulysses22.py View on Github external
# checking if a solution was found
if model.num_solutions:
    out.write("route with total distance %g found: %s" % (model.objective_value, 0))
    nc = 0
    while True:
        nc = [i for i in V if x[nc][i].x >= 0.99][0]
        out.write(" -> %s" % nc)
        if nc == 0:
            break
    out.write("\n")

# sanity tests
from mip import OptimizationStatus

if model.status == OptimizationStatus.OPTIMAL:
    assert round(model.objective_value) == 7013
elif model.status == OptimizationStatus.FEASIBLE:
    assert round(model.objective_value) >= 7013
else:
    assert model.objective_bound <= 7013 + 1e-7
model.check_optimization_results()
github coin-or / python-mip / examples / rcpsp.py View on Github external
for (j, s) in S:
    model += xsum(t * x[s][t] - t * x[j][t] for t in T) >= p[j]

model.optimize()

print('Schedule: ')
for (j, t) in product(J, T):
    if x[j][t].x >= 0.99:
        print('({},{})'.format(j, t))
print('Makespan = {}'.format(model.objective_value))


# sanity tests
from mip import OptimizationStatus
assert model.status == OptimizationStatus.OPTIMAL
assert round(model.objective_value) == 21
github coin-or / python-mip / examples / tsp-cuts-ulysses22.py View on Github external
model.optimize(max_seconds=500)

# checking if a solution was found
if model.num_solutions:
    out.write('route with total distance %g found: %s'
              % (model.objective_value, 0))
    nc = 0
    while True:
        nc = [i for i in V if x[nc][i].x >= 0.99][0]
        out.write(' -> %s' % nc)
        if nc == 0:
            break
    out.write('\n')

# sanity tests
if model.status == OptimizationStatus.OPTIMAL:
    assert round(model.objective_value) == 7013
elif model.status == OptimizationStatus.FEASIBLE:
    assert round(model.objective_value) >= 7013
else:
    assert model.objective_bound <= 7013 + 1e-7
github coin-or / python-mip / mip / cbc.py View on Github external
def get_status(self) -> OptimizationStatus:
        if cbclib.Osi_isProvenOptimal(self.osi):
            return OptimizationStatus.OPTIMAL

        if cbclib.Osi_isProvenPrimalInfeasible(
            self.osi
        ) or cbclib.Osi_isProvenDualInfeasible(self.osi):
            return OptimizationStatus.INFEASIBLE
        elif cbclib.Osi_isAbandoned(self.osi):
            return OptimizationStatus.ERROR
        return OptimizationStatus.LOADED