|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +from models import nslip |
| 4 | +import viability as vibly |
| 5 | + |
| 6 | + |
| 7 | +if __name__ == "__main__": |
| 8 | + p = { |
| 9 | + "mass": 80.0, |
| 10 | + "stiffness": 705.0, |
| 11 | + "resting_angle": 17 / 18 * np.pi, |
| 12 | + "gravity": 9.81, |
| 13 | + "angle_of_attack": 1 / 5 * np.pi, |
| 14 | + "upper_leg": 0.5, |
| 15 | + "lower_leg": 0.5, |
| 16 | + } |
| 17 | + |
| 18 | + x0 = np.array([0.0, 0.85, 5.5, 0.0, 0.0, 0.0, 0.0]) |
| 19 | + x0 = nslip.reset_leg(x0, p) |
| 20 | + p["x0"] = x0 |
| 21 | + p["total_energy"] = nslip.compute_total_energy(x0, p) |
| 22 | + |
| 23 | + p_map = nslip.p_map |
| 24 | + p_map.p = p |
| 25 | + p_map.x = x0 |
| 26 | + p_map.sa2xp = nslip.sa2xp |
| 27 | + p_map.xp2s = nslip.xp2s |
| 28 | + |
| 29 | + s_grid = np.linspace(0.1, 1.0, 61) |
| 30 | + s_grid = (s_grid[:-1],) |
| 31 | + a_grid = (np.linspace(-10 / 180 * np.pi, 70 / 180 * np.pi, 61),) |
| 32 | + grids = {"states": s_grid, "actions": a_grid} |
| 33 | + |
| 34 | + result = vibly.compute_Q_map(grids, p_map, parallel=True) |
| 35 | + Q_map = result.q_map |
| 36 | + Q_F = result.q_fail |
| 37 | + Q_V, S_V = vibly.compute_QV(Q_map, grids) |
| 38 | + S_M = vibly.project_Q2S(Q_V, grids, proj_opt=np.mean) |
| 39 | + Q_M = vibly.map_S2Q(Q_map, S_M, s_grid, Q_V=Q_V) |
| 40 | + |
| 41 | + import pickle |
| 42 | + import os |
| 43 | + |
| 44 | + filename = "nslip_map.pickle" |
| 45 | + |
| 46 | + if os.path.exists("data"): |
| 47 | + path_to_file = "data/dynamics/" |
| 48 | + else: |
| 49 | + path_to_file = "../data/dynamics/" |
| 50 | + if not os.path.exists(path_to_file): |
| 51 | + os.makedirs(path_to_file) |
| 52 | + |
| 53 | + data2save = { |
| 54 | + "grids": grids, |
| 55 | + "Q_map": Q_map, |
| 56 | + "Q_F": Q_F, |
| 57 | + "Q_V": Q_V, |
| 58 | + "Q_M": Q_M, |
| 59 | + "S_M": S_M, |
| 60 | + "p": p, |
| 61 | + "x0": x0, |
| 62 | + } |
| 63 | + with open(path_to_file + filename, "wb") as outfile: |
| 64 | + pickle.dump(data2save, outfile) |
| 65 | + |
| 66 | + plt.imshow(Q_map, origin="lower") |
| 67 | + plt.show() |
0 commit comments