Ejemplo n.º 1
0
static void on_save_button(GtkWidget *button, RendererLaser *self)
{
  GtkWidget *dialog;
  dialog = gtk_file_chooser_dialog_new("Save Data File", NULL, GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL,
      GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
  g_assert(dialog);
  gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), TRUE);
  if (self->last_save_filename)
    gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), self->last_save_filename);

  if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
    char *filename;
    filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
    if (NULL != filename) {
      FILE *file = fopen(filename, "w");
      if (NULL != file) {
        /* store selected file into self->last_save_filename */
        if (self->last_save_filename)
          g_free(self->last_save_filename);
        self->last_save_filename = g_strdup(filename);

        int count = save_points_to_file(self, file);

        DBG("Wrote %d points to file '%s'.\n", count, filename);
        fclose(file);
      }
      else {
        ERR("Error: Failed to open file: '%s', error is: '%s'\n",
            filename, g_strerror(errno));
      }
      g_free(filename);
    }
  }
  gtk_widget_destroy(dialog);
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
  ih_config_options_t *options;
  ih_core_objects_t objects;
  ih_audit_log_t *log;
  char *points_filename;
  ih_search_system_t *system;
  ih_container_array_t *initial_solutions;
  unsigned short initial_solution_count;
  ih_container_array_t *solutions;
  unsigned short solution_count;
  ih_core_bool_t still_searching;
  unsigned short i;
  ih_core_bitarray_t *solution;
  double score;
  double best_score;
  stravel_t stravel;
  double goal_distance;
  double sqrt_point_count;
  struct timeval start_time;
  char *search_algorithm_string;
  ih_search_algorithm_t search_algorithm;

  initial_solutions = NULL;
  initial_solution_count = 0;

  ih_core_objects_init(&objects);

  log = ih_audit_log_create(stdout);
  if (!log) {
    ih_core_trace_exit("ih_audit_log_create");
  }
  stravel.log = log;

  options = ih_config_options_create(argc, argv, &objects);
  if (!options) {
    ih_audit_log_trace_exit(log, "stvl", "ih_config_options_create");
  }

  if (ih_config_options_find_as_string(options, "points_filename",
          &points_filename, "")) {
    if (!load_points_from_file(&stravel, points_filename)) {
      ih_audit_log_trace_exit(log, "stvl", "load_points_from_file");
    }
  } else {
    ih_audit_log_enter
      (log, "stvl", "creating %lu random points", RANDOM_POINT_COUNT);
    create_random_points(&stravel);
  }

  if (ih_config_options_find_as_string(options, "search_algorithm",
          &search_algorithm_string, "")) {
    if (!ih_search_algorithm_get_from_string
        (search_algorithm_string, &search_algorithm)) {
      search_algorithm = DEFAULT_SEARCH_ALGORITHM;
    }
  } else {
    search_algorithm = DEFAULT_SEARCH_ALGORITHM;
  }
  search_algorithm_string = ih_search_algorithm_get_string(search_algorithm);
  ih_audit_log_enter(log, "stvl", "search algorithm: %s",
      search_algorithm_string);

  ih_audit_log_enter(log, "stvl", "saving points to stravel_points.csv");
  if (!save_points_to_file(&stravel, POINTS_FILENAME)) {
    ih_audit_log_trace_exit(log, "stvl", "save_points_to_file");
  }

  sqrt_point_count = sqrt(stravel.point_count);
  goal_distance = ((POINT_RANGE * sqrt_point_count) + POINT_RANGE) * 1.0;

  ih_audit_log_enter(log, "stvl", "caching point distances");
  cache_distances(&stravel);

  ih_audit_log_enter(log, "stvl", "creating search system");
  system = ih_search_system_create(score_solution, IH_CORE_GOAL_MINIMIZE_SCORE,
      &stravel, initial_solutions, search_algorithm, log);
  if (system) {
    ih_audit_log_enter(log, "stvl", "goal distance: %.2f", goal_distance);
    gettimeofday(&start_time, NULL);
    still_searching = ih_core_bool_true;
    while (still_searching) {
      ih_search_system_search(system, MAX_SEARCH_TIME_MICROSECONDS);
      solutions = ih_search_system_get_solutions_copy(system, 1);
      if (solutions) {
        solution = ih_container_array_find(solutions, 0);
        if (!score_solution(&stravel, solution, &best_score)) {
          ih_audit_log_trace_exit(log, "stvl", "score_solution");
        }
        solution_count = ih_container_array_get_size(solutions);
        for (i = 0; i < solution_count; i++) {
          solution = ih_container_array_find(solutions, i);
          if (!score_solution(&stravel, solution, &score)) {
            ih_audit_log_trace_exit(log, "stvl", "score_solution");
          }
          if (ih_core_bool_true) {
            ih_audit_log_enter(log, "stvl", "distance: %.2f", score);
          }
          fflush(stdout);
          if (score <= goal_distance) {
            still_searching = ih_core_bool_false;
            ih_audit_log_enter
              (log, "stvl", "reached goal distance, stopping search");
          }
        }
        ih_container_array_destroy(solutions);
      } else {
        ih_audit_log_trace_exit
          (log, "stvl", "ih_search_system_get_solutions_copy");
        break;
      }
      if (!ih_core_time_is_remaining_microseconds
          (&start_time, MAX_OVERALL_TIME_MICROSECONDS)) {
        still_searching = ih_core_bool_false;
        ih_audit_log_enter
          (log, "stvl", "max search time elapsed, stopping search");
      }
    }

    solutions = ih_search_system_get_solutions_copy(system, 1);
    if (solutions) {
      solution = ih_container_array_find(solutions, 0);
      ih_audit_log_enter
        (log, "stvl", "saving solution to %s", SOLUTION_FILENAME);
      if (!save_solution_to_file(&stravel, solution, SOLUTION_FILENAME)) {
        ih_audit_log_trace_exit(log, "stvl", "save_solution_to_file");
      }
      ih_container_array_destroy(solutions);
    } else {
      ih_audit_log_trace_exit
        (log, "stvl", "ih_search_system_get_solutions_copy");
    }

    ih_search_system_destroy(system);
  } else {
    ih_audit_log_trace_exit(log, "stvl", "ih_search_system_create");
  }

  ih_config_options_destroy(options);
  ih_audit_log_destroy(log);

  return 0;
}