int main(int argc, char *argv[0]) { if(argc < 2 || argc > 4) { printf("Usage: xcsf inputfile [MaxTrials] [NumExp]\n"); exit(EXIT_FAILURE); } // initialise environment constants_init(argc, argv); random_init(); func_init(argv[1]); gen_outfname(argv[1]); // run experiments double err[PERF_AVG_TRIALS]; double terr[PERF_AVG_TRIALS]; for(int e = 1; e < NUM_EXPERIMENTS+1; e++) { printf("\nExperiment: %d\n", e); pop_init(); outfile_init(e); // each trial in an experiment for(int cnt = 0; cnt < MAX_TRIALS; cnt++) { trial(cnt, true, err); // train trial(cnt, false, terr);// test // display performance if(cnt%PERF_AVG_TRIALS == 0 && cnt > 0) disp_perf(err, terr, cnt, pop_num); } // clean up set_kill(&pset); outfile_close(); } func_free(); return EXIT_SUCCESS; }
int do_pop_auth(int channel) { pop_init(); if (pop_reply_ok()) return 1; pop_user = NULL; if (pop_handle_state(pop_auth_commands) == POP_STATE) { pop_clean(); write_loop(channel, (char *)&pop_buffer, sizeof(pop_buffer)); write_loop(channel, pop_user, strlen(pop_user) + 1); write_loop(channel, pop_pass, strlen(pop_pass) + 1); memset(pop_pass, 0, strlen(pop_pass)); if (close(channel)) return 1; } return 0; }
void sat_simulated_evolution(sat_t *sat, uint32_t pop_size) { population_t *pop = NULL; population_t *pop_next = NULL; population_t *pop_swap = NULL; pop = pop_init(sat, pop_size); pop_next = pop_init(sat, pop_size); uint32_t ic = sat->vars_cnt; uint32_t favg; /* average fitness */ ind_t *ind_best = ind_init(sat); for (uint32_t gen = 0; gen < 10000; gen++) { for (uint32_t i = 0; i < pop->inds_cnt; i += 2) { memcpy(pop_next->inds[i + 0]->ch, ga_select(pop)->ch, sizeof(bool) * ic); memcpy(pop_next->inds[i + 1]->ch, ga_select(pop)->ch, sizeof(bool) * ic); ga_cross_2p(pop_next->inds[i + 0]->ch, pop_next->inds[i + 1]->ch, ic); ga_mutate(pop_next->inds[i + 0]->ch, ic); ga_mutate(pop_next->inds[i + 1]->ch, ic); ind_compute_fitness(pop_next->inds[i + 0]); ind_compute_fitness(pop_next->inds[i + 1]); } pop_swap = pop; pop = pop_next; pop_next = pop_swap; qsort(pop->inds, pop->inds_cnt, sizeof(ind_t **), ga_fitness_cmp); /* compute average fitness */ favg = 0; for (uint32_t i = 0; i < pop->inds_cnt; i++) { favg += pop->inds[i]->fitness; } favg /= pop->inds_cnt; // fprintf(stdout, "gen=%u avg=%u best ind:", gen, favg); // ind_print(pop->inds[0]); if (ind_best->fitness < pop->inds[0]->fitness) { memcpy(ind_best->ch, pop->inds[0]->ch, sizeof(bool) * ic); } } fprintf(stdout, "total best: %u\n", ind_best->fitness); // ind_print(ind_best); // printf("best ind: "); // ind_print(pop->inds[0]); ind_free(ind_best); population_free(pop); population_free(pop_next); }
/* * popper: Handle a Post Office Protocol version 3 session */ int main (int argc, char **argv) { POP p; state_table * s; char message[MAXLINELEN]; signal(SIGHUP, catchSIGHUP); signal(SIGPIPE, catchSIGHUP); /* Start things rolling */ pop_init(&p,argc,argv); /* Tell the user that we are listenting */ pop_msg(&p,POP_SUCCESS, "POP3 server ready"); /* State loop. The POP server is always in a particular state in which a specific suite of commands can be executed. The following loop reads a line from the client, gets the command, and processes it in the current context (if allowed) or rejects it. This continues until the client quits or an error occurs. */ for (p.CurrentState=auth1;p.CurrentState!=halt&&p.CurrentState!=error;) { if (hangup) { pop_msg(&p, POP_FAILURE, "POP hangup: %s", p.myhost); if (p.CurrentState > auth2 && !pop_updt(&p)) pop_msg(&p, POP_FAILURE, "POP mailbox update failed: %s", p.myhost); p.CurrentState = error; } else if (tgets(message, MAXLINELEN, p.input, pop_timeout) == NULL) { pop_msg(&p, POP_FAILURE, "POP timeout: %s", p.myhost); if (p.CurrentState > auth2 && !pop_updt(&p)) pop_msg(&p,POP_FAILURE, "POP mailbox update failed: %s", p.myhost); p.CurrentState = error; } else { /* Search for the command in the command/state table */ if ((s = pop_get_command(&p,message)) == NULL) continue; /* Call the function associated with this command in the current state */ if (s->function) p.CurrentState = s->result[(*s->function)(&p)]; /* Otherwise assume NOOP and send an OK message to the client */ else { p.CurrentState = s->success_state; pop_msg(&p,POP_SUCCESS,"time passes"); } } } /* Say goodbye to the client */ pop_msg(&p,POP_SUCCESS,"Pop server at %s signing off.",p.myhost); /* Log the end of activity */ pop_log(&p,POP_PRIORITY, "(v%s) Ending request from \"%s\" at %s\n",VERSION,p.client,p.ipaddr); /* Stop logging */ closelog(); return(0); }
int main(int argc, char *argv[]) { int i, g; /* generation and parent counters */ int parent1, parent2; int gop; fastf_t total_fitness = 0.0f; struct fitness_state fstate; struct population pop = {NULL, NULL, NULL, NULL, NULL, 0}; char dbname[256] = {0}; struct beset_options opts = {DEFAULT_POP_SIZE, DEFAULT_GENS, DEFAULT_RES, 0, 0, 0, 0}; struct individual *tmp = NULL; int ac; struct db_i *source_db; ac = parse_args(argc, argv, &opts); if (argc - ac != 3) usage(); /* read source model into fstate.rays */ fit_prep(&fstate, opts.res, opts.res); fit_store(argv[ac+2], argv[ac+1], &fstate); /* initialize population and spawn initial individuals */ pop_init(&pop, opts.pop_size); pop_spawn(&pop); source_db = db_open(argv[ac+1], DB_OPEN_READWRITE); db_dirbuild(source_db); pop.db_c = db_create("testdb", 5); db_close(pop.db_c); for (g = 1; g < opts.gens; g++ ) { #ifdef VERBOSE printf("\nGeneration %d:\n" "--------------\n", g); #endif total_fitness = 0.0f; snprintf(dbname, 256, "gen%.3d", g); pop.db_c = db_create(dbname, 5); pop_gop(REPRODUCE, argv[ac+2], NULL, argv[ac+2], NULL, source_db, pop.db_c, &rt_uniresource); /* calculate sum of all fitnesses and find * the most fit individual in the population * note: need to calculate outside of main pop * loop because it's needed for pop_wrand_ind()*/ for (i = 0; i < pop.size; i++) { fit_diff(NL(pop.parent[i].id), pop.db_p, &fstate); pop.parent[i].fitness = fstate.fitness; total_fitness += FITNESS; } /* sort population - used for keeping top N and dropping bottom M */ bu_sort((void *)pop.parent, pop.size, sizeof(struct individual), cmp_ind, NULL); /* remove lower M of individuals */ for (i = 0; i < opts.kill_lower; i++) { total_fitness -= pop.parent[i].fitness; } printf("Most fit from %s was %s with a fitness of %g\n", dbname, NL(pop.parent[pop.size-1].id), pop.parent[pop.size-1].fitness); printf("%6.8g\t%6.8g\t%6.8g\n", total_fitness/pop.size, pop.parent[0].fitness, pop.parent[pop.size-1].fitness); for (i = 0; i < pop.size; i++) { pop.child[i].id = i; /* keep upper N */ if (i >= pop.size - opts.keep_upper) { pop_gop(REPRODUCE, NL(pop.parent[i].id), NULL, NL(pop.child[i].id), NULL, pop.db_p, pop.db_c, &rt_uniresource); continue; } /* Choose a random genetic operation and * a parent which the op will be performed on*/ gop = pop_wrand_gop(); parent1 = pop_wrand_ind(pop.parent, pop.size, total_fitness, opts.kill_lower); /* only need 1 more individual, can't crossover, so reproduce */ if (gop == CROSSOVER && i >= pop.size-opts.keep_upper-1)gop=REPRODUCE; if (gop & (REPRODUCE | MUTATE)) { #ifdef VERBOSE printf("r(%s)\t ---------------> (%s)\n", NL(pop.parent[parent1].id), NL(pop.child[i].id)); #endif /* perform the genetic operation and output the child to the child database */ pop_gop(gop, NL(pop.parent[parent1].id), NULL, NL(pop.child[i].id), NULL, pop.db_p, pop.db_c, &rt_uniresource); } else { /* If we're performing crossover, we need a second parent */ parent2 = pop_wrand_ind(pop.parent, pop.size, total_fitness, opts.kill_lower); ++i; pop.child[i].id = i; #ifdef VERBOSE printf("x(%s, %s) --> (%s, %s)\n", NL(pop.parent[parent1].id), NL(pop.parent[parent2].id), pop.child[i-1].id, pop.child[i].id); #endif /* perform the genetic operation and output the children to the child database */ pop_gop(gop, NL(pop.parent[parent1].id), NL(pop.parent[parent2].id), NL(pop.child[i-1].id), NL(pop.child[i].id), pop.db_p, pop.db_c, &rt_uniresource); } } /* Close parent db and move children * to parent database and population * Note: pop size is constant so we * can keep the storage from the previous * pop.parent for the next pop.child*/ db_close(pop.db_p); pop.db_p = pop.db_c; tmp = pop.child; pop.child = pop.parent; pop.parent = tmp; } db_close(pop.db_p); #ifdef VERBOSE printf("\nFINAL POPULATION\n" "----------------\n"); for (i = 0; i < pop.size; i++) printf("%s\tf:%.5g\n", NL(pop.child[i].id), pop.child[i].fitness); #endif pop_clean(&pop); fit_clean(&fstate); return 0; }