


'''simple docstring'''







import math
import random
from typing import Any

from .hill_climbing import SearchProblem





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			List[Any],			SCREAMING_SNAKE_CASE__      :			bool = True,			SCREAMING_SNAKE_CASE__      :			float = math.inf,			SCREAMING_SNAKE_CASE__      :			float = -math.inf,			SCREAMING_SNAKE_CASE__      :			float = math.inf,			SCREAMING_SNAKE_CASE__      :			float = -math.inf,			SCREAMING_SNAKE_CASE__      :			bool = False,			SCREAMING_SNAKE_CASE__      :			float = 100,			SCREAMING_SNAKE_CASE__      :			float = 0.01,			SCREAMING_SNAKE_CASE__      :			float = 1,			) ->	Any:
						UpperCAmelCase_			:      Tuple     						=  False
						UpperCAmelCase_			:      Union[str, Any]     						=  search_prob
						UpperCAmelCase_			:      int     						=  start_temperate
						UpperCAmelCase_			:      Optional[Any]     						=  []
						UpperCAmelCase_			:      Tuple     						=  0
						UpperCAmelCase_			:      int     						=  None

						while not search_end:
												UpperCAmelCase_			:      Optional[Any]     						=  current_state.score()
												if best_state is None or current_score > best_state.score():
																		UpperCAmelCase_			:      Union[str, Any]     						=  current_state
												scores.append(SCREAMING_SNAKE_CASE__   )
												iterations += 1
												UpperCAmelCase_			:      Optional[int]     						=  None
												UpperCAmelCase_			:      List[Any]     						=  current_state.get_neighbors()
												while (
												    next_state is None and neighbors
												):  # till we do not find a neighbor that we can move to
																		UpperCAmelCase_			:      Optional[Any]     						=  random.randint(0,			len(SCREAMING_SNAKE_CASE__   ) - 1   )  # picking a random neighbor
																		UpperCAmelCase_			:      Optional[int]     						=  neighbors.pop(SCREAMING_SNAKE_CASE__   )
																		UpperCAmelCase_			:      List[Any]     						=  picked_neighbor.score() - current_score

																		if (
																		    picked_neighbor.x > max_x
																		    or picked_neighbor.x < min_x
																		    or picked_neighbor.y > max_y
																		    or picked_neighbor.y < min_y
																		):
																								continue  # neighbor outside our bounds

																		if not find_max:
																								UpperCAmelCase_			:      List[Any]     						=  change * -1  # in case we are finding minimum
																		if change > 0:  # improves the solution
																								UpperCAmelCase_			:      Tuple     						=  picked_neighbor
																		else:
																								UpperCAmelCase_			:      List[Any]     						=  (math.e) ** (
																								    change / current_temp
																								)  # probability generation function
																								if random.random() < probability:  # random number within probability
																														UpperCAmelCase_			:      str     						=  picked_neighbor
												UpperCAmelCase_			:      Union[str, Any]     						=  current_temp - (current_temp * rate_of_decrease)

												if current_temp < threshold_temp or next_state is None:
																		# temperature below threshold, or could not find a suitable neighbor
																		UpperCAmelCase_			:      Union[str, Any]     						=  True
												else:
																		UpperCAmelCase_			:      Tuple     						=  next_state

						if visualization:
												from matplotlib import pyplot as plt

												plt.plot(range(SCREAMING_SNAKE_CASE__   ),			SCREAMING_SNAKE_CASE__   )
												plt.xlabel('''Iterations'''   )
												plt.ylabel('''Function values'''   )
												plt.show()
						return best_state


if __name__ == "__main__":





					def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			str,			SCREAMING_SNAKE_CASE__      :			List[str]   ) ->	Tuple:
											return (x**2) + (y**2)

					# starting the problem with initial coordinates (12, 47)
					snake_case_     :       Tuple						=  SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
					snake_case_     :       List[Any]						=  simulated_annealing(
					    prob, find_max=False, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True
					)
					print(
					    "The minimum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 "
					    f'''and 50 > y > - 5 found via hill climbing: {local_min.score()}'''
					)

					# starting the problem with initial coordinates (12, 47)
					snake_case_     :       List[str]						=  SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
					snake_case_     :       Tuple						=  simulated_annealing(
					    prob, find_max=True, max_x=1_00, min_x=5, max_y=50, min_y=-5, visualization=True
					)
					print(
					    "The maximum score for f(x, y) = x^2 + y^2 with the domain 100 > x > 5 "
					    f'''and 50 > y > - 5 found via hill climbing: {local_min.score()}'''
					)





					def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			Dict,			SCREAMING_SNAKE_CASE__      :			Union[str, Any]   ) ->	List[str]:
											return (3 * x**2) - (6 * y)

					snake_case_     :       Dict						=  SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
					snake_case_     :       str						=  simulated_annealing(prob, find_max=False, visualization=True)
					print(
					    "The minimum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: "
					    f'''{local_min.score()}'''
					)

					snake_case_     :       List[Any]						=  SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
					snake_case_     :       Optional[int]						=  simulated_annealing(prob, find_max=True, visualization=True)
					print(
					    "The maximum score for f(x, y) = 3*x^2 - 6*y found via hill climbing: "
					    f'''{local_min.score()}'''
					)












'''simple docstring'''







def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			int,			SCREAMING_SNAKE_CASE__      :			int   ) ->	int:
						return abs(SCREAMING_SNAKE_CASE__   ) if a == 0 else greatest_common_divisor(b % a,			SCREAMING_SNAKE_CASE__   )





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			int,			SCREAMING_SNAKE_CASE__      :			int   ) ->	int:
						while y:  # --> when y=0 then loop will terminate and return x as final GCD.
												UpperCAmelCase_						,       UpperCAmelCase_			:      Optional[Any]     						=  y, x % y
						return abs(SCREAMING_SNAKE_CASE__   )





def        lowerCamelCase_      (  ) ->	Optional[int]:
						try:
												UpperCAmelCase_			:      Optional[Any]     						=  input('''Enter two integers separated by comma (,): '''   ).split(''','''   )
												UpperCAmelCase_			:      Optional[int]     						=  int(nums[0]   )
												UpperCAmelCase_			:      List[Any]     						=  int(nums[1]   )
												print(
												    F"""greatest_common_divisor({num_a}, {num_a}) = """
												    F"""{greatest_common_divisor(SCREAMING_SNAKE_CASE__,			SCREAMING_SNAKE_CASE__   )}"""   )
												print(F"""By iterative gcd({num_a}, {num_a}) = {gcd_by_iterative(SCREAMING_SNAKE_CASE__,			SCREAMING_SNAKE_CASE__   )}"""   )
						except (IndexError, UnboundLocalError, ValueError):
												print('''Wrong input'''   )


if __name__ == "__main__":
					main()





