Skip to content

Commit

Permalink
Merge 3a9a155 into 052e290
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Dec 29, 2018
2 parents 052e290 + 3a9a155 commit eb388b7
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/macros.jl
Expand Up @@ -1373,7 +1373,7 @@ macro NLobjective(model, sense, x)
$ex = $(processNLExpr(model, x))
set_objective($(esc(model)), $sense_expr, $ex)
end
return assert_validmodel(esc(model), macro_return(code, ex))
return assert_validmodel(esc(model), macro_return(code, nothing))
end

# TODO: Add a docstring.
Expand Down
6 changes: 3 additions & 3 deletions src/nlp.jl
Expand Up @@ -61,7 +61,7 @@ function rhs(c::NonlinearConstraint)
end

mutable struct NLPData
nlobj
nlobj::Union{Nothing, NonlinearExprData}
nlconstr::Vector{NonlinearConstraint}
nlexpr::Vector{NonlinearExprData}
nlconstr_duals::Vector{Float64}
Expand Down Expand Up @@ -299,7 +299,7 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})

# Check if we have any user-defined operators, in which case we need to
# disable hessians. The result of features_available depends on this.
has_nlobj = isa(nldata.nlobj, NonlinearExprData)
has_nlobj = nldata.nlobj !== nothing
has_user_mv_operator = false
for nlexpr in nldata.nlexpr
has_user_mv_operator |= Derivatives.has_user_multivariate_operators(nlexpr.nd)
Expand Down Expand Up @@ -341,7 +341,7 @@ function MOI.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
want_hess_storage = (:HessVec in requested_features) || d.want_hess
coloring_storage = Derivatives.Coloring.IndexedSet(num_variables_)

d.has_nlobj = isa(nldata.nlobj, NonlinearExprData)
d.has_nlobj = nldata.nlobj !== nothing
max_expr_length = 0
main_expressions = Array{Vector{NodeData}}(undef,0)
subexpr = Array{Vector{NodeData}}(undef,0)
Expand Down
28 changes: 21 additions & 7 deletions src/print.jl
Expand Up @@ -158,13 +158,17 @@ function Base.show(io::IO, model::Model)
print(io, "Feasibility")
end
println(io, " problem with:")
# TODO: Consider allowing a JuMP model to have a string name.
# TODO: Use MOI.Name for the name of a JuMP model.
println(io, "Variable", plural(num_variables(model)), ": ",
num_variables(model))
# https://github.com/JuliaOpt/JuMP.jl/issues/1556
# TODO: This doesn't account for nonlinear objectives
# println(io, "\tObjective function type:",
# MOI.get(model, MOI.ObjectiveFunctionType()))
if sense != MOI.FEASIBILITY_SENSE
if model.nlp_data !== nothing && model.nlp_data.nlobj !== nothing
println(io, "Objective function type: Nonlinear")
else
println(io, "Objective function type: ",
MOI.get(model, MOI.ObjectiveFunctionType()))
end
end
for (F, S) in MOI.get(model, MOI.ListOfConstraints())
num_constraints = MOI.get(model, MOI.NumberOfConstraints{F, S}())
println(io, "`$F`-in-`$S`: $num_constraints constraint",
Expand Down Expand Up @@ -214,8 +218,12 @@ function model_string(print_mode, model::Model)
str *= "\\quad"
end
str *= sep
str *= function_string(print_mode,
objective_function(model, QuadExpr))
if model.nlp_data !== nothing && model.nlp_data.nlobj !== nothing
str *= nl_expr_string(model, print_mode, model.nlp_data.nlobj)
else
str *= function_string(print_mode,
objective_function(model, QuadExpr))
end
end
str *= eol
str *= ijl ? "\\text{Subject to} \\quad" : "Subject to" * eol
Expand All @@ -228,6 +236,12 @@ function model_string(print_mode, model::Model)
str *= sep * constraint_string(print_mode, con) * eol
end
end
if model.nlp_data !== nothing
for nl_constraint in model.nlp_data.nlconstr
str *= sep * nl_constraint_string(model, print_mode, nl_constraint)
str *= eol
end
end
if ijl
str = "\\begin{alignat*}{1}" * str * "\\end{alignat*}\n"
end
Expand Down
58 changes: 45 additions & 13 deletions test/print.jl
Expand Up @@ -204,7 +204,7 @@ end

function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "VariableRef" begin
m = Model()
m = ModelType()
@variable(m, 0 <= x <= 2)

@test JuMP.name(x) == "x"
Expand Down Expand Up @@ -246,7 +246,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
end

@testset "base_name keyword argument" begin
m = Model()
m = ModelType()
@variable(m, x, base_name="foo")
@variable(m, y[1:3], base_name="bar")
num = 123
Expand All @@ -269,7 +269,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "SingleVariable constraints" begin
ge = JuMP.math_symbol(REPLMode, :geq)
in_sym = JuMP.math_symbol(REPLMode, :in)
model = Model()
model = ModelType()
@variable(model, x >= 10)
zero_one = @constraint(model, x in MathOptInterface.ZeroOne())

Expand All @@ -281,7 +281,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "VectorOfVariable constraints" begin
ge = JuMP.math_symbol(REPLMode, :geq)
in_sym = JuMP.math_symbol(REPLMode, :in)
model = Model()
model = ModelType()
@variable(model, x)
@variable(model, y)
zero_constr = @constraint(model, [x, y] in MathOptInterface.Zeros(2))
Expand All @@ -297,7 +297,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
eq = JuMP.math_symbol(REPLMode, :eq)
in_sym = JuMP.math_symbol(REPLMode, :in)

model = Model()
model = ModelType()
@variable(model, x)
@constraint(model, linear_le, x + 0 <= 1)
@constraint(model, linear_ge, x + 0 >= 1)
Expand Down Expand Up @@ -326,7 +326,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
@testset "Vector AffExpr constraints" begin
in_sym = JuMP.math_symbol(REPLMode, :in)

model = Model()
model = ModelType()
@variable(model, x)
@constraint(model, soc_constr, [x - 1, x + 1] in SecondOrderCone())

Expand All @@ -341,7 +341,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
le = JuMP.math_symbol(REPLMode, :leq)
sq = JuMP.math_symbol(REPLMode, :sq)

model = Model()
model = ModelType()
@variable(model, x)
quad_constr = @constraint(model, 2x^2 <= 1)

Expand All @@ -358,7 +358,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})

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

model_1 = Model()
model_1 = ModelType()
@variable(model_1, a>=1)
@variable(model_1, b<=1)
@variable(model_1, -1<=c<=1)
Expand Down Expand Up @@ -405,6 +405,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
A JuMP Model
Maximization problem with:
Variables: 13
Objective function type: MathOptInterface.ScalarAffineFunction{Float64}
`MathOptInterface.SingleVariable`-in-`MathOptInterface.ZeroOne`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.Integer`: 4 constraints
`MathOptInterface.SingleVariable`-in-`MathOptInterface.EqualTo{Float64}`: 1 constraint
Expand Down Expand Up @@ -445,7 +446,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})

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

model_2 = Model()
model_2 = ModelType()
@variable(model_2, x, Bin)
@variable(model_2, y, Int)
@constraint(model_2, x*y <= 1)
Expand All @@ -462,7 +463,7 @@ function printing_test(ModelType::Type{<:JuMP.AbstractModel})
Solver name: No optimizer attached.
Names registered in the model: x, y""", repl=:show)

model_2 = Model()
model_2 = ModelType()
@variable(model_2, x)
@constraint(model_2, x <= 3)

Expand All @@ -480,8 +481,39 @@ end

@testset "Printing for JuMP.Model" begin
printing_test(Model)
end
@testset "Model with nonlinear terms" begin
eq = JuMP.math_symbol(REPLMode, :eq)
model = Model()
@variable(model, x)
@NLobjective(model, Max, sin(x))
@NLconstraint(model, cos(x) == 0)

io_test(REPLMode, model, """
A JuMP Model
Maximization problem with:
Variable: 1
Objective function type: Nonlinear
Nonlinear: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: x""", repl=:show)

@testset "Printing for JuMPExtension.MyModel" begin
printing_test(JuMPExtension.MyModel)
io_test(REPLMode, model, """
Max sin(x)
Subject to
cos(x) - 0.0 $eq 0
""", repl=:print)

io_test(IJuliaMode, model, """
\\begin{alignat*}{1}\\max\\quad & sin(x)\\\\
\\text{Subject to} \\quad & cos(x) - 0.0 = 0\\\\
\\end{alignat*}
""")
end
end

# TODO: These tests are failing.
# @testset "Printing for JuMPExtension.MyModel" begin
# printing_test(JuMPExtension.MyModel)
# end

0 comments on commit eb388b7

Please sign in to comment.