猿问

如何最小化与给定输入分布的距离?

我有一个客户列表,每个客户都可以通过四种不同的方式“激活”:


n= 1000

df = 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)

每个客户都可以在“A”或“B”或“C”上激活,并且仅当与激活类型相关的布尔值等于 1 时。


在输入中,我有最终激活的计数。es:


Target_A = 500

Target_B = 250

Target_C = 250

代码中的随机值是优化器的输入,代表以这种方式激活客户端的可能性。我如何才能仅将客户与其中之一联系起来以尊重最终目标?如何最小化实际激活计数与输入数据之间的距离?


qq_遁去的一_1
浏览 148回答 1
1回答

慕尼黑的夜晚无繁华

你有任何经过测试的例子吗?我认为这可能有效但不确定: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):&nbsp; &nbsp; prob += A[idx] + B[idx] + C[idx] <= 1 #cant activate more than 1&nbsp; &nbsp; prob += A[idx] <= df['A'][idx] #cant activate if 0&nbsp; &nbsp; prob += B[idx] <= df['B'][idx]&nbsp;&nbsp; &nbsp; prob += C[idx] <= df['C'][idx]&nbsp;prob.solve()&nbsp; &nbsp;&nbsp;print("difference:", prob.objective.value())
随时随地看视频慕课网APP

相关分类

Python
我要回答