Skip to content

Commit 5194602

Browse files
committed
Add PSO driver code
1 parent afbcc12 commit 5194602

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Algorithms/Searching/PSO Feature Selection.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,45 @@ def print_position(self):
244244

245245
def print_velocity(self):
246246
print(self.velocity)
247+
248+
def pso_calculate(f_count, df):
249+
y_actual = []
250+
y_predict = []
251+
fitness_best_g = (-1, -1, -1)
252+
pos_fitness_g = []
253+
swarm = []
254+
no_population = 400
255+
iteration = 1
256+
257+
for i in range(0,no_population):
258+
swarm.append(PSO(f_count, df))
259+
260+
while iteration <= 10:
261+
262+
print('\nIteration : ', iteration)
263+
264+
for pos in range(0, no_population):
265+
266+
swarm[pos].evaluate_fitness()
267+
268+
#check current particle is the global best
269+
if swarm[pos].fitness_check(swarm[pos].fitness, fitness_best_g): #swarm[pos].fitness > fitness_best_g or fitness_best_g == -1:
270+
pos_fitness_g = list(swarm[pos].position)
271+
fitness_best_g = (swarm[pos].fitness)
272+
y_actual = swarm[pos].y_actual
273+
y_predict = swarm[pos].y_predict
274+
275+
for pos in range(0, no_population):
276+
swarm[pos].update_velocity(pos_fitness_g)
277+
swarm[pos].update_position()
278+
279+
print(pos_fitness_g)
280+
print(fitness_best_g)
281+
iteration+=1
282+
283+
284+
print('\n Final Solution:')
285+
print(pos_fitness_g)
286+
print(fitness_best_g)
287+
cm_2 = confusion_matrix(y_actual, y_predict)
288+
sns.heatmap(cm_2,annot=True,fmt="d")

0 commit comments

Comments
 (0)