Skip to content

Commit

Permalink
tidy up expression graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Aug 5, 2016
1 parent 7457b21 commit 929ab54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/nlp.jl
Expand Up @@ -1018,6 +1018,9 @@ end

# currently don't merge duplicates (this isn't required by MPB standard)
function affToExpr(aff::AffExpr, constant::Bool)
if length(aff.vars) == 0 && !constant
return 0
end
ex = Expr(:call,:+)
for k in 1:length(aff.vars)
push!(ex.args, Expr(:call,:*,aff.coeffs[k],:(x[$(aff.vars[k].col)])))
Expand Down Expand Up @@ -1055,6 +1058,11 @@ function tapeToExpr(k, nd::Vector{NodeData}, adj, const_values, parameter_values
op = nod.index
opsymbol = operators[op]
children_idx = nzrange(adj,k)
if opsymbol == :+ && length(children_idx) == 0
return 0
elseif opsymbol == :* && length(children_idx) == 0
return 1
end
ex = Expr(:call,opsymbol)
for cidx in children_idx
push!(ex.args, tapeToExpr(children_arr[cidx], nd, adj, const_values, parameter_values, subexpressions))
Expand Down
11 changes: 11 additions & 0 deletions test/nonlinear.jl
Expand Up @@ -663,6 +663,17 @@ facts("[nonlinear] Expression graph for ifelse") do
@fact MathProgBase.obj_expr(d) --> :(ifelse( x[1] <= 1, x[1]^2, x[1]))
end

facts("[nonlinear] Expression graphs for corner cases") do
m = Model()
@variable(m, x, start = 2)
@constraint(m, 0 <= 1)
@NLconstraint(m, x <= sum{0, i in []} + prod{1, i in []})
d = JuMP.NLPEvaluator(m)
MathProgBase.initialize(d, [:ExprGraph])
@fact MathProgBase.constr_expr(d,1) --> :(0 <= 1.0)
@fact MathProgBase.constr_expr(d,2) --> :(x[1] - (0 + 1) <= 0.0)
end

facts("[nonlinear] Hessians through MPB") do
# Issue 435
m = Model()
Expand Down

0 comments on commit 929ab54

Please sign in to comment.