Skip to content

Commit 599d0df

Browse files
committed
chore(optimizers): Adds a bunch of annotated typing.
1 parent d6fb2a1 commit 599d0df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2718
-2167
lines changed

opytimizer/optimizers/boolean/bmrfo.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
"""
33

44
import copy
5+
from typing import Any, Dict, List, Optional
56

67
import numpy as np
78

89
import opytimizer.math.random as r
910
import opytimizer.utils.exception as e
1011
from opytimizer.core import Optimizer
12+
from opytimizer.core.agent import Agent
13+
from opytimizer.core.function import Function
14+
from opytimizer.core.space import Space
1115
from opytimizer.utils import logging
1216

1317
logger = logging.get_logger(__name__)
@@ -24,11 +28,11 @@ class BMRFO(Optimizer):
2428
2529
"""
2630

27-
def __init__(self, params=None):
31+
def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
2832
"""Initialization method.
2933
3034
Args:
31-
params (dict): Contains key-value parameters to the meta-heuristics.
35+
params: Contains key-value parameters to the meta-heuristics.
3236
3337
"""
3438

@@ -46,30 +50,37 @@ def __init__(self, params=None):
4650
logger.info("Class overrided.")
4751

4852
@property
49-
def S(self):
50-
"""float: Somersault foraging."""
53+
def S(self) -> np.ndarray:
54+
"""Somersault foraging."""
5155

5256
return self._S
5357

5458
@S.setter
55-
def S(self, S):
59+
def S(self, S: np.ndarray) -> None:
5660
if not isinstance(S, np.ndarray):
5761
raise e.TypeError("`S` should be a numpy array")
5862

5963
self._S = S
6064

61-
def _cyclone_foraging(self, agents, best_position, i, iteration, n_iterations):
65+
def _cyclone_foraging(
66+
self,
67+
agents: List[Agent],
68+
best_position: np.ndarray,
69+
i: int,
70+
iteration: int,
71+
n_iterations: int,
72+
) -> np.ndarray:
6273
"""Performs the cyclone foraging procedure.
6374
6475
Args:
65-
agents (list): List of agents.
66-
best_position (np.array): Global best position.
67-
i (int): Current agent's index.
68-
iteration (int): Current iteration.
69-
n_iterations (int): Maximum number of iterations.
76+
agents: List of agents.
77+
best_position: Global best position.
78+
i: Current agent's index.
79+
iteration: Current iteration.
80+
n_iterations: Maximum number of iterations.
7081
7182
Returns:
72-
A new cyclone foraging.
83+
(np.ndarray): A new cyclone foraging.
7384
7485
"""
7586

@@ -143,16 +154,18 @@ def _cyclone_foraging(self, agents, best_position, i, iteration, n_iterations):
143154

144155
return cyclone_foraging
145156

146-
def _chain_foraging(self, agents, best_position, i):
157+
def _chain_foraging(
158+
self, agents: List[Agent], best_position: np.ndarray, i: int
159+
) -> np.ndarray:
147160
"""Performs the chain foraging procedure.
148161
149162
Args:
150-
agents (list): List of agents.
151-
best_position (np.array): Global best position.
152-
i (int): Current agent's index.
163+
agents: List of agents.
164+
best_position: Global best position.
165+
i: Current agent's index.
153166
154167
Returns:
155-
A new chain foraging.
168+
(np.ndarray): A new chain foraging.
156169
157170
"""
158171

@@ -188,15 +201,17 @@ def _chain_foraging(self, agents, best_position, i):
188201

189202
return chain_foraging
190203

191-
def _somersault_foraging(self, position, best_position):
204+
def _somersault_foraging(
205+
self, position: np.ndarray, best_position: np.ndarray
206+
) -> np.ndarray:
192207
"""Performs the somersault foraging procedure.
193208
194209
Args:
195-
position (np.array): Agent's current position.
196-
best_position (np.array): Global best position.
210+
position: Agent's current position.
211+
best_position: Global best position.
197212
198213
Returns:
199-
A new somersault foraging.
214+
(np.ndarray): A new somersault foraging.
200215
201216
"""
202217

@@ -217,14 +232,16 @@ def _somersault_foraging(self, position, best_position):
217232

218233
return somersault_foraging
219234

220-
def update(self, space, function, iteration, n_iterations):
235+
def update(
236+
self, space: Space, function: Function, iteration: int, n_iterations: int
237+
) -> None:
221238
"""Wraps Boolean Manta Ray Foraging Optimization over all agents and variables.
222239
223240
Args:
224-
space (Space): Space containing agents and update-related information.
225-
function (Function): A Function object that will be used as the objective function.
226-
iteration (int): Current iteration.
227-
n_iterations (int): Maximum number of iterations.
241+
space: Space containing agents and update-related information.
242+
function: A Function object that will be used as the objective function.
243+
iteration: Current iteration.
244+
n_iterations: Maximum number of iterations.
228245
229246
"""
230247

opytimizer/optimizers/boolean/bpso.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44
import copy
55
import time
6+
from typing import Any, Dict, Optional
67

78
import numpy as np
89

910
import opytimizer.math.random as r
1011
import opytimizer.utils.exception as e
1112
from opytimizer.core import Optimizer
13+
from opytimizer.core.function import Function
14+
from opytimizer.core.space import Space
1215
from opytimizer.utils import logging
1316

1417
logger = logging.get_logger(__name__)
@@ -27,11 +30,11 @@ class BPSO(Optimizer):
2730
2831
"""
2932

30-
def __init__(self, params=None):
33+
def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
3134
"""Initialization method.
3235
3336
Args:
34-
params (dict): Contains key-value parameters to the meta-heuristics.
37+
params: Contains key-value parameters to the meta-heuristics.
3538
3639
"""
3740

@@ -52,62 +55,62 @@ def __init__(self, params=None):
5255
logger.info("Class overrided.")
5356

5457
@property
55-
def c1(self):
56-
"""float: Cognitive constant."""
58+
def c1(self) -> np.ndarray:
59+
"""Cognitive constant."""
5760

5861
return self._c1
5962

6063
@c1.setter
61-
def c1(self, c1):
64+
def c1(self, c1: np.ndarray) -> None:
6265
if not isinstance(c1, np.ndarray):
6366
raise e.TypeError("`c1` should be a numpy array")
6467

6568
self._c1 = c1
6669

6770
@property
68-
def c2(self):
69-
"""float: Social constant."""
71+
def c2(self) -> np.ndarray:
72+
"""Social constant."""
7073

7174
return self._c2
7275

7376
@c2.setter
74-
def c2(self, c2):
77+
def c2(self, c2: np.ndarray) -> None:
7578
if not isinstance(c2, np.ndarray):
7679
raise e.TypeError("`c2` should be a numpy array")
7780

7881
self._c2 = c2
7982

8083
@property
81-
def local_position(self):
82-
"""np.array: Array of local positions."""
84+
def local_position(self) -> np.ndarray:
85+
"""Array of local positions."""
8386

8487
return self._local_position
8588

8689
@local_position.setter
87-
def local_position(self, local_position):
90+
def local_position(self, local_position: np.ndarray) -> None:
8891
if not isinstance(local_position, np.ndarray):
8992
raise e.TypeError("`local_position` should be a numpy array")
9093

9194
self._local_position = local_position
9295

9396
@property
94-
def velocity(self):
95-
"""np.array: Array of velocities."""
97+
def velocity(self) -> np.ndarray:
98+
"""Array of velocities."""
9699

97100
return self._velocity
98101

99102
@velocity.setter
100-
def velocity(self, velocity):
103+
def velocity(self, velocity: np.ndarray) -> None:
101104
if not isinstance(velocity, np.ndarray):
102105
raise e.TypeError("`velocity` should be a numpy array")
103106

104107
self._velocity = velocity
105108

106-
def compile(self, space):
109+
def compile(self, space: Space) -> None:
107110
"""Compiles additional information that is used by this optimizer.
108111
109112
Args:
110-
space (Space): A Space object containing meta-information.
113+
space: A Space object containing meta-information.
111114
112115
"""
113116

@@ -119,12 +122,12 @@ def compile(self, space):
119122
(space.n_agents, space.n_variables, space.n_dimensions), dtype=bool
120123
)
121124

122-
def evaluate(self, space, function):
125+
def evaluate(self, space: Space, function: Function) -> None:
123126
"""Evaluates the search space according to the objective function.
124127
125128
Args:
126-
space (Space): A Space object that will be evaluated.
127-
function (Function): A Function object that will be used as the objective function.
129+
space: A Space object that will be evaluated.
130+
function: A Function object that will be used as the objective function.
128131
129132
"""
130133

@@ -148,11 +151,11 @@ def evaluate(self, space, function):
148151
space.best_agent.fit = copy.deepcopy(agent.fit)
149152
space.best_agent.ts = int(time.time())
150153

151-
def update(self, space):
154+
def update(self, space: Space) -> None:
152155
"""Wraps Boolean Particle Swarm Optimization over all agents and variables.
153156
154157
Args:
155-
space (Space): Space containing agents and update-related information.
158+
space: Space containing agents and update-related information.
156159
157160
"""
158161

0 commit comments

Comments
 (0)