-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontrollers.py
293 lines (241 loc) · 11.4 KB
/
controllers.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
Monza Game - Controllers
Description: Contains the controllers to control the Monza game automatically.
Author(s): Daniel Sotelo, Jiajun Xu, Vladyslav Korenyak
Date: 03/12/2023
Version: 3.0
"""
# Import libraries
import numpy as np
import skfuzzy as fuzz
from skfuzzy import control as ctrl
from settings import *
import torch
import numpy as np
from model import Linear_QNet
'''
Sliding Mode Control (SMC) defines a "sliding surface" (error function)
The control aims to drive the system state to this surface and then slide
along it until reaching the desired state. Parameters can be furtherly
tuned to improve performance:
- Beginner: lambda = 100, k = 0.002
- Intermediate: lambda = 100, k = 0.004
- Expert: lambda = 110, k = 0.0095
- Legendary: lambda = 140, k = 0.0095
'''
class SlidingModeController:
def __init__(self, diff):
self.prev_error = 0
self.diff = diff
self.lambda_ = [100, 100, 110, 140]
self.k = [0.002, 0.004, 0.0095, 0.0095] # Control gain
def get_target_position(self, stage):
if stage >= len(diff_eqs[self.diff]):
target_position = "Exit"
else:
eq_data = diff_eqs[self.diff][stage]
if stage % 2 == 0:
target_position = eq_data["x_left"]
else:
target_position = eq_data["x_right"]
return target_position
def control(self, coin_position, target_position):
# Define the error as the difference between the target position and coin's position
e = target_position - coin_position
# Calculate the derivative of the error (rate of change)
e_prime = e - self.prev_error
# Define the sliding surface
s = e + self.lambda_[self.diff] * e_prime
# Implement the control law
control_action = -self.k[self.diff] * np.sign(s)
self.prev_error = e # Store the error for the next iteration
return control_action
'''
Fuzzy Logic Control (FLC) uses degrees of truth, rather than strict binary
(True or False), to analyze, make decisions, and control situations where
input data might be imprecise or ambiguos. The following controller is
adapted to beat Monza for all the different levels.
'''
class FuzzyLogicController:
def __init__(self, diff):
self.diff = diff
self.prev_dist = 0
self.angle = 0
self.coin = 0
def get_target_position(self, stage):
if stage >= len(diff_eqs[self.diff]):
target_position = "Exit"
else:
eq_data = diff_eqs[self.diff][stage]
if stage % 2 == 0:
target_position = eq_data["x_left"]
else:
target_position = eq_data["x_right"]
return target_position
def control(self, coin_position, coin_n, target_position):
# Obtain the current floor of the coin
eq_data = diff_eqs[self.diff][coin_n]
r_max = (eq_data["x_right"]-eq_data["x_left"]) # maximum range of x per floor
# Input and output variables
dist = target_position - coin_position
vel = dist - self.prev_dist
# Define input and output variables
distance = ctrl.Antecedent(np.arange(-r_max, r_max, 5), 'distance') # distance = target_position - coin_position
velocity = ctrl.Antecedent(np.arange(-100, 100, 0.5), 'velocity')
angle = ctrl.Consequent(np.arange(-0.04, 0.04, 0.001), 'angle')
# Define the fuzzy sets for the input and output variables
distance['left'] = fuzz.trimf(distance.universe, [-r_max, -r_max/4, 0])
distance['center'] = fuzz.trimf(distance.universe, [-30.8, 0, 30.8])
distance['right'] = fuzz.trimf(distance.universe, [0, r_max/4, r_max])
velocity['negative+'] = fuzz.trimf(velocity.universe, [-100, -100, -1.15])
velocity['negative'] = fuzz.trimf(velocity.universe, [-1.5, -1, 0])
velocity['stop'] = fuzz.trimf(velocity.universe, [-1, 0, 1])
velocity['positive'] = fuzz.trimf(velocity.universe, [0, 1, 1.5])
velocity['positive+'] = fuzz.trimf(velocity.universe, [1.15, 100, 100])
angle['left'] = fuzz.trimf(angle.universe, [-0.02, -0.005, 0])
angle['mantain'] = fuzz.trimf(angle.universe, [-0.005, 0, 0.005])
angle['right'] = fuzz.trimf(angle.universe, [0, 0.005, 0.02])
# Define fuzzy rules
rule1 = ctrl.Rule(distance['right'] & velocity['negative'], angle['right']) # going in the correct direction (clockwise)
rule2 = ctrl.Rule(distance['right'] & velocity['negative+'], angle['left']) # going in the correct direction and accelerated (counterclockwise)
rule3 = ctrl.Rule(distance['right'] & velocity['positive'], angle['right']) # going in the opposite direction (clockwise)
rule4 = ctrl.Rule(distance['right'] & velocity['stop'], angle['right']) # heading in the right direction (clockwise)
rule5 = ctrl.Rule(distance['center'] & velocity['negative'], angle['left']) # turning to not let the ball fall out and make the inertia fall slowly to the right to the next floor
rule6 = ctrl.Rule(distance['left'] & velocity['positive'], angle['left']) # going in the correct direction (counterclockwise)
rule7 = ctrl.Rule(distance['left'] & velocity['positive+'], angle['right']) # going in the correct direction and accelerated (clockwise)
rule8 = ctrl.Rule(distance['left'] & velocity['negative'], angle['left']) # going in the opposite direction (counterclockwise)
rule9 = ctrl.Rule(distance['left'] & velocity['stop'], angle['left']) # heading in the right direction (counterclockwise)
rule10 = ctrl.Rule(distance['center'] & velocity['positive'], angle['right']) # turning to not let the ball fall out and make the inertia fall slowly to the left to the next floor
# Creating and simulating a fuzzy control system
control_system = ctrl.ControlSystem([rule1, rule2, rule3, rule4, rule5, rule6, rule7, rule8, rule9, rule10])
simulator = ctrl.ControlSystemSimulation(control_system)
# Inputs for Fuzzify + Defuzzify according to centeroid
simulator.input['distance'] = dist
simulator.input['velocity'] = vel
# Calculate the output
try:
simulator.compute()
angle_output = simulator.output['angle']
self.angle = angle_output
except (AssertionError, ValueError):
angle_output = 0
self.prev_dist = dist
return -angle_output
'''
This Reinforcement Learning Controller, equipped with an Artificial Neural Network,
is trained to maintain a consistent speed while navigating a parabolic path.
It dynamically adjusts its control inputs based on real-time environmental and
system states to achieve optimal performance."
'''
#'''
class ReinforcementLearningController:
def __init__(self, diff):
self.model = Linear_QNet(3, 20, 3)
self.diff = diff
def get_target_position(self, stage):
if stage >= len(diff_eqs[self.diff]):
target_position = "Exit"
else:
if stage % 2 == 0:
target_position = -1
else:
target_position = 1
return target_position
def load_model(self, model_file):
# Load the saved state dictionary
state_dict = torch.load(model_file, map_location=torch.device('cpu')) # 'cpu' or 'cuda' if you are using GPU
# Load the state dictionary into the model
self.model.load_state_dict(state_dict)
def get_state(self, game, target_position):
# Normalized and standardized state
if target_position == -1:
if game.coin.n == 6:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 800, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
] # If you read this you owe me a beer :)
else:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 1200, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
]
else:
current_state = [
-game.circle.angle, # Already in [-1, 1]
-game.coin.dx / 1500, # Scaled to [-1, 1]
-target_position # Assuming this is already normalized
]
return np.array(current_state, dtype=np.float32)
def get_action(self, state):
final_move = [0,0,0]
state0 = torch.tensor(state, dtype=torch.float)
prediction = self.model(state0)
move = torch.argmax(prediction).item()
final_move[move] = 1
return final_move
#'''
'''
# If you want to have fun seing some bugs use this controller (pretty difficult to do it manually though!)
# Are you able to reach the end of the game taking advantage of the bugs?
class ReinforcementLearningController:
def __init__(self, diff):
self.model = Linear_QNet(3, 20, 3)
self.diff = diff
def get_target_position(self, stage):
if stage >= len(diff_eqs[self.diff]):
target_position = "Exit"
else:
if stage % 2 == 0:
target_position = -1
else:
target_position = 1
return target_position
def load_model(self, model_file):
# Load the saved state dictionary
state_dict = torch.load(model_file, map_location=torch.device('cpu')) # 'cpu' or 'cuda' if you are using GPU
# Load the state dictionary into the model
self.model.load_state_dict(state_dict)
def get_state(self, game, target_position):
# Normalized and standardized state
if target_position == -1:
if game.coin.n == 7:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 900, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
]
elif game.coin.n == 4:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 900, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
]
elif game.coin.n == 6:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 1500, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
]
else:
current_state = [
game.circle.angle, # Already in [-1, 1]
game.coin.dx / 1500, # Scaled to [-1, 1]
target_position # Assuming this is already normalized
]
else:
current_state = [
-game.circle.angle, # Already in [-1, 1]
-game.coin.dx / 1500, # Scaled to [-1, 1]
-target_position # Assuming this is already normalized
]
return np.array(current_state, dtype=np.float32)
def get_action(self, state):
final_move = [0,0,0]
state0 = torch.tensor(state, dtype=torch.float)
prediction = self.model(state0)
move = torch.argmax(prediction).item()
final_move[move] = 1
return final_move
'''