public static int CheckMinSeparation(int[,] individ, List <Flight> FlightsData)
 {
     for (int i = 0; i < individ.GetLength(1) - 1; i++)
     {
         int  NextFlightID     = individ[0, i + 1];
         int  CurrentFlightID  = individ[0, i];
         char NextFlightCat    = FlightsData[NextFlightID].Cat;
         char CurrentFlightCat = FlightsData[CurrentFlightID].Cat;
         char NextFlightAD     = FlightsData[NextFlightID].AD;
         char CurrentFlightAD  = FlightsData[CurrentFlightID].AD;
         int  SeperationTime   = BasicFunctions.GetSeperationDistance(CurrentFlightCat, NextFlightCat, CurrentFlightAD, NextFlightAD);
         if (Math.Abs(individ[1, i + 1] - individ[1, i]) < SeperationTime)
         {
             return(i);
         }
     }
     return(-1);
 }
        private void SetChildTime(ref int[,] Individ, int StartIndex, List <Flight> FlightsData)
        {
            int ChildID, ChildLastTime = -1, ChildCurrentScheduledTime;

            if (StartIndex > 0)
            {
                ChildLastTime = Individ[1, StartIndex - 1];
            }
            for (int index = StartIndex; index < Individ.GetLength(1); index++)
            {
                ChildID = Individ[0, index];

                ChildCurrentScheduledTime = FlightsData[ChildID].intScheduledTime;

                if (index == 0)
                {
                    Individ[1, index] = ChildCurrentScheduledTime;
                }
                else
                {
                    int  PreviousFlightID  = Individ[0, index - 1];
                    int  CurrentFlightID   = Individ[0, index];
                    char PreviousFlightCat = FlightsData[PreviousFlightID].Cat;
                    char CurrentFlightCat  = FlightsData[CurrentFlightID].Cat;
                    char PreviousFlightAD  = FlightsData[PreviousFlightID].AD;
                    char CurrentFlightAD   = FlightsData[CurrentFlightID].AD;
                    int  SeperationTime    = BasicFunctions.GetSeperationDistance(PreviousFlightCat, CurrentFlightCat, PreviousFlightAD, CurrentFlightAD);
                    if (ChildCurrentScheduledTime >= (ChildLastTime + SeperationTime))
                    {
                        Individ[1, index] = ChildCurrentScheduledTime;
                    }
                    else
                    {
                        Individ[1, index] = ChildLastTime + SeperationTime;
                    }
                }

                ChildLastTime = Individ[1, index];
            }
        }
Example #3
0
        public int[,] Execute(List <Flight> FlightsData)
        {
            int currentGeneration = 1;

            P = new int[2 * Parameters.PopSize, FlightsData.Count];  //int[,] P = new int[2 * PopSize, FlightsData.Count];
            InitialSolution IS = new InitialSolution();
            List <int>      PopulationIndexList = BasicFunctions.GetPopulationIndexList();

            for (int IndividIndex = 0; IndividIndex < 2 * Parameters.PopSize; IndividIndex += 2)
            {
                int [,] Individ = IS.GetRandomSolution(FlightsData);
                BasicFunctions.IndividInsert(ref P, Individ, IndividIndex);    //IndividInsert(P,Individ, IndividIndex);
            }
            int[,] Best = new int[2, FlightsData.Count];
            PopQuality  = new int[2, Parameters.PopSize];
            BasicFunctions.calculationTime = BasicFunctions.sw.ElapsedMilliseconds;
            do
            {
                for (int individIndex = 0; individIndex < P.GetLength(0); individIndex += 2)
                {
                    int currentFitness = Constraints.AssessFitness(P, individIndex, FlightsData);
                    PopQuality[0, individIndex / 2] = individIndex;
                    PopQuality[1, individIndex / 2] = currentFitness;
                    //Console.WriteLine("currentFitness = " + currentFitness);
                    if (BestFitness == -1 || currentFitness < BestFitness)
                    {
                        Best        = BasicFunctions.GetIndivid(P, individIndex);
                        BestFitness = currentFitness;
                        BasicFunctions.calculationTime = BasicFunctions.sw.ElapsedMilliseconds;
                        //Console.WriteLine("Calculation time: " + BasicFunctions.calculationTime);
                        //Console.WriteLine("Iteration: " + currentGeneration);
                        //Console.WriteLine("BestFitness = " + BestFitness);
                        //Constraints.CheckMinSeparation(Best);
                        //Constraints.CheckNoAssignBefore(Best,FlightsData);
                        //BasicFunctions.PrintIndivid(Best);
                    }
                }

                int[,] Q = new int[Parameters.PopSize * 2, FlightsData.Count];
                int[,] ParentA, ParentB;
                int ParentAIndex, ParentBIndex;
                int[,] ChildA = new int[2, FlightsData.Count];
                int[,] ChildB = new int[2, FlightsData.Count];
                List <int> TournamentSelectionIndexList;
                Parameters.ResetParameterValues(FlightsData.Count);
                for (int NewPopIndividIndex = 0; NewPopIndividIndex < Parameters.PopSize * 2; NewPopIndividIndex += 4)
                {
                    TournamentSelectionIndexList = BasicFunctions.GetTournamentSelectionIndexList(PopulationIndexList);
                    ParentAIndex = BasicFunctions.GetParentIndex(TournamentSelectionIndexList, PopQuality);
                    TournamentSelectionIndexList = BasicFunctions.GetTournamentSelectionIndexList(PopulationIndexList);
                    ParentBIndex = BasicFunctions.GetParentIndex(TournamentSelectionIndexList, PopQuality);
                    ParentA      = BasicFunctions.GetIndivid(P, ParentAIndex);
                    ParentB      = BasicFunctions.GetIndivid(P, ParentBIndex);
                    this.GenerateChildren(ref ChildA, ref ChildB, ParentA, ParentB, FlightsData);
                    this.ApplySwap(ref ChildA, FlightsData);
                    this.ApplySwap(ref ChildB, FlightsData);
                    BasicFunctions.IndividInsert(ref Q, ChildA, NewPopIndividIndex);
                    BasicFunctions.IndividInsert(ref Q, ChildB, NewPopIndividIndex + 2);
                }
                P = Q;
                //Console.WriteLine("CurrentGeneration: " + currentGeneration);
                currentGeneration++;
            } while (currentGeneration <= Parameters.maxGenerations);
            //Console.WriteLine("Best fitness: " + BestFitness);
            //BasicFunctions.SetCalculatedFlightTime(Best,FlightsData);
            //BasicFunctions.SetCalculatedPosition(Best, FlightsData);
            //BasicFunctions.PrintIndivid(Best);
            //BasicFunctions.PrintFlightTime(FlightsData);
            //Constraints.CheckMinSeparation(Best, FlightsData);
            //Constraints.CheckNoAssignBefore(Best, FlightsData);
            //Constraints.CheckAllFlightAreAssigned(Best, FlightsData);
            //PrintFlightTime();
            return(Best);
        }
        public int[,] GetRandomSolution(List <Flight> FlightsData)
        {
            int[,] result = new int[2, FlightsData.Count];
            int        resultIndex      = 0;
            List <int> Temp             = new List <int>();
            int        LastAssignedTime = 0;
            int        TempTime         = 0;

            for (int FlightID = 0; FlightID <= result.GetLength(1); FlightID++)
            {
                if (FlightID < result.GetLength(1) && Temp.Count == 0)
                {
                    Temp.Add(FlightID);
                    TempTime = FlightsData[FlightID].intScheduledTime;
                }
                else if (FlightID < result.GetLength(1) && FlightsData[FlightID].intScheduledTime == TempTime)
                {
                    Temp.Add(FlightID);
                }
                else
                {
                    if (FlightID < result.GetLength(1))
                    {
                        FlightID--;
                    }
                    while (Temp.Count > 0)
                    {
                        int RandIndex = r.Next(Temp.Count);
                        result[0, resultIndex] = Temp[RandIndex];
                        int SeperationTime;
                        if (resultIndex == 0)
                        {
                            SeperationTime = 0;
                        }
                        else
                        {
                            int  PreviousFlightID  = result[0, resultIndex - 1];
                            int  CurrentFlightID   = result[0, resultIndex];
                            char PreviousFlightCat = FlightsData[PreviousFlightID].Cat;
                            char CurrentFlightCat  = FlightsData[CurrentFlightID].Cat;
                            char PreviousFlightAD  = FlightsData[PreviousFlightID].AD;
                            char CurrentFlightAD   = FlightsData[CurrentFlightID].AD;
                            SeperationTime = BasicFunctions.GetSeperationDistance(PreviousFlightCat, CurrentFlightCat, PreviousFlightAD, CurrentFlightAD);
                        }
                        if (LastAssignedTime == 0 || FlightsData[Temp[RandIndex]].intScheduledTime - LastAssignedTime >= SeperationTime)
                        {
                            result[1, resultIndex] = FlightsData[Temp[RandIndex]].intScheduledTime;
                        }
                        else
                        {
                            // Per rastet:
                            // LastAssignedTime > FlightsData[Temp[RandIndex]].intScheduledTime
                            // FlightsData[Temp[RandIndex]].intScheduledTime-LastAssignedTime<2
                            result[1, resultIndex] = LastAssignedTime + SeperationTime;
                        }
                        // Pergaditjet per shtimin e fluturimit vijues
                        LastAssignedTime = result[1, resultIndex];
                        Temp.RemoveAt(RandIndex);
                        resultIndex++;
                    }
                }
            }

            return(result);
        }
        public int[,] Execute(List <Flight> FlightsData)
        {
            int currentGeneration = 1;

            P = new int[2 * Parameters.PopSize, FlightsData.Count];  //int[,] P = new int[2 * PopSize, FlightsData.Count];
            InitialSolution IS = new InitialSolution();
            List <int>      PopulationIndexList = BasicFunctions.GetPopulationIndexList();

            for (int IndividIndex = 0; IndividIndex < 2 * Parameters.PopSize; IndividIndex += 2)
            {
                int [,] Individ = IS.GetRandomSolution(FlightsData);
                BasicFunctions.IndividInsert(ref P, Individ, IndividIndex);    //IndividInsert(P,Individ, IndividIndex);
            }
            int[,] Best = new int[2, FlightsData.Count];
            PopQuality  = new int[2, Parameters.PopSize];
            BasicFunctions.calculationTime = BasicFunctions.sw.ElapsedMilliseconds;
            for (int individIndex = 0; individIndex < P.GetLength(0); individIndex += 2)
            {
                int currentFitness = Constraints.AssessFitness(P, individIndex, FlightsData);
                PopQuality[0, individIndex / 2] = individIndex;
                PopQuality[1, individIndex / 2] = currentFitness;
                if (BestFitness == -1 || currentFitness < BestFitness)
                {
                    Best        = BasicFunctions.GetIndivid(P, individIndex);
                    BestFitness = currentFitness;
                    //Console.WriteLine("BestFitness = " + BestFitness);
                    //BasicFunctions.PrintIndivid(Best);
                }
            }

            do
            {
                List <int> RemoveList = this.GetRandomListOfIndividualsToRemove();

                int[,] ParentA, ParentB;
                int ParentAIndex, ParentBIndex;
                int[,] ChildA = new int[2, FlightsData.Count];
                int[,] ChildB = new int[2, FlightsData.Count];
                List <int> TournamentSelectionIndexList;
                Parameters.ResetParameterValues(FlightsData.Count);
                for (int RemoveListIndex = 0; RemoveListIndex < RemoveList.Count; RemoveListIndex += 2)
                {
                    TournamentSelectionIndexList = BasicFunctions.GetTournamentSelectionIndexList(PopulationIndexList);
                    ParentAIndex = BasicFunctions.GetParentIndex(TournamentSelectionIndexList, PopQuality);
                    TournamentSelectionIndexList = BasicFunctions.GetTournamentSelectionIndexList(PopulationIndexList);
                    ParentBIndex = BasicFunctions.GetParentIndex(TournamentSelectionIndexList, PopQuality);
                    ParentA      = BasicFunctions.GetIndivid(P, ParentAIndex);
                    ParentB      = BasicFunctions.GetIndivid(P, ParentBIndex);
                    this.GenerateChildren(ref ChildA, ref ChildB, ParentA, ParentB, FlightsData);
                    this.ApplySwap(ref ChildA, FlightsData);
                    this.ApplySwap(ref ChildB, FlightsData);
                    int ChildAFitness = Constraints.AssessFitness(ChildA, FlightsData);

                    //Console.WriteLine("ChildAFitness = " + ChildAFitness);
                    if (ChildAFitness < BestFitness)
                    {
                        Best        = ChildA;
                        BestFitness = ChildAFitness;
                        BasicFunctions.calculationTime = BasicFunctions.sw.ElapsedMilliseconds;
                        //Console.WriteLine("BestFitness = " + BestFitness);
                        //BasicFunctions.PrintIndivid(Best);
                    }
                    int ChildBFitness = Constraints.AssessFitness(ChildB, FlightsData);
                    //Console.WriteLine("ChildBFitness = " + ChildBFitness);
                    if (ChildBFitness < BestFitness)
                    {
                        Best        = ChildB;
                        BestFitness = ChildBFitness;
                        BasicFunctions.calculationTime = BasicFunctions.sw.ElapsedMilliseconds;
                        //Console.WriteLine("BestFitness = " + BestFitness);
                        //BasicFunctions.PrintIndivid(Best);
                    }
                    BasicFunctions.IndividInsert(ref P, ChildA, RemoveList[RemoveListIndex]);
                    BasicFunctions.IndividInsert(ref P, ChildB, RemoveList[RemoveListIndex + 1]);
                    PopQuality[0, RemoveList[RemoveListIndex] / 2]     = RemoveList[RemoveListIndex];
                    PopQuality[1, RemoveList[RemoveListIndex] / 2]     = ChildAFitness;
                    PopQuality[0, RemoveList[RemoveListIndex + 1] / 2] = RemoveList[RemoveListIndex + 1];
                    PopQuality[1, RemoveList[RemoveListIndex + 1] / 2] = ChildBFitness;
                }
                //Console.WriteLine("CurrentGeneration: " + currentGeneration);
                currentGeneration++;
            } while (currentGeneration <= Parameters.maxGenerations);
            //Console.WriteLine("Best fitness: " + BestFitness);
            //BasicFunctions.SetCalculatedFlightTime(Best,FlightsData);
            //PrintIndivid(Best);
            //PrintFlightTime();
            return(Best);
        }