Advertisement
adwas33

Checkpoint 01.04

Apr 1st, 2023
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. import tkinter as tk
  2. import numpy as np
  3. from scipy.optimize import linprog
  4.  
  5.  
  6. class App(tk.Tk):
  7.     def __init__(self):
  8.         super().__init__()
  9.         self.title("Zagadnienie pośrednika")
  10.  
  11.         tk.Label(self, text="Podaż (rozdelone przecinkami):").grid(row=0, column=0)
  12.         self.supply_entry = tk.Entry(self)
  13.         self.supply_entry.grid(row=0, column=1)
  14.  
  15.         tk.Label(self, text="Popyt (rozdelone przecinkami):").grid(row=1, column=0)
  16.         self.demand_entry = tk.Entry(self)
  17.         self.demand_entry.grid(row=1, column=1)
  18.  
  19.         tk.Label(self, text="Ceny zakupu (rozdelone przecinkami):").grid(row=2, column=0)
  20.         self.purchase_prices_entry = tk.Entry(self)
  21.         self.purchase_prices_entry.grid(row=2, column=1)
  22.  
  23.         tk.Label(self, text="Ceny sprzedaży (rozdelone przecinkami):").grid(row=3, column=0)
  24.         self.sale_prices_entry = tk.Entry(self)
  25.         self.sale_prices_entry.grid(row=3, column=1)
  26.  
  27.         tk.Label(self, text="Koszty transportu (rozdelone przecinkami):").grid(row=4, column=0)
  28.         self.transport_costs_entry = tk.Entry(self)
  29.         self.transport_costs_entry.grid(row=4, column=1)
  30.  
  31.         self.calculate_button = tk.Button(self, text="Oblicz", command=self.calculate)
  32.         self.calculate_button.grid(row=5, column=1, pady=10)
  33.  
  34.     def display_results(self, results_text):
  35.         results_window = tk.Toplevel(self)
  36.         results_window.title("Wyniki")
  37.         results_label = tk.Label(results_window, text=results_text, justify=tk.LEFT)
  38.         results_label.pack(padx=20, pady=20)
  39.  
  40.     def calculate(self):
  41.         supply = np.array(list(map(float, self.supply_entry.get().split(','))))
  42.         demand = np.array(list(map(float, self.demand_entry.get().split(','))))
  43.         purchase_prices = np.array(list(map(float, self.purchase_prices_entry.get().split(','))))
  44.         sale_prices = np.array(list(map(float, self.sale_prices_entry.get().split(','))))
  45.         transport_costs = np.array(list(map(float, self.transport_costs_entry.get().split(','))))
  46.  
  47.         num_suppliers = len(supply)
  48.         num_consumers = len(demand)
  49.  
  50.         transport_costs = transport_costs.reshape(num_suppliers, num_consumers)
  51.         profits = sale_prices.reshape(1, -1) - purchase_prices.reshape(-1, 1) - transport_costs
  52.  
  53.         total_supply = np.sum(supply)
  54.         total_demand = np.sum(demand)
  55.  
  56.         if total_supply < total_demand:
  57.             artificial_supply = np.zeros((1, num_consumers))
  58.             transport_costs = np.vstack([transport_costs, artificial_supply])
  59.             supply = np.append(supply, total_demand - total_supply)
  60.             num_suppliers += 1
  61.         elif total_demand < total_supply:
  62.             artificial_demand = np.zeros((num_suppliers, 1))
  63.             transport_costs = np.hstack([transport_costs, artificial_demand])
  64.             demand = np.append(demand, total_supply - total_demand)
  65.             num_consumers += 1
  66.  
  67.         costs = profits.flatten()
  68.         supply_constraints = np.hstack([np.eye(num_suppliers) for _ in range(num_consumers)])
  69.         demand_constraints = np.hstack([np.eye(num_consumers) for _ in range(num_suppliers)])
  70.  
  71.         A_eq = np.vstack([supply_constraints, demand_constraints])
  72.         b_eq = np.concatenate([supply, demand])
  73.  
  74.         try:
  75.             res = linprog(-costs, A_eq=A_eq, b_eq=b_eq, method='highs')
  76.         except Exception as e:
  77.             self.display_results(f"Błąd podczas optymalizacji: {e}")
  78.             return
  79.  
  80.         if res.success:
  81.             optimal_transfers = np.round(res.x.reshape(num_suppliers, num_consumers))
  82.             if total_supply < total_demand:
  83.                 optimal_transfers = optimal_transfers[:-1, :]
  84.             elif total_demand < total_supply:
  85.                 optimal_transfers = optimal_transfers[:, :-1]
  86.  
  87.             total_cost = np.sum(optimal_transfers * transport_costs)
  88.             total_revenue = np.sum(optimal_transfers * (sale_prices - purchase_prices.reshape(-1, 1)))
  89.             total_profit = np.sum(optimal_transfers * profits)
  90.  
  91.             results_text = (
  92.                 "Tabela zysków jednostkowych:\n" + str(profits) +
  93.                 "\n\nTabela optymalnych przewozów:\n" + str(optimal_transfers) +
  94.                 f"\n\nKoszt całkowity: {total_cost:.2f}" +
  95.                 f"\nPrzychód całkowity: {total_revenue:.2f}" +
  96.                 f"\nZysk pośrednika: {total_profit:.2f}"
  97.             )
  98.             self.display_results(results_text)
  99.         else:
  100.             self.display_results(f"Błąd podczas optymalizacji: {res.message}")
  101.  
  102.  
  103. if __name__ == "__main__":
  104.     app = App()
  105.     app.mainloop()
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement