Example #1
0
/*
 * Try and read in the file from path as if it was a grid.
 *
 * Will exit with appropriate status if the file isn't actually a valid grid
 * for this game.
 *
 * On return g should be ready to play games with.
 */
void
read_grid_file(char *path, struct game *g)
{
    int n = 0;
    FILE *f;

    if ((f = fopen(path, "r")) == NULL) {
        fprintf(stderr, "Invalid grid file\n");
        exit(4);
    }
    
    if ((g->current_player = read_num_until(f, '\n', g) - 1) < 0) {
        fprintf(stderr, "Error reading grid contents\n");
        exit(5);
    }


    while (n != 2 * g->height + 1) {
        read_edges(f, n++, g);
    }

    read_filled(f, g);

    if (fgetc(f) != EOF) {
        fprintf(stderr, "Error reading grid contents\n");
        exit(5);
    }

    fclose(f);
}
Example #2
0
int read_network(NETWORK *network, FILE *stream)
{
  fill_buffer(stream);
  create_network(network);
  get_degrees(network);
  read_edges(network);
  free_buffer();

  return 0;
}
Example #3
0
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);

}
Example #4
0
/*** MAIN FUNCTION ***/
int main (int argc, char **argv) {

  // process the command line
  process_args (argc, argv);

  determine_limits();

  // check if there is something wrong in the command
  if (n_nodes <= 0 || n_edges <= 0) {
    fprintf (stderr, "Incorrect or missing node numbers\n");
    exit (1);
  }

  fprintf (stderr, "allocating edges..\n");
  edges = (edge_t *) malloc(n_edges * sizeof(edge_t));
  read_edges();

  fprintf (stderr, "allocating nodes..\n");
  nodes = (node_t *) malloc(n_nodes * sizeof(node_t));
  memset (nodes, 0, n_nodes * sizeof(node_t));
  for (unsigned int i=0; i < n_nodes; i++) {
    nodes[i].old_values = (float *) malloc (n_classes * sizeof(float));
    nodes[i].new_values = (float *) malloc (n_classes * sizeof(float));
  }

  read_labels();

  for (unsigned int i=0; i < n_nodes; i++) {
    for (short j=0; j < n_classes; j++) {
      if (!nodes[i].labeled)
         nodes[i].old_values[j] = 1.0 / n_classes;
      nodes[i].new_values[j] = 0;
    }
  }

  // compute the influx (weighted degree) of each node
  for (unsigned int i=0; i < n_edges; i++) {
    nodes[edges[i].ids[0]].influx += edges[i].weight;
    nodes[edges[i].ids[1]].influx += edges[i].weight;
  }

  float max_error;

  do {
    max_error = propagate_labels();
    fprintf (stderr, "error: %f\n", max_error);
  } while (max_error > 0.0002);

  output_labels();

  return 0;
}
Example #5
0
bool read_data(char* inputFolder) {
	bool success;
	char* nodesPath = NULL;
	char* edgesPath = NULL;

	success = (concat_path(inputFolder, "nodes", &nodesPath) && concat_path(inputFolder, "edges", &edgesPath));

	if (success) {
		success = success && read_nodes(nodesPath);
		success = success && read_edges(edgesPath);
	}

	free(nodesPath);
	free(edgesPath);

	return success;
}
Example #6
0
File: main.c Project: 8/gpio-poll
/* main entry point */
int main(int argc, char *argv[])
{
  int ret;

  /* init the default settings */
  struct settings_t settings;
  settings.gpio_base  = 55;
  settings.gpio_count = MAX_GPIO_COUNT;
  settings.loop       = 0;
  settings.poll       = 0;
  settings.poll_timeout = -1;

  print_info();
  
  /* parse the cmdline arguments */
  handle_parameters(argc, argv, &settings);

  /* initialize the gpio file names */
  init_gpio_filenames(settings.gpio_base, settings.gpio_count, settings.gpio_filenames);

  /* read the gpios */
  read_gpios(settings.gpio_count, settings.gpio_states, settings.gpio_filenames);

  /* print the states of the gpios */
  printf("initial values:\n");
  print_gpios(settings.gpio_base, settings.gpio_count, settings.gpio_states);
  printf("\n");

  /* keep reading gpios cpu intensive in a tight loop */
  if (settings.loop)
    enter_read_gpios_loop(settings.gpio_base, settings.gpio_count, settings.gpio_states, settings.gpio_filenames);

  /* poll the gpios once */
  if (settings.poll)
  {
    init_gpio_edges(settings.gpio_base, settings.gpio_count, settings.gpio_edge_filenames);
    if (read_edges(settings.gpio_count, settings.gpio_edge_values, settings.gpio_edge_filenames))
      if (validate_edges(settings.gpio_base, settings.gpio_count, settings.gpio_edge_values))
        enter_poll_gpios(settings.gpio_base, settings.gpio_count, settings.poll_timeout, settings.gpio_states, settings.gpio_filenames);
  }
}
Example #7
0
// query4 $inputfile $datapath
int main(int argc, char *argv[]){
	
	// initialization
	input_file_name = argv[1];
	dir_name = argv[2];
	dir_name_len = (int)strlen(argv[2]);
	file_name_max_len = dir_name_len + 50;
	data_file_name = new char [file_name_max_len];
	
	cnt_data_lines("person.csv", &personcnt);
	cnt_data_lines("forum.csv", &forumcnt);
	cnt_data_lines("tag.csv", &tagcnt);
	person_in_induced_graph = new char[personcnt];

#if DEBUG
	printf("cnt done.\n");
#endif
	read_edges();
	convert2undirected();
#if DEBUG
	printf("start forums.\n");
#endif
	read_forums();
	read_tags();
	
	read_queries();
#if DEBUG
	printf("read Q done\n");
#endif
	
	// solve
	freopen("query4.out", "w", stdout);
	for (vector < pair<int, int> > :: iterator it=queries.begin(); it!=queries.end(); it++)
		query(it->first, it->second);
	fclose(stdout);
	
	free_memory();
	return 0;
}
Example #8
0
int main(int argc, char **argv){
    if( argc<6){
        if(argc>1) fprintf(stderr,"Error, not enough arguments\n");
        usage();
        exit(1);
    }

    // read arguments
    color_image_t *im1 = color_image_load(argv[1]);
    color_image_t *im2 = color_image_load(argv[2]);
    float_image edges = read_edges(argv[3], im1->width, im1->height);
    float_image matches = read_matches(argv[4]);
    const char *outputfile = argv[5];

    // prepare variables
    epic_params_t epic_params;
    epic_params_default(&epic_params);
    variational_params_t flow_params;
    variational_params_default(&flow_params);
    image_t *wx = image_new(im1->width, im1->height), *wy = image_new(im1->width, im1->height);
    
    // read optional arguments 
    #define isarg(key)  !strcmp(a,key)
    int current_arg = 6;
    while(current_arg < argc ){
        const char* a = argv[current_arg++];
        if( isarg("-h") || isarg("-help") ) 
            usage();
        else if( isarg("-nw") ) 
            strcpy(epic_params.method, "NW");
        else if( isarg("-p") || isarg("-prefnn") ) 
            epic_params.pref_nn = atoi(argv[current_arg++]);
        else if( isarg("-n") || isarg("-nn") ) 
            epic_params.nn = atoi(argv[current_arg++]); 
        else if( isarg("-k") ) 
            epic_params.coef_kernel = atof(argv[current_arg++]);
        else if( isarg("-i") || isarg("-iter") ) 
            flow_params.niter_outer = atoi(argv[current_arg++]); 
        else if( isarg("-a") || isarg("-alpha") ) 
            flow_params.alpha= atof(argv[current_arg++]);  
        else if( isarg("-g") || isarg("-gamma") ) 
            flow_params.gamma= atof(argv[current_arg++]);                                  
        else if( isarg("-d") || isarg("-delta") ) 
            flow_params.delta= atof(argv[current_arg++]);  
        else if( isarg("-s") || isarg("-sigma") ) 
            flow_params.sigma= atof(argv[current_arg++]); 
        else if( isarg("-sintel") ){ 
            epic_params.pref_nn= 25; 
            epic_params.nn= 160; 
            epic_params.coef_kernel = 1.1f; 
            flow_params.niter_outer = 5;
            flow_params.alpha = 1.0f;
            flow_params.gamma = 0.72f;
            flow_params.delta = 0.0f;
            flow_params.sigma = 1.1f;            
        }  
        else if( isarg("-kitti") ){ 
            epic_params.pref_nn= 25; 
            epic_params.nn= 160; 
            epic_params.coef_kernel = 1.1f;
            flow_params.niter_outer = 2;
            flow_params.alpha = 1.0f;
            flow_params.gamma = 0.77f;
            flow_params.delta = 0.0f;
            flow_params.sigma = 1.7f; 
        }
        else if( isarg("-middlebury") ){ 
            epic_params.pref_nn= 15; 
            epic_params.nn= 65; 
            epic_params.coef_kernel = 0.2f;       
            flow_params.niter_outer = 25;
            flow_params.alpha = 1.0f;
            flow_params.gamma = 0.72f;
            flow_params.delta = 0.0f;
            flow_params.sigma = 1.1f;  
        }
        else{
            fprintf(stderr, "unknown argument %s", a);
            usage();
            exit(1);
        }   
    }
    
    // compute interpolation and energy minimization
    color_image_t *imlab = rgb_to_lab(im1);
    epic(wx, wy, imlab, &matches, &edges, &epic_params, 1);
    // energy minimization
    variational(wx, wy, im1, im2, &flow_params);
    // write output file and free memory
    writeFlowFile(outputfile, wx, wy);
    
    color_image_delete(im1);
    color_image_delete(imlab);
    color_image_delete(im2);
    free(matches.pixels);
    free(edges.pixels);
    image_delete(wx);
    image_delete(wy);

    return 0;
}
Example #9
0
static void read_all_data(DATA **data, DATA *valdata, int n_vars) {
	int i;
	DATA *area;

	init_data_minmax();
	area = get_data_area();
	for (i = 0; i < n_vars; i++)  {
		if (get_mode() == STRATIFY)
			printlog("stratum # %d:\n", i + strata_min);
		printlog("data(%s): ", name_identifier(i));
		if (data[i]->id < 0) {
			message("data(%s) was not specified\n", name_identifier(i));
			ErrMsg(ER_SYNTAX, "data specification error");
		}
		read_gstat_data(data[i]);
		report_data(data[i]);
	} /* for i */

/*
 * what to do when area is specified, but no masks or data()?
 * default prediction to `area'. Create a valdata with one point at
 * centre of area (for select()); and centre area at (0,0,0)
 */
	if (area && get_n_masks() <= 0 && valdata->id == -1) {
		valdata->id = ID_OF_VALDATA;
		valdata->centre = area->centre = 1;
	}
/* 
 * read data() data:
 */
	if (valdata->id > -1) {
		setup_valdata_X(valdata);
		if (! valdata->centre)
			valdata = read_gstat_data(valdata);
	}
/*
 * read area, if existed
 */
	if (area != NULL && get_method() != POLY) {
		read_gstat_data(area);
		/* now, before centring area: */
		if (valdata->centre)
			valdata = get_area_centre(area, valdata);
		if (area->centre)
			centre_area(area);
		printlog("area:%s\n", area->centre ? " (centred around 0)" : "");
		report_data(area);
		if (DEBUG_DATA) 
			print_data_list(area);
	}
/*
 * read edges, if existed
 */
    if (get_n_edges() > 0) {
        read_edges();
        report_edges();
/*         setup_visibility_graph(); */
        
        /*setup_planar_subdivisions();*/
    }
/*
 * setup and report data
 */

	if (valdata->id > -1) {
		printlog("data():%s ",
			valdata->centre ? " [at area centre]" : "");
		report_data(valdata);
	}
	for (i = 0; i < n_vars; i++) 
		setup_data_minmax(data[i]);
	if (valdata->id > -1)
		setup_data_minmax(valdata);
	for (i = 0; i < n_vars; i++) 
		calc_polynomials(data[i]);
	if (valdata->id > -1)
		calc_polynomials(valdata);
	if (DEBUG_DATA) {
		for (i = 0; i < n_vars; i++) 
			print_data_list(data[i]);
		if (valdata->id > -1)
			print_data_list(valdata);
	}
}