Path: blob/main/Homework/Lesson 05 HW - Local Optimization/locsearch/ls.py
852 views
from __future__ import absolute_import1from __future__ import division2from __future__ import print_function3from __future__ import unicode_literals4import abc5import copy6import math7import signal8import sys910class LocalSearcher(object):1112"""Performs local search by calling functions to calculate13objective function value and make moves on a state. The search terminates when14no improvements to the objective function value have made for max_no_improve15iterations (default 1000)16"""1718__metaclass__ = abc.ABCMeta1920# defaults21max_no_improve = 100022update_iter = 10023copy_strategy = 'deepcopy'2425# placeholders26best_x = None27best_f = None28iterations = 02930def __init__(self, initial_state=None):31if initial_state is not None:32self.state = self.copy_state(initial_state)33else:34raise ValueError('No inital_state supplied')3536@abc.abstractmethod37def move(self):38"""Create a state change"""39pass4041@abc.abstractmethod42def objective(self):43"""Calculate state's objective function value"""44pass4546def copy_state(self, state):47"""Returns an exact copy of the provided state48Implemented according to self.copy_strategy, one of4950* deepcopy : use copy.deepcopy (slow but reliable)51* slice: use list slices (faster but only works if state is list-like)52* method: use the state's copy() method53"""54if self.copy_strategy == 'deepcopy':55return copy.deepcopy(state)56elif self.copy_strategy == 'slice':57return state[:]58elif self.copy_strategy == 'method':59return state.copy()60else:61raise RuntimeError('No implementation found for ' +62'the self.copy_strategy "%s"' %63self.copy_strategy)6465def update(self):66"""67Prints the current best value and iterations every update_iter iterations.68"""6970if self.iterations == 0:71print("\n Obj Fun Val | Iterations")72else:73print(f"{self.best_f:12.2f} | {self.iterations:d}")747576def localsearch(self):77"""Minimizes the objective function value by local search.7879Parameters80state : an initial arrangement of the system8182Returns83(state, objective): the best state and objective function value found.84"""85self.iterations = 086num_moves_no_improve = 08788# set initial state and compute initial objective value89self.best_x = self.copy_state(self.state)90self.best_f = self.objective()91self.update()9293# main local search loop94while num_moves_no_improve < self.max_no_improve:95num_moves_no_improve += 196self.iterations += 197curr_state = self.copy_state(self.state)98self.move() # stores new state with move in self.state99new_f = self.objective()100if new_f < self.best_f:101num_moves_no_improve = 0102self.best_x = self.copy_state(self.state)103self.best_f = new_f104else: # if move not improvement reset state to curr_state105self.state = self.copy_state(curr_state)106if( self.iterations % self.update_iter == 0): # output every update_iter iterations107self.update() # print output108109# output one last time for final iteration110self.update()111112# Return best state and energy113return self.best_x, self.best_f114115