public Cosmos(int NumFood, int NumAnts, Size ClientSize, Graphics g, ListView initListAnalysis, Panel p, BufferedGraphics gr) { clientSize = ClientSize; this.g = g; listAnalysis = initListAnalysis; panel = p; grafx = gr; numFood = NumFood; food = new PointF[numFood]; RandomFood(); numAnts = NumAnts; ants = new Ants[numAnts]; Cosmos.elitismPopulation = (int)(numAnts * Cosmos.elitismRate); //create random ants for (int i = 0; i < numAnts; i++) { ants[i] = new Ants(clientSize, this, null, i + 1); } }
//choose to mutate and create new gen. private void NewGeneration() { int totalFit = CalculateFitness(); if (useElitism == true || landMines == true) { Sort(); if (landMines) { for (int j = 3, i = 0; j < Cosmos.elitismPopulation + 1; j++, i++) { ants[j].net.Weights = ants[0].net.Weights; } Array temp1 = ants[0].net.Weights; Array temp2 = ants[numAnts - Cosmos.elitismPopulation].net.Weights; Mate(ref temp1, ref temp2); ants[0].net.Weights = temp1; ants[numAnts - Cosmos.elitismPopulation].net.Weights = temp2; } } Array parent1; Array parent2; Ants[] newAnts = new Ants[numAnts]; //create new gen for (int i = 0; i < (numAnts / 2); i++) { parent1 = ants[RouletteWheel(totalFit)].net.Weights; parent2 = ants[RouletteWheel(totalFit)].net.Weights; Mutate(parent1); Mutate(parent2); newAnts[i * 2] = new Ants(clientSize, this, parent1, i * 2); newAnts[i * 2 + 1] = new Ants(clientSize, this, parent2, i * 2 + 1); } //replace and leave the other to GC ants = newAnts; }
public Cosmos(int NumFood, int NumAnts, Size ClientSize, Graphics g, ListView initListAnalysis, Panel p, BufferedGraphics gr) { clientSize = ClientSize; this.g = g; listAnalysis = initListAnalysis; panel = p; grafx = gr; numFood = NumFood; food = new PointF[numFood]; RandomFood(); numAnts = NumAnts; ants = new Ants[numAnts]; Cosmos.elitismPopulation = (int)(numAnts * Cosmos.elitismRate); //create random ants for (int i = 0; i < numAnts; i++) ants[i] = new Ants(clientSize, this, null, i + 1); }
public Cosmos(int NumAnts, Size ClientSize, Graphics g, ListView initListAnalysis, Panel p, BufferedGraphics gr, string[] inputFile) { // m_objDrawing = new Ivan.Drawing(); // m_objDrawing.Read(@"C:\Users\kogunlana\Documents\Airbus 2D CAD\A320-100.dwg"); clientSize = ClientSize; this.g = g; listAnalysis = initListAnalysis; panel = p; grafx = gr; int count = 0; int countObs = 0; int countAgent = 0; for (int k = 0; k < inputFile.Length; k++) { if (inputFile[k].Contains("Goals")) { string[] goal = inputFile[k].Split(' '); numFood = int.Parse(goal[1]); food = new PointF[numFood]; } if (inputFile[k].Contains("goal")) { string[] goal = inputFile[k].Split(' '); food[count].X = (float)(Double.Parse(goal[1])); food[count].Y = (float)(Double.Parse(goal[2])); count++; } if (inputFile[k].Contains("Obstacles")) { string[] obstacle = inputFile[k].Split(' '); polygons = new Polygon[int.Parse(obstacle[1])]; } if (inputFile[k].Contains("obstacle")) { if (countObs < polygons.Length) { int countPolygon = 0; polygons[countObs] = new Polygon(); while (countPolygon < 4) { // read next 4 lines of input file for polygon k++; string[] obstacle = inputFile[k].Split(' '); polygons[countObs].Points.Add(new Vector2(int.Parse(obstacle[0]), int.Parse(obstacle[1]))); countPolygon++; } countObs++; } } if (inputFile[k].Contains("agentCircles")) { string[] agent = inputFile[k].Split(' '); numAnts = int.Parse(agent[1]); ants = new Ants[numAnts]; } if (inputFile[k].Contains("type")) { string[] agents = inputFile[k].Split(' '); ants[countAgent] = new Ants(clientSize, this, null, countAgent + 1); ants[countAgent].agentType = agents[1]; // countAgent++; } if (inputFile[k].Contains("position")) { string[] pos = inputFile[k].Split(' '); ants[countAgent].x = (Double.Parse(pos[1])); ants[countAgent].y = (Double.Parse(pos[2])); } if (inputFile[k].Contains("speed")) { string[] speed = inputFile[k].Split(' '); ants[countAgent].antMaxSpeed = (Double.Parse(speed[1])); countAgent++; } } /* polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(5, 20)); * polygons[countObs].Points.Add(new Vector2(1015, 20)); * polygons[countObs].Points.Add(new Vector2(1015, 5)); * polygons[countObs].Points.Add(new Vector2(5, 5)); * * countObs++; * polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(5, 695)); * polygons[countObs].Points.Add(new Vector2(57, 695)); * polygons[countObs].Points.Add(new Vector2(57, 5)); * polygons[countObs].Points.Add(new Vector2(5, 5)); * * countObs++; * polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(5, 670)); * polygons[countObs].Points.Add(new Vector2(1015, 670)); * polygons[countObs].Points.Add(new Vector2(1015, 695)); * polygons[countObs].Points.Add(new Vector2(5, 695)); * * countObs++; * polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(1015, 200)); * polygons[countObs].Points.Add(new Vector2(965, 200)); * polygons[countObs].Points.Add(new Vector2(965, 5)); * polygons[countObs].Points.Add(new Vector2(1015, 5)); * * countObs++; * polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(1015, 400)); * polygons[countObs].Points.Add(new Vector2(965, 400)); * polygons[countObs].Points.Add(new Vector2(965, 250)); * polygons[countObs].Points.Add(new Vector2(1015, 250)); * * countObs++; * polygons[countObs] = new Polygon(); * polygons[countObs].Points.Add(new Vector2(1015, 695)); * polygons[countObs].Points.Add(new Vector2(965, 695)); * polygons[countObs].Points.Add(new Vector2(965, 450)); * polygons[countObs].Points.Add(new Vector2(1015, 450)); */ //RandomFood(); AssignGoal(); //numAnts = NumAnts; Cosmos.elitismPopulation = (int)(numAnts * Cosmos.elitismRate); //create random ants // for (int i = 0; i < numAnts; i++) // ants[i] = new Ants(clientSize, this, null, i + 1); polygon.Points.Add(new Vector2(300, 200)); polygon.Points.Add(new Vector2(450, 250)); polygon.Points.Add(new Vector2(300, 350)); polygon.Points.Add(new Vector2(200, 300)); }
//choose to mutate and create new gen. private void NewGeneration() { int totalFit = CalculateFitness(); if (useElitism == true || landMines == true) { Sort(); if (landMines) { for (int j = 3, i = 0; j < Cosmos.elitismPopulation + 1; j++, i++) ants[j].net.Weights = ants[0].net.Weights; Array temp1 = ants[0].net.Weights; Array temp2 = ants[numAnts - Cosmos.elitismPopulation].net.Weights; Mate(ref temp1, ref temp2); ants[0].net.Weights = temp1; ants[numAnts - Cosmos.elitismPopulation].net.Weights = temp2; } } Array parent1; Array parent2; Ants[] newAnts = new Ants[numAnts]; //create new gen for (int i = 0; i < (numAnts / 2); i++) { parent1 = ants[RouletteWheel(totalFit)].net.Weights; parent2 = ants[RouletteWheel(totalFit)].net.Weights; Mutate(parent1); Mutate(parent2); newAnts[i * 2] = new Ants(clientSize, this, parent1, i * 2); newAnts[i * 2 + 1] = new Ants(clientSize, this, parent2, i * 2 + 1); } //replace and leave the other to GC ants = newAnts; }