-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossovers.py
43 lines (37 loc) · 1.74 KB
/
crossovers.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
import random
import math
import player as ply
class Crossover:
# In every method, we asume both player_classes are the same
@staticmethod
def one_point(player_one, player_two):
p = random.randint(0,player_one.n_genes)
h1, h2 = player_one.genes(), player_two.genes()
for i in range(p,player_one.n_genes):
h1[i], h2[i] = h2[i], h1[i]
return [ply.Player.new_from_array(player_one.player_class, h1), ply.Player.new_from_array(player_one.player_class, h2)]
@staticmethod
def two_points(player_one, player_two):
p1 = random.randint(0,player_one.n_genes)
p2 = random.randint(0,player_one.n_genes)
if p1>p2: p1,p2=p2,p1
h1, h2 = player_one.genes(), player_two.genes()
for i in range(p1,p2):
h1[i], h2[i] = h2[i], h1[i]
return [ply.Player.new_from_array(player_one.player_class, h1), ply.Player.new_from_array(player_one.player_class, h2)]
@staticmethod
def ring(player_one, player_two):
s = player_one.n_genes
p = random.randint(0, s-1)
l = random.randint(0, math.ceil(s/2))
h1, h2 = player_one.genes(), player_two.genes()
for i in range(p,p+l):
h1[i%s], h2[i%s] = h2[i%s], h1[i%s]
return [ply.Player.new_from_array(player_one.player_class, h1), ply.Player.new_from_array(player_one.player_class, h2)]
@staticmethod
def uniform(player_one, player_two):
h1, h2 = player_one.genes(), player_two.genes()
for i in range(0,player_one.n_genes):
if random.choice([True, False]):
h1[i], h2[i] = h2[i], h1[i]
return [ply.Player.new_from_array(player_one.player_class, h1), ply.Player.new_from_array(player_one.player_class, h2)]