PID Optimization Using GA Omar Ibrahim
PID Optimization Using GA Omar Ibrahim
x1, x2 = y
dx1 = x2
dt = time[1] - time[0]
error_prev = 0
integral = 0
response = []
y = y0
for t in time:
return np.array(response)
def fitness_function(params):
# Performance metrics
best_solutions = []
fitness_history = []
for generation in range(n_generations):
best_solutions.append(population[np.argmin(fitness)])
# Tournament selection
def tournament_selection():
return population[indices[np.argmin(fitness[indices])]]
o`spring = []
# Mutation
o`spring = np.array(o`spring)
o`spring += mutations
population = o`spring
plt.figure(figsize=(8, 5))
plt.xlabel("Generation")
plt.legend()
plt.show()
# Simulate responses
plt.figure(figsize=(12, 8))
# Ziegler-Nichols response
plt.subplot(2, 1, 1)
plt.xlabel("Time (s)")
plt.ylabel("Altitude")
plt.legend()
# GA-optimized response
plt.subplot(2, 1, 2)
plt.xlabel("Time (s)")
plt.ylabel("Altitude")
plt.legend()
plt.tight_layout()
plt.savefig("Missile_Control_Comparison_Enhanced.png")
plt.show()
# Print results