Beispiel #1
0
        //! Calculates a schedule from the defined problem

        /*!
         *  \pram ScheduleProblemInterface defined problem with contactwindows
         *  This Function will calculate the solution to the problem defined in
         *  Schedule Problem Interface
         */
        public void CalculateSchedule(ScheduleProblemInterface problem)
        {
            objective = problem.getObjectiveFunction();
            result    = problem.getContactWindows();

            if (adaptiveMaxIterations)
            {
                maxNumberOfIteration = result.Count() * 4;
            }

            if (randomStart)
            {
                result.randomize();
                fillContacts(result);
            }

            if (mainform != null)
            {
                mainform.setProgressBar(maxNumberOfIteration);
            }

            currentFitness = 0.0;
            result.getAt(0).setSheduled();
            while (!isComplete())
            {
                for (int i = 0; i < result.Count(); i++)
                {
                    iterations++;
                    for (int j = 0; j < result.Count(); j++)
                    {
                        if (i != j && result.getAt(i).checkConflikt(result.getAt(j)))
                        {
                            if (result.getAt(i).getStationName() == result.getAt(j).getStationName() ||
                                result.getAt(i).getSatName() == result.getAt(j).getSatName())
                            {
                                //collision detected
                                result.getAt(i).unShedule();
                                result.getAt(j).setSheduled();
                                double newFitness = getFitness(result);
                                if (newFitness < currentFitness)
                                {
                                    result.getAt(j).unShedule();
                                    result.getAt(i).setSheduled();
                                }
                                else
                                {
                                    currentFitness = newFitness;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (Properties.Settings.Default.global_MaxPerf == false)
                {
                    System.Windows.Forms.Application.DoEvents();
                }
                if (mainform != null)
                {
                    mainform.updateProgressBar(iterations);
                }

                currentFitness = getFitness(result);
                fillContacts(result);
            }
        }
        //! 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();
            }
        }