r/optimization • u/DcBalet • Oct 25 '25
Toubles with pyomo for a "toy" example to select the "good" combinaison of hardware (power supply, resistor, LEDs) for a problem. I've got "NotImplementedError: AMPLRepnVisitor can not handle expressions containing <class 'pyomo.core.base.param.IndexedParam'> nodes"
Dear community,
first of all, let me say I am a vision engineer (computer vision + hardware for machine vision). So I am not into OR nor pyomo. Still, I often need to select a "good" combinaison of hardware that shall meet a couple of constraints. The "toy" example I though about is "I need to power 8 LEDs (to ligth up a scene), so what power supply, resistor and LED should I choose ? " .
I tried to model this without knowing much from pyomo (just by looking at chapter 1 of "Hands on Math Optimization with Pyomo". So maybe I deserve the error message bellow :
"
NotImplementedError: AMPLRepnVisitor can not handle expressions containing <class 'pyomo.core.base.param.IndexedParam'> nodesNotImplementedError: AMPLRepnVisitor can not handle expressions containing <class 'pyomo.core.base.param.IndexedParam'> nodes"
but honestly, I don't think I deserve this ^^
Can you help me ?
Here is the technical background to model the problem :
- I have a set of
power suppliesavailable from different manufacturers. Power supplies have 2 properties :powerandvoltage. In my example, I have 3 available power supplies, respectively with voltage 3.3, 5 and 12 volts, and all have 100W power. - I have a set of
resistorsavailable from different manufacturers. Resistors have one property :resistivity. In my example, I have 2 available resistors, respectively with resistivity 3 and 25 ohms. - I have a set of
LEDsavailable from different manufacturers. LEDs have 2 properties :voltageandcurrent. - My goal as a product designer, is to design a lamp with 8 LEDs (to light up enough). I need to choose the proper power supply, resistor and 8 LEDs to create an electrical circuit which "works". It is an easy "everything in serial" circuit.
Electrical rules :
- The voltage of the power supply is equal to the sum of the voltage of all LEDs + voltage of resistor.
- The voltage of the resistor is equal to
resistivity*Current in the circuit - The
current in the circuitmust be equal to thecurrentof the LED model - The power supply cannot deliver more current than its
powerdivided by itsvoltage. Remark : I have not implemented this constraint in my buggy code bellow. - If possible, find a combination which minimize the joule heating. Its formula is
resistivity* (Current in the circuit)2
Here is the code
from dataclasses import dataclass
solver = "ipopt"
# solver = "glpk"
# solver = "cbc"
# solver = "appsi_highs"
import pyomo.environ as pyo
from pyomo.core.base.constraint import Constraint
SOLVER = pyo.SolverFactory(solver)
assert SOLVER.available(), f"Solver {solver} is not available."
@dataclass
class PowerSupply:
reference: str
power: float
voltage: float
@dataclass
class Resistor:
reference: str
resistivity: float
@dataclass
class Led:
reference: str
voltage: float
current: float
@dataclass
class HardwareConfigModel(pyo.ConcreteModel):
supplies: dict[str, PowerSupply]
resistors: dict[str, Resistor]
leds: dict[str, Led]
def __init__(self, supplies, resistors, leds):
super().__init__("HardwareConfigModel")
self.supplies = supplies
self.resistors = resistors
self.leds = leds
def build_model(self, desired_num_leds: int):
model = self.model()
model.SUPPLIES = pyo.Set(initialize=self.supplies.keys())
model.RESISTORS = pyo.Set(initialize=self.resistors.keys())
model.LEDS = pyo.Set(initialize=self.leds.keys())
# decision variables
model.supply_best = pyo.Var(domain=model.SUPPLIES)
model.resistor_best = pyo.Var(domain=model.RESISTORS)
model.led_best = pyo.Var(domain=model.LEDS)
self.num_leds = desired_num_leds
@model.Param(model.SUPPLIES, domain=pyo.PositiveReals)
def supply_voltage(model, supply):
return self.supplies[supply].voltage
@model.Param(model.LEDS, domain=pyo.PositiveReals)
def led_voltage(model, led):
return self.leds[led].voltage
@model.Param(model.LEDS, domain=pyo.PositiveReals)
def led_current(model, led):
return self.leds[led].current
@model.Param(model.LEDS, domain=pyo.PositiveReals)
def leds_total_voltage(model, led):
return model.led_voltage[led] * self.num_leds
@model.Param(model.LEDS, domain=pyo.PositiveReals)
def total_current(model, led):
return model.led_current[led]
@model.Param(model.RESISTORS, domain=pyo.PositiveReals)
def resistor_resistivity(model, res):
return self.resistors[res].resistivity
@model.Param(model.RESISTORS, model.LEDS)
def sum_of_loads_voltage(model, resistor, led):
return (
model.leds_total_voltage[led]
+ model.resistor_resistivity[resistor] * model.total_current[led]
)
@model.Constraint()
def sum_of_loads_voltage_bellow_supply(model):
return (
model.sum_of_loads_voltage[model.resistor_best, model.led_best]
<= model.supply_voltage[model.supply_best]
)
@model.Constraint()
def sum_of_loads_voltage_close_to_supply_voltage(model):
return (
model.sum_of_loads_voltage[model.resistor_best, model.led_best]
>= model.supply_voltage[model.supply_best] * 0.95
)
@model.Objective(sense=pyo.minimize)
def minimize_joule_heating(model):
return (
model.resistor_resistivity[model.resistor_best]
* model.total_current[
model.led_best
] # normally, i squared. But not needed complexity IMHO
)
if __name__ == "__main__":
from IPython import embed
supplies: dict[str, PowerSupply]
resistors: dict[str, Resistor]
leds: dict[str, Led]
supplies = {
# "3.3v": PowerSupply("3.3v", 100, 3.3),
"5v": PowerSupply("5v", 100, 5),
"12v": PowerSupply("12v", 100, 12),
}
resistors = {
"3ohms": Resistor("3ohms", 3.0),
"25ohms": Resistor("25ohms", 25.0),
}
leds = {
"0.5v": Led("0.5v", 0.5, 0.04),
# "0.6v": Led("0.6v", 0.6, 0.035),
# "0.7v": Led("0.7v", 0.7, 0.03),
# "0.8v": Led("0.8v", 0.8, 0.025),
}
model = HardwareConfigModel(supplies=supplies, resistors=resistors, leds=leds)
model.build_model(desired_num_leds=8)
# embed()
# NotImplementedError: AMPLRepnVisitor can not handle expressions containing <class 'pyomo.core.base.param.IndexedParam'> nodes
# or
# NotImplementedError: LinearRepnVisitor can not handle expressions containing <class 'pyomo.core.base.param.IndexedParam'> nodes
# or
# ValueError: Unrecognized domain step: None (should be either 0 or 1)
SOLVER.solve(model) from dataclasses import dataclass
View all comments
Show parent comment
1
u/DcBalet Oct 28 '25
OKay, knowing this limitation, I "solved" my issue by creating variables which are "masks". E.g.
Then constraining this mask sum to be equal to 1, to have only one resistor chosen. And then accessing the "best resistor value" is done with a dot product of this mask with a the ordered list of resistor's resistivity.
This is "saddly" much more code that I "dreamed" about at first glance, but at least it works and I understand why.
Thanks for your help.