Exemplo n.º 1
0
 //browse
 private void button1_Click(object sender, EventArgs e)
 {
     comboBox2.Items.Clear();
     comboBox3.Items.Clear();
     //string directory;
     Microsoft.Msagl.Drawing.Graph grafVisualization = new Microsoft.Msagl.Drawing.Graph("graf");
     browse.Filter = "*.txt (file berekstensi txt) | *.txt";
     if (browse.ShowDialog() == DialogResult.OK)
     {
         int    i;
         string fileDirectory = browse.FileName;
         string filename      = browse.SafeFileName;
         input       = File.ReadAllLines(fileDirectory);
         label4.Text = filename;
         graf        = new Graf(input);
         //menambahkan simpul simpul pada comboBox
         for (i = 1; i <= graf.getGraphNode().Count; i++)
         {
             comboBox2.Items.Add(graf.getGraphNode()[i - 1].getNamaNode());
             comboBox3.Items.Add(graf.getGraphNode()[i - 1].getNamaNode());
         }
         //visualisasi graf awal
         grafVisualization = FrontEndUtility.grafVisualization(graf);
         gViewer1.Graph    = grafVisualization;
     }
 }
Exemplo n.º 2
0
        //submit
        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox2.Text != "awal" && comboBox3.Text != "tujuan")
            {
                label7.Text = "";
                //label4.Text = "Jalur";
                Microsoft.Msagl.Drawing.Graph grafVisualization = new Microsoft.Msagl.Drawing.Graph("graf");
                int i, j;
                // input simpul awal dan tujuan
                string awal = comboBox2.Text;
                string tujuan = comboBox3.Text;

                graf = new Graf(input);
                Node nodeAwal = graf.searchNode(awal);
                Node nodeTujuan = graf.searchNode(tujuan);

                if (!graf.searchNode(nodeAwal) || !graf.searchNode(nodeTujuan))
                {
                    //Console.WriteLine("Simpul tidak ada");
                }
                else
                {
                    //ALGORITMA A*
                    List <Edge> rute         = new List <Edge>();
                    List <Node> ruteNode     = new List <Node>();
                    Node        nodeAwalAlgo = nodeAwal;


                    j = 0;
                    while (nodeAwalAlgo != nodeTujuan) //selama nodeAwalAlgo bukan nodeTujuan
                    {
                        ruteNode.Add(nodeAwalAlgo);
                        Dictionary <Node, double> results = new Dictionary <Node, double>();
                        int jumlahEdges = nodeAwalAlgo.getEdges().Count;
                        i  = 0;
                        j += 1;
                        while (i < jumlahEdges)
                        {
                            //g(n) = distance dari nodeAwalAlgo ke n
                            double sumG = 0;
                            if (rute.Count > 0)
                            {
                                foreach (Edge edge in rute)
                                {
                                    sumG += edge.getBobot();
                                }
                            }
                            double g = nodeAwalAlgo.getEdges()[i].getBobot() + sumG;
                            //h(n) = straight line distance from n ke nodeTujuan
                            double h = Edge.euclideanDistance(nodeAwalAlgo.getEdges()[i].getNext().getKoordinatNode(), nodeTujuan.getKoordinatNode());
                            //f(n) = g(n) + h(n)
                            double f = g + h;
                            if (!ruteNode.Contains(nodeAwalAlgo.getEdges()[i].getNext()))
                            {
                                results.Add(nodeAwalAlgo.getEdges()[i].getNext(), f);
                            }

                            i += 1;
                        }

                        Node tempNodeAwal = nodeAwalAlgo;
                        if (results.Count > 0)
                        {
                            //ambil nilai f minimum, update nodeAwalAlgo
                            nodeAwalAlgo = results.OrderBy(KeyValuePair => KeyValuePair.Value).First().Key;
                            Edge edgeRute = new Edge(tempNodeAwal, nodeAwalAlgo);
                            rute.Add(edgeRute);
                        }
                        else
                        {
                            break;
                        }
                    }

                    //visualisasi graf hasil
                    grafVisualization = FrontEndUtility.colorizedGrafVisualization(graf, rute);
                    gViewer1.Graph    = grafVisualization;

                    //hitung jarak rute yang dihasilkan
                    double sumBobot = 0;
                    foreach (Edge edge in rute)
                    {
                        sumBobot += edge.getBobot() * 100000;
                    }
                    label7.Text = sumBobot.ToString() + " m";

                    //kasus tidak ada tujuan
                    if (nodeAwalAlgo != nodeTujuan)
                    {
                        grafVisualization = FrontEndUtility.grafVisualization(graf);
                        gViewer1.Graph    = grafVisualization;
                        label7.Text       = "Tidak ada jalur";
                    }
                }
            }
            else
            {
                label4.Text = "Tidak ada jalur";
            }
        }