Exemplo n.º 1
0
        protected override void PerformMutate(IChromosome chromosome, float probability)
        {
            try
            {
                CPChromosome cpChromosome = chromosome as CPChromosome;
                double       rand         = m_rnd.GetDouble();
                if (!(rand <= probability))
                {
                    return;
                }

                int[] genes = cpChromosome.GetValues();

                Graph graph = GraphProvider.Graph;

                foreach (int vertex in graph.Vertexes)
                {
                    IList <int> p = graph.NeighborsList(vertex);
                    if (p.Any(z => genes[z - 1] == genes[vertex - 1]))
                    {
                        genes[vertex - 1] = m_rnd.GetInt(0, chromosome.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 2
0
        private static IChromosome CreateChild(IChromosome parent, List <int> pair)
        {
            var parentCP   = parent as CPChromosome;
            var parentList = parentCP.GetValues().ToList();
            var childList  = new List <int>();

            childList.Add(pair[0]);
            childList.Add(pair[1]);

            parentList.ForEach(v =>
            {
                if (!childList.Contains(v))
                {
                    childList.Add(v);
                }
            });

            var child = new CPChromosome(childList.Count, childList.ToArray());

            return(child);
        }
Exemplo n.º 3
0
        public double Evaluate(IChromosome chromosome)
        {
            try
            {
                CPChromosome localChromosome = chromosome as CPChromosome;
                if (localChromosome == null)
                {
                    throw new InvalidProgramException("Chromosome cannot be null");
                }
                int[] chromosomeValues = localChromosome.GetValues();
#if DEBUG
                string p = $"{chromosomeValues[0]} , {chromosomeValues[1]} , {chromosomeValues[2]} , {chromosomeValues[3]}";
#endif
                Graph graph = GraphProvider.Graph;

                bool   hasAnyNeighborSameColor = false;
                double countOfBadColoring      = 0;
                foreach (int vertex in graph.Vertexes)
                {
                    IList <int> neighbors            = graph.NeighborsList(vertex).ToList();
                    int         colorOfCurrentVertex = chromosomeValues[vertex - 1];
                    hasAnyNeighborSameColor = hasAnyNeighborSameColor ||
                                              neighbors.Any(x => chromosomeValues[x - 1] == colorOfCurrentVertex);
                    countOfBadColoring += (double)(neighbors.Count(x => chromosomeValues[x - 1] == colorOfCurrentVertex) - 1) /
                                          neighbors.Count;
                }

                if (hasAnyNeighborSameColor)
                {
                    return(-(countOfBadColoring + graph.Vertexes.Count + 1));
                }

                return(-(chromosomeValues.ToList().Distinct().Count()));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return(int.MinValue);
            }
        }
Exemplo n.º 4
0
        protected override IList <IChromosome> PerformCross(IList <IChromosome> parents)
        {
            CPChromosome p1 = parents[0] as CPChromosome;
            CPChromosome p2 = parents[1] as CPChromosome;

            int index1 = m_rnd.GetInt(0, p1.Length / 2);
            int index2 = m_rnd.GetInt(0, p2.Length / 2);

            List <int> pair1 = new List <int>()
            {
                p1.GetValues()[index1], p1.GetValues()[index1 + 1]
            };
            List <int> pair2 = new List <int>()
            {
                p2.GetValues()[index2], p2.GetValues()[index2 + 1]
            };

            IChromosome child1 = CreateChild(p1, pair2);
            IChromosome child2 = CreateChild(p2, pair1);

            return(new List <IChromosome> {
                child1, child2
            });
        }