Skip to content

Commit

Permalink
Merge pull request Pyomo#3037 from jsiirola/nl-repn-scaling-factor
Browse files Browse the repository at this point in the history
NLv2: add linear presolve and general problem scaling support
  • Loading branch information
michaelbynum authored Nov 15, 2023
2 parents 71787b7 + 7ac1a56 commit 15a7bff
Show file tree
Hide file tree
Showing 7 changed files with 1,335 additions and 368 deletions.
7 changes: 7 additions & 0 deletions pyomo/common/collections/component_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def __len__(self):
# Overload MutableMapping default implementations
#

# We want a specialization of update() to avoid unnecessary calls to
# id() when copying / merging ComponentMaps
def update(self, *args, **kwargs):
if len(args) == 1 and not kwargs and isinstance(args[0], ComponentMap):
return self._dict.update(args[0]._dict)
return super().update(*args, **kwargs)

# We want to avoid generating Pyomo expressions due to
# comparison of values, so we convert both objects to a
# plain dictionary mapping key->(type(val), id(val)) and
Expand Down
2 changes: 1 addition & 1 deletion pyomo/core/base/suffix.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ def __init__(self, name, default=None):
"""
self.name = name
self.default = default
self._suffixes_by_block = ComponentMap({None: []})
self.all_suffixes = []
self._suffixes_by_block = {None: []}

def find(self, component_data):
"""Find suffix value for a given component data object in model tree
Expand Down
20 changes: 10 additions & 10 deletions pyomo/repn/linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ def _before_var(visitor, child):
_id = id(child)
if _id not in visitor.var_map:
if child.fixed:
return False, (_CONSTANT, visitor.handle_constant(child.value, child))
return False, (_CONSTANT, visitor.check_constant(child.value, child))
visitor.var_map[_id] = child
visitor.var_order[_id] = len(visitor.var_order)
ans = visitor.Result()
Expand All @@ -603,7 +603,7 @@ def _before_monomial(visitor, child):
arg1, arg2 = child._args_
if arg1.__class__ not in native_types:
try:
arg1 = visitor.handle_constant(visitor.evaluate(arg1), arg1)
arg1 = visitor.check_constant(visitor.evaluate(arg1), arg1)
except (ValueError, ArithmeticError):
return True, None

Expand All @@ -616,15 +616,15 @@ def _before_monomial(visitor, child):
if arg2.fixed:
return False, (
_CONSTANT,
arg1 * visitor.handle_constant(arg2.value, arg2),
arg1 * visitor.check_constant(arg2.value, arg2),
)
visitor.var_map[_id] = arg2
visitor.var_order[_id] = len(visitor.var_order)

# Trap multiplication by 0 and nan.
if not arg1:
if arg2.fixed:
arg2 = visitor.handle_constant(arg2.value, arg2)
arg2 = visitor.check_constant(arg2.value, arg2)
if arg2 != arg2:
deprecation_warning(
f"Encountered {arg1}*{str(arg2.value)} in expression "
Expand Down Expand Up @@ -652,14 +652,14 @@ def _before_linear(visitor, child):
arg1, arg2 = arg._args_
if arg1.__class__ not in native_types:
try:
arg1 = visitor.handle_constant(visitor.evaluate(arg1), arg1)
arg1 = visitor.check_constant(visitor.evaluate(arg1), arg1)
except (ValueError, ArithmeticError):
return True, None

# Trap multiplication by 0 and nan.
if not arg1:
if arg2.fixed:
arg2 = visitor.handle_constant(arg2.value, arg2)
arg2 = visitor.check_constant(arg2.value, arg2)
if arg2 != arg2:
deprecation_warning(
f"Encountered {arg1}*{str(arg2.value)} in expression "
Expand All @@ -673,7 +673,7 @@ def _before_linear(visitor, child):
_id = id(arg2)
if _id not in var_map:
if arg2.fixed:
const += arg1 * visitor.handle_constant(arg2.value, arg2)
const += arg1 * visitor.check_constant(arg2.value, arg2)
continue
var_map[_id] = arg2
var_order[_id] = next_i
Expand All @@ -687,7 +687,7 @@ def _before_linear(visitor, child):
const += arg
else:
try:
const += visitor.handle_constant(visitor.evaluate(arg), arg)
const += visitor.check_constant(visitor.evaluate(arg), arg)
except (ValueError, ArithmeticError):
return True, None
if linear:
Expand All @@ -713,7 +713,7 @@ def _before_external(visitor, child):
ans = visitor.Result()
if all(is_fixed(arg) for arg in child.args):
try:
ans.constant = visitor.handle_constant(visitor.evaluate(child), child)
ans.constant = visitor.check_constant(visitor.evaluate(child), child)
return False, (_CONSTANT, ans)
except:
pass
Expand Down Expand Up @@ -752,7 +752,7 @@ def __init__(self, subexpression_cache, var_map, var_order):
self._eval_expr_visitor = _EvaluationVisitor(True)
self.evaluate = self._eval_expr_visitor.dfs_postorder_stack

def handle_constant(self, ans, obj):
def check_constant(self, ans, obj):
if ans.__class__ not in native_numeric_types:
# None can be returned from uninitialized Var/Param objects
if ans is None:
Expand Down
Loading

0 comments on commit 15a7bff

Please sign in to comment.