class_docstr: Attacks a model with word substiutitions using a Particle Swarm
  Optimization (PSO) algorithm. Some key hyper-parameters are setup according
  to the original paper:
  
  "We adjust PSO on the validation set of SST and set ω_1 as 0.8 and ω_2 as 0.2.
  We set the max velocity of the particles V_{max} to 3, which means the changing
  probability of the particles ranges from 0.047 (sigmoid(-3)) to 0.953 (sigmoid(3))."
  
  Args:
      pop_size (:obj:`int`, optional): The population size. Defaults to 60.
      max_iters (:obj:`int`, optional): The maximum number of iterations to use. Defaults to 20.
      post_turn_check (:obj:`bool`, optional): If `True`, check if new position reached by moving passes the constraints. Defaults to `True`
      max_turn_retries (:obj:`bool`, optional): Maximum number of movement retries if new position after turning fails to pass the constraints.
          Applied only when `post_movement_check` is set to `True`.
          Setting it to 0 means we immediately take the old position as the new position upon failure.
class_docstr_tok: ["Attacks","a","model","with","word","substiutitions","using","a","Particle","Swarm","Optimization","(","PSO",")","algorithm",".","Some","key","hyper-parameters","are","setup","according","to","the","original","paper",":","''","We","adjust","PSO","on","the","validation","set","of","SST","and","set","\u03c9_1","as","0.8","and","\u03c9_2","as","0.2",".","We","set","the","max","velocity","of","the","particles","V_","{","max","}","to","3",",","which","means","the","changing","probability","of","the","particles","ranges","from","0.047","(","sigmoid","(","-3",")",")","to","0.953","(","sigmoid","(","3",")",")",".","''","Args",":","pop_size","(",":","obj",":","`","int","`",",","optional",")",":","The","population","size",".","Defaults","to","60.","max_iters","(",":","obj",":","`","int","`",",","optional",")",":","The","maximum","number","of","iterations","to","use",".","Defaults","to","20.","post_turn_check","(",":","obj",":","`","bool","`",",","optional",")",":","If","`","True","`",",","check","if","new","position","reached","by","moving","passes","the","constraints",".","Defaults","to","`","True","`","max_turn_retries","(",":","obj",":","`","bool","`",",","optional",")",":","Maximum","number","of","movement","retries","if","new","position","after","turning","fails","to","pass","the","constraints",".","Applied","only","when","`","post_movement_check","`","is","set","to","`","True","`",".","Setting","it","to","0","means","we","immediately","take","the","old","position","as","the","new","position","upon","failure","."]
docstr: Perturb `pop_member` in-place.
  
  Replaces a word at a random in `pop_member` with replacement word that maximizes increase in score.
  Args:
      pop_member (PopulationMember): The population member being perturbed.
      original_result (GoalFunctionResult): Result of original sample being attacked
  Returns:
      `True` if perturbation occured. `False` if not.
docstr_tok: ["Perturb","`","pop_member","`","in-place",".","Replaces","a","word","at","a","random","in","`","pop_member","`","with","replacement","word","that","maximizes","increase","in","score",".","Args",":","pop_member","(","PopulationMember",")",":","The","population","member","being","perturbed",".","original_result","(","GoalFunctionResult",")",":","Result","of","original","sample","being","attacked","Returns",":","`","True","`","if","perturbation","occured",".","`","False","`","if","not","."]
code: def _perturb(self, pop_member, original_result):
      """Perturb `pop_member` in-place.
  
      Replaces a word at a random in `pop_member` with replacement word that maximizes increase in score.
      Args:
          pop_member (PopulationMember): The population member being perturbed.
          original_result (GoalFunctionResult): Result of original sample being attacked
      Returns:
          `True` if perturbation occured. `False` if not.
      """
      # TODO: Below is very slow and is the main cause behind memory build up + slowness
      best_neighbors, prob_list = self._get_best_neighbors(
          pop_member.result, original_result
      )
      random_result = np.random.choice(best_neighbors, 1, p=prob_list)[
          0
      ]
  
      if random_result == pop_member.result:
          return False
      else:
          pop_member.attacked_text = random_result.attacked_text
          pop_member.result = random_result
          return True
  
file_path: TextAttack/textattack/search_methods/particle_swarm_optimization.py
imports: import copy
  import numpy
  from textattack.goal_function_results import GoalFunctionResultStatus
  from textattack.search_methods import PopulationBasedSearch, PopulationMember
  from textattack.shared import utils
  from textattack.shared.validators import transformation_consists_of_word_swaps
label_desc: Use image node_id 2 for calling the ParticleSwarmOptimization obj's underlying member method code with example usage: obj._perturb(pop_member, original_result) and returns: False, True