Extract constraint slacks

master
Alinson S. Xavier 4 years ago
parent 7c6ba863d6
commit e77b6acdb5

@ -144,6 +144,8 @@ function add_constraints(
set_name(constr, names[i]) set_name(constr, names[i])
data.cname_to_constr[names[i]] = constr data.cname_to_constr[names[i]] = constr
end end
data.solution = Dict()
data.x = Float64[]
return return
end end
@ -240,6 +242,7 @@ function solve(
if is_infeasible(data) if is_infeasible(data)
data.solution = Dict() data.solution = Dict()
data.x = Float64[]
primal_bound = nothing primal_bound = nothing
dual_bound = nothing dual_bound = nothing
else else
@ -470,8 +473,8 @@ function get_constraints(
) )
names = String[] names = String[]
senses, rhs = String[], Float64[] senses, rhs = String[], Float64[]
lhs_rows, lhs_cols, lhs_values = nothing, nothing, nothing lhs_rows, lhs_cols, lhs_values = Int[], Int[], Float64[]
dual_values = nothing dual_values, slacks = nothing, nothing
basis_status = nothing basis_status = nothing
sa_rhs_up, sa_rhs_down = nothing, nothing sa_rhs_up, sa_rhs_down = nothing, nothing
@ -484,12 +487,6 @@ function get_constraints(
sa_rhs_up, sa_rhs_down = Float64[], Float64[] sa_rhs_up, sa_rhs_down = Float64[], Float64[]
end end
if with_lhs
lhs_rows = Int[]
lhs_cols = Int[]
lhs_values = Float64[]
end
constr_index = 1 constr_index = 1
for (ftype, stype) in JuMP.list_of_constraint_types(data.model) for (ftype, stype) in JuMP.list_of_constraint_types(data.model)
for constr in JuMP.all_constraints(data.model, ftype, stype) for constr in JuMP.all_constraints(data.model, ftype, stype)
@ -518,14 +515,12 @@ function get_constraints(
error("Unsupported set: $stype") error("Unsupported set: $stype")
end end
push!(rhs, rhs_c) push!(rhs, rhs_c)
if with_lhs for term in cf.terms
for term in cf.terms push!(lhs_cols, term.variable_index.value)
push!(lhs_cols, term.variable_index.value) push!(lhs_rows, constr_index)
push!(lhs_rows, constr_index) push!(lhs_values, term.coefficient)
push!(lhs_values, term.coefficient)
end
constr_index += 1
end end
constr_index += 1
else else
error("Unsupported constraint type: ($ftype, $stype)") error("Unsupported constraint type: ($ftype, $stype)")
end end
@ -554,6 +549,12 @@ function get_constraints(
end end
end end
lhs = sparse(lhs_rows, lhs_cols, lhs_values)
if !isempty(data.x)
lhs_value = lhs * data.x
slacks = abs.(lhs_value - rhs)
end
return miplearn.solvers.internal.Constraints( return miplearn.solvers.internal.Constraints(
basis_status = to_str_array(basis_status), basis_status = to_str_array(basis_status),
dual_values = dual_values, dual_values = dual_values,
@ -563,6 +564,7 @@ function get_constraints(
sa_rhs_down = sa_rhs_down, sa_rhs_down = sa_rhs_down,
sa_rhs_up = sa_rhs_up, sa_rhs_up = sa_rhs_up,
senses = with_static ? to_str_array(senses) : nothing, senses = with_static ? to_str_array(senses) : nothing,
slacks = slacks,
) )
end end
@ -621,13 +623,13 @@ function __init_JuMPSolver__()
"rhs", "rhs",
"senses", "senses",
"user_features", "user_features",
"slacks",
] ]
if repr(self.data.optimizer_factory) in ["Gurobi.Optimizer"] if repr(self.data.optimizer_factory) in ["Gurobi.Optimizer"]
append!(attrs, [ append!(attrs, [
"basis_status", "basis_status",
"sa_rhs_down", "sa_rhs_down",
"sa_rhs_up", "sa_rhs_up",
# "slacks",
]) ])
end end
return attrs return attrs

Loading…
Cancel
Save