Example #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()
Example #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()
Example #3
0
# then we can import PlotUtils
from plot_utils import PlotUtils

# you could import settings from a separate file like so
from settings import settings

# plot util variable. probably make this an instance
# variable in a class
plotutils = None

if settings['plot']:
    try:
        # Create PlotUtils instance
        # 2 params. number of dimensions and an array of 2D lists with
        # the bounds for each dimension. ex [(-10,10), (-10,10)]
        plotutils = PlotUtils(settings['num_dims'], settings['bounds'])
    except ValueError:
        print("Can not plot more than 2 dimensions!")
        # set this to false so that we don't try to use
        # the plotutils variable later on
        settings['plot'] = False

# data should be an array of 2D lists with x1 and x2 data
data = [(1, 1), (8, 4), (-4, -9)]

# open or update plot
if settings['plot']:
    plotutils.plot(data)

# you can put plotutils.plot(data) in a loop and continually update
# the plot. Here I just wait for you to press enter, after which the