/// <summary> /// Constructor /// </summary> /// <param name="graph">graph to indicate dimension of the individual</param> public Individual(GraphInfo graph) { this.cubeDimension = graph.GetDimension(); int edgeCount = (int)Math.Pow(2, this.cubeDimension - 1) * this.cubeDimension; this.graph = graph; this.edgeActivity = new byte[edgeCount]; this.vertexCount = (int)Math.Pow(2, this.cubeDimension); }
/// <summary> /// Constructor /// </summary> /// <param name="daddy">individual whose properties but not edgeActivity i taken</param> public Individual(Individual daddy) { this.cubeDimension = daddy.cubeDimension; this.vertexCount = daddy.vertexCount; this.graph = daddy.graph; this.changed = daddy.changed; this.spanner = daddy.spanner; this.fitnessValue = daddy.fitnessValue; this.objectiveValue = daddy.objectiveValue; this.edgeActivity = new byte[daddy.edgeActivity.Length]; }
/// <summary> /// Get an instance of the graph /// </summary> /// <param name="dim">dimension of the graph</param> /// <returns>the instance of the graph</returns> public static GraphInfo GetInstance(int dim) { if (theInstance == null) { try { theInstance = LoadFullCube(dim); } catch { theInstance = new GraphInfo(dim); } } return(theInstance); }
/// <summary> /// Sets the structure of the individuals (i.e. the hypercube graph) /// </summary> /// <param name="example">hypercube graph</param> public void SetSampleIndividual(GraphInfo example) { this.sampleIndividual = example; }
/// <summary> /// GA running method /// </summary> /// <returns>best computed individual</returns> public Individual RunIt() { if (!Settings.parallel) { Console.WriteLine("Starting run no. " + (number + 1)); Console.WriteLine("Initializing run no. " + (number + 1)); } Stopwatch sw = Stopwatch.StartNew(); //Set the rng seed GraphInfo graph = GraphInfo.GetInstance(s.cubeDimension); Tools tools = Tools.GetInstance(s.cubeDimension); rng = new Random(number); logger.Log(s); ea = s.GetEVA(logger, rng); foreach (var op in ea.GetOperators()) { logger.Log(Logger.Level.SETTINGS, op.ToLog()); } foreach (var se in ea.GetMating()) { logger.Log(Logger.Level.SETTINGS, se.ToLog() + " MAT"); } foreach (var se in ea.GetEnvironmental()) { logger.Log(Logger.Level.SETTINGS, se.ToLog() + " ENV"); } //Create new population Population pop = new Population(); pop.SetPopulationSize(s.popSize); pop.SetSampleIndividual(graph); pop.CreateRandomInitialPopulation(rng); if (!Settings.parallel) { Console.WriteLine("Finished in {0:f2} seconds", sw.Elapsed.TotalSeconds); } logger.Log(Logger.Level.INFO, "Initialization finished in " + String.Format("{0:f2}", sw.Elapsed.TotalSeconds) + " seconds"); //Run the algorithm if (!Settings.parallel || number == 0) { Console.WriteLine("Running run no. " + (number + 1)); } try { for (int i = 0; i < s.maxGen; i++) { sw.Restart(); Program.localDetourSpanners = 0; //Make one generation ea.Evolve(pop); List <Individual> sorted = pop.GetSortedIndividuals(); //Log the best individual to console. if ((i + 1) % Settings.showGap == 0 && (!Settings.parallel || number == 0)) { int idx = 0; while (idx < s.popSize && sorted[idx].Is_3_Spanner(false) < 1) { idx++; } if (idx >= s.popSize) { idx = 0; } Console.Write("Gen: " + (i + 1)); Console.Write(" obj: " + sorted[idx].GetObjectiveValue() + " at " + idx); if (Settings.task == "eds") { Console.Write(" tc: " + sorted[idx].GetColourCount()); } if (Settings.task != "eds") { Console.Write(" fit: {0:f0}", sorted[0].GetFitnessValue()); } else { Console.Write(" fit: {0:f3}", sorted[0].GetFitnessValue()); Console.Write(" comps: "); for (int j = 0; j < Settings.maxColours; j++) { Console.Write(sorted[0].CountComponents((byte)(j + 1))); Console.Write(" "); } } Console.Write(" 3-s: {0:f2} %", (float)(Program.localDetourSpanners * 100.0 / s.popSize)); Console.Write(" avg: {0:f0}", pop.GetAverage()); Console.Write(" med: {0:f0}", sorted[s.popSize / 2].GetFitnessValue()); Console.WriteLine(); } logger.Log(pop, sorted, i); for (int j = 0; j < sorted.Count; j++) { //Console.WriteLine(sorted[j].ToString()); if (j < s.popSize * s.eliteSize) { sorted[j].elite = true; } else { sorted[j].elite = false; } } } if (!Settings.parallel) { Console.WriteLine(); } Individual bestInd; //Console.ReadLine(); for (int j = 0; j < pop.GetPopulationSize(); j++) { if ((pop.GetSortedIndividuals()[j]).Is_3_Spanner(false) == 1) { bestInd = pop.GetSortedIndividuals()[j]; logger.Log(bestInd); return(bestInd); } } bestInd = pop.GetSortedIndividuals()[0]; logger.Log(bestInd); return(bestInd); } catch (Exception e) { Console.WriteLine(e.StackTrace); } return(null); }
/// <summary> /// Main method /// </summary> /// <param name="args"></param> static void Main(string[] args) { // initialize settings, tools and the hypoercube graph Settings s = Settings.GetInstance(); Tools t = Tools.GetInstance(s.cubeDimension); graph = GraphInfo.GetInstance(s.cubeDimension); ///////////////////////////////// // // // Run the algorithm // // // ///////////////////////////////// // information output Console.WriteLine("Dimension: " + s.cubeDimension); Console.WriteLine(); List <Tuple <Individual, int> > bestInds = new List <Tuple <Individual, int> >(); // initialize the array of runs Run[] runs = new Run[s.repeats]; for (int i = 0; i < s.repeats; i++) { runs[i] = new Run(i); } var bests = new List <Tuple <Individual, int> >(); // if parallel run it parallely if (Settings.parallel) { Parallel.ForEach(runs, r => { Individual best = r.RunIt(); lock (bestInds) { // lock the list to avoid race conditions bestInds.Add(new Tuple <Individual, int>(best, r.number)); } }); } // else just run it else { for (int i = 0; i < s.repeats; i++) { bests.Add(new Tuple <Individual, int>(runs[i].RunIt(), i)); } } // output best individuals foreach (var best in bests) { if (best != null) { bestInds.Add(best); } } bestInds.Sort((x, y) => x.Item2.CompareTo(y.Item2)); Console.WriteLine(); for (int i = 0; i < bestInds.Count; i++) { Console.WriteLine("Run " + (i + 1) + ": best objective = " + bestInds[i].Item1.GetObjectiveValue()); Tools.WriteIndividual(bestInds[i].Item1); } Console.ReadLine(); }