def solve(self, show_iter=False): supply = self.table[1:-1, -1] demand = self.table[-1, 1:-1] n = len(supply) m = len(demand) #compute Rij and Rji Rij, Rji = np.zeros((2, n, m), dtype=object) for i, s in enumerate(supply): for j, d in enumerate(demand): Rij[i, j] = d / s Rji[i, j] = s / d #solve for WCD and WCS min_cost = np.inf for R, title in zip([Rij, Rji], ["WCD", "WCS"]): if show_iter: print("{} SOLUSTION\n".format(title)) #make a copy of table then multiply with Rij/Rji (WCD/WCS) cost = self.table[1:-1, 1:-1] * R supply = self.table[1:-1, -1] demand = self.table[-1, 1:-1] trans = Transportation(cost, supply, demand) trans.setup_table() ks = KaragulSahin(trans) alloc = ks.solve_part(show_iter=show_iter) total_cost = self.find_cost(alloc, self.table) if show_iter: print("{} TOTAL COST = {}\n".format(title, total_cost)) #save allocation if it has minimum cost if total_cost < min_cost: min_cost = total_cost self.alloc = alloc[:] return np.array(self.alloc, dtype=object)
if __name__ == "__main__": #example 1 balance problem cost = np.array([[11, 13, 17, 14], [16, 18, 14, 10], [21, 24, 13, 10]]) supply = np.array([250, 300, 400]) demand = np.array([200, 225, 275, 250]) #example 2 unbalance problem cost = np.array([[2, 7, 14], [3, 3, 1], [5, 4, 7], [1, 6, 2]]) supply = np.array([5, 8, 7, 15]) demand = np.array([7, 9, 18]) #initialize transportation problem trans = Transportation(cost, supply, demand) #setup transportation table. #minimize=True for minimization problem, change to False for maximization, default=True. #ignore this if problem is minimization and already balance trans.setup_table(minimize=True) #initialize ASM method with table that has been prepared before. ASM = AssigningShortestMinimax(trans) #solve problem and return allocation lists which consist n of (Ri, Cj, v) #Ri and Cj is table index where cost is allocated and v it's allocated value. #(R0, C1, 3) means 3 cost is allocated at Row 0 and Column 1. #show_iter=True will showing table changes per iteration, default=False. #revision=True will using ASM Revision algorithm for unbalance problem, default=False. allocation = ASM.solve(show_iter=False, revision=False)