-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom_controller_euc.py
173 lines (140 loc) · 6.63 KB
/
custom_controller_euc.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flight_controller import FlightController
from drone import Drone
from typing import Tuple
import numpy as np
import matplotlib.pyplot as plt
class EuclideanCustomController(FlightController):
def __init__(self):
"""
Add params for the controller:
"""
self.parameters = [0,0,0,0]
self.C = self.parameters[0] # constant
self.X = self.parameters[1] # drag coeff
self.Y = self.parameters[2]
self.S = self.parameters[3]
self.pop_size = 4**4 # num of solutions
self.number_generations = 1
def run_simulation(self, parameters):
drone = self.init_drone()
target = drone.get_next_target()
rewards = 0
r_min = np.sqrt((drone.x-target[0])**2+(drone.y-target[1])**2)
count = 0
for t in range(self.get_max_simulation_steps()):
drone.set_thrust(np.clip(self.get_thrusts(drone, parameters), 0, 1))
target_hit = drone.step_simulation(self.get_time_interval())
r = np.sqrt((drone.x-target[0])**2+(drone.y-target[1])**2)
if target_hit:
rewards+=300
target = drone.get_next_target()
r_min = np.sqrt((drone.x-target[0])**2+(drone.y-target[1])**2)
count +=1
elif r<(r_min-0.1):
rewards+=1/r # this line is the old version
r_min = r
else:
rewards -=1/r
print(count, " targets hit!")
return rewards, count
def crossover(self, survivors):
# crossover random pairs of survivors
crossover_solutions = np.empty((0,4))
solution_inds = range(len(survivors[:,0]))
for i in solution_inds:
random_pair = np.random.choice(solution_inds, 2, replace = False)
sol_1 = survivors[random_pair[0],:]
sol_2 = survivors[random_pair[1],:]
parent_sols = np.vstack((sol_1, sol_2))
x_over_sols = np.empty((2,4))
for g in range(4):
x_over_sols[:,g] = np.random.choice(parent_sols[:,g], 2)
crossover_solutions = np.vstack((crossover_solutions, x_over_sols))
return crossover_solutions
def mutate(self, survivors, generation):
mutated_solutions = np.empty((0,4))
mutation_rates = [0.3,5,5,5]
for i in range(len(survivors[:,0])):
mutations = np.empty((5,4))
for g in range(4):
sd = mutation_rates[g]*(1.5-generation/self.number_generations)
mutations[:,g] = abs(np.random.normal(survivors[i,g],sd, 5)) # FIX VARIANCE
mutated_solutions[:,0] = np.clip(mutated_solutions[:,0], 0, 1)
mutated_solutions = np.vstack((mutated_solutions, mutations))
return mutated_solutions
def breed_survivors(self,survivors, generation):
crossover_solutions = self.crossover(survivors)
mutated_solutions = self.mutate(survivors, generation)
new_solutions = np.vstack((crossover_solutions, mutated_solutions))
solutions = np.vstack((survivors, new_solutions))
return solutions
def select_survivors(self, solution_rewards, solutions):
probabilities = np.array(solution_rewards - min(solution_rewards)).astype(float)
solution_inds = range(len(solutions[:,0]))
if sum(probabilities) == 0:
probabilities[:] = 1/self.pop_size
else:
probabilities = probabilities**2/sum(probabilities**2)
# select 10% of population to create next generation
survivor_inds = np.random.choice(solution_inds, int(self.pop_size/8),
p = probabilities)
survivors = np.clip(solutions[survivor_inds], 0, None)
return survivors
def train(self, number_of_episodes):
#start by setting random parameters, for 100 solutions
self.number_generations = number_of_episodes
C_vals = np.linspace(0, 1, int(self.pop_size**0.25)) # hovering constant
X_vals = Y_vals = np.linspace(0, 100, int(self.pop_size**0.25)) # direction constants
S_vals = np.linspace(0,10, int(self.pop_size**0.25)) # rotation stabalisation constants
CC, XX, YY, SS = np.meshgrid(C_vals, X_vals, Y_vals, S_vals)
solutions = np.vstack((CC.flatten(), XX.flatten(),
YY.flatten(), SS.flatten()))
solutions = solutions.transpose()
survivors = []
# double up starting values to get kx, bx, k_theta, b_theta
average_rewards = []
gen_closest_approaches = []
for generation in range(self.number_generations):
print(generation)
simulation_analysis = np.apply_along_axis(self.run_simulation, axis = 1, arr = solutions)
#sim analysis is a list of tuples (rewards, r_min)
solution_rewards, targets_hit = simulation_analysis[:,0], simulation_analysis[:,1]
survivors = self.select_survivors(solution_rewards, solutions)
solutions = self.breed_survivors(survivors, generation)
average_rewards.append(np.mean(solution_rewards))
gen_closest_approaches.append(np.mean(targets_hit))
return solutions, average_rewards, gen_closest_approaches, survivors
def get_thrusts(self, drone: Drone, parameters) -> Tuple[float, float]:
C,X,Y,S = parameters
target = drone.get_next_target()
dx = (target[0] - drone.x)
dy = target[1] - drone.y
T1 = C + X*dx + Y*dy - S*drone.pitch_velocity
T2 = C - X*dx + Y*dy + S*drone.pitch_velocity
return np.clip([T1,T2], 0, 1)
def load(self):
pass
def save(self):
pass
def run_for(episodes):
c = EuclideanCustomController()
sols, gen_rs, rs, survivors = c.train(episodes)
return gen_rs, sols, rs, survivors
if __name__ == "__main__":
episodes = 100
gens, sols, rs, survivors = run_for(episodes)
sol = np.mean(survivors, axis = 0)
fig = plt.figure()
gs = fig.add_gridspec(2,2,)
rewards = fig.add_subplot(gs[0,:])
rewards.plot(gens, color = "purple")
rsp = rewards.twinx()
rsp.plot(rs, color = "cyan")
p01 = fig.add_subplot(gs[1,0])
p01.scatter(sols[:,0], sols[:,1], color = "g")
p01.scatter(survivors[:,0], survivors[:,1], color = "orange")
p01.scatter(sol[0], sol[1])
p23 = fig.add_subplot(gs[1,1])
p23.scatter(sols[:,2], sols[:,3], color = "r")
p23.scatter(survivors[:,2], survivors[:,3], color = "orange")
p23.scatter(sol[2], sol[3])