bool open_weights( const char *weight_file, s_motif *motifs[], int num_motifs ) { // open the file ifstream ifs; _CALL( _open_input( ifs, weight_file, ".txt" ) ); // find the EOL character char this_EOL = _find_EOL( ifs ); if( !this_EOL ) // no EOL character was identified { cout << "ERROR: The file has no EOL character." << endl; return false; } // read the file cout << "Reading the weights..." << endl; _CALL( read_weights( ifs, motifs, num_motifs, this_EOL ) ); ifs.close(); return true; }
void dd::FactorGraph::load(const CmdParser & cmd, const bool is_quiet) { // get factor graph file names from command line arguments std::string weight_file = cmd.weight_file->getValue(); std::string variable_file = cmd.variable_file->getValue(); std::string factor_file = cmd.factor_file->getValue(); std::string edge_file = cmd.edge_file->getValue(); std::string filename_edges = edge_file; std::string filename_factors = factor_file; std::string filename_variables = variable_file; std::string filename_weights = weight_file; // load variables long long n_loaded = read_variables(filename_variables, *this); assert(n_loaded == n_var); if (!is_quiet) { std::cout << "LOADED VARIABLES: #" << n_loaded << std::endl; std::cout << " N_QUERY: #" << n_query << std::endl; std::cout << " N_EVID : #" << n_evid << std::endl; } // load factors n_loaded = read_factors(filename_factors, *this); assert(n_loaded == n_factor); if (!is_quiet) { std::cout << "LOADED FACTORS: #" << n_loaded << std::endl; } // load weights n_loaded = read_weights(filename_weights, *this); assert(n_loaded == n_weight); if (!is_quiet) { std::cout << "LOADED WEIGHTS: #" << n_loaded << std::endl; } // sort the above components // NOTE This is very important, as read_edges assume variables, // factors and weights are ordered so that their id is the index // where they are stored in the array this->sort_by_id(); // load edges n_loaded = read_edges(edge_file, *this); if (!is_quiet) { std::cout << "LOADED EDGES: #" << n_loaded << std::endl; } // construct edge-based store this->organize_graph_by_edge(); this->safety_check(); assert(this->is_usable() == true); }
genotype::genotype (data *d) { this->d = d; d->get_string ("genotypename",genofile,true); d->get_string ("snpname",snpfile,true); d->get_string ("indivname",indivfile,true); d->get_boolean ("genotype", isgenotype, true); read_snps (snpfile); read_ind (indivfile); read_genotypes (genofile); givenweights = d->get_string ("weightname",weightfile,false); if (givenweights) read_weights (weightfile); d->get_boolean ("outputfail", outputfail, false, false); }
int main(int argc, char *argv[]) { /* Initialize variables used by the Agent thread */ int p; int a, aprime; float s[DECAY_COUNT], sprime[DECAY_COUNT]; int reward; int tile_array[feature_count]; unsigned int i; double delta; double Q[ACTION_COUNT]; // Learning parameters double stepsize = 0.1 / (float) num_tilings; double lambda = 0.9; double gamma = 0.9; struct sigaction act; struct sigaction oldact; act.sa_handler = endProgram; sigemptyset(&act.sa_mask); act.sa_flags = 0; sigaction(SIGINT, &act, &oldact); srand(0); pthread_mutex_init(&pktNumMutex, NULL); pthread_mutex_init(&actionMutex, NULL); pthread_mutex_init(&rewardMusicMutex, NULL); trajectoryFile.open("trajectory.txt"); if(!trajectoryFile.is_open()) printf("Trajectory file could not be opened.\n"); /* Set up variables used by individual policy components */ // --- begin initialize variables for Tians code timeStep = 0; leftCount=0, rightCount =0; diff = 0; actionToTake = 1; count = 0; alignPhase = 1; notInRightMode = false; // --- end initialize variables for Tians code // --- begin initialize variables for Amirs code cwTurn = 1; // --- end initialize variables for Amirs code // initialize weights // first try to read the weight file and if there is no file, then initialize randomly if(!read_weights(weights)){ for (i = 0; i < memory_size; i++) { weights[i] = -100.0/num_tilings; } } for (i = 0; i < memory_size; i++) { e[i] = 0; } // Set up timing + packet number p = pktNum; // State based on IR byte s[0] = redDecay; s[1] = greenDecay; s[2] = bumpDecay; s[3] = leftDecay; s[4] = rightDecay; s[5] = forwardDecay; s[6] = backwardDecay; s[7] = stopDecay; s[8] = chargeDecay; a = sAPrime[p];//epsilonGreedy(weights, s, epsilon); // Use a lock to ensure action is changed separately pthread_mutex_lock( &actionMutex ); action = a; // sets up action to be taken by csp thread pthread_mutex_unlock( &actionMutex ); prevPktNum = myPktNum; // Main agent loop while (TRUE) { int rval = getNextPacket(); if (rval == -1) { write_weights(weights); printf("Complete! Weights saved to weights.txt. Ran %d episodes.", episode + 1); break; } else if (rval == 1) { // Episode complete for (i = 0; i < memory_size; i++) { e[i] = 0; } episode++; } // Get the packet number p = pktNum; // Update decays updateDecay(p, prevPktNum, myPktNum); //printf("ir: %d\n", sIRbyte[p]); // Reward of -1 per step reward = -1; // Determine the next observation // TODO: Change this to new state representation sprime[0] = redDecay; sprime[1] = greenDecay; sprime[2] = bumpDecay; sprime[3] = leftDecay; sprime[4] = rightDecay; sprime[5] = forwardDecay; sprime[6] = backwardDecay; sprime[7] = stopDecay; sprime[8] = chargeDecay; aprime = sAPrime[p];//epsilonGreedy(weights, sprime, epsilon); // Set action variable pthread_mutex_lock( &actionMutex ); action = aprime; // sets up action to be taken by csp thread pthread_mutex_unlock( &actionMutex ); // Get Q values getQ(Q, s, weights, num_tilings, memory_size); delta = reward - Q[a]; getQ(Q, sprime, weights, num_tilings, memory_size); delta += gamma * Q[aprime]; // Update weights get_tiles(s, a, tile_array, num_tilings, memory_size); for (i = 0; i < feature_count; i++) { e[tile_array[i]] = 1; } //printf("Docking: s a r s' a':%d %d %d %d %d\n", s, a, reward, sprime, aprime); for (i = 0; i < memory_size; i++ ) { weights[i] += stepsize * delta * e[i]; e[i] *= lambda; } // Decay sensor traces performDecay(); for (i = 0; i < DECAY_COUNT; i++) { s[i] = sprime[i]; } a = aprime; prevPktNum = myPktNum; } return 0; }
unsigned read_next_wfa (wfa_t *wfa, bitfile_t *input) /* * Read next WFA frame of the WFA stream 'input'. * WFA header information has to be already present in the 'wfainfo' struct. * (i.e. open_wfa must be called first!) * * No return value. * * Side effects: * wfa->into, wfa->weights, wfa->final_distribution, wfa->states * wfa->x, wfa->y, wfa->level_of_state, wfa->domain_type * mt->type, mt->number are filled with the values of the WFA file. */ { tiling_t tiling; /* tiling information */ unsigned frame_number; /* current frame number */ assert (wfa && input); /* * Frame header information */ { const unsigned rice_k = 8; /* parameter of Rice Code */ wfa->states = read_rice_code (rice_k, input); wfa->frame_type = read_rice_code (rice_k, input); frame_number = read_rice_code (rice_k, input); } if (wfa->wfainfo->release > 1) /* no alignment in version 1 */ { INPUT_BYTE_ALIGN (input); } /* * Read image tiling info */ if (get_bit (input)) /* tiling performed ? */ read_tiling (&tiling, wfa->wfainfo->width, wfa->wfainfo->height, wfa->wfainfo->level, input); else tiling.exponent = 0; INPUT_BYTE_ALIGN (input); read_tree (wfa, &tiling, input); /* * Compute domain pool. * Large images have not been used due to image tiling. */ { unsigned state; for (state = wfa->basis_states; state < wfa->states; state++) if ((!wfa->wfainfo->color || (int) state <= wfa->tree [wfa->tree [wfa->root_state][0]][0]) && (!tiling.exponent || wfa->level_of_state [state] <= (wfa->wfainfo->level - tiling.exponent)) && ((wfa->x [state][0] + width_of_level (wfa->level_of_state [state])) <= wfa->wfainfo->width) && ((wfa->y [state][0] + height_of_level (wfa->level_of_state [state])) <= wfa->wfainfo->height)) wfa->domain_type [state] = USE_DOMAIN_MASK; else wfa->domain_type [state] = 0; } if (tiling.exponent) Free (tiling.vorder); if (get_bit (input)) /* nondeterministic prediction used */ read_nd (wfa, input); if (wfa->frame_type != I_FRAME) /* motion compensation used */ read_mc (wfa->frame_type, wfa, input); locate_delta_images (wfa); /* * Read linear combinations (coefficients and indices) */ { unsigned edges = read_matrices (wfa, input); if (edges) read_weights (edges, wfa, input); } /* * Compute final distribution of all states */ { unsigned state; for (state = wfa->basis_states; state <= wfa->states; state++) wfa->final_distribution[state] = compute_final_distribution (state, wfa); } return frame_number; }
int main(int argc, char *argv[]) { Cmdline *cmd; struct psrfits pfi, pfo; // input and output struct subband_info si; int stat=0, padding=0, userN=0; // Call usage() if we have no command line arguments if (argc == 1) { Program = argv[0]; usage(); exit(0); } // Parse the command line using the excellent program Clig cmd = parseCmdline(argc, argv); // Open the input PSRFITs files psrfits_set_files(&pfi, cmd->argc, cmd->argv); // Use the dynamic filename allocation if (pfi.numfiles==0) pfi.filenum = cmd->startfile; pfi.tot_rows = pfi.N = pfi.T = pfi.status = 0; int rv = psrfits_open(&pfi); if (rv) { fits_report_error(stderr, rv); exit(1); } // Read the user weights if requested si.userwgts = NULL; if (cmd->wgtsfileP) { read_weights(cmd->wgtsfile, &userN, &si.userwgts); if (userN != pfi.hdr.nchan) { printf("Error!: Input data has %d channels, but '%s' contains only %d weights!\n", pfi.hdr.nchan, cmd->wgtsfile, userN); exit(0); } printf("Overriding input channel weights with those in '%s'\n", cmd->wgtsfile); } // Initialize the subbanding // (including reading the first row of data and // putting it in si->fbuffer) init_subbanding(&pfi, &pfo, &si, cmd); if (cmd->outputbasenameP) strcpy(pfo.basefilename, cmd->outputbasename); // Loop through the data do { // Put the overlapping parts from the next block into si->buffer float *ptr = pfi.sub.fdata + si.buflen * si.bufwid; if (padding==0) stat = psrfits_read_part_DATA(&pfi, si.max_overlap, si.numunsigned, ptr); if (stat || padding) { // Need to use padding since we ran out of data printf("Adding a missing row (#%d) of padding to the subbands.\n", pfi.tot_rows); // Now fill the last part of si->fbuffer with the chan_avgs so that // it acts like a correctly read block (or row) fill_chans_with_avgs(si.max_overlap, si.bufwid, ptr, si.chan_avgs); } //print_raw_chan_stats(pfi.sub.data, pfi.hdr.nsblk, // pfi.hdr.nchan, pfi.hdr.npol); // if the input data isn't 8 bit, unpack: if (pfi.hdr.nbits == 2) pf_unpack_2bit_to_8bit(&pfi, si.numunsigned); else if (pfi.hdr.nbits == 4) pf_unpack_4bit_to_8bit(&pfi, si.numunsigned); if ((pfo.hdr.ds_time_fact == 1) && (pfo.hdr.ds_freq_fact == 1)) { // No subbanding is needed, so just copy the float buffer // This is useful if we are just changing the number of bits // Could do it without a copy by simply exchanging pointers // to the fdata buffers in pfo and pfi... memcpy(pfo.sub.fdata, pfi.sub.fdata, pfi.hdr.nsblk * pfi.hdr.npol * pfi.hdr.nchan * sizeof(float)); } else { // Now create the subbanded row in the output buffer make_subbands(&pfi, &si); } // Output only Stokes I (in place via floats) if (pfo.hdr.onlyI && pfo.hdr.npol==4) get_stokes_I(&pfo); // Downsample in time (in place via floats) if (pfo.hdr.ds_time_fact > 1) downsample_time(&pfo); // Compute new scales and offsets so that we can pack // into 8-bits reliably if (pfo.rownum == 1) new_scales_and_offsets(&pfo, si.numunsigned, cmd); // Convert the floats back to bytes in the output array un_scale_and_offset_data(&pfo, si.numunsigned); //print_raw_chan_stats(pfo.sub.data, pfo.hdr.nsblk / pfo.hdr.ds_time_fact, // pfo.hdr.nchan / pfo.hdr.ds_freq_fact, pfo.hdr.npol); // pack into 2 or 4 bits if needed if (pfo.hdr.nbits == 2) pf_pack_8bit_to_2bit(&pfo, si.numunsigned); else if (pfo.hdr.nbits == 4) pf_pack_8bit_to_4bit(&pfo, si.numunsigned); // Write the new row to the output file pfo.sub.offs = (pfo.tot_rows+0.5) * pfo.sub.tsubint; psrfits_write_subint(&pfo); // Break out of the loop here if stat is set if (stat) break; // shift the last part of the current row into the "last-row" // part of the data buffer memcpy(si.fbuffer, si.fbuffer + si.buflen * si.bufwid, si.max_overlap * si.bufwid * sizeof(float)); // Read the next row (or padding) padding = get_current_row(&pfi, &si); // Set the new weights properly new_weights(&pfi, &pfo); } while (pfi.status == 0); print_clips(&pfo); rv = psrfits_close(&pfi); if (rv>100) { fits_report_error(stderr, rv); } rv = psrfits_close(&pfo); if (rv>100) { fits_report_error(stderr, rv); } exit(0); }
int main(int argc, char *argv[]) { char *p; int method; int in_fd; int selection_fd; int out_fd; DCELL *result; char *selection; RASTER_MAP_TYPE map_type; int row, col; int readrow; int nrows, ncols; int n; int copycolr; int half; stat_func *newvalue; stat_func_w *newvalue_w; ifunc cat_names; double quantile; const void *closure; struct Colors colr; struct Cell_head cellhd; struct Cell_head window; struct History history; struct GModule *module; struct { struct Option *input, *output, *selection; struct Option *method, *size; struct Option *title; struct Option *weight; struct Option *gauss; struct Option *quantile; } parm; struct { struct Flag *align, *circle; } flag; DCELL *values; /* list of neighborhood values */ DCELL(*values_w)[2]; /* list of neighborhood values and weights */ G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("raster")); G_add_keyword(_("algebra")); G_add_keyword(_("statistics")); module->description = _("Makes each cell category value a " "function of the category values assigned to the cells " "around it, and stores new cell values in an output raster " "map layer."); parm.input = G_define_standard_option(G_OPT_R_INPUT); parm.selection = G_define_standard_option(G_OPT_R_INPUT); parm.selection->key = "selection"; parm.selection->required = NO; parm.selection->description = _("Name of an input raster map to select the cells which should be processed"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.method = G_define_option(); parm.method->key = "method"; parm.method->type = TYPE_STRING; parm.method->required = NO; parm.method->answer = "average"; p = G_malloc(1024); for (n = 0; menu[n].name; n++) { if (n) strcat(p, ","); else *p = 0; strcat(p, menu[n].name); } parm.method->options = p; parm.method->description = _("Neighborhood operation"); parm.method->guisection = _("Neighborhood"); parm.size = G_define_option(); parm.size->key = "size"; parm.size->type = TYPE_INTEGER; parm.size->required = NO; parm.size->description = _("Neighborhood size"); parm.size->answer = "3"; parm.size->guisection = _("Neighborhood"); parm.title = G_define_option(); parm.title->key = "title"; parm.title->key_desc = "phrase"; parm.title->type = TYPE_STRING; parm.title->required = NO; parm.title->description = _("Title of the output raster map"); parm.weight = G_define_standard_option(G_OPT_F_INPUT); parm.weight->key = "weight"; parm.weight->required = NO; parm.weight->description = _("Text file containing weights"); parm.gauss = G_define_option(); parm.gauss->key = "gauss"; parm.gauss->type = TYPE_DOUBLE; parm.gauss->required = NO; parm.gauss->description = _("Sigma (in cells) for Gaussian filter"); parm.quantile = G_define_option(); parm.quantile->key = "quantile"; parm.quantile->type = TYPE_DOUBLE; parm.quantile->required = NO; parm.quantile->description = _("Quantile to calculate for method=quantile"); parm.quantile->options = "0.0-1.0"; parm.quantile->answer = "0.5"; flag.align = G_define_flag(); flag.align->key = 'a'; flag.align->description = _("Do not align output with the input"); flag.circle = G_define_flag(); flag.circle->key = 'c'; flag.circle->description = _("Use circular neighborhood"); flag.circle->guisection = _("Neighborhood"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); sscanf(parm.size->answer, "%d", &ncb.nsize); if (ncb.nsize <= 0) G_fatal_error(_("Neighborhood size must be positive")); if (ncb.nsize % 2 == 0) G_fatal_error(_("Neighborhood size must be odd")); ncb.dist = ncb.nsize / 2; if (parm.weight->answer && flag.circle->answer) G_fatal_error(_("weight= and -c are mutually exclusive")); if (parm.weight->answer && parm.gauss->answer) G_fatal_error(_("weight= and gauss= are mutually exclusive")); ncb.oldcell = parm.input->answer; ncb.newcell = parm.output->answer; if (!flag.align->answer) { Rast_get_cellhd(ncb.oldcell, "", &cellhd); G_get_window(&window); Rast_align_window(&window, &cellhd); Rast_set_window(&window); } nrows = Rast_window_rows(); ncols = Rast_window_cols(); /* open raster maps */ in_fd = Rast_open_old(ncb.oldcell, ""); map_type = Rast_get_map_type(in_fd); /* get the method */ for (method = 0; (p = menu[method].name); method++) if ((strcmp(p, parm.method->answer) == 0)) break; if (!p) { G_warning(_("<%s=%s> unknown %s"), parm.method->key, parm.method->answer, parm.method->key); G_usage(); exit(EXIT_FAILURE); } if (menu[method].method == c_quant) { quantile = atoi(parm.quantile->answer); closure = &quantile; } half = (map_type == CELL_TYPE) ? menu[method].half : 0; /* establish the newvalue routine */ newvalue = menu[method].method; newvalue_w = menu[method].method_w; /* copy color table? */ copycolr = menu[method].copycolr; if (copycolr) { G_suppress_warnings(1); copycolr = (Rast_read_colors(ncb.oldcell, "", &colr) > 0); G_suppress_warnings(0); } /* read the weights */ if (parm.weight->answer) { read_weights(parm.weight->answer); if (!newvalue_w) weights_mask(); } else if (parm.gauss->answer) { if (!newvalue_w) G_fatal_error(_("Method %s not compatible with Gaussian filter"), parm.method->answer); gaussian_weights(atof(parm.gauss->answer)); } else newvalue_w = NULL; /* allocate the cell buffers */ allocate_bufs(); result = Rast_allocate_d_buf(); /* get title, initialize the category and stat info */ if (parm.title->answer) strcpy(ncb.title, parm.title->answer); else sprintf(ncb.title, "%dx%d neighborhood: %s of %s", ncb.nsize, ncb.nsize, menu[method].name, ncb.oldcell); /* initialize the cell bufs with 'dist' rows of the old cellfile */ readrow = 0; for (row = 0; row < ncb.dist; row++) readcell(in_fd, readrow++, nrows, ncols); /* open the selection raster map */ if (parm.selection->answer) { G_message(_("Opening selection map <%s>"), parm.selection->answer); selection_fd = Rast_open_old(parm.selection->answer, ""); selection = Rast_allocate_null_buf(); } else { selection_fd = -1; selection = NULL; } /*open the new raster map */ out_fd = Rast_open_new(ncb.newcell, map_type); if (flag.circle->answer) circle_mask(); if (newvalue_w) values_w = (DCELL(*)[2]) G_malloc(ncb.nsize * ncb.nsize * 2 * sizeof(DCELL)); else values = (DCELL *) G_malloc(ncb.nsize * ncb.nsize * sizeof(DCELL)); for (row = 0; row < nrows; row++) { G_percent(row, nrows, 2); readcell(in_fd, readrow++, nrows, ncols); if (selection) Rast_get_null_value_row(selection_fd, selection, row); for (col = 0; col < ncols; col++) { DCELL *rp = &result[col]; if (selection && selection[col]) { *rp = ncb.buf[ncb.dist][col]; continue; } if (newvalue_w) n = gather_w(values_w, col); else n = gather(values, col); if (n < 0) Rast_set_d_null_value(rp, 1); else { if (newvalue_w) newvalue_w(rp, values_w, n, closure); else newvalue(rp, values, n, closure); if (half && !Rast_is_d_null_value(rp)) *rp += 0.5; } } Rast_put_d_row(out_fd, result); } G_percent(row, nrows, 2); Rast_close(out_fd); Rast_close(in_fd); if (selection) Rast_close(selection_fd); /* put out category info */ null_cats(); if ((cat_names = menu[method].cat_names)) cat_names(); Rast_write_cats(ncb.newcell, &ncb.cats); if (copycolr) Rast_write_colors(ncb.newcell, G_mapset(), &colr); Rast_short_history(ncb.newcell, "raster", &history); Rast_command_history(&history); Rast_write_history(ncb.newcell, &history); exit(EXIT_SUCCESS); }
void genotype::set_weights (string weightfile){ givenweights = true; read_weights (weightfile); }