I am trying to understand Pyomo constraint rules. The last line of the following code…
Private research annotation
Sign in to add a private research label or note.
- User
I am trying to understand Pyomo constraint rules. The last line of the following code yields an error: m = ConcreteModel()
classes=gb_courses.groups instructors= df_TT_instructors_FALL['V_ID_NAME_CONCAT']
Define the decision variables
m.class_assignments = Var(classes, instructors, domain=Binary)
Each class must have AT MOST ONE instructor
def has_at_most_one_instructor(m, c): return sum(m.class_assignments[c, i] for i in instructors) <= 1 m.one_instructor_constraint = Constraint(classes, rule=has_at_most_one_instructor)
- Assistant
The error you're encountering is likely due to the way you're defining the
classesandinstructorsvariables. In Pyomo, when you create aConstraintorVarobject with an index set, the index set should be a PyomoSetobject or a Python iterable (like a list or a tuple). In your case,classesandinstructorsare likely pandasDataFrameGroupByandSeriesobjects, respectively, which are not directly compatible with Pyomo.To fix this, you can convert
classesandinstructorsto lists before using them in theVarandConstraintdefinitions. Here's the updated code:python
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.