Example #1
0
        private void button2_Click(object sender, EventArgs e)
        {
            List <Node> nodeselected = new List <Node>();

            nodeselected.Add(CurrentGraph.GetNodeById(0));

            //foreach (ListViewItem item in listView1.SelectedItems) {
            //    var node = (Node)item.Tag;
            //    nodeselected.Add(CurrentGraph.GetNodeById(node.GetId()));
            //}

            foreach (ListViewItem item in listView1.SelectedItems)
            {
                var node = (Node)item.Tag;
                foreach (var item2 in CurrentGraph.Nodes)
                {
                    if (item2.Value.Name == node.Name)
                    {
                        nodeselected.Add(CurrentGraph.GetNodeById(item2.Value.GetId()));
                    }
                }
            }

            Queue <Node> nodeFila = new Queue <Node>();

            nodeFila.Enqueue(CurrentGraph.GetNodeById(0));
            Node          nodePai        = null;
            Node          inicio         = null;
            Node          fim            = null;
            List <int>    visited        = new List <int>();
            List <string> visitedProf    = new List <string>();
            double        distancia      = double.MaxValue;
            double        distanciaAux   = 0;
            double        distanciaTotal = 0;
            var           flag           = 0;

            while (nodeFila.Count > 0 && visited.Count <= nodeselected.Count)
            {
                nodePai = nodeFila.Dequeue();
                foreach (var item in nodeselected)
                {
                    if (!visitedProf.Contains(item.profissao) && nodePai.profissao != item.profissao && nodePai != null && nodePai.Name != item.Name && !visited.Contains(nodePai.GetId()) && !visited.Contains(item.GetId()) && CurrentGraph.GetNodeById(nodePai.GetId()).AdjacencyList[CurrentGraph.GetNodeById(item.GetId())].Cost <= distancia)
                    {
                        distancia = CurrentGraph.GetNodeById(nodePai.GetId()).AdjacencyList[CurrentGraph.GetNodeById(item.GetId())].Cost;
                        if (double.Parse(textBox1.Text) >= distancia)
                        {
                            distanciaAux += distancia;
                            if (distanciaAux <= double.Parse(textBox1.Text) && !visitedProf.Contains(item.profissao))
                            {
                                inicio = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[nodePai.GetId()]];
                                fim    = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[item.GetId()]];
                                Steps  = CurrentGraph.Dijkstra(inicio, fim);
                            }
                        }
                    }
                }
                nodeFila.Enqueue(fim);
                if (inicio != null)
                {
                    if (!visitedProf.Contains(fim.profissao))
                    {
                        distanciaTotal += inicio.AdjacencyList[fim].Cost;
                    }
                    flag = 0;
                    visited.Add(inicio.GetId());
                    visitedProf.Add(fim.profissao);
                    distancia    = double.MaxValue;
                    CurrentStep  = Steps.Count - 1;
                    CurrentGraph = Steps[CurrentStep];
                    RefreshGraphDraw();
                }
                else
                {
                    MessageBox.Show("Não existe nenhum serviço nas proximidades");
                    return;
                }
            }
            if (inicio != null)
            {
                inicio       = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[nodePai.GetId()]];
                fim          = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[0]];
                Steps        = CurrentGraph.Dijkstra(inicio, fim);
                CurrentStep  = Steps.Count - 1;
                CurrentGraph = Steps[CurrentStep];
                RefreshGraphDraw();
            }
            CurrentGraph    = BaseGraph.Copy();
            distanciaTotal += inicio.AdjacencyList[fim].Cost;
            MessageBox.Show("Total Percorrido " + distanciaTotal.ToString("F") + " KM");
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            var filecontend = string.Empty;
            var file        = string.Empty;

            string[] trabalhadores;
            using (OpenFileDialog open = new OpenFileDialog())
            {
                open.InitialDirectory = Path.Combine(Application.StartupPath);
                open.Filter           = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    file = open.FileName;
                    var filestream = open.OpenFile();
                    using (StreamReader reader = new StreamReader(filestream, Encoding.GetEncoding("iso-8859-1")))
                    {
                        filecontend = reader.ReadToEnd();
                    }
                }
            }
            filecontend   = filecontend.Replace("\r", "");
            trabalhadores = filecontend.Split(',', ';', '\n');
            for (int i = 0; i < trabalhadores.Length; i = i + 5)
            {
                trabalhadores[i + 1] = trabalhadores[i + 1].Replace('.', ',');
                trabalhadores[i + 2] = trabalhadores[i + 2].Replace(".", ",");
            }
            CurrentGraph.AddNode("julio", "-15,836073", "-47,912019", "estudante", true);
            for (int i = 0; i < trabalhadores.Length; i = i + 5)
            {
                if (bool.Parse(trabalhadores[i + 4]) == true)
                {
                    CurrentGraph.AddNode(trabalhadores[i], trabalhadores[i + 1], trabalhadores[i + 2], trabalhadores[i + 3], bool.Parse(trabalhadores[i + 4]));
                }
            }
            for (int i = 0; i < CurrentGraph.Nodes.Count; i++)
            {
                for (int j = 0; j < CurrentGraph.Nodes.Count; j++)
                {
                    if (i != j)
                    {
                        CurrentGraph.AddAdjacency(CurrentGraph.GetNodeById(i), CurrentGraph.GetNodeById(j), converterCord(CurrentGraph.GetNodeById(i).latitude, CurrentGraph.GetNodeById(j).latitude, CurrentGraph.GetNodeById(i).longitude, CurrentGraph.GetNodeById(j).longitude));
                    }
                }
            }
            listView1.Columns.Add("Nome", 121);
            listView1.Columns.Add("Profissão", 121);
            List <string> Adicionado = new List <string>();

            for (int i = 1; i < CurrentGraph.Nodes.Count; i++)
            {
                var item1 = new ListViewItem(new[] { CurrentGraph.GetNodeById(i).Name, CurrentGraph.GetNodeById(i).profissao });
                //var item1 = new ListViewItem(new[] { CurrentGraph.GetNodeById(i).profissao });
                item1.Tag = CurrentGraph.GetNodeById(i);
                var node = (Node)item1.Tag;
                //if (!Adicionado.Contains(node.profissao)) {
                listView1.Items.Add(item1);
                //Adicionado.Add(node.profissao);
                //}
            }
            BaseGraph = CurrentGraph.Copy();
            RefreshGraphDraw();
        }