Per 2016, RMID pindah ke RMID Discord (Invite link dihapus untuk mencegah spambot -Theo @ 2019). Posting sudah tidak bisa dilakukan lagi.
Mohon maaf atas ketidaknyamanannya dan mohon kerjasamanya.

Share | 
 

 [GML] Dijkstra's Algorithm (Beta)

Topik sebelumnya Topik selanjutnya Go down 
[GML] Dijkstra's Algorithm (Beta) Empty2010-10-24, 23:40
Post[GML] Dijkstra's Algorithm (Beta)
#1
bungatepijalan 
Moe Princess
bungatepijalan

Level 5
Posts : 1487
Thanked : 30
Engine : Multi-Engine User
Skill : Intermediate
Type : Developer
Awards:
[GML] Dijkstra's Algorithm (Beta) Vide
Overview:

Script: hapus
Code:
// hapus(ii)
//where ii is index of vertex to be removed from unvisited vertex array q.
 ii=argument0
 for(iii=1;iii<=nq;iii+=1)
  if(q[iii]==ii)
  iv=iii;
 if(iv<=nq){
  if(iv<nq){
  for(iii=iv;iii<=nq-1;iii+=1){
    q[iii]=q[iii+1]
  }
  }
  nq-=1
 }

Script: ketemu
Code:
// ketemu(ii)
//where ii is index of vertex to be searched in vertex array q.
 ii=argument0
 ff=false
 for(iii=1;iii<=nq;iii+=1){
  if(q[iii]==ii)
  ff=true
 }
 return ff

Script: dijkstra
Code:
// dijkstra(s)
//where s is index of source vertex.
//
//Required data
//vertices_dist[i]: Path distance from i to source s
//vertices_prev[i]: Previous path vertex from vertex i
//vertices_neighbors[i]: Number of neighbors of vertex i
//vertices_neighbor[i,j]: j-th neighbor vertex index of vertex i
//vertices_distb[i,j]: Distance between vertex i and j-th neighbor vertex of vertex i
 s=argument0
 for(i=1;i<=n;i+=1){
  vertices_dist[i]=99999
  vertices_prev[i]=0
  q[i]=i
  nq=n
 }
 vertices_dist[s]=0;
 while(nq>0){
 
  d=vertices_dist[q[1]]
  u=q[1]
  if(nq>1){
  for(ii=2;ii<=nq;i+=1){
  if(vertices_dist[q[ii]]<d){
    d=vertices_dist[q[ii]];
    u=q[ii];
  }
  }
  }

  if(vertices_dist[u]==99999) break;
  hapus(u);
  if(vertices_neighbors[u]>0){
  for(i=1;i<=vertices_neighbors[u];i+=1){
    v=vertices_neighbor[u,i]
    if(ketemu(v)){
    alt=vertices_dist[u]+vertices_distb[u,i]
    if(alt<vertices_dist[v]){
      vertices_dist[v]=alt;
      vertices_prev[v]=u;
    }
    }
  }
  }
 }

Fungsi: mencari "jalan" terpendek menuju "titik" tujuan dari "titik" asal

Cara pemasangan:
- Buat tiga script baru, kasi nama hapus, ketemu, dan dijkstra
- Copas masing-masing bagian code di atas ke masing2 script tersebut
- Implementasikan script-script tersebut ke action Execute a piece of code. Namun array-array (vertices_*) harus diinisialisasi datanya terlebih dahulu.

Created by: Bunga Tepi Jalan

Credits
- http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm

NB: Lihat konsep dan prinsip Algoritma Dijkstra di: http://ancuracademy.forumotion.net/programming-f13/programming-dijkstra-s-algorithm-t11.htm :kabur:
Script Algoritma Dijkstra versi bahasa Pascal: http://prodig.forumotion.net/pascal-f54/pascal-dijkstra-s-algorithm-t271.htm :kabur:
[GML] Dijkstra's Algorithm (Beta) Empty2011-01-05, 12:42
PostRe: [GML] Dijkstra's Algorithm (Beta)
#2
Pi-Man 
Novice
Novice
Pi-Man

Level 5
Posts : 115
Thanked : 0
Engine : Other
Skill : Intermediate
Type : Developer

[GML] Dijkstra's Algorithm (Beta) Vide
Dikasih info juga, scriptnya untuk versi pro, atau bisa juga untuk lite

Pi-man
[GML] Dijkstra's Algorithm (Beta) Empty2011-01-06, 00:53
PostRe: [GML] Dijkstra's Algorithm (Beta)
#3
KID_VX 
Senior
Senior
KID_VX

Level 5
Posts : 959
Thanked : 24
Engine : Multi-Engine User
Skill : Very Beginner
Type : Developer

[GML] Dijkstra's Algorithm (Beta) Vide
woh baru tau,
jadi versi lite scriptnya di limit gitu ? :o
daku kira plugin dan add on nya saja :swt:
maklum bukan pengguna GM
[GML] Dijkstra's Algorithm (Beta) Empty2011-01-19, 09:39
PostRe: [GML] Dijkstra's Algorithm (Beta)
#4
bungatepijalan 
Moe Princess
bungatepijalan

Level 5
Posts : 1487
Thanked : 30
Engine : Multi-Engine User
Skill : Intermediate
Type : Developer
Awards:
[GML] Dijkstra's Algorithm (Beta) Vide
Maap telat bgt ngepostnya :hammer:
Nah, ini scriptnya yang udah stable :kabur:
Script: hapus:
Script: ketemu:
Script: dijkstra:
Dan juga algoritma Dijkstra dalam script LPM (Listra Pathfinder Module)
Code:
/*
 Dijkstra's Algorithm


@> Function
 * To find a shortest path from starting vertex (s) to target vertex (t)
  and return path distance from s to t.

@> Required Data
 * n: number of grid vertices
 * s: index of source vertex
 * t: index of target vertex
 * global.nb[u,i]: i-th neighbor of grid vertex u, whose distance from u
                  is 1 (as vertex index)
 * global.tc[u,i]: terrain cost from u to global.nb[u,i]
                  (equal to 1 normally)
 * global.neighbors: maximum number of each vertices (usually 4 for
                    4-directional movement or 8 for 8-directional movement)

@> Required Scripts
 * remove(x): Removes x from array q (as priority queue)
 * add(x): Inserts x into array q (as priority queue)
 * u_nearest(): Returns index u of array q, where vertices_dist[u]
                is smallest

@> Usage Guide
 * For graph initialization, build a passage graph with script
  buildGraph().
 * Then use this script on a game character. Set its variables s as index of
  the target's position vertex and t as index of the character's
  position vertex.
 * After that, you can trace the character's path by using vertices_prev[t].
 * You can also use vertices_dist[t] or value returned by this script to
  show the character's distance to the target.

@> Credits
 * Bunga Tepi Jalan (bungatepijalan.wordpress.com)
 * Wikipedia

@> Notes
 * If you want to learn more about Dijkstra's algorithm, see the article at:
  http://en.wikipedia.org/wiki/Dijkstra's_algorithm
 * The path constructed by this algorithm is always the shortest.
 * This script is made based on pseudocode at that article, with
  improvement on efficiency.
*/

// Initializations
nq=0        // Create open list q (as priority queue)
add(s)      // Add s into open list q
for(i=1;i<=n;i+=1){
    vertices_dist[i]=9999 // Unknown distance function from source to v
    vertices_prev[i]=0    // Previous node in optimal path from source
}
vertices_dist[s]=0      // Distance from source to source
while(nq>0){            // The main loop
    //Finds vertex u with least value of path distance
    u_nearest()
    dist_u=vertices_dist[u]
    if(u==t){
        //If u is the target, search completed and returns its distance.
        //You can add statements here, usually to read/construct path
        return dist_u
    }
    remove(u)
    for(i=1;i<=global.neighbors;i+=1){
        v=global.nb[u,i]    // where v has not yet been removed from Q.
        if(v!=0){
            alt=dist_u+global.tc[u,i]
            if(alt<vertices_dist[v]){  // Relax (u,v)
                add(v)
                vertices_dist[v]=alt
                vertices_prev[v]=u
                buildpanah(v)
            }
        }
    }
}
//Statements below this will be executed if all remaining vertices
//are inaccessible from source. This means that target vertex is isolated
//from source vertex. Also means that vertices_dist[t]==9999.
//You can add them here, usually to alert that no path is found.
Don't forget to credit me if you want to use this ;)

@Pi-Man
Dari isi scriptnya, ya jelaslah bisa utk versi apapun :kabur:
[GML] Dijkstra's Algorithm (Beta) Empty
PostRe: [GML] Dijkstra's Algorithm (Beta)
#5
Sponsored content 




[GML] Dijkstra's Algorithm (Beta) Vide
 

[GML] Dijkstra's Algorithm (Beta)

Topik sebelumnya Topik selanjutnya Kembali Ke Atas 

Similar topics

+
Halaman 1 dari 1

Permissions in this forum:Anda tidak dapat menjawab topik
RPGMakerID :: Scripts & Event Systems :: Game Maker Scripts-