def GTreeMutatorRealGaussian(genome, **args): """ A gaussian mutator for GTree of Real numbers Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if args["pmut"] <= 0.0: return 0 elements = len(genome) mutations = args["pmut"] * elements mu = genome.getParam("gauss_mu", Consts.CDefG1DListMutRealMU) sigma = genome.getParam("gauss_sigma", Consts.CDefG1DListMutRealSIGMA) if mutations < 1.0: mutations = 0 for i in xrange(len(genome)): if Util.randomFlipCoin(args["pmut"]): mutations += 1 rand_node = genome.getRandomNode() final_value = rand_node.getData() + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) rand_node.setData(final_value) else: for it in xrange(int(round(mutations))): rand_node = genome.getRandomNode() final_value = rand_node.getData() + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) rand_node.setData(final_value) return int(mutations)
def GTreeMutatorRealGaussian(genome, **args): """ A gaussian mutator for GTree of Real numbers Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if args["pmut"] <= 0.0: return 0 elements = len(genome) mutations = args["pmut"] * elements mu = genome.getParam("gauss_mu", Consts.CDefG1DListMutRealMU) sigma = genome.getParam("gauss_sigma", Consts.CDefG1DListMutRealSIGMA) if mutations < 1.0: mutations = 0 for i in range(len(genome)): if Util.randomFlipCoin(args["pmut"]): mutations += 1 rand_node = genome.getRandomNode() final_value = rand_node.getData() + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) rand_node.setData(final_value) else: for it in range(int(round(mutations))): rand_node = genome.getRandomNode() final_value = rand_node.getData() + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) rand_node.setData(final_value) return int(mutations)
def G2DListMutatorRealGaussian(genome, **args): """ A gaussian mutator for G2DList of Real Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ from . import Consts if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG2DListMutRealMU if sigma is None: sigma = Consts.CDefG2DListMutRealSIGMA if mutations < 1.0: mutations = 0 for i in range(genome.getHeight()): for j in range(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = genome[i][j] + rand_gauss(mu, sigma) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in range(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = genome[which_y][which_x] + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G2DListMutatorIntegerGaussianGradient(genome, **args): """ A gaussian mutator for G2DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. This routine generates a gaussian value with mu=1.0 and std=0.0333 and then the gene is multiplied by this value. This will cause the gene to drift no matter how large it is. """ from . import Consts if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for i in range(genome.getHeight()): for j in range(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = int(genome[i][j] * abs(rand_gauss(mu, sigma))) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in range(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = int(genome[which_y][which_x] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G2DListMutatorRealGaussian(genome, **args): """ A gaussian mutator for G2DList of Real Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG2DListMutRealMU if sigma is None: sigma = Consts.CDefG2DListMutRealSIGMA if mutations < 1.0: mutations = 0 for i in xrange(genome.getHeight()): for j in xrange(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = genome[i][j] + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in xrange(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = genome[which_y][which_x] + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G1DListMutatorRealGaussianGradient(genome, **args): """ The mutator of G1DList, Gaussian Gradient Mutator Accepts the *rangemin* and *rangemax* genome parameters, both optional. The random distribution is set with mu=1.0 and std=0.0333 The difference between this routine and the normal Gaussian Real is that the other function generates a gaussian value and adds it to the value. If the mu is 0, and the std is 1, a typical value could be 1.8 or -0.5. These small values are fine if your range is 0-10, but if your range is much larger, like 0-100,000, a relative gradient makes sense. This routine generates a gaussian value with mu=1.0 and std=0.0333 and then the gene is multiplied by this value. This will cause the gene to drift no matter how large it is. """ from . import Consts if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for it in range(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = genome[it] * abs(rand_gauss(mu, sigma)) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in range(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G2DListMutatorRealGaussianGradient(genome, **args): """ A gaussian gradient mutator for G2DList of Real Accepts the *rangemin* and *rangemax* genome parameters, both optional. The difference is that this multiplies the gene by gauss(1.0, 0.0333), allowing for a smooth gradient drift about the value. """ from . import Consts if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for i in range(genome.getHeight()): for j in range(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = genome[i][j] * abs(rand_gauss(mu, sigma)) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in range(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = genome[which_y][which_x] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G2DListMutatorIntegerGaussianGradient(genome, **args): """ A gaussian mutator for G2DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. This routine generates a gaussian value with mu=1.0 and std=0.0333 and then the gene is multiplied by this value. This will cause the gene to drift no matter how large it is. """ if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for i in xrange(genome.getHeight()): for j in xrange(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = int( genome[i][j] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in xrange(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = int(genome[which_y][which_x] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G1DListMutatorRealGaussian(genome, **args): """ The mutator of G1DList, Gaussian Mutator Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ from . import Consts if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG1DListMutRealMU if sigma is None: sigma = Consts.CDefG1DListMutRealSIGMA if mutations < 1.0: mutations = 0 for it in range(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = genome[it] + rand_gauss(mu, sigma) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in range(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] + rand_gauss(mu, sigma) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G1DListMutatorRealGaussianGradient(genome, **args): """ The mutator of G1DList, Gaussian Gradient Mutator Accepts the *rangemin* and *rangemax* genome parameters, both optional. The random distribution is set with mu=1.0 and std=0.0333 The difference between this routine and the normal Gaussian Real is that the other function generates a gaussian value and adds it to the value. If the mu is 0, and the std is 1, a typical value could be 1.8 or -0.5. These small values are fine if your range is 0-10, but if your range is much larger, like 0-100,000, a relative gradient makes sense. This routine generates a gaussian value with mu=1.0 and std=0.0333 and then the gene is multiplied by this value. This will cause the gene to drift no matter how large it is. """ if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = genome[it] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G1DListMutatorAlleleGaussian(genome, **arguments): """An allele-based mutator based on G1DListMutatorRealGaussian. Accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ from . import Consts if arguments["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = arguments["pmut"] * listSize mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG1DListMutRealMU if sigma is None: sigma = Consts.CDefG1DListMutRealSIGMA allele = genome.getParam("allele", None) if allele is None: Util.raiseException( "to use this mutator, you must specify the 'allele' parameter", TypeError) if mutations < 1.0: mutations = 0 for it in range(listSize): if Util.randomFlipCoin(arguments["pmut"]): final_value = genome[it] + rand_gauss(mu, sigma) assert len(allele[it].beginEnd ) == 1, "only single ranges are supported" rangemin, rangemax = allele[it].beginEnd[0] final_value = min(final_value, rangemax) final_value = max(final_value, rangemin) genome[it] = final_value mutations += 1 else: for it in range(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] + rand_gauss(mu, sigma) assert len(allele[which_gene].beginEnd ) == 1, "only single ranges are supported" rangemin, rangemax = allele[which_gene].beginEnd[0] final_value = min(final_value, rangemax) final_value = max(final_value, rangemin) genome[which_gene] = final_value return int(mutations)
def G1DListMutatorIntegerGaussianGradient(genome, **args): """ A gaussian mutator for G1DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. The random distribution is set with mu=1.0 and std=0.0333 Same as IntegerGaussian, except that this uses relative gradient rather than absolute gaussian. A value is randomly generated about gauss(mu=1, sigma=.0333) and multiplied by the gene to drift it up or down (depending on what side of 1 the random value falls on) and cast to integer """ from . import Consts if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for it in range(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = int(genome[it] * abs(rand_gauss(mu, sigma))) final_value = min( final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max( final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in range(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = int(genome[which_gene] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G1DListMutatorRealGaussian(genome, **args): """ The mutator of G1DList, Gaussian Mutator Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ # args["pmut"]=ga.setMutationRate(0.98) if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") length=genome.getParam("length",2) range_list=genome.getParam("range_list",None) if mu is None: mu = Consts.CDefG1DListMutRealMU if sigma is None: sigma = Consts.CDefG1DListMutRealSIGMA if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = round(genome[it] + rand_gauss(mu, sigma),length) if range_list is None: final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) else: final_value = min(final_value, range_list[it][1]) final_value = max(final_value, range_list[it][0]) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize-1) final_value = round(genome[which_gene] + rand_gauss(mu, sigma),length) range_list=genome.getParam("range_list",None) if range_list is None: final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) else: final_value = min(final_value, range_list[which_gene][1]) final_value = max(final_value, range_list[which_gene][0]) genome[which_gene] = final_value return int(mutations)
def G2DListMutatorRealGaussianGradient(genome, **args): """ A gaussian gradient mutator for G2DList of Real Accepts the *rangemin* and *rangemax* genome parameters, both optional. The difference is that this multiplies the gene by gauss(1.0, 0.0333), allowing for a smooth gradient drift about the value. """ if args["pmut"] <= 0.0: return 0 height, width = genome.getSize() elements = height * width mutations = args["pmut"] * elements mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for i in xrange(genome.getHeight()): for j in xrange(genome.getWidth()): if Util.randomFlipCoin(args["pmut"]): final_value = genome[i][j] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(i, j, final_value) mutations += 1 else: for it in xrange(int(round(mutations))): which_x = rand_randint(0, genome.getWidth() - 1) which_y = rand_randint(0, genome.getHeight() - 1) final_value = genome[which_y][which_x] * abs(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome.setItem(which_y, which_x, final_value) return int(mutations)
def G1DListMutatorIntegerGaussian(genome, **args): """ A gaussian mutator for G1DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG1DListMutIntMU if sigma is None: sigma = Consts.CDefG1DListMutIntSIGMA if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = genome[it] + int(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] + int(rand_gauss(mu, sigma)) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G1DConnBiasedMutateWeights(genome, **args): ## args['mutation_conns'] takes the list of indices of connections to be mutated if not args['mutation_conns']: mutation_conn_indices=rand_sample(numpy.array(len(genome)-1),int(args['pmut']*len(genome))) # print "Indices of mutation weights:" # print mutation_conn_indices else: mutation_conn_indices = args['mutation_conns'] # print "Indices of mutation weights:" # print mutation_conn_indices mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") new_mutation_list=[] for it in mutation_conn_indices: final_value = genome['weight'][it] + rand_gauss(mu, sigma) #final_value = min(final_value, genome.getParam("rangemax", Consts.CdefRangeMax)) #final_value = max(final_value, genome.getParam("rangemin", Consts.CdefRangeMin)) new_mutation_list.append(final_value) numpy.put(genome['weight'],[mutation_conn_indices],[new_mutation_list]) return genome
def G1DListMutatorAlleleGaussian(genome, **arguments): """An allele-based mutator based on G1DListMutatorRealGaussian. Accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if arguments["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = arguments["pmut"] * listSize mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG1DListMutRealMU if sigma is None: sigma = Consts.CDefG1DListMutRealSIGMA allele = genome.getParam("allele", None) if allele is None: Util.raiseException( "to use this mutator, you must specify the 'allele' parameter", TypeError) if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(arguments["pmut"]): final_value = genome[it] + rand_gauss(mu, sigma) assert len( allele[it].beginEnd) == 1, "only single ranges are supported" rangemin, rangemax = allele[it].beginEnd[0] final_value = min(final_value, rangemax) final_value = max(final_value, rangemin) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = genome[which_gene] + rand_gauss(mu, sigma) assert len( allele[which_gene].beginEnd) == 1, "only single ranges are supported" rangemin, rangemax = allele[which_gene].beginEnd[0] final_value = min(final_value, rangemax) final_value = max(final_value, rangemin) genome[which_gene] = final_value return int(mutations)
def G1DListMutatorIntegerGaussianGradient(genome, **args): """ A gaussian mutator for G1DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. The random distribution is set with mu=1.0 and std=0.0333 Same as IntegerGaussian, except that this uses relative gradient rather than absolute gaussian. A value is randomly generated about gauss(mu=1, sigma=.0333) and multiplied by the gene to drift it up or down (depending on what side of 1 the random value falls on) and cast to integer """ if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) mu = Consts.CDefGaussianGradientMU sigma = Consts.CDefGaussianGradientSIGMA if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = int(genome[it] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize - 1) final_value = int(genome[which_gene] * abs(rand_gauss(mu, sigma))) final_value = min(final_value, genome.getParam( "rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam( "rangemin", Consts.CDefRangeMin)) genome[which_gene] = final_value return int(mutations)
def G1DConnInitializatorGaussian(genome, **args): """ Real initialization function of G1DConnections This initializator accepts the *rangemin* and *rangemax* genome parameters. """ range_min = genome.getParam("rangemin", -50) range_max = genome.getParam("rangemax", 50) mu = genome.getParam("gauss_mu", 0) sigma = genome.getParam("gauss_sigma", 1) # print "mu=", mu, "sigma=", sigma layers = genome.getParam("layers", [2,4,1]) bias = genome.getParam("bias", [1,1,0]) ndLayers=np.array(layers) ndBias=np.array(bias) if genome.genomeList == []: totalConnections = int(np.sum([(layers[i]+1)*layers[i+1] for i in range(len(layers)-1)])) genomeList1=[] for ly in range(len(layers)-1): for toNeurode in range(layers[ly+1]): toNeurode += int(ndLayers[0:ly+1].sum()+ndBias[0:ly+1].sum()) for fromNeurode in range(layers[ly]+1): fromNeurode +=int(ndLayers[0:ly].sum()+ndBias[0:ly].sum()) weight = min(range_max, rand_gauss(mu, sigma)) weight = max(range_min, weight) genomeList1.append((fromNeurode, toNeurode, weight)) genome.genomeList = np.array(genomeList1, dtype=[('from','i'),('to','i'),('weight','f')]) else: totalConnections = len(genome.genomeList) if totalConnections != genome.genomeSize: print "%% Warning: genome.genomeSize and the length of genome.genomeList mismatch!" for conn in genome.genomeList: weight = min(range_max, rand_gauss(mu, sigma)) weight = max(range_min, weight) conn[2] = weight
def G1DConnUnbiasedMutateWeights(genome, **args): ## args['mutation_conns'] takes the list of indices of connections to be mutated if not args.get('mutation_conns',0): mutation_conn_indices=rand_sample(numpy.arange(len(genome)),int(args['pmut']*len(genome))) # print "Indices of mutation weights:" # print mutation_conn_indices else: mutation_conn_indices = args['mutation_conns'] # print "Indices of mutation weights:" # print mutation_conn_indices mu = genome.getParam("gauss_mu",0) sigma = genome.getParam("gauss_sigma",1) #new_mutation_list=[] for it in mutation_conn_indices: final_value=rand_gauss(mu, sigma) genome['weight'][it] = final_value # new_mutation_list.append(rand_uniform(genome.getParam("rangemin", Consts.CDefRangeMin),genome.getParam("rangemax", Consts.CDefRangeMax))) mutations = len(mutation_conn_indices) #numpy.put(genome['weight'],mutation_conn_indices,new_mutation_list) return int(mutations)
def G1DListMutatorIntegerGaussian(genome, **args): """ A gaussian mutator for G1DList of Integers Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ if args["pmut"] <= 0.0: return 0 listSize = len(genome) mutations = args["pmut"] * (listSize) range_list=genome.getParam("range_list",None) mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") if mu is None: mu = Consts.CDefG1DListMutIntMU if sigma is None: sigma = Consts.CDefG1DListMutIntSIGMA if mutations < 1.0: mutations = 0 for it in xrange(listSize): if Util.randomFlipCoin(args["pmut"]): final_value = genome[it] + int(rand_gauss(mu, sigma)) if range_list is None: final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) else: final_value = min(final_value, range_list[it]) final_value = max(final_value, 0) genome[it] = final_value mutations += 1 else: for it in xrange(int(round(mutations))): which_gene = rand_randint(0, listSize-1) final_value = genome[which_gene] + int(rand_gauss(mu, sigma)) if range_list is None: final_value = min(final_value, genome.getParam("rangemax", Consts.CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", Consts.CDefRangeMin)) else: final_value = min(final_value,range_list[which_gene]) final_value = max(final_value, 0) genome[which_gene] = final_value index=genome.getParam("index1",None) margin=genome.getParam("margin",None) if index!=None or margin!=None: while(1): sum=0 for i in index: sum=sum+genome[i]*margin[i] if sum<400000: break for j in index: if genome[j]>0: genome[j]=genome[j]-1 sum=sum-margin[j] #print genome[0:] if sum<400000: break #print genome[0:] return int(mutations)
def G2DListMutatorRealGaussianSpline(genome, **args): """ A gaussian mutator for G2DList of Real with UnivariateSpline for smoothing Accepts the *rangemin* and *rangemax* genome parameters, both optional. Also accepts the parameter *gauss_mu* and the *gauss_sigma* which respectively represents the mean and the std. dev. of the random distribution. """ dtgenome = DTGenomeFunctions() pmut = args["pmut"] if pmut <= 0.0: return 0 width = dtgenome.getIndividualFrameLength(genome) height = dtgenome.getIndividualLength(genome) elements = height * width mutations = pmut * elements mu = genome.getParam("gauss_mu") sigma = genome.getParam("gauss_sigma") prop = DTIndividualPropertyVanillaEvolutive() robotConfig = LoadRobotConfiguration() robotJoints = robotConfig.getJointsName() validIDs = [] jointIndex = 0 for joint in robotJoints: if not prop.avoidJoint(joint): validIDs.append(jointIndex) jointIndex += 1 if mu is None: mu = CDefG2DListMutRealMU if sigma is None: sigma = CDefG2DListMutRealSIGMA if mutations < 1.0: mutations = 0 for i in xrange(genome.getHeight()): for j in xrange(genome.getWidth()): if genome[i][j] != sysConstants.JOINT_SENTINEL and j in validIDs and randomFlipCoin(pmut): offset = rand_gauss(mu, sigma) print "OFFSET: ", offset, "joint: ", j, "frame: ", i final_value = genome[i][j] + offset final_value = min(final_value, genome.getParam("rangemax", CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", CDefRangeMin)) genome.setItem(i, j, final_value) dtgenome.smooth(genome, j, i, offset) ##pca.poseInterpolationWithPCA(chromosomeToLucyGeneticMatrix(genome), j) mutations += 1 else: for it in xrange(int(round(mutations))): foundFrameDifferentThanSentinel = False validIDFound = False while not foundFrameDifferentThanSentinel and not validIDFound: which_x = rand_randint(0, genome.getWidth() - 1) # joint to mutate if which_x in validIDs: #only mutate not avoided ids validIDFound = True which_y = rand_randint(0, genome.getHeight() - 1) # pose to mutate if genome[which_y][which_x] != sysConstants.JOINT_SENTINEL: foundFrameDifferentThanSentinel = True offset = rand_gauss(mu, sigma) final_value = genome[which_y][which_x] + offset #print "OFFSET: ", offset, "joint: ", which_x, "frame: ", which_y # to be sure that the value is less than the rangemax and more than the rangemin value final_value = min(final_value, genome.getParam("rangemax", CDefRangeMax)) final_value = max(final_value, genome.getParam("rangemin", CDefRangeMin)) # valueBeforeMutation = genome[which_y][which_x] genome.setItem(which_y, which_x, final_value) dtgenome.smooth(genome, which_x, which_y) ##pca.poseInterpolationWithPCA(chromosomeToLucyGeneticMatrix(genome), which_x) return int(mutations)