# Rows: Data Set roll_r_squared = {} steer_r_squared = {} for model_name, canonical_matrices in id_matrices.items(): roll_r_squared[model_name] = [] steer_r_squared[model_name] = [] for i, queries in enumerate(combinations_of_queries): print('Computing the coefficient of determination for data ' + 'set {} with model {}.'.format(data_set_names[i], model_name)) if model_name == 'Whipple': canonical_matrices = cbi.mean_canon(queries[0], canon, H) elif model_name == 'Arm': pass #A, B, speeds = cbi.mean_arm([rider]) #M = np.linalg.inv(B[round(v * 10)][2:, [0, 1]]) #C = -np.dot(M, A[round(v * 10)][2:, [2, 3]]) #K = -np.dot(M, A[round(v * 10)][2:, [0, 1]]) # I need some way to compute the A and b matrices using just the # M, C, and K matrices. # Select the subset of runs and remove any that have errors. run_numbers = cbi.select_runs(queries[0], maneuvers, queries[1]) run_numbers = list(set(run_numbers).intersection(good_runs)) print('Number of runs: {}'.format(len(run_numbers))) roll_r_squared[model_name].append(
for combo in combinations: print('Computing the estimate for riders: {} and environments: {}'.format( combo[0], combo[1])) if combo[0] == 'All': riders = ['Charlie', 'Jason', 'Luke'] else: riders = [combo[0]] if combo[1] == 'All': environments = ['Horse Treadmill', 'Pavillion Floor'] else: environments = [combo[1]] runs = cbi.select_runs(riders, maneuvers, environments) runs = list(set(runs).difference(errors)) means = cbi.mean_canon(riders, canon, H) idMat, rollCovar, steerCovar = cbi.enforce_symmetry( runs, trials, rollParams, steerParams, *means) idMatrices[combo[0][0] + '-' + combo[1][0]] = idMat covarMatrices[combo[0][0] + '-' + combo[1][0]] = (rollCovar, steerCovar) print('Done.') with open('../data/goodRuns.p', 'w') as f: cPickle.dump(goodRuns, f) # save all of the identified matrices to file with open('../data/idMatrices.p', 'w') as f: cPickle.dump(idMatrices, f)
if combo[0] == 'All': riders = ['Charlie', 'Jason', 'Luke'] else: riders = [combo[0]] if combo[1] == 'All': environments = ['Horse Treadmill', 'Pavillion Floor'] else: environments = [combo[1]] # Select the subset of runs and remove any that have errors. runs = cbi.select_runs(riders, maneuvers, environments) runs = list(set(runs).difference(errors)) # Compute the mean first principles model with respect to the subset of # riders. This returns a tuple: (M, C1, K0, K2, H) means = cbi.mean_canon(riders, canon, H) # Solve the linear least squares: roll equation first, steer equation # second with the matrix symmetry enforced from the roll results. id_matrix, roll_covariance, steer_covariance = \ cbi.enforce_symmetry(runs, trials, roll_parameters, steer_parameters, *means) id_matrices[combo[0][0] + '-' + combo[1][0]] = id_matrix covariance_matrices[combo[0][0] + '-' + combo[1][0]] = (roll_covariance, steer_covariance) print('Done.') # Store the identified models, the parameter covariances, and the list of # good runs.
def plot(canon, H, riders, environments, idMats): filename = '' for rider in riders: filename += '-' + rider for env in environments: filename += '-' + env.replace(' ', '') filename = '../plots/' + filename[1:] print filename v0 = 0. vf = 10. num = 100 # identified iM, iC1, iK0, iK2, iH = idMats speeds, iAs, iBs = bicycle.benchmark_state_space_vs_speed(iM, iC1, iK0, iK2, v0=v0, vf=vf, num=num) w, v = control.eigen_vs_parameter(iAs) iEigenvalues, iEigenvectors = control.sort_modes(w, v) # whipple model (mean) wM, wC1, wK0, wK2, wH = cbi.mean_canon(riders, canon, H) speeds, wAs, wBs = bicycle.benchmark_state_space_vs_speed(wM, wC1, wK0, wK2, v0=v0, vf=vf, num=num) w, v = control.eigen_vs_parameter(wAs) wEigenvalues, wEigenvectors = control.sort_modes(w, v) # arm model (mean) aAs, aBs, aSpeed = cbi.mean_arm(riders) indices = np.int32(np.round(speeds * 10)) w, v = control.eigen_vs_parameter(aAs[indices]) aEigenvalues, aEigenvectors = control.sort_modes(w, v) # eigenvalue plot rlfig = cbi.plot_rlocus_parts(speeds, iEigenvalues, wEigenvalues, aEigenvalues) rlfig.savefig(filename + '-eig.png') # root locus v0 = 0. vf = 10. num = 20 speeds, iAs, iBs = bicycle.benchmark_state_space_vs_speed(iM, iC1, iK0, iK2, v0=v0, vf=vf, num=num) iEig, null = control.eigen_vs_parameter(iAs) speeds, wAs, wBs = bicycle.benchmark_state_space_vs_speed(wM, wC1, wK0, wK2, v0=v0, vf=vf, num=num) wEig, null = control.eigen_vs_parameter(wAs) indices = np.int32(np.round(speeds * 10)) aEig, null = control.eigen_vs_parameter(aAs[indices]) rlcfig = cbi.plot_rlocus(speeds, iEig, wEig, aEig) rlcfig.savefig(filename + '-rlocus.png') # bode plots speeds = np.array([2.0, 4.0, 6.0, 9.0]) null, iAs, iBs = bicycle.benchmark_state_space_vs_speed(iM, iC1, iK0, iK2, speeds) null, wAs, wBs = bicycle.benchmark_state_space_vs_speed(wM, wC1, wK0, wK2, speeds) figs = cbi.plot_bode(speeds, iAs, iBs, wAs, wBs, aAs, aBs) figs[0].savefig(filename + '-Tphi.png') figs[1].savefig(filename + '-Tdel.png')
num = 100 # identified for all rider and all environments iM, iC1, iK0, iK2, iH = idMat['A-A'] speeds, iAs, iBs = bicycle.benchmark_state_space_vs_speed(iM, iC1, iK0, iK2, v0=v0, vf=vf, num=num) w, v = control.eig_of_series(iAs) iEigenvalues, iEigenvectors = control.sort_modes(w, v) # whipple model (mean) wM, wC1, wK0, wK2, wH = cbi.mean_canon(allRiders, canon, H) speeds, wAs, wBs = bicycle.benchmark_state_space_vs_speed(wM, wC1, wK0, wK2, v0=v0, vf=vf, num=num) w, v = control.eig_of_series(wAs) wEigenvalues, wEigenvectors = control.sort_modes(w, v) # arm model (mean) aAs, aBs, aSpeed = cbi.mean_arm(allRiders) indices = np.int32(np.round(speeds * 10)) w, v = control.eig_of_series(aAs[indices]) aEigenvalues, aEigenvectors = control.sort_modes(w, v)
# load the H lateral force vector for each rider H = cbi.lateral_force_contribution(allRiders) # Eigenvalues versus speed plot. v0 = 0.0 vf = 10.0 num = 100 # identified for all rider and all environments iM, iC1, iK0, iK2, iH = idMat["A-A"] speeds, iAs, iBs = bicycle.benchmark_state_space_vs_speed(iM, iC1, iK0, iK2, v0=v0, vf=vf, num=num) w, v = control.eig_of_series(iAs) iEigenvalues, iEigenvectors = control.sort_modes(w, v) # whipple model (mean) wM, wC1, wK0, wK2, wH = cbi.mean_canon(allRiders, canon, H) speeds, wAs, wBs = bicycle.benchmark_state_space_vs_speed(wM, wC1, wK0, wK2, v0=v0, vf=vf, num=num) w, v = control.eig_of_series(wAs) wEigenvalues, wEigenvectors = control.sort_modes(w, v) # arm model (mean) aAs, aBs, aSpeed = cbi.mean_arm(allRiders) indices = np.int32(np.round(speeds * 10)) w, v = control.eig_of_series(aAs[indices]) aEigenvalues, aEigenvectors = control.sort_modes(w, v) rlfig = cbi.plot_rlocus_parts(speeds, iEigenvalues, wEigenvalues, aEigenvalues) rlfig.set_size_inches(5.0, 5.0 / goldenRatio) rlfig.savefig("../../figures/systemidentification/A-A-eig.png") rlfig.savefig("../../figures/systemidentification/A-A-eig.pdf")