parent
897743fce7
commit
6a29411df3
@ -0,0 +1,63 @@
|
|||||||
|
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||||
|
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||||
|
|
||||||
|
from . import Component
|
||||||
|
from .transformers import PerVariableTransformer
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
class BranchPriorityComponent(Component):
|
||||||
|
def __init__(self,
|
||||||
|
initial_priority=None,
|
||||||
|
collect_training_data=True):
|
||||||
|
self.priority = initial_priority
|
||||||
|
self.transformer = PerVariableTransformer()
|
||||||
|
self.collect_training_data = collect_training_data
|
||||||
|
|
||||||
|
def before_solve(self, solver, instance, model):
|
||||||
|
assert solver.is_persistent, "BranchPriorityComponent requires a persistent solver"
|
||||||
|
var_split = self.transformer.split_variables(instance, model)
|
||||||
|
for category in var_split.keys():
|
||||||
|
var_index_pairs = var_split[category]
|
||||||
|
if self.priority is not None:
|
||||||
|
from gurobipy import GRB
|
||||||
|
for (i, (var, index)) in enumerate(var_index_pairs):
|
||||||
|
gvar = solver.internal_solver._pyomo_var_to_solver_var_map[var[index]]
|
||||||
|
gvar.setAttr(GRB.Attr.BranchPriority, int(self.priority[index]))
|
||||||
|
|
||||||
|
|
||||||
|
def after_solve(self, solver, instance, model):
|
||||||
|
if self.collect_training_data:
|
||||||
|
import subprocess, tempfile, os
|
||||||
|
src_dirname = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
model_file = tempfile.NamedTemporaryFile(suffix=".lp")
|
||||||
|
priority_file = tempfile.NamedTemporaryFile()
|
||||||
|
solver.internal_solver.write(model_file.name)
|
||||||
|
subprocess.run(["julia",
|
||||||
|
"%s/scripts/branchpriority.jl" % src_dirname,
|
||||||
|
model_file.name,
|
||||||
|
priority_file.name],
|
||||||
|
check=True)
|
||||||
|
self._merge(np.genfromtxt(priority_file.name,
|
||||||
|
delimiter=',',
|
||||||
|
dtype=int))
|
||||||
|
|
||||||
|
|
||||||
|
def fit(self, solver):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def merge(self, other):
|
||||||
|
if other.priority is not None:
|
||||||
|
self._merge(other.priority)
|
||||||
|
|
||||||
|
|
||||||
|
def _merge(self, priority):
|
||||||
|
assert isinstance(priority, np.ndarray)
|
||||||
|
if self.priority is None:
|
||||||
|
self.priority = priority
|
||||||
|
else:
|
||||||
|
assert self.priority.shape == priority.shape
|
||||||
|
self.priority += priority
|
@ -0,0 +1,23 @@
|
|||||||
|
# MIPLearn, an extensible framework for Learning-Enhanced Mixed-Integer Optimization
|
||||||
|
# Copyright (C) 2019-2020 Argonne National Laboratory. All rights reserved.
|
||||||
|
# Written by Alinson S. Xavier <axavier@anl.gov>
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Component(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def fit(self, solver):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def before_solve(self, solver, instance, model):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def after_solve(self, solver, instance, model):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def merge(self, other):
|
||||||
|
pass
|
@ -0,0 +1,67 @@
|
|||||||
|
import Base.Threads.@threads
|
||||||
|
using TinyBnB, CPLEXW, Printf
|
||||||
|
|
||||||
|
instance_name = ARGS[1]
|
||||||
|
output_filename = ARGS[2]
|
||||||
|
|
||||||
|
mip = open_mip(instance_name)
|
||||||
|
n_vars = CPXgetnumcols(mip.cplex_env[1], mip.cplex_lp[1])
|
||||||
|
|
||||||
|
pseudocost_count_up = [0 for i in 1:n_vars]
|
||||||
|
pseudocost_count_down = [0 for i in 1:n_vars]
|
||||||
|
pseudocost_sum_up = [0. for i in 1:n_vars]
|
||||||
|
pseudocost_sum_down = [0. for i in 1:n_vars]
|
||||||
|
|
||||||
|
function full_strong_branching_track(node::Node, progress::Progress)::TinyBnB.Variable
|
||||||
|
N = length(node.fractional_variables)
|
||||||
|
scores = Array{Float64}(undef, N)
|
||||||
|
rates_up = Array{Float64}(undef, N)
|
||||||
|
rates_down = Array{Float64}(undef, N)
|
||||||
|
|
||||||
|
@threads for v in 1:N
|
||||||
|
fix_vars!(node.mip, node.branch_variables, node.branch_values)
|
||||||
|
obj_up, obj_down = TinyBnB.probe(node.mip, node.fractional_variables[v])
|
||||||
|
unfix_vars!(node.mip, node.branch_variables)
|
||||||
|
delta_up = obj_up - node.obj
|
||||||
|
delta_down = obj_down - node.obj
|
||||||
|
frac_up = ceil(node.fractional_values[v]) - node.fractional_values[v]
|
||||||
|
frac_down = node.fractional_values[v] - floor(node.fractional_values[v])
|
||||||
|
rates_up[v] = delta_up / frac_up
|
||||||
|
rates_down[v] = delta_down / frac_down
|
||||||
|
scores[v] = delta_up * delta_down
|
||||||
|
end
|
||||||
|
|
||||||
|
max_score, max_offset = findmax(scores)
|
||||||
|
selected_var = node.fractional_variables[max_offset]
|
||||||
|
|
||||||
|
if rates_up[max_offset] < 1e6
|
||||||
|
pseudocost_count_up[selected_var.index] += 1
|
||||||
|
pseudocost_sum_up[selected_var.index] += rates_up[max_offset]
|
||||||
|
end
|
||||||
|
|
||||||
|
if rates_down[max_offset] < 1e6
|
||||||
|
pseudocost_count_down[selected_var.index] += 1
|
||||||
|
pseudocost_sum_down[selected_var.index] += rates_down[max_offset]
|
||||||
|
end
|
||||||
|
|
||||||
|
return selected_var
|
||||||
|
end
|
||||||
|
|
||||||
|
branch_and_bound(mip,
|
||||||
|
node_limit = 1000,
|
||||||
|
branch_rule = full_strong_branching_track,
|
||||||
|
node_rule = best_bound,
|
||||||
|
print_interval = 1)
|
||||||
|
|
||||||
|
priority = [(pseudocost_count_up[v] == 0 || pseudocost_count_down[v] == 0) ? 0 :
|
||||||
|
(pseudocost_sum_up[v] / pseudocost_count_up[v]) *
|
||||||
|
(pseudocost_sum_down[v] / pseudocost_count_down[v])
|
||||||
|
for v in 1:n_vars];
|
||||||
|
|
||||||
|
open(output_filename, "w") do file
|
||||||
|
for v in 1:n_vars
|
||||||
|
v == 1 || write(file, ",")
|
||||||
|
write(file, @sprintf("%.0f", priority[v]))
|
||||||
|
end
|
||||||
|
write(file, "\n")
|
||||||
|
end
|
Loading…
Reference in new issue