Exemplo n.º 1
0
        private void Button_GA_Click(object sender, RoutedEventArgs e)
        {
            if (tbCol.Text.Length > 0 && tbPop.Text.Length > 0)
            {
                Stopwatch time = new Stopwatch();
                time.Start();
                int _color_Num       = Convert.ToInt32(tbCol.Text);
                int _population_Size = Convert.ToInt32(tbPop.Text);


                var ga = new Genetic_Algorithm();
                ga.G_Algorithm(_edges, _nodes.Count, _color_Num, _population_Size);

                int[] _result = ga.Result;
                foreach (Node _node in _nodes)
                {
                    _node.Visited = true;
                    _node.Color   = _brushes[_result[_node.Mark]];
                    PaintNode(_node);
                }
                time.Stop();
                var resultTime = time.ElapsedMilliseconds;
                tbl.Rows.Add("Genetic Algorithm", _nodes.Count, _edges_Grage(), resultTime.ToString(), ga.Iteration);
            }
        }
        static void Main()
        {
            Genetic_Algorithm ga = new Genetic_Algorithm(0.8, 0.05, 300, 1000);

            ga.Notify         += ((a) => Console.WriteLine(a));
            ga.FitnessFunction = new GAFunction(theActualFunction);
            ga.Elitism         = true;
            ga.WorkGeneticAlgorithm();
            double[] values;
            double   fitness;

            ga.GetBest(out values, out fitness);

            Console.WriteLine(fitness);
            for (var i = 0; i < values.Length; i++)
            {
                Console.WriteLine(values[i]);
            }

            Console.Read();
        }