Esempio n. 1
0
import os

from hips import HIPSArray
from hips.constants import VarTypes, LPSense
from hips.heuristics._line_search_diving import LineSearchDiving
from hips.heuristics._rens import RENS
from hips.loader import load_mps_advanced
from hips.models import LPModel, MIPModel
from hips.solver import GurobiSolver
import numpy as np

#mip_model = MIPModel(GurobiSolver())
# Advanced
#load_mps_advanced(mip_model, path=os.getcwd() + "/mps_files/flugpl.mps")
#mip_model.set_mip_sense(lp_sense=LPSense.MIN)

mip_model = MIPModel(GurobiSolver())
x = mip_model.add_variable("x", VarTypes.INTEGER, lb=0, ub=float("inf"), dim=2)
constr1 = HIPSArray([-3, 2]) * x <= 2
constr2 = HIPSArray([2, 2]) * x <= 7
mip_model.add_constraint(constr1)
mip_model.add_constraint(constr2)
obj_func = HIPSArray([1, 2]) * x
mip_model.set_objective(obj_func)
mip_model.set_mip_sense(LPSense.MAX)

heur = RENS(mip_model)
heur.compute(100)
print(heur.mip_solver._incumbent)
Esempio n. 2
0
    #mip_model.add_constraint(1 * x1 + 1*x2 <= 1)
    #mip_model.add_constraint(1 * x1 + 1 * x2 <= 2)
    #mip_model.set_objective(x1 + x2)
    #mip_model.set_mip_sense(LPSense.MAX)
    #bb = BranchAndBound(mip_model)
    #bb.optimize()
    #print(bb._incumbent)
    #print(bb._incumbent_val)

    #import sys
    #sys.exit(0)

    import gurobipy as gb
    import time

    # start = time.time()
    # model = gb.read("../../examples/mps_files/flugpl.mps")
    # print("Finished loading")
    # model.optimize()
    # print(model.objVal)
    # print(f"GRB: {time.time() - start}")

    start = time.time()
    mip_model = MIPModel(GurobiSolver())
    load_mps_advanced(mip_model, path="../../examples/mps_files/data.mps")
    mip_model.set_mip_sense(LPSense.MIN)
    bb = BranchAndBound(mip_model)
    bb.optimize()
    print(bb._incumbent_val)
    print(f"HIPS B&B: {time.time() - start}")
Esempio n. 3
0
class ClpSolverTest(unittest.TestCase):

    def setUp(self):
        self.solver = ClpSolver()
        self.mip_model = MIPModel(self.solver, binary_variables=[], integer_variables=[])

    def test_add_variable(self):
        return
        variables = [Variable("x{}".format(i), id=i) for i in range(1000)]
        for var in variables:
            self.solver.add_variable(var)
        self.assertEqual(len(self.solver.model.variables), 1000, "Variables not added correctly")
        for var in variables:
            self.assertIn(var.id, self.solver.var_to_clp_var, "Variable not contained in dict")

    def test_remove_variable(self):
        return
        variables = [Variable("x{}".format(i), id=i) for i in range(1000)]
        for var in variables:
            self.solver.add_variable(var)
        for var in variables[:500]:
            self.solver.remove_variable(var)
        self.assertEqual(len(self.solver.var_to_clp_var), 500, "Variables not removed correctly")
        for var in variables[:500]:
            self.assertNotIn(var.id, self.solver.var_to_clp_var, "Variable not removed from dict")

    def test_optimize(self):
        x = Variable("x", id=0)
        y = Variable("y", id=1)
        self.solver.add_variable(x)
        self.solver.add_variable(y)
        self.solver.add_constraint(x + y <= 10)
        self.solver.set_lp_sense(LPSense.MAX)
        self.solver.set_objective(1 * x + y)
        self.solver.optimize()
        print(self.solver.get_objective_value())

    def test_remove_variable_and_constraint(self):
        return
        variables = [Variable("x_{}".format(i), id=i) for i in range(100)]
        for var in variables:
            self.solver.add_variable(var)
        self.solver.add_constraint(sum([1*var for var in variables[10:40]]) <= 10, name="constr1")
        self.solver.remove_constraint(name="constr1")
        for var in variables[10:40]:
            self.solver.remove_variable(var)

    def test_disc_opt_exercise2_4_a(self):
        x1 = self.mip_model.add_variable("x1", var_type=VarTypes.INTEGER, lb=0, ub=8)
        x2 = self.mip_model.add_variable("x2", var_type=VarTypes.INTEGER, lb=0, ub=8)
        self.mip_model.add_constraint(2 * x1 + x2 <= 8)
        self.mip_model.add_constraint(-2 * x1 + 10 * x2 <= 25)
        self.mip_model.set_objective(x1 + x2)
        self.mip_model.set_mip_sense(LPSense.MAX)
        self.mip_model.lp_model.optimize()
        print(self.mip_model.lp_model.get_objective_value())

    def test_set_variable_bounds(self):
        x = self.mip_model.add_variable("x", lb=0, ub=10, dim=2)
        self.solver.set_variable_bound(x, VariableBound.UB, 5)
        self.solver.set_variable_bound(x, VariableBound.LB, 4)
        self.mip_model.set_objective(x)
        self.solver.optimize()
        print(self.mip_model.lp_model.get_objective_value())
Esempio n. 4
0
import os
import numpy as np

from hips.constants import LPSense
from hips.loader.mps_loader import load_mps_advanced
from hips.models import MIPModel
from hips.solver import GurobiSolver, ClpSolver

# HIPS
import time
from hips.loader.old_loader import load_mps
start = time.time()
mip_model = MIPModel(GurobiSolver())
# Advanced
load_mps_advanced(mip_model, path=os.getcwd() + "/mps_files/10teams.mps")
mip_model.set_mip_sense(lp_sense=LPSense.MIN)
print(f"Number of binary variables: {len(mip_model.binary_variables)}")
print(f"Number of constraints: {len(mip_model.lp_model.constraints)}")
from hips.heuristics._feasibility_pump import FeasibilityPump
fs = FeasibilityPump(mip_model, seed=None)
fs.compute(1000, t=20)
print("Feasibility pump obj value: {}".format(fs.get_objective_value()))

end = time.time()
print("{} seconds".format(end - start))

sol = {x: fs.variable_solution(x) for x in mip_model.lp_model.vars}
print("Solution feasible: {}".format(mip_model.is_feasible(sol)))
fs.tracker.plot("objective value")
fs.tracker.plot("feasibility objective")
Esempio n. 5
0
 def setUp(self):
     self.solver = ClpSolver()
     self.mip_model = MIPModel(self.solver, binary_variables=[], integer_variables=[])