
'''simple docstring'''


import math

class 		__lowercase			:

			def __init__(self     ,				A     ,				A     ,				A     ,				A ):
								lowerCamelCase_   : Optional[int]       =						x
								lowerCamelCase_   : List[Any]       =						y
								lowerCamelCase_   : List[Any]       =						step_size
								lowerCamelCase_   : Optional[int]       =						function_to_optimize

			def     UpperCAmelCase__	(self ):
								return self.function(self.x     ,				self.y )

			def     UpperCAmelCase__	(self ):
								lowerCamelCase_   : Any       =						self.step_size
								return [
								    SearchProblem(A     ,				A     ,				A     ,				self.function )
								    for x, y in (
								        (self.x - step_size, self.y - step_size),
								        (self.x - step_size, self.y),
								        (self.x - step_size, self.y + step_size),
								        (self.x, self.y - step_size),
								        (self.x, self.y + step_size),
								        (self.x + step_size, self.y - step_size),
								        (self.x + step_size, self.y),
								        (self.x + step_size, self.y + step_size),
								    )
								]

			def __hash__(self ):
								return hash(str(self ) )

			def __eq__(self     ,				A ):
								if isinstance(A     ,				A ):
													return hash(str(self ) ) == hash(str(A ) )
								return False






			def __str__(self ):
								return F"""x: {self.x} y: {self.y}"""




def lowercase_		(				_lowercase     ,    _lowercase = True     ,    _lowercase = math.inf     ,    _lowercase = -math.inf     ,    _lowercase = math.inf     ,    _lowercase = -math.inf     ,    _lowercase = False     ,    _lowercase = 10_000     ,    )       ->							SearchProblem:
					'''simple docstring'''

					lowerCamelCase_   : Any       =						search_prob
					lowerCamelCase_   : List[Any]       =						[]  # list to store the current score at each iteration
					lowerCamelCase_   : str       =						0
					lowerCamelCase_   : Tuple       =						False
					lowerCamelCase_   : str       =						set()
					while not solution_found and iterations < max_iter:
										visited.add(_lowercase			)
										iterations += 1
										lowerCamelCase_   : Optional[Any]       =						current_state.score()
										scores.append(_lowercase			)
										lowerCamelCase_   : List[str]       =						current_state.get_neighbors()
										lowerCamelCase_   : str       =						-math.inf
										lowerCamelCase_   : Optional[int]       =						math.inf
										lowerCamelCase_   : int       =						None  # to hold the next best neighbor
										for neighbor in neighbors:
															if neighbor in visited:
																				continue  # do not want to visit the same state again
															if (
															    neighbor.x > max_x
															    or neighbor.x < min_x
															    or neighbor.y > max_y
															    or neighbor.y < min_y
															):
																				continue  # neighbor outside our bounds
															lowerCamelCase_   : int       =						neighbor.score() - current_score
															if find_max:  # finding max
																				# going to direction with greatest ascent
																				if change > max_change and change > 0:
																									lowerCamelCase_   : Union[str, Any]       =						change
																									lowerCamelCase_   : List[Any]       =						neighbor
															else:  # finding min
																				# to direction with greatest descent
																				if change < min_change and change < 0:
																									lowerCamelCase_   : Union[str, Any]       =						change
																									lowerCamelCase_   : Dict       =						neighbor
										if next_state is not None:
															# we found at least one neighbor which improved the current state
															lowerCamelCase_   : Optional[int]       =						next_state
										else:
															# since we have no neighbor that improves the solution we stop the search
															lowerCamelCase_   : int       =						True

					if visualization:
										from matplotlib import pyplot as plt

										plt.plot(range(_lowercase			)     ,    _lowercase			)
										plt.xlabel('''Iterations'''			)
										plt.ylabel('''Function values'''			)
										plt.show()

					return current_state


if __name__ == "__main__":
					import doctest

					doctest.testmod()




					def lowercase_		(				_lowercase     ,    _lowercase			)       ->							List[Any]:
										'''simple docstring'''

										return (x**2) + (y**2)

					# starting the problem with initial coordinates (3, 4)
					__lowercase							:				Optional[Any]					=       SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
					__lowercase							:				int					=       hill_climbing(prob, find_max=False)
					print(
					    '''The minimum score for f(x, y) = x^2 + y^2 found via hill climbing: '''
					    f'{local_min.score()}'
					)

					# starting the problem with initial coordinates (12, 47)
					__lowercase							:				List[str]					=       SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_fa)
					__lowercase							:				List[str]					=       hill_climbing(
					    prob, find_max=False, max_x=100, 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()}'
					)




					def lowercase_		(				_lowercase     ,    _lowercase			)       ->							Any:
										'''simple docstring'''

										return (3 * x**2) - (6 * y)

					__lowercase							:				Any					=       SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_fa)
					__lowercase							:				Optional[int]					=       hill_climbing(prob, find_max=True)






					print(
					    '''The maximum score for f(x, y) = x^2 + y^2 found via hill climbing: '''
					    f'{local_min.score()}'
					)




'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


__lowercase							:				List[str]					=       {
    '''configuration_sam''': [
        '''SAM_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''SamConfig''',
        '''SamMaskDecoderConfig''',
        '''SamPromptEncoderConfig''',
        '''SamVisionConfig''',
    ],
    '''processing_sam''': ['''SamProcessor'''],
}


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				List[Any]					=       [
					    '''SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''SamModel''',
					    '''SamPreTrainedModel''',
					]
try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				int					=       [
					    '''TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFSamModel''',
					    '''TFSamPreTrainedModel''',
					]
try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:				str					=       ['''SamImageProcessor''']


if TYPE_CHECKING:
					from .configuration_sam import (
					    SAM_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    SamConfig,
					    SamMaskDecoderConfig,
					    SamPromptEncoderConfig,
					    SamVisionConfig,
					)
					from .processing_sam import SamProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_sam import SAM_PRETRAINED_MODEL_ARCHIVE_LIST, SamModel, SamPreTrainedModel

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_sam import TF_SAM_PRETRAINED_MODEL_ARCHIVE_LIST, TFSamModel, TFSamPreTrainedModel

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_sam import SamImageProcessor

else:
					import sys

					__lowercase							:				List[str]					=       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


