pyomo icon indicating copy to clipboard operation
pyomo copied to clipboard

Resolve (change in) IPOPT solver/NL writer exception type for problems with invalid constants

Open shermanjasonaf opened this issue 1 year ago • 0 comments

Summary

As discussed in #2964, the NL writer is expected to raise an InvalidValueError upon encountering an invalid constant. However, as of #3353, this may not necessarily happen in some cases.

Steps to reproduce the issue

Consider the following setup:

>>> import pyomo.environ as pyo
>>> m = pyo.ConcreteModel()
>>> m.v = pyo.Var(initialize=0, bounds=(0, 1))
>>> m.w = pyo.Var(initialize=0, bounds=(0, 1))
>>> m.w.fix(0)
>>> m.obj = pyo.Objective(expr=m.v - pyo.log(m.w))
>>> pyo.SolverFactory("ipopt").solve(m, tee=True)

Notice the objective (effectively) has a log(0) term, which should eventually trigger an exception.

Running with Pyomo/main@29f8ede05e, the commit immediately preceding #3353, gives:

File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/base/solvers.py", line 598, in solve
    self._presolve(*args, **kwds)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/solver/shellcmd.py", line 223, in _pr
esolve
    OptSolver._presolve(self, *args, **kwds)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/base/solvers.py", line 704, in _preso
lve
    self._convert_problem(
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/base/solvers.py", line 756, in _conve
rt_problem
    return convert_problem(
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/base/convert.py", line 97, in convert
_problem
    problem_files, symbol_map = converter.apply(*tmp, **tmpkw)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/solvers/plugins/converter/model.py", line
 184, in apply
    (problem_filename, symbol_map_id) = instance.write(
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/core/base/block.py", line 1916, in write
    (filename, smap) = problem_writer(self, filename, solver_capability, io_options)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/plugins/nl_writer.py", line 350, in 
__call__
    info = self.write(model, FILE, ROWFILE, COLFILE, config=config)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/plugins/nl_writer.py", line 408, in 
write
    return impl.write(model)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/plugins/nl_writer.py", line 1457, in
 write
    self._write_nl_expression(info[1], True)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/plugins/nl_writer.py", line 2025, in
 _write_nl_expression
    self.ostream.write(self.template.const % repn.const)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/util.py", line 189, in __repr__
    self._error(f'Cannot emit {str(self)} in compiled representation')
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/repn/util.py", line 179, in _error
    raise InvalidValueError(msg)
pyomo.common.errors.InvalidValueError: Cannot emit InvalidNumber(nan) in compiled representation
The InvalidNumber was generated by:
        math domain error

However, running with Pyomo/main@a6438bad0 (from #3353) yields:

WARNING: Exception encountered evaluating expression 'log(0)' message: math
domain error expression: log(w)
bad line 12 of /tmp/tmp1xgm4rp0.pyomo.nl: nInvalidNumber(nan)
terminate called after throwing an instance of 'Ipopt::TNLP::INVALID_TNLP'
Ipopt 3.13.2: ERROR: Solver (ipopt) returned non-zero return code (-6)
ERROR: See the solver log above for diagnostic information.
Traceback (most recent call last):
  File "/home/jasherma/Documents/vim_example/pyomo_features_examples/test_ipopt_presolve_issue.py", line 21, in <
module>
    pyo.SolverFactory("ipopt").solve(m, tee=True)
  File "/home/jasherma/Documents/cmu/phd-project/pyomo_repo/pyomo/pyomo/opt/base/solvers.py", line 628, in solve
    raise ApplicationError("Solver (%s) did not exit normally" % self.name)
pyomo.common.errors.ApplicationError: Solver (ipopt) did not exit normally

Information on your system

Pyomo version: 6.7.4dev0 Numpy version: 2.0.2 Python version: 3.9.17 Operating system: Ubuntu 20.04 How Pyomo was installed (PyPI, conda, source): source Solver (if applicable): IPOPT

shermanjasonaf avatar Oct 21 '24 03:10 shermanjasonaf