The near future: Trains are no longer controlled by humans. Instead, artificial intelligence is used to handle traffic. Program the best AI, beat challenges on various maps and watch it fight other AIs in live, online matches!
Posts | ||
---|---|---|
a* without timing out? | Locked | |
Thread Options | ||
Sep 23 2013 Anchor | ||
I currently have an a* pathfinding model going. Presently it runs when a train is created and it picks a "target". My algorithm is nasty ugly. I am bad at algorithms, i never was taught formally about them so when i impliment them it is loops upon loops, which might ( must ) be my problem. The method itself is 100 lines long, not counting some helper methods ( distance ), though there's a lot of if's and whatnot but still... Definately over the "10-15 line MAX!" they teach in schools. Anyways, i think my biggest issue is calculating H. I'm using the Challenge 2 map, The target is(7,1). i start at (4,3, S). a* runs to 4,4 to the W,N,E junction, then it prefers to go (5,4) ; (5,5)... towards the bottom righttown. The reason a* starts searching on rail to the S is because i have it project out from the "front" of the train. H is currently just the physical distance, so a* has to check every block in the bottom right because those are all much closer than the needer path of going to the far left of the map so their H is smaller so it tries them first. I am thinking that if, when i create my railList, i calculate the H then and use nearby tiles to decrease scores so that it prefers paths near homes and hotspots etc, that this could alleviate the issue somwhat. However i don't think so because as soon as it gets to the bottom right town near houses it will immediately try and fill that area. The only other thing i can think of is stop limiting its search, it finds it pretty quickly if it happens to go north the first time. Then find the first rail in the path that has more than 2 neighbors, a junction, and then set that as the target. Then do some patching of the two paths found and send it off. I had hoped to avoid doing that though but it might be necessary. So here's the code, not sure if you'll need it or not. I'm sure there are 10's of things wrong with it and i really apologize for the loops.:
|
||
Oct 5 2013 Anchor | ||
Hi there, I'm not sure if I understand the problem correctly, and my A* knowledge is pretty rusty (if not worse), but I noticed a few things. You're on the right track.
I'm a bit confused by your approach, because from what I can tell, you never set or change "g".
... because the distance from start node to start node is 0. Then, you should update that g distance every time you look through the open nodes and update a parent. What you currently do is:
Instead, you should do:
In the same way, whenever you're adding a new rail:
The next thing you'll need to change is this line:
This line should never be true, unless currentSquare.g is lower than 0. Otherwise, two positive numbers added together can never be less than one of those two numbers...
Bottom line is: If you've changed the above and it still doesn't work, feel free to ask again, I'll be glad to help (and will hopefully receive messages about thread updates this time...) P.S.: Sorry I took so long to reply - I've relatively busy with other stuff again, and these forums still don't send me an email when a new thread is started :( Edit: One thing I forgot to say: Edited by: Germanunkol |
Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.