Exemplo n.º 1
0
void listen(Vector* threads) {

    union MapMessage msg;
    struct Message outMsg;

    mprintf("Starting listen loop\n");
    while (comm_airline_recieve(&msg) == 0) {
        mprintf("Got message with type %d\n", msg.type);
        switch (msg.type) {
            case MessageTypeStep:
                stage = 0;
                outMsg.type = MessageTypeStep;
                start_phase(threads, outMsg);
                break;
            case MessageTypeContinue:
                stage = 1;
                outMsg.type = MessageTypeContinue;
                start_phase(threads, outMsg);
                break;
            case MessageTypeDestinations:
                redirect_destinations_message(threads, &msg);
                break;
            case MessageTypeUnloadStock:
                redirect_stock_message(threads, &msg);
                break;
            default:
                if (msg.type >= MessageTypeLast) {
                    print_error("Got invalid message type on airline listen\n");
                }
                return;
        }
        mprintf("Done handling. Waiting for next msg\n");
    }
}
Exemplo n.º 2
0
void
gsm_session_start (GsmSession *session)
{
  session->phase = GSM_SESSION_PHASE_INITIALIZATION;

  start_phase (session);
}
Exemplo n.º 3
0
static void
end_phase (GsmSession *session)
{
  g_slist_free (session->pending_apps);
  session->pending_apps = NULL;

  g_debug ("ending phase %d\n", session->phase);

  session->phase++;

  if (session->phase < GSM_SESSION_PHASE_RUNNING)
    start_phase (session);
}
Exemplo n.º 4
0
int run_alps(int *steps)
{
  int i,j,k,p;
  double temp_fitness[FITNESS_COUNT];
  double temp_gene[GENE_COUNT];
  int pareto_front[POP];
  int pareto_count;  

  begin = time(NULL);
  init_population();            // Initialise population.
  t = 0;
  goal_indiv = -1;

  mprintf(1, "preamble -> {expName -> %s, phaseCount -> %d, lobotomise -> %s, task -> %d, \n", exp_name, phase_count, lobotomise ? "True" : "False", task_index + 1);
  mprintf(1, "\ttmax -> %.2lf, fitnessType -> %d, runType -> %d, randomSeed -> %ld }\n", TIME_MAX, fitness_type, run_type, random_seed);

  mprintf(1, "alpsParams -> { layerCount -> %d, popPerLayer -> %d, "
          "popCount -> %d, mutProbability -> %.3f, maxSeconds -> %d }\n",
          LAYER_COUNT, POP_PER_LAYER, POP, mut_prob, MAX_SECONDS);
  fflush(stdout);

  for (p = 0, phase = 1; 
       p < phase_count && elapsed_seconds() < MAX_SECONDS; 
       p++, phase = p + 1) {

    if (p != 0) end_phase(p);
    start_phase(phase);
    for (i = 0; i < POP; i++) {
      // Evaluate every gene.
      evaluate(genes[i], fitness_matrix + FITNESS_INDEX(i));
    }
    int met_goal = 0;
    for (j = 0; j < POP; j++) {
      if (is_goal_fitness(fitness_matrix + FITNESS_INDEX(j))) {
        met_goal = 1;
        goal_indiv = j;
      }
    }
    if (met_goal) 
      continue;
    
    for (; t < MAX_OPT_STEPS && elapsed_seconds() < MAX_SECONDS; t++) { 
      // Evaluate the pareto front for each layer.
      for (k = 0; k < LAYER_COUNT; k++) 
        pareto_front_rowmajor(pareto_front + k * POP_PER_LAYER, 
                              fitness_matrix 
                              + k * FITNESS_COUNT * POP_PER_LAYER,
                              POP_PER_LAYER,
                              FITNESS_COUNT);

      // Grab a non-dominated individual.
      int a;
      // O(n) single pass to count and grab a random individual
      // that's on the pareto front.
      pareto_count=0;
      for (i = 0; i < POP; i++) 
        if (pareto_front[i] && (rand() < 1./(double)++pareto_count))
            a = i;
      k = LAYER_OF_INDIV(a);

      if (t % DISPLAY_FREQ == 0) {
        int n = FITNESS_INDEX(a);
        if (! quiet)
        printf("t = %5d, pi = %3d, a = %2d, k = %2d, N(PF) = %2d, f(a) = {%f, %f}, "
               "efail = %3d, esucc = %5d, secs = %4ld\n", t, a, ages[a], k, 
               pareto_count, fitness_matrix[n], fitness_matrix[n + 1], 
               eval_fail_count, eval_succ_count, elapsed_seconds());

        fflush(stdout);
      }

      if (t % reset_freq == 0) {
        // Reset the bottom layer.
        for (i = 0; i < POP_PER_LAYER; i++) {
          // Try to dislogde in the layer above if it's in the pareto front.
          if (pareto_front[i])
            try_dislodge(genes[i], fitness_matrix + FITNESS_LINDEX(0, i), ages[i], 1);

          init_gene(genes[i]);
          ages[i] = 0;
        }
        for (i = 0; i < POP_PER_LAYER; i++)
          evaluate(genes[i], fitness_matrix + FITNESS_INDEX(i));
        pareto_front_rowmajor(pareto_front, fitness_matrix, POP_PER_LAYER,
                              FITNESS_COUNT);
      }

      copy(genes[a], temp_gene);
      mutate(temp_gene);
      int age = t/POP;
      evaluate(temp_gene, temp_fitness);
      int new_i = try_dislodge(temp_gene, temp_fitness, age, k);
      if (is_goal_fitness(temp_fitness)) {
        if (new_i < 0) {
          printf("warning: goal individual not able to dislodge anyone in layer %d and up.\n", k);
        }
        goal_indiv = new_i;
        break;                  /* Goto next phase */
      }
    }
  }
  if (t == MAX_OPT_STEPS || elapsed_seconds() > MAX_SECONDS) 
    alps_status = ALPS_FAIL;
  else
    alps_status = ALPS_SUCC;

  end_phase(phase - 1);
  if (steps)
    *steps = t;

  return alps_status;
}