-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotParetoFront.py
303 lines (236 loc) · 9.47 KB
/
PlotParetoFront.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import numpy as np
from sklearn.cluster import KMeans
from sklearn.mixture import GaussianMixture
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# Original Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
# New Data
new_costs = [1.2964818586720162, 1.667964983231757, 1.8405206274384565, 2.4454385483949457,
2.6289952648181285, 2.7037709795579286, 3.0528928800442934, 3.414389272794696,
4.019114145951091, 4.061721465751667]
new_costs_1 = []
new_costs_2 = []
# X-axis positions (Nest indices)
x_original = np.arange(1, len(costs) + 1)
x_new = np.arange(len(costs) + 1, len(costs) + len(new_costs) + 1)
# Function to find Pareto front
def pareto_frontier(Xs, Ys, maxX=True, maxY=False):
sorted_list = sorted([[Xs[i], Ys[i]] for i in range(len(Xs))], reverse=maxX)
p_front = [sorted_list[0]]
for pair in sorted_list[1:]:
if maxY:
if pair[1] >= p_front[-1][1]:
p_front.append(pair)
else:
if pair[1] <= p_front[-1][1]:
p_front.append(pair)
p_front_X = [pair[0] for pair in p_front]
p_front_Y = [pair[1] for pair in p_front]
return p_front_X, p_front_Y
# Find Pareto front for original and new data
p_front_X_original, p_front_Y_original = pareto_frontier(x_original, costs)
p_front_X_new, p_front_Y_new = pareto_frontier(x_new, new_costs)
# Create the scatter plot
plt.figure(figsize=(10, 6))
plt.scatter(x_original, costs, color='blue', label='Original Costs')
plt.scatter(x_new, new_costs, color='green', label='New Costs')
plt.plot(p_front_X_original, p_front_Y_original, color='red', linestyle='--', label='Pareto Front (Original)')
plt.plot(p_front_X_new, p_front_Y_new, color='orange', linestyle='--', label='Pareto Front (New)')
# Add value labels
for i, cost in enumerate(costs):
plt.annotate(f'{cost:.2f}', (x_original[i], costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
for i, cost in enumerate(new_costs):
plt.annotate(f'{cost:.2f}', (x_new[i], new_costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with Pareto Fronts')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()
############-kmeans-####################
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
# Original Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
# New Data
new_costs = [1.2964818586720162, 1.667964983231757, 1.8405206274384565, 2.4454385483949457,
2.6289952648181285, 2.7037709795579286, 3.0528928800442934, 3.414389272794696,
4.019114145951091, 4.061721465751667]
# Combine Data
all_costs = costs + new_costs
x = np.arange(1, len(all_costs) + 1)
# Combine x and all costs into a single array
data = np.column_stack((x, all_costs))
# Apply K-Means clustering
kmeans = KMeans(n_clusters=3, random_state=0)
kmeans.fit(data)
labels = kmeans.labels_
# Function to find Pareto front
def pareto_frontier(Xs, Ys, maxX=True, maxY=False):
sorted_list = sorted([[Xs[i], Ys[i]] for i in range(len(Xs))], reverse=maxX)
p_front = [sorted_list[0]]
for pair in sorted_list[1:]:
if maxY:
if pair[1] >= p_front[-1][1]:
p_front.append(pair)
else:
if pair[1] <= p_front[-1][1]:
p_front.append(pair)
p_front_X = [pair[0] for pair in p_front]
p_front_Y = [pair[1] for pair in p_front]
return p_front_X, p_front_Y
# Find Pareto front for combined data
p_front_X, p_front_Y = pareto_frontier(x, all_costs)
# Plot the scatter plot with clusters and Pareto front
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, all_costs, c=labels, cmap='viridis', label='Cost')
plt.colorbar(scatter, label='Cluster')
plt.plot(p_front_X, p_front_Y, color='red', linestyle='--', label='Pareto Front')
# Add value labels
for i, cost in enumerate(all_costs):
plt.annotate(f'{cost:.2f}', (x[i], all_costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with K-Means Clustering and Pareto Front')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()
###############ckmeans#############
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
# Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
x = np.arange(1, len(costs) + 1)
# Combine x and costs into a single array
data = np.column_stack((x, costs))
# Apply DBSCAN clustering
db = DBSCAN(eps=3, min_samples=2).fit(data)
labels = db.labels_
# Plot the scatter plot with clusters
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, costs, c=labels, cmap='viridis', label='Cost')
plt.colorbar(scatter, label='Cluster')
plt.plot(p_front_X, p_front_Y, color='red', linestyle='--', label='Pareto Front')
# Add value labels
for i, cost in enumerate(costs):
plt.annotate(f'{cost:.2f}', (x[i], costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with DBSCAN Clustering')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()
############-DBC-####################
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
# Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
x = np.arange(1, len(costs) + 1)
# Combine x and costs into a single array
data = np.column_stack((x, costs))
# Apply DBSCAN clustering
db = DBSCAN(eps=3, min_samples=2).fit(data)
labels = db.labels_
# Plot the scatter plot with clusters
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, costs, c=labels, cmap='viridis', label='Cost')
plt.colorbar(scatter, label='Cluster')
plt.plot(p_front_X, p_front_Y, color='red', linestyle='--', label='Pareto Front')
# Add value labels
for i, cost in enumerate(costs):
plt.annotate(f'{cost:.2f}', (x[i], costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with DBSCAN Clustering')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()
############-hierarchial clustering-####################
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import AgglomerativeClustering
# Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
x = np.arange(1, len(costs) + 1)
# Combine x and costs into a single array
data = np.column_stack((x, costs))
# Apply Agglomerative Hierarchical Clustering
agglo = AgglomerativeClustering(n_clusters=3)
labels = agglo.fit_predict(data)
# Plot the scatter plot with clusters
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, costs, c=labels, cmap='viridis', label='Cost')
plt.colorbar(scatter, label='Cluster')
plt.plot(p_front_X, p_front_Y, color='red', linestyle='--', label='Pareto Front')
# Add value labels
for i, cost in enumerate(costs):
plt.annotate(f'{cost:.2f}', (x[i], costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with Agglomerative Clustering')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()
####### gaussian mixture clustering #######
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
# Data
costs = [1.0884935606545056, 1.401030463573885, 2.2081470782222143, 2.859467331647874,
4.443448594557075, 5.294871846937422, 5.7420065440537345, 15.481036748293075,
19.77107999708505, 21.781522539832643]
x = np.arange(1, len(costs) + 1)
# Combine x and costs into a single array
data = np.column_stack((x, costs))
# Apply Gaussian Mixture Model clustering
gmm = GaussianMixture(n_components=3, random_state=0)
gmm.fit(data)
labels = gmm.predict(data)
# Plot the scatter plot with clusters
plt.figure(figsize=(10, 6))
scatter = plt.scatter(x, costs, c=labels, cmap='viridis', label='Cost')
plt.colorbar(scatter, label='Cluster')
plt.plot(p_front_X, p_front_Y, color='red', linestyle='--', label='Pareto Front')
# Add value labels
for i, cost in enumerate(costs):
plt.annotate(f'{cost:.2f}', (x[i], costs[i]), textcoords="offset points", xytext=(0, 5), ha='center')
# Set titles and labels
plt.title('Scatter Plot of Costs for Top Nests with Gaussian Mixture Clustering')
plt.xlabel('Nest Index')
plt.ylabel('Cost')
plt.legend()
plt.grid(True)
# Show the plot
plt.tight_layout()
plt.show()