Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DataScienceUWL
GitHub Repository: DataScienceUWL/DS775
Path: blob/main/Lessons/Lesson 05 - Local Optimization/locsearch/__pycache__/ls.cpython-310.pyc
873 views
o

��$e:�@slddlmZddlmZddlmZddlmZddlZddlZddlZddlZddl	Z	Gdd�de
�ZdS)�)�absolute_import)�division)�print_function)�unicode_literalsNc@sleZdZdZejZdZdZdZ	dZ
dZdZddd�Z
ejd	d
��Zejdd��Zd
d�Zdd�Zdd�ZdS)�
LocalSearcheraPerforms local search by calling functions to calculate
    objective function value and make moves on a state.  The search terminates when 
    no improvements to the objective function value have made for max_no_improve 
    iterations (default 1000)
    i��d�deepcopyNrcCs |dur|�|�|_dStd��)NzNo inital_state supplied)�
copy_state�state�
ValueError)�self�
initial_state�r�A/home/user/Lessons/Lesson 05 - Local Optimization/locsearch/ls.py�__init__szLocalSearcher.__init__cC�dS)zCreate a state changeNr�rrrr�move%�zLocalSearcher.movecCr)z*Calculate state's objective function valueNrrrrr�	objective*rzLocalSearcher.objectivecCsN|jdkr
t�|�S|jdkr|dd�S|jdkr|��Stdd|j��)a+Returns an exact copy of the provided state
        Implemented according to self.copy_strategy, one of

        * deepcopy : use copy.deepcopy (slow but reliable)
        * slice: use list slices (faster but only works if state is list-like)
        * method: use the state's copy() method
        r�sliceN�methodzNo implementation found for zthe self.copy_strategy "%s")�
copy_strategy�copyr�RuntimeError)rr
rrrr	/s



��zLocalSearcher.copy_statecCs4|jdkrtd�dSt|jd�d|jd���dS)z\
        Prints the current best value and iterations every update_iter iterations.
        rz
 Obj Fun Val | Iterationsz12.2fz | �dN)�
iterations�print�best_frrrr�updateBs
zLocalSearcher.updatecCs�d|_d}|�|j�|_|��|_|��||jkr\|d7}|jd7_|�|j�}|��|��}||jkrEd}|�|j�|_||_n|�|�|_|j|j	dkrW|��||jks|��|j|jfS)z�Minimizes the objective function value by local search.

        Parameters
        state : an initial arrangement of the system

        Returns
        (state, objective): the best state and objective function value found.
        r�)
rr	r
�best_xrrr�max_no_improver�update_iter)r�num_moves_no_improve�
curr_state�new_frrr�localsearchMs*	



�zLocalSearcher.localsearch)N)�__name__�
__module__�__qualname__�__doc__�abc�ABCMeta�
__metaclass__r"r#rr!rrr�abstractmethodrrr	rr'rrrrrs"


r)�
__future__rrrrr,r�math�signal�sys�objectrrrrr�<module>s