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.
fix issue with wrong nonlinear variable indices
- Loading branch information
Showing
8 changed files
with
180 additions
and
88 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 |
---|---|---|
@@ -1 +1,50 @@ | ||
aggregate.jl | ||
""" | ||
aggregate_backends!(graph::OptiGraph) | ||
Aggregate the moi backends from each subgraph within `graph` to create a single backend. | ||
""" | ||
function aggregate_backends!(graph::OptiGraph) | ||
dest = JuMP.backend(graph) | ||
|
||
end | ||
|
||
### Helpful utilities | ||
|
||
# """ | ||
# append_to_backend!(dest::MOI.ModelLike, src::MOI.ModelLike) | ||
|
||
# Copy the underylying model from `src` into `dest`, but ignore attributes | ||
# such as objective function and objective sense | ||
# """ | ||
# function append_to_backend!(dest::MOI.ModelLike, src::MOI.ModelLike) | ||
# vis_src = MOI.get(src, MOI.ListOfVariableIndices()) #returns vector of MOI.VariableIndex | ||
# index_map = MOIU.IndexMap() | ||
|
||
|
||
# # has_nlp = MOI.NLPBlock() in MOI.get(src, MOI.ListOfModelAttributesSet()) | ||
# # constraints_not_added = if has_nlp | ||
# constraints_not_added = Any[ | ||
# MOI.get(src, MOI.ListOfConstraintIndices{F,S}()) for | ||
# (F, S) in MOI.get(src, MOI.ListOfConstraintTypesPresent()) if | ||
# MOIU._is_variable_function(F) | ||
# ] | ||
# # else | ||
# # Any[ | ||
# # MOIU._try_constrain_variables_on_creation(dest, src, index_map, S) | ||
# # for S in MOIU.sorted_variable_sets_by_cost(dest, src) | ||
# # ] | ||
# # end | ||
|
||
# # Copy free variables into graph optimizer | ||
# MOI.Utilities._copy_free_variables(dest, index_map, vis_src) | ||
|
||
# # Copy variable attributes (e.g. name, and VariablePrimalStart()) | ||
# MOI.Utilities.pass_attributes(dest, src, index_map, vis_src) | ||
|
||
# # Normally this copies ObjectiveSense() and ObjectiveFunction(), but we don't want to do that here | ||
# #MOI.Utilities.pass_attributes(dest, src, idxmap) | ||
|
||
# MOI.Utilities._pass_constraints(dest, src, index_map, constraints_not_added) | ||
|
||
# return index_map #return an idxmap for each source model | ||
# 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
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,4 +1,4 @@ | ||
# dev file for subgraphs | ||
# dev file for subgraphs; single backend | ||
using Plasmo | ||
using HiGHS | ||
|
||
|
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,18 +1,75 @@ | ||
using JuMP | ||
using Ipopt | ||
# dev file for subgraphs; different backends | ||
using Plasmo | ||
using HiGHS | ||
|
||
m = Model() | ||
@variable(m, x >= 0) | ||
@variable(m, y >= 0) | ||
@constraint(m, ref, x^3 + y^3 >= 0) | ||
graph = OptiGraph(; name=:g1) | ||
|
||
jump_func = jump_function(constraint_object(ref)) | ||
moi_func = moi_function(constraint_object(ref)) | ||
# subgraph1 | ||
sg1 = Plasmo.add_subgraph(graph; name=:sg1) | ||
|
||
# node 1 | ||
n1 = Plasmo.add_node(sg1) | ||
@variable(n1, x >= 0) | ||
@variable(n1, y >= 0) | ||
@constraint(n1, ref1, n1[:x]+ n1[:y] <= 10) | ||
|
||
f(x::Real) = x^2 | ||
@operator(m, op_f, 1, f) | ||
@expression(m, z, op_f(x)) | ||
#node 2 | ||
n2 = Plasmo.add_node(sg1) | ||
@variable(n2, x >= 1) | ||
@variable(n2, y >= 2) | ||
@constraint(n2, ref2, n2[:x] + n2[:y] <= 4) | ||
|
||
set_optimizer(m, Ipopt.Optimizer) | ||
JuMP.optimize!(m) | ||
# linking constraint on subgraph1 | ||
edge1 = Plasmo.add_edge(sg1, n1, n2) | ||
@constraint(edge1, ref_edge_1, n1[:x] == n2[:x]) | ||
|
||
# subgraph 2 | ||
sg2 = Plasmo.add_subgraph(graph; name=:sg2) | ||
|
||
# node 3 | ||
n3 = Plasmo.add_node(sg2) | ||
@variable(n3, x >= 0) | ||
@variable(n3, y >= 0) | ||
@constraint(n3, ref3, n3[:x] + n3[:y] <= 10) | ||
|
||
#node 4 | ||
n4 = Plasmo.add_node(sg2) | ||
@variable(n4, x >= 1) | ||
@variable(n4, y >= 2) | ||
@constraint(n4, ref4, n4[:x] + n4[:y] <= 4) | ||
|
||
# linking constraint on subgraph2 | ||
edge2 = Plasmo.add_edge(sg2, n3, n4) | ||
@constraint(edge2, ref_edge_2, n3[:x] == n4[:x]) | ||
|
||
# link across subgraphs | ||
edge3 = Plasmo.add_edge(graph, n2, n4) | ||
@constraint(edge3, ref_edge_3, n2[:y] == n4[:y]) | ||
|
||
|
||
# optimize subgraph1 | ||
@objective(sg1, Min, n1[:x] + n2[:y]) | ||
set_optimizer(sg1, HiGHS.Optimizer) | ||
optimize!(sg1) | ||
|
||
# optimize subgraph2 | ||
@objective(sg2, Min, n3[:x] + n4[:y]) | ||
set_optimizer(sg2, HiGHS.Optimizer) | ||
optimize!(sg2) | ||
|
||
|
||
# optimize complete graph | ||
# @objective(graph, Max, n1[:x] + n2[:x] + n3[:x] + n4[:x]) | ||
# set_optimizer(graph, HiGHS.Optimizer) | ||
# optimize!(graph) | ||
|
||
|
||
|
||
# @show value(n1[:x]) | ||
# @show value(n2[:x]) | ||
# @show value(n3[:x]) | ||
# @show value(n4[:x]) | ||
# @show value(n2[:y]) | ||
# @show value(n4[:y]) | ||
|
||
# println(objective_value(graph)) |
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
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
Oops, something went wrong.