def main(train_data_path, test_data_path, output_path, n_estimators, learning_rate, max_depth, loss, verbose): if verbose: print 'Reading train and test data' data_train = pd.read_csv(train_data_path) data_test = pd.read_csv(test_data_path) if verbose: print 'Transforming datasets using MinCountSketch' X_train, y_train = transform(data_train) X_test = transform(data_test, False) if verbose: print 'Fitting a model on training dataset' model = GradientBoostingRegressor(n_estimators=n_estimators, learning_rate=learning_rate, \ loss=loss, max_depth=max_depth, verbose=verbose) model.fit(X_train, y_train) if verbose: print 'Predicting' predictions = model.predict(X_test) with open(output_path, 'w') as out: out.write(data_train.columns[0] + ',' + data_train.columns[-1] + '\n') for i in range(len(predictions)): out.write(str(data_test.ix[i, 0])) out.write(',') out.write(str(predictions[i]) + '\n')
def predict_puck_motion(table, arm, puck_pose): """ This is the main function that will be called by the raspberry pi constantly. The raspberry pi will call computer vision scripts to return the puck's current location and velocity. This script calls many other functions and overall returns information such as the final collision of the puck with the extent of reach of the arm, all the deflections of the puck with the wall, and the desired joint angles for the robot arm. :param table: :type: Struct that contains the attributes: width: width of table length: length of table :param arm: :type: Struct that contains: x: base x of arm y: base y of arm link_length: length of one link(two links for our 2DOF robot arm) num_links: number of links for one arm (in our case 2) :param puck_pose: :type: Struct: x: center x of puck y: center y of puck vx: velocity x-component of puck vy: velocity y-component of puck :returns: """ # note: graphics frame v.s real-world frame flipped on y-axis transformed_pose = copy.deepcopy(puck_pose) transformed_pose.y = util.transform(transformed_pose.y, True, table.length) transformed_pose.vy = util.transform(transformed_pose.vy, False) deflections = find_deflections(table, arm, transformed_pose, []) lin_trajectory = linearize_trajectory(puck_pose, deflections) collision_info = vector_circle_intersect(arm, lin_trajectory) joint_info = util.Struct() joint0, joint1 = calc_joints_from_pos(arm.link_length, collision_info.x - arm.x, (collision_info.y - arm.y)) joint_info.joint0 = joint0 joint_info.joint1 = joint1 return collision_info, deflections, joint_info
def mousePressed(event, data): data.ball_goal_x = event.x data.ball_goal_y = event.y approx_vel(data) # un-transformed data puck_pose = util.Struct() puck_pose.x = data.ball_x puck_pose.y = data.ball_y puck_pose.vx = data.ball_vx puck_pose.vy = data.ball_vy # store table info table = util.Struct() table.width = SC_WIDTH table.length = SC_HEIGHT # store arm info arm = util.Struct() arm.x = data.arm0_x arm.y = util.transform(data.arm0_y, True, SC_HEIGHT) arm.num_links = NUM_LINKS arm.link_length = data.arm_length # Note: to show linearized trajectory, modify the below function to also # return lin_trajectory collision_info, deflections, joint_info = ( motion.predict_puck_motion(table, arm, puck_pose)) data.deflections = transform_deflections(deflections) data.collision_x = collision_info.x data.collision_y = util.transform(collision_info.y, True, SC_HEIGHT) data.time_to_collision = collision_info.time_to_collision data.goal0 = joint_info.joint0 / DEG_TO_RAD data.goal1 = joint_info.joint1 / DEG_TO_RAD data.reached_goal = False
elif WITHIMAGE: d = 4 input_shape = (8, 8) else: d = 3 input_shape = (8, 8) m = functools.reduce(operator.mul, input_shape, 1) n = len(set(label)) images_lib = {k: pic_resize(IMAGE_PATH + str(k) + '.jpg', input_shape, pad=True) for k in range(1, 1585, 1)} \ if WITHIMAGE or IMAGEONLY else None train_data = transform(data=train, label=label, dim=d, input_shape=m, pixels=images_lib, normalize=True) default = { 'hidden_layer_1': [[5, 5, d, 32], [32]], 'hidden_layer_2': [[5, 5, 32, 64], [64]], 'dense_conn_1': [[2 * 2 * 64, 1024], [1024], [-1, 2 * 2 * 64]], 'read_out': [[1024, n], [n]], 'alpha': 5e-5, 'test_size': .20, 'batch_size': 200, 'num_epochs': 5000, 'drop_out': [.4, .5] }
def transform_deflections(deflections): for deflect in deflections: deflect[1] = util.transform(deflect[1], True, SC_HEIGHT) return deflections