-
Notifications
You must be signed in to change notification settings - Fork 1
/
SSO.py
95 lines (82 loc) · 3.47 KB
/
SSO.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
import numpy as np
import time as ti
import statistics
import random as ra
class SSO(object):
def __init__(self, N_sol, N_var, N_generations, Cg=0.8, Cp=0.5, Cw=0.2, VarMax=5.12, VarMin=-5.12, CPUTime=100):
self.N_sol = N_sol
self.N_var = N_var
self.N_generations = N_generations
self.Cg = Cg
self.Cp = Cp
self.Cw = Cw
self.VarMax = VarMax
self.VarMin = VarMin
self.CPUTime = CPUTime
self.main_process(self.N_generations)
def fitness(self, x): return sum(x**2)
def main_process(self, N_generations):
population = self._init_solContnet()
countTime = self.count_time()
for t in range(N_generations):
if countTime < self.CPUTime:
population = self.dice(population)
countTime = self.count_time()
else:
print('\nMeet the CPUTime!!\n')
break
self.gg = population
self.__str__(population, countTime)
def dice(self, dice_object):
for i in range(dice_object[:].__len__()):
for j in range(self.N_var):
r = ra.random()
if r < self.Cw:
pass
elif r < self.Cp:
dice_object[i]['sol'][j] = dice_object[i]['pbestSol'][j]
elif r < self.Cg:
dice_object[i]['sol'][j] = dice_object[self.gbest]['pbestSol'][j]
else:
dice_object[i]['sol'][j] = np.random.uniform(
self.VarMin, self.VarMax)
dice_object[i]['solFitness'] = self.fitness(
dice_object[i]['sol'])
# update pbest、gbest
if dice_object[i]['solFitness'] < dice_object[i]['pbestFitness']:
dice_object[i]['pbestSol'] = dice_object[i]['sol'].copy()
dice_object[i]['pbestFitness'] = dice_object[i]['solFitness']
if dice_object[i]['pbestFitness'] < dice_object[self.gbest]['pbestFitness']:
self.gbest = i
return dice_object
def _init_solContnet(self):
self.startTime = ti.time()
gbest = 0
population = []
for i in range(self.N_sol):
population.append({'sol': None, 'solFitness': None,
'pbestSol': None, 'pbestFitness': None})
population[i]['sol'] = np.random.uniform(
self.VarMin, self.VarMax, self.N_var)
population[i]['pbestSol'] = population[i]['sol']
population[i]['solFitness'] = self.fitness(
population[i]['sol'])
population[i]['pbestFitness'] = population[i]['solFitness']
if population[i]['solFitness'] < population[gbest]['solFitness']:
gbest = i
self.gbest = gbest
self.gg = population
return population
def count_time(self):
# use countTime record run time
currentTime = ti.time()
return (lambda countTime: countTime)(currentTime - self.startTime)
def __str__(self, population,countTime):
return print('the best cost is:',
population[self.gbest]['pbestFitness'],'\n',
'the gbest: %i' % self.gbest,'\n',
'time elapsed : %s' % countTime)
if __name__ == "__main__":
a = SSO(N_sol=512, N_generations=2000, N_var=500, Cg=0.8, Cp=0.5,
Cw=0.2, VarMax=5.12, VarMin=-5.12, CPUTime=10000)
print('run out of SSO')