Skip to content

Commit

Permalink
updating examples; update optimizer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jalving committed May 18, 2024
1 parent a5ae476 commit d358e56
Show file tree
Hide file tree
Showing 17 changed files with 164 additions and 211 deletions.
58 changes: 58 additions & 0 deletions examples/01_create_optigraph.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Plasmo
using Ipopt

graph = OptiGraph(;name=:graph)

#Add nodes to a OptiGraph
n1 = add_node(graph)
@variable(n1, 0 <= x <= 2)
@variable(n1, 0 <= y <= 3)
@variable(n1, 0 <= z <= 2)
@constraint(n1, x + y + z >= 4)

n2 = add_node(graph)
@variable(n2, x >= 0)
@constraint(n2, ref, exp(x) >= 2)
@variable(n2, 0 <= z <= 2)
@constraint(n2, z + x >= 4)

n3 = add_node(graph)
@variable(n3, x[1:3] >= 0)
@constraint(n3, nlcon, exp(x[3]) >= 5)
@constraint(n3, conref, sum(x[i] for i in 1:3) == 10)

n4 = add_node(graph)
@variable(n4, x[1:2] >= 0)
@constraint(n4, sum(x[i] for i in 1:2) >= 10)
@constraint(n4, ref, exp(x[2]) >= 4)

# Add link constsraints
@linkconstraint(graph, link1, n1[:x] == n2[:x])
@linkconstraint(graph, link2, n2[:x] == n3[:x][3])
@linkconstraint(graph, link3, n3[:x][1] == n4[:x][1])

# set an objective for the graph
@objective(graph, Min, n1[:y] + n2[:x] - (n3[:x][1] + n3[:x][2] + n3[:x][3]) + n4[:x][2]^3)

# optimize the graph
optimizer = Ipopt.Optimizer
set_optimizer(graph, optimizer)
optimize!(graph)

println()
println("objective value = ", objective_value(graph))
println()

println("variable values:")
for var in all_variables(graph)
println(var, " = ", value(var))
end
println()

println("constraint dual values:")
for constraint_type in list_of_constraint_types(graph)
cons = all_constraints(graph, constraint_type[1], constraint_type[2])
for con in cons
println("($con) = $(dual(con))")
end
end
47 changes: 47 additions & 0 deletions examples/02_create_nested_optigraph.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Plasmo
using Ipopt

function add_model(graph::OptiGraph)
node = add_node(graph)
@variable(node, x >= 0)
@variable(node, y >= 1)
@constraint(node, x + y <= 5)
@constraint(node, exp(x) >= 2)
return node
end

# the top-level graph
graph = OptiGraph(;name=:graph)

# subgraph 1
subgraph1 = OptiGraph(;name=:sg1)
n1 = add_model(subgraph1)
n2 = add_model(subgraph1)
@linkconstraint(subgraph1, n1[:x] == n2[:x])

# subgraph 2
subgraph2 = OptiGraph(;name=:sg2)
n3 = add_model(subgraph2)
n4 = add_model(subgraph2)
@linkconstraint(subgraph2, n3[:x] == n4[:x])

# add subgraphs to top-level graph
add_subgraph(graph, subgraph1)
add_subgraph(graph, subgraph2)

# add links between subgraphs
@linkconstraint(graph, n1[:x] == n3[:x])
@linkconstraint(graph, n2[:x] == n4[:x])

# objective function
@objective(graph, Min, sum(node[:x] + node[:y] for node in all_nodes(graph)))

set_optimizer(graph, Ipopt.Optimizer)
optimize!(graph)

println("n1[:x]= ", value(graph, n1[:x]))
println("n2[:x]= ", value(graph, n2[:x]))
println("n3[:x]= ", value(graph, n3[:x]))
println("n4[:x]= ", value(graph, n4[:x]))

println("objective = ", objective_value(graph))
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
#Example
using Plasmo
using GLPK
using HiGHS

#Create a modelgraph
# create optigraph; set optimizer
graph = OptiGraph()
optimizer = GLPK.Optimizer
optimizer = HiGHS.Optimizer

#Add nodes to optigraph
# add nodes using macro
n1 = @optinode(graph)
n2 = @optinode(graph)

#Node 1 Model
@variable(n1, 0 <= x <= 2)
# node 1 model
@variable(n1, 1 <= x <= 2)
@variable(n1, 0 <= y <= 3)
@variable(n1, z >= 0)
@constraint(n1, x + y + z >= 4)

#Node 2 Model
# node 2 model
@variable(n2, x)
@variable(n2, z >= 0)
@constraint(n2, z + x >= 4)

#Link constraints take the same expressions as the JuMP @constraint macro
# add link constraints
@linkconstraint(graph, n1[:x] == n2[:x])
@linkconstraint(graph, n1[:z] == n2[:z])

#Objective function
# add objective function
@objective(graph, Min, n1[:y] + n2[:x] + n1[:z])

#Aggregate optinodes into a single optinode to solve using JuMP's interface
# aggregate optinodes into a single optinode
aggregate_node, reference_map = aggregate(graph)
set_optimizer(aggregate_node, optimizer)
optimize!(aggregate_node)
agg_graph = source_graph(aggregate_node)
set_optimizer(agg_graph, optimizer)
optimize!(agg_graph)

#Use the reference map to look up values on the aggregate node
# use the reference map to look up values on the aggregate node
println("n1[:x] = ", value(reference_map[n1[:x]]))
println("n2[:x] = ", value(reference_map[n2[:x]]))
println("n1[:z] = ", value(reference_map[n1[:z]]))
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using Plasmo
using LightGraphs
using Graphs

function create_optigraph()
graph = OptiGraph()
function create_optigraph(name)
graph = OptiGraph(;name=name)
@optinode(graph, nodes[1:3])

#node 1
# node 1
@variable(nodes[1], 0 <= x <= 2)
@variable(nodes[1], 0 <= y <= 3)
@constraint(nodes[1], x + y <= 4)
@objective(nodes[1], Min, x)

#node 2
# node 2
@variable(nodes[2], x >= 1)
@variable(nodes[2], 0 <= y <= 5)
@constraint(nodes[2], x + y <= 7)
@objective(nodes[2], Min, x)

#node 3
# node 3
@variable(nodes[3], x >= 0)
@variable(nodes[3], y >= 0)
@constraint(nodes[3], x + y == 2)
@objective(nodes[3], Max, x)

#Link constraints take the same expressions as the JuMP @constraint macro
# link constraints
@linkconstraint(graph, nodes[1][:x] == nodes[2][:x])
@linkconstraint(graph, nodes[2][:y] == nodes[3][:x])
@linkconstraint(graph, nodes[3][:x] == nodes[1][:x])
Expand All @@ -32,11 +32,11 @@ function create_optigraph()
return graph
end

graph = OptiGraph()
graph = OptiGraph(;name=:graph)

graph1 = create_optigraph()
graph2 = create_optigraph()
graph3 = create_optigraph()
graph1 = create_optigraph(:sg1)
graph2 = create_optigraph(:sg2)
graph3 = create_optigraph(:sg3)

add_subgraph!(graph, graph1)
add_subgraph!(graph, graph2)
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 0 additions & 7 deletions examples/dev_optigraph.jl

This file was deleted.

File renamed without changes.
58 changes: 0 additions & 58 deletions examples/optigraph_2_nodes.jl

This file was deleted.

64 changes: 0 additions & 64 deletions examples/optigraph_4_nodes.jl

This file was deleted.

46 changes: 0 additions & 46 deletions examples/optigraph_subgraphs.jl

This file was deleted.

Loading

0 comments on commit d358e56

Please sign in to comment.