慕尼黑的夜晚无繁华
你有任何经过测试的例子吗?我认为这可能有效但不确定:import pandas as pdimport numpy as npfrom pulp import LpProblem, LpVariable, LpMinimize, LpInteger, lpSum, valueprob = LpProblem("problem", LpMinimize)n= 1000df = pd.DataFrame(list(range(0,n)), columns = ['Customer_ID'])df['A'] = np.random.randint(2, size=n)df['B'] = np.random.randint(2, size=n)df['C'] = np.random.randint(2, size=n)Target_A = 500Target_B = 250Target_C = 250A = LpVariable.dicts("A", range(0, n), lowBound=0, upBound=1, cat='Boolean')B = LpVariable.dicts("B", range(0, n), lowBound=0, upBound=1, cat='Boolean')C = LpVariable.dicts("C", range(0, n), lowBound=0, upBound=1, cat='Boolean')O1 = LpVariable("O1", cat='Integer')O2 = LpVariable("O2", cat='Integer')O3 = LpVariable("O3", cat='Integer')#objectiveprob += O1 + O2 + O3#constraintsprob += O1 >= Target_A - lpSum(A)prob += O1 >= lpSum(A) - Target_Aprob += O2 >= Target_B - lpSum(B)prob += O2 >= lpSum(B) - Target_Bprob += O3 >= Target_C - lpSum(C)prob += O3 >= lpSum(C) - Target_Cfor idx in range(0, n): prob += A[idx] + B[idx] + C[idx] <= 1 #cant activate more than 1 prob += A[idx] <= df['A'][idx] #cant activate if 0 prob += B[idx] <= df['B'][idx] prob += C[idx] <= df['C'][idx] prob.solve() print("difference:", prob.objective.value())