-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
96 lines (81 loc) · 3.11 KB
/
main.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Lotto prediction with Genetic Algorithm and Mersenne Twister (MT19937)
# Go Namhyeon <[email protected]>
# first created: 2021-04-04
# last updated: 2021-12-31
# download excel data: https://dhlottery.co.kr/gameResult.do?method=byWin
import math
import random
import pandas as pd
import numpy as np
from geneticalgorithm2 import geneticalgorithm2 as ga
cols = [1, 13, 14, 15, 16, 17, 18, 19] # included bonus number
df = pd.read_excel('excel.xlsx', skiprows=2, usecols=cols, names=[0, 1, 2, 3, 4, 5, 6, 7])
rows = df.values[:100]
def make_num(x, n, a, b, c, d, e, min, max):
seed = int(x*a + n*b + c*d + e)
rs = np.random.RandomState(np.random.MT19937())
rs.seed(seed)
return min + math.floor((max - min) * rs.random())
def f(X):
score = 0
for row in rows:
x = row[0]
N = row[1:]
_N = []
_num = 0
k = 9
for n in range(1, 8): # included bonus number
_num = make_num(x, n, X[0], X[1], X[2], _num, X[3], 1, 45)
while _num in _N:
_num = make_num(x, k, X[0], X[1], X[2], _num, X[3], 1, 45)
k += 1
_N.append(_num)
#_N = [make_num(x, n, X[0], X[1], X[2], 1, 45) for n in range(1, 8)] # included bonus number
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
varbound = np.array([[0, 10000]]*4)
model = ga(function=f, dimension=4, variable_type='int', variable_boundaries=varbound, algorithm_parameters={
'max_num_iteration': 255
})
model.run()
solution = model.output_dict
print (solution)
_variables = model.output_dict['last_generation']['variables']
variable = solution['variable']
variables = random.sample([list(item) for item in set(tuple(x) for x in _variables)], 9)
_x = max([row[0] for row in rows]) + 1 # get highest number
print()
print ('Best matched numbers (' + str(_x) + 'th):')
#nums = sorted([make_num(_x, n, variable[0], variable[1], variable[2], 1, 45) for n in range(1, 7)]) # excluded bonus number
nums = []
_num = 0
_k = 8
for n in range(1, 7): # excluded bonus number
_num = make_num(_x, n, variable[0], variable[1], variable[2], _num, variable[3], 1, 45)
while _num in nums:
_num = make_num(_x, _k, variable[0], variable[1], variable[2], _num, variable[3], 1, 45)
_k += 1
nums.append(_num)
print(', '.join(str(x) for x in nums))
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
nums = []
_num = 0
_k = 8
for n in range(1, 7): # excluded bonus number
_num = make_num(_x, n, variable[0], variable[1], variable[2], _num, variable[3], 1, 45)
while _num in nums:
_num = make_num(_x, _k, variable[0], variable[1], variable[2], _num, variable[3], 1, 45)
_k += 1
nums.append(_num)
print(', '.join(str(x) for x in nums))