Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bounds of variable wrt to kind #636

Merged
merged 1 commit into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Algorithm/benders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ function solve_sp_to_gencut!(

optresult = getoptstate(optstate)

if getterminationstatus(optresult) == INFEASIBLE # if status != MOI.OPTIMAL
if getterminationstatus(optresult) != OPTIMAL && getterminationstatus(optresult) != UNBOUNDED
sp_is_feasible = false
# @logmsg LogLevel(-3) "benders_sp prob is infeasible"
bd = PrimalBound(spform)
Expand Down
2 changes: 1 addition & 1 deletion src/ColunaBase/ColunaBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export Bound, Solution, getvalue, getbound, isbetter, diff, gap, printbounds,

# Statuses
export TerminationStatus, SolutionStatus, OPTIMIZE_NOT_CALLED, OPTIMAL,
INFEASIBLE, TIME_LIMIT, NODE_LIMIT, OTHER_LIMIT, UNCOVERED_TERMINATION_STATUS,
INFEASIBLE, UNBOUNDED, TIME_LIMIT, NODE_LIMIT, OTHER_LIMIT, UNCOVERED_TERMINATION_STATUS,
FEASIBLE_SOL, INFEASIBLE_SOL, UNKNOWN_FEASIBILITY, UNKNOWN_SOLUTION_STATUS,
UNCOVERED_SOLUTION_STATUS, convert_status

Expand Down
34 changes: 34 additions & 0 deletions src/MathProg/varconstr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Change is propagated to the current lower bound of the variable.
"""
function setperenlb!(form::Formulation, var::Variable, lb)
var.perendata.lb = lb
_setperenbounds_wrt_perenkind!(form, var, getperenkind(form, var))
return setcurlb!(form, var, lb)
end

Expand Down Expand Up @@ -111,6 +112,7 @@ function setcurlb!(form::Formulation, var::Variable, lb::Float64)
if isexplicit(form, var) && iscuractive(form, var)
change_bound!(form.buffer, getid(var))
end
_setcurbounds_wrt_curkind!(form, var, getcurkind(form, var))
return
end
setcurlb!(form::Formulation, varid::VarId, lb::Float64) = setcurlb!(form, getvar(form, varid), lb)
Expand All @@ -124,6 +126,7 @@ Change is propagated to the current upper bound of the variable.
"""
function setperenub!(form::Formulation, var::Variable, ub)
var.perendata.ub = ub
_setperenbounds_wrt_perenkind!(form, var, getperenkind(form, var))
return setcurub!(form, var, ub)
end

Expand Down Expand Up @@ -158,6 +161,7 @@ function setcurub!(form::Formulation, var::Variable, ub::Float64)
if isexplicit(form, var) && iscuractive(form, var)
change_bound!(form.buffer, getid(var))
end
_setcurbounds_wrt_curkind!(form, var, getcurkind(form, var))
return
end
setcurub!(form::Formulation, varid::VarId, ub::Float64) = setcurub!(form, getvar(form, varid), ub)
Expand Down Expand Up @@ -250,6 +254,20 @@ Return the current kind of a variable in a formulation.
getcurkind(form::Formulation, varid::VarId) = getcurkind(form, getvar(form, varid))
getcurkind(::Formulation, var::Variable) = var.curdata.kind

function _setperenbounds_wrt_perenkind!(form::Formulation, var::Variable, kind::VarKind)
if kind == Binary
if getperenlb(form, var) < 0
setperenlb!(form, var, 0.0)
end
if getperenub(form, var) > 1
setperenub!(form, var, 1.0)
end
elseif kind == Integer
setperenlb!(form, var, ceil(getperenlb(form, var)))
setperenub!(form, var, floor(getperenub(form, var)))
end
end

"""
setperenkind!(formulation, variable, kind)
setperenkind!(formulation, varid, kind)
Expand All @@ -259,10 +277,25 @@ This change is then propagated to the current kind of the variable.
"""
function setperenkind!(form::Formulation, var::Variable, kind::VarKind)
var.perendata.kind = kind
_setperenbounds_wrt_perenkind!(form, var, kind)
return setcurkind!(form, var, kind)
end
setperenkind!(form::Formulation, varid::VarId, kind::VarKind) = setperenkind!(form, getvar(form, varid), kind)

function _setcurbounds_wrt_curkind!(form::Formulation, var::Variable, kind::VarKind)
if kind == Binary
if getcurlb(form, var) < 0
setcurlb!(form, var, 0.0)
end
if getcurub(form, var) > 1
setcurub!(form, var, 1.0)
end
elseif kind == Integer
setcurlb!(form, var, ceil(getcurlb(form, var)))
setcurub!(form, var, floor(getcurub(form, var)))
end
end

"""
setcurkind!(formulation, variable, kind::VarKind)
setcurkind!(formulation, varid, kind::VarKind)
Expand All @@ -273,6 +306,7 @@ application to the subsolver
"""
function setcurkind!(form::Formulation, var::Variable, kind::VarKind)
var.curdata.kind = kind
_setcurbounds_wrt_curkind!(form, var, kind)
if isexplicit(form, var) && iscuractive(form, var)
change_kind!(form.buffer, getid(var))
end
Expand Down
16 changes: 0 additions & 16 deletions src/MathProg/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ mutable struct VarData <: AbstractVcData
is_explicit::Bool
end

function _set_bounds_acc_kind!(vdata::VarData, kind::VarKind)
if kind == Binary
if vdata.lb < 0
vdata.lb = 0
end
if vdata.ub > 1
vdata.ub = 0
end
elseif kind == Integer
vdata.lb = ceil(vdata.lb)
vdata.ub = floor(vdata.ub)
end
return
end

"""
VarData

Expand All @@ -35,7 +20,6 @@ function VarData(
inc_val::Float64 = -1.0, is_active::Bool = true, is_explicit::Bool = true
)
vc = VarData(cost, lb, ub, kind, inc_val, is_active, is_explicit)
_set_bounds_acc_kind!(vc, kind)
return vc
end

Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ using Coluna, ReTest

include("ColunaTests.jl")

retest(Coluna, ColunaTests)
retest(Coluna, ColunaTests)

# Run a specific test:
# retest(ColunaTests, "name of test")
80 changes: 80 additions & 0 deletions test/unit/MathProg/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,86 @@
@test ClMP.getcurincval(form, varid) == 4.0
end

@testset "bounds of binary variable 1" begin
form = ClMP.create_formulation!(Env(Coluna.Params()), ClMP.Original())
var = ClMP.setvar!(
form, "var1", ClMP.OriginalVar, kind = ClMP.Binary
)

@test ClMP.getperenlb(form, var) == 0.0
@test ClMP.getperenub(form, var) == 1.0
@test ClMP.getcurlb(form, var) == 0.0
@test ClMP.getcurub(form, var) == 1.0

ClMP.setperenlb!(form, var, -1.0)
ClMP.setperenub!(form, var, 2.0)

@test ClMP.getperenlb(form, var) == 0.0
@test ClMP.getperenub(form, var) == 1.0
@test ClMP.getcurlb(form, var) == 0.0
@test ClMP.getcurub(form, var) == 1.0

ClMP.setcurlb!(form, var, -1.1)
ClMP.setcurub!(form, var, 2.1)

@test ClMP.getcurlb(form, var) == 0.0
@test ClMP.getcurub(form, var) == 1.0

ClMP.setperenlb!(form, var, 0.1)
ClMP.setperenub!(form, var, 0.9)

@test ClMP.getperenlb(form, var) == 0.1
@test ClMP.getperenub(form, var) == 0.9
@test ClMP.getcurlb(form, var) == 0.1
@test ClMP.getcurub(form, var) == 0.9

ClMP.setcurlb!(form, var, 0.2)
ClMP.setcurub!(form, var, 0.8)

@test ClMP.getperenlb(form, var) == 0.1
@test ClMP.getperenub(form, var) == 0.9
@test ClMP.getcurlb(form, var) == 0.2
@test ClMP.getcurub(form, var) == 0.8
end

@testset "bounds of binary variable 2" begin
form = ClMP.create_formulation!(Env(Coluna.Params()), ClMP.Original())
var = ClMP.setvar!(
form, "var1", ClMP.OriginalVar, kind = ClMP.Continuous, lb = -10.0, ub = 10.0
)

@test ClMP.getperenlb(form, var) == -10.0
@test ClMP.getperenub(form, var) == 10.0
@test ClMP.getcurlb(form, var) == -10.0
@test ClMP.getcurub(form, var) == 10.0

ClMP.setperenkind!(form, var, ClMP.Binary)

@test ClMP.getperenlb(form, var) == 0.0
@test ClMP.getperenub(form, var) == 1.0
@test ClMP.getcurlb(form, var) == 0.0
@test ClMP.getcurub(form, var) == 1.0
end

@testset "bounds of binary variable 3" begin
form = ClMP.create_formulation!(Env(Coluna.Params()), ClMP.Original())
var = ClMP.setvar!(
form, "var1", ClMP.OriginalVar, kind = ClMP.Continuous, lb = -10.0, ub = 10.0
)

@test ClMP.getperenlb(form, var) == -10.0
@test ClMP.getperenub(form, var) == 10.0
@test ClMP.getcurlb(form, var) == -10.0
@test ClMP.getcurub(form, var) == 10.0

ClMP.setcurkind!(form, var, ClMP.Binary)

@test ClMP.getperenlb(form, var) == -10.0
@test ClMP.getperenub(form, var) == 10.0
@test ClMP.getcurlb(form, var) == 0.0
@test ClMP.getcurub(form, var) == 1.0
end

@testset "record" begin
v_rec = ClMP.MoiVarRecord(; index = ClMP.MoiVarIndex(-15))

Expand Down