Skip to content

Commit

Permalink
Merge pull request #3332 from jsiirola/nlv2_threading_fix
Browse files Browse the repository at this point in the history
Resolve NLv2 incompatibility with multithreading
  • Loading branch information
jsiirola authored Aug 19, 2024
2 parents cb7d880 + 6661d82 commit ccc96bf
Show file tree
Hide file tree
Showing 8 changed files with 1,352 additions and 1,286 deletions.
5 changes: 2 additions & 3 deletions pyomo/contrib/incidence_analysis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import enum
from pyomo.common.config import ConfigDict, ConfigValue, InEnum
from pyomo.common.modeling import NOTSET
from pyomo.repn.plugins.nl_writer import AMPLRepnVisitor, text_nl_template
from pyomo.repn.ampl import AMPLRepnVisitor
from pyomo.repn.util import FileDeterminism, FileDeterminism_to_SortComponents


Expand All @@ -33,7 +33,7 @@ class IncidenceMethod(enum.Enum):
"""

ampl_repn = 3
"""Use ``pyomo.repn.plugins.nl_writer.AMPLRepnVisitor``"""
"""Use ``pyomo.repn.ampl.AMPLRepnVisitor``"""


class IncidenceOrder(enum.Enum):
Expand Down Expand Up @@ -140,7 +140,6 @@ def get_config_from_kwds(**kwds):
export_defined_variables = False
sorter = FileDeterminism_to_SortComponents(FileDeterminism.ORDERED)
amplvisitor = AMPLRepnVisitor(
text_nl_template,
subexpression_cache,
external_functions,
var_map,
Expand Down
8 changes: 1 addition & 7 deletions pyomo/contrib/incidence_analysis/incidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pyomo.core.expr.numvalue import value as pyo_value
from pyomo.repn import generate_standard_repn
from pyomo.util.subsystems import TemporarySubsystemManager
from pyomo.repn.plugins.nl_writer import AMPLRepn
from pyomo.contrib.incidence_analysis.config import (
IncidenceMethod,
get_config_from_kwds,
Expand Down Expand Up @@ -95,12 +94,7 @@ def _nonlinear_var_id_collector(idlist):
yield _id

var_map = visitor.var_map
orig_activevisitor = AMPLRepn.ActiveVisitor
AMPLRepn.ActiveVisitor = visitor
try:
repn = visitor.walk_expression((expr, None, 0, 1.0))
finally:
AMPLRepn.ActiveVisitor = orig_activevisitor
repn = visitor.walk_expression((expr, None, 0, 1.0))

nonlinear_var_id_set = set()
unique_nonlinear_var_ids = []
Expand Down
8 changes: 5 additions & 3 deletions pyomo/contrib/pyros/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
from pyomo.core.expr import value
from pyomo.core.expr.numeric_expr import NPV_MaxExpression, NPV_MinExpression
from pyomo.repn.standard_repn import generate_standard_repn
from pyomo.repn.plugins import nl_writer as pyomo_nl_writer
import pyomo.repn.plugins.nl_writer as pyomo_nl_writer
import pyomo.repn.ampl as pyomo_ampl_repn
from pyomo.core.expr.visitor import (
identify_variables,
identify_mutable_parameters,
Expand Down Expand Up @@ -1824,8 +1825,9 @@ def call_solver(model, solver, config, timing_obj, timer_name, err_msg):
# e.g., a Var fixed outside bounds beyond the Pyomo NL writer
# tolerance, but still within the default IPOPT feasibility
# tolerance
current_nl_writer_tol = pyomo_nl_writer.TOL
current_nl_writer_tol = pyomo_nl_writer.TOL, pyomo_ampl_repn.TOL
pyomo_nl_writer.TOL = 1e-4
pyomo_ampl_repn.TOL = 1e-4

try:
results = solver.solve(
Expand All @@ -1845,7 +1847,7 @@ def call_solver(model, solver, config, timing_obj, timer_name, err_msg):
results.solver, TIC_TOC_SOLVE_TIME_ATTR, tt_timer.toc(msg=None, delta=True)
)
finally:
pyomo_nl_writer.TOL = current_nl_writer_tol
pyomo_nl_writer.TOL, pyomo_ampl_repn.TOL = current_nl_writer_tol

timing_obj.stop_timer(timer_name)
revert_solver_max_time_adjustment(
Expand Down
Loading

0 comments on commit ccc96bf

Please sign in to comment.