Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Recursive functions slow?

Author
Message
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 3rd May 2017 19:11 Edited at: 4th May 2021 23:09
Hey,

I just ported a recursive algorithm from Blitz3D to AGK2. It's about the 8 queens puzzle:
https://en.wikipedia.org/wiki/Eight_queens_puzzle

Takes around 555 millisecs for Blitz3D to solve the puzzle 1000 times, whereas it takes AppGameKit around 16 to 19 seconds.

That's probably owned to the fact that AppGameKit is an interpreter, right?


PSY LABS Games
Coders don't die, they just gosub without return
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 3rd May 2017 19:49
It certainly has a factor that AppGameKit is interpreted.
Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200. Using AGK2 Tier 1.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 3rd May 2017 19:54
The 8 queens puzzle is what got me started on my game programming journey. I'd programmed simple stuff before, but this one really got my attention. It was a puzzle in Kings Quest, and I solved it using visual basic 3.0.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 3rd May 2017 20:24 Edited at: 16th Nov 2017 18:17
Yeah,

good old times...King's quest
Bought the Space Quest Saga recently on Steam

My first recursive algo was the Tower of Hanoi problem.


PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 3rd May 2017 20:44
can you show some code of your recursive call or an example?
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 4th May 2017 15:07

Quote: "That's probably owned to the fact that AppGameKit is an interpreter, right?"


It's be a big part of it with the choice of method. The cost of accessing any structure in these languages is high, to the point where simple redundancy have a big impact on the execution performance of any routine.


PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 4th May 2017 17:18 Edited at: 16th Nov 2017 18:18
@Markus

This is the original code in Blitz3D. Count shows the total number of solutions, which is 92 on a 8x8 chess board.
This code needs about 555ms on my machine for 1000 loops




This is the same code, ported to AGK. Needs about 15-18 seconds for 1000 loops



PSY LABS Games
Coders don't die, they just gosub without return
Stab in the Dark software
Valued Member
21
Years of Service
User Offline
Joined: 12th Dec 2002
Playing: Badges, I don't need no stinkin badges
Posted: 4th May 2017 20:11
Quote: "sleep ( 5000 )"


Maybe adding to the increased time?
The coffee is lovely dark and deep,and I have code to write before I sleep.
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 4th May 2017 21:27
my blitzbasic3d do it in 1.2 sec and in debug mode ~8 seconds ....
agk normal start 28 and debug start did nothing^^

AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
MikeHart
AGK Bronze Backer
21
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 4th May 2017 22:06 Edited at: 4th May 2017 22:09
The AGK2 version first runs around 13.5 seconds on my Imac.

If I change the isfree function so the end value of the for loop is precalculated, to


It takes only 10.8 seconds. If you split the If-Check into 2 statements, you can squeeze another .2 seconds out.

Interesting is also that if you define iy2 as a glbal at the beginning of the app, it runs then 1.1 seconds slower.
Running Windows 7 Home, 64 bit, 8 GB ram, Athlon II X2 255, ATI Radeon HD 4200. Using AGK2 Tier 1.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 5th May 2017 00:55
Quote: "This is the same code, ported to AGK. Needs about 15-18 seconds for 1000 loops"


10.5 seconds for me. And 11 seconds using MikeHart's change.

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 5th May 2017 01:04
It is executing the inner loop 46.7 Million times. Is that right?
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 5th May 2017 05:25
you can shuffle the code in isFree() around a bit.

Starting with,


pre compute loop counters



solve x[j] to remove the second array access, as generally variables are cheaper on the runtime than accessing an array.



anyway.. you should be able to shuffle (split up) the inner remove at least one of the ABS() functions... which would be quicker, but that's not going to get it up to speed. You can often precompute table permutations, but anyway..



PlayBASIC To HTML5/WEB - Convert PlayBASIC To Machine Code
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th May 2017 06:42
I managed to shave a little more time off by moving x[j] into a variable. This would suggest array access is more than twice as slow as variable access.

Quote: "Quote: "sleep ( 5000 )"
Maybe adding to the increased time?"

This is just displaying the result for 5 seconds.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Preben
AGK Studio Developer
20
Years of Service
User Offline
Joined: 30th Jun 2004
Location:
Posted: 5th May 2017 08:31
Here is a 5.41 sec version on my slow mac mini 1.4ghz.
Optimizing is fun

AGK solves ALL if conditions before evaluating so just splitting it out into 2 statements made the largest speed increase.





Anything else that could be made faster ? , perhaps bitwise removing the - bit instead of using Abs ?

best regards Preben Eriksen,
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 5th May 2017 16:16 Edited at: 16th Nov 2017 18:18
Quote: "It is executing the inner loop 46.7 Million times. Is that right?"

Yes

Thanks guys,

fact is I was planning to port a Negamax algo I once wrote for a 4-in-a-row game from B3D to AGK.
The 8 queens prob was about seeing how fast recursive functions generally run on AGK.

Seems to be too slow when being interpreted, though...


Cheers,
PSY


PSY LABS Games
Coders don't die, they just gosub without return
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th May 2017 18:36
It depends on the situation. If I was playing 4-in-a-row against a computer and it immediately responded every time I played, it would make a dull game.
A little bit of smoke and mirrors, and a few seconds is perfectly acceptable.
There are also only 8 moves you can ever make, you don't need to analyse every slot for the best move.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 5th May 2017 19:37 Edited at: 16th Nov 2017 18:18
Yeah, but

Player one makes a move, there are 8 possibilities the board can look like after the move
Player two makes a move, there are now 8x8 = 64 board positions
Player one makes a move again, there are now 64x8 = 512 possible board positions...and so on

If I analyze a search depth of 8 ( each player makes 4 moves ), there are 8^8 = 16.7 million board positions that need to be evaluated...


For 4-in-a-row, there are other algorithms, but the recursive negamax ( basically minimax with cutoff ) is what is used for this type of games in general ( chess, checkers etc. )

I'm gonna port it to AppGameKit and see how it performs


PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 5th May 2017 22:02 Edited at: 5th May 2017 22:02
can your recursive method
convert into iterative recursive methodic?
2. did paul compiled the agk in debug mode?
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th May 2017 16:34 Edited at: 16th Nov 2017 18:18
I dont think recursive iteration would be faster
Debugging works for me


PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 6th May 2017 18:23
Quote: "I dont think recursive iteration would be faster
Debugging works for me"


i tryed this iterative thing but i can't solved it



AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 6th May 2017 20:12 Edited at: 16th Nov 2017 18:19
So you're trying to solve the 8 Queens Problem without recursion?


PSY LABS Games
Coders don't die, they just gosub without return
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 8th May 2017 23:58 Edited at: 8th May 2017 23:59
Shaved a bit off with:

remove a loop:


Remove abs():
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th May 2017 00:14
Quote: "So you're trying to solve the 8 Queens Problem without recursion?"

not, i tryed only convert your code into this iterative thing
its a different technic that not call the function again, instead it runs in the same.
i found this by blind chance long ago, not used by myself but very insteresting.
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th May 2017 01:15 Edited at: 9th May 2017 01:37
Could you just go through the pieces and check if the angle to the other pieces is mod(angle, 45) = 0?
Unless you're making a computer player and have to figure out the next move
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 9th May 2017 01:36 Edited at: 16th Nov 2017 18:19
Just need to check what's above, so basically 45, 90, 135.
That's essentially what the code does, but without trigonometric functions, which are far slower


PSY LABS Games
Coders don't die, they just gosub without return
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 9th May 2017 01:37
Quote: "Could you just go through the pieces"

my solution, 7 Queens must die
AGK (Steam) V2017.03.31 : Windows 10 Pro 64 Bit : AMD (17.2.1) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th May 2017 01:40
Does the grid keep growing or is it a fixed size?
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th May 2017 01:50
Ok how about this;
As you add each piece you flag a 2d array (representing the board) of each square that cannot be occupied.
If the computer has to move then pick a unflagged square.
If the user moves they cannot select a flagged square
The game is over when you have flagged squares = total squares
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th May 2017 01:58 Edited at: 9th May 2017 02:48
I think i get it. If they place a queen on the grid you are trying to see if there's a possible solution is that it?

*edit*
There are 92 possible solutions to the 8 queens problem on an 8x8 grid. I would put them in an array and depending on where the player places a queen you could filter out the possibilities.
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 9th May 2017 22:43 Edited at: 16th Nov 2017 18:20
Quote: "my solution, 7 Queens must die "

Quickest solution ever

Quote: "Does the grid keep growing or is it a fixed size?"

The grid itself is fixed in most games

Quote: "Ok how about this;
As you add each piece you flag a 2d array (representing the board) of each square that cannot be occupied.
If the computer has to move then pick a unflagged square.
If the user moves they cannot select a flagged square
The game is over when you have flagged squares = total squares"

That's basically what the algo does
There is just one player

Quote: "I think i get it. If they place a queen on the grid you are trying to see if there's a possible solution is that it?"

The queen problem is about how to place 8 queens on a chessboard so they can't take each other, and how many possible solutions there are.
It's basically a 1 player game

Quote: "*edit*
There are 92 possible solutions to the 8 queens problem on an 8x8 grid. I would put them in an array and depending on where the player places a queen you could filter out the possibilities."

It's a one-time real-time problem

I just wanted to see if recursion can be used in AppGameKit on a 'deep' scale, meaning if AppGameKit can do a deep recursive evaluation of all possible moves up to a certain depth.
The algo I'm gonna use is called Negamax with alpha-beta pruning. It basically goes through all possible positions on any game board with a set depth.
You can see how it works here: https://en.wikipedia.org/wiki/Negamax#/media/File:Negamax_AlphaBeta.gif

So there is no precalculation, there is only real-time calculation.
The sole purpose of the 8 Queens Problem was to see whether AppGameKit ( being an interpreter ) is fast enough to do this or not.

The main reason I'm doing this is a project I once started in Blitz3D. It's a 4-in-a-row game with different opponents ( total beginner, expert, drunk guy, and so on ).
I want to port the algo to AppGameKit and finish the game.


Cheers,
PSY


PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-09-30 03:28:40
Your offset time is: 2024-09-30 03:28:40