コード例 #1
0
ファイル: OutputWriter.cs プロジェクト: artifact113/ants-tsp
 public static void Write(OutputData outputData, string path)
 {
     try
     {
         XmlSerializer serializer = new XmlSerializer(typeof(OutputData));
         FileStream    stream     = new FileStream(@path, FileMode.Create);
         serializer.Serialize(stream, outputData);
         stream.Close();
     }
     catch (Exception ex)
     {
         ex.ToString();
     }
 }
コード例 #2
0
ファイル: FUeberwachung.cs プロジェクト: artifact113/ants-tsp
 private void tODOErgsSpeichernToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if (_algorithm != null)
     {
         SaveFileDialog sfd = new SaveFileDialog();
         sfd.Title  = "Speichern ...";
         sfd.Filter = sfd.Filter = "xml files (*.xml)|*.xml";
         if (sfd.ShowDialog() == DialogResult.OK)
         {
             string path = sfd.FileName;
             _outputData = _algorithm.GetOutputData();
             OutputWriter.Write(_outputData, sfd.FileName);
         }
     }
     else
     {
         MessageBox.Show(this, "Noch keine Ausgabedaten vorhanden", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
 }
コード例 #3
0
ファイル: Algorithm.cs プロジェクト: artifact113/ants-tsp
        private void TryToSolveTSP()
        {
            //Zeichenform akzeptiert keine Mausklicks mehr
            _drawForm.Invoke(new FormDisablementInvoker(_drawForm.IsAlgoRunning), true);

            for (int iter = 0; iter < _iterCount; iter++)
            {
                //Überprüfen der Abbruchkriterien
                if (_bestTourGloabl <= _optTourLength)
                {
                    _owner.Invoke(new StopTimerInvoker(_owner.StopTimer));
                    MessageBox.Show("Optimale Tour gefunden");
                    return;
                }
                if (_bestTourGloabl <= _minTourLength)
                {
                    _owner.Invoke(new StopTimerInvoker(_owner.StopTimer));
                    MessageBox.Show("Gewüsnchte Tour gefunden");
                    return;
                }

                //Werte der GUI und Bestwerte der Iteration aktualisieren
                _owner.Invoke(new IterCountInvoker(_owner.SetNumberOfIters), iter + 1);
                _owner.Invoke(new OutputWindowInvoker(_owner.SetBestTourIter), "0");
                _owner.Invoke(new OutputWindowInvoker(_owner.SetAVRIter), "0");
                _bestTourIter = double.PositiveInfinity;
                _avrTourIter  = 0;

                List <City> currentWay = new List <City>();
                //für jede Ameise
                for (int iterAnt = 0; iterAnt < _antList.Count; iterAnt++)
                {
                    if (_stopNow)
                    {
                        return;
                    }

                    Ant       ant = _antList[iterAnt];
                    ArrayList currentWayAsArray = new ArrayList();
                    _owner.Invoke(new AntCountInvoker(_owner.SetNumberOfAnts), iterAnt + 1);

                    while (_antList[iterAnt].cityList.Count > 0)
                    {
                        //enthält den Wert der attraktivsten Strecke
                        double highestLikeliness = .0;
                        //enthält den Key zu der Stadt, die auf der attraktivsten Strecke liegt
                        int keyToHighestLikeliness = -1;

                        //für jede noch nicht besuchte Stadt der Ameise die likeliness berechnen
                        foreach (City cityToGo in _antList[iterAnt].cityList.Values)
                        {
                            // Nenner der Formel
                            double sum            = .0;
                            int    smallForNenner = -1;
                            int    bigForNenner   = -1;

                            //Berechnung des Nenners
                            foreach (City c in _antList[iterAnt].cityList.Values)
                            {
                                smallForNenner = c.key;
                                bigForNenner   = _antList[iterAnt].city;
                                CheckIndices(ref smallForNenner, ref bigForNenner);

                                sum += Math.Pow(_cityList[smallForNenner][bigForNenner].phero, _alpha)
                                       * Math.Pow(_cityList[smallForNenner][bigForNenner].atractivity, _beta);
                                if (sum == 0)
                                {
                                }
                            }
                            int small = cityToGo.key;
                            int big   = _antList[iterAnt].city;
                            CheckIndices(ref small, ref big);

                            double likeliness = (Math.Pow(_cityList[small][big].phero, _alpha)
                                                 * Math.Pow(_cityList[small][big].atractivity, _beta))
                                                / (sum);

                            if (!((Double)likeliness).Equals(Double.NaN))
                            {
                                if (highestLikeliness <= likeliness)
                                {
                                    highestLikeliness      = likeliness;
                                    keyToHighestLikeliness = cityToGo.key;
                                }
                            }
                            else
                            {
                                keyToHighestLikeliness = _antList[iterAnt].cityList.Keys[0];
                            }
                        }//Ende der Auswahl der nächsten Stadt

                        // Update walked distance
                        if (keyToHighestLikeliness >= 0)
                        {
                            int keyFrom = _antList[iterAnt].city;
                            int keyTo   = keyToHighestLikeliness;
                            CheckIndices(ref keyFrom, ref keyTo);
                            double dist = _cityList[keyFrom][keyTo].distance;

                            // Ameise in neue Stadt setzen und neue Stadt aus Ameisenstädte löschen
                            // distance addieren
                            ant.walkedDistance += dist;
                            ant.city            = keyToHighestLikeliness;
                            currentWay.Add(_cityList[keyFrom][keyTo]);
                            ant.DeleteCityFromList(keyToHighestLikeliness);
                            _antList[iterAnt] = ant;

                            int x = _tsp.Koords[keyToHighestLikeliness].X;
                            int y = _tsp.Koords[keyToHighestLikeliness].Y;
                            currentWayAsArray.Add(new Point(x, y));
                        }
                    }//Ameise ist alle Städte einmal durchgelaufen
                    int smallCity = ant.city;
                    int bigCity   = ant.firstCity;
                    CheckIndices(ref smallCity, ref bigCity);
                    double newDist = _cityList[smallCity][bigCity].distance;
                    ant.walkedDistance += newDist;
                    ant.city            = ant.firstCity;
                    int x2 = _tsp.Koords[ant.city].X;
                    int y2 = _tsp.Koords[ant.city].Y;
                    currentWayAsArray.Add(new Point(x2, y2));

                    _antList[iterAnt] = ant;;

                    // GUI aktualisieren
                    if (_antList[iterAnt].walkedDistance < _bestTourGloabl)
                    {
                        _bestWay = currentWayAsArray;
                    }

                    UpdateLength(iterAnt, iter);
                    _drawForm.Invoke(new UpdateWayByAntInvoker(_drawForm.ShowCurrentWay), currentWayAsArray, _bestWay);

                    //Pheromonupdate ausführen
                    for (int row = 1; row <= _cityList.Count; row++)
                    {
                        for (int col = row + 1; col < _cityList[row].Values.Count; col++)
                        {
                            City city = _cityList[row][col];

                            double deltaTau = .0;
                            if (currentWay.Contains(city))
                            {
                                deltaTau = _q / ant.walkedDistance;
                            }

                            city.phero = (1 - _rho) *
                                         city.phero + deltaTau;

                            _cityList[row][col] = city;
                        }
                    }
                }//Alle Ameisen sind für diese Iteration durchgelaufen

                InitAntsAfterIter();
            }//Alle Iterationen sind beendet

            _owner.Invoke(new StopTimerInvoker(_owner.StopTimer));
            MessageBox.Show("Alle Iterationen durchlaufen");
            _drawForm.Invoke(new FormDisablementInvoker(_drawForm.IsAlgoRunning), false);
            _outputData = new OutputData(_tau, _q, _rho, _alpha, _beta, _antCount, _iterCount,
                                         _bestWay, _bestTourGloabl, _avrTourGloabl, _owner);
        }