nycto / astarnim Goto Github PK
View Code? Open in Web Editor NEWA* path finding algorithm in Nim
License: MIT License
A* path finding algorithm in Nim
License: MIT License
The example works for me, but when I try the following code, I get a "cannot instantiate path" error, and I really can't figure out why.
import strformat
import astar
const
Up* = (x: 0, y: 1, z: 0)
Down* = (x: 0, y: -1, z: 0)
West* = (x: -1, y: 0, z: 0)
East* = (x: 1, y: 0, z: 0)
South* = (x: 0, y: 0, z: 1)
North* = (x: 0, y: 0, z: -1)
directions* = [Up, Down, West, East, South, North]
type GNode* = object
x: int
y: int
z: int
type Grid* = seq[seq[seq[GNode]]]
proc `$`(g: Grid): string =
let h = g.len
let l = g[0].len
let w = g[0][0].len
result = "({w=}, {h=}, {l=})".fmt
proc `$`(gn: GNode): string = "({gn.x=}, {gn.y=}, {gn.z=})".fmt
proc newGrid(): Grid =
result = @[]
for y in 0..4:
var plane = newSeq[seq[GNode]]()
for z in 0..4:
var row = newSeq[Gnode]()
for x in 0..4:
let gnode = GNode(x: x, y: y, z: z)
row.add(gnode)
plane.add(row)
result.add(plane)
iterator neighbors*(g: Grid, n: GNode): Gnode =
for dir in directions:
let x = n.x + dir.x
let y = n.y + dir.y
let z = n.z + dir.z
let h = g.len
let l = g[0].len
let w = g[0][0].len
if (y in 0..<h):
if (z in 0..<l):
if (x in 0..<w):
yield g[y][z][x]
proc cost*(g: Grid, a, b: GNode): int = 1
proc heuristic*(g: Grid, next, goal: GNode): int = abs(goal.x - next.x) + abs(goal.y - next.y) + abs(goal.z - next.z)
when isMainModule:
let g: Grid = newGrid()
echo g
let start: GNode = g[0][0][0]
let goal: GNode = g[4][0][2]
for n in g.neighbors(start):
echo n, ", cost: ", g.cost(start, n), ", heuristic: ", g.heuristic(n, goal)
for p in astar.path[Grid, GNode, int](g, start, goal):
let gn: GNode = p
echo gn
nim c --run astar_tst.nim
Hint: used config file '/home/dvyal/tmp/nim-0.14.2/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: astar_tst [Processing]
Hint: astar [Processing]
Hint: binaryheap [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: etcpriv [Processing]
Hint: options [Processing]
Hint: typetraits [Processing]
astar_tst.nim(46, 18) Error: type mismatch: got (Grid) but expected 'Graph = astar'
% nim -v
Nim Compiler Version 0.14.2 (2016-06-11) [Linux: amd64]
Copyright (c) 2006-2016 by Andreas Rumpf
git hash: abf4e204c2f1a8a6915a8d32c970b5ce64f58e10
active boot switches: -d:release
I want to use this package but I cannot get the latest version installed with nimble. If I do
nimble install astar
it will install the latest release which will not compile with recent versions of Nim. To make it work I have to manually download the files from github repository and work directly with the astar.nim file. Can the latest version of this package be released as a new version?
Using astar in latest nim 1.2.2 is throwing an exception
... C:\Users\risharan\.nimble\pkgs\astar-0.4.0\astar.nim(25, 22) Error: undeclared identifier: 'THash'
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.