Path: blob/main/Lessons/Lesson 05 - Local Optimization/locsearch/__pycache__/ls.cpython-310.pyc
873 views
o ��$e: � @ sl d dl mZ d dl mZ d dl mZ d dl mZ d dlZd dlZd dlZd dlZd dl Z G dd� de �ZdS )� )�absolute_import)�division)�print_function)�unicode_literalsNc @ sl e Zd ZdZejZdZdZdZ dZ dZdZddd�Z ejd d � �Zejdd� �Zd d� Zdd� Zdd� ZdS )� LocalSearchera Performs 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 �deepcopyNr c C s |d ur| � |�| _d S td��)NzNo inital_state supplied)� copy_state�state� ValueError)�self� initial_state� r �A/home/user/Lessons/Lesson 05 - Local Optimization/locsearch/ls.py�__init__ s zLocalSearcher.__init__c C � dS )zCreate a state changeNr �r r r r �move% � zLocalSearcher.movec C r )z*Calculate state's objective function valueNr r r r r � objective* r zLocalSearcher.objectivec C sN | j dkr t�|�S | j dkr|dd� S | j dkr|�� S tdd| 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 r r r r / s ��zLocalSearcher.copy_statec C s4 | j dkrtd� dS t| jd�d| j d��� dS )z\ Prints the current best value and iterations every update_iter iterations. r z Obj Fun Val | Iterationsz12.2fz | �dN)� iterations�print�best_fr r r r �updateB s zLocalSearcher.updatec C s� d| _ d}| �| j�| _| �� | _| �� || jk r\|d7 }| j d7 _ | �| j�}| �� | �� }|| jk rEd}| �| j�| _|| _n| �|�| _| j | j dkrW| �� || jk s| �� | 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 � ) r r r �best_xr r r �max_no_improver �update_iter)r �num_moves_no_improve� curr_state�new_fr r r �localsearchM s* �zLocalSearcher.localsearch)N)�__name__� __module__�__qualname__�__doc__�abc�ABCMeta� __metaclass__r"