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

时间:2019-11-05 18:42:35

标签: python optimization linear-programming pyomo mixed-integer-programming

我正在一个项目中,我需要根据百分比值数组细分整数值。 我的最终数组必须包含整数值,并且数组的总和必须等于初始整数。

下面是一个伪造的例子。我们列出了带有某些“潜力”的汽车,我们需要将此潜力分配给特定的邮政编码。邮政编码分配是由一些售罄信息决定的。

SELLOUTS_PER_P_CODE规定了每个邮政编码分配的权重。例如,对于第一辆车(car 1),p_code_3的权重很大,p_code_2的权重更小,p_code_1的权重更小,因此应该分别分配用于汽车1 p_code_1=1p_code_2=2p_code_3=4

贝勒是问题的数学形式。

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

1 个答案:

答案 0 :(得分:7)

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