Skip to content

Commit bf6a6b2

Browse files
committed
Moved position clipping to Coordinate base class
Moved value update to Coordinate base class
1 parent 46cfcc1 commit bf6a6b2

File tree

7 files changed

+50
-34
lines changed

7 files changed

+50
-34
lines changed

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"recommendations": [
33
"ms-python.python",
44
"streetsidesoftware.code-spell-checker",
5-
"njpwerner.autodocstring"
5+
"njpwerner.autodocstring",
6+
"gruntfuggly.todo-tree"
67
]
78
}

swarmlib/cuckoosearch/cuckoo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ def levy_flight(start: Tuple[float, float], alpha: float, param_lambda: float) -
1919
Arguments:
2020
start {Tuple[float, float]} -- The cuckoo's start position
2121
alpha {float} -- The step size
22-
23-
Keyword Arguments:
2422
param_lambda {float} -- lambda parameter of the levy distribution
2523
2624
Returns:

swarmlib/cuckoosearch/nest.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
from typing import Tuple
77

8-
import numpy as np
9-
108
from ..util.coordinate import Coordinate
119

1210

@@ -28,8 +26,7 @@ def abandoned(self) -> bool:
2826

2927
def abandon(self) -> None:
3028
self.__abandoned = True
31-
self._position = np.random.uniform(self._lower_boundary, self._upper_boundary, 2)
32-
self._value = self._function(self._position)
29+
self._initialize()
3330

3431
def update_pos(self, new_position: Tuple[float, float]) -> None:
3532
"""
@@ -40,8 +37,8 @@ def update_pos(self, new_position: Tuple[float, float]) -> None:
4037
"""
4138

4239
new_value = self._function(new_position)
43-
if new_value < self._value:
40+
if new_value < self.value:
4441
if self.__abandoned:
4542
self.__abandoned = False
46-
self._value = new_value
43+
# Value is updated automatically in base class
4744
self._position = new_position

swarmlib/fireflyalgorithm/firefly.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ def __init__(self, **kwargs):
1818
self.__alpha = kwargs.get('alpha', 0.25)
1919
self.__beta = kwargs.get('beta', 1)
2020
self.__gamma = kwargs.get('gamma', 0.97)
21-
self.__upper_boundary = kwargs.get('upper_boundary', 0.)
22-
self.__lower_boundary = kwargs.get('lower_boundary', 4.)
23-
24-
def update_intensity(self):
25-
self._value = self._function(self._position)
2621

2722
def move_towards(self, better_position):
2823
# euclidean distance
@@ -33,7 +28,5 @@ def move_towards(self, better_position):
3328
self.__beta*np.exp(-self.__gamma*(distance**2)) * (better_position-self._position) + \
3429
self.__alpha*(random.uniform(0, 1)-0.5)
3530

36-
self._position = np.clip(self._position, a_min=self.__lower_boundary, a_max=self.__upper_boundary)
37-
3831
def random_walk(self, area):
3932
self._position = np.array([np.random.uniform(cord-area, cord+area) for cord in self._position])

swarmlib/fireflyalgorithm/firefly_problem.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ def __init__(self, **kwargs):
3535
for _ in range(kwargs['firefly_number'])
3636
]
3737

38-
# Initialize intensity
39-
for firefly in self.__fireflies:
40-
firefly.update_intensity()
41-
4238
# Initialize visualizer for plotting
4339
self.__visualizer = Visualizer(**kwargs)
4440
self.__visualizer.add_data(positions=[firefly.position for firefly in self.__fireflies])
@@ -51,7 +47,6 @@ def solve(self):
5147
for j in self.__fireflies:
5248
if j.value < i.value:
5349
i.move_towards(j.position)
54-
i.update_intensity()
5550

5651
current_best = min(self.__fireflies, key=lambda firefly: firefly.value)
5752
if not best or current_best.value < best.value:
@@ -61,7 +56,6 @@ def solve(self):
6156

6257
# randomly walk the best firefly
6358
current_best.random_walk(0.1)
64-
current_best.update_intensity()
6559

6660
# Add data for visualization
6761
self.__visualizer.add_data(positions=[firefly.position for firefly in self.__fireflies])

swarmlib/pso/particle.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, **kwargs):
2525

2626
# Local best
2727
self.__best_position = self._position
28-
self.__best_value = self._value
28+
self.__best_value = self.value
2929

3030
@property
3131
def velocity(self) -> float:
@@ -49,15 +49,12 @@ def step(self, global_best_pos: Tuple[float, float]) -> None:
4949
self.__clip_velocity()
5050

5151
# Update position and clip it to boundaries
52-
self._position = np.clip(self._position + self.__velocity, a_min=self._lower_boundary, a_max=self._upper_boundary)
53-
54-
# Update value
55-
self._value = self._function(self._position)
52+
self._position = self._position + self.__velocity
5653

5754
# Update local best
58-
if self._value < self.__best_value:
55+
if self.value < self.__best_value:
5956
self.__best_position = self._position
60-
self.__best_value = self._value
57+
self.__best_value = self.value
6158

6259
def __clip_velocity(self):
6360
norm = np.linalg.norm(self.__velocity)

swarmlib/util/coordinate.py

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,53 @@
1010

1111
class Coordinate:
1212
def __init__(self, **kwargs) -> None:
13-
self._lower_boundary = kwargs.get('lower_boundary', 0.)
14-
self._upper_boundary = kwargs.get('upper_boundary', 4.)
13+
"""
14+
Initializes a new random coordinate.
15+
"""
16+
self.__lower_boundary = kwargs.get('lower_boundary', 0.)
17+
self.__upper_boundary = kwargs.get('upper_boundary', 4.)
1518
self._function = kwargs['function']
1619

17-
self._position = np.random.uniform(self._lower_boundary, self._upper_boundary, 2)
18-
self._value = self._function(self._position)
20+
self.__value = None
21+
self.__position = None
22+
self._initialize()
23+
24+
def _initialize(self) -> None:
25+
"""
26+
Initialize a new random position and its value
27+
"""
28+
self._position = np.random.uniform(self.__lower_boundary, self.__upper_boundary, 2)
29+
1930

2031
@property
2132
def position(self) -> Tuple[float, float]:
33+
"""
34+
Get the coordinate's position
35+
36+
Returns:
37+
Tuple[float, float]: the Position
38+
"""
2239
return self._position
2340

41+
# Internal Getter
42+
@property
43+
def _position(self) -> Tuple[float, float]:
44+
return self.__position
45+
46+
# Internal Setter for automatic position clipping and value update
47+
@_position.setter
48+
def _position(self, new_pos: Tuple[float, float]) -> None:
49+
"""
50+
Set the coordinate's new position.
51+
Also updates checks whether the position is within the set boundaries
52+
and updates the coordinate's value.
53+
54+
Args:
55+
value (Tuple[float, float]): The new coordinate position
56+
"""
57+
self.__position = np.clip(new_pos, a_min=self.__lower_boundary, a_max=self.__upper_boundary)
58+
self.__value = self._function(self.__position)
59+
2460
@property
2561
def value(self) -> float:
26-
return self._value
62+
return self.__value

0 commit comments

Comments
 (0)