





'''simple docstring'''

import argparse
import copy





def lowerCamelCase						(     __lowerCamelCase :    int    )						->str:
    _SCREAMING_SNAKE_CASE									=			{}

    with open(__lowerCamelCase    ) as f:
        for line in f:
            if line.split()[0] not in dict_of_neighbours:
                _SCREAMING_SNAKE_CASE									=			[]
                _list.append([line.split()[1], line.split()[2]]    )
                _SCREAMING_SNAKE_CASE									=			_list
            else:
                dict_of_neighbours[line.split()[0]].append(
                    [line.split()[1], line.split()[2]]    )
            if line.split()[1] not in dict_of_neighbours:
                _SCREAMING_SNAKE_CASE									=			[]
                _list.append([line.split()[0], line.split()[2]]    )
                _SCREAMING_SNAKE_CASE									=			_list
            else:
                dict_of_neighbours[line.split()[1]].append(
                    [line.split()[0], line.split()[2]]    )

    return dict_of_neighbours





def lowerCamelCase						(     __lowerCamelCase :    List[Any]					,       __lowerCamelCase :    str    )						->Union[str, Any]:

    with open(__lowerCamelCase    ) as f:
        _SCREAMING_SNAKE_CASE									=			f.read(1    )
    _SCREAMING_SNAKE_CASE									=			start_node

    _SCREAMING_SNAKE_CASE									=			[]

    _SCREAMING_SNAKE_CASE									=			start_node

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

        first_solution.append(__lowerCamelCase    )
        _SCREAMING_SNAKE_CASE									=			distance_of_first_solution + int(__lowerCamelCase    )
        _SCREAMING_SNAKE_CASE									=			best_node

    first_solution.append(__lowerCamelCase    )

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

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





def lowerCamelCase						(     __lowerCamelCase :    Union[str, Any]					,       __lowerCamelCase :    List[str]    )						->str:
    _SCREAMING_SNAKE_CASE									=			[]

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

            _SCREAMING_SNAKE_CASE									=			copy.deepcopy(__lowerCamelCase    )
            _SCREAMING_SNAKE_CASE									=			kn
            _SCREAMING_SNAKE_CASE									=			n

            _SCREAMING_SNAKE_CASE									=			0

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

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

    _SCREAMING_SNAKE_CASE									=			len(neighborhood_of_solution[0]    ) - 1

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





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

    while count <= iters:
        _SCREAMING_SNAKE_CASE									=			find_neighborhood(__lowerCamelCase					,       __lowerCamelCase    )
        _SCREAMING_SNAKE_CASE									=			0
        _SCREAMING_SNAKE_CASE									=			neighborhood[index_of_best_solution]
        _SCREAMING_SNAKE_CASE									=			len(__lowerCamelCase    ) - 1

        _SCREAMING_SNAKE_CASE									=			False
        while not found:
            _SCREAMING_SNAKE_CASE									=			0
            while i < len(__lowerCamelCase    ):
                if best_solution[i] != solution[i]:
                    _SCREAMING_SNAKE_CASE									=			best_solution[i]
                    _SCREAMING_SNAKE_CASE									=			solution[i]
                    break
                _SCREAMING_SNAKE_CASE									=			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]    )
                _SCREAMING_SNAKE_CASE									=			True
                _SCREAMING_SNAKE_CASE									=			best_solution[:-1]
                _SCREAMING_SNAKE_CASE									=			neighborhood[index_of_best_solution][best_cost_index]
                if cost < best_cost:
                    _SCREAMING_SNAKE_CASE									=			cost
                    _SCREAMING_SNAKE_CASE									=			solution
            else:
                _SCREAMING_SNAKE_CASE									=			index_of_best_solution + 1
                _SCREAMING_SNAKE_CASE									=			neighborhood[index_of_best_solution]

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

        _SCREAMING_SNAKE_CASE									=			count + 1

    return best_solution_ever, best_cost





def lowerCamelCase						(     __lowerCamelCase :    List[str]=None    )						->Optional[Any]:
    _SCREAMING_SNAKE_CASE									=			generate_neighbours(args.File    )

    _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			generate_first_solution(
        args.File					,       __lowerCamelCase    )

    _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			tabu_search(
        __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       args.Iterations					,       args.Size					,       )

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


if __name__ == "__main__":
      lowercase_					= 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'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowercase_					= {"""configuration_mbart""": ["""MBART_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MBartConfig""", """MBartOnnxConfig"""]}

try:
      if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_					= ["""MBartTokenizer"""]

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_					= ["""MBartTokenizerFast"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_					= [
          """MBART_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """MBartForCausalLM""",
          """MBartForConditionalGeneration""",
          """MBartForQuestionAnswering""",
          """MBartForSequenceClassification""",
          """MBartModel""",
          """MBartPreTrainedModel""",
      ]

try:
      if not is_tf_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_					= [
          """TFMBartForConditionalGeneration""",
          """TFMBartModel""",
          """TFMBartPreTrainedModel""",
      ]

try:
      if not is_flax_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_					= [
          """FlaxMBartForConditionalGeneration""",
          """FlaxMBartForQuestionAnswering""",
          """FlaxMBartForSequenceClassification""",
          """FlaxMBartModel""",
          """FlaxMBartPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_mbart import MBART_PRETRAINED_CONFIG_ARCHIVE_MAP, MBartConfig, MBartOnnxConfig

      try:
            if not is_sentencepiece_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_mbart import MBartTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_mbart_fast import MBartTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mbart import (
                MBART_PRETRAINED_MODEL_ARCHIVE_LIST,
                MBartForCausalLM,
                MBartForConditionalGeneration,
                MBartForQuestionAnswering,
                MBartForSequenceClassification,
                MBartModel,
                MBartPreTrainedModel,
            )

      try:
            if not is_tf_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_tf_mbart import TFMBartForConditionalGeneration, TFMBartModel, TFMBartPreTrainedModel

      try:
            if not is_flax_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_flax_mbart import (
                FlaxMBartForConditionalGeneration,
                FlaxMBartForQuestionAnswering,
                FlaxMBartForSequenceClassification,
                FlaxMBartModel,
                FlaxMBartPreTrainedModel,
            )

else:
      import sys

      lowercase_					= _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



