2021-04-03 23:51:52 +00:00
|
|
|
# Lotto prediction with Genetic Algorithm and Mersenne Twister (MT19937)
|
2021-04-03 23:50:49 +00:00
|
|
|
# Go Namhyeon <gnh1201@gmail.com>
|
|
|
|
# 2021-04-04
|
|
|
|
# download excel data: https://dhlottery.co.kr/gameResult.do?method=byWin
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
from geneticalgorithm2 import geneticalgorithm2 as ga
|
|
|
|
|
2021-04-04 02:20:54 +00:00
|
|
|
cols = [1, 13, 14, 15, 16, 17, 18, 19] # included bonus number
|
2021-04-04 02:18:54 +00:00
|
|
|
df = pd.read_excel('excel.xlsx', skiprows=2, usecols=cols, names=[0, 1, 2, 3, 4, 5, 6, 7])
|
2021-04-04 02:03:20 +00:00
|
|
|
rows = df.values[:10]
|
2021-04-03 23:50:49 +00:00
|
|
|
|
|
|
|
def make_num(x, n, a, b, c, min, max):
|
|
|
|
seed = int(x*a + n*b + c)
|
|
|
|
rs = np.random.RandomState(np.random.MT19937())
|
|
|
|
rs.seed(seed)
|
|
|
|
return min + round((max - min) * rs.random())
|
|
|
|
|
|
|
|
def f(X):
|
|
|
|
score = 0
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
x = row[0]
|
|
|
|
N = row[1:]
|
2021-04-04 02:20:54 +00:00
|
|
|
_N = [make_num(x, n, X[0], X[1], X[2], 1, 45) for n in range(1, 8)] # included bonus number
|
2021-04-03 23:50:49 +00:00
|
|
|
result = len(list(set(N) & set(_N)))
|
|
|
|
if result > 5:
|
|
|
|
score += 100
|
|
|
|
elif result > 4:
|
|
|
|
score += 75
|
|
|
|
elif result > 3:
|
|
|
|
score += 50
|
|
|
|
elif result > 2:
|
|
|
|
score += 25
|
|
|
|
|
|
|
|
return -score
|
|
|
|
|
2021-04-04 02:11:33 +00:00
|
|
|
varbound = np.array([[0, 10000]]*3)
|
2021-04-03 23:50:49 +00:00
|
|
|
|
|
|
|
model = ga(function=f, dimension=3, variable_type='int', variable_boundaries=varbound)
|
|
|
|
model.run()
|
2021-04-04 02:03:20 +00:00
|
|
|
|
|
|
|
solution = model.output_dict
|
2021-04-04 02:38:21 +00:00
|
|
|
_variables = model.output_dict['last_generation']['variables']
|
|
|
|
variable = solution['variable']
|
|
|
|
variables = [list(item) for item in set(tuple(x) for x in _variables)]
|
2021-04-04 02:03:20 +00:00
|
|
|
|
2021-04-04 02:15:23 +00:00
|
|
|
_x = max([row[0] for row in rows]) + 1 # get highest number
|
2021-04-04 02:03:20 +00:00
|
|
|
|
2021-04-04 02:38:21 +00:00
|
|
|
print()
|
|
|
|
print ('Best matched numbers (' + str(_x) + 'th):')
|
2021-04-04 02:20:54 +00:00
|
|
|
nums = sorted([make_num(_x, n, variable[0], variable[1], variable[2], 1, 45) for n in range(1, 7)]) # excluded bonus number
|
2021-04-04 02:11:33 +00:00
|
|
|
print(', '.join(str(x) for x in nums))
|
2021-04-04 02:38:21 +00:00
|
|
|
|
|
|
|
print()
|
|
|
|
print ('Recommended numbers (' + str(_x) + 'th):')
|
|
|
|
for variable in variables:
|
|
|
|
nums = sorted([make_num(_x, n, variable[0], variable[1], variable[2], 1, 45) for n in range(1, 7)]) # excluded bonus number
|
|
|
|
print(', '.join(str(x) for x in nums))
|