コード例 #1
0
    private void DoCrossOver()
    {
        int lvIndex1 = -1;
        int lvIndex2 = -1;
        int lvCount = 0;
        int lvIndividualSize = mFather.Count - 1;
        int lvHalfIndex = (int)lvIndividualSize / 2;
        Gene lvGeneFather = null;
        Gene lvGeneMother = null;
        Gene lvGene = null;
        Queue<Gene> lvQueueSon = new Queue<Gene>();
        Queue<Gene> lvQueueDaughter = new Queue<Gene>();
        List<Gene> lvInitialSon = null;
        List<Gene> lvInitialDaughter = null;
        List<Gene> lvGenes = null;
        HashSet<Gene> lvHashSetSon = new HashSet<Gene>();
        HashSet<Gene> lvHashSetDaughter = new HashSet<Gene>();

        Random lvRandom = new Random(DateTime.Now.Millisecond);

        if (USE_CROSSOVER_2_PONTOS)
        {
            lvIndex1 = lvRandom.Next(lvHalfIndex + 1);
            lvIndex2 = lvRandom.Next(lvHalfIndex + 1, lvIndividualSize + 1);
        }
        else
        {
            lvIndex1 = lvRandom.Next(lvIndividualSize+1);
            lvIndex2 = lvIndividualSize + 1;
        }

        mSon = new TrainIndividual(mFitness, mDateRef, null, null);
        lvInitialSon = mFather.GetGenes(0, lvIndex1);
        mSon.AddGenes(lvInitialSon);
        foreach (Gene lvGen in lvInitialSon)
        {
            if (lvGen.HeadWayTime == DateTime.MinValue)
            {
                lvHashSetSon.Add(lvGen);
            }
        }
        //DumpStopLocation((TrainIndividual)mSon);

        mDaughter = new TrainIndividual(mFitness, mDateRef, null, null);
        lvInitialDaughter = mMother.GetGenes(0, lvIndex1);
        mDaughter.AddGenes(lvInitialDaughter);
        foreach (Gene lvGen in lvInitialDaughter)
        {
            if (lvGen.HeadWayTime == DateTime.MinValue)
            {
                lvHashSetDaughter.Add(lvGen);
            }
        }

        /*
        DebugLog.Logar(" ");
        DebugLog.Logar("lvHashSetSon: ");
        DumpHashSet(lvHashSetSon);

        DebugLog.Logar(" ");
        DebugLog.Logar("lvHashSetDaughter: ");
        DumpHashSet(lvHashSetDaughter);

        DebugLog.Logar("Father: ");
        ((TrainIndividual)mFather).DumpGene(null);

        DebugLog.Logar("Mother: ");
        ((TrainIndividual)mMother).DumpGene(null);

        //DebugLog.Logar(" ");
        */

        lvCount = mMother.Count;
        if (mFather.Count > lvCount)
        {
            lvCount = mFather.Count;
        }

        for (int i = 0; i < lvCount; i++)
        {
            lvGeneFather = mFather.GetGene(i);
            lvGeneMother = mMother.GetGene(i);

            if ((mSon != null) && (lvGeneMother != null))
            {
                if ((mSon.Count - 1) < lvIndex2)
                {
                    if (!lvHashSetSon.Contains(lvGeneMother) && lvGeneMother.HeadWayTime == DateTime.MinValue)
                    {
                        /*
                        DebugLog.Logar("Tentando adicionar em crossover ao individuo mSon o Gene: " + lvGeneMother);
                        if (lvGeneMother.StopLocation != null)
                        {
                            DebugLog.Logar("lvGeneMother.StopLocation = " + lvGeneMother.StopLocation);
                        }
                        else
                        {
                            DebugLog.Logar("lvGeneMother.StopLocation is null !");
                        }
                        DebugLog.Logar("lvGeneMother.GetHashCode() = " + lvGeneMother.GetHashCode());
                        DebugLog.Logar("lvHashSetSon.Contains(lvGeneMother) = " + lvHashSetSon.Contains(lvGeneMother));

                        DebugLog.Logar("Listando movimentos anteriores a esse Gene em: " + mSon);
                        lvGene = ((TrainIndividual)mSon).DumpGene(lvGeneMother);
                        if (lvGene != null)
                        {
                            if (lvGene.StopLocation != null)
                            {
                                DebugLog.Logar("Ultimo Stop Location: " + lvGene.StopLocation);
                            }
                        }
                        */

                        AddFromQueue(mSon, lvQueueSon, lvHashSetSon);
                        lvGenes = mSon.GetNextPosition(lvGeneMother, DateTime.MaxValue);
                        if (lvGenes != null)
                        {
                            mSon.AddGenes(lvGenes);

                            if (lvGenes.Count == 2)
                            {
                                lvHashSetSon.Add(lvGenes[0]);
                                //mStopLocationOcupation = ((TrainIndividual)mSon).GetStopLocationOcupation();
                                //DumpStopLocation(lvGenes[lvGenes.Count - 1]);
                            }
                        }
                        else
                        {
                            lvQueueSon.Enqueue(lvGeneMother);
                            /*
                            DebugLog.Logar("Erro ao tentar unir Genes em crossover:");
                            ((TrainIndividual)mSon).DumpGene(null);
                            DebugLog.Logar("Erro ao tentar adicionar Gene (" + lvGeneMother + ")");
                            DebugLog.Logar("Genes de Mother:");
                            ((TrainIndividual)mMother).DumpGene(null);
                            DebugLog.Logar("Existe movimentacao pendente para sair de Dead Lock = " + VerifyPendentGene((TrainIndividual)mSon, (TrainIndividual)mMother, i, lvIndex2));
                            //mSon = null;
                             */
                        }
                    }
                }
            }

            if ((mDaughter != null) && (lvGeneFather != null))
            {
                if ((mDaughter.Count - 1) < lvIndex2)
                {
                    if (!lvHashSetDaughter.Contains(lvGeneFather) && lvGeneFather.HeadWayTime == DateTime.MinValue)
                    {
                        /*
                        DebugLog.Logar("Tentando adicionar em crossover ao individuo mDaughter o Gene: " + lvGeneFather);
                        if (lvGeneFather.StopLocation != null)
                        {
                            DebugLog.Logar("lvGeneFather.StopLocation = " + lvGeneFather.StopLocation);
                        }
                        else
                        {
                            DebugLog.Logar("lvGeneFather.StopLocation is null !");
                        }
                        DebugLog.Logar("lvGeneFather.GetHashCode() = " + lvGeneFather.GetHashCode());
                        DebugLog.Logar("lvHashSetDaughter.Contains(lvGeneFather) = " + lvHashSetDaughter.Contains(lvGeneFather));

                        DebugLog.Logar("Listando movimentos anteriores a esse Gene em: " + mDaughter);

                        lvGene = ((TrainIndividual)mDaughter).DumpGene(lvGeneFather);
                        if (lvGene != null)
                        {
                            if(lvGene.StopLocation != null)
                            {
                                DebugLog.Logar("Ultimo Stop Location: " + lvGene.StopLocation);
                            }
                        }
                        */

                        AddFromQueue(mDaughter, lvQueueDaughter, lvHashSetDaughter);
                        lvGenes = mDaughter.GetNextPosition(lvGeneFather, DateTime.MaxValue);
                        if (lvGenes != null)
                        {
                            mDaughter.AddGenes(lvGenes);

                            if (lvGenes.Count == 2)
                            {
                                lvHashSetDaughter.Add(lvGenes[0]);
                                //mStopLocationOcupation = ((TrainIndividual)mDaughter).GetStopLocationOcupation();
                                //DumpStopLocation(lvGenes[lvGenes.Count - 1]);
                            }
                        }
                        else
                        {
                            lvQueueDaughter.Enqueue(lvGeneFather);
                            /*
                            DebugLog.Logar("Erro ao tentar unir Genes em crossover:");
                            ((TrainIndividual)mDaughter).DumpGene(null);
                            DebugLog.Logar("Erro ao tentar adicionar Gene (" + lvGeneFather + ")");
                            DebugLog.Logar("Genes de Father:");
                            ((TrainIndividual)mFather).DumpGene(null);
                            DebugLog.Logar("Existe movimentacao pendente para sair de Dead Lock = " + VerifyPendentGene((TrainIndividual)mSon, (TrainIndividual)mMother, i, lvIndex2));
                            */
                            //mDaughter = null;
                        }
                    }
                }
            }

            if (((mSon.Count - 1) >= lvIndex2) && ((mDaughter.Count - 1) >= lvIndex2))
            {
                AddFromQueue(mSon, lvQueueSon, lvHashSetSon);
                AddFromQueue(mDaughter, lvQueueDaughter, lvHashSetDaughter);
                break;
            }
        }

        for(int i = lvIndex1 + 1; i < lvCount; i++)
        {
            lvGeneFather = mFather.GetGene(i);
            lvGeneMother = mMother.GetGene(i);

            if ((mSon != null) && (lvGeneFather != null))
            {
                if (!lvHashSetSon.Contains(lvGeneFather) && lvGeneFather.HeadWayTime == DateTime.MinValue)
                {
                    /*
                    DebugLog.Logar("Tentando adicionar em crossover ao individuo mSon o Gene: " + lvGeneFather);
                    if (lvGeneFather.StopLocation != null)
                    {
                        DebugLog.Logar("lvGeneFather.StopLocation = " + lvGeneFather.StopLocation);
                    }
                    else
                    {
                        DebugLog.Logar("lvGeneFather.StopLocation is null !");
                    }
                    DebugLog.Logar("lvGeneFather.GetHashCode() = " + lvGeneFather.GetHashCode());
                    DebugLog.Logar("lvHashSetSon.Contains(lvGeneFather) = " + lvHashSetSon.Contains(lvGeneFather));

                    ((TrainIndividual)mSon).DumpGene(null);
                    DebugLog.Logar("Listando movimentos anteriores a esse Gene em: " + mSon);
                    lvGene = ((TrainIndividual)mSon).DumpGene(lvGeneFather);
                    if (lvGene != null)
                    {
                        if (lvGene.StopLocation != null)
                        {
                            DebugLog.Logar("Ultimo Stop Location: " + lvGene.StopLocation);
                        }
                    }
                     */
                    AddFromQueue(mSon, lvQueueSon, lvHashSetSon);
                    lvGenes = mSon.GetNextPosition(lvGeneFather, DateTime.MaxValue);
                    if (lvGenes != null)
                    {
                        mSon.AddGenes(lvGenes);

                        if (lvGenes.Count == 2)
                        {
                            lvHashSetSon.Add(lvGenes[0]);
                            //mStopLocationOcupation = ((TrainIndividual)mSon).GetStopLocationOcupation();
                            //DumpStopLocation(lvGenes[lvGenes.Count - 1]);
                        }
                    }
                    else
                    {
                        lvQueueSon.Enqueue(lvGeneFather);
                        /*
                        DebugLog.Logar("Erro ao tentar unir Genes em crossover:");
                        ((TrainIndividual)mSon).DumpGene(null);
                        DebugLog.Logar("Erro ao tentar adicionar Gene (" + lvGeneFather + ")");
                        DebugLog.Logar("Genes de Father:");
                        ((TrainIndividual)mFather).DumpGene(null);
                        DebugLog.Logar("Existe movimentacao pendente para sair de Dead Lock = " + VerifyPendentGene((TrainIndividual)mSon, (TrainIndividual)mFather, i, lvCount));
                        */
                        //mSon = null;
                    }
                }
            }

            if ((mDaughter != null) && (lvGeneMother != null))
            {
                if (!lvHashSetDaughter.Contains(lvGeneMother) && lvGeneMother.HeadWayTime == DateTime.MinValue)
                {
                    /*
                    DebugLog.Logar("Tentando adicionar em crossover ao individuo mDaughter o Gene: " + lvGeneMother);
                    if (lvGeneMother.StopLocation != null)
                    {
                        DebugLog.Logar("lvGeneMother.StopLocation = " + lvGeneMother.StopLocation);
                    }
                    else
                    {
                        DebugLog.Logar("lvGeneMother.StopLocation is null !");
                    }
                    DebugLog.Logar("lvGeneMother.GetHashCode() = " + lvGeneMother.GetHashCode());
                    DebugLog.Logar("lvHashSetSon.Contains(lvGeneMother) = " + lvHashSetSon.Contains(lvGeneMother));

                    ((TrainIndividual)mDaughter).DumpGene(null);
                    DebugLog.Logar("Listando movimentos anteriores a esse Gene em: " + mDaughter);
                    lvGene = ((TrainIndividual)mDaughter).DumpGene(lvGeneMother);
                    if (lvGene != null)
                    {
                        if (lvGene.StopLocation != null)
                        {
                            DebugLog.Logar("Ultimo Stop Location: " + lvGene.StopLocation);
                        }
                    }
                     */
                    AddFromQueue(mDaughter, lvQueueDaughter, lvHashSetDaughter);
                    lvGenes = mDaughter.GetNextPosition(lvGeneMother, DateTime.MaxValue);
                    if (lvGenes != null)
                    {
                        mDaughter.AddGenes(lvGenes);

                        if (lvGenes.Count == 2)
                        {
                            lvHashSetDaughter.Add(lvGenes[0]);
                            //mStopLocationOcupation = ((TrainIndividual)mSon).GetStopLocationOcupation();
                            //DumpStopLocation(lvGenes[lvGenes.Count - 1]);
                        }
                    }
                    else
                    {
                        lvQueueDaughter.Enqueue(lvGeneMother);
                        /*
                        DebugLog.Logar("Erro ao tentar unir Genes em crossover:");
                        ((TrainIndividual)mDaughter).DumpGene(null);
                        DebugLog.Logar("Erro ao tentar adicionar Gene (" + lvGeneMother + ")");
                        DebugLog.Logar("Genes de Mother:");
                        ((TrainIndividual)mMother).DumpGene(null);
                        DebugLog.Logar("Existe movimentacao pendente para sair de Dead Lock = " + VerifyPendentGene((TrainIndividual)mDaughter, (TrainIndividual)mMother, i, lvCount));
                        */
                        //mDaughter = null;
                    }
                }
            }
        }

        AddFromQueue(mSon, lvQueueSon, lvHashSetSon);
        AddFromQueue(mDaughter, lvQueueDaughter, lvHashSetDaughter);

        if (lvQueueSon.Count > 0)
        {
            mSon = null;
        }

        if (lvQueueDaughter.Count > 0)
        {
            mDaughter = null;
        }

        /*
        if (mSon != null)
        {
            mSon.IsValid();
        }

        if (mDaughter != null)
        {
            mDaughter.IsValid();
        }
         */
    }
コード例 #2
0
    private void AddFromQueue(IIndividual<Gene> pIndividual, Queue<Gene> pQueue, HashSet<Gene> pHashRef)
    {
        List<Gene> lvGenes = null;
        Gene lvGene = null;

        for (int i = 0; i < pQueue.Count; i++)
        {
            lvGene = pQueue.Dequeue();
            lvGenes = pIndividual.GetNextPosition(lvGene, DateTime.MaxValue);
            if (lvGenes != null)
            {
                pIndividual.AddGenes(lvGenes);

                if (lvGenes.Count == 2)
                {
                    pHashRef.Add(lvGenes[0]);
                }
            }
            else
            {
                pQueue.Enqueue(lvGene);
            }
        }
    }