2017-03-16 1 views
0

私はPythonでDEAPパッケージを使用して、Geneticに固有の進化アルゴリズムを使用して最適化プログラムを作成します。Pythonのdeapパッケージのさまざまな範囲の乱数リストを作成するにはどうすればいいですか?

私はPythonでリストタイプを使用して染色体を作成する必要があります。この染色体は異なる範囲の5つの浮遊遺伝子(対立遺伝子)を有するべきである。

私の主な問題は、そのような染色体を作り出すことです。しかし、このためにdeapパッケージのtools.initRepeat関数を使うことができれば良いでしょう。

import random 

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

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

IND_SIZE=10 

toolbox = base.Toolbox() 
toolbox.register("attr_float", random.random) 
toolbox.register("individual", tools.initRepeat, creator.Individual, 
       toolbox.attr_float, n=IND_SIZE) 

私はhereからもらった:全ての遺伝子は、我々は次のコードを使用することができ、同じ範囲内にされた場合については

答えて

0

私は良いお勧めを見つけましたhere

def genFunkyInd(icls, more_params): 
    genome = list() 
    param_1 = random.uniform(...) 
    genome.append(param_1) 
    param_2 = random.randint(...) 
    genome.append(param_2) 
    # Many more parameter initialization 

    return icls(genome) 

The icls (standing for individual class) parameter should receives the type created with the creator, while all other parameters configuring your ranges can be passed like the more_params argument or with defined constants in your script. Here is how it is registered in the toolbox. 

toolbox.register('individual', genFunkyInd, creator.Individual, more_params) 

手動で染色体のクラスを作成します。私はそれが最善の選択かどうかわかりませんが、私の問題を解決するために使うことができます。

関連する問題