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: For given current text, find its neighboring texts that yields
  maximum improvement (in goal function score) for each word.
  
  Args:
      current_result (GoalFunctionResult): `GoalFunctionResult` of current text
      original_result (GoalFunctionResult): `GoalFunctionResult` of original text.
  Returns:
      best_neighbors (list[GoalFunctionResult]): Best neighboring text for each word
      prob_list (list[float]): discrete probablity distribution for sampling a neighbor from `best_neighbors`
docstr_tok: ["For","given","current","text",",","find","its","neighboring","texts","that","yields","maximum","improvement","(","in","goal","function","score",")","for","each","word",".","Args",":","current_result","(","GoalFunctionResult",")",":","`","GoalFunctionResult","`","of","current","text","original_result","(","GoalFunctionResult",")",":","`","GoalFunctionResult","`","of","original","text",".","Returns",":","best_neighbors","(","list","[","GoalFunctionResult","]",")",":","Best","neighboring","text","for","each","word","prob_list","(","list","[","float","]",")",":","discrete","probablity","distribution","for","sampling","a","neighbor","from","`","best_neighbors","`"]
code: def _get_best_neighbors(self, current_result, original_result):
      """For given current text, find its neighboring texts that yields
      maximum improvement (in goal function score) for each word.
  
      Args:
          current_result (GoalFunctionResult): `GoalFunctionResult` of current text
          original_result (GoalFunctionResult): `GoalFunctionResult` of original text.
      Returns:
          best_neighbors (list[GoalFunctionResult]): Best neighboring text for each word
          prob_list (list[float]): discrete probablity distribution for sampling a neighbor from `best_neighbors`
      """
      current_text = current_result.attacked_text
      neighbors_list = [[] for _ in range(len(current_text.words))]
      transformed_texts = self.get_transformations(
          current_text, original_text=original_result.attacked_text
      )
      for transformed_text in transformed_texts:
          diff_idx = next(
              iter(
                  transformed_text.attack_attrs[
                      "newly_modified_indices"
                  ]
              )
          )
          neighbors_list[diff_idx].append(transformed_text)
  
      best_neighbors = []
      score_list = []
      for i in range(len(neighbors_list)):
          if not neighbors_list[i]:
              best_neighbors.append(current_result)
              score_list.append(0)
              continue
  
          neighbor_results, self._search_over = self.get_goal_results(
              neighbors_list[i]
          )
          if not len(neighbor_results):
              best_neighbors.append(current_result)
              score_list.append(0)
          else:
              neighbor_scores = np.array(
                  [r.score for r in neighbor_results]
              )
              score_diff = neighbor_scores - current_result.score
              best_idx = np.argmax(neighbor_scores)
              best_neighbors.append(neighbor_results[best_idx])
              score_list.append(score_diff[best_idx])
  
      prob_list = normalize(score_list)
  
      return best_neighbors, prob_list
  
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 5 for calling the ParticleSwarmOptimization obj's underlying member method code with example usage: obj._get_best_neighbors(current_result, original_result) and returns: best_neighbors,  prob_list