-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain2.py
145 lines (106 loc) · 3.88 KB
/
train2.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
import numpy as np
from environment import Environment
# creating the bots
# dna = 3,1,2,0
# fitnesss = total distance travelled
class Route():
def __init__(self,dnaLength):
self.dnaLength = dnaLength
self.dna = list()
self.distance = 0
for i in range(self.dnaLength-1):
# dna = 3,1,2
rnd = np.random.randint(1,self.dnaLength)
while rnd in self.dna:
rnd = np.random.randint(1,self.dnaLength)
self.dna.append(rnd)
self.dna.append(0)
# Building cross over method
def mix(self,dna1,dna2):
# dna1 = 1,2,3,4,0
# sna2 = 4,3,2,1,0
# dna = dna1.copy()
# i=1 dna1[i] =2 , dna2[i]=3
# previous = dna[i]=2
self.dna = dna1.copy()
for i in range(self.dnaLength-1):
if np.random.rand()<=0.5:
previous = self.dna[i]
inx = self.dna.index(dna2[i])
self.dna[inx] = previous
self.dna[i] = dna2[i]
# random partial mutation
for i in range(self.dnaLength-1):
if np.random.rand()<=0.1:
previous = self.dna[i]
rnd = np.random.randint(1,self.dnaLength)
inx = self.dna.index(rnd)
self.dna[inx] = previous
self.dna[i] = rnd
elif np.random.rand() <= 0.1:
rnd = np.random.randint(1, self.dnaLength)
prevInx = self.dna.index(rnd)
self.dna.insert(i,rnd)
if i>= prevInx:
self.dna.pop(prevInx)
else:
self.dna.pop(prevInx+1)
# initializing the main code
population_size = 50
mutation_rate = 0.1
n_selected = 5
env = Environment()
dnaLength = len(env.planets)
population = list()
# creating first population
for i in range(population_size):
route = Route(dnaLength)
population.append(route)
# starting main loop
generation = 0
bestDist = np.inf
while True:
generation+=1
# Evaluating the population
for route in population:
env.reset()
for i in range(dnaLength):
action = route.dna[i]
route.distance += env.step(action,'none')
# Sorting the population
sortedPop = sorted(population,key= lambda x:x.distance)
population.clear()
if sortedPop[0].distance < bestDist:
bestDist = sortedPop[0].distance
# Adding best previous bots to population.
for i in range(n_selected):
best = sortedPop[i]
best.distance = 0
population.append(best)
#Filling the rest of population
left = population_size - n_selected
for i in range(left):
new_route =Route(dnaLength)
if np.random.rand()<=mutation_rate:
population.append(new_route)
else:
inx1 = np.random.randint(0,n_selected)
inx2 = np.random.randint(0,n_selected)
while inx1==inx2:
inx2 = np.random.randint(0,n_selected)
dna1 = sortedPop[inx1].dna
dna2 = sortedPop[inx2].dna
new_route.mix(dna1, dna2)
population.append(new_route)
# Display the results
env.reset()
for i in range(dnaLength):
action = sortedPop[0].dna[i]
_ = env.step(action,'normal')
if generation % 100 ==0:
env.reset()
for i in range(dnaLength):
action = sortedPop[0].dna[i]
_ = env.step(action,'beautiful')
print("Generation: "+str(generation) + ' Shortest distance: {:.2f}'.format(bestDist)+ ' light years ')