def __init__(self, pos): """Solution constructor.""" self.pos = pos # initialize solution as a network to check fitness, since fitness is # a function of feedforwarding training examples network = net.initialize_network(self.pos, FEATURES, \ HIDDEN_SIZE, CLASSES) self.fit = net.mse(network, CLASSES, TRAIN, activation_function)
def set_genes(self, genes): """Genes mutator method.""" self.genes = genes # when setting genes subsequent times # update the fitness network = net.initialize_network(self.genes, FEATURES, \ HIDDEN_SIZE, CLASSES) self.fit = net.mse(network, CLASSES, TRAIN, activation_function)
def __init__(self, pos): """Particle constructor.""" # initialize position and velocity self.pos, self.vel = pos, [0.00 for _ in range(len(pos))] # find fitness at instantiation network = net.initialize_network(self.pos, FEATURES, \ HIDDEN_SIZE, CLASSES) self.fit = net.mse(network, CLASSES, TRAIN, activation_function) # best so far is just initial self.best_pos, self.best_fit = self.pos, self.fit
def __init__(self, pos): """Bat constructor.""" self.pos, self.vel = pos, [0.00 for _ in range(len(pos))] self.loudness = uniform(1, 2) # loudness is some random value 1..2 self.max_pulse_rate = uniform(0, 1) # max pulse rate varies per bat self.pulse_rate = 0 # initially pulse rate is 0 and climbs to max # find fitness at instantiation network = net.initialize_network(self.pos, FEATURES, \ HIDDEN_SIZE, CLASSES) self.fit = net.mse(network, CLASSES, TRAIN, activation_function)
def __init__(self, genes, fit=None): """Chromosome constructor without fitness.""" # initialize weights from parameter self.genes = genes # if no argument passed as fitness # take fitness from genes argument # else init as fit argument if fit is None: network = net.initialize_network(self.genes, FEATURES, \ HIDDEN_SIZE, CLASSES) self.fit = net.mse(network, CLASSES, TRAIN, activation_function) else: self.fit = fit
def set_pos(self, pos): """Position mutator method.""" self.pos = pos if not any(p < -BOUND for p in pos)\ and not any(p > BOUND for p in pos): # get fitness of new position network = net.initialize_network(self.pos, FEATURES, \ HIDDEN_SIZE, CLASSES) fitness = net.mse(network, CLASSES, TRAIN, activation_function) # if better if fitness < self.best_fit: self.fit = fitness # update best fitness self.best_fit = self.fit # update best position self.best_pos = self.pos