Пример #1
0
    def __init__(self, problem, parallel=False, *args, **kwargs):
        """
        Create object instance and perform a single optimization run using PSO.

        :param problem: The problem to be optimized. Instance of Problem-class.

        :param parallel:
            Evaluate the fitness for the particles in parallel.
            See the README.md file for a discussion on this.

        :return:
            Object instance. Get the optimization results from the object's variables.
            -   best is the best-found solution.
            -   best_fitness is the associated fitness of the best-found solution.
            -   fitness_trace is an instance of the FitnessTrace-class.
        """

        # Copy arguments to instance variables.
        self.problem = problem
        self.parallel = parallel

        # Initialize all particles with random positions in the search-space.
        # The first index is for the particle number.
        # The second index is for the search-space dimension.
        # Note that self.num_particles must be set prior to this by the sub-class.
        self.particle = tools.rand_population(lower=problem.lower_init,
                                              upper=problem.upper_init,
                                              num_agents=self.num_particles,
                                              dim=problem.dim)

        # Initialize best-known positions for the particles to their starting positions.
        # A copy is made because the particle positions will change during optimization
        # regardless of improvement to the particle's fitness.
        self.particle_best = np.copy(self.particle)

        # Initialize fitness of best-known particle positions to infinity.
        self.particle_best_fitness = np.repeat(np.inf, self.num_particles)

        # Boundaries for the velocity. These are set to the range of the search-space.
        bound_range = np.abs(problem.upper_bound - problem.lower_bound)
        self.velocity_lower_bound = -bound_range
        self.velocity_upper_bound = bound_range

        # Initialize all velocities with random values in the allowed range.
        self.velocity = tools.rand_population(lower=self.velocity_lower_bound,
                                              upper=self.velocity_upper_bound,
                                              num_agents=self.num_particles,
                                              dim=problem.dim)

        # Initialize parent-class which also starts the optimization run.
        SingleRun.__init__(self, *args, **kwargs)
Пример #2
0
    def __init__(self, problem, parallel=False, *args, **kwargs):
        """
        Create object instance and perform a single optimization run using PSO.

        :param problem: The problem to be optimized. Instance of Problem-class.

        :param parallel:
            Evaluate the fitness for the particles in parallel.
            See the README.md file for a discussion on this.

        :return:
            Object instance. Get the optimization results from the object's variables.
            -   best is the best-found solution.
            -   best_fitness is the associated fitness of the best-found solution.
            -   fitness_trace is an instance of the FitnessTrace-class.
        """

        # Copy arguments to instance variables.
        self.problem = problem
        self.parallel = parallel

        # Initialize all particles with random positions in the search-space.
        # The first index is for the particle number.
        # The second index is for the search-space dimension.
        # Note that self.num_particles must be set prior to this by the sub-class.
        self.particle = tools.rand_population(lower=problem.lower_init,
                                              upper=problem.upper_init,
                                              num_agents=self.num_particles,
                                              dim=problem.dim)

        # Initialize best-known positions for the particles to their starting positions.
        # A copy is made because the particle positions will change during optimization
        # regardless of improvement to the particle's fitness.
        self.particle_best = np.copy(self.particle)

        # Initialize fitness of best-known particle positions to infinity.
        self.particle_best_fitness = np.repeat(np.inf, self.num_particles)

        # Boundaries for the velocity. These are set to the range of the search-space.
        bound_range = np.abs(problem.upper_bound - problem.lower_bound)
        self.velocity_lower_bound = -bound_range
        self.velocity_upper_bound = bound_range

        # Initialize all velocities with random values in the allowed range.
        self.velocity = tools.rand_population(lower=self.velocity_lower_bound,
                                              upper=self.velocity_upper_bound,
                                              num_agents=self.num_particles,
                                              dim=problem.dim)

        # Initialize parent-class which also starts the optimization run.
        SingleRun.__init__(self, *args, **kwargs)
Пример #3
0
    def __init__(self, problem, parameters=parameters_default, parallel=False, *args, **kwargs):
        """
        Create object instance and perform a single optimization run using DE.

        :param problem: The problem to be optimized. Instance of Problem-class.

        :param parameters:
            Control parameters for the DE.
            These may have a significant impact on the optimization performance.
            First try and use the default parameters and if they don't give satisfactory
            results, then experiment with other parameters or meta-optimization.

        :param parallel:
            Evaluate the fitness for the agents in parallel.
            See the README.md file for a discussion on this.

        :return:
            Object instance. Get the optimization results from the object's variables.
            -   best is the best-found solution.
            -   best_fitness is the associated fitness of the best-found solution.
            -   fitness_trace is an instance of the FitnessTrace-class.
        """

        # Copy arguments to instance variables.
        self.problem = problem
        self.parallel = parallel

        # Unpack control parameters.
        self.num_agents, self.crossover_probability, self.differential_weight = parameters

        # The number of agents must be an integer.
        self.num_agents = int(self.num_agents)

        # Initialize population with random positions in the search-space.
        # The first index is for the agent number.
        # The second index is for the search-space dimension.
        # These are actually the best-known positions for each agent
        # and will only get replaced when improvements are found.
        self.population = tools.rand_population(lower=problem.lower_init,
                                                upper=problem.upper_init,
                                                num_agents=self.num_agents,
                                                dim=problem.dim)

        # Population used for generating new agents.
        self.new_population = np.copy(self.population)

        # Initialize fitness for all agents to infinity.
        self.fitness = np.repeat(np.inf, self.num_agents)

        # Initialize parent-class which also starts the optimization run.
        SingleRun.__init__(self, *args, **kwargs)