forked from plasmo-dev/Plasmo.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updating examples; update optimizer interface
- Loading branch information
Showing
17 changed files
with
164 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
28 changes: 15 additions & 13 deletions
28
examples/optigraph_aggregate.jl → examples/03_aggregate_optigraphs.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.