How to use the deap.creator.Individual function in deap

To help you get started, we’ve selected a few deap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nilinswap / neuro-evolution / main1_nll_mse_misc_com.py View on Github external
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
				fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
				for ind, fit in zip(invalid_ind, fitnesses):
					ind.fitness.values = fit
		dum_ctr = 0
		for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
			# print(ind1.fitness.values)
			"""if not flag :
				ind1.modify_thru_backprop(indim, outdim, network_obj.rest_setx, network_obj.rest_sety, epochs=10, learning_rate=0.1, n_par=10)
				flag = 1
				print("just testing")
			"""
			flag = 0
			if random.random() <= CXPB:
				ind1, ind2 = toolbox.mate(ind1, ind2, gen)
				ind1 = creator.Individual(indim, outdim, ind1)
				ind2 = creator.Individual(indim, outdim, ind2)
				flag = 1
			maxi = max(maxi, ind1.node_ctr, ind2.node_ctr)
			toolbox.mutate(ind1)
			toolbox.mutate(ind2)

			offspring[dum_ctr] = ind1
			offspring[dum_ctr + 1] = ind2
			del offspring[dum_ctr].fitness.values, offspring[dum_ctr + 1].fitness.values
			dum_ctr += 2

		# Evaluate the individuals with an invalid fitness
		invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
		fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
		for ind, fit in zip(invalid_ind, fitnesses):
			ind.fitness.values = fit
github nilinswap / neuro-evolution / postmidsem / codes / main_folder / pilot / main1.py View on Github external
time8 = time.time()
		if gen == NGEN-1:
			time9 = time.time()
			
		for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
			# print(ind1.fitness.values)
			"""if not flag :
				ind1.modify_thru_backprop(indim, outdim, network_obj.rest_setx, network_obj.rest_sety, epochs=10, learning_rate=0.1, n_par=10)
				flag = 1
				print("just testing")
			"""

			if random.random() <= CXPB:
				ind1, ind2 = toolbox.mate(ind1, ind2, gen)
				ind1 = creator.Individual(indim, outdim,  ind1 )
				ind2 = creator.Individual( indim, outdim, ind2 )

			maxi = max(maxi, ind1.node_ctr, ind2.node_ctr)
			toolbox.mutate(ind1)
			toolbox.mutate(ind2)
			del ind1.fitness.values, ind2.fitness.values
			
		if gen == 1:
			print("1st gen after newpool",time.time() - time8)
		if gen == NGEN-1:
			print("last gen after newpool", time.time() - time9)
		# Evaluate the individuals with an invalid fitness
		invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
		fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
		for ind, fit in zip(invalid_ind, fitnesses):
			ind.fitness.values = fit
github nilinswap / neuro-evolution / main / codes / others / pilot2 / main1.py View on Github external
child2 = crossover(ind1, ind2, gen_no, inputdim=8, outputdim=2)

    return child1, child2


def mymutate(ind1):
    new_ind = ind1.do_mutation(0.2, 0.1, 0.05, indim, outdim, n_hidden, numpy.random)
    return new_ind


def initIndividual(ind_class, inputdim, outputdim):
    ind = ind_class(inputdim, outputdim)
    return ind


toolbox.register("individual", initIndividual, creator.Individual, indim, outdim)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("evaluate", minimize)
toolbox.register("mate", mycross)
toolbox.register("mutate", mymutate)
toolbox.register("select", tools.selNSGA2)

bp_rate = 0.05


def main(seed=None, play = 0, NGEN = 40, MU = 4 * 10):
    random.seed(seed)


      # this has to be a multiple of 4. period.
    CXPB = 0.9
github nilinswap / neuro-evolution / main.py View on Github external
child2 = crossover(ind1, ind2, gen_no, inputdim=8, outputdim=2)

    return child1, child2


def mymutate(ind1):
    new_ind = ind1.do_mutation(0.2, 0.1, 0.05, indim, outdim, n_hidden, numpy.random)
    return new_ind


def initIndividual(ind_class, inputdim, outputdim):
    ind = ind_class(inputdim, outputdim)
    return ind


toolbox.register("individual", initIndividual, creator.Individual, indim, outdim)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("evaluate", minimize)
toolbox.register("mate", mycross)
toolbox.register("mutate", mymutate)
toolbox.register("select", tools.selNSGA2)

bp_rate = 0.05
def main(seed=None, play = 0, NGEN = 40, MU = 4 * 10):
    random.seed(seed)


      # this has to be a multiple of 4. period.
    CXPB = 0.9

    stats = tools.Statistics(lambda ind: ind.fitness.values[1])
github DEAP / deap / doc / code / tutorials / part_2 / 2_3_1_bag.py View on Github external
## 2.3.1 Bag
import random

from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)

IND_SIZE = 20

toolbox = base.Toolbox()
toolbox.register("attr_int", random.randint, -20, 20)
toolbox.register("individual", tools.initRepeat, creator.Individual,
                 toolbox.attr_int, n=IND_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.population(n=100)
github DEAP / deap / doc / code / tutorials / part_2 / 2_2_6_funky_one.py View on Github external
from deap import base
from deap import creator
from deap import tools

creator.create("FitnessMax", base.Fitness, weights=(1.0, 1.0))
creator.create("Individual", list, fitness=creator.FitnessMax)

toolbox = base.Toolbox()

INT_MIN, INT_MAX = 5, 10
FLT_MIN, FLT_MAX = -0.2, 0.8
N_CYCLES = 4

toolbox.register("attr_int", random.randint, INT_MIN, INT_MAX)
toolbox.register("attr_flt", random.uniform, FLT_MIN, FLT_MAX)
toolbox.register("individual", tools.initCycle, creator.Individual,
                 (toolbox.attr_int, toolbox.attr_flt), n=N_CYCLES)
github nilinswap / neuro-evolution / main1_nll_mse_misc_com.py View on Github external
# Evaluate the individuals with an invalid fitness
				invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
				fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
				for ind, fit in zip(invalid_ind, fitnesses):
					ind.fitness.values = fit
		if gen == 1:
			time8 = time.time()
		if gen == NGEN-1:
			time9 = time.time()
		dum_ctr = 0
		for ind1, ind2 in zip(offspring[::2], offspring[1::2]):
			flag = 0
			if random.random() <= CXPB:
				ind1, ind2 = toolbox.mate(ind1, ind2, gen)
				ind1 = creator.Individual(indim, outdim,  ind1 )
				ind2 = creator.Individual( indim, outdim, ind2 )
				flag = 1
			maxi = max(maxi, ind1.node_ctr, ind2.node_ctr)
			toolbox.mutate(ind1)
			toolbox.mutate(ind2)

			offspring[dum_ctr] = ind1
			offspring[dum_ctr+1] = ind2
			del offspring[dum_ctr].fitness.values, offspring[dum_ctr+1].fitness.values
			dum_ctr+=2
		if gen == 1:
			print("1st gen after newpool",time.time() - time8)
		if gen == NGEN-1:
			print("last gen after newpool", time.time() - time9)

		# Evaluate the individuals with an invalid fitness
github DEAP / deap / examples / bbob.py View on Github external
toolbox = base.Toolbox()
    toolbox.register("update", update)
    toolbox.register("evaluate", func)
    toolbox.decorate("evaluate", tupleize)
    
    # Create the desired optimal function value as a Fitness object
    # for later comparison
    opt = creator.FitnessMin((ftarget,))
    
    # Interval in which to initialize the optimizer
    interval = -5, 5
    sigma = (interval[1] - interval[0])/2.0
    alpha = 2.0**(1.0/dim)
    
    # Initialize best randomly and worst as a place holder
    best = creator.Individual(random.uniform(interval[0], interval[1]) for _ in range(dim))
    worst = creator.Individual([0.0] * dim)
    
    # Evaluate the first individual
    best.fitness.values = toolbox.evaluate(best)
    
    # Evolve until ftarget is reached or the number of evaluation
    # is exausted (maxfuncevals)
    for g in range(1, maxfuncevals):
        toolbox.update(worst, best, sigma)
        worst.fitness.values = toolbox.evaluate(worst)
        if best.fitness <= worst.fitness:
            # Incease mutation strength and swap the individual
            sigma = sigma * alpha
            best, worst = worst, best
        else:
            # Decrease mutation strength
github cjekel / piecewise_linear_fit_py / pwlf / ga.py View on Github external
def genetic_algorithm(total_set, nvar, objective_function, ngen=50, mu=20,
                      lam=40, cxpb=0.7, mutpb=0.2, tournsize=3,
                      verbose=False):
    # Creates a new class name set_mod which is based on the standard python
    # set. This means set_mod is just like set, with the addion of a fitness
    # attribute.
    creator.create("Fitness", base.Fitness, weights=(-1.0,))
    creator.create("Individual", set, fitness=creator.Fitness)
    set_mod = creator.Individual

    def random_samp(size):
        """Function to initlize individual in the population."""
        return set_mod(random.sample(total_set, size))

    def evaluation(individual):
        """Evaluate the objective function."""
        return objective_function(list(individual)),

    def cxSet(ind1, ind2):
        """Apply a crossover operation on two sets."""
        full_set = list(ind1 | ind2)
        ind1 = set_mod(random.sample(full_set, nvar))
        ind2 = set_mod(random.sample(full_set, nvar))
        return ind1, ind2