Exemple #1
0
def genPop(N, chromGenfuncs, chromGenParams):
    """ Return a population (list) of N unique individuals.
        Each individual has len(chromgGenFuncs) chromosomes.
        For each individual, chromosome_i is generated by calling chromGenFuncs_i(chromeGenParams_i)

        pre:
            N >= 0
            isinstance(chromGenfuncs, list)
            isinstance(chromGenParams, list)
            len(chromGenfuncs) == len(chromGenParams)

        post:
            isinstance(__return__, list)
            len(__return__) == N
            forall(__return__, lambda indiv: __return__.count(indiv) == 1)

        post[chromGenfuncs, chromGenParams]:
            __old__.chromGenfuncs == chromGenfuncs
            __old__.chromGenParams == chromGenParams
    """

    answer = set()
    chromGens = zip(chromGenfuncs, chromGenParams)
    while len(answer) < N:
        indiv = Individual([])
        for genfunc, genparams in chromGens:
            indiv.append(genfunc(*genparams))
        answer.add(indiv)
    return list(answer)
Exemple #2
0
def oneChildCrossover(p1, p2, crossfuncs, crossparams):
    """
		Crossover all the chromosomes in the two individuals.
		The crossover function for each pair of  corresponding chromosomes is the the corresponding element in crossfuncs, 
		called with the corresponding tuple in crossparams.
		
		Note that it is assumed that each crossover function in crossfuncs returns one child chromosome
		
		pre:
			isinstance(p1, Individual)
			isinstance(p2, Individual)
			len(p1) == len(p2)
			len(p1) == len(crossfuncs)
			len(crossfuncs) == len(crossparams)
		
		post:
			__old__.p1 is p1
			__old__.p2 is p2
			__old__.p1 == p1
			__old__.p2 == p2
			
			len(__return__) == 1
			isinstance(__return__, Individual)
			len(__return__)==len(p1)
			all(len(c)==len(p1[i]) for i,c in enumerate(__return__.chromosomes))
			all(len(c)==len(p1[i]) for i,c in enumerate(__return__.chromosomes))
	"""
    answer = Individual([])
    for i, (crossfunc, crossparams) in enumerate(zip(crossfuncs, crossparams)):
        answer.append(crossfunc(p1[i], p2[i], *crossparams))

    return answer
Exemple #3
0
def oneChildCrossover(args): #p1, p2, crossfuncs, crossparams):
	"""
		Crossover all the chromosomes in the two individuals.
		The crossover function for each pair of  corresponding chromosomes is the the corresponding element in crossfuncs, 
		called with the corresponding tuple in crossparams.
		
		Note that it is assumed that each crossover function in crossfuncs returns one child chromosome
	"""
	answer = Individual([])
	for i, crossfunc, crossparams in zip(itertools.count(), args.crossfuncs, args.crossparams):
		answer.append(crossfunc(args.p1[i], args.p2[i], crossparams))
	
	return answer
def genPop(args):
    """ Return a population (list) of N unique individuals.
		Each individual has len(chromgGenFuncs) chromosomes.
		For each individual, chromosome_i is generated by calling chromGenFuncs_i(chromeGenParams_i)
	"""

    answer = set()
    while len(answer) < args.popSize:
        indiv = Individual([])
        for genfunc, genparams in args.chromGens:
            indiv.append(genfunc(genparams))
        answer.add(indiv)
    return list(answer)
Exemple #5
0
def twoChildCrossover(p1, p2, crossfuncs, crossparams):
    """
		Crossover all the chromosomes in the two individuals.
		The crossover function for each pair of  corresponding chromosomes is the the corresponding element in crossfuncs, 
		called with the corresponding tuple in crossparams.
		
		Note that it is assumed that each crossover function in crossfuncs returns two child chromosomes
	
		pre:
			isinstance(p1, Individual)
			isinstance(p2, Individual)
			len(p1) == len(p2)
			len(p1) == len(crossfuncs)
			len(crossfuncs) == len(crossparams)
			forall(crossparams, lambda params: isinstance(params, tuple))
		
		post:
			__old__.p1 is p1
			__old__.p2 is p2
			__old__.p1 == p1
			__old__.p2 == p2
			
			len(__return__) == 2
			forall(_return__, lambda p: isinstance(p, Individual))
			forall(__return__, lambda p: len(p)==len(p1))
			forall(__return__, lambda p: all(len(c)==len(p1[i]) for i,c in enumerate(p.chromosomes)))
			forall(__return__, lambda p: all(len(c)==len(p2[i]) for i,c in enumerate(p.chromosomes)))
	"""

    c1, c2 = Individual([]), Individual([])
    for i, (crossfunc, crossparams) in enumerate(zip(crossfuncs, crossparams)):
        chrom1, chrom2 = crossfunc(p1[i], p2[i], *crossparams)
        c1.append(chrom1)
        c2.append(chrom2)

    return c1, c2