-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathac_controller.py
More file actions
152 lines (120 loc) · 5.54 KB
/
ac_controller.py
File metadata and controls
152 lines (120 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
import matplotlib.pyplot as plt
TempError = ctrl.Antecedent(np.arange(-5, 6, 1), 'TempError')
Humidity = ctrl.Antecedent(np.arange(0, 101, 1), 'Humidity')
CompressorSpeed = ctrl.Consequent(np.arange(0, 101, 1), 'CompressorSpeed')
TempError['Cold'] = fuzz.trimf(TempError.universe, [-5, -5, 0])
TempError['Good'] = fuzz.trimf(TempError.universe, [-1, 0, 1])
TempError['Hot'] = fuzz.trimf(TempError.universe, [0, 5, 5])
Humidity['Low'] = fuzz.trimf(Humidity.universe, [0, 0, 50])
Humidity['High'] = fuzz.trimf(Humidity.universe, [50, 100, 100])
CompressorSpeed['Off'] = fuzz.trimf(CompressorSpeed.universe, [0, 0, 10])
CompressorSpeed['Slow'] = fuzz.trimf(CompressorSpeed.universe, [10, 30, 50])
CompressorSpeed['Medium'] = fuzz.trimf(CompressorSpeed.universe, [40, 60, 80])
CompressorSpeed['Fast'] = fuzz.trimf(CompressorSpeed.universe, [70, 100, 100])
rule1 = ctrl.Rule(TempError['Cold'], CompressorSpeed['Off'])
rule2 = ctrl.Rule(TempError['Good'], CompressorSpeed['Slow'])
rule3 = ctrl.Rule(TempError['Hot'] & Humidity['Low'], CompressorSpeed['Medium'])
rule4 = ctrl.Rule(TempError['Hot'] & Humidity['High'], CompressorSpeed['Fast'])
ac_ctrl = ctrl.ControlSystem([rule1, rule2, rule3, rule4])
ac_sim = ctrl.ControlSystemSimulation(ac_ctrl)
# ...existing code...
if __name__ == "__main__":
print("\n--- Evaluating Controller ---")
ac_sim.input['TempError'] = 3
ac_sim.input['Humidity'] = 30
ac_sim.compute()
print(f"Case 1 (Hot, Low Humidity): Compressor Speed = {ac_sim.output['CompressorSpeed']:.2f}%")
ac_sim.input['TempError'] = 3
ac_sim.input['Humidity'] = 80
ac_sim.compute()
print(f"Case 2 (Hot, High Humidity): Compressor Speed = {ac_sim.output['CompressorSpeed']:.2f}%")
ac_sim.input['TempError'] = -2
ac_sim.input['Humidity'] = 50
ac_sim.compute()
print(f"Case 3 (Too Cold): Compressor Speed = {ac_sim.output['CompressorSpeed']:.2f}%")
fig, axs = plt.subplots(2, 2, figsize=(10, 7))
TempError.view(ax=axs[0, 0])
axs[0, 0].set_title('Temperature Error Membership Functions')
Humidity.view(ax=axs[0, 1])
axs[0, 1].set_title('Humidity Membership Functions')
# show compressor membership functions and mark last computed output
CompressorSpeed.view(ax=axs[1, 0])
axs[1, 0].set_title('Compressor Speed Membership Functions')
# mark the last computed crisp output on the membership plot (Case 3)
try:
axs[1, 0].axvline(ac_sim.output['CompressorSpeed'], color='r', linestyle='--', label='Output')
axs[1, 0].legend()
except Exception:
pass
# hide unused subplot
axs[1, 1].axis('off')
plt.tight_layout()
plt.show()
"""
import random
import numpy as np
import matplotlib.pyplot as plt
from deap import base, creator, tools, algorithms
def total_distance(tour, dist_matrix):
distance = 0
for i in range(len(tour) - 1):
distance += dist_matrix[tour[i]][tour[i + 1]]
distance += dist_matrix[tour[-1]][tour[0]] # Return to starting city
return distance
def setup_and_run_ga(num_cities=10, seed=1, pop_size=100, ngen=20, cxpb=0.7, mutpb=0.2):
# reproducibility
random.seed(seed)
np.random.seed(seed)
cities = np.random.rand(num_cities, 2) * 100
dist_matrix = np.linalg.norm(cities[:, np.newaxis] - cities[np.newaxis, :], axis=2)
# avoid re-creating creators if module is reloaded
try:
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
except Exception:
# creators already exist in this session
pass
toolbox = base.Toolbox()
toolbox.register("indices", random.sample, range(num_cities), num_cities)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.indices)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evaluate(individual):
return (total_distance(individual, dist_matrix),)
toolbox.register("evaluate", evaluate)
toolbox.register("mate", tools.cxOrdered)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
print("Starting Genetic Algorithm for TSP...\n")
population = toolbox.population(n=pop_size)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
stats.register("max", np.max)
pop, log = algorithms.eaSimple(population, toolbox, cxpb=cxpb, mutpb=mutpb,
ngen=ngen, stats=stats, verbose=True)
# select best from final population
best_ind = tools.selBest(pop, 1)[0]
min_dist = total_distance(best_ind, dist_matrix)
print("\n--- GA Finished ---")
print("Best tour found:")
print(list(best_ind))
print(f"Minimum tour distance: {min_dist:.2f}")
# Plot result
plt.figure(figsize=(7, 6))
plt.scatter(cities[:, 0], cities[:, 1], c='blue', s=50, label="Cities")
tour_idx = list(best_ind) + [best_ind[0]] # Return to start
tour_coords = cities[tour_idx]
plt.plot(tour_coords[:, 0], tour_coords[:, 1], 'r-', linewidth=1.5, label="Best Path")
plt.title(f"Best Tour Found (Distance: {min_dist:.2f})")
plt.xlabel("X Coordinate")
plt.ylabel("Y Coordinate")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
if __name__ == "__main__":
setup_and_run_ga(num_cities=10, seed=1, pop_size=100, ngen=20, cxpb=0.7, mutpb=0.2)