0

I am using Jump/Julia to solve an optimization problem. It is a transportation problem with some source locations and some destinations. Additionally, I have different product types so sending one kind of product from source i to destination j is not same for other kind of product. Usually when the products are homogeneous, I can write a cost matrix in this way

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]

It simply means cost of sending one product from i to j. I am trying to find a way to modify the cost matrix when cost of the movement also depends on the type of product. Example c[1,2,3] would mean cost of moving product type 1 from source 2 to destination 3. Thank you.

bhawesh sah
  • 103
  • 1
  • 10

2 Answers2

0

I may have misunderstood the question, but I think you can do it as follows (assuming 3 products for the sake of illustration):

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

Then write 2-dimensional source-destination matrices for the other products (e.g. tr_product_2 and tr_product_3) and repeat the process. For the sake of illustration I've just used a multiplier:

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

In this case the 3-dimensional matrix is in the form [source, destination, product], but I think this works better with the way Julia lays it out.

Mark Birtwistle
  • 362
  • 2
  • 10
0

I prefer to use dictionaries for multidimensional problems, as I can keep track of the dimension names.

For a very similar problem (the canonical transport problem) using dictionaries see https://lobianco.org/antonello/personal:blog:2017:0203_jump_for_gams_users

Basically you define the set of allowed elements for each dimension in vectors (e.g. plants = ["seattle","san_diego"]; markets = ["new_york","chicago","topeka"]) and then you can use something like this in 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]

The linked example still use homogeneous costs, but using dictionaries it is very trivial to extend it for heterogeneous costs.

Antonello
  • 6,092
  • 3
  • 31
  • 56