Ejemplo n.º 1
0
    def run(trucks, cities):
        tiempo_inicial_t2 = time()
        print(
            "--------------------------------------------------------- Random Solution --------------------------------------------------------- \n"
        )

        def zerolistmaker(n):
            listofzeros = [0] * n
            return listofzeros

        globTotal = math.inf
        globSolution = math.inf
        matrix = Generator.generateHA(trucks, cities)
        rows = list(range(0, trucks))
        for i in range(40000):
            clients = list(range(0, cities - 1))
            columns = random.choices(clients, k=trucks)
            zipped = zip(rows, columns)
            zipped = list(zipped)
            total = 0
            solution = []
            for (row, column) in zipped:
                value = matrix[row][column]
                total += value
                solution.append(
                    str('truck ' + str(row) + '---> customer ' + str(column)))
            if total < globTotal:
                globTotal = total
                globSolution = solution
        print("Solution:", globSolution)
        print("Total allocation cost:", globTotal)
        tiempo_final_t2 = time()
        total_time = tiempo_final_t2 - tiempo_inicial_t2
        print("Total time: ", total_time, " secs.\n")
        return globTotal, total_time
Ejemplo n.º 2
0
 def hungarian(trucks, cities):
     print(
         "--------------------------------------------------------- Hungarian Algorithm --------------------------------------------------------- \n"
     )
     m = Munkres()
     matrix = Generator.generateHA(trucks, cities)
     tiempo_inicial_t2 = time()
     indexes = m.compute(matrix)
     solution = []
     total = 0
     for row, column in indexes:
         value = matrix[row][column]
         total += value
         solution.append(
             str('truck ' + str(row) + '---> customer ' + str(column)))
     print("Solution:", solution)
     print("Total allocation cost:", total)
     tiempo_final_t2 = time()
     total_time = tiempo_final_t2 - tiempo_inicial_t2
     print("Total time: ", total_time, " secs.\n")
     return total, total_time