parent
7caca9a882
commit
295358c553
@ -0,0 +1,61 @@
|
||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
using JSON2
|
||||
import Base: dump
|
||||
|
||||
get_instance_features(instance) = [0.]
|
||||
get_variable_features(instance, var, index) = [0.]
|
||||
find_violated_lazy_constraints(instance, model) = []
|
||||
build_lazy_constraint(instance, model, v) = nothing
|
||||
|
||||
dump(instance::PyCall.PyObject, filename) = @pycall instance.dump(filename)
|
||||
load!(instance::PyCall.PyObject, filename) = @pycall instance.load(filename)
|
||||
|
||||
macro Instance(klass)
|
||||
quote
|
||||
@pydef mutable struct Wrapper <: Instance
|
||||
function __init__(self, args...; kwargs...)
|
||||
self.data = $(esc(klass))(args...; kwargs...)
|
||||
end
|
||||
|
||||
function dump(self, filename)
|
||||
prev_data = self.data
|
||||
self.data = JSON2.write(prev_data)
|
||||
Instance.dump(self, filename)
|
||||
self.data = prev_data
|
||||
end
|
||||
|
||||
function load(self, filename)
|
||||
Instance.load(self, filename)
|
||||
self.data = JSON2.read(self.data, $(esc(klass)))
|
||||
end
|
||||
|
||||
to_model(self) =
|
||||
$(esc(:to_model))(self.data)
|
||||
|
||||
get_instance_features(self) =
|
||||
get_instance_features(self.data)
|
||||
|
||||
get_variable_features(self, var, index) =
|
||||
get_variable_features(self.data, var, index)
|
||||
|
||||
function find_violated_lazy_constraints(self, model)
|
||||
find_violated_lazy_constraints(self.data, model)
|
||||
end
|
||||
|
||||
function build_lazy_constraint(self, model, v)
|
||||
build_lazy_constraint(self.data, model, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
export get_instance_features,
|
||||
get_variable_features,
|
||||
find_violated_lazy_constraints,
|
||||
build_lazy_constraint,
|
||||
dump,
|
||||
load!,
|
||||
@Instance
|
@ -1,32 +0,0 @@
|
||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
using PyCall
|
||||
|
||||
@pydef mutable struct KnapsackInstance <: Instance
|
||||
function __init__(self, weights, prices, capacity)
|
||||
self.weights = weights
|
||||
self.prices = prices
|
||||
self.capacity = capacity
|
||||
end
|
||||
|
||||
function to_model(self)
|
||||
model = Model()
|
||||
n = length(self.weights)
|
||||
@variable(model, x[1:n], Bin)
|
||||
@objective(model, Max, sum(x[i] * self.prices[i] for i in 1:n))
|
||||
@constraint(model, sum(x[i] * self.weights[i] for i in 1:n) <= self.capacity)
|
||||
return model
|
||||
end
|
||||
|
||||
function get_instance_features(self)
|
||||
return [0.]
|
||||
end
|
||||
|
||||
function get_variable_features(self, var, index)
|
||||
return [0.]
|
||||
end
|
||||
end
|
||||
|
||||
export KnapsackInstance
|
@ -0,0 +1,28 @@
|
||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
struct LearningSolver
|
||||
py::PyCall.PyObject
|
||||
end
|
||||
|
||||
function LearningSolver(;
|
||||
optimizer,
|
||||
kwargs...,
|
||||
)::LearningSolver
|
||||
py = @pycall miplearn.LearningSolver(;
|
||||
kwargs...,
|
||||
solver=JuMPSolver(optimizer=optimizer))
|
||||
return LearningSolver(py)
|
||||
end
|
||||
|
||||
solve!(solver::LearningSolver, instance; kwargs...) =
|
||||
@pycall solver.py.solve(instance; kwargs...)
|
||||
|
||||
fit!(solver::LearningSolver, instances; kwargs...) =
|
||||
@pycall solver.py.fit(instances; kwargs...)
|
||||
|
||||
add!(solver::LearningSolver, component; kwargs...) =
|
||||
@pycall solver.py.add(component; kwargs...)
|
||||
|
||||
export LearningSolver
|
@ -0,0 +1,34 @@
|
||||
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
import MIPLearn: get_instance_features,
|
||||
get_variable_features
|
||||
find_violated_lazy_constraints
|
||||
using JuMP
|
||||
|
||||
struct KnapsackData
|
||||
weights
|
||||
prices
|
||||
capacity
|
||||
end
|
||||
|
||||
function to_model(data::KnapsackData)
|
||||
model = Model()
|
||||
n = length(data.weights)
|
||||
@variable(model, x[1:n], Bin)
|
||||
@objective(model, Max, sum(x[i] * data.prices[i] for i in 1:n))
|
||||
@constraint(model, sum(x[i] * data.weights[i] for i in 1:n) <= data.capacity)
|
||||
return model
|
||||
end
|
||||
|
||||
function get_instance_features(data::KnapsackData)
|
||||
return [0.]
|
||||
end
|
||||
|
||||
|
||||
function get_variable_features(data::KnapsackData, var, index)
|
||||
return [0.]
|
||||
end
|
||||
|
||||
KnapsackInstance = @Instance(KnapsackData)
|
Loading…
Reference in new issue