Skip to content

Commit

Permalink
support parameters and make performance updates (#128)
Browse files Browse the repository at this point in the history
* support variable-constrain and parameters

* fix parameter issues

* more utility funcs

* linting

* bump version

* add separable tests
  • Loading branch information
jalving authored Nov 2, 2024
1 parent 13f130b commit 0a22e91
Show file tree
Hide file tree
Showing 12 changed files with 576 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Plasmo"
uuid = "d3f7391f-f14a-50cc-bbe4-76a32d1bad3c"
authors = ["Jordan Jalving <jhjalving@gmail.com>"]
repo = "https://github.com/plasmo-dev/Plasmo.jl.git"
version = "0.6.3"
version = "0.6.4"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![](https://img.shields.io/badge/docs-dev-blue.svg)](https://plasmo-dev.github.io/Plasmo.jl/dev/)
[![](https://img.shields.io/badge/docs-stable-blue.svg)](https://plasmo-dev.github.io/Plasmo.jl/stable/)
[![DOI](https://zenodo.org/badge/96967382.svg)](https://zenodo.org/badge/latestdoi/96967382)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)


# Plasmo.jl

Expand Down
8 changes: 7 additions & 1 deletion src/Plasmo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export OptiGraph,
OptiNode,
OptiEdge,
NodeVariableRef,
EdgeConstraintRef,
direct_moi_graph,
graph_backend,
graph_index,
Expand Down Expand Up @@ -108,7 +109,10 @@ export OptiGraph,

# other functions

set_jump_model
set_jump_model,
extract_variables,
is_separable,
extract_separable_terms

include("core_types.jl")

Expand Down Expand Up @@ -138,6 +142,8 @@ include("graph_functions/topology.jl")

include("graph_functions/partition.jl")

include("utils.jl")

# extensions
function __init__()
@require KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880" include(
Expand Down
57 changes: 46 additions & 11 deletions src/backends/moi_backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,13 @@ function _add_edge(backend::GraphMOIBackend, edge::OptiEdge)
return nothing
end

#
# MOI Methods
#

### graph attributes
## graph attributes

function MOI.supports(backend::GraphMOIBackend, attr::MOI.AnyAttribute, args...)
return MOI.supports(JuMP.backend(backend), attr, args...)
end

function MOI.get(
backend::GraphMOIBackend, attr::AT
Expand Down Expand Up @@ -528,22 +530,54 @@ end
# MOI variables and constraints
#

function MOI.add_variable(graph_backend::GraphMOIBackend, vref::NodeVariableRef)
function MOI.add_variable(backend::GraphMOIBackend, vref::NodeVariableRef)
# return if variable already exists in backend
vref in keys(graph_backend.element_to_graph_map.var_map) && return nothing
vref in keys(backend.element_to_graph_map.var_map) && return nothing

# add the variable
graph_var_index = MOI.add_variable(graph_backend.moi_backend)
graph_var_index = MOI.add_variable(backend.moi_backend)

# map reference to index
backend.element_to_graph_map[vref] = graph_var_index
backend.graph_to_element_map[graph_var_index] = vref

# create key for node if necessary
if !haskey(backend.node_variables, vref.node)
backend.node_variables[vref.node] = MOI.VariableIndex[]
end
push!(backend.node_variables[vref.node], graph_var_index)
return graph_var_index
end

function MOI.add_constrained_variable(
backend::GraphMOIBackend,
vref::NodeVariableRef,
cref::NodeConstraintRef,
set::MOI.AbstractScalarSet,
)
# return if variable already exists in backend
vref in keys(backend.element_to_graph_map.var_map) && return nothing

# add the variable and parameter constraint
graph_var_index, graph_con_index = MOI.add_constrained_variable(
backend.moi_backend, set
)

# map reference to index
graph_backend.element_to_graph_map[vref] = graph_var_index
graph_backend.graph_to_element_map[graph_var_index] = vref
backend.element_to_graph_map[vref] = graph_var_index
backend.graph_to_element_map[graph_var_index] = vref
backend.element_to_graph_map[cref] = graph_con_index
backend.graph_to_element_map[graph_con_index] = cref

# create key for node if necessary
if !haskey(graph_backend.node_variables, vref.node)
graph_backend.node_variables[vref.node] = MOI.VariableIndex[]
if !haskey(backend.node_variables, vref.node)
backend.node_variables[vref.node] = MOI.VariableIndex[]
end
if !haskey(backend.element_constraints, vref.node)
graph_backend.element_constraints[vref.node] = MOI.ConstraintIndex[]
end
push!(graph_backend.node_variables[vref.node], graph_var_index)
push!(backend.node_variables[vref.node], graph_var_index)
push!(backend.element_constraints[vref.node], graph_con_index)
return graph_var_index
end

Expand Down Expand Up @@ -879,6 +913,7 @@ function _copy_node_variables(

# map existing variables in the index_map
# existing variables may come from linking constraints added between graphs
# TODO: could be slow...
existing_vars = intersect(node_variables, keys(dest.element_to_graph_map.var_map))
for var in existing_vars
src_graph_index = graph_index(var)
Expand Down
2 changes: 0 additions & 2 deletions src/core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ struct ElementData{GT<:AbstractOptiGraph}
# track constraint indices
last_constraint_index::OrderedDict{OptiElement,Int}
end

# default is OptiGraph
function ElementData(GT::Type{<:AbstractOptiGraph})
return ElementData{GT}(
OrderedDict{OptiNode{GT},Vector{GT}}(),
Expand Down
Loading

2 comments on commit 0a22e91

@jalving
Copy link
Member Author

@jalving jalving commented on 0a22e91 Nov 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/118539

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.4 -m "<description of version>" 0a22e9133b93bc46716d666a1dc9bb72bce4a1fe
git push origin v0.6.4

Please sign in to comment.