<jupyter_start><jupyter_text>HeuristicBounds example This heuristic is implemented after the scipopt primal heuristic 'heur_bounds.h'.The scipopt library describes this as a "heuristic which fixes all integer variables to a bound (lower/upper) and solves the remaining LP ".  Installing the packages In the following we use the `GurobiSolver` and thus need to install gurobi.<jupyter_code>%%capture
!pip install gurobipy<jupyter_output><empty_output><jupyter_text>Afterwards, we can install hips.<jupyter_code>%%capture
!pip install https://github.com/cxlvinchau/hips/archive/master.zip<jupyter_output><empty_output><jupyter_text>Example First add the required imports:<jupyter_code>from hips import *
from hips.models import *
from hips.solver import GurobiSolver
from hips.heuristics import HeuristicBounds<jupyter_output><empty_output><jupyter_text>Next we initialize a helper function, which allows us to load a model. The model represents the following MIP:$$\begin{array}{lr@{}c@{}r@{}l}    \text{maximize }   & x_1 + x_2  \\    \text{subject to } & x_1 + \frac{2}{3} x_2 \leq 2 \\                       & x_1 + 3 x_2 \leq 3 \\                       & x_1, x_2 \leq 2\\                       & x_1, x_2 \geq 0 \\                       & x_1, x_2 \in \mathbb{Z}\end{array}$$<jupyter_code>def build_model(mip_model):
    x = mip_model.add_variable("x", VarTypes.INTEGER, lb=0, ub=2, dim=2)
    constr1 = HIPSArray([1,2/3])*x <= 2
    constr2 = HIPSArray([1,3])*x <= 3
    mip_model.add_constraint(constr1)
    mip_model.add_constraint(constr2)
    obj_func = HIPSArray([1,1])*x
    mip_model.set_objective(obj_func)
    mip_model.lp_model.set_lp_sense(ProblemSense.MAX)<jupyter_output><empty_output><jupyter_text>LOWER Now we can load this model and use the `HeuristicBounds` heuristic to solve it to the LOWER bound:<jupyter_code># Test lower bound (LOWER) -> x* = [0,0]
mip_model = MIPModel(GurobiSolver())
build_model(mip_model)
heur = HeuristicBounds(mip_model, BoundDirection.LOWER)
heur.compute()
print("Status: {}".format(heur.get_status()))
print("Found solution: {}".format(heur.get_objective_value()))
print("With Variable values: {}".format({var: heur.variable_solution(var).to_numpy() for var in mip_model.get_variables()}))
print("#---------------------------#")<jupyter_output><empty_output><jupyter_text>UPPER Fixing the variables to their UPPER bound should yield an infeasible solution:<jupyter_code>#Test upper bound (UPPER) -> Infeasible
mip_model = MIPModel(GurobiSolver())
build_model(mip_model)
heur = HeuristicBounds(mip_model, BoundDirection.UPPER)
heur.compute()
print("Status: {}".format(heur.get_status()))
print("#---------------------------#")<jupyter_output><empty_output><jupyter_text>CLOSEST At last we solve to the CLOSEST bounds:<jupyter_code>#Test closest bound (CLOSEST) -> [2,0]
mip_model = MIPModel(GurobiSolver())
build_model(mip_model)
heur = HeuristicBounds(mip_model, BoundDirection.CLOSEST)
heur.compute()
print("Status: {}".format(heur.get_status()))
print("Found solution: {}".format(heur.get_objective_value()))
print("With Variable values: {}".format({var: heur.variable_solution(var).to_numpy() for var in mip_model.get_variables()}))
print("#---------------------------#")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15268817204301074, 'fraction_numerical': 0.011981566820276499, 'mean_word_length': 4.742504409171076, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 32, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '198424', 'license': 'MIT', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1117, 'n_words': 247, 'path': 'notebooks/.ipynb_checkpoints/bounds-example-checkpoint.ipynb', 'repo_name': 'cxlvinchau/hips-examples', 'size': '3255'}