Пример #1
0
	def optimize(self, objective_func, iters, n_processes=None, verbose=False, silent=False, **kwargs):
		if verbose:
			logginglevel=logging.INFO
		if silent:
			remove_stream_handlers()
		self.rep.log("obj.func args: {}".format(kwargs), lvl=logging.DEBUG)
		self.rep.log("Optimize for {} iters with {}".format(iters, self.options), lvl=logginglevel)

		#populate memory of the handlers
		self.bh.memory = self.swarm.position
		self.vh.memory = self.swarm.position

		# Setup Pool of processes for parallel evaluation
		pool = None if n_processes is None else mp.Pool(n_processes)

		self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)

		for i in range(iters) if not verbose or silent else self.rep.pbar(iters, self.name):
			# Compute cost for current position and personal best
			# fmt: off
			self.swarm.current_cost = compute_objective_function(self.swarm, objective_func, pool=pool, **kwargs)
			self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
			# Set best_cost_yet_found for ftol
			best_cost_yet_found = self.swarm.best_cost
			self.swarm.best_pos, self.swarm.best_cost = self.top.compute_gbest(self.swarm)
			# fmt: on
			if verbose and not silent:
				self.rep.hook(best_cost=self.swarm.best_cost)
			# save to history
			hist = self.ToHistory(
				best_cost=self.swarm.best_cost,
				mean_pbest_cost=np.mean(self.swarm.pbest_cost),
				position=self.swarm.position,
				velocity=self.swarm.velocity,
				)
			self._populate_history(hist)
			# verify stop criteria based on the relative acceptable cost ftol
			relative_measure = self.ftol * (1 + np.abs(best_cost_yet_found))
			if (np.abs(self.swarm.best_cost - best_cost_yet_found) < relative_measure):
				break
			# perform velocity and position updates
			self.swarm.velocity = self.top.compute_velocity(self.swarm, self.velocity_clamp, self.vh, self.bounds)
			self.swarm.position = self.top.compute_position(self.swarm, self.bounds, self.bh)

		# obtain the final best_cost and the final best_position
		final_best_cost = self.swarm.best_cost.copy()
		final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()

		# write report in log and return final cost and position
		self.rep.log(
			"Optimization Finished | Best Cost: {}, Best Position: {}".format(final_best_cost, final_best_pos),
			lvl=logginglevel,
		)
		# close pool of processes
		if n_processes is not None:
			pool.close()
		return(final_best_cost, final_best_pos)
    def optimize(self, objective_func, iters, n_processes=None, **kwargs):
        self.rep.log("Obj. func. args: {}".format(kwargs), lvl=logging.DEBUG)
        self.rep.log(
            "Optimize for {} iters with {}".format(iters, self.options),
            lvl=logging.INFO,
        )
        self.rep.log(
            "Population size: {}".format(self.swarm_size),
            lvl=logging.INFO,
        )
        filename = "CHECKPOINT_PYSWARMS_"+self.rep.logger.name
        # Populate memory of the handlers
        self.bh.memory = self.swarm.position
        self.vh.memory = self.swarm.position

        # Setup Pool of processes for parallel evaluation
        pool = None if n_processes is None else mp.Pool(n_processes)
        self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
        for i in self.rep.pbar(iters, self.name):
            last_time = time.time()
            # Compute cost for current position and personal best
            self.swarm.current_cost = compute_objective_function(
                self.swarm, objective_func, pool=pool, **kwargs
            )
            self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
                self.swarm
            )
            best_cost_yet_found = np.min(self.swarm.best_cost)
            # Update gbest from neighborhood
            self.swarm.best_pos, self.swarm.best_cost = self.top.compute_gbest(
                self.swarm, p=self.p, k=self.k
            )
            self.rep.hook(best_cost=np.min(self.swarm.best_cost))
            # Save to history
            hist = self.ToHistory(
                best_cost=self.swarm.best_cost,
                mean_pbest_cost=np.mean(self.swarm.pbest_cost),
                mean_neighbor_cost=np.mean(self.swarm.best_cost),
                position=self.swarm.position,
                velocity=self.swarm.velocity,
            )
            self._populate_history(hist)
            # Verify stop criteria based on the relative acceptable cost ftol
            relative_measure = self.ftol * (1 + np.abs(best_cost_yet_found))
            if (
                np.abs(self.swarm.best_cost - best_cost_yet_found)
                < relative_measure
            ):
                break
            # Perform position velocity update
            self.swarm.velocity = self.top.compute_velocity(
                self.swarm, self.velocity_clamp, self.vh, self.bounds
            )
            self.swarm.position = self.top.compute_position(
                self.swarm, self.bounds, self.bh
            )
            self.rep.log(
                "Generation {}: Mean-fitness {}, std_dev_fitness {}, best {}".format(i,
                                                                                     np.mean(self.swarm.current_cost),
                                                                                     np.std(self.swarm.current_cost),
                                                                                     self.swarm.best_cost),
                lvl=logging.INFO,
            )
            self.rep.log("Time elapsed {}".format(time.time() - last_time), lvl=logging.INFO, )
            with open(filename, "wb") as fout:
                pickle.dump(self.swarm, fout)
        # Obtain the final best_cost and the final best_position
        final_best_cost = self.swarm.best_cost.copy()
        final_best_pos = self.swarm.pbest_pos[self.swarm.pbest_cost.argmin()].copy()
        # Write report in log and return final cost and position
        self.rep.log(
            "Optimization finished | best cost: {}, best pos: {}".format(
                final_best_cost, final_best_pos
            ),
            lvl=logging.INFO,
        )
        return (final_best_cost, final_best_pos)
    def optimize(self,
                 objective_func,
                 iters,
                 n_processes=None,
                 verbose=True,
                 **kwargs):
        """Optimize the swarm for a number of iterations

        Performs the optimization to evaluate the objective
        function :code:`f` for a number of iterations :code:`iter.`

        Parameters
        ----------
        objective_func : function
            objective function to be evaluated
        iters : int
            number of iterations
        n_processes : int, optional
            number of processes to use for parallel particle evaluation
            Defaut is None with no parallelization.
        verbose : bool
            enable or disable the logs and progress bar (default: True = enable logs)
        kwargs : dict
            arguments for objective function

        Returns
        -------
        tuple
            the local best cost and the local best position among the
            swarm.
        """
        # Apply verbosity
        if verbose:
            log_level = logging.INFO
        else:
            log_level = logging.NOTSET

        self.rep.log("Obj. func. args: {}".format(kwargs), lvl=logging.DEBUG)
        self.rep.log(
            "Optimize for {} iters with {}".format(iters, self.options),
            lvl=log_level,
        )
        # Populate memory of the handlers
        self.bh.memory = self.swarm.position
        self.vh.memory = self.swarm.position

        # Setup Pool of processes for parallel evaluation
        pool = None if n_processes is None else mp.Pool(n_processes)

        self.swarm.pbest_cost = np.full(self.swarm_size[0], np.inf)
        ftol_history = deque(maxlen=self.ftol_iter)
        for i in self.rep.pbar(iters, self.name) if verbose else range(iters):

            # Compute cost for current position and personal best
            ''' Binary swarm postitions need to be transformed to discrete swarm
                postitions first, because the objective function expects discrete
                values (only positions are transformed!), original binary
                position is saved in binary_swarm_position'''
            binary_swarm_position = self.BinarySwarmPositions_to_DiscreteSwarmPositions(
            )

            # Evaluate Cost Function
            self.swarm.current_cost = compute_objective_function(
                self.swarm, objective_func, pool, **kwargs)
            ''' Transform discrete swarm positions back to binary positions, 
            because the PSO works on binary particles'''
            self.swarm.position = binary_swarm_position

            self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(
                self.swarm)
            best_cost_yet_found = np.min(self.swarm.best_cost)
            # Update gbest from neighborhood
            self.swarm.best_pos, self.swarm.best_cost = self.top.compute_gbest(
                self.swarm, p=self.p, k=self.k)
            if verbose:
                # Print to console
                self.rep.hook(best_cost=self.swarm.best_cost)
            # Save to history
            hist = self.ToHistory(
                best_cost=self.swarm.best_cost,
                mean_pbest_cost=np.mean(self.swarm.pbest_cost),
                mean_neighbor_cost=np.mean(self.swarm.best_cost),
                position=self.swarm.position,
                velocity=self.swarm.velocity,
            )
            self._populate_history(hist)
            # Verify stop criteria based on the relative acceptable cost ftol
            relative_measure = self.ftol * (1 + np.abs(best_cost_yet_found))
            delta = (np.abs(self.swarm.best_cost - best_cost_yet_found) <
                     relative_measure)
            if i < self.ftol_iter:
                ftol_history.append(delta)
            else:
                ftol_history.append(delta)
                if all(ftol_history):
                    break
            # Perform position velocity update
            self.swarm.velocity = self.top.compute_velocity(
                self.swarm, self.velocity_clamp, self.vh)
            self.swarm.position = self._compute_position(self.swarm)

        # Obtain the final best_cost and the final best_position
        final_best_cost = self.swarm.best_cost.copy()
        final_best_pos = self.swarm.pbest_pos[
            self.swarm.pbest_cost.argmin()].copy()
        self.rep.log(
            "Optimization finished | best cost: {}, best pos: {}".format(
                final_best_cost, final_best_pos),
            lvl=log_level,
        )
        # Close Pool of Processes
        if n_processes is not None:
            pool.close()

        return (final_best_cost, final_best_pos)