





import argparse
import copy



def     __lowerCAmelCase						(							snake_case	:    Union[str, Any] )       ->							Tuple:
   __lowerCamelCase:				Dict     =	{}

   with open(snake_case ) as f:
      for line in f:
         if line.split()[0] not in dict_of_neighbours:
            __lowerCamelCase:				Optional[int]     =	[]
            _list.append([line.split()[1], line.split()[2]] )
            __lowerCamelCase:				List[str]     =	_list
         else:
            dict_of_neighbours[line.split()[0]].append(
                [line.split()[1], line.split()[2]] )
         if line.split()[1] not in dict_of_neighbours:
            __lowerCamelCase:				Tuple     =	[]
            _list.append([line.split()[0], line.split()[2]] )
            __lowerCamelCase:				List[Any]     =	_list
         else:
            dict_of_neighbours[line.split()[1]].append(
                [line.split()[0], line.split()[2]] )

   return dict_of_neighbours



def     __lowerCAmelCase						(							snake_case	:    Dict						,      snake_case	:    Tuple )       ->							int:

   with open(snake_case ) as f:
      __lowerCamelCase:				List[str]     =	f.read(1 )
   __lowerCamelCase:				int     =	start_node

   __lowerCamelCase:				str     =	[]

   __lowerCamelCase:				Optional[int]     =	start_node

   __lowerCamelCase:				Any     =	0
   while visiting not in first_solution:
      __lowerCamelCase:				Optional[Any]     =	10000
      for k in dict_of_neighbours[visiting]:
         if int(k[1] ) < int(snake_case ) and k[0] not in first_solution:
            __lowerCamelCase:				Optional[Any]     =	k[1]
            __lowerCamelCase:				Union[str, Any]     =	k[0]

      first_solution.append(snake_case )
      __lowerCamelCase:				List[Any]     =	distance_of_first_solution + int(snake_case )
      __lowerCamelCase:				Tuple     =	best_node

   first_solution.append(snake_case )

   __lowerCamelCase:				Optional[Any]     =	0
   for k in dict_of_neighbours[first_solution[-2]]:
      if k[0] == start_node:
         break
      position += 1

   __lowerCamelCase:				Optional[Any]     =	(
       distance_of_first_solution
       + int(dict_of_neighbours[first_solution[-2]][position][1] )
       - 10000
   )
   return first_solution, distance_of_first_solution



def     __lowerCAmelCase						(							snake_case	:    Optional[int]						,      snake_case	:    str )       ->							str:
   __lowerCamelCase:				Dict     =	[]

   for n in solution[1:-1]:
      __lowerCamelCase:				int     =	solution.index(snake_case )
      for kn in solution[1:-1]:
         __lowerCamelCase:				int     =	solution.index(snake_case )
         if n == kn:
            continue

         __lowerCamelCase:				str     =	copy.deepcopy(snake_case )
         __lowerCamelCase:				List[Any]     =	kn
         __lowerCamelCase:				Optional[Any]     =	n

         __lowerCamelCase:				Dict     =	0

         for k in _tmp[:-1]:
            __lowerCamelCase:				Dict     =	_tmp[_tmp.index(snake_case ) + 1]
            for i in dict_of_neighbours[k]:
               if i[0] == next_node:
                  __lowerCamelCase:				Tuple     =	distance + int(i[1] )
         _tmp.append(snake_case )

         if _tmp not in neighborhood_of_solution:
            neighborhood_of_solution.append(_tmp )

   __lowerCamelCase:				Optional[int]     =	len(neighborhood_of_solution[0] ) - 1

   neighborhood_of_solution.sort(key=lambda snake_case : x[index_of_last_item_in_the_list] )
   return neighborhood_of_solution



def     __lowerCAmelCase						(							snake_case	:    Any						,      snake_case	:    Optional[Any]						,      snake_case	:    int						,      snake_case	:    Optional[int]						,      snake_case	:    Union[str, Any] )       ->							List[Any]:
   __lowerCamelCase:				Union[str, Any]     =	1
   __lowerCamelCase:				str     =	first_solution
   __lowerCamelCase:				Union[str, Any]     =	[]
   __lowerCamelCase:				Optional[int]     =	distance_of_first_solution
   __lowerCamelCase:				Union[str, Any]     =	solution

   while count <= iters:
      __lowerCamelCase:				List[str]     =	find_neighborhood(snake_case						,      snake_case )
      __lowerCamelCase:				Optional[Any]     =	0
      __lowerCamelCase:				Union[str, Any]     =	neighborhood[index_of_best_solution]
      __lowerCamelCase:				Dict     =	len(snake_case ) - 1

      __lowerCamelCase:				List[Any]     =	False
      while not found:
         __lowerCamelCase:				Union[str, Any]     =	0
         while i < len(snake_case ):
            if best_solution[i] != solution[i]:
               __lowerCamelCase:				List[Any]     =	best_solution[i]
               __lowerCamelCase:				Union[str, Any]     =	solution[i]
               break
            __lowerCamelCase:				str     =	i + 1

         if [first_exchange_node, second_exchange_node] not in tabu_list and [
             second_exchange_node,
             first_exchange_node,
         ] not in tabu_list:
            tabu_list.append([first_exchange_node, second_exchange_node] )
            __lowerCamelCase:				List[str]     =	True
            __lowerCamelCase:				Dict     =	best_solution[:-1]
            __lowerCamelCase:				Dict     =	neighborhood[index_of_best_solution][best_cost_index]
            if cost < best_cost:
               __lowerCamelCase:				Dict     =	cost
               __lowerCamelCase:				Union[str, Any]     =	solution
         else:
            __lowerCamelCase:				Dict     =	index_of_best_solution + 1
            __lowerCamelCase:				Union[str, Any]     =	neighborhood[index_of_best_solution]

      if len(snake_case ) >= size:
         tabu_list.pop(0 )

      __lowerCamelCase:				Optional[int]     =	count + 1

   return best_solution_ever, best_cost



def     __lowerCAmelCase						(							snake_case	:    Union[str, Any]=None )       ->							Optional[Any]:
   __lowerCamelCase:				Union[str, Any]     =	generate_neighbours(args.File )

   __lowerCamelCase   ,     __lowerCamelCase:				List[str]     =	generate_first_solution(
       args.File						,      snake_case )

   __lowerCamelCase   ,     __lowerCamelCase:				List[str]     =	tabu_search(
       snake_case						,      snake_case						,      snake_case						,      args.Iterations						,      args.Size						,      )

   print(f'Best solution: {best_sol}, with total distance: {best_cost}.' )


if __name__ == "__main__":
   _A		:				Any									=       argparse.ArgumentParser(description='''Tabu Search''')
   parser.add_argument(
       '''-f''',
       '''--File''',
       type=str,
       help='''Path to the file containing the data''',
       required=True,
   )
   parser.add_argument(
       '''-i''',
       '''--Iterations''',
       type=int,
       help='''How many iterations the algorithm should perform''',
       required=True,
   )
   parser.add_argument(
       '''-s''', '''--Size''', type=int, help='''Size of the tabu list''', required=True
   )

   # Pass the arguments to main method
   main(parser.parse_args())






from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers.models.distilbert.modeling_tf_distilbert import (
       TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFDistilBertForMaskedLM,
       TFDistilBertForMultipleChoice,
       TFDistilBertForQuestionAnswering,
       TFDistilBertForSequenceClassification,
       TFDistilBertForTokenClassification,
       TFDistilBertModel,
   )



class        a					:





     def __init__(    self					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				str    ,	):
        __lowerCamelCase:				Optional[int]     =	parent
        __lowerCamelCase:				Dict     =	13
        __lowerCamelCase:				List[Any]     =	7
        __lowerCamelCase:				Optional[int]     =	True
        __lowerCamelCase:				str     =	True
        __lowerCamelCase:				Dict     =	False
        __lowerCamelCase:				Union[str, Any]     =	True
        __lowerCamelCase:				Any     =	99
        __lowerCamelCase:				Dict     =	32
        __lowerCamelCase:				str     =	2
        __lowerCamelCase:				Optional[int]     =	4
        __lowerCamelCase:				Optional[Any]     =	37
        __lowerCamelCase:				Union[str, Any]     =	"""gelu"""
        __lowerCamelCase:				List[Any]     =	0.1
        __lowerCamelCase:				Dict     =	0.1
        __lowerCamelCase:				str     =	512
        __lowerCamelCase:				Dict     =	16
        __lowerCamelCase:				List[str]     =	2
        __lowerCamelCase:				Optional[int]     =	0.02
        __lowerCamelCase:				Dict     =	3
        __lowerCamelCase:				Optional[int]     =	4
        __lowerCamelCase:				Tuple     =	None





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size							)

        __lowerCamelCase:				Any     =	None
        if self.use_input_mask:
           __lowerCamelCase:				Optional[int]     =	random_attention_mask([self.batch_size, self.seq_length]							)

        __lowerCamelCase:				Optional[Any]     =	None
        __lowerCamelCase:				Optional[Any]     =	None
        __lowerCamelCase:				List[str]     =	None
        if self.use_labels:
           __lowerCamelCase:				Any     =	ids_tensor([self.batch_size]    ,	self.type_sequence_label_size							)
           __lowerCamelCase:				List[Any]     =	ids_tensor([self.batch_size, self.seq_length]    ,	self.num_labels							)
           __lowerCamelCase:				Optional[int]     =	ids_tensor([self.batch_size]    ,	self.num_choices							)

        __lowerCamelCase:				Any     =	DistilBertConfig(
            vocab_size=self.vocab_size    ,	dim=self.hidden_size    ,	n_layers=self.num_hidden_layers    ,	n_heads=self.num_attention_heads    ,	hidden_dim=self.intermediate_size    ,	hidden_act=self.hidden_act    ,	dropout=self.hidden_dropout_prob    ,	attention_dropout=self.attention_probs_dropout_prob    ,	max_position_embeddings=self.max_position_embeddings    ,	initializer_range=self.initializer_range    ,	)

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				List[str]							):
        __lowerCamelCase:				str     =	TFDistilBertModel(config=SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				List[Any]     =	{"""input_ids""": input_ids, """attention_mask""": input_mask}

        __lowerCamelCase:				Optional[Any]     =	model(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				int     =	[input_ids, input_mask]

        __lowerCamelCase:				str     =	model(SCREAMING_SNAKE_CASE_							)

        self.parent.assertEqual(result.last_hidden_state.shape    ,	(self.batch_size, self.seq_length, self.hidden_size)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				str    ,	SCREAMING_SNAKE_CASE_					:				List[Any]    ,	SCREAMING_SNAKE_CASE_					:				Dict							):
        __lowerCamelCase:				Any     =	TFDistilBertForMaskedLM(config=SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Dict     =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
        __lowerCamelCase:				str     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.vocab_size)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Tuple    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]							):
        __lowerCamelCase:				Optional[int]     =	TFDistilBertForQuestionAnswering(config=SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Tuple     =	{
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
        }
        __lowerCamelCase:				Tuple     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.start_logits.shape    ,	(self.batch_size, self.seq_length)							)
        self.parent.assertEqual(result.end_logits.shape    ,	(self.batch_size, self.seq_length)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]							):
        __lowerCamelCase:				Any     =	self.num_labels
        __lowerCamelCase:				Optional[Any]     =	TFDistilBertForSequenceClassification(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Dict     =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
        __lowerCamelCase:				Optional[int]     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_labels)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				int    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				int    ,	SCREAMING_SNAKE_CASE_					:				Dict							):
        __lowerCamelCase:				Tuple     =	self.num_choices
        __lowerCamelCase:				Optional[int]     =	TFDistilBertForMultipleChoice(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Optional[Any]     =	tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_    ,	1							)    ,	(1, self.num_choices, 1)							)
        __lowerCamelCase:				str     =	tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_    ,	1							)    ,	(1, self.num_choices, 1)							)
        __lowerCamelCase:				List[Any]     =	{
            """input_ids""": multiple_choice_inputs_ids,
            """attention_mask""": multiple_choice_input_mask,
        }
        __lowerCamelCase:				int     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.num_choices)							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Dict    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[Any]    ,	SCREAMING_SNAKE_CASE_					:				Optional[int]    ,	SCREAMING_SNAKE_CASE_					:				List[str]    ,	SCREAMING_SNAKE_CASE_					:				Any    ,	SCREAMING_SNAKE_CASE_					:				Union[str, Any]							):
        __lowerCamelCase:				Union[str, Any]     =	self.num_labels
        __lowerCamelCase:				Optional[int]     =	TFDistilBertForTokenClassification(SCREAMING_SNAKE_CASE_							)
        __lowerCamelCase:				Optional[Any]     =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
        __lowerCamelCase:				str     =	model(SCREAMING_SNAKE_CASE_							)
        self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.num_labels)							)







     def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple							):
        __lowerCamelCase:				Dict     =	self.prepare_config_and_inputs()
        ((__lowerCamelCase)   ,     (__lowerCamelCase)   ,     (__lowerCamelCase)   ,     (__lowerCamelCase)   ,     (__lowerCamelCase)   ,     (__lowerCamelCase)):				Tuple     =	config_and_inputs
        __lowerCamelCase:				Dict     =	{"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict



@require_tf
class        a					(			_UpperCAmelCase     ,_UpperCAmelCase     ,unittest.TestCase   ):
     UpperCAmelCase__ :  Any						      =	(
         (
             TFDistilBertModel,
             TFDistilBertForMaskedLM,
             TFDistilBertForQuestionAnswering,
             TFDistilBertForSequenceClassification,
             TFDistilBertForTokenClassification,
             TFDistilBertForMultipleChoice,
         )
         if is_tf_available()
         else None
     )
     UpperCAmelCase__ :  Dict						      =	(
         {
             "feature-extraction": TFDistilBertModel,
             "fill-mask": TFDistilBertForMaskedLM,
             "question-answering": TFDistilBertForQuestionAnswering,
             "text-classification": TFDistilBertForSequenceClassification,
             "token-classification": TFDistilBertForTokenClassification,
             "zero-shot": TFDistilBertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     UpperCAmelCase__ :  Optional[Any]						      =	False
     UpperCAmelCase__ :  Tuple						      =	False





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
        __lowerCamelCase:				List[str]     =	TFDistilBertModelTester(self							)
        __lowerCamelCase:				Union[str, Any]     =	ConfigTester(self    ,	config_class=SCREAMING_SNAKE_CASE_    ,	dim=37							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
        self.config_tester.run_common_tests()





     def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]							):
        __lowerCamelCase:				Dict     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_model(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				List[Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_masked_lm(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]							):
        __lowerCamelCase:				Tuple     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_question_answering(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				List[str]							):
        __lowerCamelCase:				List[Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_sequence_classification(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[Any]							):
        __lowerCamelCase:				List[Any]     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_multiple_choice(*SCREAMING_SNAKE_CASE_							)





     def  SCREAMING_SNAKE_CASE__       (    self					:				str							):
        __lowerCamelCase:				int     =	self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_token_classification(*SCREAMING_SNAKE_CASE_							)







     @slow
     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]							):
           __lowerCamelCase:				List[str]     =	TFDistilBertModel.from_pretrained(SCREAMING_SNAKE_CASE_							)
           self.assertIsNotNone(SCREAMING_SNAKE_CASE_							)



@require_tf
class        a					(			unittest.TestCase   ):





     @slow
     def  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]							):
        __lowerCamelCase:				int     =	TFDistilBertModel.from_pretrained("""distilbert-base-uncased"""							)
        __lowerCamelCase:				Optional[Any]     =	tf.constant([[0, 1, 2, 3, 4, 5]]							)
        __lowerCamelCase:				Any     =	model(SCREAMING_SNAKE_CASE_							)[0]

        __lowerCamelCase:				Union[str, Any]     =	[1, 6, 768]
        self.assertEqual(output.shape    ,	SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	tf.constant(
            [
                [
                    [0.1926_1885, -0.1373_2955, 0.411_9799],
                    [0.2215_0156, -0.0742_2661, 0.3903_7204],
                    [0.2275_6018, -0.089_6414, 0.370_1467],
                ]
            ]							)
        tf.debugging.assert_near(output[:, :3, :3]    ,	SCREAMING_SNAKE_CASE_    ,	atol=1E-4							)


