- scipy - plotly - matplotlib

Innovative Section - Network Logistic

Introduction Image
Transport problem. Watch Tutorial
import numpy as np from scipy.optimize import linprog from js import window from pyodide import create_proxy def format_data(matrix, supply, demand, bottom_left_data=''): h = [''] for i in range(len(demand)): h.append(f'Store{i+1}') h.append('supply') index = [] for j in range(len(supply)): index.append(f'Factory{j+1}') index.append('demand') result = np.column_stack((matrix, supply)) demand = np.append(demand, bottom_left_data) result = np.vstack((result, demand)) result = np.column_stack((np.array(index), result)) return h, result.tolist() def get_cost_and_total_result(cost_matrix, supply, demand): # create matrix num_factories = len(supply) num_stores = len(demand) # constraints A_supply = [] for i in range(num_factories): row = [0] * (num_factories * num_stores) for j in range(num_stores): row[i * num_stores + j] = 1 A_supply.append(row) A_supply = np.array(A_supply) b_supply = supply A_demand = [] for j in range(num_stores): row = [0] * (num_factories * num_stores) for i in range(num_factories): row[i * num_stores + j] = 1 A_demand.append(row) A_demand = np.array(A_demand) b_demand = demand A = np.vstack((A_supply, -A_demand)) b = np.hstack((b_supply, -b_demand)) c = cost_matrix.flatten() # solveing using optimization libbrary in pyscript result = linprog(c, A_ub=A, b_ub=b, method='highs') if result.x is not None: x = result.x.reshape((num_factories, num_stores)) # output result here print("Optimal solution:") print(x) print("Optimal value (total cost):", result.fun) return x, result.fun else: return None def on_instant(event): try: n = int(Element("rows").element.value) m = int(Element("cols").element.value) comma_separated_list = Element("result").element.innerHTML.split(',') comma_separated_list[-1] = '0' values = list(map(int, comma_separated_list)) matrix = np.array(values).reshape((n+1, m+1)) cost_matrix = matrix[:n, :m] supply = matrix[:-1, -1].reshape(-1) demand = matrix[-1, :-1].reshape(-1) print(cost_matrix, supply, demand) result = get_cost_and_total_result(cost_matrix, supply, demand) if result: x, total_cost = result h, d = format_data(x, supply, demand, f'Total Cost:{total_cost}') Element("h2").element.style.display = "block" window.setTable(str(h), str(d), "out2") Element("out_instant").element.innerHTML = "Calculate successfully." else: Element("out_instant").element.innerHTML = "Calculate error." except ValueError as e: print(e) Element("out_instant").element.innerHTML = "Invalid input. Please enter numeric values." def init_data(event): n = int(Element("rows").element.value) m = int(Element("cols").element.value) comma_separated_list = Element("result").element.innerHTML.split(',') comma_separated_list[-1] = '0' values = list(map(int, comma_separated_list)) matrix = np.array(values).reshape((n+1, m+1)) cost_matrix = matrix[:n, :m] supply = matrix[:-1, -1].reshape(-1) demand = matrix[-1, :-1].reshape(-1) h, d = format_data(cost_matrix, supply, demand, f'{np.sum(demand)}/{np.sum(supply)}') Element("h1").element.style.display = "block" window.setTable(str(h), str(d), "out1") Element("show_data_tabel").element.onclick = init_data Element("button_instant").element.onclick = on_instant
INPUT
OUTPUT