Пример #1
0
        public int FindShortestDistanceBetween(char start, char finish)
        {
            if (!NodeMap.ContainsKey(start) || !NodeMap.ContainsKey(finish))
            {
                return(-1);
            }

            var startNode  = NodeMap[start];
            var finishNode = NodeMap[finish];

            var results = DijkstraAlgorithm.Dijkstra(this, startNode, finishNode);

            return(results[finish]);
        }
Пример #2
0
        private void buttonKruskal_Click(object sender, EventArgs e)
        {
            Stopwatch t = new Stopwatch();

            t.Start();
            var p = KruskalAlgorithm.Kruskal(graph);

            t.Stop();

            Graph dist = new Graph();

            foreach (var x in graph.Nodes)
            {
                dist.AddNode(x);
            }

            double cost = 0;

            for (int i = 0; i < p.Length / 2; i++)
            {
                foreach (var x in graph.Edges)
                {
                    if (x.From.Id == p[i, 0] && x.To.Id == p[i, 1] || x.From.Id == p[i, 1] && x.To.Id == p[i, 0])
                    {
                        x.IsMinE = true;
                        cost    += x.Weight;
                        dist.AddEdge(x);
                    }
                }
            }


            cost = 0;
            var distRez = DijkstraAlgorithm.Dijkstra(GraphRepresentation.toWeightMatrix(dist), 0, dist.Nodes.Count);

            textBoxResult.Text = "Расстояние в метрах от главного коммутатора до: \n\r";

            for (int i = 1; i < dist.Nodes.Count; i++)
            {
                textBoxResult.Text += "Коммутатора " + i + " = " + distRez[i] + " м." + Environment.NewLine;
                cost += distRez[i];
            }


            labelCost.Text = "Итоговая стоимость (р) " + cost * Convert.ToDouble(textBoxCost.Text);
            labelTime.Text = "Время в тиках " + t.ElapsedTicks;

            Repaint();
        }
        private void Run(object p)
        {
            Hashtable para       = p as Hashtable;
            var       wait       = para["wait"] as ProgressWait;
            string    folderPath = para["folderPath"].ToString();
            int       totalCount = Cities.Count(item => item.IsSelected);
            int       count      = 0;

            try
            {
                wait.SetWaitCaption("计算高铁城市空间可达性");
                foreach (string city in _dijkstra.GetCityEnumerator())
                {
                    wait.SetProgress((double)count++ / _dijkstra.Count);
                    _highTrainStation.Add(city, CalculationCity(city));
                }
                Dictionary <string, RasterOp> backup = new Dictionary <string, RasterOp>(_highTrainStation.Count);
                //backup
                foreach (var keyValue in _highTrainStation)
                {
                    backup.Add(keyValue.Key, new RasterOp(keyValue.Value));
                }
                //***********************************
                wait.SetProgress(0);
                wait.SetWaitCaption("计算高铁城市叠加效应");
                count = 0;
                foreach (string city in _dijkstra.GetCityEnumerator())
                {
                    wait.SetProgress((double)count++ / _dijkstra.Count);
                    float[] times = _dijkstra.Dijkstra(city);
                    foreach (var otherCity in _dijkstra.GetCityEnumerator())
                    {
                        if (city != otherCity)
                        {
                            int cityIndex = _dijkstra.GetCityIndex(otherCity);
                            backup[otherCity].Overlay(item => item + times[cityIndex]);
                            _highTrainStation[city].Overlay(backup[otherCity], Math.Min);
                            backup[otherCity].Overlay(item => item - times[cityIndex]);
                        }
                    }
                }
                //****************************************
                backup.Clear();
                //foreach (var keyValue in _highTrainStation)
                //{
                //    backup.Add(keyValue.Key, new RasterOp(keyValue.Value));
                //}
                wait.SetWaitCaption("计算所有城市空间可达性");
                wait.SetProgress(0);
                count = 0;
                foreach (var calculatorCity in Cities)
                {
                    if (calculatorCity.IsSelected)
                    {
                        wait.SetProgress((double)count++ / totalCount);
                        RasterOp res;
                        if (_highTrainStation.ContainsKey(calculatorCity.Name))
                        {
                            res = _highTrainStation[calculatorCity.Name];
                        }
                        else
                        {
                            res = CalculationCity(calculatorCity.Name);
                            RasterOp back = res.Clone();

                            foreach (var station in _highTrainStation)
                            {
                                //RasterOp op = back.Clone();
                                City    city     = _allCities.First(item => item.Name == station.Key);
                                Postion pos      = _rasterReader.Coordinate(city.XCoord, city.YCoord);
                                float   timecost = (float)back.Read(pos.XIndex, pos.YIndex);
                                _highTrainStation[station.Key].Overlay(item => item + timecost);
                                res.Overlay(_highTrainStation[station.Key], Math.Min);
                                _highTrainStation[station.Key].Overlay(item => item - timecost);
                            }
                        }
                        RasterWriter writer
                            = new RasterWriter(folderPath, calculatorCity.Name + "_高铁通车后", _rasterReader.RasterInfo);
                        res.WriteRaster(writer, "TIFF");
                    }
                }
                para["ret"] = true;
            }
            catch (Exception e)
            {
                _log.Error(e.Message + e.StackTrace);
                para["ret"] = false;
            }
            finally
            {
                wait.CloseWait();
            }
        }