Learning · Robot club · about 20 min
Learning a controller gain by trial: measure, change, keep the better.
[1 mark]What does this program print?
xs = [2, -4, 1, -1] errors = [abs(x) for x in xs] print(sum(errors) / len(errors))
[1 mark]A trial's score is the mean of abs(x). Gain 6 scores 2.2 and gain 12 scores 1.3. Which is better?
[1 mark]What does this program print?
def trial(gain):
return abs(gain - 10) + 1
gain, direction = 2.0, 1
best_gain, best_error = gain, 1e9
for t in range(5):
error = trial(gain)
if error < best_error:
best_gain, best_error = gain, error
else:
direction = -direction
gain = max(1.0, best_gain + direction * 4.0)
print(best_gain)[1 mark]Why does a gain of 1 never steer this robot until it is 15 cm out?
[1 mark]Suppose a robot leaks 2 cm sideways every second, and the lane is 8 cm wide with the robot starting on the centre line. After how many seconds is it out of the lane?
[1 mark]In the hill climb, a trial scores worse than the best so far. What happens next?
[1 mark]The same measure, change, keep the best loop can tune which of these?
Tick every answer that is true.
Drive up and down the lane, trying gains and keeping the best. After 30 seconds the robot must stay inside the lane. Print best gain: <value> at the end.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
gain = 0.3
# do this 10 times (leg counts from 0)
for leg in range(10):
# do this 50 times (tick counts from 0)
for tick in range(50):
# where am I? (cm from where I started)
x, y = position()
# forward, sideways, rotation: -100 to 100 each, until the next command
drive(40 if leg % 2 == 0 else -40, -x * gain, 0)
# pause 0.1 s (the robot keeps doing what it was told)
wait(0.1)
# all motors off
stop()
print(f'best gain: {gain:.1f}')Plan your program here, then type it in and press Run.