
"""simple docstring"""




import argparse
import copy


def   __magic_name__	(      __snake_case				:       Any  ) ->				List[str]:
       lowercase    :				Union[str, Any]       		=							{}

       with open(__SCREAMING_SNAKE_CASE  ) as f:
              for line in f:
                     if line.split()[0] not in dict_of_neighbours:
                            lowercase    :				Optional[int]       		=							[]
                            _list.append([line.split()[1], line.split()[2]]  )
                            lowercase    :				Tuple       		=							_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    :				int       		=							[]
                            _list.append([line.split()[0], line.split()[2]]  )
                            lowercase    :				Any       		=							_list
                     else:
                            dict_of_neighbours[line.split()[1]].append(
                                [line.split()[0], line.split()[2]]  )

       return dict_of_neighbours


def   __magic_name__	(      __snake_case				:       Optional[int]     , __snake_case				:       Optional[Any]  ) ->				Union[str, Any]:

       with open(__SCREAMING_SNAKE_CASE  ) as f:
              lowercase    :				List[str]       		=							f.read(1  )
       lowercase    :				str       		=							start_node

       lowercase    :				Optional[Any]       		=							[]

       lowercase    :				Union[str, Any]       		=							start_node

       lowercase    :				Optional[Any]       		=							0
       while visiting not in first_solution:
              lowercase    :				List[str]       		=							1_0000
              for k in dict_of_neighbours[visiting]:
                     if int(k[1]  ) < int(__SCREAMING_SNAKE_CASE  ) and k[0] not in first_solution:
                            lowercase    :				List[str]       		=							k[1]
                            lowercase    :				Union[str, Any]       		=							k[0]

              first_solution.append(__SCREAMING_SNAKE_CASE  )
              lowercase    :				List[Any]       		=							distance_of_first_solution + int(__SCREAMING_SNAKE_CASE  )
              lowercase    :				Union[str, Any]       		=							best_node

       first_solution.append(__SCREAMING_SNAKE_CASE  )

       lowercase    :				Union[str, Any]       		=							0
       for k in dict_of_neighbours[first_solution[-2]]:
              if k[0] == start_node:
                     break
              position += 1

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


def   __magic_name__	(      __snake_case				:       Optional[Any]     , __snake_case				:       List[str]  ) ->				Tuple:
       lowercase    :				Any       		=							[]

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

                     lowercase    :				Union[str, Any]       		=							copy.deepcopy(__SCREAMING_SNAKE_CASE  )
                     lowercase    :				str       		=							kn
                     lowercase    :				Dict       		=							n

                     lowercase    :				List[str]       		=							0

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

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

       lowercase    :				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   __magic_name__	(      __snake_case				:       Optional[Any]     , __snake_case				:       Dict     , __snake_case				:       str     , __snake_case				:       Union[str, Any]     , __snake_case				:       List[Any]  ) ->				Optional[int]:
       lowercase    :				Union[str, Any]       		=							1
       lowercase    :				int       		=							first_solution
       lowercase    :				List[str]       		=							[]
       lowercase    :				Union[str, Any]       		=							distance_of_first_solution
       lowercase    :				Optional[int]       		=							solution

       while count <= iters:
              lowercase    :				Dict       		=							find_neighborhood(__SCREAMING_SNAKE_CASE     , __SCREAMING_SNAKE_CASE  )
              lowercase    :				Tuple       		=							0
              lowercase    :				str       		=							neighborhood[index_of_best_solution]
              lowercase    :				Optional[Any]       		=							len(__SCREAMING_SNAKE_CASE  ) - 1

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

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

              lowercase    :				str       		=							count + 1

       return best_solution_ever, best_cost


def   __magic_name__	(      __snake_case				:       List[str]=None  ) ->				List[str]:
       lowercase    :				Any       		=							generate_neighbours(args.File  )

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

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

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


if __name__ == "__main__":
   _A			:       List[str]   		=       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())



"""simple docstring"""







import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
       from PIL import Image
else:







       class        UpperCamelCase		:
          @staticmethod
          def 			_UpperCAmelCase   (			*__UpperCamelCase     ,**__UpperCamelCase			)	->      Union[str, Any]:


               '''simple docstring'''


               pass







@is_pipeline_test
@require_vision
@require_torch
class        UpperCamelCase		(			unittest.TestCase  ):
   lowercase							=  MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase     ,__UpperCamelCase			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				int													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	[
            {
                'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                'candidate_labels': ['cat', 'remote', 'couch'],
            }
        ]
        return object_detector, examples
   def 			_UpperCAmelCase   (			self     ,__UpperCamelCase     ,__UpperCamelCase			)	->      str:


        '''simple docstring'''


        lowercase_	:				Tuple													=	object_detector(examples[0]     ,threshold=0.0			)

        lowercase_	:				Any													=	len(__UpperCamelCase			)
        self.assertGreater(__UpperCamelCase     ,0			)
        self.assertEqual(
            __UpperCamelCase     ,[
                {
                    'score': ANY(__UpperCamelCase			),
                    'label': ANY(__UpperCamelCase			),
                    'box': {'xmin': ANY(__UpperCamelCase			), 'ymin': ANY(__UpperCamelCase			), 'xmax': ANY(__UpperCamelCase			), 'ymax': ANY(__UpperCamelCase			)},
                }
                for i in range(__UpperCamelCase			)
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Optional[Any]:


        '''simple docstring'''


        pass
   @require_torch
   def 			_UpperCAmelCase   (			self			)	->      int:


        '''simple docstring'''


        lowercase_	:				List[Any]													=	pipeline(
            'zero-shot-object-detection'     ,model='hf-internal-testing/tiny-random-owlvit-object-detection'			)

        lowercase_	:				Optional[int]													=	object_detector(
            './tests/fixtures/tests_samples/COCO/000000039769.png'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                }
            ]     ,threshold=0.64     ,)

        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.7235, 'label': 'cat', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7218, 'label': 'remote', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.7184, 'label': 'couch', 'box': {'xmin': 204, 'ymin': 167, 'xmax': 232, 'ymax': 190}},
                    {'score': 0.6748, 'label': 'remote', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6656, 'label': 'cat', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6614, 'label': 'couch', 'box': {'xmin': 571, 'ymin': 83, 'xmax': 598, 'ymax': 103}},
                    {'score': 0.6456, 'label': 'remote', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                    {'score': 0.642, 'label': 'remote', 'box': {'xmin': 67, 'ymin': 274, 'xmax': 93, 'ymax': 297}},
                    {'score': 0.6419, 'label': 'cat', 'box': {'xmin': 494, 'ymin': 105, 'xmax': 521, 'ymax': 127}},
                ]
            ]     ,)
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Tuple:


        '''simple docstring'''


        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[str]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
            ]     ,)

        lowercase_	:				Union[str, Any]													=	object_detector(
            [
                {
                    'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                },
                {
                    'image': 'http://images.cocodataset.org/val2017/000000039769.jpg',
                    'candidate_labels': ['cat', 'remote', 'couch'],
                },
            ]     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
                [
                    {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                    {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                    {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
                    {'score': 0.1474, 'label': 'remote', 'box': {'xmin': 335, 'ymin': 74, 'xmax': 371, 'ymax': 187}},
                    {'score': 0.1208, 'label': 'couch', 'box': {'xmin': 4, 'ymin': 0, 'xmax': 642, 'ymax': 476}},
                ],
            ]     ,)
   @require_tf
   @unittest.skip('Zero Shot Object Detection not implemented in TF'			)
   def 			_UpperCAmelCase   (			self			)	->      Union[str, Any]:


        '''simple docstring'''


        pass
   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Optional[int]:


        '''simple docstring'''


        lowercase_	:				Union[str, Any]													=	0.2
        lowercase_	:				Any													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				str													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,threshold=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
                {'score': 0.2537, 'label': 'cat', 'box': {'xmin': 1, 'ymin': 55, 'xmax': 315, 'ymax': 472}},
            ]     ,)







   @require_torch
   @slow
   def 			_UpperCAmelCase   (			self			)	->      Any:


        '''simple docstring'''


        lowercase_	:				Tuple													=	2
        lowercase_	:				Optional[Any]													=	pipeline('zero-shot-object-detection'			)

        lowercase_	:				List[Any]													=	object_detector(
            'http://images.cocodataset.org/val2017/000000039769.jpg'     ,candidate_labels=['cat', 'remote', 'couch']     ,top_k=__UpperCamelCase     ,)
        self.assertEqual(
            nested_simplify(__UpperCamelCase     ,decimals=4			)     ,[
                {'score': 0.2868, 'label': 'cat', 'box': {'xmin': 324, 'ymin': 20, 'xmax': 640, 'ymax': 373}},
                {'score': 0.277, 'label': 'remote', 'box': {'xmin': 40, 'ymin': 72, 'xmax': 177, 'ymax': 115}},
            ]     ,)

