def Task11a(mode): '''time evolution''' if mode == 0: #simple oscillation a = [0, 0, np.pi / 2, 0] figtitle = 'Simple oscillation time evolution' elif mode == 1: #full rotation a = [0, 0, 0, 2] figtitle = 'Full rotation time evolution' cart.setState(a) cart.performAction() cart.sysvar[2] = [remap_angle(value) for value in cart.sysvar[2]] v = np.asarray(cart.sysvar) titles = ['Cart location', 'Cart velocity', 'Pole angle', 'Pole velocity'] fig, axes = plt.subplots(2, 2) plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) for ax, var, title in zip(axes.flatten(), v, titles): ax.plot(var) ax.set_xlabel('Time (s)') ax.set_xticks(np.linspace(0, cart.sim_steps, 5)) ax.set_xticklabels(np.linspace(0, cart.delta_time, 5)) ax.set_ylabel(title) fig.tight_layout() fig.suptitle(figtitle) fig.subplots_adjust(top=0.9)
def LFunc(p, v0, nsteps=10): l, n = 0, 0 state, a = v0[:4], v0[4] while n < nsteps: a = np.dot(p, state.T) cart.setState(state) cart.performAction(action=a) cart.sysvar[2] = np.array([remap_angle(s) for s in cart.sysvar[2]]) state = cart.sysvar[:, -1] l += np.mean([loss(s) for s in cart.sysvar.T]) n += 1 return l / nsteps
def check_error(model, n=1, remap=False, force=False, nv=10): VL = np.array([getv0(force=force) for i in range(nv)]) error = 0 for v in VL: cart.setState(v[:4]) cart.performAction(action=v[4]) sysvar = np.asarray(cart.sysvar)[:, -1] if remap: sysvar[2] = remap_angle(sysvar[2]) actual = sysvar pred = sim2(v, model, nsteps=n)[-1] error += np.mean((actual - pred)**2) return error
def Task21a(n=2000, m=160, l=0.1, dt=0.2, specific=True, v=np.array([0, 0, np.pi, 0, 0]), remap=False, force=False): '''one step ok''' cm = plt.cm.tab20(np.arange(20)) fig1, axes1 = plt.subplots(2, 2) ax1 = axes1.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) fig2, axes2 = plt.subplots(2, 2) ax2 = axes2.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) model = NLModel(n=n, m=m, l=l, force=force, dt=dt) var, xlabels = getvar(force=True) v0 = getv0(force) if specific == False else np.array(v) title = v0.copy() for i in range(4): actual, pred = [], [] for w in var[i]: v0[i] = w cart.setState(v0[:4]) cart.performAction(action=v0[4]) sysvar = np.asarray(cart.sysvar)[:, -1] if remap: sysvar[2] = remap_angle(sysvar[2]) actual.append(sysvar) pred.append(sim2(v0, model)[-1]) actual = np.asarray(actual) pred = np.asarray(pred).reshape((41, 4)) ax1[i].set_xlabel(titles[i] + ' input') ax1[i].set_xticks([i * 5 for i in np.arange(9)]) ax1[i].set_xticklabels(xlabels[i]) ax1[i].set_ylabel('Next step') for j in range(4): ax1[i].plot(actual[:, j], color=cm[j * 2]) ax1[i].plot(pred[:, j], color=cm[1 + j * 2]) ax2[i].plot(actual[:, i], pred[:, i], color=cm[i * 2]) ax2[i].set_xlabel('Actual ' + titles[i]) ax2[i].set_ylabel('Predicted ' + titles[i]) custom_lines = [Line2D([0], [0], color=cm[c * 2]) for c in range(4)] fig1.legend(custom_lines, titles, loc='lower center', ncol=4) fig1.suptitle(["%.2f" % title[t] for t in range(5)])
def sim(v0, model, nsteps=1, remap=True): state = np.asarray(v0[:4]).reshape((1, 4)) force = np.asarray(v0[4]) oldstate = state n = 0 while n < nsteps: vector = np.append(oldstate, force) newstate = oldstate + np.matmul(vector, model) if remap: newstate[:, 2] = remap_angle(newstate[:, 2]) state = np.append(state, newstate, axis=0) oldstate = newstate n += 1 return state
def sim2(v0, model, nsteps=1, remap=False): sigma, xprime, am = model['sigma'], model['xprime'], model['am'] state = v0[:4] force = v0[4] pred = [state] n = 0 while n < nsteps: curr = np.append(state, force) Knm = np.asarray([calc_K(curr, value, sigma) for value in xprime]) y = np.matmul(Knm, am) state = state + y if remap: state[2] = remap_angle(state[2]) pred.append(state) n += 1 return np.asarray(pred)
def Task21b(n=2000, m=160, delta_time=0.2, step_size=50, v=np.array([0, 0, 0.3, 0, 0]), l=0.1, nsteps=10, remap=True, specific=False, force=True): '''time evolution ok''' v0 = getv0(force) if not specific else v cart.sim_steps = step_size * nsteps cart.delta_time = delta_time * nsteps cart.setState(v0[:4]) cart.performAction(action=v0[4]) actual = np.asarray(cart.sysvar) model = NLModel(n=n, m=m, l=l, force=force, dt=delta_time) pred = np.asarray(sim2(v0, model, nsteps=nsteps, remap=remap)) if remap: actual[2] = np.asarray([remap_angle(value) for value in actual[2]]) aindex = len(actual[0]) pindex = [ index * step_size for index in range(int(np.ceil(aindex / step_size))) ] cm = plt.cm.tab20(np.arange(20)) fig, axes = plt.subplots(2, 2) ax = axes.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) xspace = int(nsteps / 5) for i in range(4): ax[i].plot(actual[i]) ax[i].plot(pindex, pred[:, i]) ax[i].set_xticks( [n * step_size * xspace for n in np.arange(int(nsteps / xspace))]) ax[i].set_xticklabels( [n * xspace for n in np.arange(int((nsteps + 1) / xspace))]) ax[i].set_xlabel('Number of steps') ax[i].set_ylabel(titles[i]) fig.legend(['Actual', 'Predicted'], loc='lower center', ncol=2) fig.suptitle('Actual vs predicted Time Evolution') print(v0) return model
def Task14(model, delta_time=0.2, step_size=50, nsteps=10, remap=True, specific=None, force=False): '''time evolution''' cart.sim_steps = step_size * nsteps cart.delta_time = delta_time * nsteps fig, axes = plt.subplots(2, 2) ax = axes.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) xspace = int(nsteps / 5) if specific == None: v0 = getv0(force) else: v0 = specific cart.setState(v0[:4]) cart.performAction(action=v0[4]) actual = np.asarray(cart.sysvar) if remap: actual[2] = np.asarray([remap_angle(value) for value in actual[2]]) pred = sim(v0, model, nsteps=nsteps, remap=remap) nindex = len(actual[0]) rindex = [ index * step_size for index in range(int(np.ceil(nindex / step_size))) ] for i in range(4): ax[i].plot(actual[i]) ax[i].plot(rindex, pred[:, i]) ax[i].set_xticks( [n * step_size * xspace for n in np.arange(int(nsteps / xspace))]) ax[i].set_xticklabels( [n * xspace for n in np.arange(int((nsteps + 1) / xspace))]) ax[i].set_xlabel('Number of steps') ax[i].set_ylabel(titles[i]) fig.legend(['Actual', 'Predicted'], loc='lower center', ncol=2) fig.suptitle('Actual vs predicted Time Evolution') print(v0)
def Task13(model, nsteps=1, remap=True, force=False): '''(one step) change in state var against var''' cm = plt.cm.tab20(np.arange(20)) fig1, axes1 = plt.subplots(2, 2) ax1 = axes1.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) fig2, axes2 = plt.subplots(2, 2) ax2 = axes2.flatten() plt.subplots_adjust(hspace=0.3, wspace=0.4, bottom=0.15) var, xlabels = getvar() v0 = getv0(force) title = v0.copy() for i in range(4): actual, pred = [], [] for w in var[i]: v0[i] = w cart.setState(v0[:4]) cart.performAction(action=v0[4]) if remap: cart.sysvar[2] = [ remap_angle(value) for value in cart.sysvar[2] ] actual.append(np.asarray(cart.sysvar)[:, -1]) pred.append(sim(v0, model, nsteps=nsteps, remap=remap)[-1]) actual = np.asarray(actual).reshape((41, 4)) pred = np.asarray(pred).reshape((41, 4)) ax1[i].set_xlabel(titles[i] + ' input') ax1[i].set_xticks([i * 5 for i in np.arange(9)]) ax1[i].set_xticklabels(xlabels[i]) ax1[i].set_ylabel('Next step') for v in range(4): ax1[i].plot(actual[:, v], color=cm[v * 2]) ax1[i].plot(pred[:, v], color=cm[1 + v * 2]) ax2[i].plot(actual[:, i], pred[:, i], color=cm[i * 2]) ax2[i].set_xlabel('Actual ' + titles[i]) ax2[i].set_ylabel('Predicted ' + titles[i]) custom_lines = [Line2D([0], [0], color=cm[c * 2]) for c in range(4)] fig1.legend(custom_lines, titles, loc='lower center', ncol=4) fig1.suptitle(["%.2f" % title[t] for t in range(5)])