【问题标题】:Allocation algorithm using Python使用 Python 的分配算法
【发布时间】:2019-03-24 03:50:58
【问题描述】:

我正在尝试创建一个模型,在该模型中,制造商可以发布需要运输的货物,运输商可以发布他的卡车从 A 点开往 B 点的信息。如果原产地、目的地和货物(要运输和卡车容量)匹配,然后它们都会像火种匹配一样被通知。

我曾尝试研究自动匹配,但最接近的是解决分配问题的匈牙利算法,但我不确定它是否是正确的方向。

在模型中,我已经为制造商和运输商这两个部分创建了输入表单,并且数据保存在数据库中。我正在考虑应用一个触发器函数,该函数每次在数据库中出现新条目时都会重新检查最佳匹配

这是来自两个输入表单的数据:

制造商

M_ID From To M_Type    T_Type  T_Length T_Weight #Trucks Loading_Time
1025 A    B  Boxes     Open    12-Tyre  22       3       27-March-2019 6:00PM
1029 C    D  Cylinders Trailer HIGH     23       2       28-March-2019 6:00PM
1989 G    H  Scrap     Open    14-Tyre  25       5       26-March-2019 9:00PM

运输工具

T_ID From To T_Type  T_Length T_Weight #Trucks  Price
6569 A    B  Open    12-Tyre  22       5        1500
8658 G    H  Open    14-Tyre  25       10       1200
4595 A    B  Open    12-Tyre  22       3        1000
1252 A    B  Trailer Low      28       5        1800

我们可以看到,Transporter 4595 是制造商 1025 的最佳匹配,而 Transporter 6569 是第二好的。我想同时匹配它们,同时向制造商表明他也有另一种选择。

【问题讨论】:

  • 为什么这被标记为python?它看起来更像是一个数据库或 CS 问题。

标签: python database algorithm computer-science


【解决方案1】:

这个问题可以看作是一个有向图,其中从顶点A 到另一个顶点B 的边表示卡车从AB(或者制造商希望货物被运输从AB),边缘的重量可用于表示负载量(或卡车容量)。

您可以为制造商和运输商分别使用邻接矩阵。每次将新条目填充到任一矩阵(假设是制造商的矩阵)中时,都会在另一个矩阵(运输商的矩阵)中检查相应的条目,并且还会比较负载以查看是否匹配。

```python
class trans_struct(T_ID, T_Type, T_Length, Trucks, Price):
    def __init__(self, T_ID, T_Type, T_Length, Trucks, Price) 
    self.T_ID = T_ID
    self.T_Length = T_Length
    self.T_Trucks = T_Trucks
    self.T_Type = T_Type
    self.T_Price = T_Price

class man_struct(M_ID, M_Type, T_Length, Trucks, Loading_Time):
    def __init__(self, M_ID, M_Type, T_Length, Trucks, Loading_Time)
    self.M_ID = M_ID
    self.T_Length = T_Length
    self.T_Trucks = T_Trucks
    self.T_Type = T_Type
    self.T_Price = T_Price

dicti = {A:0, B:1, C:2} #dictionary to map places to integeral indexes
num_places = len(dicti)    
trans_mat = [[[] for __ in range(num_places)] for _ in range(num_places)]  #initialize transport matrix to a default value
manf_mat = [[[] for __ in range(num_places)] for _ in range(num_places)]


def manf_input():
    #take input for manufacturer's data in this func into the structure object
    manf_mat[dicti[A]][dicti[B]].append((struct.T_Weight, struct))  #assuming manufacturer wanted to move goods from A to B
    check_for_match(A, B)     #function to compare corresponding matrix entries each time a new entry is inserted

def check_for_match(A, B, T_Length):
    for entry in trans_mat[dicti[A]][dicti[B]]:
        if  entry[0]>= T_Length:
            #match found display required info by retreiving from the structure
#
```


我在这里只写了一些函数。我只编写了检查何时为制造商创建新条目的函数,反之亦然。您可以添加额外的约束,如日期、时间等。

【讨论】:

  • 嗨 Virmis_007,我部分明白了你想说的意思,但你能举个例子解释一下吗?或者如果我将模拟数据放在问题中会有帮助吗?
  • @RahulSharma 是的,我可以尝试用虚拟数据进行更多解释。
  • 我做了一些更改,请您再过一遍问题,看看矩阵是否有效?
猜你喜欢
  • 1970-01-01
  • 2014-07-01
  • 2021-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-29
  • 2019-05-24
相关资源
最近更新 更多