Ejemplo n.º 1
0
    public Results Perform(int start)
    {
        float[]   d = GetStartingTraversalCost(start);
        int[]     p = GetStartingBestPath(start);
        BasicHeap Q = new BasicHeap();

        debugText.text += "\n<color=#800000>Starting Traversal Cost from node 0</color>";

        for (int i = 0; i != TotalNodeCount; i++)
        {
            Q.Push(i, d [i]);

            if (d[i] != Mathf.Infinity)
            {
                debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>";
            }
            else
            {
                debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>";
            }
        }

        debugText.text += "\n<color=#800000>Start Main Loop</color>";

        while (Q.Count > 0)
        {
            int v = Q.Pop();

            debugText.text += "\n<color=blue>[---CEK NODE " + v + "---] \n> Remaining Q = " + Q.Count + "] : </color> ";

            for (int i = 0; i < Q.Count; i++)
            {
                debugText.text += "\nnode " + Q.getIndex(i) + " : ";
                debugText.text += "cost : " + Q.getWight(i);
            }

            debugText.text += "\n<color=blue>> Cek Nearby dari node " + v + " : </color>";

            foreach (int w in nodeConfigurator.GetNearbyNodes(v))
            {
                debugText.text += "\nnode " + v + " > node " + w;

                float cost = nodeConfigurator.getTraversalCost(v, w);
                nodeConfigurator.markCurrentPath(v, w);

                debugText.text += ", cost = " + cost;

                if (d[v] + cost < d[w])
                {
                    debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#660066>Lebih kecil dari cost yg sekarang = " + d[w] + "</color>";
                    d[w]            = d[v] + cost;
                    debugText.text += "\n> Set cost : dr node sebelumnya > " + v + " > " + w + " = " + d[w];
                    debugText.text += "\n> Set best path menuju " + w + " dari = " + p[w] + " menjadi = " + v;
                    p[w]            = v;
                    debugText.text += ">\nPush index = " + w + ", weight = " + d[w] + " ke Q untuk dicek selanjutnya";
                    Q.Push(w, d[w]);
                    debugText.text += "\njadi Q = " + Q.Count;
                }
                else if (d[v] + cost == d[w])
                {
                    debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang sama dari cost yg sekarang = " + d[w] + "</color>";
                }
                else if (d[v] + cost > d[w])
                {
                    debugText.text += "\nTravecost dari start > " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang lebih besar dari cost yg sekarang = " + d[w] + "</color>";
                }
            }
        }

        debugText.text += "\n<color=#800000>Node hasil perhitungan</color>";

        for (int i = 0; i < p.Length; i++)
        {
            debugText.text += "\nStart > node " + i + " melalui node " + p[i];
        }

        debugText.text += "\n<color=#800000>Distance dari start ke masing2 node</color>";

        for (int i = 0; i < d.Length; i++)
        {
            debugText.text += "\n> Start > " + i + " = " + d[i];
        }

        return(new Results(p, d));
    }
Ejemplo n.º 2
0
    //-------------method struct untuk menentukan hasil djikstra
    public Results Perform(int start)
    {
        //get starting travelcost
        float[] d = GetStartingTraversalCost(start);
        //set bestpath, set semua menjadi start, misal 0
        int[] p = GetStartingBestPath(start);
        // define basic heap
        BasicHeap Q = new BasicHeap();

        //tampilkan di log
        debugText.text += "\n<color=#800000>Starting Traversal Cost from node 0</color>";
        //jika i != total node
        for (int i = 0; i != TotalNodeCount; i++)
        {
            //masukkan nilai starting path & starting travesalcost kedalam heap
            Q.Push(i, d [i]);
            //tampilkan di log
            if (d[i] != Mathf.Infinity)
            {
                // neighbor dr starting node
                debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>";
            }
            else
            {
                //node yang belum terjamah
                debugText.text += "\n<color=#0000ff>node " + i + " = " + d[i] + "</color>";
            }
        }
        //tampilkan di debug
        debugText.text += "\n<color=#800000>Start Main Loop</color>";
        // jika Q (node yang harus di cek) lebih dari 0
        while (Q.Count > 0)
        {
            // buat variable v (visited / current node) dari nilai heap paling awal
            int v = Q.Pop();
            //tampilkan di log
            debugText.text += "\n<color=blue>[---CEK NODE " + v + "---] \n> Remaining Q = " + Q.Count + "] : </color> ";
            // tampilkan di log node dan traversalcost ned yang belum terjamah / yg harus di cek
            for (int i = 0; i < Q.Count; i++)
            {
                //tampilkan di log
                debugText.text += "\nnode " + Q.getIndex(i) + " : ";
                debugText.text += "cost : " + Q.getWight(i);
            }
            //tampilkan di log
            debugText.text += "\n<color=blue>> Cek Nearby dari node " + v + " : </color>";
            //untuk setiap neighbor dari node yang sedang di cek
            foreach (int w in nodeConfigurator.GetNearbyNodes(v))
            {
                //tampilkan di log
                debugText.text += "\nnode " + v + " > node " + w;
                //tentukan traversalcost dari current node ke neighbor
                float cost = nodeConfigurator.getTraversalCost(v, w);
                //tandai node yang telah dilalui
                nodeConfigurator.markCurrentPath(v, w);
                //tampilkan di log :
                debugText.text += ", cost = " + cost;
                //jika traversal cost V + cost ke neighbor yg sedang di cek 'w' < dari traversal cost sebelumnya
                if (d[v] + cost < d[w])
                {
                    //tampilkan di log
                    debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#660066>Lebih kecil dari cost yg sekarang = " + d[w] + "</color>";
                    //masukan nilai baru hasil pertambahan current ke neighbor yang di cek ke dalam travelcost yng sedang di cek
                    d[w] = d[v] + cost;
                    //tampilkan di log
                    debugText.text += "\n> Set cost : dr node sebelumnya > " + v + " > " + w + " = " + d[w];
                    debugText.text += "\n> Set best path menuju " + w + " dari = " + p[w] + " menjadi = " + v;
                    // set best path menjadi node yang di cek
                    p[w] = v;
                    //tampilkan di log
                    debugText.text += ">\nPush index = " + w + ", weight = " + d[w] + " ke Q untuk dicek selanjutnya";
                    //push node dan weightnya ke heap
                    Q.Push(w, d[w]);
                    //tampilkan di log :
                    debugText.text += "\njadi Q = " + Q.Count;
                    //jika perbandingan harganya sama
                }
                else if (d[v] + cost == d[w])
                {
                    //tampilkan di log :
                    debugText.text += "\nTravecost dari " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang sama dari cost yg sekarang = " + d[w] + "</color>";
                    //jika perbandingan harganya lebih besar
                }
                else if (d[v] + cost > d[w])
                {
                    //tampilkan di log :
                    debugText.text += "\nTravecost dari start > " + v + " > " + w + " = " + (d[v] + cost) + "\n<color=#006600>Harga yang lebih besar dari cost yg sekarang = " + d[w] + "</color>";
                }
            }
        }

        //tampilkan di log
        debugText.text += "\n<color=#800000>Node hasil perhitungan</color>";
        // cek masing2 nilai p sekarang
        for (int i = 0; i < p.Length; i++)
        {
            //tampilkan di log
            debugText.text += "\nStart > node " + i + " melalui node " + p[i];
        }
        //tampilkan di log :
        debugText.text += "\n<color=#800000>Distance dari start ke masing2 node</color>";
        //cek nilai d baru
        for (int i = 0; i < d.Length; i++)
        {
            //tampilkan di log :
            debugText.text += "\n> Start > " + i + " = " + d[i];
        }
        //return nilia p dan d
        return(new Results(p, d));
    }