빌드 단계 추가 및 코드 정리

This commit is contained in:
jung-geun
2024-03-08 17:49:44 +09:00
parent 9cce58c177
commit 4cd563190f
10 changed files with 2478 additions and 2384 deletions

View File

@@ -1,5 +1,6 @@
import numpy as np
class PSO(object):
"""
Class implementing PSO algorithm
@@ -16,16 +17,16 @@ class PSO(object):
"""
self.func = func
self.n_particles = n_particles
self.init_pos = init_pos # 검색할 차원
self.init_pos = init_pos # 검색할 차원
self.particle_dim = len(init_pos) # 검색할 차원의 크기
self.particles_pos = np.random.uniform(size=(n_particles, self.particle_dim)) \
* self.init_pos
self.particles_pos = (
np.random.uniform(size=(n_particles, self.particle_dim)) * self.init_pos
)
# 입력받은 파티클의 개수 * 검색할 차원의 크기 만큼의 균등한 위치를 생성
self.velocities = np.random.uniform(
size=(n_particles, self.particle_dim))
self.velocities = np.random.uniform(size=(n_particles, self.particle_dim))
# 입력받은 파티클의 개수 * 검색할 차원의 크기 만큼의 속도를 무작위로 초기화
self.g_best = init_pos # 최대 사이즈로 전역 최적갑 저장 - global best
self.p_best = self.particles_pos # 모든 파티클의 위치 - particles best
self.g_best = init_pos # 최대 사이즈로 전역 최적갑 저장 - global best
self.p_best = self.particles_pos # 모든 파티클의 위치 - particles best
self.g_history = []
self.history = []
@@ -42,24 +43,24 @@ class PSO(object):
"""
x = np.array(x) # 각 파티클의 위치
v = np.array(v) # 각 파티클의 속도(방향과 속력을 가짐)
new_x = x + v # 각 파티클을 랜덤한 속도만큼 진행
return new_x # 진행한 파티클들의 위치를 반환
new_x = x + v # 각 파티클을 랜덤한 속도만큼 진행
return new_x # 진행한 파티클들의 위치를 반환
def update_velocity(self, x, v, p_best, g_best, c0=0.5, c1=1.5, w=0.75):
"""
Update particle velocity
Update particle velocity
Args:
x(array-like): particle current position
v (array-like): particle current velocity
p_best(array-like): the best position found so far for a particle
g_best(array-like): the best position regarding all the particles found so far
c0 (float): the congnitive scaling constant, 인지 스케일링 상수
c1 (float): the social scaling constant
w (float): the inertia weight, 관성 중량
Args:
x(array-like): particle current position
v (array-like): particle current velocity
p_best(array-like): the best position found so far for a particle
g_best(array-like): the best position regarding all the particles found so far
c0 (float): the congnitive scaling constant, 인지 스케일링 상수
c1 (float): the social scaling constant
w (float): the inertia weight, 관성 중량
Returns:
The updated velocity (array-like).
Returns:
The updated velocity (array-like).
"""
x = np.array(x)
v = np.array(v)
@@ -73,7 +74,7 @@ class PSO(object):
# 가중치(상수)*속도 + \
# 스케일링 상수*랜덤 가중치*(나의 최적값 - 처음 위치) + \
# 전역 스케일링 상수*랜덤 가중치*(전체 최적값 - 처음 위치)
new_v = w*v + c0*r*(p_best - x) + c1*r*(g_best - x)
new_v = w * v + c0 * r * (p_best - x) + c1 * r * (g_best - x)
return new_v
def optimize(self, maxiter=200):
@@ -90,10 +91,9 @@ class PSO(object):
for _ in range(maxiter):
for i in range(self.n_particles):
x = self.particles_pos[i] # 각 파티클 추출
v = self.velocities[i] # 랜덤 생성한 속도 추출
p_best = self.p_best[i] # 결과치 저장할 변수 지정
self.velocities[i] = self.update_velocity(
x, v, p_best, self.g_best)
v = self.velocities[i] # 랜덤 생성한 속도 추출
p_best = self.p_best[i] # 결과치 저장할 변수 지정
self.velocities[i] = self.update_velocity(x, v, p_best, self.g_best)
# 다음에 움직일 속도 = 최초 위치, 현재 속도, 현재 위치, 최종 위치
self.particles_pos[i] = self.update_position(x, v)
# 현재 위치 = 최초 위치 현재 속도
@@ -106,7 +106,7 @@ class PSO(object):
if self.func(self.particles_pos[i]) < self.func(self.g_best):
self.g_best = self.particles_pos[i]
self.g_history.append(self.g_best)
self.history.append(self.particles_pos.copy())
# 전체 최소 위치, 전체 최소 벡터