gpt4 book ai didi

python - 将整数值分解为保持总和的整数数组

转载 作者:行者123 更新时间:2023-12-03 15:58:38 27 4
gpt4 key购买 nike

我正在做一个项目,我需要根据百分比值数组分解一个整数值。
我的结束数组必须包含整数值,数组的总和必须等于初始整数。

下面是一个假的例子。我们有一个具有一些“潜力”的汽车列表,我们需要将这种潜力分配给特定的邮政编码。邮政编码分配由一些售罄信息决定。
SELLOUTS_PER_P_CODE决定了每个邮政编码分配的权重。例如,对于第一辆车( car 1 ),p_code_3 的权重很大。和更少的 p_code_2 p_code_1 甚至更少所以分配应该分别为car 1 p_code_1=1 , p_code_2=2 , p_code_3=4 .

Bellow 是问题的数学形式。

enter image description here

在这里,我使用 pyomo 实现了这个公式,但是它没有产生预期的结果。该模型未考虑来自 SELLOUTS_PER_P_CODE 的权重因子

from pyomo.environ import *
from pprint import pprint


def distribute(total, weights):
scale = float(sum(weights.values())) / total
return {k: v / scale for k, v in weights.items()}


Cars = ["car 1", "car 2", "car 3"]
Locations = ["p_code_1", "p_code_2", "p_code_3"]
POTENTIALS = {"car 1": 7, "car 2": 2, "car 3": 14}
SELLOUTS = {"p_code_1": 0.2, "p_code_2": 0.3, "p_code_3": 0.5}

SELLOUTS_PER_P_CODE = {}

for car in Cars:
pot = POTENTIALS[car]
scaled_sellout = distribute(pot, SELLOUTS)
t = {(car, p_code): v for p_code, v in scaled_sellout.items()}
SELLOUTS_PER_P_CODE.update(t)

pprint(SELLOUTS_PER_P_CODE)

model = ConcreteModel(name="Breakdown Potential to Postal Code")

model.Cars = Set(initialize=Cars)
model.Locations = Set(initialize=Locations)

model.a = Param(model.Cars, model.Locations, initialize=SELLOUTS_PER_P_CODE)
model.p = Param(model.Cars, initialize=POTENTIALS)

model.X_pos = Var(model.Cars, model.Locations, within=NonNegativeIntegers)
model.X_neg = Var(model.Cars, model.Locations, within=NonNegativeIntegers)


def objective_rule(model):
return sum(
(model.X_pos[i, j] - model.a[i, j] * model.p[i])
- (model.X_neg[i, j] - model.a[i, j] * model.p[i])
for i in model.Cars
for j in model.Locations
)


model.objective = Objective(rule=objective_rule, sense=minimize)


def sum_maintained_rule(model, i):
return (
sum(model.X_pos[i, j] for j in model.Locations)
+ sum(model.X_neg[i, j] for j in model.Locations)
== model.p[i]
)


model.sum_maintained = Constraint(model.Cars, rule=sum_maintained_rule)


def pyomo_postprocess(options=None, instance=None, results=None):
model.pprint()


if __name__ == "__main__":
opt = SolverFactory("glpk")
results = opt.solve(model)
results.write()
print("\nDisplaying Solution\n" + "-" * 80)
pyomo_postprocess(None, model, results)

最后这里是不正确的输出。通知 X_negX_pos用于输出分配。
Displaying Solution
--------------------------------------------------------------------------------
5 Set Declarations
Cars : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=None
['car 1', 'car 2', 'car 3']
Locations : Dim=0, Dimen=1, Size=3, Domain=None, Ordered=False, Bounds=None
['p_code_1', 'p_code_2', 'p_code_3']
X_neg_index : Dim=0, Dimen=2, Size=9, Domain=None, Ordered=False, Bounds=None
Virtual
X_pos_index : Dim=0, Dimen=2, Size=9, Domain=None, Ordered=False, Bounds=None
Virtual
a_index : Dim=0, Dimen=2, Size=9, Domain=None, Ordered=False, Bounds=None
Virtual

2 Param Declarations
a : Size=9, Index=a_index, Domain=Any, Default=None, Mutable=False
Key : Value
('car 1', 'p_code_1') : 1.4000000000000001
('car 1', 'p_code_2') : 2.1
('car 1', 'p_code_3') : 3.5
('car 2', 'p_code_1') : 0.4
('car 2', 'p_code_2') : 0.6
('car 2', 'p_code_3') : 1.0
('car 3', 'p_code_1') : 2.8000000000000003
('car 3', 'p_code_2') : 4.2
('car 3', 'p_code_3') : 7.0
p : Size=3, Index=Cars, Domain=Any, Default=None, Mutable=False
Key : Value
car 1 : 7
car 2 : 2
car 3 : 14

2 Var Declarations
X_neg : Size=9, Index=X_neg_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
('car 1', 'p_code_1') : 0 : 7.0 : None : False : False : NonNegativeIntegers
('car 1', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 1', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_1') : 0 : 2.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_1') : 0 : 14.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers
X_pos : Size=9, Index=X_pos_index
Key : Lower : Value : Upper : Fixed : Stale : Domain
('car 1', 'p_code_1') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 1', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 1', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_1') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 2', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_1') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_2') : 0 : 0.0 : None : False : False : NonNegativeIntegers
('car 3', 'p_code_3') : 0 : 0.0 : None : False : False : NonNegativeIntegers

1 Objective Declarations
objective : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : X_pos[car 1,p_code_1] - 9.8 - (X_neg[car 1,p_code_1] - 9.8) + X_pos[car 1,p_code_2] - 14.700000000000001 - (X_neg[car 1,p_code_2] - 14.700000000000001) + X_pos[car 1,p_code_3] - 24.5 - (X_neg[car 1,p_code_3] - 24.5) + X_pos[car 2,p_code_1] - 0.8 - (X_neg[car 2,p_code_1] - 0.8) + X_pos[car 2,p_code_2] - 1.2 - (X_neg[car 2,p_code_2] - 1.2) + X_pos[car 2,p_code_3] - 2.0 - (X_neg[car 2,p_code_3] - 2.0) + X_pos[car 3,p_code_1] - 39.2 - (X_neg[car 3,p_code_1] - 39.2) + X_pos[car 3,p_code_2] - 58.800000000000004 - (X_neg[car 3,p_code_2] - 58.800000000000004) + X_pos[car 3,p_code_3] - 98.0 - (X_neg[car 3,p_code_3] - 98.0)

1 Constraint Declarations
sum_maintained : Size=3, Index=Cars, Active=True
Key : Lower : Body : Upper : Active
car 1 : 7.0 : X_pos[car 1,p_code_1] + X_pos[car 1,p_code_2] + X_pos[car 1,p_code_3] + X_neg[car 1,p_code_1] + X_neg[car 1,p_code_2] + X_neg[car 1,p_code_3] : 7.0 : True
car 2 : 2.0 : X_pos[car 2,p_code_1] + X_pos[car 2,p_code_2] + X_pos[car 2,p_code_3] + X_neg[car 2,p_code_1] + X_neg[car 2,p_code_2] + X_neg[car 2,p_code_3] : 2.0 : True
car 3 : 14.0 : X_pos[car 3,p_code_1] + X_pos[car 3,p_code_2] + X_pos[car 3,p_code_3] + X_neg[car 3,p_code_1] + X_neg[car 3,p_code_2] + X_neg[car 3,p_code_3] : 14.0 : True

11 Declarations: Cars Locations a_index a p X_pos_index X_pos X_neg_index X_neg objective sum_maintained

最佳答案

从您发布的问题来看,参数“a”应使用“位置”而不是“汽车”和“位置”进行初始化。除此之外,其他一切看起来都不错。

关于python - 将整数值分解为保持总和的整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58717585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com