2016-08-29 4 views
0

나는 genetic algorithm toolbox을 Python으로 사용하고 있습니다. 코드 :유전자 알고리즘 도구 상자에서 0과 1 사이의 임의의 숫자를 생성하는 방법,

toolbox.register("attr_bool", random.randint, 0, 1)은 보통 01의 난수를 생성합니다. 질문은01 사이에서 임의의 숫자 을 찾고 있다는 것입니다. 나는 toolbox.register("attr_bool", random.uniform(0, 1))을 사용하지만, 그것은 나에게 아래의 오류합니다 : 그것은 함수 역할을 할 수 있어야한다는 것을 의미 TypeError: the first argument must be callable

+0

가능한 중복 http://stackoverflow.com/questions/33359740/random :이 문제를 해결하려면, 당신은 단순히 lambda 키워드 익명 래퍼 함수를 ​​만들 수 있습니다 -number-between-0-and-1-in-python) –

+0

첫 번째가 아닌 두 번째 대답을보세요 –

+0

괄호를보세요. 예제의 괄호와 비교하십시오. – user2357112

답변

2

오류는 그것이 첫 번째 인수를 호출 할 수 있어야 함을 말하고있다. random.randint은 기능이지만 random.uniform(0, 1)은 아닙니다. 부동 소수점 숫자입니다.

toolbox.register("attr_bool", lambda: random.uniform(0, 1)) 
[파이썬에서 0과 1 사이의 임의의 숫자] (의
+0

'TypeError : 정수 인수가 필요합니다. float가 있습니다. ' –

+0

우리는 다음과 같이 변경해야합니다 : –

+0

'toolbox.register ("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 100)' 'toolbox.register ("population", tools.initRepeat, list, toolbox. 개별)' –