import pickle
import matplotlib.pyplot as plt
"""   Initialize lorenz object by loading parameters from the training data file   """
f = open("learning_algorithm2_training_data", "rb")
d = pickle.load(f)
sigma = d['sigma']
b = d['b']
r = d['r']
lrz = Lorenz(sigma, b, r)
lrz.X = d['X']
lrz.U = d['U']
"""  Initialize lorenz object state and compute trajectories with learning based control, lyapunov based control, 
and without any control  """
n = 6000  # number of time steps
lrz.state = [-4, -4, -1]
y_l, u_l, t_l = lrz.trajectory(n, 0)
lrz.state = [-4, -4, -1]
y_m, u_m, t_m = lrz.trajectory(n, 1)
lrz.state = [-4, -4, -1]
y_wc, t_wc = lrz.trajectory_no_control(n)
"""  trajectory visualization  """
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.plot(y_wc[:, 0],
        y_wc[:, 1],
        y_wc[:, 2],
        'r',
        linewidth=2,
        label="uncontrolled trajectory")
ax.plot(y_l[:, 0],
        y_l[:, 1],
control objective is achieved and documented as a 1 in the task. otherwise it is documented as 0.
sum of all the tasks divided by number of tasks gives the accuracy of the learning algorithm
"""
m = 1000  # number of trajectories to generate for checking accuracy of learned algorithm
n = 1000  # number of time steps for each trajectory
lrz.dt = 0.01  # set default time step to 0.01
xstart = np.zeros((m, 3))  # stores the initial state of the n trajectories
xend = np.zeros((m, 3))  # stores the final state of the n trajectories
task = np.zeros(
    (m, 1), dtype=float
)  # for each trajectory task stores 1 (resp. 0) for control objective
# (resp. not) achieved

for j in range(m):
    xstart[j, :] = lrz.reset()
    lrz.trajectory(n, 0)
    xend[j, :] = lrz.state
    if lrz.reward() > -0.15:
        task[j, 0] = 1.0

print('Efficiency of the learning algorithm is ',
      np.squeeze(100 * sum(task) / m), '%')

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(xstart[:, 0],
           xstart[:, 1],
           xstart[:, 2],
           c='k',
           marker='x',
           label="starting states")