Пример #1
0
        private static void ShowChromosome(TspChromosome c)
        {
            Console.WriteLine("Fitness: {0:n2}", c.Fitness);
            Console.WriteLine("Cities: {0:n0}", c.Length);
            Console.WriteLine("Distance: {0:n2}", c.Distance);

            var cities = c.GetGenes().Select(g => g.Value.ToString()).ToArray();

            Console.WriteLine("City tour: {0}", string.Join(", ", cities));
        }
        public void Cross_TspChromosome_Child()
        {
            var target      = new AlternatingPositionCrossover();
            var chromosome1 = new TspChromosome(100);
            var chromosome2 = new TspChromosome(100);
            var actual      = target.Cross(new TspChromosome[] { chromosome1, chromosome2 });

            Assert.AreEqual(2, actual.Count);

            CollectionAssert.AllItemsAreUnique(chromosome1.GetGenes());
            CollectionAssert.AllItemsAreUnique(chromosome2.GetGenes());
        }
Пример #3
0
        /// <summary>
        /// Draws the sample.
        /// </summary>
        public override void Draw()
        {
            var buffer = Context.Buffer;
            var gc     = Context.GC;
            var layout = Context.Layout;

            // Draw cities.
            foreach (var c in m_fitness.Cities)
            {
                buffer.DrawRectangle(gc, true, c.X - 2, c.Y - 2, 4, 4);
            }

            if (m_bestChromosome != null)
            {
                var genes = m_bestChromosome.GetGenes();

                for (int i = 0; i < genes.Length; i += 2)
                {
                    var cityOneIndex = Convert.ToInt32(genes[i].Value);
                    var cityTwoIndex = Convert.ToInt32(genes[i + 1].Value);
                    var cityOne      = m_fitness.Cities[cityOneIndex];
                    var cityTwo      = m_fitness.Cities[cityTwoIndex];

                    if (i > 0)
                    {
                        var previousCity = m_fitness.Cities[Convert.ToInt32(genes[i - 1].Value)];
                        buffer.DrawLine(gc, previousCity.X, previousCity.Y, cityOne.X, cityOne.Y);
                    }

                    buffer.DrawLine(gc, cityOne.X, cityOne.Y, cityTwo.X, cityTwo.Y);

                    if (m_showIndexes)
                    {
                        layout.SetMarkup("<span color='black'>{0}</span>".With(i));
                        buffer.DrawLayout(gc, cityOne.X, cityOne.Y, layout);

                        layout.SetMarkup("<span color='black'>{0}</span>".With(i + 1));
                        buffer.DrawLayout(gc, cityTwo.X, cityTwo.Y, layout);
                    }
                }

                var lastCity  = m_fitness.Cities[Convert.ToInt32(genes[genes.Length - 1].Value)];
                var firstCity = m_fitness.Cities[Convert.ToInt32(genes[0].Value)];
                buffer.DrawLine(gc, lastCity.X, lastCity.Y, firstCity.X, firstCity.Y);

                Context.WriteText("Distance: {0:n2}", m_bestChromosome.Distance);
            }
        }