void NitroFile::replaceContents(void* ptr, int nsize) { iprintf("Replacing: "); print(); int newOffs = begOffs; if(size < nsize) { newOffs = parent->findFreeSpace(nsize); iprintf("%x -> %x\n", nsize, newOffs); } begOffs = newOffs; size = nsize; endOffs = begOffs+size; fseek(parent->romFile, begOffs, SEEK_SET); fwrite(ptr, 1, nsize, parent->romFile); saveOffsets(); if(!cached) parent->flushCache(); }
int main(){ // Load Movie data loadMovieData(); // printf("test %f %f %f %f\n", userOffset[5], userOffset[num_users - 1], movieOffset[5], movieOffset[num_movies - 1]); // Set up offsets and implicit data userOffset = calloc(num_users, sizeof(float)); movieOffset = calloc(num_movies, sizeof(float)); userImplicitData = calloc(num_users*2, sizeof(float)); if (userOffset == NULL || movieOffset == NULL || userImplicitData == NULL) { printf("Malloc failed\n"); return -1; } //loadData("../stats/user_offset_reg2.dta", userOffset); //loadData("../stats/movie_offset_reg.dta", movieOffset); loadUserImplicit("../stats/user_implicit_2.dta", userImplicitData); // Initialize features // Initialize random seed srand (time(NULL)); initializeUserFeatures(); initializeMovieFeatures(); initializeImplicitMovies("../../implicit/user_implicit_movies.dta"); //printf("test %f\n", userImplicitMovies[num_users - 1][2]); initializeImplicitFeatures(); printf("\n--------------Training --------------\n"); unsigned int user, movie, rating, line_number; unsigned int temp = 0; float err, feature_c; float train_err, val_err; train_errs = calloc(epochs, sizeof(float)); float n = userImplicitData[1*2 + 1]; // get n for first user float GLOBAL_AVG = 3.609516; for (unsigned int i = 1; i <= epochs; i++) { train_err = 0; val_err = 0; for (unsigned int j = 0; j < num_lines; j++) { line_number = j * 3; user = movie_data[line_number]; movie = movie_data[line_number + 1]; rating = movie_data[line_number + 2]; // Get rating from raw data if we have a new user if (temp != user) { // update implict feature for previous user if (temp > 0) { updateImplicitFeatures(temp, n); } n = userImplicitData[user*2 + 1]; getImplicitC(user, n); //baseline_c = userOffset[user] + movieOffset[movie]; temp = user; // if (user == 34821) { // printf("user %d err %f n %f implicitC %f tempC %f\n", user, total_err, n, implicitC[0], tempImplicitC[0]); // printf("base %f\n", baseline_c); // } } feature_c = 0; for (unsigned int i = 0; i < num_features; i++) { feature_c += (userFeatures[user][i] + tempImplicitC[i])* movieFeatures[movie][i]; } err = (float) rating - (GLOBAL_AVG + userOffset[user] + movieOffset[movie] + feature_c); //printf("err %f rating %d predict %f baseline_c %f feature_c %f\n", err, rating, GLOBAL_AVG + baseline_c + feature_c, baseline_c, feature_c); // if (idx[j] == 4) { // val_err += err * err; // } train_err += err * err; updateFeatures(user, movie, err, n); updateBaseline(user, movie, err); } // update last user updateImplicitFeatures(user, n); // Update gammas by factor gamma1 *= gamma_step; gamma2 *= gamma_step; train_errs[i-1] = sqrt(train_err / num_lines); printf("Epoch %d Train RMSE: %f\n", i, train_errs[i-1]); } printf("-----------Saving features-----------\n"); saveOffsets(); saveUserFeatures("features/f220_e050/user_features.dta"); saveMovieFeatures("features/f220_e050/movie_features.dta"); saveImplicitFeatures("features/f220_e050/implicit_features.dta"); saveErrors("features/f220_e050/error.dta"); free(userOffset); free(movieOffset); free(movie_data); return 0; }
int main(){ // Load Movie data loadMovieData(); // printf("test %f %f %f %f\n", userOffset[5], userOffset[num_users - 1], movieOffset[5], movieOffset[num_movies - 1]); // Set up offsets and implicit data userOffset = calloc(num_users, sizeof(float)); movieOffset = calloc(num_movies, sizeof(float)); userImplicitData = calloc(num_users*2, sizeof(float)); if (userOffset == NULL || movieOffset == NULL || userImplicitData == NULL) { printf("Malloc failed\n"); return -1; } loadData("../stats/user_offset_reg2.dta", userOffset); loadData("../stats/movie_offset_reg.dta", movieOffset); loadUserImplicit("../stats/user_implicit_2.dta", userImplicitData); // Initialize features // Initialize random seed srand (time(NULL)); initializeUserFeatures(); initializeMovieFeatures(); initializeImplicitMovies("../../implicit/user_implicit_movies.dta"); //printf("test %f\n", userImplicitMovies[num_users - 1][2]); initializeImplicitFeatures(); printf("\n--------------Training --------------\n"); int user, movie, line_number; float rating, predict, err; float total_err; for (int i = 1; i <= epochs; i++) { total_err = 0; for (int j = 0; j < num_lines; j++) { line_number = j * 3; user = movie_data[line_number]; movie = movie_data[line_number + 1]; rating = (float)movie_data[line_number + 2]; // printf("User %d Movie %d Rating %d Baseling %f\n", user, movie, rating, baseline); getImplicitC(user); predict = predictRating(user, movie); err = rating - predict; total_err += err * err; updateFeatures(user, movie, err); updateBaseline(user, movie, err); updateImplicitFeatures(user, err); } // Update gammas by factor gamma1 *= gamma_step; gamma2 *= gamma_step; printf("Epoch %d RMSE: %f\n", i, sqrt(total_err / num_lines)); } printf("-----------Saving features-----------\n"); saveOffsets(); saveUserFeatures("f010_e020/user_features.dta"); saveMovieFeatures("f010_e020/movie_features.dta"); saveImplicitFeatures("f010_e020/implicit_features.dta"); free(userOffset); free(movieOffset); free(movie_data); return 0; }
/* * raven_homing() * 1- Discover joint position by running to hard stop * 2- Move joints to "home" position. * */ int raven_homing(struct device *device0, struct param_pass *currParams, int begin_homing) { static int homing_inited = 0; static unsigned long int delay, delay2; struct DOF *_joint = NULL; struct mechanism* _mech = NULL; int i=0,j=0; // Only run in init mode if ( ! (currParams->runlevel == RL_INIT && currParams->sublevel == SL_AUTO_INIT )) { homing_inited = 0; delay = gTime; return 0; } // Wait a short time for amps to turn on if (gTime - delay < 1000) { return 0; } // Initialize the homing sequence. if (begin_homing || !homing_inited) { // Zero out joint torques, and control inputs. Set joint.state=not_ready. _mech = NULL; _joint = NULL; while (loop_over_joints(device0, _mech, _joint, i,j) ) { _joint->tau_d = 0; _joint->mpos_d = _joint->mpos; _joint->jpos_d = _joint->jpos; _joint->jvel_d = 0; _joint->state = jstate_not_ready; if (is_toolDOF(_joint)) jvel_PI_control(_joint, 1); // reset PI control integral term homing_inited = 1; } } // Specify motion commands _mech = NULL; _joint = NULL; while ( loop_over_joints(device0, _mech, _joint, i,j) ) { // Initialize tools first. if ( is_toolDOF(_joint) || tools_ready( &(device0->mech[i]) ) ) { homing(_joint); } } //Inverse Cable Coupling invCableCoupling(device0, currParams->runlevel); // Do PD control on all joints _mech = NULL; _joint = NULL; while ( loop_over_joints(device0, _mech, _joint, i,j) ) { mpos_PD_control( _joint ); } // Calculate output DAC values TorqueToDAC(device0); // Check homing conditions and set joint angles appropriately. _mech = NULL; _joint = NULL; while ( loop_over_joints(device0, _mech, _joint, i,j) ) { struct DOF * _joint = &(_mech->joint[j]); // Check to see if we've reached the joint limit. if( check_homing_condition(_joint) ) { log_msg("Found limit on joint %s", jointIndexAndArmName(_joint->type).c_str(), _joint->current_cmd, DOF_types[_joint->type].DAC_max); _joint->state = jstate_hard_stop; _joint->current_cmd = 0; stop_trajectory(_joint); } // For each mechanism, check to see if the mech is finished homing. if ( j == (MAX_DOF_PER_MECH-1) ) { /// if we're homing tools, wait for tools to be finished bool ready = ( !tools_ready(_mech) && _mech->joint[TOOL_ROT].state==jstate_hard_stop && _mech->joint[WRIST ].state==jstate_hard_stop && _mech->joint[GRASP1 ].state==jstate_hard_stop ) || ( tools_ready( _mech ) && _mech->joint[SHOULDER].state==jstate_hard_stop && _mech->joint[ELBOW ].state==jstate_hard_stop && _mech->joint[Z_INS ].state==jstate_hard_stop ); if (ready) { if (delay2==0) { delay2=gTime; } if (gTime > delay2 + 200) { set_joints_known_pos(_mech, !tools_ready(_mech) ); delay2 = 0; } } } } saveOffsets(*device0); return 0; }