Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DataScienceUWL
GitHub Repository: DataScienceUWL/DS775
Path: blob/main/Homework/Lesson 05 HW - Local Optimization/locsearch/__pycache__/ls.cpython-38.pyc
852 views
U

`�_:�@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"|dk	r|�|�|_ntd��dS)NzNo inital_state supplied)�
copy_state�state�
ValueError)�selfZ
initial_state�r
�C/home/user/Solutions/Lesson 04 HW Solutions - QP NP/locsearch/ls.py�__init__szLocalSearcher.__init__cCsdS)zCreate a state changeNr
�rr
r
r�move%szLocalSearcher.movecCsdS)z*Calculate state's objective function valueNr
rr
r
r�	objective*szLocalSearcher.objectivecCsR|jdkrt�|�S|jdkr*|dd�S|jdkr<|��Stdd|j��dS)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
r
r
rr	/s



��zLocalSearcher.copy_statecCs2|jdkrtd�nt|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_frr
r
r�updateBs

zLocalSearcher.updatecCs�d|_d}|�|j�|_|��|_|��||jkr�|d7}|jd7_|�|j�}|��|��}||jkr�d}|�|j�|_||_n|�|�|_|j|j	dkr*|��q*|��|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_improveZ
curr_state�new_fr
r
r�localsearchMs(	



zLocalSearcher.localsearch)N)�__name__�
__module__�__qualname__�__doc__�abc�ABCMeta�
__metaclass__rr rrrrr�abstractmethodrrr	rr#r
r
r
rrs 


r)�
__future__rrrrr(r�math�signal�sys�objectrr
r
r
r�<module>s