Skip to content

Commit

Permalink
(slow) pretty JuMPDict printing
Browse files Browse the repository at this point in the history
  • Loading branch information
joehuchette committed Jan 28, 2014
1 parent b99e325 commit 45c08c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,20 +258,21 @@ 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
code = :( $(refcall) = Variable($m, $lb, $ub, $t) )
tup = Expr(:tuple,[esc(x) for x in idxvars]...)
code = :( $(refcall) = Variable($m, $lb, $ub, $t, $(string(var.args[1]))*string($tup) ) )
for (idxvar, idxset) in zip(reverse(idxvars),reverse(idxsets))
code = quote
for $idxvar in $idxset
for $(esc(idxvar)) in $idxset
$code
end
end
Expand Down Expand Up @@ -307,4 +308,4 @@ macro defConstrRef(var)
end
return code
end
end
end
8 changes: 4 additions & 4 deletions test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ 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 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

0 comments on commit 45c08c4

Please sign in to comment.