I have a number of variables each assigned an integer value. I need to split these variables in three groups with a predefined number of variables going into each group while optimizing towards predefined sums of the values in each group. Each group sum should be as close as possible to the predefined value, but can be above or below. All variables should be used and each variable can only be used once.
For example, I might have 10 variables...
...and the goal could be to create three groups:
|Group||#Variables||Sum optimized towards|
So group X should hold 6 variables and their sums should be as close as possible to 200 - but I need to optimize for each of the groups simultanously.
I've tried to set up
PuLP to perform this task. I seem to have found a solution for creating a single group, but I cannot figure out how to split the variables into groups and optimize the assignments based on the sums for each group. Is there a way to do this?
Below is my code for producing the first group with the presented variables.
from pulp import LpMaximize, LpMinimize, LpProblem, lpSum, LpVariable, PULP_CBC_CMD, value, LpStatus keys = ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"] data = [98,20,30,50,20,34,43,21,32,54] problem_name = 'repex' prob = LpProblem(problem_name, LpMaximize) optiSum = 200 # Optimize towards this sum variableCount = 6 # Number of variables that should be in the group # Create decision variables decision_variables =  for i,n in enumerate(data): variable = i variable = LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Binary') decision_variables.append(variable) # Add constraints sumConstraint = "" # Constraint on sum of data elements for i, n in enumerate(decision_variables): formula = data[i]*n sumConstraint += formula countConstraint = "" # Constrain on number of elements used for i, n in enumerate(decision_variables): formula = n countConstraint += formula prob += (sumConstraint <= optiSum) prob += (countConstraint == variableCount) prob += sumConstraint # Solve optimization_result = prob.solve(PULP_CBC_CMD(msg=0)) prob.writeLP(problem_name + ".lp" ) print("Status:", LpStatus[prob.status]) print("Optimal Solution to the problem: ", value(prob.objective)) print ("Individual decision_variables: ") for v in prob.variables(): print(v.name, "=", v.varValue)
Which produces the following output:
Status: Optimal Optimal Solution to the problem: 200.0 Individual decision_variables: 0 = 0.0 1 = 1.0 2 = 0.0 3 = 1.0 4 = 0.0 5 = 1.0 6 = 1.0 7 = 1.0 8 = 1.0 9 = 0.0