Constraint violating solution output
AnsweredI am attempting to create a model to minimize the number of groupings for part numbers, based on their assigned clusters, which have overlaps between the clusters. I defined the part numbers and their clusters as fixed inputs, and then binary decision variables as the part number assigned to a particular group and cluster assigned to a particular group. I have the following constraints:
- Each part is assigned to exactly one group.
- The number of unique clusters assigned to a group does not exceed 70.
I don't know if I need to include anything relating to if a part is assigned to a group, then the associated clusters should also be assigned to the group. I'm unsure if that would already be implied based on the input parameter data.
Here is my attempt at the code:
mod = Model("part_grouping")
#create a dictionary where each key is a part # and each value is a set of clusters for that part #
part_clusters = data.groupby('Part #')['Cluster'].apply(set).to_dict()
#create a list of part #s
parts = list(part_clusters.keys())
#create a list of clusters
clusters = list(set(data['Cluster']))
#create a list of groups as a starting point
groups = clusters.copy()
#decision vars
Y = mod.addVars(parts, groups, vtype=GRB.BINARY, name="Y")
Z = mod.addVars(clusters, groups, vtype=GRB.BINARY, name="Z")
#objective function
mod.setObjective(Y.sum(), GRB.MINIMIZE) #minimize the number of total groups
#constraints
for i in parts:
mod.addConstr(Y.sum(i, '*') == 1, "part_assigned[{}]".format(i)) #each part assigned to only 1 group
for k in groups:
mod.addConstr(Z.sum('*', k) <= 70, "max_clusters[{}]".format(k)) #less than or equal to 70 clusters per group
mod.optimize()
When this runs though, the solution creates two groups, one with 3 clusters and the other with well over 1000 clusters, which violates the max_clusters constraint. What am I missing or why does the solution violate one of the constraints?
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Ryan,
As the first step to debugging the behaviour you are observing, please write your model in the LP file format using model.write("test.lp") and check if the max_clusters constraints are constructed as per your expectation in the model.
Best regards,
Simran0
Post is closed for comments.
Comments
2 comments