Scheduler -- Why is my AI so much slower?!

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Scheduler -- Why is my AI so much slower?!

Post by TrueBrain »

As of today, NoAI gained a scheduler. This beautiful piece of work by Morloth has several side-effects, of which most noticeable: your AI will perform a bit worse. What happens is this:

If your AI would contains:

Code: Select all

while (true) { }
Your AI would hang for ever, and worse: OpenTTD would too. As of today, this is no longer true. Every 4000 internal Squirrel commands, your AI is given a Sleep() for 1 tick. This makes sure the game keeps on responding no matter what. Also, it no longer allows you to scan the whole map in the first day, and fast pick all the good routes before any other AI can. You have to think a bit more now. All AIs are scheduled 'fair', and all get a piece to look at in the first game-day.

On a nice side-note: when you issue a Sleep() yourself, the counter is reset. This means if you add Sleep() at nice places in your code, the chances are that your AI performs better than when you remove them all.

Happy AI writing :)
The only thing necessary for the triumph of evil is for good men to do nothing.
User avatar
Ralph
Engineer
Engineer
Posts: 87
Joined: 21 Jun 2004 15:25

Re: Scheduler -- Why is my AI so much slower?!

Post by Ralph »

Cool, I have no idea how much slower this will make things, but seems like a good change.

Is there any way of accessing the internal command count? It may be useful for checking how expensive functions are so we know where to optimise.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: Scheduler -- Why is my AI so much slower?!

Post by Zutty »

Excellent idea. Plus what Ralph said.... should be handy. :)

My AI is yet to be optimised, but it hangs terribly in places! I have nested loops with differing algorithms, so I'm never sure where to put the Sleep()s for the best results. This should help to ease that problem.
PathZilla - A networking AI - Now with tram support.
Misha
Engineer
Engineer
Posts: 18
Joined: 21 Jul 2008 17:39

Re: Scheduler -- Why is my AI so much slower?!

Post by Misha »

TrueLight wrote:Also, it no longer allows you to scan the whole map in the first day, and fast pick all the good routes before any other AI can. You have to think a bit more now. All AIs are scheduled 'fair', and all get a piece to look at in the first game-day.
This looks very important for a fair competition between AI's. Good work.:) I have a few questions though.

Is it possible for an AI to detect which piece it can view?
What happens when an AI tries something on a tile it can't view?
From when will an AI be capable to view every tile on the map?
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Scheduler -- Why is my AI so much slower?!

Post by Yexo »

Misha wrote:Is it possible for an AI to detect which piece it can view?
What happens when an AI tries something on a tile it can't view?
From when will an AI be capable to view every tile on the map?
You're misunderstanding things here. There is a limit on the amount of commands an AI can execute every ingame day, not a limit on the amount of tiles the AI can use. Because of that command limit, it's not possible to do all pathfinding in one ingame day, since the AI will be paused every x opcodes.
Misha
Engineer
Engineer
Posts: 18
Joined: 21 Jul 2008 17:39

Re: Scheduler -- Why is my AI so much slower?!

Post by Misha »

Ah, that makes sense. :)
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests