public GAIndividual best_ind; // best individual of this evolution public GAEvolve(int generations, int pop_size, int nvert, int xrate, int mrate) { // xrate: cross-over rate, the initial value // mrate: mutation rate, the initial value // xrate and mrate may be adapted over generations // nvert: number of graph vertices best_fitness = new float[generations]; worst_fitness = new float[generations]; avr_fitness = new float[generations]; GAPopulation gap = new GAPopulation(pop_size, nvert); best_fitness[0] = gap.ind[gap.best_index].fitness; worst_fitness[0] = gap.ind[gap.worst_index].fitness; avr_fitness[0] = gap.avr_fitness; for (int i = 1; i < generations; i++) { gap = GAPopulation.generate(gap, xrate, mrate); best_fitness[i] = gap.ind[gap.best_index].fitness; worst_fitness[i] = gap.ind[gap.worst_index].fitness; avr_fitness[i] = gap.avr_fitness; } best_ind = gap.ind[gap.best_index]; }
public static void Main() { GAIndividual.adj_matrix = GAUtils.readMatrix(@"d:\graph.txt"); int nvert = GAIndividual.adj_matrix.Length; // number of vertices GAPopulation gap = new GAPopulation(100, nvert); for (int i = 0; i < 100; i++) { Console.WriteLine(gap.ind[gap.best_index]); gap = GAPopulation.generate(gap, 60, 30); } Console.WriteLine(gap.ind[gap.best_index]); }
public static GAPopulation generate(GAPopulation p, int xrate, int mrate) { // Generate a new population from p, xrate percent of the induviduals // of new population are generated by cross-over, mrate percent of them are // generated by mutation, and others by reproduction. if (xrate < 0 || xrate > 100 || mrate < 0 || mrate > 100 || xrate + mrate > 100) { Console.Error.WriteLine("error: xrate and/or mrate are not properly set"); } GAIndividual[] newg = new GAIndividual[p.pop_size]; int newg_index = 0; int xn = xrate * p.pop_size / 100; // xn: number of offsprings to be produced by xover int mn = mrate * p.pop_size / 100; // mn: number of offsprings to be produced by mutation // xover: for (int i = 0; i < xn; i++) { // select two parents for cross-over: // we want two distinct parents (i.e. p1 != p2) int p1, p2; do { p1 = p.tr_select(); p2 = p.tr_select(); }while (p1 == p2); newg[newg_index++] = GAIndividual.xover1p(p.ind[p1], p.ind[p2]); } // mutation: for (int i = 0; i < mn; i++) { newg[newg_index++] = p.ind[p.tr_select()].mutate(); } // reproduction: for (int i = newg_index; i < p.pop_size; i++) { newg[i] = p.ind[p.tr_select()]; } return(new GAPopulation(newg)); }
void Awake() { InfluenceMap.getInstance().center = new Vector2(center.x, center.z); InfluenceMap.getInstance().DefaultY = center.y; InfluenceMap.getInstance().height = height; InfluenceMap.getInstance().width = width; InfluenceMap.getInstance().htileCount = h_Tile; InfluenceMap.getInstance().wtileCount = w_Tile; InfluenceMap.getInstance().mPlane = mPlane; InfluenceMap.getInstance().Init(); GAPopulation.GlobalInit(); GAPopulationManager.getInstance().Init(); Type type = Type.GetType(DemoWorldName); aiWorld = (AIWorld)type.Assembly.CreateInstance(DemoWorldName); aiWorld.Init(); }