- 
                Notifications
    
You must be signed in to change notification settings  - Fork 560
 
Model Observer #3695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Model Observer #3695
Changes from 16 commits
f2066ea
              897c0ef
              97aeb31
              a69d5e6
              9763e9a
              ff635b8
              db0fda4
              149ab06
              42c8cc8
              ccb6de4
              d718e9a
              23ba4d9
              14f928b
              d7b9918
              c313fe5
              a424cfb
              33f831a
              5f3f403
              79e1b47
              7275176
              823e15a
              73258f5
              5a8a5a6
              22c9169
              321755a
              f8cfc33
              2adefbc
              df26c4e
              2dc566e
              6b2ffbd
              e68ee73
              82e40b2
              ae7e031
              5778688
              cd1c4ef
              3f30893
              4f7e93e
              ac9d952
              dc19b17
              bb60959
              99ac089
              213d353
              065f43e
              ddf0a39
              643f546
              f569d43
              be0e043
              0739cf6
              dfbdce0
              3408913
              9bee2bd
              1568e1f
              817f274
              a000db6
              2d54b54
              036a320
              9b236b6
              ab3400f
              af51971
              7d09c27
              880dfe8
              4cea685
              d96283e
              2fbe49f
              cfdf7d6
              8edfb8f
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # ___________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2025 | ||
| # National Technology and Engineering Solutions of Sandia, LLC | ||
| # Under the terms of Contract DE-NA0003525 with National Technology and | ||
| # Engineering Solutions of Sandia, LLC, the U.S. Government retains certain | ||
| # rights in this software. | ||
| # This software is distributed under the 3-clause BSD License. | ||
| # ___________________________________________________________________________ | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # ___________________________________________________________________________ | ||
| # | ||
| # Pyomo: Python Optimization Modeling Objects | ||
| # Copyright (c) 2008-2025 | ||
| # National Technology and Engineering Solutions of Sandia, LLC | ||
| # Under the terms of Contract DE-NA0003525 with National Technology and | ||
| # Engineering Solutions of Sandia, LLC, the U.S. Government retains certain | ||
| # rights in this software. | ||
| # This software is distributed under the 3-clause BSD License. | ||
| # ___________________________________________________________________________ | ||
| 
     | 
||
| from pyomo.core.expr.visitor import StreamBasedExpressionVisitor | ||
| from pyomo.core.expr.numeric_expr import ( | ||
| ExternalFunctionExpression, | ||
| NPV_ExternalFunctionExpression, | ||
| ) | ||
| from pyomo.core.base.var import VarData, ScalarVar | ||
| from pyomo.core.base.param import ParamData, ScalarParam | ||
| from pyomo.core.base.expression import ExpressionData, ScalarExpression | ||
| 
     | 
||
| 
     | 
||
| def handle_var(node, collector): | ||
| collector.variables[id(node)] = node | ||
                
       | 
||
| return None | ||
| 
     | 
||
| 
     | 
||
| def handle_param(node, collector): | ||
| collector.params[id(node)] = node | ||
| return None | ||
| 
     | 
||
| 
     | 
||
| def handle_named_expression(node, collector): | ||
| collector.named_expressions[id(node)] = node | ||
| return None | ||
                
      
                  michaelbynum marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| 
     | 
||
| def handle_external_function(node, collector): | ||
| collector.external_functions[id(node)] = node | ||
| return None | ||
| 
     | 
||
| 
     | 
||
| collector_handlers = { | ||
| VarData: handle_var, | ||
| ScalarVar: handle_var, | ||
| ParamData: handle_param, | ||
| ScalarParam: handle_param, | ||
| ExpressionData: handle_named_expression, | ||
| ScalarExpression: handle_named_expression, | ||
| ExternalFunctionExpression: handle_external_function, | ||
| NPV_ExternalFunctionExpression: handle_external_function, | ||
| } | ||
| 
     | 
||
| 
     | 
||
| class _ComponentFromExprCollector(StreamBasedExpressionVisitor): | ||
                
      
                  michaelbynum marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| def __init__(self, **kwds): | ||
| self.named_expressions = {} | ||
| self.variables = {} | ||
| self.params = {} | ||
| self.external_functions = {} | ||
| super().__init__(**kwds) | ||
| 
     | 
||
| def exitNode(self, node, data): | ||
| nt = type(node) | ||
| if nt in collector_handlers: | ||
| return collector_handlers[nt](node, self) | ||
| return None | ||
| 
     | 
||
| 
     | 
||
| _visitor = _ComponentFromExprCollector() | ||
| 
     | 
||
| 
     | 
||
| def collect_components_from_expr(expr): | ||
| _visitor.__init__() | ||
| _visitor.walk_expression(expr) | ||
| return ( | ||
| list(_visitor.named_expressions.values()), | ||
| list(_visitor.variables.values()), | ||
| list(_visitor.params.values()), | ||
| list(_visitor.external_functions.values()), | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.