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

Example homeworks

parent 0895fb73
Branches
No related tags found
No related merge requests found
Showing
with 627 additions and 0 deletions
""" Source code for 02466, Introduction to reinforcement learning and control, offered at DTU """
__version__ = "0.0.1"
# Do not import Matplotlib (or imports which import matplotlib) in case you have to run in headless mode.
import shutil
import inspect
import lzma, pickle
import numpy as np
import os
# Global imports from across the API. Allows imports like
# > from irlc import Agent, train
# from irlc.utils.player_wrapper_pyglet import PlayWrapper as PlayWrapper # deprecated.
# from irlc.utils.video_monitor import VideoMonitor as VideoMonitor # deprecated.
def get_irlc_base():
dir_path = os.path.dirname(os.path.realpath(__file__))
return dir_path
def get_students_base():
return os.path.join(get_irlc_base(), "../../../02465students/")
def pd2latex_(pd, index=False, escape=False, column_spec=None, **kwargs): # You can add column specs.
for c in pd.columns:
if pd[c].values.dtype == 'float64' and all(pd[c].values - np.round(pd[c].values)==0):
pd[c] = pd[c].astype(int)
ss = pd.to_latex(index=index, escape=escape, **kwargs)
return fix_bookstabs_latex_(ss,column_spec=column_spec)
def fix_bookstabs_latex_(ss, linewidth=True, first_column_left=True, column_spec=None):
to_tabular_x = linewidth
if to_tabular_x:
ss = ss.replace("tabular", "tabularx")
lines = ss.split("\n")
hd = lines[0].split("{")
if column_spec is None:
adj = (('l' if to_tabular_x else 'l') if first_column_left else 'C') + ("".join(["C"] * (len(hd[-1][:-1]) - 1)))
else:
adj = column_spec
# adj = ( ('l' if to_tabular_x else 'l') if first_column_left else 'C') + ("".join(["C"] * (len(hd[-1][:-1])-1)))
if linewidth:
lines[0] = "\\begin{tabularx}{\\linewidth}{" + adj + "}"
else:
lines[0] = "\\begin{tabular}{" + adj.lower() + "}"
ss = '\n'.join(lines)
return ss
def _savepdf_env(file, env):
from PIL import Image
import matplotlib.pyplot as plt
env.render_mode, rmt = 'rgb_array', env.render_mode
frame = env.render()
env.render_mode = rmt
im = Image.fromarray(frame)
snapshot_base = file
if snapshot_base.endswith(".png"):
sf = snapshot_base[:-4]
fext = 'png'
else:
fext = 'pdf'
if snapshot_base.endswith(".pdf"):
sf = snapshot_base[:-4]
else:
sf = snapshot_base
sf = f"{sf}.{fext}"
dn = os.path.dirname(sf)
if len(dn) > 0 and not os.path.isdir(dn):
os.makedirs(dn)
print("Saving snapshot of environment to", os.path.abspath(sf))
if fext == 'png':
im.save(sf)
from irlc import _move_to_output_directory
_move_to_output_directory(sf)
else:
plt.figure(figsize=(16, 16))
plt.imshow(im)
plt.axis('off')
plt.tight_layout()
from irlc import savepdf
savepdf(sf, verbose=True)
plt.show()
def savepdf(pdf, verbose=False, watermark=False, env=None):
"""
Convenience function for saving PDFs. Just call it after you have created your plot as ``savepdf('my_file.pdf')``
to save a PDF of the plot.
You can also pass an environment, in which case the environment will be stored to a pdf file.
:param pdf: The file to save to, for instance ``"my_pdf.pdf"``
:param verbose: Print output destination (optional)
:param watermark: Include a watermark (optional)
:return: Full path of the created PDF.
"""
if env is not None:
_savepdf_env(pdf, env)
return
import matplotlib.pyplot as plt
pdf = os.path.normpath(pdf.strip())
pdf = pdf+".pdf" if not pdf.endswith(".pdf") else pdf
if os.sep in pdf:
pdf = os.path.abspath(pdf)
else:
pdf = os.path.join(os.getcwd(), "pdf", pdf)
if not os.path.isdir(os.path.dirname(pdf)):
os.makedirs(os.path.dirname(pdf))
# filename = None
stack = inspect.stack()
modules = [inspect.getmodule(s[0]) for s in inspect.stack()]
files = [m.__file__ for m in modules if m is not None]
if any( [f.endswith("RUN_OUTPUT_CAPTURE.py") for f in files] ):
return
wd = os.getcwd()
irlc_base = os.path.dirname(__file__)
if False:
pass
else:
plt.savefig(fname=pdf)
outf = os.path.normpath(os.path.abspath(pdf))
print("> [savepdf]", pdf + (f" [full path: {outf}]" if verbose else ""))
return outf
def _move_to_output_directory(file):
"""
Hidden function: Move file given file to static output dir.
"""
if not is_this_my_computer():
return
CDIR = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
shared_output_dir = CDIR + "/../../shared/output"
shutil.copy(file, shared_output_dir + "/"+ os.path.basename(file) )
def bmatrix(a):
if False:
return a.__str__()
else:
np.set_printoptions(suppress=True)
"""Returns a LaTeX bmatrix
:a: numpy array
:returns: LaTeX bmatrix as a string
"""
if len(a.shape) > 2:
raise ValueError('bmatrix can at most display two dimensions')
lines = str(a).replace('[', '').replace(']', '').splitlines()
rv = [r'\begin{bmatrix}']
rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines]
rv += [r'\end{bmatrix}']
return '\n'.join(rv)
def is_this_my_computer():
CDIR = os.path.dirname(os.path.realpath(__file__)).replace('\\', '/')
return os.path.exists(CDIR + "/../../Exercises")
def cache_write(object, file_name, only_on_professors_computer=False, verbose=True, protocol=-1): # -1 is default protocol. Fix crash issue with large files.
if only_on_professors_computer and not is_this_my_computer():
""" Probably for your own good :-). """
return
dn = os.path.dirname(file_name)
if not os.path.exists(dn):
os.mkdir(dn)
if verbose: print("Writing cache...", file_name)
with lzma.open(file_name, 'wb') as f:
pickle.dump(object, f)
# compress_pickle.dump(object, f, compression="lzma", protocol=protocol)
if verbose:
print("Done!")
def cache_exists(file_name):
return os.path.exists(file_name)
def cache_read(file_name):
if os.path.exists(file_name):
with lzma.open(file_name, 'rb') as f:
return pickle.load(f)
else:
return None
\ No newline at end of file
%% Cell type:markdown id:1cd2586b tags:
# A problem about adding numbers
## Subproblem 1: This problem will surely test your awesome number-adding skills.
In this problem, you should compute $x_1 = a + b$ where $a = 45$ and $b = 48$.
%% Cell type:code id:9d13c5b8 tags:
``` python
x1 = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
%% Cell type:markdown id:d725d517 tags:
## Subproblem 2: Multiplication
In this problem, you should compute $x_2 = a b$
%% Cell type:code id:32966dc5 tags:
``` python
x2 = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
%% Cell type:markdown id:72838c51 tags:
# A problem with more troublesome derivative
## Subproblem 1: Testing some other stuff
Consider the function $f(x) = \frac{7 x^{a}}{a}$. Suppose that $7= f'(1)$, what is $a$?
%% Cell type:code id:4cf45579 tags:
``` python
a = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
from unitgrade import hide, UTestCase
from unitgrade import UTestCase, Report
from exam_generator.exam import Question, jinjafy
from types import SimpleNamespace
import gym
import numpy as np
import sympy as sym
import os
import inspect
from unitgrade import NotebookTestCase
class Question1(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_addition(self):
self.assertIsNotNone(self.nb.x1)
def test_multiplication(self):
self.assertIsNotNone(self.nb.x1)
class Question2(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_derivative(self):
self.assertIsNotNone(self.nb.a)
class Exam2024spring_0(Report):
title = "Report id exam2024spring"
abbreviate_questions = True
questions = [
(Question1, 10), (Question2, 10), ]
import irlc
pack_imports = [irlc]
if __name__ == "__main__":
from unitgrade import evaluate_report_student
evaluate_report_student(Exam2024spring_0())
This diff is collapsed.
File added
File added
File added
\ No newline at end of file
%% Cell type:markdown id:bb0323ea tags:
# A problem about subtracting numbers.
This problem set will test your skills in subtracting numbers
## Subproblem 1: This problem will surely test your awesome number-adding skills.
In this problem, you should compute $x_1 = a + b - c$ where $a = 6$ and $b = 9$ and $c = 10$
%% Cell type:code id:530b5ca1 tags:
``` python
x1 = None # Write your result here.
# TODO: 1 lines missing.
print(x1)
```
%% Cell type:markdown id:3fa4acbb tags:
## Subproblem 2: Multiplication and a parenthesis.
In this problem, you should compute $x_2 = a (b-c)$
%% Cell type:code id:5b262dc7 tags:
``` python
x2 = None # Write your result here.
# TODO: 1 lines missing.
print(x2)
```
%% Cell type:markdown id:89e7ff17 tags:
# A problem with more troublesome derivative
## Subproblem 1: Testing some other stuff
Consider the function $f(x) = \frac{10 x^{a}}{a}$. Suppose that $1280= f'(2)$, what is $a$?
%% Cell type:code id:6c3ed12d tags:
``` python
a = None # Write your result here.
a = 7
```
from unitgrade import hide, UTestCase
from unitgrade import UTestCase, Report
from exam_generator.exam import Question, jinjafy
from types import SimpleNamespace
import gym
import numpy as np
import sympy as sym
import os
import inspect
from unitgrade import NotebookTestCase
class Question1(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_addition(self):
self.assertIsNotNone(self.nb.x1)
def test_multiplication(self):
self.assertIsNotNone(self.nb.x1)
class Question2(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_derivative(self):
self.assertIsNotNone(self.nb.a)
class Exam2024spring_1(Report):
title = "Report id exam2024spring"
abbreviate_questions = True
questions = [
(Question1, 10), (Question2, 10), ]
import irlc
pack_imports = [irlc]
if __name__ == "__main__":
from unitgrade import evaluate_report_student
evaluate_report_student(Exam2024spring_1())
This diff is collapsed.
File added
File added
File added
File added
\ No newline at end of file
%% Cell type:markdown id:ab27cf79 tags:
# A problem about adding numbers
## Subproblem 1: This problem will surely test your awesome number-adding skills.
In this problem, you should compute $x_1 = a + b$ where $a = 41$ and $b = 16$.
%% Cell type:code id:979c206b tags:
``` python
x1 = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
%% Cell type:markdown id:f33e65f7 tags:
## Subproblem 2: Multiplication
In this problem, you should compute $x_2 = a b$
%% Cell type:code id:c1611200 tags:
``` python
x2 = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
%% Cell type:markdown id:d56a39a0 tags:
# A problem with more troublesome derivative
## Subproblem 1: Testing some other stuff
Consider the function $f(x) = \frac{10 x^{a}}{a}$. Suppose that $160= f'(4)$, what is $a$?
%% Cell type:code id:dcc5f3bf tags:
``` python
a = None # Write your result here.
# TODO: 1 lines missing.
raise NotImplementedError("Insert your solution and remove this error.")
```
from unitgrade import hide, UTestCase
from unitgrade import UTestCase, Report
from exam_generator.exam import Question, jinjafy
from types import SimpleNamespace
import gym
import numpy as np
import sympy as sym
import os
import inspect
from unitgrade import NotebookTestCase
class Question1(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_addition(self):
self.assertIsNotNone(self.nb.x1)
def test_multiplication(self):
self.assertIsNotNone(self.nb.x1)
class Question2(NotebookTestCase):
notebook = "exam2024spring.ipynb"
def test_derivative(self):
self.assertIsNotNone(self.nb.a)
class Exam2024spring_2(Report):
title = "Report id exam2024spring"
abbreviate_questions = True
questions = [
(Question1, 10), (Question2, 10), ]
import irlc
pack_imports = [irlc]
if __name__ == "__main__":
from unitgrade import evaluate_report_student
evaluate_report_student(Exam2024spring_2())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment