//! 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)
        {
            set      = problem.getContactWindows();
            schedule = new ContactWindowsVector();
            set.sort(Structs.sortByField.TIME);
            while (!isComplete())
            {
                for (int i = 0; i < set.Count(); i++)
                {
                    bool       collisionFound = false;
                    List <int> collisionSet   = new List <int>();
                    collisionSet.Add(i);
                    for (int j = 1; j < set.Count() - 1; j++)
                    {
                        collisionFound = checkCollision(set.getAt(i), set.getAt(j));
                        collisionSet.Add(j);
                    }
                    if (collisionFound)
                    {
                        set.getAt(i).setSheduled();
                        for (int k = 0; k < collisionSet.Count - 1; k++)
                        {
                            set.getAt(collisionSet[k]).unShedule();
                        }
                    }
                    else
                    {
                        schedule.add(set.getAt(i));
                        set.deleteAt(i);
                        i--;
                    }
                }
            }
            //retrive all the contactwindows that need to be scheduled
            //ContactWindowsVector set = problem.getContactWindows();
            //Scheduler Magic until is Complete returns true
            //No Element of the ContactWindowsVector set should be deleted
            //To Schedule a item call set.getAt(index).setSheduled()
            //To Unschedule a item call set.getAt(index).unShedule()
        }
Beispiel #2
0
        //! Draw Conatact Windows.

        /*!
         * \param ContactWindowsVector Contacts
         * \param bool Draw All if false only scheduled contacts will be drawn
         * \return Image bmp-Image
         * Creates a bmp Image and returns it
         */
        public static Image drawContacts(ContactWindowsVector contacts, bool drawAll)
        {
            if (contacts != null)
            {
                //sort by Groundstations
                contacts.sort(Structs.sortByField.GROUNDSTATION);

                List <string> satNameList = contacts.getSatelliteNames();
                List <string> staNameList = contacts.getStationNames();

                int x_offset     = 100;
                int y_offset     = 20;
                int satBoxheight = 20;

                //calculate Size of Image
                double timeLength = contacts.getStopTime().getEpoch() - contacts.getStartTime().getEpoch();
                int    imWidth    = Convert.ToInt32(timeLength = 8640 * timeLength);
                int    imHeight   = satNameList.Count() * staNameList.Count() * 20;
                Image  dest       = new Bitmap(imWidth + x_offset, imHeight + y_offset);

                //Generate Front and Brusch for drawing
                System.Drawing.Graphics g;
                System.Drawing.Pen      pen = new System.Drawing.Pen(Color.Black, 1F);
                Font       drawFont         = new Font("Arial", 10);
                SolidBrush brush            = new SolidBrush(Color.Black);
                g = Graphics.FromImage(dest);
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;

                //Start Drawing Contact Windows
                for (int i = 0; i < contacts.Count(); i++)
                {
                    //Draw only if scheduled unless drawAll = true
                    if (contacts.getAt(i).getSheduledInfo() || drawAll)
                    {
                        int satPosY = satNameList.IndexOf(contacts.getAt(i).getSatName());
                        int staPosY = staNameList.IndexOf(contacts.getAt(i).getStationName());

                        int y            = 0;
                        int x            = 0;
                        int satBoxhWidth = 0;

                        y = satPosY * 20 + staPosY * satNameList.Count() * 20 + y_offset;
                        double satx = 8640 * (contacts.getAt(i).getStartTime().getEpoch() - contacts.getStartTime().getEpoch());
                        x            = Convert.ToInt32(satx) + x_offset;
                        satBoxhWidth = Convert.ToInt32(contacts.getAt(i).getDuration() / 10.0);

                        //brush = new SolidBrush(Color.FromArgb(contacts.getAt(i).getHash()));
                        if (drawAll && !contacts.getAt(i).getSheduledInfo())
                        {
                            brush = new SolidBrush(Color.FromArgb(215, 215, 215));
                            g.FillRectangle(brush, new Rectangle(x, y, satBoxhWidth, satBoxheight));
                            g.DrawString(contacts.getAt(i).getSatName(), drawFont, new SolidBrush(Color.DarkGray), x, y);
                        }
                        else
                        {
                            brush = new SolidBrush(Color.FromArgb(contacts.getAt(i).getHash()));
                            g.FillRectangle(brush, new Rectangle(x, y, satBoxhWidth, satBoxheight));
                            g.DrawString(contacts.getAt(i).getSatName(), drawFont, new SolidBrush(Color.Black), x, y);
                        }
                    }
                }

                System.Drawing.Pen stationPen;
                stationPen = new System.Drawing.Pen(Color.DarkGray, 1);

                g.DrawLine(stationPen, x_offset, 0, x_offset, imHeight);

                //Start Drawing Stations Names
                for (int i = 0; i < staNameList.Count(); i++)
                {
                    int x1 = 0;
                    int y1 = (i + 1) * satNameList.Count() * 20;
                    int x2 = imWidth;
                    int y2 = (i + 1) * satNameList.Count() * 20;
                    g.DrawString(staNameList[i], drawFont, new SolidBrush(Color.Black), 5, y1 - ((satNameList.Count() / 2) * 20));
                    g.DrawLine(stationPen, x1, y1 + y_offset, x2, y2 + y_offset);
                }

                One_Sgp4.EpochTime time = new One_Sgp4.EpochTime(contacts.getStartTime());
                g.DrawString(contacts.getStartTime().ToString(), drawFont, new SolidBrush(Color.Black), x_offset, 5);

                for (int i = x_offset; i < imWidth; i += 180)
                {
                    g.DrawString(time.ToString(), drawFont, new SolidBrush(Color.Black), i, 5);
                    g.DrawLine(stationPen, i, 0, i, imHeight);
                    time.addTick(10 * 180);
                }

                //g.Dispose();
                drawFont.Dispose();
                brush.Dispose();
                pen.Dispose();

                dest.Save("Contacts.bmp");

                return(dest);
            }
            else
            {
                int   imWidth  = 8640;
                int   imHeight = 20;
                Image dest     = new Bitmap(imWidth, imHeight);
                return(dest);
            }
        }
        //! 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();
            }
        }