

import argparse
import copy






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__      )  ->					int:
	__lowerCamelCase    :    Dict 	=			{}

	with open(lowerCamelCase__      ) as f:
		for line in f:
			if line.split()[0] not in dict_of_neighbours:
				__lowerCamelCase    :    Optional[Any] 	=			[]
				_list.append([line.split()[1], line.split()[2]]      )
				__lowerCamelCase    :    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:
				__lowerCamelCase    :    List[Any] 	=			[]
				_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        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__      )  ->					List[str]:







	with open(lowerCamelCase__      ) as f:
		__lowerCamelCase    :    Union[str, Any] 	=			f.read(1      )
	__lowerCamelCase    :    Optional[Any] 	=			start_node

	__lowerCamelCase    :    Dict 	=			[]

	__lowerCamelCase    :    List[str] 	=			start_node

	__lowerCamelCase    :    Any 	=			0
	while visiting not in first_solution:
		__lowerCamelCase    :    Tuple 	=			1_0_0_0_0
		for k in dict_of_neighbours[visiting]:
			if int(k[1]      ) < int(lowerCamelCase__      ) and k[0] not in first_solution:
				__lowerCamelCase    :    Optional[Any] 	=			k[1]
				__lowerCamelCase    :    str 	=			k[0]

		first_solution.append(lowerCamelCase__      )
		__lowerCamelCase    :    Any 	=			distance_of_first_solution + int(lowerCamelCase__      )
		__lowerCamelCase    :    Tuple 	=			best_node

	first_solution.append(lowerCamelCase__      )

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

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






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__      )  ->					Tuple:
	__lowerCamelCase    :    Optional[Any] 	=			[]

	for n in solution[1:-1]:
		__lowerCamelCase    :    Optional[Any] 	=			solution.index(lowerCamelCase__      )
		for kn in solution[1:-1]:
			__lowerCamelCase    :    Union[str, Any] 	=			solution.index(lowerCamelCase__      )
			if n == kn:
				continue

			__lowerCamelCase    :    Tuple 	=			copy.deepcopy(lowerCamelCase__      )
			__lowerCamelCase    :    List[str] 	=			kn
			__lowerCamelCase    :    Dict 	=			n

			__lowerCamelCase    :    Optional[Any] 	=			0

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

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

	__lowerCamelCase    :    Tuple 	=			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        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__      )  ->					str:
	__lowerCamelCase    :    Union[str, Any] 	=			1
	__lowerCamelCase    :    Union[str, Any] 	=			first_solution
	__lowerCamelCase    :    Any 	=			[]
	__lowerCamelCase    :    List[str] 	=			distance_of_first_solution
	__lowerCamelCase    :    Any 	=			solution

	while count <= iters:
		__lowerCamelCase    :    List[Any] 	=			find_neighborhood(lowerCamelCase__     ,			lowerCamelCase__      )
		__lowerCamelCase    :    str 	=			0
		__lowerCamelCase    :    Tuple 	=			neighborhood[index_of_best_solution]
		__lowerCamelCase    :    List[str] 	=			len(lowerCamelCase__      ) - 1

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

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

		__lowerCamelCase    :    Tuple 	=			count + 1

	return best_solution_ever, best_cost






def        SCREAMING_SNAKE_CASE__	(  lowerCamelCase__=None      )  ->					int:
	__lowerCamelCase    :    Union[str, Any] 	=			generate_neighbours(args.File      )

	__lowerCamelCase    :    Any 	=			generate_first_solution(
	    args.File     ,			lowerCamelCase__      )

	__lowerCamelCase    :    List[Any] 	=			tabu_search(
	    lowerCamelCase__     ,			lowerCamelCase__     ,			lowerCamelCase__     ,			args.Iterations     ,			args.Size     ,			)

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


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

from collections.abc import Callable
from typing import Generic, TypeVar

a		=TypeVar("""T""")
a		=TypeVar("""U""")






class 	A_		(	Generic[T, U]    ):
							def __init__(		self		:      Tuple       ,SCREAMING_SNAKE_CASE__		:      T | None       ,SCREAMING_SNAKE_CASE__		:      U | None):
								__lowerCamelCase    :    Optional[Any] 	=			key
								__lowerCamelCase    :    Tuple 	=			val
								__lowerCamelCase    :    DoubleLinkedListNode[T, U] | None 	=			None
								__lowerCamelCase    :    DoubleLinkedListNode[T, U] | None 	=			None


							def __repr__(		self		:      Optional[int]):
								return (
								    F"Node: key: {self.key}, val: {self.val}, "
								    F"has next: {bool(self.next)}, has prev: {bool(self.prev)}"
								)






class 	A_		(	Generic[T, U]    ):
							def __init__(		self		:      List[Any]):
								__lowerCamelCase    :    DoubleLinkedListNode[T, U] 	=			DoubleLinkedListNode(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
								__lowerCamelCase    :    DoubleLinkedListNode[T, U] 	=			DoubleLinkedListNode(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
								__lowerCamelCase	,			__lowerCamelCase    :    List[Any] 	=			self.rear, self.head
							def __repr__(		self		:      Optional[int]):
								__lowerCamelCase    :    int 	=			['DoubleLinkedList']
								__lowerCamelCase    :    Tuple 	=			self.head
								while node.next is not None:
									rep.append(str(SCREAMING_SNAKE_CASE__))
									__lowerCamelCase    :    Union[str, Any] 	=			node.next
								rep.append(str(self.rear))
								return ",\n    ".join(SCREAMING_SNAKE_CASE__)
							def    lowerCAmelCase					(		self		:      Optional[Any]       ,SCREAMING_SNAKE_CASE__		:      DoubleLinkedListNode[T, U]):
								__lowerCamelCase    :    str 	=			self.rear.prev

								# All nodes other than self.head are guaranteed to have non-None previous
								assert previous is not None

								__lowerCamelCase    :    Any 	=			node
								__lowerCamelCase    :    str 	=			previous
								__lowerCamelCase    :    str 	=			node
								__lowerCamelCase    :    List[str] 	=			self.rear


							def    lowerCAmelCase					(		self		:      int       ,SCREAMING_SNAKE_CASE__		:      DoubleLinkedListNode[T, U]):

								if node.prev is None or node.next is None:
									return None

								__lowerCamelCase    :    Union[str, Any] 	=			node.next
								__lowerCamelCase    :    Dict 	=			node.prev
								__lowerCamelCase    :    Optional[int] 	=			None
								__lowerCamelCase    :    List[str] 	=			None
								return node






class 	A_		(	Generic[T, U]    ):
							_UpperCAmelCase   :  dict[Callable[[T], U], LRUCache[T, U]]										=				{}
							def __init__(		self		:      str       ,SCREAMING_SNAKE_CASE__		:      int):
								__lowerCamelCase    :    DoubleLinkedList[T, U] 	=			DoubleLinkedList()
								__lowerCamelCase    :    List[Any] 	=			capacity
								__lowerCamelCase    :    Optional[int] 	=			0
								__lowerCamelCase    :    int 	=			0
								__lowerCamelCase    :    Optional[Any] 	=			0
								__lowerCamelCase    :    dict[T, DoubleLinkedListNode[T, U]] 	=			{}
							def __repr__(		self		:      Optional[Any]):
								return (
								    F"CacheInfo(hits={self.hits}, misses={self.miss}, "
								    F"capacity={self.capacity}, current size={self.num_keys})"
								)
							def __contains__(		self		:      List[str]       ,SCREAMING_SNAKE_CASE__		:      T):
								return key in self.cache
							def    lowerCAmelCase					(		self		:      Union[str, Any]       ,SCREAMING_SNAKE_CASE__		:      T):
								# Note: pythonic interface would throw KeyError rather than return None

								if key in self.cache:
									self.hits += 1
									__lowerCamelCase    :    DoubleLinkedListNode[T, U] 	=			self.cache[key]
									__lowerCamelCase    :    List[str] 	=			self.list.remove(self.cache[key])
									assert node == value_node

									# node is guaranteed not None because it is in self.cache
									assert node is not None
									self.list.add(SCREAMING_SNAKE_CASE__)
									return node.val
								self.miss += 1
								return None
							def    lowerCAmelCase					(		self		:      Dict       ,SCREAMING_SNAKE_CASE__		:      T       ,SCREAMING_SNAKE_CASE__		:      U):

								if key not in self.cache:
									if self.num_keys >= self.capacity:
										# delete first node (oldest) when over capacity
										__lowerCamelCase    :    Optional[Any] 	=			self.list.head.next

										# guaranteed to have a non-None first node when num_keys > 0
										# explain to type checker via assertions
										assert first_node is not None
										assert first_node.key is not None
										assert (
										    self.list.remove(SCREAMING_SNAKE_CASE__) is not None
										)  # node guaranteed to be in list assert node.key is not None

										del self.cache[first_node.key]
										self.num_keys -= 1
									__lowerCamelCase    :    List[Any] 	=			DoubleLinkedListNode(SCREAMING_SNAKE_CASE__       ,SCREAMING_SNAKE_CASE__)
									self.list.add(self.cache[key])
									self.num_keys += 1

								else:
									# bump node to the end of the list, update value
									__lowerCamelCase    :    List[str] 	=			self.list.remove(self.cache[key])
									assert node is not None  # node guaranteed to be in list
									__lowerCamelCase    :    List[Any] 	=			value
									self.list.add(SCREAMING_SNAKE_CASE__)


							@classmethod
							def    lowerCAmelCase					(		cls		:      Optional[int]       ,SCREAMING_SNAKE_CASE__		:      int = 1_2_8):

								def cache_decorator_inner(SCREAMING_SNAKE_CASE__		:      Callable[[T], U]) -> Callable[..., U]:
									def cache_decorator_wrapper(*SCREAMING_SNAKE_CASE__		:      T) -> U:
										if func not in cls.decorator_function_to_instance_map:
											__lowerCamelCase    :    str 	=			LRUCache(SCREAMING_SNAKE_CASE__)

										__lowerCamelCase    :    Dict 	=			cls.decorator_function_to_instance_map[func].get(args[0])
										if result is None:
											__lowerCamelCase    :    str 	=			func(*SCREAMING_SNAKE_CASE__)
											cls.decorator_function_to_instance_map[func].put(args[0]       ,SCREAMING_SNAKE_CASE__)
										return result

									def cache_info() -> LRUCache[T, U]:
										return cls.decorator_function_to_instance_map[func]

									setattr(SCREAMING_SNAKE_CASE__       ,'cache_info'       ,SCREAMING_SNAKE_CASE__)  # noqa: B010

									return cache_decorator_wrapper

								return cache_decorator_inner


if __name__ == "__main__":
					import doctest

					doctest.testmod()




