Skip to content
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

Fix handling of POW in Baron writer #2693

Merged
merged 4 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions pyomo/repn/plugins/baron_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@


def _handle_PowExpression(visitor, node, values):
x,y = node.args
if type(x) not in native_types and not x.is_fixed() and \
type(y) not in native_types and not y.is_fixed():
# Per the BARON manual, x ^ y is allowed as long as x
# and y are not both variables
return f"exp(({values[0]}) * log({values[1]}))"
else:
# Per the BARON manual, x ^ y is allowed as long as x and y are not
# both variables. There is an issue that if one of the arguments
# contains "0*var", Pyomo will see that as fixed, but Baron will see
# it as variable. We will work around that by resolving any fixed
# expressions to their corresponding fixed value.
unfixed_count = 0
for i, arg in enumerate(node.args):
if type(arg) in native_types:
pass
elif arg.is_fixed():
values[i] = ftoa(value(arg))
else:
unfixed_count += 1

if unfixed_count < 2:
return f"{values[0]} ^ {values[1]}"
else:
return f"exp(({values[0]}) * log({values[1]}))"

_allowableUnaryFunctions = {
'exp', 'log10', 'log', 'sqrt',
Expand Down
27 changes: 26 additions & 1 deletion pyomo/repn/tests/baron/test_baron.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
from io import StringIO

import pyomo.common.unittest as unittest

from pyomo.common.collections import OrderedSet
from pyomo.common.fileutils import this_file_dir
import pyomo.core.expr.current as EXPR
from pyomo.core.base import SymbolMap

from pyomo.environ import (
ConcreteModel, Var, Param, Constraint, Objective, Block, sin,
maximize, Binary, Suffix
)
from pyomo.repn.plugins.baron_writer import expression_to_string

thisdir = this_file_dir()

Expand Down Expand Up @@ -216,6 +219,28 @@ def test_invalid_suffix(self):
m.write(StringIO(), format='bar')


class TestToBaronVisitor(unittest.TestCase):
def test_pow(self):
variables = OrderedSet()
smap = SymbolMap()

m = ConcreteModel()
m.x = Var(initialize=1)
m.y = Var(initialize=2)
m.p = Param(mutable=True, initialize=0)

e = m.x ** m.y
test = expression_to_string(e, variables, smap)
self.assertEqual(test, "exp((x) * log(y))")

e = m.x ** (3 + EXPR.ProductExpression((m.p, m.y)))
test = expression_to_string(e, variables, smap)
self.assertEqual(test, "x ^ 3")

e = (3 + EXPR.ProductExpression((m.p, m.y))) ** m.x
test = expression_to_string(e, variables, smap)
self.assertEqual(test, "3 ^ x")

#class TestBaron_writer(unittest.TestCase):
class XTestBaron_writer(object):
"""These tests verified that the BARON writer complained loudly for
Expand Down