


"""simple docstring"""



import unittest

from transformers import SPIECE_UNDERLINE, ReformerTokenizer, ReformerTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


UpperCAmelCase      		=			get_tests_dir('''fixtures/test_sentencepiece.model''')
@require_sentencepiece
@require_tokenizers
class 					__magic_name__		(			__UpperCAmelCase			,						unittest.TestCase     ):
       __A			:      Optional[Any]        =   ReformerTokenizer
       __A			:      Optional[int]        =   ReformerTokenizerFast
       __A			:      int        =   True
       __A			:      int        =   False
       __A			:      str        =   True






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              super().setUp()

              lowercase		:Optional[Any]       							=				ReformerTokenizer(snake_case__  ,     keep_accents=snake_case__				)
              tokenizer.save_pretrained(self.tmpdirname				)






       def  __snake_case						(   self  :    str				):

              '''simple docstring'''






              lowercase		:Any       							=				'''<s>'''
              lowercase		:List[str]       							=				1

              self.assertEqual(self.get_tokenizer()._convert_token_to_id(snake_case__				)  ,     snake_case__				)
              self.assertEqual(self.get_tokenizer()._convert_id_to_token(snake_case__				)  ,     snake_case__				)






       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				list(self.get_tokenizer().get_vocab().keys()				)

              self.assertEqual(vocab_keys[0]  ,     '''<unk>'''				)
              self.assertEqual(vocab_keys[1]  ,     '''<s>'''				)
              self.assertEqual(vocab_keys[-1]  ,     '''j'''				)
              self.assertEqual(len(snake_case__				)  ,     1_0_0_0				)






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              self.assertEqual(self.get_tokenizer().vocab_size  ,     1_0_0_0				)






       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              if not self.test_rust_tokenizer:
                     return

              lowercase		:Any       							=				self.get_tokenizer()
              lowercase		:List[Any]       							=				self.get_rust_tokenizer()

              lowercase		:Optional[int]       							=				'''I was born in 92000, and this is falsé.'''

              lowercase		:Optional[int]       							=				tokenizer.tokenize(snake_case__				)
              lowercase		:Union[str, Any]       							=				rust_tokenizer.tokenize(snake_case__				)
              self.assertListEqual(snake_case__  ,     snake_case__				)

              lowercase		:List[str]       							=				tokenizer.encode(snake_case__  ,     add_special_tokens=snake_case__				)
              lowercase		:Any       							=				rust_tokenizer.encode(snake_case__  ,     add_special_tokens=snake_case__				)
              self.assertListEqual(snake_case__  ,     snake_case__				)

              lowercase		:str       							=				self.get_rust_tokenizer()
              lowercase		:List[Any]       							=				tokenizer.encode(snake_case__				)
              lowercase		:Union[str, Any]       							=				rust_tokenizer.encode(snake_case__				)
              self.assertListEqual(snake_case__  ,     snake_case__				)






       def  __snake_case						(   self  :    str  ,     snake_case__  :    Tuple=1_5				):

              '''simple docstring'''






              for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
                     with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""				):
                            lowercase		:int       							=				self.rust_tokenizer_class.from_pretrained(snake_case__  ,     **snake_case__				)

                            # Simple input
                            lowercase		:int       							=				'''This is a simple input'''
                            lowercase		:str       							=				['''This is a simple input 1''', '''This is a simple input 2''']
                            lowercase		:List[Any]       							=				('''This is a simple input''', '''This is a pair''')
                            lowercase		:Any       							=				[
                                ('''This is a simple input 1''', '''This is a simple input 2'''),
                                ('''This is a simple pair 1''', '''This is a simple pair 2'''),
                            ]

                            # Simple input tests
                            self.assertRaises(snake_case__  ,     tokenizer_r.encode  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''				)

                            # Simple input
                            self.assertRaises(snake_case__  ,     tokenizer_r.encode_plus  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''				)

                            # Simple input
                            self.assertRaises(
                                snake_case__  ,     tokenizer_r.batch_encode_plus  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''  ,     )

                            # Pair input
                            self.assertRaises(snake_case__  ,     tokenizer_r.encode  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''				)

                            # Pair input
                            self.assertRaises(snake_case__  ,     tokenizer_r.encode_plus  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''				)

                            # Pair input
                            self.assertRaises(
                                snake_case__  ,     tokenizer_r.batch_encode_plus  ,     snake_case__  ,     max_length=snake_case__  ,     padding='''max_length'''  ,     )






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				ReformerTokenizer(snake_case__  ,     keep_accents=snake_case__				)

              lowercase		:Union[str, Any]       							=				tokenizer.tokenize('''This is a test'''				)
              self.assertListEqual(snake_case__  ,     ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']				)

              self.assertListEqual(
                  tokenizer.convert_tokens_to_ids(snake_case__				)  ,     [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]  ,     )

              lowercase		:Optional[Any]       							=				tokenizer.tokenize('''I was born in 92000, and this is falsé.'''				)
              self.assertListEqual(
                  snake_case__  ,     [
                      SPIECE_UNDERLINE + '''I''',
                      SPIECE_UNDERLINE + '''was''',
                      SPIECE_UNDERLINE + '''b''',
                      '''or''',
                      '''n''',
                      SPIECE_UNDERLINE + '''in''',
                      SPIECE_UNDERLINE + '''''',
                      '''9''',
                      '''2''',
                      '''0''',
                      '''0''',
                      '''0''',
                      ''',''',
                      SPIECE_UNDERLINE + '''and''',
                      SPIECE_UNDERLINE + '''this''',
                      SPIECE_UNDERLINE + '''is''',
                      SPIECE_UNDERLINE + '''f''',
                      '''al''',
                      '''s''',
                      '''é''',
                      '''.''',
                  ]  ,     )
              lowercase		:Dict       							=				tokenizer.convert_tokens_to_ids(snake_case__				)
              self.assertListEqual(
                  snake_case__  ,     [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 0, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 0, 4]  ,     )

              lowercase		:Optional[Any]       							=				tokenizer.convert_ids_to_tokens(snake_case__				)
              self.assertListEqual(
                  snake_case__  ,     [
                      SPIECE_UNDERLINE + '''I''',
                      SPIECE_UNDERLINE + '''was''',
                      SPIECE_UNDERLINE + '''b''',
                      '''or''',
                      '''n''',
                      SPIECE_UNDERLINE + '''in''',
                      SPIECE_UNDERLINE + '''''',
                      '''<unk>''',
                      '''2''',
                      '''0''',
                      '''0''',
                      '''0''',
                      ''',''',
                      SPIECE_UNDERLINE + '''and''',
                      SPIECE_UNDERLINE + '''this''',
                      SPIECE_UNDERLINE + '''is''',
                      SPIECE_UNDERLINE + '''f''',
                      '''al''',
                      '''s''',
                      '''<unk>''',
                      '''.''',
                  ]  ,     )






       @cached_property
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              return ReformerTokenizer.from_pretrained('''google/reformer-crime-and-punishment'''				)






       @slow
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:str       							=				'''Hello World!'''
              lowercase		:Optional[int]       							=				[1_2_6, 3_2, 2_6_2, 1_5_2, 3_8, 7_2, 2_8_7]

              self.assertListEqual(snake_case__  ,     self.big_tokenizer.encode(snake_case__				)				)






       @slow
       def  __snake_case						(   self  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:int       							=				(
                  '''This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) " [ ] ! : - . Also we will'''
                  ''' add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth'''
              )
              lowercase		:int       							=				[
                  1_0_8,
                  2_6_5,
                  2_4,
                  1_1_1,
                  4,
                  2_5_8,
                  1_5_6,
                  3_5,
                  2_8,
                  2_7_5,
                  3,
                  2_5_9,
                  2_9_7,
                  2_6_0,
                  8_4,
                  4,
                  3_5,
                  1_1_0,
                  4_4,
                  8,
                  2_5_9,
                  9_1,
                  2_6_8,
                  2_1,
                  1_1,
                  2_0_9,
                  2_7_4,
                  1_0_9,
                  2_6_6,
                  2_7_7,
                  1_1_7,
                  8_6,
                  9_3,
                  3_1_5,
                  2_5_8,
                  2_7_8,
                  2_5_8,
                  2_7_7,
                  2_5_8,
                  0,
                  2_5_8,
                  2_8_8,
                  2_5_8,
                  3_1_9,
                  2_5_8,
                  0,
                  2_5_8,
                  0,
                  2_5_8,
                  0,
                  2_5_8,
                  0,
                  2_5_8,
                  2_8_7,
                  2_5_8,
                  3_1_5,
                  2_5_8,
                  2_8_9,
                  2_5_8,
                  2_7_8,
                  9_9,
                  2_6_9,
                  2_6_6,
                  2_6_2,
                  8,
                  2_5_9,
                  2_4_1,
                  4,
                  2_1_7,
                  2_3_0,
                  2_6_8,
                  2_6_6,
                  5_5,
                  1_6_8,
                  1_0_6,
                  7_5,
                  1_9_3,
                  2_6_6,
                  2_2_3,
                  2_7,
                  4_9,
                  2_6,
                  2_8_2,
                  2_5,
                  2_6_4,
                  2_9_9,
                  1_9,
                  2_6,
                  0,
                  2_5_8,
                  2_7_7,
                  1_1_7,
                  8_6,
                  9_3,
                  1_7_6,
                  1_8_3,
                  2_7_0,
                  1_1,
                  2_6_2,
                  4_2,
                  6_1,
                  2_6_5,
              ]

              self.assertListEqual(snake_case__  ,     self.big_tokenizer.encode(snake_case__				)				)






       @require_torch
       @slow
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              import torch

              from transformers import ReformerConfig, ReformerModel

              # Build sequence
              lowercase		:Any       							=				list(self.big_tokenizer.get_vocab().keys()				)[:1_0]
              lowercase		:int       							=				''' '''.join(snake_case__				)
              lowercase		:Optional[Any]       							=				self.big_tokenizer.encode_plus(snake_case__  ,     return_tensors='''pt'''				)
              lowercase		:List[Any]       							=				self.big_tokenizer.batch_encode_plus([sequence, sequence]  ,     return_tensors='''pt'''				)

              lowercase		:Dict       							=				ReformerConfig()
              # The input gets padded during training so adjust the axial position encodings from the pretrained model value of (512, 1024)
              lowercase		:str       							=				encoded_sequence['''input_ids'''].shape
              lowercase		:Optional[int]       							=				ReformerModel(snake_case__				)

              # Reformer has config.vocab_size == tokenizer.vocab_size == len(tokenizer) - 1 = 320; len(tokenizer) is 321 (including a pad token with id 320)
              assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size

              with torch.no_grad():
                     model(**snake_case__				)
                     model(**snake_case__				)







       @slow
       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				{'''input_ids''': [[1_0_8, 2_6_5, 2_4, 1_1_1, 4, 2_5_8, 1_5_6, 7, 5_1, 2_7_9, 5_8, 7, 7_6, 2_5, 6_9, 2_7_8], [1_4_0, 2_4_3, 2_6_4, 1_3_4, 1_7, 2_6_7, 7_7, 2_6_3, 2_2, 2_6_2, 2_9_7, 2_5_8, 3_0_4, 1_7_7, 2_7_9, 2_6_6, 1_4, 8_9, 1_3, 3_5, 2_6_1, 2_9_9, 2_7_2, 1_3_7, 2_7_5, 2_7_8]], '''attention_mask''': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
              # fmt: on

              # This tokenizer does not know some characters like ")".
              # That is the reason why we use very simple texts here.
              # Also see https://github.com/huggingface/transformers/pull/11737#issuecomment-850769064
              lowercase		:Optional[int]       							=				[
                  '''This is a very simple sentence.''',
                  '''The quick brown fox jumps over the lazy dog.''',
              ]

              self.tokenizer_integration_test_util(
                  expected_encoding=snake_case__  ,     model_name='''google/reformer-crime-and-punishment'''  ,     revision='''0e6c3decb8211d49bf881013425dc8b0448b3f5a'''  ,     padding=snake_case__  ,     sequences=snake_case__  ,     )





"""simple docstring"""



import argparse
import copy






def       lowerCamelCase					(a_						:Union[str, Any])     ->							Tuple:
       lowercase		:Dict       							=				{}

       with open(a_) as f:
              for line in f:
                     if line.split()[0] not in dict_of_neighbours:
                            lowercase		:List[str]       							=				[]
                            _list.append([line.split()[1], line.split()[2]])
                            lowercase		:Any       							=				_list
                     else:
                            dict_of_neighbours[line.split()[0]].append(
                                [line.split()[1], line.split()[2]])
                     if line.split()[1] not in dict_of_neighbours:
                            lowercase		:List[Any]       							=				[]
                            _list.append([line.split()[0], line.split()[2]])
                            lowercase		:Dict       							=				_list
                     else:
                            dict_of_neighbours[line.split()[1]].append(
                                [line.split()[0], line.split()[2]])

       return dict_of_neighbours






def       lowerCamelCase					(a_						:Any				,				a_						:Dict)     ->							Any:

       with open(a_) as f:
              lowercase		:Any       							=				f.read(1)
       lowercase		:Any       							=				start_node

       lowercase		:Any       							=				[]

       lowercase		:Union[str, Any]       							=				start_node

       lowercase		:int       							=				0
       while visiting not in first_solution:
              lowercase		:int       							=				1_0000
              for k in dict_of_neighbours[visiting]:
                     if int(k[1]) < int(a_) and k[0] not in first_solution:
                            lowercase		:str       							=				k[1]
                            lowercase		:str       							=				k[0]

              first_solution.append(a_)
              lowercase		:int       							=				distance_of_first_solution + int(a_)
              lowercase		:Dict       							=				best_node

       first_solution.append(a_)

       lowercase		:int       							=				0
       for k in dict_of_neighbours[first_solution[-2]]:
              if k[0] == start_node:
                     break
              position += 1

       lowercase		:List[str]       							=				(
           distance_of_first_solution
           + int(dict_of_neighbours[first_solution[-2]][position][1])
           - 1_0000
       )
       return first_solution, distance_of_first_solution






def       lowerCamelCase					(a_						:List[str]				,				a_						:str)     ->							str:
       lowercase		:Any       							=				[]

       for n in solution[1:-1]:
              lowercase		:int       							=				solution.index(a_)
              for kn in solution[1:-1]:
                     lowercase		:Union[str, Any]       							=				solution.index(a_)
                     if n == kn:
                            continue

                     lowercase		:int       							=				copy.deepcopy(a_)
                     lowercase		:str       							=				kn
                     lowercase		:List[Any]       							=				n

                     lowercase		:int       							=				0

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

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

       lowercase		:Dict       							=				len(neighborhood_of_solution[0]) - 1

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






def       lowerCamelCase					(a_						:int				,				a_						:Optional[int]				,				a_						:List[Any]				,				a_						:Any				,				a_						:Optional[Any])     ->							List[Any]:
       lowercase		:Union[str, Any]       							=				1
       lowercase		:str       							=				first_solution
       lowercase		:int       							=				[]
       lowercase		:int       							=				distance_of_first_solution
       lowercase		:List[str]       							=				solution

       while count <= iters:
              lowercase		:Optional[Any]       							=				find_neighborhood(a_				,				a_)
              lowercase		:Any       							=				0
              lowercase		:Optional[Any]       							=				neighborhood[index_of_best_solution]
              lowercase		:int       							=				len(a_) - 1

              lowercase		:Dict       							=				False
              while not found:
                     lowercase		:List[str]       							=				0
                     while i < len(a_):
                            if best_solution[i] != solution[i]:
                                   lowercase		:Tuple       							=				best_solution[i]
                                   lowercase		:Tuple       							=				solution[i]
                                   break
                            lowercase		:List[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])
                            lowercase		:Tuple       							=				True
                            lowercase		:Optional[int]       							=				best_solution[:-1]
                            lowercase		:Any       							=				neighborhood[index_of_best_solution][best_cost_index]
                            if cost < best_cost:
                                   lowercase		:Union[str, Any]       							=				cost
                                   lowercase		:Optional[Any]       							=				solution
                     else:
                            lowercase		:Dict       							=				index_of_best_solution + 1
                            lowercase		:int       							=				neighborhood[index_of_best_solution]

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

              lowercase		:int       							=				count + 1

       return best_solution_ever, best_cost






def       lowerCamelCase					(a_						:Tuple=None)     ->							Any:
       lowercase		:Tuple       							=				generate_neighbours(args.File)

       lowercase							,  lowercase		:List[str]       							=				generate_first_solution(
           args.File				,				a_)

       lowercase							,  lowercase		:Union[str, Any]       							=				tabu_search(
           a_				,				a_				,				a_				,				args.Iterations				,				args.Size				,				)

       print(F"""Best solution: {best_sol}, with total distance: {best_cost}.""")


if __name__ == "__main__":
 UpperCAmelCase      		=			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())

