def __init__( self, space: Space, optimizer: Optimizer, function: Function, save_agents: Optional[bool] = False, ) -> None: """Initialization method. Args: space: Space-child instance. optimizer: Optimizer-child instance. function: Function or Function-child instance. save_agents: Saves all agents in the search space. """ logger.info("Creating class: Opytimizer.") # Space self.space = space # Optimizer (and its additional variables) self.optimizer = optimizer self.optimizer.compile(space) # Function self.function = function # Optimization history self.history = History(save_agents) # Current iteration self.iteration = 0 # Total number of iterations self.total_iterations = 0 logger.debug( "Space: %s | Optimizer: %s| Function: %s.", self.space, self.optimizer, self.function, ) logger.info("Class created.")
def __init__(self, space, optimizer, function, save_agents=False): """Initialization method. Args: space (Space): Space-child instance. optimizer (Optimizer): Optimizer-child instance. function (Function): Function or Function-child instance. save_agents (bool): Saves all agents in the search space. """ logger.info('Creating class: Opytimizer.') # Space self.space = space # Optimizer (and its additional variables) self.optimizer = optimizer self.optimizer.compile(space) # Function self.function = function # Optimization history self.history = History(save_agents) # Current iteration self.iteration = 0 # Total number of iterations self.total_iterations = 0 # Logs the properties logger.debug('Space: %s | Optimizer: %s| Function: %s.', self.space, self.optimizer, self.function) logger.info('Class created.')
if __name__ == '__main__': # Gathers the input arguments args = get_arguments() # Gathering variables from arguments dataset = args.dataset descriptor = args.descriptor fold = args.fold type = args.type meta = args.mh # Defining an input file input_file = f'output/{meta}_{type}_{dataset}_val_{fold}.pkl' # Creating a History object h = History() # Loading the input file h.load(input_file) # Loading the predictions and labels preds, y = l.load_candidates(dataset, 'test', fold) # If descriptor is global-based if descriptor == 'global': # Gets the global predictors preds = preds[:, :35] # If descriptor is cnn-based elif descriptor == 'cnn': # Gets the CNN predictors
import numpy as np import opytimizer.visualization.convergence as c from opytimizer.utils.history import History # Creating the history object history = History() # Loading saved optimization task history.load('') # Gathering desired keys from the object # In this case, we will the first agent's position and fitness agent_pos = history.get(key='agents', index=(0, 0)) agent_fit = history.get(key='agents', index=(0, 1)) # We will also gather the best agent's position and fitness best_agent_pos = history.get(key='best_agent', index=(0,)) best_agent_fit = history.get(key='best_agent', index=(1,)) # Plotting convergence graphs # Plotting the convergence of agent's positions c.plot(agent_pos[0], agent_pos[1], labels=['$x_0$', '$x_1$'], title='Sphere Function: $x^2 \mid x \in [-10, 10]$', subtitle='Agent: 0 | Algorithm: Particle Swarm Optimization') # Plotting the convergence of best agent's positions c.plot(best_agent_pos[0], best_agent_pos[1], labels=['$x^*_0$', '$x^*_1$'], title='Sphere Function: $x^2 \mid x \in [-10, 10]$', subtitle="Agent: Best | Algorithm: Particle Swarm Optimization") # Plotting the convergence of agent's and best agent's fitness c.plot(agent_fit, best_agent_fit, labels=[