如何使用 Pyevolve 在实值染色体内设置不同的参数范围?

How to set different parameter ranges within a real valued chromosome using Pyevolve?

我正在尝试使用 pyevolve 来实现一个真正有价值的遗传算法。 (此处提供示例文档:http://pyevolve.sourceforge.net/examples.html#example-2-real-numbers-gaussian-mutator

参数的范围(本例中为20)可以使用setParams设置如下:

# Genome instance genome = G1DList.G1DList(20) genome.setParams(rangemin=-6.0, rangemax=6.0)

但是,相同的范围适用于所有 20 个。我希望参数有不同的范围。我尝试这样做的方法是更改​​ Initializators 文件。

文件中的原始相关部分是:

    def G1DListInitializatorReal(genome, **args):
       """ Real initialization function of G1DList

        This initializator accepts the *rangemin* and *rangemax* genome parameters.

       """
       genome.clearList()

       for i in xrange(genome.listSize):
          randomReal = rand_uniform(genome.getParam("rangein", 0),
                            genome.getParam("rangemax", 100))
          genome.append(randomReal)

我的修改(假设前15个有一个范围,后5个有另一个范围)是这样的:

     def G1DListInitializatorReal(genome, **args):

       genome.clearList()

       for i in xrange(0,15):
          print i
          randomReal = rand_uniform(genome.getParam("rangein_1", 0),
                            genome.getParam("rangemax_1", 100))
          genome.append(randomReal)
       for j in xrange(15,20):
          print j
          randomReal2 = rand_uniform(genome.getParam("rangein_2", 0),
                            genome.getParam("rangemax_2", 100))
          genome.append(randomReal2)

我添加了索引 i 和 j 的打印,以确保我知道这个正在被调用。我已将修改后的 Initializators 文件放在与我的代码相同的文件夹中,但是当我 运行 它时,它会从其他地方调用原始文件。我觉得我错过了我需要在 pyevolve 中进行的更多更改,或者我没有正确调用 Initializators,或者...我不知道。

如何在 pyevolve 中成功更改我的染色体参数范围?

提前致谢。

你可以使用等位基因,看这个example

创建等位基因:

alleles = GAllele.GAlleles()

为每个参数创建范围:

alleles.add(GAllele.GAlleleRange(range_min, range_max))

设置参数:

genome.setParams(alleles)

以下代码使用名为 "data" 的列表来定义范围。

from pyevolve import GAllele
from pyevolve import G1DList
from pyevolve import GSimpleGA
from pyevolve import Crossovers
from pyevolve import Initializators
from pyevolve import Mutators
from pyevolve import Scaling

# data values are the [min, max] for each parameter
data=[ [0,99], [1,10], [1,10], [90,110], [1,10],
[1,10], [1,10], [1,10], [1,10], [1,10],
[1,10], [5,10], [1,10], [1,10], [50,100],
[50,100], [50,100], [50,100], [50,100], [7,77] ]

def Grid_Constructor(a=data):
    alleles = GAllele.GAlleles()
    for i in range(0, 20):
        alleles.add(GAllele.GAlleleRange(a[i][0], a[i][1], real=True))
    return alleles

# set the params, initializator and mutator
genome = G1DList.G1DList(20)
genome.setParams(allele=Grid_Constructor())
genome.initializator.set(Initializators.G1DListInitializatorAllele)
genome.mutator.set(Mutators.G1DListMutatorAllele)
ga = GSimpleGA.GSimpleGA(genome)
print(Grid_Constructor())
#ga.evolve(freq_stats=10)

此代码的输出是:

- GAlleles
    Homogeneous:     False
    List size:   20
    Alleles:

Allele for 0 position:
- GAlleleRange
    Real:        True
    Ranges Count:    1
    Range List:
             Range from [0] to [99]

Allele for 1 position:
- GAlleleRange
    Real:        True
    Ranges Count:    1
    Range List:
             Range from [1] to [10]

Allele for 2 position:
- GAlleleRange
    Real:        True
    Ranges Count:    1
    Range List:
             Range from [1] to [10]

.
.
.

Allele for 19 position:
- GAlleleRange
    Real:        True
    Ranges Count:    1
    Range List:
             Range from [7] to [77]




------------------
(program exited with code: 0)
Press return to continue