class_docstr: An MOA class, inherited from Optimizer.
  
  This is the designed class to define MOA-related
  variables and methods.
  
  References:
      M.-H. Tayarani and M.-R. Akbarzadeh. Magnetic-inspired optimization algorithms: Operators and structures.
      Swarm and Evolutionary Computation (2014).
class_docstr_tok: ["An","MOA","class",",","inherited","from","Optimizer",".","This","is","the","designed","class","to","define","MOA-related","variables","and","methods",".","References",":","M.-H.","Tayarani","and","M.-R.","Akbarzadeh",".","Magnetic-inspired","optimization","algorithms",":","Operators","and","structures",".","Swarm","and","Evolutionary","Computation","(","2014",")","."]
docstr: Wraps Magnetic Optimization Algorithm over all agents and variables.
  
  Args:
      space: Space containing agents and update-related information.
docstr_tok: ["Wraps","Magnetic","Optimization","Algorithm","over","all","agents","and","variables",".","Args",":","space",":","Space","containing","agents","and","update-related","information","."]
code: def update(self, space: Space) -> None:
      """Wraps Magnetic Optimization Algorithm over all agents and variables.
  
      Args:
          space: Space containing agents and update-related information.
  
      """
  
      space.agents.sort(key=lambda x: x.fit)
  
      # Gathers the best and worst agents and calculates a list of normalized fitness (eq. 2)
      best, worst = space.agents[0], space.agents[-1]
      fitness = [
          (agent.fit - best.fit) / (worst.fit - best.fit + c.EPSILON)
          for agent in space.agents
      ]
  
      # Calculates the masses (eq. 3)
      mass = [self.alpha + self.rho * fit for fit in fitness]
  
      for i, agent in enumerate(space.agents):
          # Gathers the agents neighbours (eq. 4)
          root = np.sqrt(space.n_agents)
          north = int((i - root) % space.n_agents)
          south = int((i + root) % space.n_agents)
          west = int(
              (i - 1) + ((i + root - 1) % root) // (root - 1) * root
          )
          east = int((i + 1) - (i % root) // (root - 1) * root)
          neighbours = [north, south, west, east]
  
          force = 0
  
          for n in neighbours:
              # Calculates the distance between current agent and neighbour (eq. 7)
              distance = g.euclidean_distance(
                  agent.position, space.agents[n].position
              )
  
              # Calculates the force between agents (eq. 5)
              force += (
                  (space.agents[n].position - agent.position)
                  * fitness[n]
                  / (distance + c.EPSILON)
              )
  
          force = np.mean(force)
  
          # Updates the agent's velocity(eq. 9)
          r1 = r.generate_uniform_random_number()
          velocity = force / mass[i] * r1
  
          # Updates the agent's position (eq. 10)
          agent.position += velocity
          agent.clip_by_bound()
  
file_path: opytimizer/opytimizer/optimizers/science/moa.py
imports: from typing import Any, Dict, Optional
  import numpy
  import opytimizer.math.general
  import opytimizer.math.random
  import opytimizer.utils.constant
  import opytimizer.utils.exception
  from opytimizer.core import Optimizer
  from opytimizer.core.space import Space
  from opytimizer.utils import logging
label_desc: Use image node_id 7 for calling the MOA obj's underlying member method code with example usage: obj.update(space) without return types