Beispiel #1
0
def GTreeMutatorRealRange(genome, **args):
    """ The mutator of GTree, Real Range Mutator
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   .. versionadded:: 0.6
      The *GTreeMutatorRealRange* function
   """

    if args["pmut"] <= 0.0:
        return 0
    elements = len(genome)
    mutations = args["pmut"] * elements

    range_min = genome.getParam("rangemin", constants.CDefRangeMin)
    range_max = genome.getParam("rangemax", constants.CDefRangeMax)

    if mutations < 1.0:
        mutations = 0
        for i in xrange(len(genome)):
            if utils.randomFlipCoin(args["pmut"]):
                mutations += 1
                rand_node = genome.getRandomNode()
                random_real = prng.uniform(range_min, range_max)
                rand_node.setData(random_real)

    else:
        for it in xrange(int(round(mutations))):
            rand_node = genome.getRandomNode()
            random_real = prng.uniform(range_min, range_max)
            rand_node.setData(random_real)

    return int(mutations)
Beispiel #2
0
def G1DListMutatorSIM(genome, **args):
    """
   The mutator of G1DList, Simple Inversion Mutation
   .. note:: this mutator is :term:`Data Type Independent`
   """

    mutations = 0
    if args["pmut"] <= 0.0:
        return 0

    cuts = [
        prng.randint(0,
                     len(genome) + 1),
        prng.randint(0,
                     len(genome) + 1)
    ]  # HERE IT SHOULD BE INCLUSIVE

    if cuts[0] > cuts[1]:
        utils.listSwapElement(cuts, 0, 1)

    if (cuts[1] - cuts[0]) <= 0:
        cuts[1] = prng.randint(cuts[0],
                               len(genome) + 1)  # HERE IT SHOULD BE INCLUSIVE

    if utils.randomFlipCoin(args["pmut"]):
        part = genome[cuts[0]:cuts[1]]
        if len(part) == 0:
            return 0
        part.reverse()
        genome[cuts[0]:cuts[1]] = part
        mutations += 1

    return mutations
Beispiel #3
0
def G1DListMutatorSIM(genome, **args):

   """
   The mutator of G1DList, Simple Inversion Mutation
   .. note:: this mutator is :term:`Data Type Independent`
   """

   mutations = 0
   if args["pmut"] <= 0.0:
      return 0

   cuts = [prng.randint(0, len(genome) + 1), prng.randint(0, len(genome) + 1)] # HERE IT SHOULD BE INCLUSIVE

   if cuts[0] > cuts[1]:
      utils.listSwapElement(cuts, 0, 1)

   if (cuts[1] - cuts[0]) <= 0:
      cuts[1] = prng.randint(cuts[0], len(genome) + 1) # HERE IT SHOULD BE INCLUSIVE

   if utils.randomFlipCoin(args["pmut"]):
      part = genome[cuts[0]:cuts[1]]
      if len(part) == 0:
         return 0
      part.reverse()
      genome[cuts[0]:cuts[1]] = part
      mutations += 1

   return mutations
Beispiel #4
0
def G2DBinaryStringMutatorSwap(genome, **args):
    """ The mutator of G2DBinaryString, Swap Mutator
   .. versionadded:: 0.6
      The *G2DBinaryStringMutatorSwap* function
   """

    if args["pmut"] <= 0.0:
        return 0
    height, width = genome.getSize()
    elements = height * width

    mutations = args["pmut"] * elements

    if mutations < 1.0:
        mutations = 0
        for i in xrange(height):
            for j in xrange(width):
                if utils.randomFlipCoin(args["pmut"]):
                    index_b = (prng.randint(0, height), prng.randint(0, width))
                    utils.list2DSwapElement(genome.genomeString, (i, j),
                                            index_b)
                    mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            index_a = (prng.randint(0, height), prng.randint(0, width))
            index_b = (prng.randint(0, height), prng.randint(0, width))
            utils.list2DSwapElement(genome.genomeString, index_a, index_b)

    return int(mutations)
Beispiel #5
0
def G2DBinaryStringXUniform(genome, **args):

    """
    The G2DBinaryString Uniform Crossover
    .. versionadded:: 0.6
      The *G2DBinaryStringXUniform* function
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    h, w = gMom.getSize()

    for i in xrange(h):
      for j in xrange(w):
         if utils.randomFlipCoin(constants.CDefG2DBinaryStringUniformProb):
            temp = sister.getItem(i, j)
            sister.setItem(i, j, brother.getItem(i, j))
            brother.setItem(i, j, temp)

    return (sister, brother)
Beispiel #6
0
def G2DBinaryStringMutatorSwap(genome, **args):

   """ The mutator of G2DBinaryString, Swap Mutator
   .. versionadded:: 0.6
      The *G2DBinaryStringMutatorSwap* function
   """

   if args["pmut"] <= 0.0:
      return 0
   height, width = genome.getSize()
   elements = height * width

   mutations = args["pmut"] * elements

   if mutations < 1.0:
      mutations = 0
      for i in xrange(height):
         for j in xrange(width):
            if utils.randomFlipCoin(args["pmut"]):
               index_b = (prng.randint(0, height), prng.randint(0, width))
               utils.list2DSwapElement(genome.genomeString, (i, j), index_b)
               mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         index_a = (prng.randint(0, height), prng.randint(0, width))
         index_b = (prng.randint(0, height), prng.randint(0, width))
         utils.list2DSwapElement(genome.genomeString, index_a, index_b)

   return int(mutations)
Beispiel #7
0
def G1DListMutatorRealRange(genome, **args):
    """ Simple real range mutator for G1DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

    if args["pmut"] <= 0.0:
        return 0
    listSize = len(genome)
    mutations = args["pmut"] * (listSize)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                genome[it] = prng.uniform(
                    genome.getParam("rangemin", constants.CDefRangeMin),
                    genome.getParam("rangemax", constants.CDefRangeMax))
                mutations += 1

    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            genome[which_gene] = prng.uniform(
                genome.getParam("rangemin", constants.CDefRangeMin),
                genome.getParam("rangemax", constants.CDefRangeMax))

    return int(mutations)
Beispiel #8
0
def GTreeMutatorRealRange(genome, **args):

   """ The mutator of GTree, Real Range Mutator
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   .. versionadded:: 0.6
      The *GTreeMutatorRealRange* function
   """

   if args["pmut"] <= 0.0:
      return 0
   elements = len(genome)
   mutations = args["pmut"] * elements

   range_min = genome.getParam("rangemin", constants.CDefRangeMin)
   range_max = genome.getParam("rangemax", constants.CDefRangeMax)

   if mutations < 1.0:
      mutations = 0
      for i in xrange(len(genome)):
         if utils.randomFlipCoin(args["pmut"]):
            mutations += 1
            rand_node = genome.getRandomNode()
            random_real = prng.uniform(range_min, range_max)
            rand_node.setData(random_real)

   else:
      for it in xrange(int(round(mutations))):
         rand_node = genome.getRandomNode()
         random_real = prng.uniform(range_min, range_max)
         rand_node.setData(random_real)

   return int(mutations)
Beispiel #9
0
def GTreeGPMutatorOperation(genome, **args):

   """ The mutator of GTreeGP, Operation Mutator
   .. versionadded:: 0.6
      The *GTreeGPMutatorOperation* function
   """

   if args["pmut"] <= 0.0:
      return 0
   elements = len(genome)
   mutations = args["pmut"] * elements
   ga_engine = args["ga_engine"]

   gp_terminals = ga_engine.getParam("gp_terminals")
   assert gp_terminals is not None

   gp_function_set = ga_engine.getParam("gp_function_set")
   assert gp_function_set is not None

   if mutations < 1.0:
      mutations = 0
      for i in xrange(len(genome)):
         if utils.randomFlipCoin(args["pmut"]):
            mutations += 1
            rand_node = genome.getRandomNode()
            assert rand_node is not None
            if rand_node.getType() == constants.nodeType["TERMINAL"]:
               term_operator = prng.choice(gp_terminals)
            else:
               op_len = gp_function_set[rand_node.getData()]
               fun_candidates = []
               for o, l in gp_function_set.items():
                  if l == op_len:
                     fun_candidates.append(o)

               if len(fun_candidates) <= 0:
                  continue

               term_operator = prng.choice(fun_candidates)
            rand_node.setData(term_operator)
   else:
      for it in xrange(int(round(mutations))):
         rand_node = genome.getRandomNode()
         assert rand_node is not None
         if rand_node.getType() == constants.nodeType["TERMINAL"]:
            term_operator = prng.choice(gp_terminals)
         else:
            op_len = gp_function_set[rand_node.getData()]
            fun_candidates = []
            for o, l in gp_function_set.items():
               if l == op_len:
                  fun_candidates.append(o)

            if len(fun_candidates) <= 0:
               continue

            term_operator = prng.choice(fun_candidates)
         rand_node.setData(term_operator)

   return int(mutations)
Beispiel #10
0
def GTreeMutatorSwap(genome, **args):

   """ The mutator of GTree, Swap Mutator
   .. versionadded:: 0.6
      The *GTreeMutatorSwap* function
   """

   if args["pmut"] <= 0.0:
      return 0
   elements = len(genome)
   mutations = args["pmut"] * elements

   if mutations < 1.0:
      mutations = 0
      for i in xrange(len(genome)):
         if utils.randomFlipCoin(args["pmut"]):
            mutations += 1
            nodeOne = genome.getRandomNode()
            nodeTwo = genome.getRandomNode()
            nodeOne.swapNodeData(nodeTwo)
   else:
      for it in xrange(int(round(mutations))):
         nodeOne = genome.getRandomNode()
         nodeTwo = genome.getRandomNode()
         nodeOne.swapNodeData(nodeTwo)

   return int(mutations)
Beispiel #11
0
def GTreeMutatorSwap(genome, **args):
    """ The mutator of GTree, Swap Mutator
   .. versionadded:: 0.6
      The *GTreeMutatorSwap* function
   """

    if args["pmut"] <= 0.0:
        return 0
    elements = len(genome)
    mutations = args["pmut"] * elements

    if mutations < 1.0:
        mutations = 0
        for i in xrange(len(genome)):
            if utils.randomFlipCoin(args["pmut"]):
                mutations += 1
                nodeOne = genome.getRandomNode()
                nodeTwo = genome.getRandomNode()
                nodeOne.swapNodeData(nodeTwo)
    else:
        for it in xrange(int(round(mutations))):
            nodeOne = genome.getRandomNode()
            nodeTwo = genome.getRandomNode()
            nodeOne.swapNodeData(nodeTwo)

    return int(mutations)
Beispiel #12
0
def G2DListMutatorIntegerRange(genome, **args):

   """ Simple integer range mutator for G2DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

   if args["pmut"] <= 0.0:
      return 0
   height, width = genome.getSize()
   elements = height * width

   mutations = args["pmut"] * elements

   range_min = genome.getParam("rangemin", constants.CDefRangeMin)
   range_max = genome.getParam("rangemax", constants.CDefRangeMax)

   if mutations < 1.0:
      mutations = 0
      for i in xrange(genome.getHeight()):
         for j in xrange(genome.getWidth()):
            if utils.randomFlipCoin(args["pmut"]):
               random_int = prng.randint(range_min, range_max + 1) # HERE IT SHOULD BE INCLUSIVE
               genome.setItem(i, j, random_int)
               mutations += 1

   else:
      for it in xrange(int(round(mutations))):
         which_x = prng.randint(0, genome.getWidth())
         which_y = prng.randint(0, genome.getHeight())
         random_int = prng.randint(range_min, range_max + 1) # HERE IT SHOULD BE INCLUSIVE
         genome.setItem(which_y, which_x, random_int)

   return int(mutations)
Beispiel #13
0
def G2DListMutatorSwap(genome, **args):
    """ The mutator of G1DList, Swap Mutator
   .. note:: this mutator is :term:`Data Type Independent`
   """

    if args["pmut"] <= 0.0:
        return 0
    height, width = genome.getSize()
    elements = height * width

    mutations = args["pmut"] * elements

    if mutations < 1.0:
        mutations = 0
        for i in xrange(height):
            for j in xrange(width):
                if utils.randomFlipCoin(args["pmut"]):
                    index_b = (prng.randint(0, height), prng.randint(0, width))
                    utils.list2DSwapElement(genome.genomeList, (i, j), index_b)
                    mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            index_a = (prng.randint(0, height), prng.randint(0, width))
            index_b = (prng.randint(0, height), prng.randint(0, width))
            utils.list2DSwapElement(genome.genomeList, index_a, index_b)

    return int(mutations)
Beispiel #14
0
def G1DListMutatorAllele(genome, **args):
    """ The mutator of G1DList, Allele Mutator
   To use this mutator, you must specify the *allele* genome parameter with the
   :class:`GAllele.GAlleles` instance.
   """

    if args["pmut"] <= 0.0:
        return 0
    listSize = len(genome)
    mutations = args["pmut"] * listSize

    allele = genome.getParam("allele", None)
    if allele is None:
        utils.raiseException(
            "to use the G1DListMutatorAllele, you must specify the 'allele' parameter",
            TypeError)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                new_val = allele[it].getRandomAllele()
                genome[it] = new_val
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            new_val = allele[which_gene].getRandomAllele()
            genome[which_gene] = new_val

    return int(mutations)
Beispiel #15
0
def G1DListMutatorIntegerBinary(genome, **args):
    """ The mutator of G1DList, the binary mutator
   This mutator will random change the 0 and 1 elements of the 1D List.

   """
    if args["pmut"] <= 0.0:
        return 0
    listSize = len(genome)
    mutations = args["pmut"] * (listSize)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                if genome[it] == 0:
                    genome[it] = 1
                elif genome[it] == 1:
                    genome[it] = 0

                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            if genome[which_gene] == 0:
                genome[which_gene] = 1
            elif genome[which_gene] == 1:
                genome[which_gene] = 0

    return int(mutations)
Beispiel #16
0
def G1DBinaryStringMutatorFlip(genome, **args):
    """
   The classical flip mutator for binary strings
   """

    if args["pmut"] <= 0.0:
        return 0
    stringLength = len(genome)
    mutations = args["pmut"] * (stringLength)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(stringLength):
            if utils.randomFlipCoin(args["pmut"]):
                if genome[it] == 0:
                    genome[it] = 1
                else:
                    genome[it] = 0
                mutations += 1

    else:
        for it in xrange(int(round(mutations))):
            which = prng.randint(0, stringLength)
            if genome[which] == 0:
                genome[which] = 1
            else:
                genome[which] = 0

    return int(mutations)
Beispiel #17
0
def G2DListMutatorIntegerRange(genome, **args):
    """ Simple integer range mutator for G2DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

    if args["pmut"] <= 0.0:
        return 0
    height, width = genome.getSize()
    elements = height * width

    mutations = args["pmut"] * elements

    range_min = genome.getParam("rangemin", constants.CDefRangeMin)
    range_max = genome.getParam("rangemax", constants.CDefRangeMax)

    if mutations < 1.0:
        mutations = 0
        for i in xrange(genome.getHeight()):
            for j in xrange(genome.getWidth()):
                if utils.randomFlipCoin(args["pmut"]):
                    random_int = prng.randint(range_min, range_max +
                                              1)  # HERE IT SHOULD BE INCLUSIVE
                    genome.setItem(i, j, random_int)
                    mutations += 1

    else:
        for it in xrange(int(round(mutations))):
            which_x = prng.randint(0, genome.getWidth())
            which_y = prng.randint(0, genome.getHeight())
            random_int = prng.randint(range_min, range_max +
                                      1)  # HERE IT SHOULD BE INCLUSIVE
            genome.setItem(which_y, which_x, random_int)

    return int(mutations)
Beispiel #18
0
def G2DListCrossoverUniform(genome, **args):
    """
    The G2DList Uniform Crossover
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    h, w = gMom.getSize()

    for i in xrange(h):
        for j in xrange(w):
            if utils.randomFlipCoin(constants.CDefG2DListCrossUniformProb):
                temp = sister.getItem(i, j)
                sister.setItem(i, j, brother.getItem(i, j))
                brother.setItem(i, j, temp)

    return (sister, brother)
Beispiel #19
0
def G1DListMutatorIntegerRange(genome, **args):

   """ Simple integer range mutator for G1DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

   if args["pmut"] <= 0.0:
      return 0
   listSize = len(genome)
   mutations = args["pmut"] * listSize

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            genome[it] = prng.randint(genome.getParam("rangemin", constants.CDefRangeMin), # HERE IT SHOULD BE INCLUSIVE
                                      genome.getParam("rangemax", constants.CDefRangeMax) + 1) # HERE IT SHOULD BE INCLUSIVE
            mutations += 1

   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         genome[which_gene] = prng.randint(genome.getParam("rangemin", constants.CDefRangeMin), # HERE IT SHOULD BE INCLUSIVE
                                           genome.getParam("rangemax", constants.CDefRangeMax) + 1) # HERE IT SHOULD BE INCLUSIVE

   return int(mutations)
Beispiel #20
0
def G1DBinaryStringMutatorFlip(genome, **args):

   """
   The classical flip mutator for binary strings
   """

   if args["pmut"] <= 0.0:
      return 0
   stringLength = len(genome)
   mutations = args["pmut"] * (stringLength)

   if mutations < 1.0:
      mutations = 0
      for it in xrange(stringLength):
         if utils.randomFlipCoin(args["pmut"]):
            if genome[it] == 0:
               genome[it] = 1
            else:
               genome[it] = 0
            mutations += 1

   else:
      for it in xrange(int(round(mutations))):
         which = prng.randint(0, stringLength)
         if genome[which] == 0:
            genome[which] = 1
         else:
            genome[which] = 0

   return int(mutations)
Beispiel #21
0
def G2DListMutatorSwap(genome, **args):

   """ The mutator of G1DList, Swap Mutator
   .. note:: this mutator is :term:`Data Type Independent`
   """

   if args["pmut"] <= 0.0:
      return 0
   height, width = genome.getSize()
   elements = height * width

   mutations = args["pmut"] * elements

   if mutations < 1.0:
      mutations = 0
      for i in xrange(height):
         for j in xrange(width):
            if utils.randomFlipCoin(args["pmut"]):
               index_b = (prng.randint(0, height), prng.randint(0, width))
               utils.list2DSwapElement(genome.genomeList, (i, j), index_b)
               mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         index_a = (prng.randint(0, height), prng.randint(0, width))
         index_b = (prng.randint(0, height), prng.randint(0, width))
         utils.list2DSwapElement(genome.genomeList, index_a, index_b)

   return int(mutations)
Beispiel #22
0
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", constants.CDefG1DListMutRealMU)
   sigma = genome.getParam("gauss_sigma", constants.CDefG1DListMutRealSIGMA)

   if mutations < 1.0:
      mutations = 0
      for i in xrange(len(genome)):
         if utils.randomFlipCoin(args["pmut"]):
            mutations += 1
            rand_node = genome.getRandomNode()
            final_value = rand_node.getData() + prng.normal(mu, sigma)
            final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
            final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))
            rand_node.setData(final_value)
   else:
      for it in xrange(int(round(mutations))):
         rand_node = genome.getRandomNode()
         final_value = rand_node.getData() + prng.normal(mu, sigma)
         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))
         rand_node.setData(final_value)

   return int(mutations)
Beispiel #23
0
def G1DListMutatorIntegerRange(genome, **args):
    """ Simple integer range mutator for G1DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

    if args["pmut"] <= 0.0:
        return 0
    listSize = len(genome)
    mutations = args["pmut"] * listSize

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                genome[it] = prng.randint(
                    genome.getParam(
                        "rangemin",
                        constants.CDefRangeMin),  # HERE IT SHOULD BE INCLUSIVE
                    genome.getParam("rangemax", constants.CDefRangeMax) +
                    1)  # HERE IT SHOULD BE INCLUSIVE
                mutations += 1

    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            genome[which_gene] = prng.randint(
                genome.getParam(
                    "rangemin",
                    constants.CDefRangeMin),  # HERE IT SHOULD BE INCLUSIVE
                genome.getParam("rangemax", constants.CDefRangeMax) +
                1)  # HERE IT SHOULD BE INCLUSIVE

    return int(mutations)
Beispiel #24
0
def HeterogeneousListMutatorRealRange(genome, **args):

    """
    Real range mutator for HeterogeneousList
    :param genome:
    :param args:
    :return:
    """

    if args["pmut"] <= 0.0:
        return 0

    listSize = len(genome)
    mutations = args["pmut"] * (listSize)

    if mutations < 1.0:

        mutations = 0
        for it in xrange(listSize):

            if utils.randomFlipCoin(args["pmut"]):

                genome[it] = prng.uniform(genome.getParam("minima")[it], genome.getParam("maxima")[it])
                mutations += 1

    else:

        for it in xrange(int(round(mutations))):

            which_gene = prng.randint(0, listSize)

            genome[which_gene] = prng.uniform(genome.getParam("minima")[which_gene], genome.getParam("maxima")[which_gene])

    return int(mutations)
Beispiel #25
0
def G2DListCrossoverUniform(genome, **args):

    """
    The G2DList Uniform Crossover
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    h, w = gMom.getSize()

    for i in xrange(h):
      for j in xrange(w):
         if utils.randomFlipCoin(constants.CDefG2DListCrossUniformProb):
            temp = sister.getItem(i, j)
            sister.setItem(i, j, brother.getItem(i, j))
            brother.setItem(i, j, temp)

    return (sister, brother)
Beispiel #26
0
def G1DListMutatorAllele(genome, **args):

   """ The mutator of G1DList, Allele Mutator
   To use this mutator, you must specify the *allele* genome parameter with the
   :class:`GAllele.GAlleles` instance.
   """

   if args["pmut"] <= 0.0:
      return 0
   listSize = len(genome)
   mutations = args["pmut"] * listSize

   allele = genome.getParam("allele", None)
   if allele is None:
      utils.raiseException("to use the G1DListMutatorAllele, you must specify the 'allele' parameter", TypeError)

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            new_val = allele[it].getRandomAllele()
            genome[it] = new_val
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         new_val = allele[which_gene].getRandomAllele()
         genome[which_gene] = new_val

   return int(mutations)
Beispiel #27
0
def G1DListMutatorIntegerBinary(genome, **args):

   """ The mutator of G1DList, the binary mutator
   This mutator will random change the 0 and 1 elements of the 1D List.

   """
   if args["pmut"] <= 0.0:
      return 0
   listSize = len(genome)
   mutations = args["pmut"] * (listSize)

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            if genome[it] == 0:
               genome[it] = 1
            elif genome[it] == 1:
               genome[it] = 0

            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         if genome[which_gene] == 0:
            genome[which_gene] = 1
         elif genome[which_gene] == 1:
            genome[which_gene] = 0

   return int(mutations)
Beispiel #28
0
def GTreeGPInitializator(genome, **args):

    """This initializator accepts the follow parameters:
    *max_depth*
       The max depth of the tree
    *method*
       The method, accepts "grow", "full" or "ramped"
    .. versionadded:: 0.6
       The *GTreeGPInitializator* function.
    """

    max_depth = genome.getParam("max_depth", 5)
    method = genome.getParam("method", "grow")
    ga_engine = args["ga_engine"]

    if method == "grow":
        root = tree.buildGTreeGPGrow(ga_engine, 0, max_depth)
    elif method == "full":
        root = tree.buildGTreeGPFull(ga_engine, 0, max_depth)
    elif method == "ramped":
        if utils.randomFlipCoin(0.5):
            root = tree.buildGTreeGPFull(ga_engine, 0, max_depth)
        else:
            root = tree.buildGTreeGPGrow(ga_engine, 0, max_depth)
    else:
        utils.raiseException("Unknown tree initialization method [%s] !" % method)

    genome.setRoot(root)
    genome.processNodes()
    assert genome.getHeight() <= max_depth
Beispiel #29
0
def G1DListMutatorRealRange(genome, **args):

   """ Simple real range mutator for G1DList
   Accepts the *rangemin* and *rangemax* genome parameters, both optional.
   """

   if args["pmut"] <= 0.0:
      return 0
   listSize = len(genome)
   mutations = args["pmut"] * (listSize)

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            genome[it] = prng.uniform(genome.getParam("rangemin", constants.CDefRangeMin),
                                      genome.getParam("rangemax", constants.CDefRangeMax))
            mutations += 1

   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         genome[which_gene] = prng.uniform(genome.getParam("rangemin", constants.CDefRangeMin),
                                           genome.getParam("rangemax", constants.CDefRangeMax))

   return int(mutations)
Beispiel #30
0
def G2DBinaryStringXUniform(genome, **args):
    """
    The G2DBinaryString Uniform Crossover
    .. versionadded:: 0.6
      The *G2DBinaryStringXUniform* function
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    h, w = gMom.getSize()

    for i in xrange(h):
        for j in xrange(w):
            if utils.randomFlipCoin(constants.CDefG2DBinaryStringUniformProb):
                temp = sister.getItem(i, j)
                sister.setItem(i, j, brother.getItem(i, j))
                brother.setItem(i, j, temp)

    return (sister, brother)
Beispiel #31
0
def G2DListMutatorIntegerGaussian(genome, **args):
    """
   A gaussian mutator for G2DList 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
    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 = constants.CDefG2DListMutIntMU

    if sigma is None:
        sigma = constants.CDefG2DListMutIntSIGMA

    if mutations < 1.0:
        mutations = 0

        for i in xrange(genome.getHeight()):
            for j in xrange(genome.getWidth()):
                if utils.randomFlipCoin(args["pmut"]):
                    final_value = genome[i][j] + int(prng.normal(mu, sigma))

                    final_value = min(
                        final_value,
                        genome.getParam("rangemax", constants.CDefRangeMax))
                    final_value = max(
                        final_value,
                        genome.getParam("rangemin", constants.CDefRangeMin))

                    genome.setItem(i, j, final_value)
                    mutations += 1
    else:

        for it in xrange(int(round(mutations))):
            which_x = prng.randint(0, genome.getWidth())
            which_y = prng.randint(0, genome.getHeight())

            final_value = genome[which_y][which_x] + int(prng.normal(
                mu, sigma))

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome.setItem(which_y, which_x, final_value)

    return int(mutations)
Beispiel #32
0
def GTreeGPMutatorOperation(genome, **args):
    """ The mutator of GTreeGP, Operation Mutator
   .. versionadded:: 0.6
      The *GTreeGPMutatorOperation* function
   """

    if args["pmut"] <= 0.0:
        return 0
    elements = len(genome)
    mutations = args["pmut"] * elements
    ga_engine = args["ga_engine"]

    gp_terminals = ga_engine.getParam("gp_terminals")
    assert gp_terminals is not None

    gp_function_set = ga_engine.getParam("gp_function_set")
    assert gp_function_set is not None

    if mutations < 1.0:
        mutations = 0
        for i in xrange(len(genome)):
            if utils.randomFlipCoin(args["pmut"]):
                mutations += 1
                rand_node = genome.getRandomNode()
                assert rand_node is not None
                if rand_node.getType() == constants.nodeType["TERMINAL"]:
                    term_operator = prng.choice(gp_terminals)
                else:
                    op_len = gp_function_set[rand_node.getData()]
                    fun_candidates = []
                    for o, l in gp_function_set.items():
                        if l == op_len:
                            fun_candidates.append(o)

                    if len(fun_candidates) <= 0:
                        continue

                    term_operator = prng.choice(fun_candidates)
                rand_node.setData(term_operator)
    else:
        for it in xrange(int(round(mutations))):
            rand_node = genome.getRandomNode()
            assert rand_node is not None
            if rand_node.getType() == constants.nodeType["TERMINAL"]:
                term_operator = prng.choice(gp_terminals)
            else:
                op_len = gp_function_set[rand_node.getData()]
                fun_candidates = []
                for o, l in gp_function_set.items():
                    if l == op_len:
                        fun_candidates.append(o)

                if len(fun_candidates) <= 0:
                    continue

                term_operator = prng.choice(fun_candidates)
            rand_node.setData(term_operator)

    return int(mutations)
Beispiel #33
0
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 = constants.CDefGaussianGradientMU
    sigma = constants.CDefGaussianGradientSIGMA

    if mutations < 1.0:
        mutations = 0

        for i in xrange(genome.getHeight()):
            for j in xrange(genome.getWidth()):
                if utils.randomFlipCoin(args["pmut"]):
                    final_value = int(genome[i][j] *
                                      abs(prng.normal(mu, sigma)))

                    final_value = min(
                        final_value,
                        genome.getParam("rangemax", constants.CDefRangeMax))
                    final_value = max(
                        final_value,
                        genome.getParam("rangemin", constants.CDefRangeMin))

                    genome.setItem(i, j, final_value)
                    mutations += 1
    else:

        for it in xrange(int(round(mutations))):
            which_x = prng.randint(0, genome.getWidth())
            which_y = prng.randint(0, genome.getHeight())

            final_value = int(genome[which_y][which_x] *
                              abs(prng.normal(mu, sigma)))

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome.setItem(which_y, which_x, final_value)

    return int(mutations)
Beispiel #34
0
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 = constants.CDefGaussianGradientMU
    sigma = constants.CDefGaussianGradientSIGMA

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                final_value = genome[it] * abs(prng.normal(mu, sigma))

                final_value = min(
                    final_value,
                    genome.getParam("rangemax", constants.CDefRangeMax))
                final_value = max(
                    final_value,
                    genome.getParam("rangemin", constants.CDefRangeMin))

                genome[it] = final_value
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            final_value = genome[which_gene] * abs(prng.normal(mu, sigma))

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome[which_gene] = final_value

    return int(mutations)
Beispiel #35
0
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 = constants.CDefGaussianGradientMU
    sigma = constants.CDefGaussianGradientSIGMA

    if mutations < 1.0:
        mutations = 0

        for i in xrange(genome.getHeight()):
            for j in xrange(genome.getWidth()):
                if utils.randomFlipCoin(args["pmut"]):
                    final_value = genome[i][j] * abs(prng.normal(mu, sigma))

                    final_value = min(
                        final_value,
                        genome.getParam("rangemax", constants.CDefRangeMax))
                    final_value = max(
                        final_value,
                        genome.getParam("rangemin", constants.CDefRangeMin))

                    genome.setItem(i, j, final_value)
                    mutations += 1
    else:

        for it in xrange(int(round(mutations))):
            which_x = prng.randint(0, genome.getWidth())
            which_y = prng.randint(0, genome.getHeight())

            final_value = genome[which_y][which_x] * abs(prng.normal(
                mu, sigma))

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome.setItem(which_y, which_x, final_value)

    return int(mutations)
Beispiel #36
0
def G2DListMutatorIntegerGaussian(genome, **args):

   """
   A gaussian mutator for G2DList 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
   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 = constants.CDefG2DListMutIntMU

   if sigma is None:
      sigma = constants.CDefG2DListMutIntSIGMA

   if mutations < 1.0:
      mutations = 0

      for i in xrange(genome.getHeight()):
         for j in xrange(genome.getWidth()):
            if utils.randomFlipCoin(args["pmut"]):
               final_value = genome[i][j] + int(prng.normal(mu, sigma))

               final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
               final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

               genome.setItem(i, j, final_value)
               mutations += 1
   else:

      for it in xrange(int(round(mutations))):
         which_x = prng.randint(0, genome.getWidth())
         which_y = prng.randint(0, genome.getHeight())

         final_value = genome[which_y][which_x] + int(prng.normal(mu, sigma))

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome.setItem(which_y, which_x, final_value)

   return int(mutations)
Beispiel #37
0
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.
   """

    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 = constants.CDefG1DListMutRealMU

    if sigma is None:
        sigma = constants.CDefG1DListMutRealSIGMA

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                final_value = genome[it] + prng.normal(mu, sigma)

                final_value = min(
                    final_value,
                    genome.getParam("rangemax", constants.CDefRangeMax))
                final_value = max(
                    final_value,
                    genome.getParam("rangemin", constants.CDefRangeMin))

                genome[it] = final_value
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            final_value = genome[which_gene] + prng.normal(mu, sigma)

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome[which_gene] = final_value

    return int(mutations)
Beispiel #38
0
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 = constants.CDefGaussianGradientMU
    sigma = constants.CDefGaussianGradientSIGMA

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                final_value = int(genome[it] * abs(prng.normal(mu, sigma)))

                final_value = min(
                    final_value,
                    genome.getParam("rangemax", constants.CDefRangeMax))
                final_value = max(
                    final_value,
                    genome.getParam("rangemin", constants.CDefRangeMin))

                genome[it] = final_value
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_gene = prng.randint(0, listSize)
            final_value = int(genome[which_gene] * abs(prng.normal(mu, sigma)))

            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))

            genome[which_gene] = final_value

    return int(mutations)
Beispiel #39
0
def GTreeGPMutatorSubtree(genome, **args):
    """ The mutator of GTreeGP, Subtree Mutator
   This mutator will recreate random subtree of the tree using the grow algorithm.
   .. versionadded:: 0.6
      The *GTreeGPMutatorSubtree* function
   """

    if args["pmut"] <= 0.0:
        return 0
    ga_engine = args["ga_engine"]
    max_depth = genome.getParam("max_depth", None)
    mutations = 0

    if max_depth is None:
        utils.raiseException(
            "You must specify the max_depth genome parameter !", ValueError)

    if max_depth < 0:
        utils.raiseException(
            "The max_depth must be >= 1, if you want to use GTreeGPMutatorSubtree crossover !",
            ValueError)

    branch_list = genome.nodes_branch
    elements = len(branch_list)

    for i in xrange(elements):

        node = branch_list[i]
        assert node is not None

        if utils.randomFlipCoin(args["pmut"]):
            depth = genome.getNodeDepth(node)
            mutations += 1

            root_subtree = tree.buildGTreeGPGrow(ga_engine, 0,
                                                 max_depth - depth)
            node_parent = node.getParent()

            if node_parent is None:
                genome.setRoot(root_subtree)
                genome.processNodes()
                return mutations
            else:
                root_subtree.setParent(node_parent)
                node_parent.replaceChild(node, root_subtree)
            genome.processNodes()

    return int(mutations)
Beispiel #40
0
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 = constants.CDefG1DListMutRealMU
    if sigma is None:
        sigma = constants.CDefG1DListMutRealSIGMA

    allele = genome.getParam("allele", None)
    if allele is None:
        utils.raiseException(
            "to use this mutator, you must specify the 'allele' parameter",
            TypeError)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(arguments["pmut"]):
                final_value = genome[it] + prng.normal(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 = prng.randint(0, listSize)
            final_value = genome[which_gene] + prng.normal(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)
Beispiel #41
0
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 = constants.CDefGaussianGradientMU
   sigma = constants.CDefGaussianGradientSIGMA

   if mutations < 1.0:
      mutations = 0

      for i in xrange(genome.getHeight()):
         for j in xrange(genome.getWidth()):
            if utils.randomFlipCoin(args["pmut"]):
               final_value = int(genome[i][j] * abs(prng.normal(mu, sigma)))

               final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
               final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

               genome.setItem(i, j, final_value)
               mutations += 1
   else:

      for it in xrange(int(round(mutations))):
         which_x = prng.randint(0, genome.getWidth())
         which_y = prng.randint(0, genome.getHeight())

         final_value = int(genome[which_y][which_x] * abs(prng.normal(mu, sigma)))

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome.setItem(which_y, which_x, final_value)

   return int(mutations)
Beispiel #42
0
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 = constants.CDefGaussianGradientMU
   sigma = constants.CDefGaussianGradientSIGMA

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            final_value = genome[it] * abs(prng.normal(mu, sigma))

            final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
            final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

            genome[it] = final_value
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         final_value = genome[which_gene] * abs(prng.normal(mu, sigma))

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome[which_gene] = final_value

   return int(mutations)
Beispiel #43
0
def HeterogeneousListMutatorRealGaussian(genome, **args):
    """
   Heregogeneous version of real gaussian list mutator
   """

    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 = constants.CDefG1DListMutRealMU

    if sigma is None:
        sigma = constants.CDefG1DListMutRealSIGMA

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):

                final_value = genome[it] + prng.normal(mu, sigma)

                final_value = min(final_value, genome.getParam("maxima")[it])
                final_value = max(final_value, genome.getParam("minima")[it])

                genome[it] = final_value
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):

            which_gene = prng.randint(0, listSize)
            final_value = genome[which_gene] + prng.normal(mu, sigma)

            final_value = min(final_value,
                              genome.getParam("maxima")[which_gene])
            final_value = max(final_value,
                              genome.getParam("minima")[which_gene])

            genome[which_gene] = final_value

    return int(mutations)
Beispiel #44
0
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 = constants.CDefG1DListMutRealMU
    if sigma is None:
        sigma = constants.CDefG1DListMutRealSIGMA

    allele = genome.getParam("allele", None)
    if allele is None:
        utils.raiseException("to use this mutator, you must specify the 'allele' parameter", TypeError)

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(arguments["pmut"]):
                final_value = genome[it] + prng.normal(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 = prng.randint(0, listSize)
            final_value = genome[which_gene] + prng.normal(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)
Beispiel #45
0
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 = constants.CDefGaussianGradientMU
   sigma = constants.CDefGaussianGradientSIGMA

   if mutations < 1.0:
      mutations = 0

      for i in xrange(genome.getHeight()):
         for j in xrange(genome.getWidth()):
            if utils.randomFlipCoin(args["pmut"]):
               final_value = genome[i][j] * abs(prng.normal(mu, sigma))

               final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
               final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

               genome.setItem(i, j, final_value)
               mutations += 1
   else:

      for it in xrange(int(round(mutations))):
         which_x = prng.randint(0, genome.getWidth())
         which_y = prng.randint(0, genome.getHeight())

         final_value = genome[which_y][which_x] * abs(prng.normal(mu, sigma))

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome.setItem(which_y, which_x, final_value)

   return int(mutations)
Beispiel #46
0
def GTreeGPMutatorSubtree(genome, **args):

   """ The mutator of GTreeGP, Subtree Mutator
   This mutator will recreate random subtree of the tree using the grow algorithm.
   .. versionadded:: 0.6
      The *GTreeGPMutatorSubtree* function
   """

   if args["pmut"] <= 0.0:
      return 0
   ga_engine = args["ga_engine"]
   max_depth = genome.getParam("max_depth", None)
   mutations = 0

   if max_depth is None:
      utils.raiseException("You must specify the max_depth genome parameter !", ValueError)

   if max_depth < 0:
      utils.raiseException("The max_depth must be >= 1, if you want to use GTreeGPMutatorSubtree crossover !", ValueError)

   branch_list = genome.nodes_branch
   elements = len(branch_list)

   for i in xrange(elements):

      node = branch_list[i]
      assert node is not None

      if utils.randomFlipCoin(args["pmut"]):
         depth = genome.getNodeDepth(node)
         mutations += 1

         root_subtree = tree.buildGTreeGPGrow(ga_engine, 0, max_depth - depth)
         node_parent = node.getParent()

         if node_parent is None:
            genome.setRoot(root_subtree)
            genome.processNodes()
            return mutations
         else:
            root_subtree.setParent(node_parent)
            node_parent.replaceChild(node, root_subtree)
         genome.processNodes()

   return int(mutations)
Beispiel #47
0
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.
   """

   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 = constants.CDefG1DListMutRealMU

   if sigma is None:
      sigma = constants.CDefG1DListMutRealSIGMA

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            final_value = genome[it] + prng.normal(mu, sigma)

            final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
            final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

            genome[it] = final_value
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         final_value = genome[which_gene] + prng.normal(mu, sigma)

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome[which_gene] = final_value

   return int(mutations)
Beispiel #48
0
def GTreeInitializatorInteger(genome, **args):

    """ Integer initialization function of GTree
    This initializator accepts the *rangemin* and *rangemax* genome parameters.
    It accepts the following parameters too:

    *max_depth*
       The max depth of the tree

    *max_siblings*
       The number of maximum siblings of an node

    *method*
       The method, accepts "grow", "full" or "ramped".

    .. versionadded:: 0.6
       The *GTreeInitializatorInteger* function.
    """

    max_depth = genome.getParam("max_depth", 5)
    max_siblings = genome.getParam("max_siblings", 2)

    range_min = genome.getParam("rangemin", 0)
    range_max = genome.getParam("rangemax", 100)

    lambda_generator = lambda: prng.randint(range_min, range_max + 1) # HERE IT SHOULD BE INCLUSIVE

    method = genome.getParam("method", "grow")

    if method == "grow":
        root = tree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth)
    elif method == "full":
        root = tree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth)
    elif method == "ramped":
        if utils.randomFlipCoin(0.5):
            root = tree.buildGTreeGrow(0, lambda_generator, max_siblings, max_depth)
        else:
            root = tree.buildGTreeFull(0, lambda_generator, max_siblings, max_depth)
    else:
        utils.raiseException("Unknown tree initialization method [%s] !" % method)

    genome.setRoot(root)
    genome.processNodes()
    assert genome.getHeight() <= max_depth
Beispiel #49
0
def HeterogeneousListMutatorRealGaussian(genome, **args):

   """
   Heregogeneous version of real gaussian list mutator
   """

   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 = constants.CDefG1DListMutRealMU

   if sigma is None:
      sigma = constants.CDefG1DListMutRealSIGMA

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):

            final_value = genome[it] + prng.normal(mu, sigma)

            final_value = min(final_value, genome.getParam("maxima")[it])
            final_value = max(final_value, genome.getParam("minima")[it])

            genome[it] = final_value
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):

         which_gene = prng.randint(0, listSize)
         final_value = genome[which_gene] + prng.normal(mu, sigma)

         final_value = min(final_value, genome.getParam("maxima")[which_gene])
         final_value = max(final_value, genome.getParam("minima")[which_gene])

         genome[which_gene] = final_value

   return int(mutations)
Beispiel #50
0
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 = constants.CDefGaussianGradientMU
   sigma = constants.CDefGaussianGradientSIGMA

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            final_value = int(genome[it] * abs(prng.normal(mu, sigma)))

            final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
            final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

            genome[it] = final_value
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_gene = prng.randint(0, listSize)
         final_value = int(genome[which_gene] * abs(prng.normal(mu, sigma)))

         final_value = min(final_value, genome.getParam("rangemax", constants.CDefRangeMax))
         final_value = max(final_value, genome.getParam("rangemin", constants.CDefRangeMin))

         genome[which_gene] = final_value

   return int(mutations)
Beispiel #51
0
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", constants.CDefG1DListMutRealMU)
    sigma = genome.getParam("gauss_sigma", constants.CDefG1DListMutRealSIGMA)

    if mutations < 1.0:
        mutations = 0
        for i in xrange(len(genome)):
            if utils.randomFlipCoin(args["pmut"]):
                mutations += 1
                rand_node = genome.getRandomNode()
                final_value = rand_node.getData() + prng.normal(mu, sigma)
                final_value = min(
                    final_value,
                    genome.getParam("rangemax", constants.CDefRangeMax))
                final_value = max(
                    final_value,
                    genome.getParam("rangemin", constants.CDefRangeMin))
                rand_node.setData(final_value)
    else:
        for it in xrange(int(round(mutations))):
            rand_node = genome.getRandomNode()
            final_value = rand_node.getData() + prng.normal(mu, sigma)
            final_value = min(
                final_value, genome.getParam("rangemax",
                                             constants.CDefRangeMax))
            final_value = max(
                final_value, genome.getParam("rangemin",
                                             constants.CDefRangeMin))
            rand_node.setData(final_value)

    return int(mutations)
Beispiel #52
0
def G2DListMutatorAllele(genome, **args):
    """ The mutator of G2DList, Allele Mutator
   To use this mutator, you must specify the *allele* genome parameter with the
   :class:`GAllele.GAlleles` instance.
   .. warning:: the :class:`GAllele.GAlleles` instance must have the homogeneous flag enabled
   """

    if args["pmut"] <= 0.0:
        return 0
    listSize = genome.getHeight() * genome.getWidth() - 1
    mutations = args["pmut"] * (listSize + 1)

    allele = genome.getParam("allele", None)
    if allele is None:
        utils.raiseException(
            "to use the G2DListMutatorAllele, you must specify the 'allele' parameter",
            TypeError)

    if not allele.homogeneous:
        utils.raiseException(
            "to use the G2DListMutatorAllele, the 'allele' must be homogeneous"
        )

    if mutations < 1.0:
        mutations = 0

        for i in xrange(genome.getHeight()):
            for j in xrange(genome.getWidth()):
                if utils.randomFlipCoin(args["pmut"]):
                    new_val = allele[0].getRandomAllele()
                    genome.setItem(i, j, new_val)
                    mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            which_x = prng.randint(0, genome.getHeight())
            which_y = prng.randint(0, genome.getWidth())

            new_val = allele[0].getRandomAllele()
            genome.setItem(which_x, which_y, new_val)

    return int(mutations)
Beispiel #53
0
def G1DBinaryStringXUniform(genome, **args):
    """
    The G1DList Uniform Crossover
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    for i in xrange(len(gMom)):
        if utils.randomFlipCoin(constants.CDefG1DBinaryStringUniformProb):
            temp = sister[i]
            sister[i] = brother[i]
            brother[i] = temp

    return (sister, brother)
Beispiel #54
0
def G1DListMutatorSwap(genome, **args):
    """ The mutator of G1DList, Swap Mutator
   .. note:: this mutator is :term:`Data Type Independent`
   """

    if args["pmut"] <= 0.0:
        return 0
    listSize = len(genome)
    mutations = args["pmut"] * listSize

    if mutations < 1.0:
        mutations = 0
        for it in xrange(listSize):
            if utils.randomFlipCoin(args["pmut"]):
                utils.listSwapElement(genome, it, prng.randint(0, listSize))
                mutations += 1
    else:
        for it in xrange(int(round(mutations))):
            utils.listSwapElement(genome, prng.randint(0, listSize),
                                  prng.randint(0, listSize))

    return int(mutations)
Beispiel #55
0
def G1DListMutatorSwap(genome, **args):

   """ The mutator of G1DList, Swap Mutator
   .. note:: this mutator is :term:`Data Type Independent`
   """

   if args["pmut"] <= 0.0:
      return 0
   listSize = len(genome)
   mutations = args["pmut"] * listSize

   if mutations < 1.0:
      mutations = 0
      for it in xrange(listSize):
         if utils.randomFlipCoin(args["pmut"]):
            utils.listSwapElement(genome, it, prng.randint(0, listSize))
            mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         utils.listSwapElement(genome, prng.randint(0, listSize), prng.randint(0, listSize))

   return int(mutations)
Beispiel #56
0
def G2DListMutatorAllele(genome, **args):

   """ The mutator of G2DList, Allele Mutator
   To use this mutator, you must specify the *allele* genome parameter with the
   :class:`GAllele.GAlleles` instance.
   .. warning:: the :class:`GAllele.GAlleles` instance must have the homogeneous flag enabled
   """

   if args["pmut"] <= 0.0:
      return 0
   listSize = genome.getHeight() * genome.getWidth() - 1
   mutations = args["pmut"] * (listSize + 1)

   allele = genome.getParam("allele", None)
   if allele is None:
      utils.raiseException("to use the G2DListMutatorAllele, you must specify the 'allele' parameter", TypeError)

   if not allele.homogeneous:
      utils.raiseException("to use the G2DListMutatorAllele, the 'allele' must be homogeneous")

   if mutations < 1.0:
      mutations = 0

      for i in xrange(genome.getHeight()):
         for j in xrange(genome.getWidth()):
            if utils.randomFlipCoin(args["pmut"]):
               new_val = allele[0].getRandomAllele()
               genome.setItem(i, j, new_val)
               mutations += 1
   else:
      for it in xrange(int(round(mutations))):
         which_x = prng.randint(0, genome.getHeight())
         which_y = prng.randint(0, genome.getWidth())

         new_val = allele[0].getRandomAllele()
         genome.setItem(which_x, which_y, new_val)

   return int(mutations)
Beispiel #57
0
def G1DListCrossoverUniform(genome, **args):
    """
    The G1DList Uniform Crossover
    Each gene has a 50% chance of being swapped between mom and dad
    """

    sister = None
    brother = None
    gMom = args["mom"]
    gDad = args["dad"]

    sister = gMom.clone()
    brother = gDad.clone()
    sister.resetStats()
    brother.resetStats()

    for i in xrange(len(gMom)):
        if utils.randomFlipCoin(constants.CDefG1DListCrossUniformProb):
            temp = sister[i]
            sister[i] = brother[i]
            brother[i] = temp

    return (sister, brother)
Beispiel #58
0
def G1DBinaryStringMutatorSwap(genome, **args):

   """
   The 1D Binary String Swap Mutator
   """

   if args["pmut"] <= 0.0:
      return 0
   stringLength = len(genome)
   mutations = args["pmut"] * (stringLength)

   if mutations < 1.0:
      mutations = 0
      for it in xrange(stringLength):
         if utils.randomFlipCoin(args["pmut"]):
            utils.listSwapElement(genome, it, prng.randint(0, stringLength))
            mutations += 1

   else:
      for it in xrange(int(round(mutations))):
         utils.listSwapElement(genome, prng.randint(0, stringLength), prng.randint(0, stringLength))

   return int(mutations)