Open
Description
Summary
I've created a model using pyomo, and when I try to generate LaTeX, I get a KeyError
. The traceback can be found below.
Steps to reproduce the issue
Run the python code below.
# example.py
import pandas as pd
# Read the CSV file into a Pandas DataFrame
data = {
'Size': {0: 'Medium', 1: 'Medium', 2: 'Large', 3: 'Medium', 4: 'Large', 5: 'Medium'},
'Material': {0: 'Glass', 1: 'Plastic', 2: 'Plastic', 3: 'Plastic', 4: 'Glass', 5: 'Plastic'},
'Mass': {0: 2507, 1: 1127, 2: 2032, 3: 1150, 4: 7599, 5: 1109},
'TargetMass': {0: 0, 1: 0, 2: 1, 3: 0, 4: 0, 5: 0},
'Mass2k': {0: 1, 1: 0, 2: 1, 3: 0, 4: 1, 5: 0},
'NormPrice': {0: 1.0, 1: 0.93, 2: 0.61, 3: 0.67, 4: 0.95, 5: 0.4},
'Price': {0: 100, 1: 93, 2: 61, 3: 67, 4: 95, 5: 40},
'NormRating': {0: 1.0, 1: 0.6, 2: 0.6, 3: 0.6, 4: 0.6, 5: 0.6},
'Pop_1': {0: 5, 1: 3, 2: 3, 3: 3, 4: 3, 5: 3}
}
df = pd.DataFrame(data)
import pyomo.environ as pyo
# Create a Pyomo model
m = pyo.ConcreteModel()
# Create a binary decision variable for each item in the filtered dataframe
m.in_bag = pyo.Var([item for item in df.index], domain=pyo.Binary, initialize=0)
# Assisted by WCA@IBM
m.bag_size = pyo.Expression(expr=pyo.summation(m.in_bag))
# Constraints
m.cons = pyo.ConstraintList()
# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
m.cons.add(m.bag_size >= 4)
m.cons.add(m.bag_size <= 6)
# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
# Objective function 1: Total of the normalized price of the included items
m.obj1 = pyo.Objective(expr=sum(m.in_bag[item] * df.loc[item, "NormPrice"] for item in df.index))
# Objective function 2: Average normalized rating of the included items
m.obj2 = pyo.Objective(expr=sum(m.in_bag[item] * df.loc[item, "NormRating"] for item in df.index))
# Objective function 3: Average of the TargetMass plus the average of the Mass2k of the selected items
m.obj3 = pyo.Objective(expr=(sum(m.in_bag[item] * (df.loc[item, "TargetMass"] + df.loc[item, "Mass2k"]) for item in df.index)))
# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
# Create the objective function
m.obj = pyo.Objective(expr=0.5*m.obj1 - 0.3*m.obj2 + 0.2*m.obj3)
# Assisted by WCA@IBM
# Latest GenAI contribution: ibm/granite-20b-code-instruct-v2
del m.obj1, m.obj2, m.obj3
from pyomo.contrib.latex_printer import latex_printer
print(latex_printer(m))
Error Message
Traceback (most recent call last):
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/contrib/latex_printer/latex_printer.py", line 435, in exitNode
return self._operator_handles[node.__class__](self, node, *data)
KeyError: <class 'numpy.int64'>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/harold/Documents/workspace/watsonx-challenge-2024/problem3.py", line 123, in <module>
print(latex_printer(m))
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/contrib/latex_printer/latex_printer.py", line 853, in latex_printer
visitor.walk_expression(obj_template),
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 276, in walk_expression
result = self._process_node(root, RECURSION_LIMIT)
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 359, in _process_node_general
child_result = self._process_node(child, recursion_limit)
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 359, in _process_node_general
child_result = self._process_node(child, recursion_limit)
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 359, in _process_node_general
child_result = self._process_node(child, recursion_limit)
[Previous line repeated 2 more times]
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 379, in _process_node_general
return self.exitNode(node, data)
File "/Users/harold/miniforge3/envs/watsonx-challenge-2024/lib/python3.10/site-packages/pyomo/contrib/latex_printer/latex_printer.py", line 437, in exitNode
raise DeveloperError(
pyomo.common.errors.DeveloperError: Internal Pyomo implementation error:
"Latex printer encountered an error when processing type <class
'numpy.int64'>, contact the developers"
Please report this to the Pyomo Developers.
Information on your system
Pyomo version: 6.7.3
Python version: 3.10.14
Operating system: MacOS Sonoma 14.5 (intel)
How Pyomo was installed (PyPI, conda, source): pip install
Solver (if applicable): N/A
Additional information
Forced numpy version 1.26.4.
Output of pip install
:
Package Version
--------------- -----------
numpy 1.26.4
pandas 2.2.2
pip 24.0
ply 3.11
Pyomo 6.7.3
python-dateutil 2.9.0.post0
pytz 2024.1
setuptools 70.1.1
six 1.16.0
tzdata 2024.1
wheel 0.43.0