2017-11-10 7 views
0

조지아에 맞는 비례 선택 접근법이 있어야하지만,이 경우 확률을 생성하는 동안 내 인구는 구조 (순서)를 느슨하게 할 수는 없지만 개인은 잘못된 무게, 프로그램은 다음과 같습니다룰렛 휠 비 순차적 피트니스 값 선택

population=[[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [6], [0]], 
[[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [4], [1]], 
[[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [6], [2]], 
[[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [4], [3]]] 

popultion_d={'0,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1': 6, 
'0,0,1,1,1,0,0,1,1,0,1,1,0,0,0,1': 4, 
'0,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0': 6, 
'1,0,0,1,1,1,0,0,1,1,0,1,1,0,0,0': 4} 

def ProbabilityList(population_d): 
    fitness = population_d.values() 
    total_fit = (sum(fitness)) 
    relative_fitness = [f/total_fit for f in fitness] 
    probabilities = [sum(relative_fitness[:i+1]) for i in range(len(relative_fitness))] 
    return (probabilities) 

def FitnessProportionateSelection(population, probabilities, number): 
    chosen = [] 
    for n in range(number): 
     r = random.random() 
     for (i, individual) in enumerate(population): 
      if r <= probabilities[i]: 
       chosen.append(list(individual)) 
       break 
    return chosen 

number=2 

인구 요소는 다음과 같습니다 [개인], [피트니스], [카운터]

확률 함수 출력은 : 나는 알 무엇 [0.42857142857142855, 0.5714285714285714, 0.8571428571428571, 1.0]

여기는 p 뒤쪽 무게는 반드시 다음 순서로 합쳐지며, 반드시 초승달 모양이 아니어야합니다. 따라서 가장 낮은 체력으로 더 높은 체중이 주어진다고 생각하십시오.

나중에 목록을 색인으로 지정해야하기 때문에 주문을 원하지 않으므로 잘못된 매치가 발생할 것으로 생각됩니다.

누구나이 경우 가중치있는 선택을 수행 할 수있는 가능한 해결책, 패키지 또는 다른 접근법을 알고 있습니까?

p.s : 사전이 중복 될 수 있음을 알고 있지만 목록 자체를 사용하는 데 몇 가지 다른 문제가 있습니다.

편집 : 당신이 아래에 볼 수있는 나는 (상대 체력을 사용하여) random.choices()를 사용하려고 : TypeError: choices() takes from 2 to 3 positional arguments but 4 were given

감사합니다 :

def FitnessChoices(population, probabilities, number): 
    return random.choices(population, probabilities, number) 

그러나 나는이 오류!

답변

1

random.choices을 사용하는 것이 좋습니다. 함수 호출을 이해하기 만하면됩니다. 확률이 한계 또는 누적인지 여부를 지정해야합니다. 그래서 당신은 사용할 수 있습니다 중 하나

import random 

def ProbabilityList(population_d): 
    fitness = population_d.values() 
    total_fit = sum(fitness) 
    relative_fitness = [f/total_fit for f in fitness] 
    return relative_fitness 

def FitnessChoices(population, relative_fitness, number): 
    return random.choices(population, weights = relative_fitness, k = number) 

또는

import random 

def ProbabilityList(population_d): 
    fitness = population_d.values() 
    total_fit = sum(fitness) 
    relative_fitness = [f/total_fit for f in fitness] 
    cum_probs = [sum(relative_fitness[:i+1]) for i in range(len(relative_fitness))] 
    return cum_probs 

def FitnessChoices(population, cum_probs, number): 
    return random.choices(population, cum_weights = cum_probs, k = number) 

내가 파이썬에서 키워드와 위치 인수의 차이점을 살펴 가지고 당신을 권 해드립니다.

+0

하나만 물어 볼 수 있습니까? 내가 cumultaed 가중치를 사용하기로 결정한다면,이 경우에 cum_probs에 대한 내 공식은 틀릴 것입니다, 맞습니까? – vferraz

+0

올바른 것처럼 보이지만 간단히'numpy.cumsum'을 사용할 수도 있습니다. – tsabsch

+0

만약 내가 단지 누적 합계를 사용한다면, 이전 값을 다음에 더할 것이고, 더 낮은 체력이 더 높은 가중치를 갖게 될 것입니다. 아니면 어떻게 함수가 작동하는지에 대해 착각합니까? – vferraz