일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- SWEA 15612번
- 백준 1331번
- 백준
- 알고리즘
- javascript
- 모각코
- 백준 17451번
- 백준 18310번
- 다이나믹프로그래밍
- 그리디
- HUFS 모각코 캠프
- 다이나믹 프로그래밍
- java_programming
- 백준 2512번
- 백준 3085번
- SQL
- 백준 16918번
- ubuntu
- react
- 백준 1987
- Python
- 백준 1253번
- MySQL
- 깃헙
- 그래프
- 명품자바
- 자바
- AWS
- 백준 15787번
- 머신러닝과 딥러닝
Archives
- Today
- Total
차곡차곡
[Nonogram-solver] 2022년 5월 15일 일요일 본문
오늘 한 것
- GA crossover, converge 메소드 등 기존 코드 분석
# 랜덤 생성한 solution 집합 P에서 적합도(확률) 높은 것들 뽑아서 새로운 solution 집합 PP 생성
def crossover(P, constraints):
rules, nLines, nColumns, nPoints, nPopulation = constraints
PP = []
# P는 여러 solution들이 담겨져 있는 list
P = sorted(P, key = lambda s : (s.fitness, random.random())) # 적합도 -> 랜덤 순으로 solution 오름차순 정렬
n = (nPopulation*(nPopulation+1))/2 # solution 개수?
prob=[i/n for i in range(1, nPopulation+1)] # 각 solution별 확률 생성
# solution 적합도에 따라 오름차순 정렬했으니까 적합도가 더 큰 게 뒤로 옴
# -> solution별 확률 부여할 때 뒤로 갈 수록 i가 커져서 뒤 solution 더 큰 확률 부여받게 됨
# 무작위로 부모 유전자 두 개 뽑아서
for _ in range(nPopulation):
# 자손 유전자 생성
child1Points = []
child2Points = []
parent1, parent2 = random.choice(P, p=prob, replace=False, size=2)
# 무작위 표본 추출
# P: 모집단, p: 모집단 내 각 원소별로 표본으로 뽑힐 확률, replace=False: 비복원 추출(추출한 것을 제자리에 되돌리지 않고 다음 것을 추출. 즉 중복 추출 X), size=2: 두 개 추출
# 확률이 높은 게 표본으로 뽑힐 것
for i in range(nPoints):
if random.random() <= 0.5: # 랜덤값이 50% 이하면 그대로 넣고, 더 크면 교차해서 넣음
child1Points += [parent1.points[i]]
child2Points += [parent2.points[i]]
else:
child1Points += [parent2.points[i]]
child2Points += [parent1.points[i]]
PP += [Solution(child1Points, constraints), Solution(child2Points, constraints)] # 새로운 solution 집합 생성 (크기 두 배일 거 같은 ??)
return PP
def converge(P, constraints):
rules, nLines, nColumns, nPoints, nPopulation = constraints
# 적합도가 0인 solution이 존재하면 GA 함수에 while문 빠져나옴
for s in P:
if s.fitness == 0:
return True
# 한 solution이 다음 solution과 다른 게 하나라도 있으면 False return
for i in range(len(P)-1):
if P[i].points != P[i+1].points:
return False
return True # 한 solution과 다음 soultion이 다른 것이 하나도 없으면 True return
Python Numpy 강좌 : 제 10강 - 난수
무작위 선택
076923.github.io
[Python numpy] np.random.choice() 메소드로 임의표본 추출하기 (무작위, 확률 샘플링)
이번 포스팅에서는 Python numpy 모듈의 random.choice() 메소드를 사용하여 임의(무작위, 확률) 추출 (random sampling)하는 방법을 소개하겠습니다. numpy.random.choice() 메소드의 기본 Syntax는 아래와 같습..
rfriend.tistory.com
numpy.random.choice() 함수 사용하기
numpy.random.choice(a, size=None, replace=True, p=None) a : 1차원 배열 또는 정수 (정수인 경우, np.arange(a) 와 같은 배열 생성) size : 정수 또는 튜플(튜플인 경우, 행렬로 리턴됨. (m, n, k) -> m *..
sweepover.tistory.com
[ML] GA(Genetic Algorithm)
이번 포스팅에서는 생물체가 환경에 적응하면서 진화해나가는 모습을 모방하여 최적의 해를 구하는 방법론인 GA라고 불리는 유전 알고리즘에 대해서 알아보려고 한다. GA의 이름에 알고리즘이
techblog-history-younghunjo1.tistory.com
'토이 프로젝트 > Nonogram-solver' 카테고리의 다른 글
[Nonogram-solver] 최종 결과물 (0) | 2022.07.30 |
---|---|
[Nonogram-solver] 2022년 5월 26일 목요일 (0) | 2022.05.27 |
[Nonogram-solver] 2022년 5월 18일 수요일 (0) | 2022.05.18 |
[Nonogram-solver] 2022년 5월 10일 화요일 (0) | 2022.05.10 |
Comments