Example #1
0
        //! calculate the fitness value of ALL defined objetive values

        /*!
         * \param ContactWindowsVector contact windows to check
         * \param int max number of Contacts of the Scheduling problem
         * \param int[] population representation of the Genetic scheduler default NULL
         */
        private void calculate(ContactWindowsVector contactWindows, int priorityValue, int nrOfAllContacts = 0, int[] population = null, ContactWindowsVector allcontactWindows = null)
        {
            //list of stations and Satellites
            List <string> stationList   = contactWindows.getStationNames();
            List <string> satelliteList = contactWindows.getSatelliteNames();

            //number of times sation and satellite is scheduled
            int nrOfStation    = contactWindows.getNumberOfStation();
            int nrOfSatellites = contactWindows.getNumberOfSatellites();

            //
            int[] nrOfContactsPerSatellite = new int[nrOfSatellites];
            int[] nrOfContactsPerStation   = new int[nrOfStation];

            //number of Contacts Scheduled
            int nrOfScheduledContacts = 0;

            //Overall Scheduled Time
            double scheduledDuration = 0.0;
            //Complete Time of ALL contacts
            double allDuaration = 0.0;
            //Priority counts
            int priorityMax = priorityValue;
            int prio        = calcualteMaxPrioValue(contactWindows);

            for (int i = 0; i < contactWindows.Count(); i++)
            {
                int stapo = -1;
                int satpo = -1;
                if (population != null)
                {
                    if (population[i] == 1)
                    {
                        stapo = stationList.IndexOf(contactWindows.getAt(i).getStationName());
                        satpo = satelliteList.IndexOf(contactWindows.getAt(i).getSatName());
                        nrOfScheduledContacts++;
                        scheduledDuration += contactWindows.getAt(i).getDuration();
                    }
                }
                else
                {
                    if (nrOfAllContacts == 0)
                    {
                        if (contactWindows.getAt(i).getSheduledInfo())
                        {
                            stapo              = stationList.IndexOf(contactWindows.getAt(i).getStationName());
                            satpo              = satelliteList.IndexOf(contactWindows.getAt(i).getSatName());
                            scheduledDuration += contactWindows.getAt(i).getDuration();
                            nrOfScheduledContacts++;
                        }
                    }
                    else
                    {
                        stapo              = stationList.IndexOf(contactWindows.getAt(i).getStationName());
                        satpo              = satelliteList.IndexOf(contactWindows.getAt(i).getSatName());
                        scheduledDuration += contactWindows.getAt(i).getDuration();
                        nrOfScheduledContacts++;
                    }
                }
                if (allcontactWindows == null)
                {
                    allDuaration += contactWindows.getAt(i).getDuration();
                }

                if (stapo > -1)
                {
                    nrOfContactsPerStation[stapo]++;
                }
                if (satpo > -1)
                {
                    nrOfContactsPerSatellite[satpo]++;
                }
            }

            //calculate Fairness for Stations
            double a_station = 0.0;
            double b_station = 0.0;

            for (int i = 0; i < nrOfStation; i++)
            {
                a_station = a_station + nrOfContactsPerStation[i];
                b_station = b_station + Math.Pow(nrOfContactsPerStation[i], 2);
            }
            val_FairStations = Math.Pow(a_station, 2) / (nrOfStation * b_station);

            //calculate Fairnes for Satellites
            double a_satellites = 0.0;
            double b_satellites = 0.0;

            for (int i = 0; i < nrOfSatellites; i++)
            {
                a_satellites = a_satellites + nrOfContactsPerSatellite[i];
                b_satellites = b_satellites + Math.Pow(nrOfContactsPerSatellite[i], 2);
            }
            val_FairSatellites = Math.Pow(a_satellites, 2) / (nrOfSatellites * b_satellites);

            if (nrOfAllContacts == 0)
            {
                nrOfAllContacts = contactWindows.Count();
            }

            val_Scheduled = nrOfScheduledContacts / (double)nrOfAllContacts;

            val_Duration = scheduledDuration / allDuaration;

            val_Priority = (double)prio / (double)priorityMax;
        }
Example #2
0
        //! Starts the process of finding the best solution through genetor

        /*!
         *  \param ScheduleProblemInterface definition of the problem to solve
         */
        public void CalculateSchedule(ScheduleProblemInterface problem)
        {
            objective = problem.getObjectiveFunction();
            set       = problem.getContactWindows();
            //Sort by time
            set.sort(Structs.sortByField.TIME);

            nrOfContacts   = set.Count();
            nrOfStation    = set.getNumberOfStation();
            nrOfSatellites = set.getNumberOfSatellites();
            satelliteList  = new List <string>(set.getSatelliteNames());
            stationList    = new List <string>(set.getStationNames());

            fitness      = new double[popSize];
            bestSolution = new int[nrOfContacts];

            if (runUnitlTime)
            {
                startrunTime = DateTime.Now;
                double h = hours;
                startrunTime = startrunTime.AddHours(h);
            }
            generation = 0;

            //create 5-10 random schedules from the Data
            population = new List <int[]>();
            for (int i = 0; i < popSize; i++)
            {
                population.Add(new int[nrOfContacts]);
                fitness[i] = Constants.maxInt;
            }
            //Randomize the starting Population
            Random rnd = new Random();

            for (int k = 0; k < popSize; k++)
            {
                for (int i = 0; i < nrOfContacts; i++)
                {
                    int randStart = rnd.Next(0, 100);
                    if (randStart <= genCrea)
                    {
                        population[k][i] = 1;
                    }
                }
            }
            //--
            bool foundSolution = false;

            while (!foundSolution)
            {
                generation++;
                //Console.WriteLine("Generation: " + generation.ToString() );
                //elliminate all collsions either by using random chance or
                //by priority.
                //check fitness of each one (survival rate)
                for (int i = 0; i < popSize; i++)
                {
                    surviveConflicts(population[i], rnd, conflictValue);
                    fitness[i] = checkFitness(population[i]);
                }

                if (Properties.Settings.Default.global_MaxPerf == false)
                {
                    System.Windows.Forms.Application.DoEvents();
                }

                //take only the fittest
                survivalOfFittest(fitness, population);

                //Combine randomly and generate children
                createChildren(population, fitness, rnd);


                //Mutate (remove or add requests by slight change)
                for (int i = 0; i < popSize; i++)
                {
                    mutate(population[i], rnd);
                }

                //check if Solution has been found
                foundSolution = isComplete();

                if (generatePlotData > 0)
                {
                    WriteLog();
                }

                if (Properties.Settings.Default.global_MaxPerf == false)
                {
                    System.Windows.Forms.Application.DoEvents();
                }

                if (f != null)
                {
                    f.incrementProgressBar();
                }
            }
            if (solveConflict)
            {
                surviveConflicts(bestSolution, rnd, conflictValue);
            }
            //

            //surviveConflicts(bestSolution, rnd);
            setSchedule(bestSolution, set);
            Console.WriteLine("Found Solution after " + generation.ToString() + " Generations");
            result = set;
            result.randomize();
            fillGaps();
            if (generatePlotData > 0)
            {
                plotWr.Close();
            }
            if (f != null)
            {
                f.resetProgressBar();
            }
        }