Skip to content

Commit

Permalink
chore(optimizers): Adds a bunch of annotated typing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gugarosa committed May 4, 2022
1 parent d6fb2a1 commit 599d0df
Show file tree
Hide file tree
Showing 83 changed files with 2,718 additions and 2,167 deletions.
69 changes: 43 additions & 26 deletions opytimizer/optimizers/boolean/bmrfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
"""

import copy
from typing import Any, Dict, List, Optional

import numpy as np

import opytimizer.math.random as r
import opytimizer.utils.exception as e
from opytimizer.core import Optimizer
from opytimizer.core.agent import Agent
from opytimizer.core.function import Function
from opytimizer.core.space import Space
from opytimizer.utils import logging

logger = logging.get_logger(__name__)
Expand All @@ -24,11 +28,11 @@ class BMRFO(Optimizer):
"""

def __init__(self, params=None):
def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
"""Initialization method.
Args:
params (dict): Contains key-value parameters to the meta-heuristics.
params: Contains key-value parameters to the meta-heuristics.
"""

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

@property
def S(self):
"""float: Somersault foraging."""
def S(self) -> np.ndarray:
"""Somersault foraging."""

return self._S

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

self._S = S

def _cyclone_foraging(self, agents, best_position, i, iteration, n_iterations):
def _cyclone_foraging(
self,
agents: List[Agent],
best_position: np.ndarray,
i: int,
iteration: int,
n_iterations: int,
) -> np.ndarray:
"""Performs the cyclone foraging procedure.
Args:
agents (list): List of agents.
best_position (np.array): Global best position.
i (int): Current agent's index.
iteration (int): Current iteration.
n_iterations (int): Maximum number of iterations.
agents: List of agents.
best_position: Global best position.
i: Current agent's index.
iteration: Current iteration.
n_iterations: Maximum number of iterations.
Returns:
A new cyclone foraging.
(np.ndarray): A new cyclone foraging.
"""

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

return cyclone_foraging

def _chain_foraging(self, agents, best_position, i):
def _chain_foraging(
self, agents: List[Agent], best_position: np.ndarray, i: int
) -> np.ndarray:
"""Performs the chain foraging procedure.
Args:
agents (list): List of agents.
best_position (np.array): Global best position.
i (int): Current agent's index.
agents: List of agents.
best_position: Global best position.
i: Current agent's index.
Returns:
A new chain foraging.
(np.ndarray): A new chain foraging.
"""

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

return chain_foraging

def _somersault_foraging(self, position, best_position):
def _somersault_foraging(
self, position: np.ndarray, best_position: np.ndarray
) -> np.ndarray:
"""Performs the somersault foraging procedure.
Args:
position (np.array): Agent's current position.
best_position (np.array): Global best position.
position: Agent's current position.
best_position: Global best position.
Returns:
A new somersault foraging.
(np.ndarray): A new somersault foraging.
"""

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

return somersault_foraging

def update(self, space, function, iteration, n_iterations):
def update(
self, space: Space, function: Function, iteration: int, n_iterations: int
) -> None:
"""Wraps Boolean Manta Ray Foraging Optimization over all agents and variables.
Args:
space (Space): Space containing agents and update-related information.
function (Function): A Function object that will be used as the objective function.
iteration (int): Current iteration.
n_iterations (int): Maximum number of iterations.
space: Space containing agents and update-related information.
function: A Function object that will be used as the objective function.
iteration: Current iteration.
n_iterations: Maximum number of iterations.
"""

Expand Down
45 changes: 24 additions & 21 deletions opytimizer/optimizers/boolean/bpso.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import copy
import time
from typing import Any, Dict, Optional

import numpy as np

import opytimizer.math.random as r
import opytimizer.utils.exception as e
from opytimizer.core import Optimizer
from opytimizer.core.function import Function
from opytimizer.core.space import Space
from opytimizer.utils import logging

logger = logging.get_logger(__name__)
Expand All @@ -27,11 +30,11 @@ class BPSO(Optimizer):
"""

def __init__(self, params=None):
def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
"""Initialization method.
Args:
params (dict): Contains key-value parameters to the meta-heuristics.
params: Contains key-value parameters to the meta-heuristics.
"""

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

@property
def c1(self):
"""float: Cognitive constant."""
def c1(self) -> np.ndarray:
"""Cognitive constant."""

return self._c1

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

self._c1 = c1

@property
def c2(self):
"""float: Social constant."""
def c2(self) -> np.ndarray:
"""Social constant."""

return self._c2

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

self._c2 = c2

@property
def local_position(self):
"""np.array: Array of local positions."""
def local_position(self) -> np.ndarray:
"""Array of local positions."""

return self._local_position

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

self._local_position = local_position

@property
def velocity(self):
"""np.array: Array of velocities."""
def velocity(self) -> np.ndarray:
"""Array of velocities."""

return self._velocity

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

self._velocity = velocity

def compile(self, space):
def compile(self, space: Space) -> None:
"""Compiles additional information that is used by this optimizer.
Args:
space (Space): A Space object containing meta-information.
space: A Space object containing meta-information.
"""

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

def evaluate(self, space, function):
def evaluate(self, space: Space, function: Function) -> None:
"""Evaluates the search space according to the objective function.
Args:
space (Space): A Space object that will be evaluated.
function (Function): A Function object that will be used as the objective function.
space: A Space object that will be evaluated.
function: A Function object that will be used as the objective function.
"""

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

def update(self, space):
def update(self, space: Space) -> None:
"""Wraps Boolean Particle Swarm Optimization over all agents and variables.
Args:
space (Space): Space containing agents and update-related information.
space: Space containing agents and update-related information.
"""

Expand Down
Loading

0 comments on commit 599d0df

Please sign in to comment.