ChooChoo, a train network AI

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
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

ChooChoo, a train network AI

Post by Michiel »

Latest update: 2024-03-14

For new players: are you looking for an AI to add to your game? Give ChooChoo a go!
It is stable, it builds aesthetically pleasing train networks, and it's quite resilient without being murderously competitive.

Hi everyone, I would like to present ChooChoo, a train network AI. I've tried to make something that builds "interesting", nice looking networks. It works by placing a four way crossing at a randomish location and extending its rail network to towns in 4 directions. It'll build a train station on the outskirts of town, with a small bus service to the city center and nearby towns. When a destination town is "off to the side", it'll build a new crossing, connect the crossings and the station, and extend the newly placed crossing. This continues until the network can't be extended further. Then, it places a new seed crossing to build a new network. Here's a picture of the kind of network it builds:

Image

Image

It aims to create simple, but nice looking crossings and good looking bridges:
Image

And here's a screenshot of a nice set of crossings (links to a bigger one):
Image

Picture of the new junction waypoints and station layout:
Image

Picture of the new stations with bus service:
Image

Known limitations:
  • Limited load/save support (it'll "forget" the current work in progress).
  • It probably won't behave nicely if there are no trains available, so don't start the game in too early a year.
  • It cheats a bit by extending its station coverage with bus stations; this requires "build on town roads" to be enabled.
  • No train replacement except autorenew, but it will switch to new rail types when it starts a new network.
It'll play nice with multiple instances of itself because they randomize their starting spots. It's also independent of map size.

Notes for those who want to peek inside:
I've implemented the main processing loop as a "to do list" of tasks. Each task is represented by an object, holding enough state so tasks can be broken off (usually due to running out of money, but it could also be a VEHICLE_IN_THE_WAY, etc.) and resumed. For example, building a crossing is a task (with several subtasks) which then pushes four new tasks onto the queue to expand its four exits.
I'm defining crossings and stations in a local coordinate system which can be moved and rotated to a place on the map. For example, the terminus stations have a single representation from which the four orientations are derived. Feel free to steal bits and pieces that look useful :)

Anyway, enjoy!

Updates
2009-07-09:
- improved startup
- evaluate seed crossing locations before building
- transports mail
- restricted to one station per town

2009-07-12:
- clean up failed crossing exits instead of capping them with depots
- engine selection should now work with newgrfs
- money management to prevent bankrupcy

2009-07-13:
- don't block off crossings when expanding (can still accidentally block an unrelated one)
- start up with some cheap single track lines to get some revenue going

2009-07-16:
- add a fixed number (currently 3) trains per new station and send them to stations that need more trains the most

2009-08-04:
- save/load support
- clean up dead ends more neatly
- removed exit signals from crossings
- stations come with depots; less depots along tracks
- the usual handful of bug fixes

2010-05-17:
- fixed the broken tracks bug
- junctions now get a name in the form of a waypoint, making log output much easier to read (will probably become optional)
- better cleanup of dead track (try blocking a path being built)

2010-05-17:
- settings! Maximum bridge length, how many single track lines to start with, and whether to place waypoints at junctions are now configurable
- first train for a station gets full load orders to improve ratings
- build bus stations before trains to improve cargo production
- more options for station placement by considering the entire station area instead of just the "origin" tile
- handle events while waiting for money

2010-05-25:
- start with cargo, instead of passengers
- places signs showing what it's doing (optional)
- selectable path finder speed: trade off between speed and optimality (the new default is slightly faster than the old one)

2010-05-30:
- build a company HQ
- terrain levelling for stations
- plants trees to raise local authority ratings
- removed depots from tracks to prevent trains going into dead ends

2010-05-30a:
- fixed a terraforming bug

2010-06-05:
- fixed another terraforming bug
- cargo lines are now built single track at first, then extended to double track
- smaller terraforming footprint for crossings

2010-06-12:
- check for compatibility with game settings (allowed to build disjoint stations, drive through stations on town roads)
- cost estimates for bootstrap cargo lines
- single platform cargo stations
- save money to autorenew vehicles
- ignore very small (<200 population) towns
- a whole bunch of bug fixes, including one that would cause it to miss a lot of cargo opportunities

2010-07-01:
- bus service to improve ratings (and because it looks nice)
- improved track building: smoother curves, obstacle avoidance and better double track layout

2011-03-13:
- several start up and profit related improvements, including better cargo route selection
- double track: have the second track follow the first
- try to place more crossings to grow larger networks

2011-03-14:
- double track: REALLY have the second track follow the first

2011-03-19:
- bug fix: don't build multiple stations at the same source industry
- shorter passenger trains

2012-03-28:
Wow, over a year since the last update! And it's mostly bug fixes:
- handle maps with no industries, and large maps with lots of industries
- fixed "Inconsistent compare function"
- "desync" multiple ChooChoo AIs by having them sleep for a short random time so they don't all try to build the same cargo routes
- fixed crash when there are no wagons available for a cargo type

2017-08-26:
Holy crap, has it been over five years? :shock:
- fixed an issue where the AI would be killed for using too much CPU time inside a valuator function

2023-03-16:
- fixed a crash when trying to sell off a vehicle that was already gone
- added an option (on by default) to keep running on unhandled errors

2023-03-23:
- uploaded to GitHub: https://github.com/mkonstapel/choochoo

2023-03-26:
- prefer shorter and nice looking bridges over ugly, fast ones
- tighter, neater double track

2023-03-30:
- significant improvements to pathfinder performance

2023-05-31:
- fixed signal placement so it doesn't create undersized signal blocks at station exits, which could cause deadlocks

2024-02-13:
- don't crash if mail doesn't exist as a cargo type

2024-03-04:
- add option for left hand traffic on double track

2024-03-07:
- add lake detector to avoid excessive pathfinding

2024-03-14:
- improved expansion algorithm
Last edited by Michiel on 27 Mar 2024 10:57, edited 56 times in total.
Morloth
Transport Coordinator
Transport Coordinator
Posts: 378
Joined: 07 Feb 2008 14:06
Location: Glasgow

Re: ChooChoo, a train network AI

Post by Morloth »

Congratulations Michiel,

I just toyed around with it a little bit and it looks pretty cool! One little thing though, you may want to use other software to pack your tar files, because OpenTTD didn't recognize your AI until I unpacked it...

But I really like the way you use these crossings, makes for some neat reuse of rail tracks. Are you btw going to improve these crossings by using bridges / tunnels so that the transition of 1 train doesn't block the access for any other train on a crossing? But otherwise I'll keep an eye on your project and...
Michiel wrote:Feel free to steal bits and pieces that look useful :)
Don't mind if I do ;).

Keep up the good work! :)
Bram
Maninthebox
Engineer
Engineer
Posts: 113
Joined: 08 Aug 2008 11:35
Contact:

Re: ChooChoo, a train network AI

Post by Maninthebox »

Morloth wrote:One little thing though, you may want to use other software to pack your tar files, because OpenTTD didn't recognize your AI until I unpacked it...
Michiel: slacker... :P We even have a nice and shiny Makefile in our own subversion that makes the tarballs just the way openttd likes them: covered in chocolate and whipped cream on top ;)
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: ChooChoo, a train network AI

Post by planetmaker »

That network screenshot looked to well that I could pass by without giving it a quick shot.

The first result is shown in the first attachment which featured my current test newgrfs. It kinda failed... Building a number of those hubs but nothing else. Setting was starting year 2000, 150k GBP initial loan, on a hilly rough 512^2 temperate map with normal town density.
hubs...
hubs...
ChooChoo, 03-03-2000.png (70.02 KiB) Viewed 63840 times
And the reason seems to be (I started the same w/o newgrf, but same results):
too little money...
too little money...
Unbenannt, 03-06-1999.png (12.63 KiB) Viewed 63838 times
With very easy settings (max loan and flat map, no newgrf) it started off quite well as you described. It's building IMO exessive bus stations, though, only half of them would be needed for the same coverage:
station coverage
station coverage
Eichendorf Transport, 28-03-2000.png (112.51 KiB) Viewed 63857 times
It looks like an interesting start, a good concept which can be modularily improved, so I'm looking forward to further improvements and versions of this AI. Please keep us posted :) - and use bananas for my convenience :P
User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9432
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: ChooChoo, a train network AI

Post by planetmaker »

I had the game continue to run... It build one very interesting route (see attachment)
Maybe it would be worth to implement bridges and loop-detection. My guess is that that route won't get overly productive...

This loop-behaviour seems to be a general problem where it actually blocks itself; the AI has a problem with routes which are routed along a shore where it builds the wrong track of the two first so that the other then loops around one hub.
Attachments
Eichendorf Transport, 01-04-2002.png
loop-route
(177.57 KiB) Downloaded 267 times
Eichendorf Transport, 16-06-2002.png
broken route
(240.92 KiB) Downloaded 236 times
Eichendorf Transport, 01-05-2004.sav
save to demonstrate behaviour
(362.5 KiB) Downloaded 1435 times
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Maninthebox wrote:
Morloth wrote:One little thing though, you may want to use other software to pack your tar files, because OpenTTD didn't recognize your AI until I unpacked it...
Michiel: slacker... :P We even have a nice and shiny Makefile in our own subversion that makes the tarballs just the way openttd likes them: covered in chocolate and whipped cream on top ;)
We do?

Code: Select all

# svn update
... we do! How convenient :) I just did tar cvf ChooChoo/*.nut and the result looked reasonable. I'll upload a new one, Made by Makefile. Oh wait, I should probably test that one...

Thanks for the feedback guys!
Are you btw going to improve these crossings by using bridges / tunnels so that the transition of 1 train doesn't block the access for any other train on a crossing?
Not sure - I'll probably try to manage (reduce) the number of trains instead. Those overpass junctions take up a lot of space, and they don't look very nice.
It's building IMO exessive bus stations, though, only half of them would be needed for the same coverage:
Yeah... I'm basically throwing em down wherever there's a suitable road tile :oops: OK, it does skip the next tile when it places one. You've found a very nice worst case scenario :)

I think I can easily prevent it from blocking its own crossings, the code is already there to keep the entrance track from blocking off the exit, and vice versa. To prevent those pointless networks, I'm thinking of either cleaning up failed attempts, or maybe spend some time investigating the site before building a new seed crossing. Maybe even both! Or I get bored and start looking for a new project, so no guarantees :p

Bananas: good point, I'll look into that.
User avatar
fanioz
Transport Coordinator
Transport Coordinator
Posts: 320
Joined: 19 Dec 2008 05:03
Location: Indonesia
Contact:

Re: ChooChoo, a train network AI

Post by fanioz »

Well done and congratulation... :D
I'll try it at home. It have a good concept. (and it smell like release version of otvitrAIn :?: or just another side project :lol: )
Correct me If I am wrong - PM me if my English was bad :D

**[OpenTTD AI]** Image
***[NewGRF] *** Image
Wold
Transport Coordinator
Transport Coordinator
Posts: 318
Joined: 03 Aug 2006 19:32
Skype: woldy_bg
Location: Bulgaria

Re: ChooChoo, a train network AI

Post by Wold »

Found a major bug - don't introduce desert terrain to the AIs - the result is pooor
Cathill Transport, 13th Aug 1960.png
Cathill Transport, 13th Aug 1960.png (43.09 KiB) Viewed 21020 times
Cathill Transport, 13th Aug 1960#1.png
Cathill Transport, 13th Aug 1960#1.png (43.69 KiB) Viewed 20987 times
Image
Wasila
Tycoon
Tycoon
Posts: 1498
Joined: 15 Mar 2008 07:02

Re: ChooChoo, a train network AI

Post by Wasila »

Looks like a good concept, keep it up!
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Wold wrote:Found a major bug - don't introduce desert terrain to the AIs - the result is pooor
Cathill Transport, 13th Aug 1960.png
Cathill Transport, 13th Aug 1960#1.png
Looks like it's working as intended, though... what happens, does it go bankrupt? The red output is, unfortunately, unavoidable if you use exceptions in your code. The first screenshot shows it's waiting for money before continuing, and the second is an expected failure (it can't extend a crossing because there is no town in the area it's looking in). It should just continue - though if it runs out of starting funds before it gets a decent line going, it won't get very far.
Wold
Transport Coordinator
Transport Coordinator
Posts: 318
Joined: 03 Aug 2006 19:32
Skype: woldy_bg
Location: Bulgaria

Re: ChooChoo, a train network AI

Post by Wold »

Well I've played like 5-6 games and the AI went bankrupt all the times. I even left it alone with no one to compete and it bankrupted in 3-4 years top. The problem I see is that it chooses small towns and the trains produce little income and can't keep with the taxes, since the interest is 10k/year.

Another problem is that it takes all the loan at once and steadily loses money. Another thing is the crossings it makes everywhere which cost ~15k each, but still doesn't connect them.

There should be some priorities in choosing towns, which will make it operate much more efficiently.

I like it though, keep working!

Edit: It doesn't build enough trains ( money issues ) but later, when it has, it doesn't build additional trains to service the route.

Edit2: Not just desert cities * Temperate too
Last edited by Wold on 08 Jul 2009 18:17, edited 1 time in total.
Image
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Thanks! I guess it's having problems with the low population desert towns. I'll look into evaluating whether a network is viable before actually building it, and maybe implementing DoSomethingIntelligentBeforeBuildingTrains() :wink:
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Update: now evaluates seed crossing sites and starts out building cheap (shorter distances, ignore small towns, cheap locomotives, no extra crossings), so it usually manages to bootstrap itself on a 100k loan in desert maps. It also restricts itself to one station per town, which cleans up the map quite a bit.
User avatar
FHS
Director
Director
Posts: 577
Joined: 18 Apr 2009 17:17
Location: Basel, CH

Re: ChooChoo, a train network AI

Post by FHS »

I do really like your AI as it seems the first to reuse tracks but:
1. Your AI doesnt support NewGRF trainsets
2. Its got some weird overpasses because it cant build two diagonal tracks on a tile

Otherwise its the best AI i know to date, i think this design is is probably the best ive seen by an AI so far

(Remeber OldAI?)
( :lol: )
Attachments
Stupidly Getting from A to B
Stupidly Getting from A to B
AIStupid.gif (35.52 KiB) Viewed 63571 times
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

LOL, what the... it built that? In that case, the pathfinder needs a good spanking :) Or did you mean that was the handiwork of the old AI? I'm pretty sure the AI pathfinder I use won't build 90 degree turns.
Its got some weird overpasses because it cant build two diagonal tracks on a tile
I think I know what you're referring to, where it builds bridges to "hop over" the other track. I'm currently using a pathfinder I borrowed from OtviTrain; I should switch to the library one. It may have some improvements for this case.
Your AI doesnt support NewGRF trainsets
Hmm.. Question for other AI writers: how hard is it to support NewGRF trainsets? I've never used a NewGRF anything. They seem to be quite popular, so if it's not too hard, I'll look into it; otherwise I'll focus on the network building aspects first.
Otherwise its the best AI i know to date, i think this design is is probably the best ive seen by an AI so far
Wow, thanks :D
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: ChooChoo, a train network AI

Post by Yexo »

Michiel wrote:
Its got some weird overpasses because it cant build two diagonal tracks on a tile
I think I know what you're referring to, where it builds bridges to "hop over" the other track. I'm currently using a pathfinder I borrowed from OtviTrain; I should switch to the library one. It may have some improvements for this case.
The library pathfinder has the same problem.
Your AI doesnt support NewGRF trainsets
Hmm.. Question for other AI writers: how hard is it to support NewGRF trainsets? I've never used a NewGRF anything. They seem to be quite popular, so if it's not too hard, I'll look into it; otherwise I'll focus on the network building aspects first.
It's easy really, just use AIEngineList and the AIEngine::* functions to select an engine instead of hard-coding one.
User avatar
Zutty
Director
Director
Posts: 565
Joined: 22 Jan 2008 16:33

Re: ChooChoo, a train network AI

Post by Zutty »

Really cool AI Michiel. :) Its a nice, elegant way to create a network.
Michiel wrote:Hmm.. Question for other AI writers: how hard is it to support NewGRF trainsets?
I dont do train sets yet but I have tried to work with some RV sets. I think you just need to try some popular sets and balance your selection criteria accordingly. NewGRF authors don't stick to a set of common rules so sets can be very different, especially the prices and running costs.

One issue I found with eGRVTS (large RV set) is that vehicle statistics were balanced differently depending on the date of their design, especially for very early vehicles (pre-1950). I use the current date as a balancing factor in my selection criteria to address this.

I dont think there is any way to guarantee that your AI will work perfectly with all NewGRF sets though, given how vastly different they can be.
PathZilla - A networking AI - Now with tram support.
User avatar
Michiel
Transport Coordinator
Transport Coordinator
Posts: 339
Joined: 13 Jul 2008 00:57
Contact:

Re: ChooChoo, a train network AI

Post by Michiel »

Yexo wrote:It's easy really, just use AIEngineList and the AIEngine::* functions to select an engine instead of hard-coding one.
Oh, I'm doing that already, either picking the fastest or the cheapest (after checking it's not a wagon - the first attempt at cheap was trying to have copper ore hoppers pull its trains :P). I'll see if I can install one of them new fangled train set thingies and see what happens. FHS, I assume you were trying it with "the 2cc set"?
User avatar
FHS
Director
Director
Posts: 577
Joined: 18 Apr 2009 17:17
Location: Basel, CH

Re: ChooChoo, a train network AI

Post by FHS »

One time the 2cc set and another time the outdated swiss set taster, i didnt seem to work with either.
Wold
Transport Coordinator
Transport Coordinator
Posts: 318
Joined: 03 Aug 2006 19:32
Skype: woldy_bg
Location: Bulgaria

Re: ChooChoo, a train network AI

Post by Wold »

Since I have no intentions to flood:
#1 - for almost 2 years the AI has built 2 trains ( 1 route )
#2 - no clue what this is, but it should be important
#3 - Day 1 - total failure :D I couldn't find anything like this, but you are the coder
#4 - like #2
#5 & #6 - there is nothing on the map and it couldn't find a town



Something else - because of all the checks the AI does the game runs hell of a slow. You should consider a limit of some sort ( either from the AI itself, or user-defined ).
Attachments
Choo.zip
(327.81 KiB) Downloaded 1044 times
Image
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest