TeshiNet, yet another 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

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

TeshiNet, yet another AI

Post by teshiron » 05 Dec 2011 01:55

Hi everyone,

I'm releasing TeshiNet, an AI that primarily uses road vehicles, and some aircraft.

It will transport most types of cargo, including passengers, and should be broadly compatible with most NewGRF's as long as non-articulated RV's are available that can be refit for the available cargoes.

TeshiNet is written to "play nicely" -- it will not try for more than one passenger subsidy per instance at any given time, and it doesn't tend to spam RV's on any particular route, so traffic jams are not much of an issue.

Current version: v4.0.3, released 2015-04-20

Change log:

v4.0.3
  • Because of the impending closure of Google Code, project is now hosted on GitHub. Links to Google Code revision numbers may stop working when that site closes.
  • Bugfix: crash at load due to requiring old version of SuperLib. Now depends on v38.
  • BaNaNaS update: Added PriorityQueue to the dependencies to prevent crashes.
v4.0.2
  • Bugfix: Although 4.0.1 resolved the reported crash, it regressed a prior bug where the first try at a route (as a new company) would never be built properly, and in some cases many stations would not be built correctly. Re-fixed.
v4.0.1
  • Bugfix: crash at main.nut line 607 due to invalid parameters. Thanks to MAG101 for the bug report.
v4
Version 4 breaks compatibility with older savegames. v4 and up cannot load savegames from v3 and previous.
  • Implemented an event handler to detect crashed vehicles, unprofitable vehicles, and industry closures
  • Now searches periodically for available upgrades to road vehicles, and replaces the outdated vehicles
  • Now detects the location of an RV-hit-by-a-train crash, and replaces the level crossing with a bridge if possible
  • Also detects vehicles with invalid orders, along with vehicles missing orders entirely, and sells them. Thanks to Lowkee33 for the suggestion.
  • Now sets order flags for freight routes to avoid pickup at the destination station.
  • Restrict new passenger routes to towns above 500 population.
  • Removed unprofitable route check, since checking for individually unprofitable vehicles makes it redundant
  • Fixed a minor issue where the first route as a new company would never be built properly.
  • Modified the "enable buses" AI setting so changing it will actually take effect before reloading the game.
  • Removed profitability restriction on adding vehicles to routes, per suggestion from Lowkee33. Also now adds new trucks before buses (in case it runs out of money) as freight cargoes are generally worth more per unit than PAX.
Known issues:
None currently

Planned improvements:
  • Add support for trains
  • Better handling of unprofitable plane routes
  • Convert to API version 1.1
  • Handle freight delivered to towns, such as goods or food
  • Make a setting as to whether to "play nicely", would mostly affect how many subsidies it tries for
TeshiNet is available on BaNaNaS. Please let me know if you have any suggestions for improvement, or run into any bugs! You can also file bugs on the GitHub page for TeshiNet.
Last edited by teshiron on 20 Apr 2015 20:33, edited 10 times in total.

Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: TeshiNet, yet another AI

Post by Kogut » 05 Dec 2011 07:57

@Tourists - there is a function in Superlib to handle this ( GetPAXCargo() ). And welcome!
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 05 Dec 2011 14:47

Kogut wrote:@Tourists - there is a function in Superlib to handle this ( GetPAXCargo() ). And welcome!
Thanks! It's interesting because I'm already using that function to identify the PAX cargo, but it still ends up building tourist buses anyway.

User avatar
planetmaker
OpenTTD Developer
OpenTTD Developer
Posts: 9278
Joined: 07 Nov 2007 22:44
Location: Sol d

Re: TeshiNet, yet another AI

Post by planetmaker » 05 Dec 2011 15:45

teshiron wrote:
Kogut wrote:@Tourists - there is a function in Superlib to handle this ( GetPAXCargo() ). And welcome!
Thanks! It's interesting because I'm already using that function to identify the PAX cargo, but it still ends up building tourist buses anyway.
Both, tourists and 'normal' passenger are a PAX cargo. You might need to refit your bus to normal passengers and / or check the actual cargo label. but maybe there's a more elegant or an existing solution

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 05 Dec 2011 16:53

planetmaker wrote:Both, tourists and 'normal' passenger are a PAX cargo. You might need to refit your bus to normal passengers and / or check the actual cargo label. but maybe there's a more elegant or an existing solution
The function in SuperLib tries to distinguish PASS from TOUR, and it actually does so correctly most of the time. My problem was that I had decided to save the identified pax cargoID, and I was testing using a savegame where I had previously saved the wrong cargoID -- causing it to overwrite the correct pax cargo identification when Load() was called. D'oh.

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: TeshiNet, yet another AI

Post by Brumi » 05 Dec 2011 19:04

I decided to give your AI a test run.
It built 2-3 routes, then a few years later, it sold its stations, leaving the vehicles behind. After restarting the AI, it repeated this 3 times.

EDIT: Corrected ugly grammar mistake
Last edited by Brumi on 05 Dec 2011 22:46, edited 1 time in total.

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 05 Dec 2011 21:53

Brumi wrote:I decided to give your AI a test run.
It built 2-3 routes, then a few years later, it sold its stations, leaving the vehicles behind. After I restarting the AI, it repeated this 3 times.
That's odd. It's most likely related to one or more settings or NewGRFs you are using, because I didn't encounter anything like that in my testing. The behavior you describe would seem to be the AI trying to kill the route because it's unprofitable, and the vehicles are not getting the message to go to the depot. But why the route wouldn't work in the first place, and why it wouldn't clean up the route properly, is beyond me.

Can you provide a savegame or a list of the NewGRFs you used? That would be very helpful.

Brumi
President
President
Posts: 904
Joined: 18 Jul 2009 17:54

Re: TeshiNet, yet another AI

Post by Brumi » 05 Dec 2011 22:49

Well, most probably it's my settings that cause the trouble. I used the Generic Tram Set and the Temperate Oil Wells Only Decrease Neutraliser when I tested your AI, these shouldn't make a difference. Now I tested it without any NewGRFs, and I still saw the strange behaviour.

So here is an openttd.cfg, the bug can be reproduced with these settings.
Attachments
openttd.cfg
(9.99 KiB) Downloaded 117 times

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 07 Dec 2011 07:51

Brumi wrote:Well, most probably it's my settings that cause the trouble. I used the Generic Tram Set and the Temperate Oil Wells Only Decrease Neutraliser when I tested your AI, these shouldn't make a difference. Now I tested it without any NewGRFs, and I still saw the strange behaviour.

So here is an openttd.cfg, the bug can be reproduced with these settings.
That was very helpful. I forget sometimes that not everyone plays with the same settings I do!

With high construction costs, low number of towns, mountainous terrain, and the slow vehicles available at that date (1950), it was just very difficult for the initial routes to turn a profit. I'd set it to start removing routes too early, and not wait long enough for the vehicles to get to the depot when removing a route.

I've made several changes to its early game behavior in order to make it more viable, including; get a larger initial loan, build shorter routes when only slow vehicles are available, do not remove routes unless it already already has 10 routes going, only build 3 vehicles for each route instead of 5, etc. I also have it waiting 4x as long for vehicles to get to the depot when a route is removed. These changes will all be in version 3 which I hope to release this week sometime.

Thanks for your feedback!

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 08 Dec 2011 04:36

TeshiNet v3 has been released; see the top post in this topic to download.

Note: I made a typo in one function call that I didn't catch until after I uploaded to BaNaNas, and 3 unfortunate souls downloaded the buggy version. If you were one of them, sorry! Update from BaNaNaS to get the replacement version of v3.
Last edited by teshiron on 08 Dec 2011 05:30, edited 1 time in total.

Kogut
Tycoon
Tycoon
Posts: 2493
Joined: 26 Aug 2009 06:33
Location: Poland

Re: TeshiNet, yet another AI

Post by Kogut » 08 Dec 2011 04:44

You can also bump version of fixed AI, this way they will update it in the automatic way.
Correct me If I am wrong - PM me if my English is bad
AIAI - AI for OpenTTD

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 08 Dec 2011 05:25

Kogut wrote:You can also bump version of fixed AI, this way they will update it in the automatic way.
That's true, I forgot you can use decimal versions on BaNaNaS.

Lowkee33
Engineer
Engineer
Posts: 20
Joined: 25 Nov 2011 20:49

Re: TeshiNet, yet another AI

Post by Lowkee33 » 11 Dec 2011 00:46

Hi teshiron. I've been playing with your AI recently. One thing that would make this better was if the amount of buses/trucks the AI was variable, rather than just three. I think I have seen it buy more as time goes by, but there is a lot of missed profit.

I've also occasionally seen buses/trucks with "Invalid Order" as their first order. I see by the log that you are checking for vehicles with "No Orders", but I don't know if those two things are exclusive. I think it happens when you delete the stations before all of the vehicles are sold.

Anyway, a very nice AI so far. Haven't seen any glaring bugs, or any crashes. Sometimes it has trouble getting started, but I think that would be solved by it buying more vehicles rather than look for new routes.

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 11 Dec 2011 01:26

@Lowkee33: Thanks for the feedback.!

I'm planning to check for "Invalid Order" as well as --No Orders-- in the next version; not really sure why I didn't in the first place! :)

As for route capacity, I had originally planned (when I started writing this off-and-on over a year ago) that I could manage capacity well enough by starting with 5 vehicles per route regardless, and then buying more vehicles, one at a time, whenever there was more than 200 units of cargo waiting at a periodic check.

That seems to work fairly well in later game years or on easier difficulty settings, but 5 to start often seemed to be overkill on shorter routes or for smaller towns. This was especially noticeable using the settings file Brumi provided. So, I cut it back to 3 vehicles when starting the route, and also restricted it from buying vehicles to supplement the route unless the vehicle could pay for itself within 3 years (i.e. only purchase if average profit per vehicle for the route * 3 > vehicle price).

What I really need to do is find a way to calculate capacity and buy vehicles accordingly, but I haven't figured a good way to do it (and I'm stubborn -- I want to figure it out myself and not shamelessly stealborrow a method from another AI :lol: )

Until then, I might make the number of vehicles a configurable setting in v4, and I welcome suggestions for other ways to tackle the problem.

Lowkee33
Engineer
Engineer
Posts: 20
Joined: 25 Nov 2011 20:49

Re: TeshiNet, yet another AI

Post by Lowkee33 » 11 Dec 2011 02:47

teshiron wrote:(and I'm stubborn -- I want to figure it out myself and not shamelessly stealborrow a method from another AI :lol: )

Until then, I might make the number of vehicles a configurable setting in v4, and I welcome suggestions for other ways to tackle the problem.
So what do you want? :)

I am working on an AI as well. I use station ratings to figure out if another vehicle is needed. I'm going to have to rant a little, figuring this out was a little tricky.

A main difference between your AI and mine is that my buses have a full load in their first order. This means that bus and truck orders are very similar, I have the same function create them (the only difference is the given cargo ID). Full Load at station 1, regular stop at Station 2, then go to the depot if needed.

The tricky part is that Get Station Ratings requires a cargo ID. It is tough to tackle from the station side of things, because how do we know that there is only one cargo/industry within the range of the station. My solution is with the depots, every route has its own. This means:

Code: Select all

local depotList = AIDepotList(AITile.TRANSPORT_ROAD);
foreach (depot, index in depotList){
  Sleep(1);
  local vehicleList = AIVehicleList_Depot(depot);		
  local vehicle = vehicleList.Begin();
  local stationPos = AIOrder.GetOrderDestination (vehicle, 0);
  local station = AIStation.GetStationID(stationPos);
  local engine = AIVehicle.GetEngineType(vehicle);
  local cargo = AIEngine.GetCargoType(engine);
  
  if (AIStation.GetCargoRating (station, cargo) < 63) {
    takeLoan();
    local newVehicle = AIVehicle.CloneVehicle(AIOrder.GetOrderDestination (vehicle, 2), vehicle, false);  // OrderDestination 2 is the Depot
    AIVehicle.StartStopVehicle (newVehicle);
    repayLoan();
  }
}
Every route has its main station kept at very-good ratings. I have issues with over-building. Right now this is controlled by a Sleep(2500) between every call to this function. That is pretty much my whole while (true) loop. Every few years check for a new route, otherwise just maintain ratings.

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 13 Dec 2011 04:04

@Lowkee33: The problem I can foresee when trying to use ratings is that, with slow vehicles and somewhat long-ish routes, the number of vehicles to keep the rating high will be more than is necessary for the cargo to transport -- i.e. they will not run enough cargo each to turn a profit.

What I meant by determining capacity is how some AI's seem to have a way of determining how many vehicles they should buy, in advance -- I see log messages like "Need capacity of 500, at 49, buying 10 buses" or whatever. Absent a system to do that, I think buying more vehicles when there's excessive waiting cargo, and selling vehicles when they have an unprofitable year, works well enough. Try v4 and let me know what you think.

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 13 Dec 2011 04:06

TeshiNet v4 released!

Change log:
Version 4 breaks compatibility with older savegames. v4 and up cannot load savegames from v3 and previous.
  • Implemented an event handler to detect crashed vehicles, unprofitable vehicles, and industry closures
  • Now searches periodically for available upgrades to road vehicles, and replaces the outdated vehicles
  • Now detects the location of an RV-hit-by-a-train crash, and replaces the level crossing with a bridge if possible
  • Also detects vehicles with invalid orders, along with vehicles missing orders entirely, and sells them. Thanks to Lowkee33 for the suggestion.
  • Now sets order flags for freight routes to avoid pickup at the destination station.
  • Restrict new passenger routes to towns above 500 population.
  • Removed unprofitable route check, since checking for individually unprofitable vehicles makes it redundant
  • Fixed a minor issue where the first route as a new company would never be built properly.
  • Modified the "enable buses" AI setting so changing it will actually take effect before reloading the game.
  • Removed profitability restriction on adding vehicles to routes, per suggestion from Lowkee33. Also now adds new trucks before buses (in case it runs out of money) as freight cargoes are generally worth more per unit than PAX.
Please see the first message for download, or download from BaNaNas, and as always, you can post here or open an issue on the Google Code site if you encounter any bugs or have suggestions.

MAG101
Engineer
Engineer
Posts: 28
Joined: 14 Dec 2011 00:55

Re: TeshiNet, yet another AI

Post by MAG101 » 14 Dec 2011 16:12

Your AI crashed about 2 days after its loaded

There a Screenshot (savegame too large to uploaded)
Attachments
Felső-Balatonladányháza Transport, 13th Dec 1946.png
(118.68 KiB) Downloaded 3 times
Last edited by MAG101 on 14 Dec 2011 16:26, edited 4 times in total.

Yexo
Tycoon
Tycoon
Posts: 3653
Joined: 20 Dec 2007 12:49

Re: TeshiNet, yet another AI

Post by Yexo » 14 Dec 2011 16:14

MAG101 wrote:There a Save and Screenshot
Great, could you also upload those :)

teshiron
Engineer
Engineer
Posts: 12
Joined: 25 Dec 2009 19:56

Re: TeshiNet, yet another AI

Post by teshiron » 15 Dec 2011 15:40

MAG101 wrote:Your AI crashed about 2 days after its loaded

There a Screenshot (savegame too large to uploaded)
Oops! I thought I had fixed that error, but I apparently didn't catch it everywhere. Thank you for your bug report, version 4.0.1 has been released to fix this bug. You can download it here or from BaNaNaS.

Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests