bungatepijalan Moe Princess
Posts : 1487 Thanked : 30 Engine : Multi-Engine User Skill : Intermediate Type : Developer
Trophies
Awards: | Subyek: [GML] Dijkstra's Algorithm (Beta) 2010-10-24, 23:40 | |
| - Overview:
Algoritma Dijkstra adalah algoritma yang mencari suatu "jalan" terpendek menuju "titik" tujuan dari "titik" asal.
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 Script Algoritma Dijkstra versi bahasa Pascal: http://prodig.forumotion.net/pascal-f54/pascal-dijkstra-s-algorithm-t271.htm | |
|
Pi-Man Novice
Posts : 115 Thanked : 0 Engine : Other Skill : Intermediate Type : Developer
| Subyek: Re: [GML] Dijkstra's Algorithm (Beta) 2011-01-05, 12:42 | |
| Dikasih info juga, scriptnya untuk versi pro, atau bisa juga untuk lite
Pi-man | |
|
KID_VX Senior
Posts : 959 Thanked : 24 Engine : Multi-Engine User Skill : Very Beginner Type : Developer
| Subyek: Re: [GML] Dijkstra's Algorithm (Beta) 2011-01-06, 00:53 | |
| woh baru tau, jadi versi lite scriptnya di limit gitu ? daku kira plugin dan add on nya saja maklum bukan pengguna GM | |
|
bungatepijalan Moe Princess
Posts : 1487 Thanked : 30 Engine : Multi-Engine User Skill : Intermediate Type : Developer
Trophies
Awards: | Subyek: Re: [GML] Dijkstra's Algorithm (Beta) 2011-01-19, 09:39 | |
| Maap telat bgt ngepostnya Nah, ini scriptnya yang udah stable
- Script: hapus:
- Code:
-
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=nq-1; }
- Script: ketemu:
- Code:
-
ii=argument0 ff=false; for(iii=1;iii<=nq;iii+=1){ if(q[iii]==ii) ff=true; } return ff;
- Script: dijkstra:
- Code:
-
// dijkstra //where s is index of source vertex. // //Required data //M[i,j]: Distance from vertex i to vertex j
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;ii+=1){ if(vertices_dist[q[ii]]<d){ d=vertices_dist[q[ii]]; u=q[ii]; } } }
if(vertices_dist[u]==99999) break; hapus(u); for(i=1;i<=n;i+=1){ if(M[u,i]<99999 && M[u,i]>0){ if(ketemu(i)){ alt=vertices_dist[u]+M[u,i] if(alt<vertices_dist[i]){ vertices_dist[i]=alt; vertices_prev[i]=u; } } } } } 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 | |
|
Sponsored content
| Subyek: Re: [GML] Dijkstra's Algorithm (Beta) | |
| |
|