首页 文章

Gurobi / Jump / Julia中的多维数组

提问于
浏览
0

我正在使用Jump / Julia来解决优化问题 . 这是一个运输问题,有一些源位置和一些目的地 . 另外,我有不同的产品类型,因此从源i到目的地j发送一种产品与其他类型的产品不同 . 通常当产品是同质的时候,我可以用这种方式编写成本矩阵

tr =[0 2.82 4.24 5.83 4.12 0;
2.82 0 1.41 3.16 2.23 2.82;
4.24 1.41 0 2 2.23 4.24;
5.83 3.16 2 0 2.23 5.83;
4.12 2.23 2.23 2.23 0 4.12;
0 2.82 4.24 5.83 4.12 0]

它只是意味着将一种产品从i发送到j的成本 . 当运动成本也取决于产品类型时,我试图找到一种修改成本矩阵的方法 . 示例c [1,2,3]表示将产品类型1从源2移动到目标3的成本 . 谢谢 .

2 回答

  • 0

    我可能误解了这个问题,但我认为你可以按照以下方式做到(假设有三种产品为了说明):

    tr_product_1 = [0 2.82 4.24 5.83 4.12 0;
    2.82 0 1.41 3.16 2.23 2.82;
    4.24 1.41 0 2 2.23 4.24;
    5.83 3.16 2 0 2.23 5.83;
    4.12 2.23 2.23 2.23 0 4.12;
    0 2.82 4.24 5.83 4.12 0]
    
    cost_matrix = zeros(6,6,3)
    cost_matrix[:,:,1] = tr_product_1
    

    然后为其他产品(例如 tr_product_2tr_product_3 )编写二维源 - 目标矩阵并重复该过程 . 为了便于说明,我刚刚使用了乘数:

    tr_product_2 = 1.2 * tr_product_1
    tr_product_3 = 1.5 * tr_product_1
    
    cost_matrix[:,:,2] = tr_product_2
    cost_matrix[:,:,3] = tr_product_3
    
    cost_matrix
    
    6×6×3 Array{Float64,3}:
    [:, :, 1] =
     0.0   2.82  4.24  5.83  4.12  0.0 
     2.82  0.0   1.41  3.16  2.23  2.82
     4.24  1.41  0.0   2.0   2.23  4.24
     5.83  3.16  2.0   0.0   2.23  5.83
     4.12  2.23  2.23  2.23  0.0   4.12
     0.0   2.82  4.24  5.83  4.12  0.0 
    
    [:, :, 2] =
     0.0    3.384  5.088  6.996  4.944  0.0  
     3.384  0.0    1.692  3.792  2.676  3.384
     5.088  1.692  0.0    2.4    2.676  5.088
     6.996  3.792  2.4    0.0    2.676  6.996
     4.944  2.676  2.676  2.676  0.0    4.944
     0.0    3.384  5.088  6.996  4.944  0.0  
    
    [:, :, 3] =
     0.0    4.23   6.36   8.745  6.18   0.0  
     4.23   0.0    2.115  4.74   3.345  4.23 
     6.36   2.115  0.0    3.0    3.345  6.36 
     8.745  4.74   3.0    0.0    3.345  8.745
     6.18   3.345  3.345  3.345  0.0    6.18 
     0.0    4.23   6.36   8.745  6.18   0.0
    

    在这种情况下,三维矩阵的形式为 [source, destination, product] ,但我认为这与Julia的解决方式相比更好 .

  • 0

    我更喜欢使用字典来处理多维问题,因为我可以跟踪维度名称 .

    对于使用字典的非常类似的问题(规范传输问题),请参阅https://lobianco.org/antonello/personal:blog:2017:0203_jump_for_gams_users

    基本上你定义了向量中每个维度的允许元素集(例如 plants = ["seattle","san_diego"]; markets = ["new_york","chicago","topeka"] ),然后你可以在JuMP中使用这样的东西:

    @constraints trmodel begin
        supply[p in plants],   # observe supply limit at plant p
            sum(x[p,m] for m in markets)  <=  a[p]
        demand[m in markets],  # satisfy demand at market m
            sum(x[p,m] for p in plants)  >=  b[m]
    

    链接的示例仍然使用同构成本,但使用字典对于异构成本进行扩展非常简单 .

相关问题