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-36.pyc
852 views
3

`�_:�@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|j|�|_ntd��dS)NzNo inital_state supplied)�
copy_state�state�
ValueError)�selfZ
initial_state�r
�A/home/user/Lessons/Lesson 04 Presentation - 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
)rr
r
r�	objective*szLocalSearcher.objectivecCsR|jdkrtj|�S|jdkr*|dd�S|jdkr<|j�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_f)rr
r
r�updateBs

zLocalSearcher.updatecCs�d|_d}|j|j�|_|j�|_|j�x�||jkr�|d7}|jd7_|j|j�}|j�|j�}||jkr�d}|j|j�|_||_n|j|�|_|j|j	dkr,|j�q,W|j�|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Znum_moves_no_improveZ
curr_state�new_fr
r
r�localsearchMs(	

zLocalSearcher.localsearch)N)�__name__�
__module__�__qualname__�__doc__�abc�ABCMeta�
__metaclass__rrrrrrr�abstractmethodrrr	rr!r
r
r
rrs
r)�
__future__rrrrr&r�math�signal�sys�objectrr
r
r
r�<module>s