-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwarm.py
More file actions
74 lines (58 loc) · 2.94 KB
/
Swarm.py
File metadata and controls
74 lines (58 loc) · 2.94 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
import time
from Pallets import Pallets
from Particle import Particle
from Particle_With_LS import Particle_With_LS
class Swarm:
"""
Represents a particle swarm for an instance of the pallet problem.
"""
def __init__(self, pallet_problem : Pallets, cognitive_coefficient = 1.1193):
self.pallet_problem = pallet_problem
Particle.COGNITIVE_COEFFICIENT = cognitive_coefficient
Particle_With_LS.COGNITIVE_COEFFICIENT = cognitive_coefficient
def timed_swarm_search(self, swarm_size: int, run_time: int):
"""
Conduct a PSO swarm search for a solution to the pallet problem for a specified length of time.
Parameters:
swarm_size: number of particles in the swarm
run_time: length of time (seconds) to search for
Returns: a list containing the best solution found, followed by its cost
"""
end_time = time.time() + run_time
gbest = self.pallet_problem.generate_random_solution()
gbest_cost = self.pallet_problem.evaluate_cost(gbest)
self.particles = [None for x in range(swarm_size)]
for i in range(swarm_size):
self.particles[i] = Particle(self.pallet_problem, self.pallet_problem.generate_random_solution())
while time.time() < end_time:
for j in range(len(self.particles)):
pbest = self.particles[j].update_particle(gbest)
pbest_cost = self.pallet_problem.evaluate_cost(pbest)
if pbest_cost < gbest_cost:
gbest = pbest
gbest_cost = pbest_cost
#print(f"Best found by swarm search: {self.gbest} costing {self.gbest_cost}")
return [gbest, gbest_cost]
def timed_swarm_search_with_lsi(self, swarm_size: int, run_time: int):
"""
Conduct a PSOwLSI swarm search for a solution to the pallet problem for a specified length of time.
Parameters:
swarm_size: number of particles in the swarm
run_time: length of time (seconds) to search for
Returns: a list containing the best solution found, followed by its cost
"""
end_time = time.time() + run_time
gbest = self.pallet_problem.generate_random_solution()
gbest_cost = self.pallet_problem.evaluate_cost(gbest)
self.particles = [None for x in range(swarm_size)]
for i in range(swarm_size):
self.particles[i] = Particle_With_LS(self.pallet_problem, self.pallet_problem.generate_random_solution())
while time.time() < end_time:
for j in range(len(self.particles)):
pbest = self.particles[j].update_particle(gbest)
pbest_cost = self.pallet_problem.evaluate_cost(pbest)
if pbest_cost < gbest_cost:
gbest = pbest
gbest_cost = pbest_cost
#print(f"Best found by swarm search: {self.gbest} costing {self.gbest_cost}")
return [gbest, gbest_cost]