[micro_ai] slow

The place to post your WML questions and answers.

Moderator: Forum Moderators

Forum rules
  • Please use [code] BBCode tags in your posts for embedding WML snippets.
  • To keep your code readable so that others can easily help you, make sure to indent it following our conventions.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

Celtic_Minstrel wrote: May 27th, 2018, 10:50 pm or use formula="moves > 0"
No. Like Ravana said, formula cannot be used in filter_wml.
I was quite sure that moves are inside filter_wml...
Celtic_Minstrel wrote: May 27th, 2018, 10:50 pm Also, "greater than 1" is not the same as "equal to 2 or 3".
Yeah I didn't mean that it's same.. I meant rather:
a) would it work? (since filter_wml variables dont let me use coma separated list, or rather it doesnt work with range=2,3,4,5,6)
b) would it be same speed efficient if it worked with more simple and mathematically equaly written example... like:

Code: Select all

[filter]
  formula="wml_vars.range = 2 or wml_vars.range = 3"
[/filter]
Actually $this_unit is also new to me.. I wonder if you know how speed efficient it is...? Because in viewtopic.php?f=10&t=46370&start=45#p626762 if I used $unit.side instead of $side_number inside filter it was about 10 times slower. Would it be different from $unit.side problem?
Celtic_Minstrel wrote: May 27th, 2018, 10:50 pm (I think wml_vars is undocumented, but it's basically the WFL way to access [variables if I recall correctly.)
so much useful information on filters... thank you very much ;) Looks like I was quite very limited on my filters knowledge...

Could anyone give me a clue if putting into micro_ai code like this:

Code: Select all

[filter_location]
         [filter]
				id=$this_unit.id
				[/filter]
				radius=3
            [/filter_location]
would be faster than code like this:

Code: Select all

[filter_location]
         [filter]
				side=$side_number
				[/filter]
				radius=3
            [/filter_location]
Would it shorten the amount of locations to just 1 current unit? (I assume less locations to think is faster, but is using $this_unit not going to slow things down? And would it actually work?) Thanks in advance!
User avatar
Ravana
Forum Moderator
Posts: 2950
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [micro_ai] slow

Post by Ravana »

$this_unit performance was discussed few h ago in #wesnoth-dev, check logs for that, but I understand using $ in formula makes it 100 times slower.

id=$this_unit.id is always true, so same as no filter.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

Ravana wrote: May 28th, 2018, 3:17 am I understand using $ in formula makes it 100 times slower.
you mean using $ in filters or using $ in formula=? Or you mean just using formula=?
Ravana wrote: May 28th, 2018, 3:17 am id=$this_unit.id is always true, so same as no filter.
I might be understanding you I'm not sure...
I guess I need to try...
How does micro ai work? Does it store all units and then take each unit individually and trying to apply filter to it? Or does it use filter to store all units matching the filter and then is trying to apply filters locations individually? Or does it store all units matching the filter and then stores all locations matching the filter and then makes calculations? Is there a point to use $this_unit in MAI at all?
User avatar
Ravana
Forum Moderator
Posts: 2950
Joined: January 29th, 2012, 12:49 am
Location: Estonia
Contact:

Re: [micro_ai] slow

Post by Ravana »

In formula, $ prevents compiling the formula.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [micro_ai] slow

Post by Celtic_Minstrel »

enclave wrote: May 28th, 2018, 2:49 am
Celtic_Minstrel wrote: May 27th, 2018, 10:50 pm or use formula="moves > 0"
No. Like Ravana said, formula cannot be used in filter_wml.
I was quite sure that moves are inside filter_wml...
Yes, moves is in filter_wml, but formula is not.
enclave wrote: May 28th, 2018, 2:49 am (since filter_wml variables dont let me use coma separated list, or rather it doesnt work with range=2,3,4,5,6)
Formulas allow for a lot of [wiki=Wesnoth Formula Language]complicated things[/wiki]. For example, I believe you could get that sort of comma-separated list in a formula with formula="wml_vars.range in [2, 3, 4, 5, 6]", or since they're consecutive numbers, the even shorter formula="wml_vars.range in 2 ~ 6" should work, if I recall correctly.

That linked page documents most of the functions you can use in a unit filter formula, but not all; some of the functions documented for FormulaAI also work (mainly ones that just deal with units or locations). Eventually I'll copy those over to the main WFL page.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: [micro_ai] slow

Post by mattsc »

Celtic_Minstrel and Ravana already said most of it, so just a few additional things.
enclave wrote: May 28th, 2018, 11:24 am
Ravana wrote: May 28th, 2018, 3:17 am id=$this_unit.id is always true, so same as no filter.
I might be understanding you I'm not sure...
this_unit refers to the unit that is currently being looked at by the filter. So that line checks whether the id of the current unit is equal to the id of the current unit.

As a general rule though, do not use $this_unit unless there is absolutely no other way, or in isolated cases when speed does not matter. It is super slow, just like [filter_wml]. (I guess Ravana already said that too.)
enclave wrote: May 28th, 2018, 11:24 am How does micro ai work? Does it […]
If you want to know the details, you should really have a look at the Goto MAI Lua code. To summarize though (see also my Points #6 and #7 from two days ago), it first finds all locations matching the SLF, then if finds all units matching the SUF (and then it does pathfinding for all valid combinations). As I said already, there’s a reason to do it that way and we will almost certainly not change that for the mainline version, but I also suggested that changing the order (do SUF first, then SLF) might give you the biggest time saving overall for your setting (if you adopted a local version of the AI).

Now, in the meantime I have talked on irc with Celtic_Minstrel and others and found out that I had misunderstood what he was suggesting. I also learned a couple things about filters that I didn’t know and gfgtdf fixed an inefficiency in SUF evaluation in the engine. Long story short, there's no need for separating evaluating the filter and eliminating units without moves any more. It can all be done quickly and efficiently in one step. So I'll do that and since this is just an internal change to improve efficiency with no API change, it will be in 1.14.3. That should give you the 40% speed improvement we've been talking about previously without you having to change anything.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

Thank you very much everyone ;) I think I have enough tools now for a while.. so I'l get back here if I run in any kind of new trouble :D

By the way got an error for micro ai on 1.14.1
w1.jpg
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: [micro_ai] slow

Post by mattsc »

I just saw that you added that error report (I get notifications when a new post is made, but not when one is edited). I know what that message means, but there's no way of figuring out why it is happening without a test case. Do you still have the save from when you got this?

In other news:
enclave wrote: May 27th, 2018, 1:33 pmI would be suprised if it goes down to 1ms, but I have no reasons not to believe ;)
Be ready to be surprised then. ;) We've implemented a number of changes to the filter evaluations for 1.14.3 (7be39c93, fb364cf2, 4b8870ed, 7727130e). With that, the lag time due to the AI evaluation at the end of the turn in the scenario you sent me is indeed down to 1 ms. Of course, the other 350 ms or so that have nothing to do with the MAI still remain.
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

mattsc wrote: June 7th, 2018, 2:22 pm I just saw that you added that error report (I get notifications when a new post is made, but not when one is edited). I know what that message means, but there's no way of figuring out why it is happening without a test case. Do you still have the save from when you got this?
Unfortunately i dont have the save, I dont think so.. If i come across this again, I will save it and upload.
PS. I know about notifications, but that's what moderators want people like me to do... edit the posts instead of posting new.. so... ;(
mattsc wrote: June 7th, 2018, 2:22 pm Be ready to be surprised then. ;) We've implemented a number of changes to the filter evaluations for 1.14.3 (7be39c93, fb364cf2, 4b8870ed, 7727130e). With that, the lag time due to the AI evaluation at the end of the turn in the scenario you sent me is indeed down to 1 ms. Of course, the other 350 ms or so that have nothing to do with the MAI still remain.
That's great! Can't wait for 1.14.3 to be released ;)
mattsc wrote: June 7th, 2018, 2:22 pm As far as I know, the AI is only run on the host, so there's no need for the clients to also have the add-on when there is a local AI running. Should be easy to test though.
By the way I wanted to ask, you mentioned that if I rebuild the ai as you advised it would only run on host, but what would happen if host leaves the game and new host is different player? Would everything still work without oos? If you are not sure, then tell me at least what you think? In future I might test it, or is it better worth to wait for 1.14.3 and not to play with custom candidate action? I mean if 1.14.3 is really such a huge difference maker, then I would rather wait and test the speed gain there.. and if it's not sufficient then custom candidate action still may help possibly?
Thanks for everything!
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: [micro_ai] slow

Post by mattsc »

enclave wrote: June 8th, 2018, 12:33 am PS. I know about notifications, but that's what moderators want people like me to do... edit the posts instead of posting new.. so... ;(
Yeah, I know. This was meant as an explanation why I had not replied earlier, rather than a "complaint." There is some leeway with this though. You shouldn't make new posts with small incremental additions to previous thoughts, but a new bug report could get its own post so that it is noticed, as far as I know.
enclave wrote: June 8th, 2018, 12:33 am By the way I wanted to ask, you mentioned that if I rebuild the ai as you advised it would only run on host, but what would happen if host leaves the game and new host is different player? Would everything still work without oos? If you are not sure, then tell me at least what you think?
Actually, no, I won't guess. I really don't know and there's no point in me guessing because it wouldn't be worth anything since you'd just have to test it anyway. Maybe somebody else knows.
enclave wrote: June 8th, 2018, 12:33 am In future I might test it, or is it better worth to wait for 1.14.3 and not to play with custom candidate action? I mean if 1.14.3 is really such a huge difference maker, then I would rather wait and test the speed gain there.. and if it's not sufficient then custom candidate action still may help possibly?
1.14.3 is supposed to be tagged this weekend, so I'd say wait and see how it works for you. You can then always still add a custom CA later (the changes to 1.14.3 only address a couple very specific problems, so there might be other things you want to do).
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

mattsc wrote: June 8th, 2018, 3:44 pm 1.14.3 is supposed to be tagged this weekend, so I'd say wait and see how it works for you. You can then always still add a custom CA later (the changes to 1.14.3 only address a couple very specific problems, so there might be other things you want to do).
The first impression was that it works like magic! I didn't evaluate it with numbers/speeds yet, but it is obvious to the human eye that it works much much quicker. If my further observation show the same successful improvement of speed then I'm very happy with the results, currently I don't see why I would try to change anything, since [micro_ai] now is really quick and works perfectly fine. I will later try to add more micro_ais and see if things start to slow down or not. Thank you very much for a huge improvement job!!!!!!!!!
enclave
Posts: 936
Joined: December 15th, 2007, 8:52 am

Re: [micro_ai] slow

Post by enclave »

mattsc wrote: June 8th, 2018, 3:44 pm
Hello again mattsc, it's been a while since my last question, but I have some new ones now :)

1) What happens to micro_ai when host leave the game? I remember you mentioned something that it's executed only on host machine, I don't remember for sure.. but in my own experience when host leaves I suspect that micro ai stops working.. is that true? What could be done to avoid it? What could be done to change it? (from add-on creator point of view, - what can I do about it?)

2) What happens to micro_ai after reload? I suspect that it stops working after reload of saved game.. I looked the debug inspect and all goals seems to be there.. but I have no proof that anything still works, I'm quite sure it doesn't... am I wrong/right? What could be done about it? If i put micro ai code into preload event would it solve it? would it create double code? (so if i put in preload then i need to delete the old micro_ai IDs that were there from turn 1/prestart/start event?)

no rush with answers.. I don't plan to do anything about it straight away.. I don't have time for changes.. but in not so far future (2 weeks maybe) I will so I need to know already, when you have time to answer. Thanks.
User avatar
Celtic_Minstrel
Developer
Posts: 2166
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [micro_ai] slow

Post by Celtic_Minstrel »

I'm not mattsc, but I'm pretty sure a host leaving the game won't affect a micro_ai. Similarly, the micro_ai adds data into the saved game, so it should persist just fine through a reload. If it doesn't, that's probably a bug.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
mattsc
Inactive Developer
Posts: 1217
Joined: October 13th, 2010, 6:14 pm

Re: [micro_ai] slow

Post by mattsc »

Yeah, as Celtic_Minstrel says, neither of those should have an effect on a Micro AI. I did just do a quick test with the Goto MAI test scenario and it is definitely persistent there through reloading.
NotTalking
Posts: 2
Joined: July 28th, 2018, 7:41 pm

Re: [micro_ai] slow

Post by NotTalking »

mattsc wrote: November 19th, 2018, 7:45 pm Yeah, as Celtic_Minstrel says, neither of those should have an effect on a Micro AI. I did just do a quick test with the Goto MAI test scenario and it is definitely persistent there through reloading.
are you saying that you done the test and it works fine after reloading? thanks
which event did you have your micro_ai code?
Post Reply