Skip to content

Commit

Permalink
Boost coverage on JuMPeR.jl, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
IainNZ committed Feb 6, 2015
1 parent 2910810 commit 086dbb1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
19 changes: 6 additions & 13 deletions src/JuMPeR.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,11 @@ UAffExpr() = UAffExpr(Uncertain[],Float64[],0.)
UAffExpr(c::Real) = UAffExpr(Uncertain[],Float64[],float(c))
UAffExpr(u::Uncertain) = UAffExpr([u],[1.],0.)
UAffExpr(u::Uncertain, c::Real) = UAffExpr([u],[float(c)],0.)
# Next is a bit weird - its basically the vectorized version of UAffExpr(c)
UAffExpr(coeffs::Array{Float64,1}) = [UAffExpr(c) for c in coeffs]
Base.zero(a::Type{UAffExpr}) = UAffExpr() # For zeros(UAffExpr, dims...)
Base.zero(a::UAffExpr) = zero(typeof(a))

Base.print(io::IO, a::UAffExpr) = print(io, affToStr(a))
Base.show( io::IO, a::UAffExpr) = print(io, affToStr(a))


#############################################################################
# Full Affine Expression class
# Todo: better name. In my other robust modelling tools I called it
Expand All @@ -133,26 +130,22 @@ typealias FullAffExpr GenericAffExpr{UAffExpr,Variable}
FullAffExpr() = FullAffExpr(Variable[], UAffExpr[], UAffExpr())
Base.zero(a::Type{FullAffExpr}) = FullAffExpr()
Base.zero(a::FullAffExpr) = zero(typeof(a))
function Base.push!(faff::FullAffExpr, new_coeff::Real, new_var::Variable)
function Base.push!(faff::FullAffExpr, new_coeff::UAffExpr, new_var::Variable)
push!(faff.vars, new_var)
push!(faff.coeffs, new_coeff)
end
function Base.push!(faff::FullAffExpr, new_coeff::Union(Real,Uncertain), new_var::Variable)
push!(faff.vars, new_var)
push!(faff.coeffs, UAffExpr(new_coeff))
end

#############################################################################
# UncSetConstraint Just uncertainties
typealias UncSetConstraint GenericRangeConstraint{UAffExpr}
# For 0.2
UncSetConstraint(uaff::UAffExpr,x::Float64,y::Int) = UncSetConstraint(uaff,x,float(y))
UncSetConstraint(uaff::UAffExpr,x::Int,y::Float64) = UncSetConstraint(uaff,float(x),x)
UncSetConstraint(uaff::UAffExpr,x::Int,y::Int) = UncSetConstraint(uaff,float(x),float(y))
addConstraint(m::Model, c::UncSetConstraint) = push!(getRobust(m).uncertaintyset, c)

# UncConstraint Mix of variables and uncertains
typealias UncConstraint GenericRangeConstraint{FullAffExpr}
# For 0.2
UncConstraint(faff::FullAffExpr,x::Float64,y::Int) = UncConstraint(faff,x,float(y))
UncConstraint(faff::FullAffExpr,x::Int,y::Float64) = UncConstraint(faff,float(x),x)
UncConstraint(faff::FullAffExpr,x::Int,y::Int) = UncConstraint(faff,float(x),float(y))
function addConstraint(m::Model, c::UncConstraint, w=nothing)
rd = getRobust(m)
# Handle the odd special case where there are actually no variables in
Expand Down
4 changes: 3 additions & 1 deletion src/oracle_gen_graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ function generateCut(gen::GeneralGraphOracle, master::Model, rm::Model, inds::Ve
end
@setObjective(cut_model, cut_sense, cut_obj)

cut_solve_status = solve(cut_model, suppress_warnings=true)
cut_solve_status = solve(cut_model) #, suppress_warnings=true)
println(cut_model)
@show cut_solve_status
cut_solve_status != :Optimal && error("Cutting plane problem infeasible or unbounded!")
lhs_of_cut = getObjectiveValue(cut_model) + lhs_const

Expand Down
10 changes: 5 additions & 5 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ function aff_str(mode, a::FullAffExpr; show_constant=true)
# Constant -1
term_str[numTerms] = first ? "-$varn" : " - $varn"
else
# Constant is other than 0, +1, -1
sign = uaff.constant < 0 ? "-" : ""
term_str[numTerms] = (first?"$sign":" $sign ") *
"$(str_round(abs(uaff.constant))) $varn"
# Constant is other than 0, +1, -1
sign = first ? (uaff.constant < 0 ? "-" : "") :
(uaff.constant < 0 ? " - " : " + ")
term_str[numTerms] = "$(sign)$(str_round(abs(uaff.constant))) $varn"
end
# Coefficient expression is a single uncertainty
elseif length(uaff.vars) == 1
Expand All @@ -348,7 +348,7 @@ function aff_str(mode, a::FullAffExpr; show_constant=true)
first = false
end

# And then connect them up with +s
# And then connect them up
ret = join(term_str[1:numTerms], "")

# Now the constant term
Expand Down
6 changes: 5 additions & 1 deletion test/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Base.Test

#----------------------------------------------------------------------

println(" Test 1")
m = RobustModel()
r = JuMPeR.getRobust(m)
@defUnc(m, 0 <= u[1:2,1:4] <= 1)
Expand All @@ -29,6 +30,7 @@ solveRobust(m, prefer_cuts=false) # REFORM

#----------------------------------------------------------------------

println(" Test 2")
m = RobustModel()
r = JuMPeR.getRobust(m)
@defUnc(m, 2 <= u[1:3] <= 2)
Expand All @@ -52,13 +54,14 @@ solveRobust(m, prefer_cuts=false) # REFORM

#----------------------------------------------------------------------

println(" Test 3")
m = RobustModel()
r = JuMPeR.getRobust(m)
@defUnc(m, u[1:4] >= 0)
addConstraint(m, u[1] + u[3] == 1)
addConstraint(m, u[2] == 1)
addConstraint(m, u[2] + u[3] == 1)
addConstraint(m, u[4] >= 5)
addConstraint(m, u[4] <= 5)
ret = JuMPeR.detect_components(r.numUncs, r.uncertaintyset)
@test ret[1] == [1,1,1,2]
@test ret[2] == [1,1,1,2]
Expand All @@ -68,6 +71,7 @@ ret = JuMPeR.detect_components(r.numUncs, r.uncertaintyset)
setDefaultOracle!(m, JuMPeR.GeneralGraphOracle())
addConstraint(m, x[1] >= u[1])
addConstraint(m, x[2] >= u[4])
printRobust(m)
solveRobust(m, prefer_cuts=true) # CUTS
@test_approx_eq getValue(x[1]) 1.0
@test_approx_eq getValue(x[2]) 5.0
Expand Down
29 changes: 29 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ faff = (5a+1)x + (2b + 3)
@fact affToStr(uaff2) => "3.4 b + 1.1"
@fact affToStr(faff) => "(5 a + 1) x + 2 b + 3"

context("Core JuMPeR type methods") do
# Model
@fact getNumUncs(m) => 2
@fact_throws getNumUncs(Model())

# Uncertain
@fact isequal(a,b) => false
@fact getName(a) => "a"

# UAffExpr
@fact affToStr(UAffExpr()) => "0"
@fact affToStr(UAffExpr(1)) => "1"
@fact affToStr(UAffExpr(a)) => "a"
@fact affToStr(UAffExpr(a,2)) => "2 a"
@fact typeof(zero(uaff)) => UAffExpr
@fact affToStr(zero(uaff)) => "0"

# FullAffExpr
@fact affToStr(FullAffExpr()) => "0"
@fact typeof(zero(faff)) => FullAffExpr
@fact affToStr(zero(faff)) => "0"
pusher = a * x
@fact affToStr(pusher) => "a x"
push!(pusher, 2.0, y)
@fact affToStr(pusher) => "a x + 2 y"
push!(pusher, b, y)
@fact affToStr(pusher) => "a x + 2 y + b y"
end


context("Number--... tests") do
# Number--Uncertain
Expand Down

0 comments on commit 086dbb1

Please sign in to comment.