Skip to content

Commit

Permalink
fix issue with is_valid
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed Jun 18, 2024
1 parent 014f681 commit 7a03518
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,13 @@ function MOI.is_valid(backend::GraphMOIBackend, cref::ConstraintRef)
return MOI.is_valid(backend.moi_backend, graph_index(cref))
end

# function MOI.is_valid(backend::GraphMOIBackend, vi::MOI.VariableIndex)
# return MOI.is_valid(backend.moi_backend, vi)
# end
function MOI.is_valid(backend::GraphMOIBackend, vi::MOI.VariableIndex)
return MOI.is_valid(backend.moi_backend, vi)
end

# function MOI.is_valid(backend::GraphMOIBackend, ci::MOI.ConstraintIndex)
# return MOI.is_valid(backend.moi_backend, ci)
# end
function MOI.is_valid(backend::GraphMOIBackend, ci::MOI.ConstraintIndex)
return MOI.is_valid(backend.moi_backend, ci)
end

# optimize!

Expand Down
5 changes: 4 additions & 1 deletion src/node_variables.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# TODO: parameterize on precision
# TODO: move low-level methods to graph backend
# TODO: start values

struct NodeVariableRef <: JuMP.AbstractVariableRef
node::OptiNode
index::MOI.VariableIndex
Expand Down Expand Up @@ -245,7 +248,7 @@ function _moi_nv_set_lower_bound(
end

function JuMP.has_upper_bound(nvref::NodeVariableRef)
return _moi_nv_has_upper_bound(graph_backend(nvref), nvref)
return _moi_nv_has_upper_bound(nvref)
end

function JuMP.set_upper_bound(nvref::NodeVariableRef, upper::Number)
Expand Down
78 changes: 46 additions & 32 deletions test/test_optigraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,14 @@ function test_variables()

@optinode(graph, nodes[1:2])
n1,n2 = all_nodes(graph)

@variable(n1, x >= 1)
@variable(n2, x >= 2)

@test has_lower_bound(n1[:x]) == true
@test has_upper_bound(n1[:x]) == false
@test lower_bound(n1[:x]) == 1

# fix variables
JuMP.fix(n1[:x], 1; force=true)
optimize!(graph)
Expand All @@ -247,44 +251,54 @@ function test_variables()
optimize!(graph)
@test value(n1[:x]) == 0

# TODO
# set start value
# set_start_value(n2[:x], 3.0)
# @test start_value(n2[:x]) == 3.0

# integer and binary
set_binary(n1[:x])
@test is_binary(n1[:x]) == true
optimize!(graph)

set_integer(n2[:x])
@test is_integer(n2[:x]) == true
optimize!(graph)
end

# function test_assemble_optigraph()
# graph = _create_optigraph()
# new_optigraph = assemble_optigraph(all_nodes(graph), all_edges(graph))
function test_assemble_optigraph()
graph = _create_test_nonlinear_optigraph()
new_optigraph = assemble_optigraph(all_nodes(graph), all_edges(graph))

# @test num_all_nodes(optigraph_ref) == num_all_nodes(graph)
# @test num_all_variables(optigraph_ref) == num_all_variables(graph)
# @test num_constraints(optigraph_ref) == num_constraints(graph)
# @test num_all_linkconstraints(optigraph_ref) == num_all_linkconstraints(graph)
# end
@test num_nodes(new_optigraph) == num_nodes(graph)
@test num_variables(new_optigraph) == num_variables(graph)
@test num_constraints(new_optigraph) == num_constraints(graph)
@test num_link_constraints(new_optigraph) == num_link_constraints(graph)
end

# function test_multiple_solves()
# graph = _create_optigraph()
# n1 = optinode(graph, 1)
# set_optimizer(graph, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
# optimize!(graph)
# @test isapprox(value(n1[:x]), 1, atol=1e-6)

# set_lower_bound(n1[:x], 1.5)
# optimize!(graph)
# @test isapprox(value(n1[:x]), 1.5, atol=1e-6)

# set_start_value(n1[:x], 10)
# optimize!(graph)
# @test isapprox(value(n1[:x]), 1.5, atol=1e-6)
# @test start_value(n1[:x]) == 10

# # TODO: support variable attributes on optigraph
# set_start_value(graph, n1[:x], 20)
# optimize!(graph)
# @test isapprox(value(n1[:x]), 1.5, atol=1e-6)
# @test start_value(graph, n1[:x]) == 20
# @test graph.moi_backend.optimizer.model.variable_primal_start[1] == 20
# end
function test_multiple_solves()
graph = _create_test_nonlinear_optigraph()
n1 = graph[1]
set_optimizer(graph, optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0))
optimize!(graph)
@test isapprox(value(n1[:x]), 1, atol=1e-6)

set_lower_bound(n1[:x], 1.5)
optimize!(graph)
@test isapprox(value(n1[:x]), 1.5, atol=1e-6)

set_start_value(n1[:x], 10)
optimize!(graph)
@test isapprox(value(n1[:x]), 1.5, atol=1e-6)
@test start_value(n1[:x]) == 10

# TODO: support variable attributes on optigraph
set_start_value(graph, n1[:x], 20)
optimize!(graph)
@test isapprox(value(n1[:x]), 1.5, atol=1e-6)
@test start_value(graph, n1[:x]) == 20
@test graph.moi_backend.optimizer.model.variable_primal_start[1] == 20
end

# function test_optimizer_attributes()
# graph = _create_optigraph()
Expand Down

0 comments on commit 7a03518

Please sign in to comment.