예제 #1
0
    def __init__(self, settings, function):  # TODO add settings parameter
        super(self.__class__, self).__init__()
        # read in settings

        num_dims = settings['number_of_dimensions']
        population_size = settings['population_size']
        bounds = settings['bounds']

        if settings['velocity_type'] == 'constriction':
            phi = max(settings['cp'] + settings['cg'], 4.0)
            self.k = 2.0 / abs(2.0 - phi - sqrt(phi * phi - 4.0 * phi))
        else:
            self.k = 1

        # check to make sure num_dims and number of bounds provided match
        if len(bounds) != num_dims:
            raise ValueError(
                "Number of dimensions doesn't match number of bounds provided")

        # set instance variables
        self.settings = settings
        self.function = function
        # initialize population
        self.population = PSO.__gen_population(bounds, population_size,
                                               function)
        self.total_population = population_size
        self.best_x = PSO.__get_best_particle(self.population)
        self.num_iterations = 1

        if settings['plot']:
            try:
                self.plotutils = PlotUtils(num_dims, bounds, function)
                self.__plot_state()
            except ValueError:
                print("Can not plot more than 2 dimensions")
                settings['plot'] = False

        if settings['print_iterations']:
            self.__display_state()

        if settings['step_through']:
            oa_utils.pause()
예제 #2
0
    def __init__(self, settings, function): # TODO add settings parameter
        super(self.__class__, self).__init__()

        # read in settings
        num_dims        = settings['number_of_dimensions']
        population_size = settings['population_size']
        bounds          = settings['bounds']

        # check to make sure num_dims and number of bounds provided match
        if len(bounds) != num_dims:
            raise ValueError("Number of dimensions doesn't match number of bounds provided")

        # set instance variables
        self.settings        = settings
        self.function        = function
        # initialize population
        self.population      = GA.__gen_population(bounds, population_size, function)
        self.total_organisms = len(self.population)
        self.best_x          = self.population[0]
        self.num_generations = 1
        # stopping criteria variables
        self.func_val_improvement       = 0
        self.num_iter_since_improvement = 0

        if settings['plot']:
            try:
                self.plotutils = PlotUtils(num_dims, bounds, function)
                self.__plot_state()
            except ValueError:
                print("Can not plot more than 2 dimensions")
                settings['plot'] = False

        if settings['print_iterations']:
            self.__display_state()

        if settings['step_through']:
            oa_utils.pause()
예제 #3
0
    def do_loop(self):
        population = self.population

        population = GA.__selection(population,                        \
                                    self.settings['selection_cutoff'], \
                                    self.settings['print_actions'])

        population = GA.__crossover(self.total_organisms, \
                                    population,           \
                                    self.settings['population_size'], \
                                    self.function,        \
                                    self.settings['print_actions'])
        self.total_organisms += len(population)

        population = GA.__mutation(population, \
                                   self.settings['bounds'], \
                                   self.settings['mutation_rate'],       \
                                   self.settings['max_mutation_amount'], \
                                   self.settings['print_actions'])

        self.population = GA.__sort_population(population)
        self.num_generations += 1

        if self.population[0].fitness < self.best_x.fitness:
            # add on the improvement in function value
            self.func_val_improvement += (self.best_x.fitness - self.population[0].fitness)
            self.best_x = self.population[0]

        if self.settings['plot']:
            self.__plot_state()

        if self.settings['print_iterations']:
            self.__display_state()

        if self.settings['step_through']:
            oa_utils.pause()
예제 #4
0
    def do_loop(self):
        population = self.population

        population = PSO.__update_velocity(population,              \
                                    self.settings['velocity_type'], \
                                    self.settings['print_actions'], \
                                    self.get_best_x().pos,          \
                                    self.settings['cp'],            \
                                    self.settings['cg'],            \
                                    self.k,                         \
                                    self.settings['weight'])

        if self.settings['cg_plus']:
            self.settings['cg'] += 0.1
            phi = max(self.settings['cp'] + self.settings['cg'], 4.0)
            self.k = 2.0 / abs(2.0 - phi - sqrt(phi * phi - 4.0 * phi))

        population = PSO.__update_position(population)

        self.num_iterations += 1

        self.population = population

        current_best = PSO.__get_best_particle(self.population)

        if current_best.get_fval() < self.best_x.get_fval():
            self.best_x = current_best

        if self.settings['plot']:
            self.__plot_state()

        if self.settings['print_iterations']:
            self.__display_state()

        if self.settings['step_through']:
            oa_utils.pause()
예제 #5
0
    print("")

    # time initialization
    if settings['time']:
        start_time = time.time()

    # create algorithm instance
    ga = GA(settings, function)

    if settings['time']:
        print(" --- Initialized in %s seconds --- " % (time.time() - start_time))
        if settings['time_delay'] > 0.0 or settings['plot'] \
          or settings['print_actions'] or settings['print_iterations'] or settings['step_through']:
            print("\n --- WARNING: You are timing with either time_delay, plot, print_actions,")
            print("              print_iterations, or step_through enabled. --- \n")
            oa_utils.pause()
        ga.start_timer()
        #start_time = time.time()

    ga.run()

    if settings['time']:
        ga.stop_timer()
        print(" --- Ran for %s seconds --- " % (ga.get_time()))
        #print(" --- Ran for %s seconds --- " % (time.time() - start_time))

    # print out some data
    print("")
    print(str(ga))

    sys.exit()