public void RunGDE3 <S>() where S : ContinuousVector, new() { GDE3 <S> algorithm = new GDE3 <S>(GetProblem()); algorithm.PopulationSize = 100; algorithm.Initialize(); ClearStatusInfo(); BackgroundWorker worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.DoWork += (s1, e1) => { while (!algorithm.IsTerminated) { algorithm.Evolve(); worker.ReportProgress(0); } }; worker.ProgressChanged += (s1, e1) => { txtStatus1.Text = string.Format("Current Generation: {0}", algorithm.CurrentGeneration); txtStatus2.Text = string.Format("Size of Archive: {0}", algorithm.NondominatedArchiveSize); }; worker.RunWorkerCompleted += (s1, e1) => { NondominatedPopulation <S> archive = algorithm.NondominatedArchive; DisplayParetoFront(archive); }; worker.RunWorkerAsync(); }
private void DisplayParetoFront <S>(NondominatedPopulation <S> archive) where S : ContinuousVector, new() { GraphPane pane = chtParetoFront.GraphPane; pane.XAxis.Title.Text = "Objective 1"; pane.YAxis.Title.Text = "Objective 2"; pane.Title.Text = string.Format("Problem: {0}", (ProblemType)cboProblem.SelectedItem); PointPairList list = new PointPairList(); foreach (S s in archive.Solutions) { list.Add(s.FindObjectiveAt(0), s.FindObjectiveAt(1)); } AlgorithmType algorithm_type = (AlgorithmType)cboAlgorithm.SelectedItem; LineItem myCurve = pane.AddCurve(string.Format("{0} ({1})", algorithm_type.ToString(), cboProblem.SelectedItem), list, GetParetoFrontColor()); myCurve.Symbol.Type = ZedGraph.SymbolType.Plus; myCurve.Line.IsVisible = false; pane.AxisChange(); chtParetoFront.Invalidate(); DataTable table = new DataTable(); table.Columns.Add("#"); IMOOProblem problem = archive[0].Problem; for (int i = 0; i < problem.GetObjectiveCount(); ++i) { table.Columns.Add(string.Format("Objective {0}", i + 1)); } for (int i = 0; i < problem.GetDimensionCount(); ++i) { table.Columns.Add(string.Format("x[{0}]", i)); } for (int i = 0; i < archive.Count; ++i) { S s = archive[i]; List <object> values = new List <object>(); values.Add(i + 1); for (int j = 0; j < problem.GetObjectiveCount(); ++j) { values.Add(s.FindObjectiveAt(j)); } for (int j = 0; j < problem.GetDimensionCount(); ++j) { values.Add(s[j]); } table.Rows.Add(values.ToArray()); } dgvParetoFront.DataSource = table; }
static void RunHybridGame() { HybridGame <ContinuousVector> algorithm = new HybridGame <ContinuousVector>(new NDNDProblem()); algorithm.Config.PopulationSize = 100; algorithm.Initialize(); while (!algorithm.IsTerminated) { algorithm.Evolve(); Console.WriteLine("Current Generation: {0}", algorithm.CurrentGeneration); Console.WriteLine("Size of Archive: {0}", algorithm.NondominatedArchiveSize); } NondominatedPopulation <ContinuousVector> paretoFront = algorithm.NondominatedArchive; }
public static void Main(string[] args) { #region variables int nPopulation = 100; //string fragmentsDefinitionPath = "Fragments.txt"; //Dictionary<int, IFragmentElemet> VectorTemplate = new Dictionary<int, IFragmentElemet>(); //string shows fragmentElement and int shows its location in population //Dictionary<int, Dictionary<int, IFragmentElemet>> ModelFragmentVector = new Dictionary<int, Dictionary<int, IFragmentElemet>>(); //string shows fragmentElement and int shows its location in population Dictionary <Model, int> ModelFragmentVector = new Dictionary <Model, int>(); //List<int[]> population = new List<int[]>(); //each bool shows if a fragment is selected or not List <Dictionary <Model, int> > population = new List <Dictionary <Model, int> >(); System.Random rnd = new System.Random(); #endregion #region work with model var repository = new ModelRepository(); //var model = repository.Resolve("ClassModel.xmi"); //var model_a = repository.Resolve("a.xmi"); //var classModel_a = model_a.RootElements[0] as ClassModel; var model_b = repository.Resolve("b.xmi"); var classModel_b = model_b.RootElements[0] as ClassModel; //var model_c = repository.Resolve("c.xmi"); //var classModel_c = model_c.RootElements[0] as ClassModel; //var model_d = repository.Resolve("d.xmi"); //var classModel_d = model_d.RootElements[0] as ClassModel; //var model_e = repository.Resolve("e.xmi"); //var classModel_e = model_e.RootElements[0] as ClassModel; //step 1: Read All Fragments DirectoryInfo d = new DirectoryInfo("Inputs");//Assuming Test is your Folder FileInfo[] Files = d.GetFiles("*.xmi"); foreach (FileInfo file in Files) { //step2:Make solution space based on loaded Fragments try { var fragmentModel = repository.Resolve(file.FullName); var fragmentClassModel = fragmentModel.RootElements[0] as ClassModel; // for trace purpose only ModelFragmentVector.Add(fragmentModel, 0); //temp } catch (Exception ex) { } } //step2:Make solution space based on loaded Fragments for (int i = 0; i < nPopulation; i++) { //int[] solution = new int[ModelFragmentVector.Count]; //for (int j = 0; j < ModelFragmentVector.Count; j++) // solution[j] = rnd.NextDouble() >= 0.5 ? 1 : 0; //population.Add(solution); //clone as ModelFragmentVector Dictionary <Model, int> solution = new Dictionary <Model, int>(); foreach (KeyValuePair <Model, int> entry in ModelFragmentVector) { solution.Add(entry.Key, rnd.NextDouble() >= 0.5 ? 1 : 0); } //add to population population.Add(solution); } //step3: write objectives //step4: run the algorithm #endregion work with model #region work with meta-model #region create vector template //Type[] typelist = GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MDEMGTT.ArchitectureCRA"); //for (int i = 0; i < typelist.Length; i++) // if (!typelist[i].GetTypeInfo().IsInterface) // VectorTemplate.Add(typelist[i].Name, false); #endregion make solution space from template #endregion work with meta-model Console.WriteLine("Model Fragments Extracted!"); #region algrorithm play //NSGAII<ContinuousVector> algorithm = new NSGAII<ContinuousVector>(new CRAProblem(population, ModelFragmentVector)); Algorithms.NSGAII algorithm = new Algorithms.NSGAII(new CRAProblem(population, ModelFragmentVector)); algorithm.PopulationSize = 100; algorithm.Initialize(); while (!algorithm.IsTerminated) { algorithm.Evolve(); Console.WriteLine("Current Generation: {0}", algorithm.CurrentGeneration); Console.WriteLine("Size of Archive: {0}", algorithm.NondominatedArchiveSize); } ContinuousVector finalSolution = algorithm.GlobalBestSolution; NondominatedPopulation <ContinuousVector> paretoFront = algorithm.NondominatedArchive; Console.WriteLine(finalSolution.CrowdingDistance); Console.WriteLine(paretoFront.Count); Console.ReadKey(); #endregion algorithm play }