Skip to content

Commit

Permalink
Merge pull request #97 from JuliaOpt/dictPrint2
Browse files Browse the repository at this point in the history
clean commit for dictPrint
  • Loading branch information
joehuchette committed Feb 4, 2014
2 parents 59368e0 + 94ec1e4 commit 5d8ac71
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
43 changes: 36 additions & 7 deletions src/JuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Model
colLower::Vector{Float64}
colUpper::Vector{Float64}
colCat::Vector{Int}

# Solution data
objVal
colVal::Vector{Float64}
Expand All @@ -80,6 +80,9 @@ type Model
# callbacks
lazycallback
cutcallback

# JuMPDict list
dictList::Vector
end

# Default constructor
Expand All @@ -93,7 +96,7 @@ function Model(sense::Symbol;lpsolver=MathProgBase.defaultLPsolver,mipsolver=Mat
Model(QuadExpr(),sense,LinearConstraint[], QuadConstraint[],
0,String[],Float64[],Float64[],Int[],
0,Float64[],Float64[],Float64[],nothing,MathProgBase.MissingSolver("",Symbol[]),true,
nothing,nothing)
nothing,nothing,JuMPDict[])
else
if !isa(solver,AbstractMathProgSolver)
error("solver argument ($solver) must be an AbstractMathProgSolver")
Expand All @@ -102,7 +105,7 @@ function Model(sense::Symbol;lpsolver=MathProgBase.defaultLPsolver,mipsolver=Mat
Model(QuadExpr(),sense,LinearConstraint[], QuadConstraint[],
0,String[],Float64[],Float64[],Int[],
0,Float64[],Float64[],Float64[],nothing,solver,true,
nothing,nothing)
nothing,nothing,JuMPDict[])
end
end

Expand All @@ -116,7 +119,7 @@ function Model(;solver=nothing,lpsolver=MathProgBase.defaultLPsolver,mipsolver=M
Model(QuadExpr(),:Min,LinearConstraint[], QuadConstraint[],
0,String[],Float64[],Float64[],Int[],
0,Float64[],Float64[],Float64[],nothing,MathProgBase.MissingSolver("",Symbol[]),true,
nothing,nothing)
nothing,nothing,JuMPDict[])
else
if !isa(solver,AbstractMathProgSolver)
error("solver argument ($solver) must be an AbstractMathProgSolver")
Expand All @@ -125,7 +128,7 @@ function Model(;solver=nothing,lpsolver=MathProgBase.defaultLPsolver,mipsolver=M
Model(QuadExpr(),:Min,LinearConstraint[], QuadConstraint[],
0,String[],Float64[],Float64[],Int[],
0,Float64[],Float64[],Float64[],nothing,solver,true,
nothing,nothing)
nothing,nothing,JuMPDict[])
end
end

Expand All @@ -141,8 +144,30 @@ function setObjectiveSense(m::Model, newSense::Symbol)
m.objSense = newSense
end

function fillVarNames(m)
for dict in m.dictList
idxsets = dict.indexsets
lengths = map(length, idxsets)
N = length(idxsets)
name = dict.name
cprod = cumprod([lengths...])
for (ind,var) in enumerate(dict.innerArray)
setName(var,string("$name[$(idxsets[1][mod1(ind,lengths[1])])", [ ",$(idxsets[i][int(ceil(mod1(ind,cprod[i]) / cprod[i-1]))])" for i=2:N ]..., "]"))
end
end
m.dictList = JuMPDict[]
end


# Pretty print
function print(io::IO, m::Model)
for i in 1:m.numCols
if m.colNames[i] == ""
fillVarNames(m)
break
end
end

println(io, string(m.objSense," ",quadToStr(m.obj)))
println(io, "Subject to: ")
for c in m.linconstr
Expand Down Expand Up @@ -242,12 +267,16 @@ setName(v::Variable,n::String) = (v.m.colNames[v.col] = n)

function getName(v::Variable)
if length(v.m.colNames) > 0
return (v.m.colNames[v.col] == "" ? string("_col",v.col) : v.m.colNames[v.col])
v.m.colNames[v.col] == "" && fillVarNames(v.m)
return ( v.m.colNames[v.col] == "" ? "_col$(v.col)" : v.m.colNames[v.col] )
end
nothing
end

getName(m::Model, col) = (m.colNames[col] == "" ? string("_col",col) : m.colNames[col])
function getName(m::Model, col)
m.colNames[col] == "" && fillVarNames(m)
return ( m.colNames[col] == "" ? "_col$(col)" : m.colNames[col] )
end
print(io::IO, v::Variable) = print(io, getName(v))
show(io::IO, v::Variable) = print(io, getName(v))

Expand Down
14 changes: 9 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,29 +258,33 @@ macro defVar(m, x, extra...)
refcall = Expr(:ref,varname)
for s in var.args[2:end]
if isa(s,Expr) && s.head == :(=)
idxvar = esc(s.args[1])
idxvar = s.args[1]
idxset = esc(s.args[2])
else
idxvar = gensym()
idxset = esc(s)
end
push!(idxvars, idxvar)
push!(idxsets, idxset)
push!(refcall.args, idxvar)
push!(refcall.args, esc(idxvar))
end
tup = Expr(:tuple, [esc(x) for x in idxvars]...)
# code = :( $(refcall) = Variable($m, $lb, $ub, $t, $(string(var.args[1]))*string($tup) ) )
code = :( $(refcall) = Variable($m, $lb, $ub, $t) )
for (idxvar, idxset) in zip(reverse(idxvars),reverse(idxsets))
code = quote
for $idxvar in $idxset
for $(esc(idxvar)) in $idxset
$code
end
end
end

mac = Expr(:macrocall,symbol("@gendict"),varname,:Variable,idxsets...)
addDict = :( push!($(m).dictList, $varname) )
code = quote
$mac
$code
$addDict
nothing
end
return code
Expand All @@ -307,4 +311,4 @@ macro defConstrRef(var)
end
return code
end
end
end
12 changes: 6 additions & 6 deletions test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ maff = Model()
@defVar(maff, 0 <= LongName <= 99)
# Test affToStr
a1 = x[1] + LongName + 5
@test affToStr(a1) == "1.0 _col1 + 1.0 LongName + 5.0"
@test affToStr(a1) == "1.0 x[1] + 1.0 LongName + 5.0"
# Test like term collection
a2 = 2*(x[2] + LongName + x[2]) + 0
@test affToStr(a2) == "4.0 _col2 + 2.0 LongName"
@test affToStr(a2) == "4.0 x[2] + 2.0 LongName"
# Test appending functionality
push!(a1, 5.0, x[2])
@test affToStr(a1) == "1.0 _col1 + 1.0 LongName + 5.0 _col2 + 5.0"
@test affToStr(a1) == "1.0 x[1] + 1.0 LongName + 5.0 x[2] + 5.0"
append!(a1, a2)
@test affToStr(a1) == "1.0 _col1 + 3.0 LongName + 9.0 _col2 + 5.0"
@test affToStr(a1) == "1.0 x[1] + 3.0 LongName + 9.0 x[2] + 5.0"

# Test quadToStr
q1 = x[1]*x[2] + 27.2*LongName + 5
@test quadToStr(q1) == "1.0 _col1*_col2 + 27.2 LongName + 5.0"
@test quadToStr(q1) == "1.0 x[1]*x[2] + 27.2 LongName + 5.0"
# Test like term collection
q2 = x[1]*x[2] + x[2]*x[1]
@test quadToStr(q2) == "2.0 _col1*_col2"
@test quadToStr(q2) == "2.0 x[1]*x[2]"
12 changes: 6 additions & 6 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,26 @@ let
@defVar(m, y)
C = [1 2 3; 4 5 6; 7 8 9]
@addConstraint(m, sum{ C[i,j]*x[i,j], i = 1:2, j = 2:3 } <= 1)
@test conToStr(m.linconstr[end]) == "2.0 _col2 + 3.0 _col3 + 5.0 _col5 + 6.0 _col6 <= 1.0"
@test conToStr(m.linconstr[end]) == "2.0 x[1,2] + 3.0 x[1,3] + 5.0 x[2,2] + 6.0 x[2,3] <= 1.0"
@addConstraint(m, sum{ C[i,j]*x[i,j], i = 1:3, j = 1:3; i != j} == y)
@test conToStr(m.linconstr[end]) == "2.0 _col2 + 3.0 _col3 + 4.0 _col4 + 6.0 _col6 + 7.0 _col7 + 8.0 _col8 - 1.0 y == 0.0"
@test conToStr(m.linconstr[end]) == "2.0 x[1,2] + 3.0 x[1,3] + 4.0 x[2,1] + 6.0 x[2,3] + 7.0 x[3,1] + 8.0 x[3,2] - 1.0 y == 0.0"

@addConstraint(m, sum{ C[i,j]*x[i,j], i = 1:3, j = 1:i} == 0);
@test conToStr(m.linconstr[end]) == "1.0 _col1 + 4.0 _col4 + 5.0 _col5 + 7.0 _col7 + 8.0 _col8 + 9.0 _col9 == 0.0"
@test conToStr(m.linconstr[end]) == "1.0 x[1,1] + 4.0 x[2,1] + 5.0 x[2,2] + 7.0 x[3,1] + 8.0 x[3,2] + 9.0 x[3,3] == 0.0"
end

let
m = Model()
@defVar(m, x[1:3,1:3])
C = [1 2 3; 4 5 6; 7 8 9]
con = @addConstraint(m, sum{ C[i,j]*x[i,j], i = 1:3, j = 1:3; i != j} == 0)
@test conToStr(m.linconstr[end]) == "2.0 _col2 + 3.0 _col3 + 4.0 _col4 + 6.0 _col6 + 7.0 _col7 + 8.0 _col8 == 0.0"
@test conToStr(m.linconstr[end]) == "2.0 x[1,2] + 3.0 x[1,3] + 4.0 x[2,1] + 6.0 x[2,3] + 7.0 x[3,1] + 8.0 x[3,2] == 0.0"

@defVar(m, y, 0, [con], [-1.0])
@test conToStr(m.linconstr[end]) == "2.0 _col2 + 3.0 _col3 + 4.0 _col4 + 6.0 _col6 + 7.0 _col7 + 8.0 _col8 - 1.0 y == 0.0"
@test conToStr(m.linconstr[end]) == "2.0 x[1,2] + 3.0 x[1,3] + 4.0 x[2,1] + 6.0 x[2,3] + 7.0 x[3,1] + 8.0 x[3,2] - 1.0 y == 0.0"

chgConstrRHS(con, 3)
@test conToStr(m.linconstr[end]) == "2.0 _col2 + 3.0 _col3 + 4.0 _col4 + 6.0 _col6 + 7.0 _col7 + 8.0 _col8 - 1.0 y == 3.0"
@test conToStr(m.linconstr[end]) == "2.0 x[1,2] + 3.0 x[1,3] + 4.0 x[2,1] + 6.0 x[2,3] + 7.0 x[3,1] + 8.0 x[3,2] - 1.0 y == 3.0"
end

let
Expand Down
9 changes: 9 additions & 0 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ setName(x, "x2")
@test getName(x) == "x2"
setName(x, "")
@test getName(x) == "_col1"
@defVar(m, z[1:2,3:5])
@defVar(m, w[3:9,["red","blue","green"]])
rng = 2:5
@defVar(m, v[rng,rng,rng,rng,rng,rng,rng])
@test getName(z[1,3]) == "z[1,3]"
@test getName(z[2,4]) == "z[2,4]"
@test getName(z[2,5]) == "z[2,5]"
@test getName(w[7,"green"]) == "w[7,green]"
@test getName(v[4,5,2,3,2,2,4]) == "v[4,5,2,3,2,2,4]"

# Bounds
@test getLower(x) == 0
Expand Down

0 comments on commit 5d8ac71

Please sign in to comment.