Skip to content

Commit

Permalink
address above comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Jul 5, 2024
1 parent 0ac2b8c commit 7c5a870
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/node_variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function JuMP.upper_bound(nvref::NodeVariableRef)
end

function JuMP.delete_upper_bound(nvref::NodeVariableRef)
JuMP.delete(JuMP.owner_model(nvref), JuMP.LowerBoundRef(nvref))
JuMP.delete(JuMP.owner_model(nvref), JuMP.UpperBoundRef(nvref))
return nothing
end

Expand Down
9 changes: 9 additions & 0 deletions src/optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,15 @@ end
# Objective function
#

function has_node_objective(graph::OptiGraph)
for node in all_nodes(graph)
if has_objective(node)
return true
end
end
return false
end

function set_to_node_objectives(graph::OptiGraph)
obj = 0
for node in all_nodes(graph)
Expand Down
9 changes: 9 additions & 0 deletions src/optimizer_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ function JuMP.optimize!(
throw(JuMP.NoOptimizer())
end

# check for node objectives when graph objective is empty
if iszero(objective_function(graph))
if has_node_objective(graph)
@warn "The optigraph objective is empty but objectives exist on optinodes.
If this is not intended, consider using `set_to_node_objectives(graph)` to
set the graph objective function."
end
end

try
# make sure subgraph elements are tracked in parent graph after solve
MOI.optimize!(graph_backend(graph))
Expand Down
14 changes: 14 additions & 0 deletions src/optinode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ function JuMP.set_objective(
return nothing
end

function JuMP.set_objective_function(node::OptiNode, func::JuMP.AbstractJuMPScalar)
# check that all func terms are for this node
unique(collect_nodes(func)) == [node] || error("Optinode does not own all variables.")
d = JuMP.object_dictionary(node)
d[(node, :objective_function)] = func
return nothing
end

function JuMP.set_objective_sense(node::OptiNode, sense::MOI.OptimizationSense)
d = JuMP.object_dictionary(node)
d[(node, :objective_sense)] = sense
return nothing
end

function JuMP.objective_function(node::OptiNode)
return JuMP.object_dictionary(node)[(node, :objective_function)]
end
Expand Down
6 changes: 6 additions & 0 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ function test_objective_functions()
@test objective_function(graph) ==
n1[:x] - n2[:x] + n3[:x][1]^2 + n3[:x][2]^2 + n4[:x]^2

JuMP.set_objective_function(n1, n1[:y])
JuMP.set_objective_sense(n1, MOI.MAX_SENSE)
set_to_node_objectives(graph)
@test objective_function(graph) ==
-n1[:y] - n2[:x] + n3[:x][1]^2 + n3[:x][2]^2 + n4[:x]^2

@objective(n4, Min, n4[:x]^3)
set_to_node_objectives(graph)
@test typeof(objective_function(graph)) == GenericNonlinearExpr{NodeVariableRef}
Expand Down

0 comments on commit 7c5a870

Please sign in to comment.