Search code examples
pythondatabasealgorithmcomputer-science

Allocation algorithm using Python


I am trying to create a model in which manufacturers can post a load which needs to be shipped and a transporter can post that his truck is going from point A to point B. If the Origin, Destination and load(to be transported and truck capacity) matches then both of them are notified like a tinder match.

I have tried researching on automatic matching but the closest I come to is the Hungarian Algorithm that solves the assignment problem, but I am not so sure if it's the right direction or not.

In the model I have already created input forms for both sections i.e. manufacturers and transporters and the data is saved in a Database. I am thinking of applying a trigger function which rechecks for a best match everytime a new entry comes in Database

Here is the Data from both input forms:

Manufacturer

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

Transporter

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

We can see that Transporter 4595 is the best match for Manufacturer 1025 and Transporter 6569 is the second best. I want to match both of them and also show the manufacturer that he has another option too.


Solution

  • This problem could be seen as a directed graph, where an edge from a vertex A to another vertex B represents the truck going from A to B (or the manufacturer wanting the load to get transported from A to B), and weight of the edge could be used to represent the amount of load (or truck capacity).

    You could use an adjacency matrix each for both manufacturer and transporter. Each time a new entry is filled into either of the matrix (let's say manufacturer's matrix), the corresponding entry is checked is checked in the other matrix (transporter's matrix), and the loads are also compared to see if there is a match.

    ```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
    #
    ```
    


    I have written only some functions here. I've written only the function which checks when a new entry is created for a manufacturer, but not vice-versa. You could add additional constraints like date, time etc.