Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing constant objective function #80

Merged
merged 12 commits into from
Jun 29, 2020
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ os:
julia:
- 1.0
- 1.1
- 1.2
- 1.3
- 1.4
notifications:
email: false
sudo: false
Expand Down
12 changes: 9 additions & 3 deletions src/StructJuMP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,17 @@ function JuMP.constraint_object(cref::StructuredConstraintRef, F::Type, S::Type)
end

# Objective
function JuMP.set_objective(m::StructuredModel, sense::MOI.OptimizationSense,
f::JuMP.AbstractJuMPScalar)
m.objective_sense = sense
function JuMP.set_objective_function(m::StructuredModel, f::AbstractJuMPScalar)
m.objective_function = f
end

JuMP.set_objective_function(m::StructuredModel, f::Real) = JuMP.set_objective_function(m, convert(AffExpr, f))

function JuMP.set_objective(m::StructuredModel, sense::MOI.OptimizationSense, f)
kibaekkim marked this conversation as resolved.
Show resolved Hide resolved
kibaekkim marked this conversation as resolved.
Show resolved Hide resolved
m.objective_sense = sense
set_objective_function(m, f)
end

JuMP.objective_sense(m::StructuredModel) = m.objective_sense
JuMP.objective_function_type(model::StructuredModel) = typeof(model.objective_function)
JuMP.objective_function(model::StructuredModel) = model.objective_function
Expand Down
23 changes: 23 additions & 0 deletions test/objective.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Test
using StructJuMP

@testset "objective functions" begin
m = StructuredModel(num_scenarios = 2)
@variable(m, x >= 0)
@objective(m, Min, x*x)

sm = StructuredModel(parent = m, id = 1)
@variable(sm, y >= 0)
@objective(sm, Max, y)

sm = StructuredModel(parent = m, id = 2)
@variable(sm, y >= 0)
@objective(sm, Min, 0)

@test objective_sense(m) == MOI.MIN_SENSE
@test objective_sense(m.children[1]) == MOI.MAX_SENSE
@test objective_sense(m.children[2]) == MOI.MIN_SENSE
@test objective_function_type(m) == GenericQuadExpr{Float64,StructJuMP.StructuredVariableRef}
@test objective_function_type(m.children[1]) == StructJuMP.StructuredVariableRef
@test objective_function_type(m.children[2]) == GenericAffExpr{Float64,VariableRef}
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ include("benderstest.jl")
# include("farmer.jl")

include("examples_smoketest.jl")

include("objective.jl")