plasmo-dev / Plasmo.jl

A Platform for Scalable Modeling and Optimization
Other
143 stars 20 forks source link

Adding variables or objective to overall `OptiGraph` object #72

Open avinashresearch1 opened 1 year ago

avinashresearch1 commented 1 year ago

Hi, Thanks for the exciting toolkit.

I see in the API documentation that the OptiGraph object extends a JuMP.AbstractModel and supports most JuMP.Model functions. My understanding was that one could specify certain OptiNode objects within the OptiGraph, and then one could have a custom overall objective that one would specify in the OptiGraphtaking variables from different OptiNodes.

For example, I would like to only specify feasibility problems in the OptiNodes and then set up an objective for the OptiGraph (instead of using a linear combination as is default).

However, when I try to add a variable or an objective to the Optigraph object, I get a MethodError:

image

  1. I realize that jump_model(node::OptiNode) prints out the respective JuMP model. I was wondering if there is a corresponding function for the OptiGraph
jalving commented 1 year ago

Hi @avinashresearch1. Thanks for your interest in the package!

An OptiGraph does not support adding variables directly. It requires variables to be defined on OptiNodes (although I have been thinking about adding support for light-weight variable nodes). You can still create your objective function using @objective, you just have to reference variables on the nodes. For example, you should be able to do something like:

graph = OptiGraph()
@optinode(graph, node1)
@optinode(graph, node2)
@variable(node1, x>=0)
@variable(node2, x>=0)
@objective(graph, Min, node1[:x] + node2[:x])

For your second question, the closest thing is aggregate(graph). This copies everything in the optigraph into a new node and returns the node and a dictionary that maps optigraph elements to the new node elements. This would look like:

aggregated_node, ref_dict = aggregate(graph)
aggregated_node_x1 = ref_dict[node1[:x]] 
aggregated_node_x2 = ref_dict[node2[:x]]

The OptiGraph is actually a model object in its own right with a MathOptInterface backend just like a JuMP model.