Skip to content

Handling of float('nan') especially in GAMSWriter #3911

@Bhosecmu

Description

@Bhosecmu

Summary

TLDR: Variable values are converted from float('nan') to nan. However, nan doesn't mean anything in GAMS. It should be converted to NA instead.

Steps to reproduce the issue

Here is a minimal example (can be run with GAMS demo license)

import pyomo.environ as pyo

# Create a simple model
model = pyo.ConcreteModel()

# Define a variable and explicitly set it to NaN
model.x = pyo.Var(initialize=float('nan'))

# Add a dummy objective or constraint so the solver has something to do
model.obj = pyo.Objective(expr=model.x**2, sense=pyo.minimize)
model.c = pyo.Constraint(expr=model.x >= 1)

# Set up the GAMS solver link
# Note: Ensure 'gams' is in your PATH or specify the executable path
solver = pyo.SolverFactory('gams')
results = solver.solve(model, tee=True , keepfiles=True, tmpdir="tmp")

Pyomo produces a warning but the error comes from GAMS (that the symbol nan is undefined):

WARNING (W1001): Setting Var 'x' to a value `nan` (float) not in domain Reals.

Information on your system

Pyomo version: 6.10.1
Python version: 3.13.4
Operating system: Windows
How Pyomo was installed (PyPI, conda, source): PyPI
Solver (if applicable): GAMS

Additional information

At first, I don't think this is a bigger issue for Pyomo beyond just the GAMS solver. A couple of conflicting arguments come to my mind:

  • If the variable initialized to float('nan') is eventually not part of the model after presolve, this might be OK and this could be why Pyomo chooses to throw a warning for this instead of an error.
  • GAMS is in general stricter and always throws an error but I got the same error by directly calling IPOPT from Pyomo. I wonder if there are any cases where the solver automatically switches float('nan') to a default initial value without complaining. If there are no such solvers, it could be better to catch this early and throw an error.

A potential fix: instead of using ftoa() for gams_writer.py, use a wrapper gams_ftoa() that uses ftoa and switches nan to NA

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions