public FixedGA(CAState archetype, int population_size, int period, CARuleSet rule_set, float outer_mutation_rate, float inner_mutation_rate, float cull_rate, int max_iterations) { this.archetype = archetype; this.period = period; this.rule_set = rule_set; this.outer_mutation_rate = outer_mutation_rate; this.inner_mutation_rate = inner_mutation_rate; this.cull_rate = cull_rate; this.max_iterations = max_iterations; population = new CAState[population_size]; n_cull = (int)(cull_rate * population.Length); fitness_weights = new float[period]; float fwt = 0.0f; for (int i = 0; i < fitness_weights.Length; ++i) { float t = (float)(i + 1) / (fitness_weights.Length); fitness_weights[i] = 0.75f - (float)(Math.Sin(10 * t / Math.PI)); fwt += Math.Abs(fitness_weights[i]); } for (int i = 0; i < fitness_weights.Length; ++i) { fitness_weights[i] /= fwt; } }
public static void printOscillator(CAState c, CARuleSet rule_set) { CAState temp0 = new CAState(c); CAState temp1 = new CAState(c); while (true) { Console.WriteLine(temp0.ToString()); Console.WriteLine(); rule_set.getTransition(temp0, temp1); if (temp1.getRelativeDifference(c) == 0) { break; } temp0.copyFrom(temp1); } }
public static void saveImages(CAState c, CARuleSet rule_set, String path) { CAState temp0 = new CAState(c); CAState temp1 = new CAState(c); int i = 0; while (true) { Bitmap image = temp0.toImage(c.Width * 100, c.Height * 100); image.Save(path + "/" + i + ".bmp"); rule_set.getTransition(temp0, temp1); if (temp1.getRelativeDifference(c) == 0) { break; } temp0.copyFrom(temp1); ++i; } }
static void Main() { //CAState.Width = 10; //CAState.Height = 10; //CAState.Wrap_Mode = CAState.WrapMode.Extend; //int population_size = 200; //int period = 2; //CARuleSet rule_set = new CARuleSet(); //float mutation_rate = 0.01f; //float cull_rate = 0.5f; //float crossover_rate = 1.0f / (CAState.Width * CAState.Height); //int max_iterations = 10000; //int seed = 1102260512; //(int)(DateTime.Now.Ticks); CAState archetype = new CAState(12, 12, CAState.WrapMode.Extend); int population_size = 100; int period = 2; CARuleSet rule_set = new CARuleSet(); float outer_mutation_rate = 0.5f; float inner_mutation_rate = 0.7f; float cull_rate = 0.25f; int max_iterations = 10000; int seed = (int)(DateTime.Now.Ticks); FixedGA ga = new FixedGA(archetype, population_size, period, rule_set, outer_mutation_rate, inner_mutation_rate, cull_rate, max_iterations); StringBuilder sb = new StringBuilder(); sb.AppendLine("CA dimensions: " + archetype.Width + "x" + archetype.Height); sb.AppendLine("CA wrap_mode: " + archetype.wrap_mode); sb.AppendLine("population size: " + population_size); sb.AppendLine("period: " + period); sb.AppendLine("outer mutation rate: " + outer_mutation_rate); sb.AppendLine("inner mutation rate: " + inner_mutation_rate); sb.AppendLine("cull rate: " + cull_rate); sb.AppendLine("max iterations: " + max_iterations); sb.AppendLine("seed: " + seed); ga.run(seed, sb); //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new MainWindow()); //CARuleSet crs = new CARuleSet(); //String name = CAState.getRandomOscillatorName(); //Console.WriteLine(name); //CAState c = CAState.loadOscillatorFromName(name); //c.addPadding(2, 2); //int period = crs.getPeriod(c); //float heat = crs.getHeat(c, period); //Console.WriteLine("period " + period); //Console.WriteLine("heat " + heat); //CAState temp0 = new CAState(c); //CAState temp1 = new CAState(c); //for (int i=0; i<period; ++i) //{ // Console.WriteLine(temp0.ToString()); // crs.getTransition(temp0, temp1); // temp0.copyFrom(temp1); //} }