[AI] RailwAI

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

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

The NoAI framework doesn't support to let an AI know much about available road types such as rattroads. Currently RailwAI assumes there can be 2 types of road (as per default): roads and tramways. I can try to let it work with rattroads, but it will always be a guess: The characteristics of other road types than the defaults can not be determined by an AI, while I want to treat roads and tramways differently.
Edit: my statement might be incorrect starting from openttd 1.10.0, let's wait if it will improve :-)

User avatar
3iff
Tycoon
Tycoon
Posts: 1012
Joined: 21 Oct 2005 09:26
Location: Birmingham, England

Re: [AI] RailwAI

Post by 3iff »

Quite impressed by this. A couple of issues discovered that I'd like to mention. Running version 20 on JGR 29

The AI linked an iron ore mine to a steel mill via roads then proceeded to build 3 truck services within a few months. That's hardly an efficient way to do it as only 2 stations will be getting resources. However, all three services are still active after 10 years - and it does tend to block other companies from setting up a service!

The double tracked rail lines set their signals so trains drive on the right. I always set my trains to drive on the left. I had a quick look at the code and basically didn't understand most of it. I'm guessing it's not an easy thing to change this but if you think it's possible if you could perhaps give me a few pointers so I might change it... I'm NOT asking you to make any changes (I really wouldn't expect you to do so). I'm pretty sure that players who have trains driving on the left are few in number, but I'd like to change it for myself if I possibly can. It only becomes relevant when I take over such a company and I can live with it how it is at the moment.

I do like the fact that there are shared vehicles which makes it so much easier after takeovers. Bigger rail stations are also appreciated.

---------

Actually, on further inspection, the signalling is way more complex than I first thought. There's a strange combination of single, path and one-way stuff. It mostly works except for one instance where a train went the wrong way (on a line that connected another service and the AI ended with 4 trains stuck).

It's also slightly annoying that if a rail service closes that the associated track is left in place - but I can understand the situation when services share some common track.

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thank you for your reaction! Blocking other companies should not be involved in RailwAI, so that should be a problem of the other companies. Sometimes the strategy might be inefficient, but that's part of having random behaviour, everything could be an option. Or do you mean that it tried to start one service for which no cargo is produced, so nothing is being transported?

Changing trains to drive on the left track will be quite complex for sure. The rail construction contains a lot of code that is written to have trains running the right hand track. You could try to change the orientation (and types) of signals being placed on new railway lines, but it might cause deadlocks on two-track sections on single track lines.
The best way would be to change the RailTrackPlan (yes it is a huge bunch of code) in rail.nut and some more other changes. Required changes (as far as I think would be needed):
  • Make sure you understand how the VectorCoordinates work (world.nut).
  • path1 should not go from the right track to the left track, but from the left track to the right track. This can be changed in the pathfinder.InitializePath command (line 686 of rail.nut). Not that all the avoidArea and avoidTiles must be changed too: these are tiles that must be avoided by the pathfinder, basically already reserving some space for the other track.
  • GetTilesLeftFrom must be changed to a function GetTilesRightFrom. This function now returns a tile/direction array left of a given path. Note that it behaves different from corners to the left and corners to the right, because the other track crosses a different number of tiles.
  • BuildPassingSectionsLeftFromTiles must be changed to a function BuildPassingSectionsRightFromTiles
  • If there is more that refers to left or right (variable names or comments), try and check if it must be changed.
  • Disallow for train stations with more than two platforms (numPlatforms in station.nut). Current station exits will cause deadlocks with trains running on the left track, and it is all hardcoded (I still have the idea to refactor it one day). You could also try to change it, but it won't be easy.
Signal usage might look difficult, but some basic guidelines are involved:
  • On double track lines, use simple signals
  • Before switches, place path signals. Use a one-way path signal on the right track to prevent trains running on the left track.
  • Also use path signals (preferably one-way signals) on two-track sections of one track railway lines (note that more than 50% can be double track)
  • If something is changed to an existing railway line (a new bridge connected, a railroad crossing removed/placed, a new switch was placed) replace simple signals there with path signals
When a rail service closes it eventually tries after a while to remove railway track. It is still an open issue on my list that tracks towards a depot are not removed. Currently I'm quite busy in my real-world life, so I hope to be able to update RailwAI again in 2020. :)

User avatar
3iff
Tycoon
Tycoon
Posts: 1012
Joined: 21 Oct 2005 09:26
Location: Birmingham, England

Re: [AI] RailwAI

Post by 3iff »

Thanks for the reply.

I'm not going to attempt any changes to train driveside or signals...way too complex.

As for the 3 stations at one mine, having 2 stations should be a max limit (which might effectively block opposition - that's fine) but having a third pickup station is self defeating - so perhaps being able to detect and prevent that happening would be helpful for that company.

Removing unused track would be helpful as otherwise a smallish map can easily get clogged with redundant track and a takeover or bankruptcy is the only effective way to clear it.

Ikta
Engineer
Engineer
Posts: 1
Joined: 22 Nov 2019 17:59

Re: [AI] RailwAI

Post by Ikta »

Ran into a error trying to load a game today that I was playing last night
Image

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thanks for reporting! The bug looks rather easy to fix, so it will be solved in next release :-)

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

RailwAI wrote:
31 Oct 2019 20:02
The NoAI framework doesn't support to let an AI know much about available road types such as rattroads. Currently RailwAI assumes there can be 2 types of road (as per default): roads and tramways. I can try to let it work with rattroads, but it will always be a guess: The characteristics of other road types than the defaults can not be determined by an AI, while I want to treat roads and tramways differently.
Edit: my statement might be incorrect starting from openttd 1.10.0, let's wait if it will improve :-)
According to the docs it will be possible with openTTD 1.10, I'll improve RailwAI after relase of 1.10.0

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

No road types support yet, but version 21 is now available.
The changelog:
  • Trains: became better in building passing loops on single track lines
  • Trains: trains should not have more locomotives than carriages, even when game settings are set to high slope steepness
  • Trains: Remove abandoned railway lines
  • Trains: Better support to use freight trains to transport goods to cities
  • Trains: Put less often signals around short railway bridges
  • Trains: Put more often signals around longer tunnels and bridges
  • Ships: Place more canal tiles around docks, to prevent ships being lost
  • Small changes in rail pathfinder
  • Transport valuables in two directions, also with freight trains
  • Several code speed optimizations
  • Try sometimes to reconnect a road if one vehicle is locked on a single piece of road
  • Have a small preference to transport from newly opened industries
  • Bugfix: error 'avoidArea' does not exist
  • Bugfix: do not accidentally turn around a one-way path signal after building a new railway bridge
  • Bugfix: be able to build a depot on both sides of the track near a train station
  • Bugfix: Always remove the depot, when a new train station plan is cancelled
  • Bugfix: Rail pathfinder could generate illegal routes around bridges
  • Bugfix: Saving a game while building a train station, could cancel the building plans
  • Bugfix: Should not come here in World.Direction

xarick
Transport Coordinator
Transport Coordinator
Posts: 291
Joined: 26 Feb 2015 00:52

Re: [AI] RailwAI

Post by xarick »

Got a crash right in the first year.
Attachments
Unnamed, 1950-12-29.png
(514.81 KiB) Not downloaded yet
RailwAI v21 crash.sav
(113.75 KiB) Downloaded 17 times
Formerly known as Samu

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

I don't like crashes, so thanks for reporting! This one was very easy to fix, thus you can now download version 22. I also made some improvements after I saw some deadlocked trains in my games. The changelog:
  • Trains: be able to make adjacent bridges double track
  • Trains: Place less often signals around railway bridges
  • Trains: Do not connect railway bridges to station platforms
  • Trains: Build depots near some bridges
  • Bugfix: Do not crash when no train could be bought

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

I got crash right after loading my save game. Btw, thanks for maintaining this AI until today :)
EDIT: attached save game
Attachments
Aurora Logistic, Nov 27th, 1940.png
(507.54 KiB) Not downloaded yet
Aurora Logistic, Nov 22nd, 1940.sav
(3.78 MiB) Downloaded 17 times

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thank you! It was a silly bug, as I had forgotten to change 2 array indexes. Version 23 solves this issue and should be able to load this savegame.

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

A little request, could you add an option to make this AI friendly a.k.a don't build station inside player controlled town? Thanks :bow:

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

I think your AI mistakenly place signal that jam the entire track (Fenfingway - Slonburg) that causes its company to close after 43 years of operation. Other than that, it has done a brilliant job to avoid building a station that already been served by other competitor.
Attachments
Bridge Transport, Jan 8th, 1982.png
(1.36 MiB) Not downloaded yet
Bridge Transport, Dec 7th, 1983.png
(1008.67 KiB) Not downloaded yet
Bridge Transport, Jan 16th, 1982.sav
(429.11 KiB) Downloaded 17 times

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Thanks for reporting, I suppose this signal is turned accidentally and I'll try to fix with the next release.

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

It took me a while to reproduce the bad behaviour, but I could do with your savegame and some manual adjustments :)

A small sidenote: this errorneous signal was not the reason of going bankrupt. The trains that are blocked only cause $-160k on the balance, while all trains together get $10½M profit. It company has problems with the high maintenance costs ($8½M for rail pieces and $4M for signals).

The bug relied on the fact that RailwAI found out that some trains were deadlocked. RailwAI had wanted to replace the one-way path signal by a normal path signal. As it had red numbers on the bank balance, it failed and accidentally turned around the signal. The bug will be solved in the next release, but I will still go bankrupt :P

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

I thought the AI does pretty well with maintenance costs and inflation turned on until it accidentally left-clicked the signal. Anyway, why RailwAI rarely upgrades its' train? Sometimes I still saw it renews steam engine (which no longer available to buy) in electrified - monorail era.

User avatar
RailwAI
Engineer
Engineer
Posts: 55
Joined: 22 Jul 2018 20:30
Location: Headquarters

Re: [AI] RailwAI

Post by RailwAI »

Simply because I didn't implement upgrading normal tracks to monorail tracks; these track types are non-compatible with each other. By the way, who doesn't like to see some steam trains in the 21st century?

Version 24 is now available on BaNaNaS. I tried to compete for Xarick's plane challenge, but bankrupcy can occur due to random business strategies. The changelog:
  • Added planes
  • Use a faster pathfinder
  • Allow AI Settings to be changed during the game
  • Add a setting to limit company size
  • Do not take over other companies that are in financial problems (it's usually not worth it).
  • Bugfix: increased performance for goods and food transport by rail
  • Bugfix: Do not assume the company is always founded in 1950
  • Bugfix: replacing ships
  • Bugfix: Do not turn around one-way path signals when the company is going bankrupt
  • Do not always send a vehicle to a depot when its reliability is 0%

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

tested it again with 3 same AIs and none of them use airplanes. I gave them each 60 million and they just build more trains, RVs, and ships but not aircraft (noise limit enabled and aircraft speed was 2/4). Then with company expansion limit enabled, some of railwAI just build only and only 2 RV stations with buses like tiles it claimed was too small. I don't think this is a bug, yet I'm sure I missed something important here?

RockyPapery
Engineer
Engineer
Posts: 16
Joined: 08 Apr 2020 13:23

Re: [AI] RailwAI

Post by RockyPapery »

Found some issues.
- AI crashes when loading save data
- Helicopter's Order is set to load, then go into the hangar instead to other heliport. Thus preventing it from dropping of its passengers.
Attachments
Unnamed, Dec 12th, 2008.png
(63.85 KiB) Not downloaded yet
Unnamed, Jan 13th, 2009.png
(82.57 KiB) Not downloaded yet
railwAI, Dec 6th, 2008.sav
(1.45 MiB) Downloaded 19 times

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 14 guests