public Population(Board board, int size) { this.Board = board; this.Size = size; this.Chromosomes = new List<Monk>(); GenerateFirstPopulation(size); }
public Population(Board board) { this.Board = board; this.Chromosomes = new List<Monk>(); }
//protected int RakeRandomly(Board b, int start_x, int start_y, char mark, Direction dir) //{ // int x = start_x; // int y = start_y; // int new_x; // int new_y; // Random rand = new Random(); // int raked = 0; // while ( (x >= 0) && (x< b.X) && (y>=0) && (y<b.Y)) // { // if (b.Map[x, y] == Board.Nothing) // { // b.Map[x, y] = mark; // ++raked; // switch (dir) // { // case Direction.Up: // new_y = y - 1; // if (new_y < 0) // return raked; // else if (b.Map[x, new_y] != Board.Nothing) // { // if (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing) // { // dir = Direction.Left; // x--; // continue; // } // else if (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing) // { // dir = Direction.Right; // x++; // continue; // } // } // y = new_y; // break; // case Direction.Down: // new_y = y + 1; // if (new_y == b.Y) // return raked; // //ak nie je volno // else if (b.Map[x, new_y] != Board.Nothing) // { // //pozri vlavo // if (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing) // { // dir = Direction.Left; // x--; // continue; // } // //pozri vpravo // else if (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing) // { // dir = Direction.Right; // x++; // continue; // } // } // y = new_y; // break; // case Direction.Right: // new_x = x + 1; // //return 1; // if (new_x == b.X) // return raked; // else if (b.Map[new_x, y] != Board.Nothing) // { // //pozri hore // if (((y - 1) < 0) || b.Map[x , y - 1] == Board.Nothing) // { // dir = Direction.Up; // y--; // continue; // } // //pozri dole // else if (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing) // { // dir = Direction.Down; // y++; // continue; // } // } // x = new_x; // break; // case Direction.Left: // new_x = x - 1; // //return 1; // if (new_x < 0) // return raked; // else if (b.Map[new_x, y] != Board.Nothing) // { // //pozri hore // if (((y - 1) < 0) || b.Map[x , y - 1] == Board.Nothing) // { // dir = Direction.Up; // y--; // continue; // } // //pozri dole // else if (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing) // { // dir = Direction.Down; // y++; // continue; // } // } // x = new_x; // //return 1; // break; // default: // break; // } // } // else // { // return 0; // } // } // return raked; //} //public void EvaluateOnRandomly(Board b) //{ // double fitness = 0.0 ; // var a = Encoding.ASCII.GetBytes("a")[0]; // //poprechadzaj celu mapu podla instrukcii, pojde to postupne, kedze list je zoradeny // //Console.WriteLine("X= {0} Y={1} obvod={2}", b.X, b.Y, 2 * (b.X + b.Y)); // byte i = 0; // int y = -1; // int x = -1; // int sum = 0; // Direction dir = Direction.Up; ; // b.Reset(); // foreach (var gene in Chromosome) // { // char mark = (char)(i+a); // if (gene.Start < b.X) // { // //this is case when the monk is entering from the upper side // //Console.WriteLine(" {0} => upper side",gene.ToString()); // x = gene.Start; // y = 0; // dir = Direction.Down; // //RakeRandomly(b, x, y, mark, Direction.Down); // } // else if ( (gene.Start>= b.X) && (gene.Start < (b.X+b.Y)) ) // { // //the right side // //Console.WriteLine(" {0} => right side", gene.ToString()); // x = b.X - 1; // y = gene.Start % b.X; // dir = Direction.Left; // } // else if ((gene.Start >= (b.X + b.Y)) && (gene.Start < (b.X + b.X + b.Y))) // { // //the lower side // //Console.WriteLine(" {0} => lower side", gene.ToString()); // x = b.X - (gene.Start % (b.X+b.Y)) - 1; // y = b.Y - 1; // dir = Direction.Up; // } // else if ((gene.Start >= (b.X + b.X + b.Y)) && (gene.Start < 2 * (b.X + b.Y))) // { // //the left side // //Console.WriteLine(" {0} => left side", gene.ToString()); // x = 0; // y = b.Y - (gene.Start % (b.X + b.X + b.Y)) - 1 ; // dir = Direction.Right; // } // else // { // //error // Console.WriteLine("ERROR: {0}",gene.ToString()); // throw new Exception("Gene.Start out of range"); // } // int raked = RakeRandomly(b, x, y, mark, dir); // if (raked > 0) // { // ++i; // sum += raked; // //.RakedSurfaceMap = b.ToString(); // //Console.WriteLine(PrintResult()); // } // else if (raked == (-1)) // break; // } // RakedSurfaceMap = b.ToString(); // fitness = sum; // this.Fitness = fitness; //} public void EvaluateOn(Board b) { var a = Encoding.ASCII.GetBytes("a")[0]; //poprechadzaj celu mapu podla instrukcii, pojde to postupne, kedze list je zoradeny //Console.WriteLine("X= {0} Y={1} obvod={2}", b.X, b.Y, 2 * (b.X + b.Y)); byte i = 0; int y = -1; int x = -1; int sum = 0; Direction dir = Direction.Up; ; b.Reset(); foreach (var gene in Chromosome) { char mark = (char)(i + a); if (gene.Start < b.X) { //this is case when the monk is entering from the upper side //Console.WriteLine(" {0} => upper side",gene.ToString()); x = gene.Start; y = 0; dir = Direction.Down; //RakeRandomly(b, x, y, mark, Direction.Down); } else if ((gene.Start >= b.X) && (gene.Start < (b.X + b.Y))) { //the right side //Console.WriteLine(" {0} => right side", gene.ToString()); x = b.X - 1; y = gene.Start % b.X; dir = Direction.Left; } else if ((gene.Start >= (b.X + b.Y)) && (gene.Start < (b.X + b.X + b.Y))) { //the lower side //Console.WriteLine(" {0} => lower side", gene.ToString()); x = b.X - (gene.Start % (b.X + b.Y)) - 1; y = b.Y - 1; dir = Direction.Up; } else if ((gene.Start >= (b.X + b.X + b.Y)) && (gene.Start < 2 * (b.X + b.Y))) { //the left side //Console.WriteLine(" {0} => left side", gene.ToString()); x = 0; y = b.Y - (gene.Start % (b.X + b.X + b.Y)) - 1; dir = Direction.Right; } else { //error Console.WriteLine("ERROR: {0}", gene.ToString()); throw new Exception("Gene.Start out of range"); } bool was_stuck = false; int raked = Rake(b,gene.TurnsRight, x, y, mark, dir,ref was_stuck); if (raked > 0) { ++i; sum += raked; //.RakedSurfaceMap = b.ToString(); //Console.WriteLine(PrintResult()); } if (was_stuck) break; } RakedSurfaceMap = b.ToString(); this.Fitness = sum; }
//mnich pri narazeni do prekazky vzdy preferuje smer, ktory ma v gene, ak vsak do tej strany nemoze ist, //pojde do tej druhej protected int Rake(Board b,bool turnsRight, int start_x,int start_y, char mark, Direction dir,ref bool was_stuck) { int x = start_x; int y = start_y; int new_x; int new_y; Random rand = new Random(); int raked = 0; while ((x >= 0) && (x < b.X) && (y >= 0) && (y < b.Y)) { if (b.Map[x, y] == Board.Nothing) { b.Map[x, y] = mark; ++raked; switch (dir) { case Direction.Up: new_y = y - 1; if (new_y < 0) return raked; else if (b.Map[x, new_y] != Board.Nothing) { if (!turnsRight && (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing)) { dir = Direction.Left; x--; continue; } else if (turnsRight && (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing)) { dir = Direction.Right; x++; continue; } else if (turnsRight && (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing)) { dir = Direction.Left; x--; continue; } else if (!turnsRight && (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing)) { dir = Direction.Right; x++; continue; } else { was_stuck = true; return raked; } } y = new_y; break; case Direction.Down: new_y = y + 1; if (new_y == b.Y) return raked; //ak nie je volno else if (b.Map[x, new_y] != Board.Nothing) { //pozri vlavo, pre mnicha iduceho dole je to vpravo if (turnsRight && (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing)) { dir = Direction.Left; x--; continue; } //pozri vpravo else if (!turnsRight && (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing)) { dir = Direction.Right; x++; continue; } else if (!turnsRight && (((x - 1) < 0) || b.Map[x - 1, y] == Board.Nothing)) { dir = Direction.Left; x--; continue; } //pozri vpravo else if (turnsRight && (((x + 1) == b.X) || b.Map[x + 1, y] == Board.Nothing)) { dir = Direction.Right; x++; continue; } else { was_stuck = true; return raked; } } y = new_y; break; case Direction.Right: new_x = x + 1; //return 1; if (new_x == b.X) return raked; else if (b.Map[new_x, y] != Board.Nothing) { //pozri hore if (!turnsRight && (((y - 1) < 0) || b.Map[x, y - 1] == Board.Nothing)) { dir = Direction.Up; y--; continue; } //pozri dole else if (turnsRight && (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing)) { dir = Direction.Down; y++; continue; } else if (turnsRight && (((y - 1) < 0) || b.Map[x, y - 1] == Board.Nothing)) { dir = Direction.Up; y--; continue; } //pozri dole else if (!turnsRight && (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing)) { dir = Direction.Down; y++; continue; } else { was_stuck = true; return raked; } } x = new_x; break; case Direction.Left: new_x = x - 1; if (new_x < 0) return raked; else if (b.Map[new_x, y] != Board.Nothing) { //pozri hore if (turnsRight && (((y - 1) < 0) || b.Map[x, y - 1] == Board.Nothing)) { dir = Direction.Up; y--; continue; } //pozri dole else if (!turnsRight && (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing)) { dir = Direction.Down; y++; continue; } else if (!turnsRight && (((y - 1) < 0) || b.Map[x, y - 1] == Board.Nothing)) { dir = Direction.Up; y--; continue; } //pozri dole else if (turnsRight && (((y + 1) == b.Y) || b.Map[x, y + 1] == Board.Nothing)) { dir = Direction.Down; y++; continue; } else { was_stuck = true; return raked; } } x = new_x; break; default: break; } } else { return raked; } } return raked; }
private void BtnLoad_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (board == null) { board = new Board(); } if (openFileDialog1.ShowDialog() == true) { try { if (!openFileDialog1.FileName.Equals(null)) { //board.LoadFromFile("../../Boards/test1.txt"); board.LoadFromFile(openFileDialog1.FileName); if (File.Exists("ftf_result.txt")) File.Delete("ftf_result.txt"); if (File.Exists("avg_fitness.txt")) File.Delete("avg_fitness.txt"); if (File.Exists("max_fitness.txt")) File.Delete("max_fitness.txt"); AppendLine(board.X.ToString() + " " + board.Y.ToString()); AppendLine(board.ToString()); AppendLine("Surface = " + board.Surface()); AppendLine("Strategy = " + selection.ToString()); AppendLine("Elites = " + elitarism_on); AppendLine("Population size = " + pop_size); AppendLine("Elite rate = " + elite_rate); AppendLine("Mutation rate = " + mutation_chance); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }