-
Notifications
You must be signed in to change notification settings - Fork 0
/
generation.py
56 lines (43 loc) · 1.31 KB
/
generation.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
# generation.py
import random
import config
# Garden properties
length = config.length
height = config.height
rocks = config.rocks
# Function to generate start coordinates
def generate_coordinates():
if random.randint(0, 1) == 0:
# Horizontal start
y = random.randint(0, height - 1)
x = random.randint(0, 1) * (length - 1)
else:
# Vertical start
y = random.randint(0, 1) * (height - 1)
x = random.randint(0, length - 1)
start = (y, x)
if start in [(0, 0), (height - 1, 0), (0, length - 1), (height - 1, length - 1)]:
return generate_coordinates()
if start in rocks:
return generate_coordinates()
return start
# Function to generate genes for one individual
def generate_genes():
positions = []
turns = []
while len(positions) < length + height:
coordinates = generate_coordinates()
if coordinates not in positions:
positions.append(coordinates)
while len(turns) < len(rocks):
if random.choice([True, False]):
turns.append("r")
else:
turns.append("l")
return positions, turns, 0
# Function to initialize population
def generate_population(size):
population=[]
for i in range(size):
population.append(generate_genes())
return population