


"""simple docstring"""





import argparse
import copy





def        a_  (   _lowerCAmelCase						:				List[str]  ):





       '''simple docstring'''

       lowercase__    :	int     					=			{}

       with open(_lowerCAmelCase  ) 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__    :	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__    :	List[Any]     					=			[]
                            _list.append([line.split()[0], line.split()[2]]  )
                            lowercase__    :	Union[str, Any]     					=			_list
                     else:
                            dict_of_neighbours[line.split()[1]].append(
                                [line.split()[0], line.split()[2]]  )

       return dict_of_neighbours





def        a_  (   _lowerCAmelCase						:				Optional[int]     ,				_lowerCAmelCase						:				List[Any]  ):





       '''simple docstring'''

       with open(_lowerCAmelCase  ) as f:
              lowercase__    :	Optional[int]     					=			f.read(1  )
       lowercase__    :	List[Any]     					=			start_node

       lowercase__    :	List[Any]     					=			[]

       lowercase__    :	str     					=			start_node

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

              first_solution.append(_lowerCAmelCase  )
              lowercase__    :	Any     					=			distance_of_first_solution + int(_lowerCAmelCase  )
              lowercase__    :	Optional[int]     					=			best_node

       first_solution.append(_lowerCAmelCase  )

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

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





def        a_  (   _lowerCAmelCase						:				str     ,				_lowerCAmelCase						:				Any  ):





       '''simple docstring'''

       lowercase__    :	Tuple     					=			[]

       for n in solution[1:-1]:
              lowercase__    :	Dict     					=			solution.index(_lowerCAmelCase  )
              for kn in solution[1:-1]:
                     lowercase__    :	Tuple     					=			solution.index(_lowerCAmelCase  )
                     if n == kn:
                            continue

                     lowercase__    :	Union[str, Any]     					=			copy.deepcopy(_lowerCAmelCase  )
                     lowercase__    :	Optional[int]     					=			kn
                     lowercase__    :	List[Any]     					=			n

                     lowercase__    :	List[Any]     					=			0

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

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

       lowercase__    :	Union[str, Any]     					=			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        a_  (   _lowerCAmelCase						:				Any     ,				_lowerCAmelCase						:				str     ,				_lowerCAmelCase						:				List[Any]     ,				_lowerCAmelCase						:				Tuple     ,				_lowerCAmelCase						:				Dict  ):





       '''simple docstring'''

       lowercase__    :	str     					=			1
       lowercase__    :	List[Any]     					=			first_solution
       lowercase__    :	Any     					=			[]
       lowercase__    :	str     					=			distance_of_first_solution
       lowercase__    :	str     					=			solution

       while count <= iters:
              lowercase__    :	Union[str, Any]     					=			find_neighborhood(_lowerCAmelCase     ,				_lowerCAmelCase  )
              lowercase__    :	Dict     					=			0
              lowercase__    :	int     					=			neighborhood[index_of_best_solution]
              lowercase__    :	Optional[int]     					=			len(_lowerCAmelCase  ) - 1

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

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

              lowercase__    :	Optional[int]     					=			count + 1

       return best_solution_ever, best_cost





def        a_  (   _lowerCAmelCase						:				str=None  ):





       '''simple docstring'''

       lowercase__    :	List[str]     					=			generate_neighbours(args.File  )

       lowercase__    :	Optional[Any]     					=			generate_first_solution(
           args.File     ,				_lowerCAmelCase  )

       lowercase__    :	int     					=			tabu_search(
           _lowerCAmelCase     ,				_lowerCAmelCase     ,				_lowerCAmelCase     ,				args.Iterations     ,				args.Size     ,				)

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


if __name__ == "__main__":
       _UpperCamelCase     :		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 numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor





def        a_  (   _lowerCAmelCase						:				dict  ):





       '''simple docstring'''

       return (data["data"], data["target"])





def        a_  (   _lowerCAmelCase						:				np.ndarray     ,				_lowerCAmelCase						:				np.ndarray     ,				_lowerCAmelCase						:				np.ndarray  ):





       '''simple docstring'''

       lowercase__    :	Any     					=			XGBRegressor(verbosity=0     ,				random_state=42  )
       xgb.fit(_lowerCAmelCase     ,				_lowerCAmelCase  )
       # Predict target for test data
       lowercase__    :	str     					=			xgb.predict(_lowerCAmelCase  )
       lowercase__    :	Union[str, Any]     					=			predictions.reshape(len(_lowerCAmelCase  )     ,				1  )
       return predictions





def        a_  (   ):





       '''simple docstring'''

       lowercase__    :	Optional[Any]     					=			fetch_california_housing()
       lowercase__					,   lowercase__    :	str     					=			data_handling(_lowerCAmelCase  )
       lowercase__					,   lowercase__					,   lowercase__					,   lowercase__    :	str     					=			train_test_split(
           _lowerCAmelCase     ,				_lowerCAmelCase     ,				test_size=0.2_5     ,				random_state=1  )
       lowercase__    :	Any     					=			xgboost(_lowerCAmelCase     ,				_lowerCAmelCase     ,				_lowerCAmelCase  )
       # Error printing
       print(f"""Mean Absolute Error : {mean_absolute_error(_lowerCAmelCase     ,				_lowerCAmelCase  )}"""  )
       print(f"""Mean Square Error  : {mean_squared_error(_lowerCAmelCase     ,				_lowerCAmelCase  )}"""  )


if __name__ == "__main__":
       import doctest

       doctest.testmod(verbose=True)
       main()

