This repository was archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple-genetic-algorithm.py
More file actions
66 lines (48 loc) · 1.47 KB
/
simple-genetic-algorithm.py
File metadata and controls
66 lines (48 loc) · 1.47 KB
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
## This is not the exact version of Genetic Algorithm
import random
import math
MIN, MAX = 100, 500
MAX_RESULT = 99999999999999
best_values = (None, None, None, None)
best_answer = MAX_RESULT
counter = 0
def goal(x, y, z, t):
try:
return (((x * math.pi) / y) * z + math.cos(t)) / t / 2
except:
return MAX_RESULT
def check(x, y, z, t):
global counter, best_values, best_answer
is_valuable = False
counter += 1
result = goal(x, y, z, t)
if result < best_answer:
best_answer = result
best_values = (x, y, z, t)
print("\nFounded,", best_answer, best_values)
is_valuable = True
if counter % 10000 == 0:
print(".", end="")
return is_valuable
def linear_algorithm():
for x in range(MIN, MAX):
for y in range(MIN, MAX):
for z in range(MIN, MAX):
for t in range(MIN, MAX):
check(x, y, z, t)
def simplified_genetic_algorithm():
G = [[MIN, MIN, MIN, MIN], [MAX, MAX, MAX, MAX]]
while True:
NEW = [None, None, None, None]
# CROSSOVER
for index in range(4):
NEW[index] = G[random.choice([0, 1])][index]
# MUTATION
NEW[random.choice([0, 1, 2, 3])] = random.randint(MIN, MAX)
# REMOVE WORST MEMBER
x, y, z, t = NEW
if check(x, y, z, t):
G[random.choice([0, 1])] = NEW
if __name__ == "__main__":
# linear_algorithm()
simplified_genetic_algorithm()