Skip to content
Snippets Groups Projects
Commit 4d78e8d8 authored by tuhe's avatar tuhe
Browse files

Updated lecture examples

parent 37aa3bb5
No related branches found
No related tags found
No related merge requests found
Showing
with 284 additions and 0 deletions
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc import train
from irlc.ex04.model_harmonic import HarmonicOscilatorEnvironment
from irlc import Agent
import numpy as np
class NullAgent(Agent):
def pi(self, x, k, info=None):
return np.asarray([0])
if __name__ == "__main__":
env = HarmonicOscilatorEnvironment(render_mode='human')
train(env, NullAgent(env), num_episodes=1, max_steps=200)
env.close()
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc.ex04.pid_lunar import get_lunar_lander
import gymnasium
from irlc import train
if __name__ == "__main__":
env = gymnasium.make('LunarLanderContinuous-v3', render_mode='human')
env._max_episode_steps = 1000 # We don't want it to time out.
agent = get_lunar_lander(env)
stats, traj = train(env, agent, return_trajectory=True, num_episodes=10)
env.close()
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc import Agent, train
from irlc.ex04.model_pendulum import GymSinCosPendulumEnvironment
if __name__ == "__main__":
env = GymSinCosPendulumEnvironment(Tmax=20, render_mode='human')
train(env, Agent(env), num_episodes=1)
env.close()
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc.lectures.lec04.lecture_04_pid_p import pidplot
if __name__ == "__main__":
pidplot(Kp=40, Kd=100, Ki=0)
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc.lectures.lec04.lecture_04_pid_p import pidplot
if __name__ == "__main__":
pidplot(Kp=40, Kd=50, Ki=0, slope=2, target=0)
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc.lectures.lec04.lecture_04_pid_p import pidplot
if __name__ == "__main__":
pidplot(Kp=40, Kd=50, Ki=10, slope=2, target=0)
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from irlc.ex04.locomotive import LocomotiveEnvironment
from irlc.ex04.pid_locomotive_agent import PIDLocomotiveAgent
from irlc.ex01.agent import train
def pidplot(Kp=40, Kd=0, Ki=0, slope=0, target=0):
dt = .04
m = 70
Tmax=20
env = LocomotiveEnvironment(m=m, slope=slope, dt=dt, Tmax=Tmax, render_mode='human')
agent = PIDLocomotiveAgent(env, dt=dt, Kp=Kp, Ki=Ki, Kd=Kd, target=0)
train(env, agent, num_episodes=1)
env.close()
if __name__ == "__main__":
pidplot(Kp=40, Kd=0, Ki=0)
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from unitgrade import Report
import irlc
from unitgrade import UTestCase
from irlc.ex03.kuramoto import KuramotoModel, f
import sympy as sym
import numpy as np
class Problem1Kuramoto(UTestCase):
""" Test the Kuromoto Osscilator """
def test_continious_model(self):
cmodel = KuramotoModel()
x, u = sym.symbols("x u")
expr = cmodel.sym_f([x], [u])
# Check the expression has the right type.
self.assertIsInstance(expr, list)
# Evaluate the expression and check the result in a given point.
self.assertEqualC(expr[0].subs([(x, 0.2), (u, 0.93)]))
def test_f(self):
self.assertLinf(f([0.1], [0.4]), tol=1e-6)
def test_RK4(self):
from irlc.ex03.kuramoto import rk4_simulate
cmodel = KuramotoModel()
x0 = np.asarray(cmodel.x0_bound().low) # Get the starting state x=0.
u = 1.3
xs, ts = rk4_simulate(x0, [u], t0=0, tF=20, N=100)
# xs, us, ts = cmodel.simulate(x0, u_fun=u , t0=0, tF=20)
self.assertLinf(ts, tol=1e-6)
# self.assertLinf(us, tol=1e-6)
self.assertLinf(xs, tol=1e-6)
# Test the same with a varying function:
xs, ts = rk4_simulate(x0, [u+1], t0=0, tF=10, N=50)
# xs, us, ts = cmodel.simulate(x0, u_fun=lambda x,t: np.sin(x + u) , t0=0, tF=10)
self.assertLinf(ts, tol=1e-6)
# self.assertLinf(us, tol=1e-6)
self.assertLinf(xs, tol=1e-6)
class Exam5InventoryEvaluation(UTestCase):
def test_a_test_expected_items_next_day(self):
from irlc.ex03.inventory_evaluation import a_expected_items_next_day
self.assertAlmostEqual(a_expected_items_next_day(x=0, u=1), 0.1, places=5)
def test_b_test_expected_items_next_day(self):
from irlc.ex03.inventory_evaluation import b_evaluate_policy
pi = self.get_pi()
self.assertAlmostEqual(b_evaluate_policy(pi, 1), 2.7, places=5)
def get_pi(self):
from irlc.ex02.inventory import InventoryDPModel
model = InventoryDPModel()
pi = [{x: 1 if x == 0 else 0 for x in model.S(k)} for k in range(model.N)]
return pi
class Exam6Toy2d(UTestCase):
def test_rk4_a(self):
from irlc.ex03.toy_2d_control import toy_simulation
w = toy_simulation(u0=0.4, T=5)
self.assertFalse(isinstance(w, np.ndarray), msg="Your toy_simulation function must return a float")
self.assertEqual(type(float(w)), float, msg="Your toy_simulation function must return a float")
self.assertLinf(w, tol=0.01, msg="Your simulation ended up at the wrong angle")
def test_rk4_b(self):
from irlc.ex03.toy_2d_control import toy_simulation
w = toy_simulation(u0=-0.1, T=2)
self.assertFalse( isinstance(w, np.ndarray), msg="Your toy_simulation function must return a float")
self.assertEqual(type(float(w)), float, msg="Your toy_simulation function must return a float")
self.assertLinf(w, tol=0.01, msg="Your simulation ended up at the wrong angle")
class Week03Tests(Report): #240 total.
title = "Tests for week 03"
pack_imports = [irlc]
individual_imports = []
questions = [
(Problem1Kuramoto, 10),
(Exam5InventoryEvaluation, 10),
(Exam6Toy2d, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week03Tests())
# This file may not be shared/redistributed without permission. Please read copyright notice in the git repo. If this file contains other copyright notices disregard this text.
from unitgrade import Report
from unitgrade import UTestCase
import irlc
from irlc.car.car_model import CarEnvironment
from irlc.ex04.pid_car import PIDCarAgent
from irlc import train
from irlc.ex04.pid_locomotive_agent import LocomotiveEnvironment, PIDLocomotiveAgent
from irlc.ex03.kuramoto import KuramotoModel, f
from irlc.ex04.discrete_kuramoto import fk, dfk_dx
import sympy as sym
import numpy as np
class Problem1DiscreteKuromoto(UTestCase):
""" Test the Kuromoto Osscilator """
def test_continious_model(self):
cmodel = KuramotoModel()
x, u = sym.symbols("x u")
expr = cmodel.sym_f([x], [u])
# Check the expression has the right type.
self.assertIsInstance(expr, list)
# Evaluate the expression and check the result in a given point.
self.assertEqualC(expr[0].subs([(x, 0.2), (u, 0.93)]))
def test_f(self):
self.assertLinf(f([0.1], [0.4]), tol=1e-6)
def test_fk(self):
self.assertLinf(fk([0.1], [0.4]), tol=1e-6)
def test_dfk_dx(self):
self.assertLinf(dfk_dx([0.1], [0.4]), tol=1e-6)
class Problem3PID(UTestCase):
""" PID Control """
def test_pid_class(self, Kp=40, Ki=0, Kd=0, target=0, x=0):
dt = 0.08
from irlc.ex04.pid import PID
pid = PID(Kp=Kp, Kd=Kd, Ki=Ki, target=target, dt=0.8)
u = pid.pi(x)
self.assertL2(u, tol=1e-4)
def test_pid_Kp(self):
self.test_pid_class(40, 0, 0, 0, 1)
self.test_pid_class(10, 0, 0, 0, 2)
def test_pid_target(self):
self.test_pid_class(40, 0, 0, 3, 1)
self.test_pid_class(20, 0, 0, 0, 2)
def test_pid_all(self):
self.test_pid_class(4, 3, 8, 1, 1)
self.test_pid_class(40, 10, 3, 0, 2)
class Problem4PIDAgent(UTestCase):
""" PID Control """
def pid_locomotive(self, Kp=40, Ki=0, Kd=0, slope=0, target=0):
dt = 0.08
env = LocomotiveEnvironment(m=10, slope=slope, dt=dt, Tmax=5)
agent = PIDLocomotiveAgent(env, dt=dt, Kp=Kp, Ki=Ki, Kd=Kd, target=target)
stats, traj = train(env, agent, return_trajectory=True, verbose=False)
self.assertL2(traj[0].state, tol=1e-4)
def test_locomotive_flat(self):
self.pid_locomotive()
def test_locomotive_Kd(self):
""" Test the derivative term """
self.pid_locomotive(Kd = 10)
def test_locomotive_Ki(self):
""" Test the integral term """
self.pid_locomotive(Kd = 10, Ki=5, slope=5)
def test_locomotive_all(self):
""" Test all terms """
self.pid_locomotive(Kp=35, Kd = 10, Ki=5, slope=5, target=1)
class Problem7PIDCar(UTestCase):
lt = -1
@classmethod
def setUpClass(cls) -> None:
env = CarEnvironment(noise_scale=0, Tmax=80, max_laps=2)
agent = PIDCarAgent(env, v_target=1.0)
stats, trajectories = train(env, agent, num_episodes=1, return_trajectory=True)
d = trajectories[0].state[:, 4]
lt = len(d) * env.dt / 2
print("Lap time", lt)
cls.lt = lt
def test_below_60(self):
""" Testing if lap time is < 60 """
self.assertTrue(0 < self.__class__.lt < 60)
def test_below_40(self):
""" Testing if lap time is < 60 """
self.assertTrue(0 < self.__class__.lt < 40)
def test_below_30(self):
""" Testing if lap time is < 60 """
self.assertTrue(0 < self.__class__.lt < 30)
def test_below_22(self):
""" Testing if lap time is < 22 """
self.assertTrue(0 < self.__class__.lt < 22)
class Week04Tests(Report):
title = "Tests for week 04"
pack_imports = [irlc]
individual_imports = []
questions = [
(Problem1DiscreteKuromoto, 10),
(Problem3PID, 10),
(Problem4PIDAgent, 10), # ok
(Problem7PIDCar, 10), # ok
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week04Tests())
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment