コード例 #1
0
        private bool checkStopCriteria()
        {
            CTour bestGlobalTour = CIterationList.getInstance().getBestGlobalTour();

            // haben wir eine bessere Lösung als die Optimale Lösung gefunden?
            // dann können wir abbrechen
            if (mOptTour != null)
            {
                if (bestGlobalTour != null)
                {
                    if ((bestGlobalTour.getTourLength() <= mOptTour.getTourLength()) && (mAlgorithmParam.bBestTourStop == true))
                    {
                        return(true);
                    }
                }
            }

            // Haben wir den Schwellenwert unterschritten?
            if (bestGlobalTour != null)
            {
                if ((bestGlobalTour.getTourLength() <= mAlgorithmParam.iLimitStop) && (mAlgorithmParam.bLimitStop == true))
                {
                    return(true);
                }
            }
            return(false);
        }
コード例 #2
0
        private void drawTour(CTour tour, float depth, float red, float green, float blue, bool specialColor = false)
        {
            if (tour == null)
            {
                return;
            }

            Gl.glLineWidth(3f);
            Gl.glColor3f(red, green, blue);

            Gl.glBegin(Gl.GL_LINE_STRIP);



            for (int pointIndex = 0; pointIndex < tour.getListLength(); pointIndex++)
            {
                if (specialColor == true)
                {
                    float color = (float)pointIndex / (float)tour.getListLength();
                    Gl.glColor3f(color, color, 0.5f);
                }

                CTSPPoint point = tour.getPoint(pointIndex);
                Gl.glVertex3f(point.x, point.y, depth);
            }
            Gl.glEnd();
        }
コード例 #3
0
        private void drawBestPaths()
        {
            CIterationList iterationList = CIterationList.getInstance();

            if (mDraw.optPath)
            {
                CTour optimumTour = CAntAlgorithmParameters.getInstance().optTour;
                drawTour(optimumTour, OPT_TOUR_DRAW_LAYER, 0f, 0f, 1f);
            }

            if (mDraw.bestPathOfIteration)
            {
                CTour bestIterationTour = iterationList.getBestLastIterationTour();
                drawTour(bestIterationTour, BEST_GLOBAL_TOUR_DRAW_LAYER, 0f, 1f, 0f);
            }

            if (mDraw.bestPathOfAllIterations)
            {
                CTour bestGlobalTour = iterationList.getBestGlobalTour();
                drawTour(bestGlobalTour, BEST_ITERATION_TOUR_DRAW_LAYER, 1f, 0f, 0f);
            }

#if DEBUG
            if (debugTour != null)
            {
                drawTour(debugTour, OPT_TOUR_DRAW_LAYER - 0.5f, 1f, 1f, 0f, true);
            }
#endif
        }
コード例 #4
0
        private string handleTourSection(StreamReader reader)
        {
            String actualLine = reader.ReadLine();

            if (mFileHeader.dimension == 0)
            {
                mFileHeader.dimension = Int32.MaxValue;
            }

            // Die Anzahl der Knoten in der Datei muss mit der Anzahl der Einträge in der Punktliste übereinstimmen.
            // Sonst greifen wir eventuell noch auf Punkte zu die es nicht gibt.
            if (mFileHeader.dimension != CTSPPointList.getInstance().length())
            {
                throw new Exception("Tour-Datei ist nicht kompatibel mit der geladenen TSP-Datei!");
            }

            CTour optTour = new CTour();

            int i = 1;

            while ((!(actualLine == "-1")) && (i <= mFileHeader.dimension))
            {
                actualLine = actualLine.Trim();

                // Punkt suchen
                CTSPPoint point = CTSPPointList.getInstance().getPoint(actualLine);

                if (point != null)
                {
                    // Punkt in Tour einfügen
                    optTour.addPoint(point);
                }
                else
                {
                    throw new Exception("Fehler beim auslesen der Optimalen Tour!");
                }

                actualLine = reader.ReadLine();
                i++;
            }

            // prüfen ob die Tour ein Kreis ist (erstes == letztes Element)
            if (optTour.getPoint(0) != optTour.getPoint(optTour.getListLength() - 1))
            {
                // Nein, also den Kreis herstellen
                optTour.addPoint(optTour.getPoint(0));
            }

            // Tour abspeichern
            CAntAlgorithmParameters.getInstance().optTour = optTour;

            return(actualLine);
        }
コード例 #5
0
        public void NewIteration()
        {
            CreateNewAnts();

            List <Thread> antThreadList = new List <Thread>();

            // Ameisen laufen lassen
            foreach (CAnt ant in arrayOfAnts)
            {
                Thread antThread = new Thread(new ParameterizedThreadStart(this.handleAnt));
                antThreadList.Add(antThread);

                antThread.Name     = "AntThread" + antThreadList.Count.ToString();
                antThread.Priority = ThreadPriority.Highest;

                antThread.Start(ant);
            }

            // Prüfen ob alle Threads fertig sind
            bool bAllThreadsFinished = false;

            while (bAllThreadsFinished == false)
            {
                Thread.Sleep(1);
                bAllThreadsFinished = true;

                foreach (Thread antThread in antThreadList)
                {
                    if (antThread.ThreadState == System.Threading.ThreadState.Running)
                    {
                        bAllThreadsFinished = false;
                    }
                }
            }

            // kürzeste Tour ermitteln und die durchschnittliche Länge
            CTour  shortestTour      = new CTour();
            double averageTourLength = 0;

            foreach (CAnt ant in arrayOfAnts)
            {
                averageTourLength += ant.GetTour().getTourLength() / arrayOfAnts.Length;
                if ((ant.GetTour().getTourLength() < shortestTour.getTourLength()) || (shortestTour.getTourLength() == 0))
                {
                    shortestTour = ant.GetTour();
                }
            }

            Debug.WriteLine(shortestTour.getTourLength());

            // Iteration in die Liste zurückspeichern
            CIterationList.getInstance().Add(new CIteration(shortestTour, averageTourLength));
        }
コード例 #6
0
        public CAntAlgorithm(RenderWindow renderWindow, TextBox iterationTextbox)
        {
            mRenderWindow       = renderWindow;
            mItertationsTextbox = iterationTextbox;

            mMaxIterations     = mAlgorithmParam.numberMaxIterations;
            mPheromoneParam    = mAlgorithmParam.pheromoneParameter;
            mPhermomoneUpdate  = mAlgorithmParam.pheromoneUpdate;
            mInitialPheromone  = mAlgorithmParam.initialPheromone;
            mEvaporationFactor = mAlgorithmParam.evaporationFactor;
            mOptTour           = mAlgorithmParam.optTour;
        }
コード例 #7
0
 public CTour getBestGlobalTour()
 {
     if (mIterationList.Count > 0)
     {
         bestTour = mIterationList[0].ShortestTour;
         for (var i = 1; i < mIterationList.Count; i++)
         {
             var currentTour = mIterationList[i].ShortestTour;
             if (currentTour.getTourLength() < bestTour.getTourLength())
             {
                 bestTour = currentTour;
             }
         }
         return(bestTour);
     }
     return(null);
 }
コード例 #8
0
ファイル: CAnt.cs プロジェクト: artifact113/uni-tsp-ant
 public CAnt(CTSPPoint startPoint)
 {
     tour          = new CTour();
     PointsToVisit = CTSPPointList.getInstance().copy();
     CurrentPoint  = startPoint;
 }
コード例 #9
0
 public CIteration(CTour shortestTour, double averageTourLength)
 {
     _averageTourLength = averageTourLength;
     _shortestTour      = shortestTour;
 }
コード例 #10
0
        public void startAlgorithm()
        {
            CProgressManager.setStepsIteration(mMaxIterations);

            mConnectionList.SetInitialPheromone(mInitialPheromone);

            mItertationsTextbox.Invoke(new Action(delegate()
            {
                mItertationsTextbox.Text = "0/" + mMaxIterations;
            }));

            DateTime startTime = DateTime.Now;

            int  iteration      = 0;
            bool bStopAlgorithm = false;

            while ((iteration < mMaxIterations) && (bStopAlgorithm == false))
            {
                NewIteration();

                // Evaporation (verdunstung)
                //--------------------------------
                // Dazu iterieren wir durch alle Verbindungen und lassen das Pheromon verdunsten
                foreach (CConnection connection in mConnectionList)
                {
                    connection.evaporate(mEvaporationFactor);
                }

                // Pheromon-Update
                //--------------------------------
                // Dazu durch alle Touren von allen Ameisen nachgehen und für jede Verbindung die
                // Pheromonwerte neu berechnen
                // Formel: delta(t ij) = Q / (distance ij)
                foreach (CAnt ant in arrayOfAnts)
                {
                    CTour antTour = ant.GetTour();

                    // wir fangen mit Index 1 an! Damit die Punkte der Verbindungen mit den Indizies
                    // index -1 und index geholt werden können
                    for (int pointIndex = 1; pointIndex < antTour.getListLength(); pointIndex++)
                    {
                        CTSPPoint fromPoint = antTour.getPoint(pointIndex - 1);
                        CTSPPoint toPoint   = antTour.getPoint(pointIndex);

                        CConnection tourConnection = mConnectionList.getConnection(fromPoint, toPoint);
                        tourConnection.addPheromone(mPhermomoneUpdate);
                    }
                }

                CIterationList.getInstance().refreshStatisticNumbers();
                mRenderWindow.Invoke(mRenderWindow.refreshDelegate);
                CProgressManager.stepDone();

                mItertationsTextbox.Invoke(new Action(delegate()
                {
                    mItertationsTextbox.Text = (iteration + 1) + "/" + mMaxIterations;
                }));

                Debug.WriteLine("Iteration done: " + (iteration + 1));


                // Stopkriterien testen
                bStopAlgorithm = checkStopCriteria();

                // Iterationszähler erhöhen
                iteration++;
            }

            Debug.WriteLine("Dauer: " + (DateTime.Now - startTime).TotalSeconds + " sek");
        }