示例#1
0
int strategy_fini(void) {
  int retval = 0;
  int i;

  for (i = 0; i < strat_count; i++) {
    // finalize all strategies; if there are any errors, we'll let the
    // last session_error call govern (since there is no way to retrieve
    // its value).
    if (strategies[i].fini)
      retval |= (strategies[i].fini)();
  }
  if (retval)
    retval = -1;
  free_strategies();

  // other things to free
  free(window);
  free(best_per_strategy);
  free(best_perf_per_strategy);

  return retval;
}
示例#2
0
/**
   Free all memory allocated by the program.
   This mainly means we have to free a lot of strings
   and GArrays. */
void
free_memory(void)
{
#ifdef DEBUG
    printf("free_memory\n");
#endif

    free_variables();
    free_names(FALSE);
    free_transfer_list();
    free_strategies();
    free_country(&country, FALSE);
    free_users(FALSE);
    free_bets(FALSE);
    free_lg_commentary(FALSE);
    free_news(FALSE);
    free_newspaper(FALSE);
    free_support_dirs();
    free_jobs(FALSE);

    free_g_array(&live_games);
}
示例#3
0
int strategy_init(hsignature_t *sig) {
  int retval;

  best = HPOINT_INITIALIZER;
  best_perf = INFINITY;

  // Load constants for sliding window and tradeoff constant
  const char *window_str = session_getcfg(WINDOW_SIZE_PARAM);
  if (!window_str)
    window_size = DEFAULT_WINDOW_SIZE;
  else {
    window_size = atoi(window_str);
    if (window_size <= 0) {
      session_error("Invalid " WINDOW_SIZE_PARAM);
      return -1;
    }
  }

  const char *tradeoff_str = session_getcfg(TRADEOFF_PARAM);
  if (!tradeoff_str)
    tradeoff = DEFAULT_TRADEOFF;
  else {
    tradeoff = atof(tradeoff_str);
    if (tradeoff <= 0.0) {
      session_error("Invalid " TRADEOFF_PARAM);
      return -1;
    }
  }
  explore_factor = tradeoff * sqrt(2.0 * log2((double) window_size));
  
  // Load the sub-strategies and run their init's in order
  const char *strats = session_getcfg(STRATEGIES_PARAM);
  if (!strats) {
    session_error("No strategy list provided in " STRATEGIES_PARAM);
    return -1;
  }

  retval = parse_strategies(strats);
  if (retval < 0) {
    session_error(errmsg 
		  ? errmsg 
		  : "Error parsing strategy list (allocation filed?)");
    return -1;
  }
  if (strat_count > MAX_NUM_STRATEGIES) {
    session_error("Too many strategies specified.");
    free_strategies();
    return -1;
  }

  best_per_strategy = (hpoint_t *) malloc(strat_count * sizeof(hpoint_t));
  best_perf_per_strategy = (double *) malloc(strat_count * sizeof(double));
  if (best_per_strategy == NULL
      || best_perf_per_strategy == NULL) {
    session_error("Couldn't allocate memory for control structures.");
    free_strategies();
    free(best_per_strategy);
    free(best_perf_per_strategy);
    return -1;
  }

  // code above this should be run once; below this should be run on
  // every restart---unless we want to allow window size and tradeoff
  // to change on restart?

  if (init_sliding_window() < 0) {
    free_strategies();
    free(best_per_strategy);
    free(best_perf_per_strategy);
    return -1;
  }

  int i;
  for (i = 0; i < strat_count; i++) {
    hook_init_t init = strategies[i].init;
    if (init) {
      retval = (strategies[i].init)(sig);
      if (retval < 0)
	// sub-strategy should have called session_error
	return retval;
    }
  }

  // TODO I have not handled restarts; need to understand that better.

  return retval;
}