Untitled
import json import random import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression # Wczytaj dane wejściowe with open("simulation_input.json", "r") as file: data = json.load(file) # Parametry wejściowe mass_tonnes = data["mass_tonnes"] fuel_consumption_factor = data["fuel_consumption_factor"] cost_per_tonne_usd = data["cost_per_tonne_usd"] isru_enabled = data["isru_enabled"] # Funkcja celu: minimalizacja kosztów def calculate_cost(mass, fuel_consumption_factor, cost_per_tonne_usd, isru_enabled): fuel_cost = mass * fuel_consumption_factor * cost_per_tonne_usd if isru_enabled: fuel_cost *= 0.8 # ISRU zmniejsza koszt o 20% return fuel_cost # Algorytm genetyczny def genetic_algorithm(mass_options, population_size=10, generations=20, mutation_rate=0.1): population = [{"mass": random.choice(mass_options)} for _ in range(population_size)] for _ in range(generations): fitness_scores = [calculate_cost(ind["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru_enabled) for ind in population] sorted_population = [x for _, x in sorted(zip(fitness_scores, population))] population = sorted_population[:population_size // 2] offspring = [] for _ in range(len(population) // 2): parent1 = random.choice(population) parent2 = random.choice(population) child = {"mass": (parent1["mass"] + parent2["mass"]) // 2} offspring.append(child) for ind in offspring: if random.random() < mutation_rate: ind["mass"] = random.choice(mass_options) population += offspring best_solution = min(population, key=lambda x: calculate_cost(x["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru_enabled)) return best_solution # Algorytm PSO def pso_algorithm(mass_options, iterations=20, swarm_size=10): swarm = [{"mass": random.choice(mass_options), "velocity": 0} for _ in range(swarm_size)] global_best = min(swarm, key=lambda x: calculate_cost(x["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru_enabled)) for _ in range(iterations): for particle in swarm: cost = calculate_cost(particle["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru_enabled) if cost < calculate_cost(global_best["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru_enabled): global_best = particle particle["velocity"] = 0.5 * particle["velocity"] + random.random() * (global_best["mass"] - particle["mass"]) particle["mass"] += int(particle["velocity"]) particle["mass"] = max(min(particle["mass"], max(mass_options)), min(mass_options)) return global_best # Algorytm ACO def aco_algorithm(mass_options, iterations=20): pheromones = {mass: 1.0 for mass in mass_options} best_solution = None best_cost = float("inf") for _ in range(iterations): for mass in mass_options: cost = calculate_cost(mass, fuel_consumption_factor, cost_per_tonne_usd, isru_enabled) pheromones[mass] += 1 / cost if cost < best_cost: best_cost = cost best_solution = {"mass": mass} return best_solution # Uczenie maszynowe (ML) def ml_prediction(mass_tonnes, fuel_consumption_factor, cost_per_tonne_usd): X = np.array(mass_tonnes).reshape(-1, 1) y = np.array([calculate_cost(mass, fuel_consumption_factor, cost_per_tonne_usd, isru_enabled) for mass in mass_tonnes]) model = LinearRegression() model.fit(X, y) future_mass = np.array([60, 70, 80]).reshape(-1, 1) predictions = model.predict(future_mass) return future_mass.flatten(), predictions # Symulacja results = [] for isru in [True, False]: for algorithm in ["genetic", "pso", "aco"]: if algorithm == "genetic": best_result = genetic_algorithm(mass_tonnes) elif algorithm == "pso": best_result = pso_algorithm(mass_tonnes) elif algorithm == "aco": best_result = aco_algorithm(mass_tonnes) cost = calculate_cost(best_result["mass"], fuel_consumption_factor, cost_per_tonne_usd, isru) results.append({ "algorithm": algorithm, "isru": isru, "mass": best_result["mass"], "cost": cost }) # Wyniki ML future_mass, predictions = ml_prediction(mass_tonnes, fuel_consumption_factor, cost_per_tonne_usd) # Wykres 1: Porównanie kosztów algorytmów plt.figure(figsize=(10, 6)) for alg in ["genetic", "pso", "aco"]: costs = [r["cost"] for r in results if r["algorithm"] == alg] labels = [f"ISRU: {r['isru']}" for r in results if r["algorithm"] == alg] plt.plot(labels, costs, label=alg.upper()) plt.title("Porównanie kosztów algorytmów") plt.xlabel("Scenariusz") plt.ylabel("Koszt") plt.legend() plt.grid() plt.show() # Wykres 2: Prognoza ML plt.figure(figsize=(10, 6)) plt.plot(future_mass, predictions, marker='o', label="Prognoza ML") plt.title("Prognoza kosztów w zależności od masy") plt.xlabel("Masa (t)") plt.ylabel("Koszt") plt.legend() plt.grid() plt.show() # Wykres 3: Koszty dla każdego algorytmu for alg in ["genetic", "pso", "aco"]: masses = [r["mass"] for r in results if r["algorithm"] == alg] costs = [r["cost"] for r in results if r["algorithm"] == alg] plt.figure(figsize=(10, 6)) plt.bar(masses, costs) plt.title(f"Koszty dla algorytmu {alg.upper()}") plt.xlabel("Masa (t)") plt.ylabel("Koszt") plt.grid() plt.show()
Leave a Comment