Skip to content

Commit

Permalink
fix(opytimizer): code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasrodrigues committed Dec 18, 2023
1 parent 3d2d04e commit d058bc5
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions opytimizer/optimizers/swarm/waoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""

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

import numpy as np
Expand All @@ -18,7 +19,7 @@
class WAOA(Optimizer):
"""A WAOA class, inherited from Optimizer.
This is the designed class to dife WAOA-related
This is the designed class to define WAOA-related
variables and methods.
References:
Expand All @@ -34,14 +35,28 @@ def __init__(self, params: Optional[Dict[str, Any]] = None) -> None:
params (str): Contains key-value parameters to the meta-heuristics.
"""

logger.info("Overriding class: Optimizer -> SSA")
logger.info("Overriding class: Optimizer -> WAOA")

super(WAOA, self).__init__()

self.build(params)

logger.info("Class overrided.")

def evaluate(self, space: Space) -> None:
"""Evaluates the search space according to the objective function.
Args:
space: A Space object that will be evaluated.
"""
print('evaluating...')
for agent in space.agents:
if agent.fit < space.best_agent.fit:
space.best_agent.position = copy.deepcopy(agent.position)
space.best_agent.fit = copy.deepcopy(agent.fit)
space.best_agent.ts = int(time.time())

def update(self, space: Space, function: Function, iteration: int) -> None:
"""Wraps Walrus Optimization Algorithm over all agents and variables.
Expand All @@ -55,14 +70,16 @@ def update(self, space: Space, function: Function, iteration: int) -> None:
for i, agent in enumerate(space.agents):
a = copy.deepcopy(agent)

for j in range(space.n_variables):

r1 = r.generate_integer_random_number(1, 2)
r2 = r.generate_uniform_random_number()
r1 = r.generate_integer_random_number(
1, 3, size=(space.n_variables, space.n_dimensions)
)
r2 = r.generate_uniform_random_number(
size=(space.n_variables, space.n_dimensions)
)

a.position[j] = agent.position[j] + r2 * (
space.best_agent.position[j] - r1 * agent.position[j]
)
a.position = agent.position + r2 * (
space.best_agent.position - r1 * agent.position
)

a.clip_by_bound()

Expand All @@ -75,24 +92,26 @@ def update(self, space: Space, function: Function, iteration: int) -> None:

if space.agents[k].fit < agent.fit:

for j in range(space.n_variables):

r1 = r.generate_integer_random_number(1, 2)
r2 = r.generate_uniform_random_number()
r3 = r.generate_integer_random_number(
1, 3, size=(space.n_variables, space.n_dimensions)
)
r4 = r.generate_uniform_random_number(
size=(space.n_variables, space.n_dimensions)
)

a.position[j] = agent.position[j] + r2 * (
space.agents[k].position[j] - r1 * agent.position[j]
)
a.position = agent.position + r4 * (
space.agents[k].position - r3 * agent.position
)

else:

for j in range(space.n_variables):

r2 = r.generate_uniform_random_number()
r5 = r.generate_uniform_random_number(
size=(space.n_variables, space.n_dimensions)
)

a.position[j] = agent.position[j] + r2 * (
agent.position[j] - space.agents[k].position[j]
)
a.position = agent.position + r5 * (
agent.position - space.agents[k].position
)

a.clip_by_bound()

Expand All @@ -101,18 +120,18 @@ def update(self, space: Space, function: Function, iteration: int) -> None:
agent.position = copy.deepcopy(a.position)
agent.fit = copy.deepcopy(a.fit)

for j in range(space.n_variables):
r6 = r.generate_uniform_random_number(
size=(space.n_variables, space.n_dimensions)
)

r2 = r.generate_uniform_random_number()
lb = (agent.lb / (iteration + 1)).reshape(-1, 1)
ub = (agent.ub / (iteration + 1)).reshape(-1, 1)

a.position[j] = agent.position[j] + (
(agent.ub[j] / (iteration + 1))
- r2 * (agent.lb[j] / (iteration + 1))
)
a.position = agent.position + (lb + (ub - r6 * lb))

a.clip_by_bound()

a.fit = function(a.position)
if a.fit < agent.fit:
agent.position = copy.deepcopy(a.position)
agent.fit = copy.deepcopy(a.fit)
agent.fit = copy.deepcopy(a.fit)

0 comments on commit d058bc5

Please sign in to comment.