Qbasicnews.com
April 09, 2020, 05:28:47 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Help! Efficiency freak *meets his match! :(  (Read 1951 times)
Agamemnus
x/ \z
*****
Posts: 3491



« on: February 11, 2003, 02:48:17 PM »

I need advice with this.. I'm doing a genetic program program for a competition. (www.gridwars.com) It will spit out genetic programs eventually, once I'm done. Right now I have a random player, but I would like to make this faster with bits... It might not make it faster, just use less space. I don't know. (see comments)

I need it to be as fast as possible because I'll be calling this routine A LOT, since genetic programming (creating random programs and then evolving them) takes a long time. (if you want something good)

If I can't get the vars to work how I want them, I know I can do what I want in C/C++, but I want to use QB's string manipulation and I/O functions (for later).. and I don't know much about that in C/C++.

Suggestions?

Code:

'by Agamemnus
'competition "GRID WARS": www.gridwars.com

DECLARE SUB think.routine (n%)
DIM SHARED a.max%(2): a.max%(1) = 50: a.max%(2) = 50

DIM SHARED board(a.max%(1), a.max%(2)) AS INTEGER
DIM SHARED start.loc(8, 2) AS INTEGER

'bullets. stores the bullets
'maximum value: 8 for each board position.
'can represent as: 8x3bits + 3 bits to determine last read position.
'27 bits PREFERRABLE, but impossible:
'last index should function as the nth bullet, and value as cell-number:
'DIM SHARED bullets(a.max%(1),a.max%(2), 8) AS BITS * 3
'DIM SHARED bullets.lastread(a.max%(1),a.max%(2)) AS BITS * 3

'currently, last index functions as cell-number and value is how many bullets.
'also requires 13 less bits to achieve!!
DIM SHARED bullets(a.max%(1), a.max%(2), 8) AS INTEGER
DIM SHARED bullet.loc(2) AS INTEGER, bullet.loc.a(2, 2) AS INTEGER
DIM SHARED x%, y%
DIM SHARED b.amount%(8)

SCREEN 7
RANDOMIZE 1233
'create random start locations.

FOR I% = 1 TO 8
redo1:
start.loc(I%, 1) = INT(RND * a.max%(1))
start.loc(I%, 2) = INT(RND * a.max%(2))
IF board(start.loc(I%, 1), start.loc(I%, 2)) <> 0 THEN GOTO redo1
board(start.loc(I%, 1), start.loc(I%, 2)) = I%
NEXT I%

time1$ = TIME$
LOCATE 12, 1: PRINT time1$
'DO:LOCATE 12, 1: PRINT TIME$:LOOP UNTIL MID$(TIME$, 7, 2) = "00"


DO
n1 = n1 + 1

'draw.cells:
FOR x% = 1 TO a.max%(1)
FOR y% = 1 TO a.max%(2)
PSET (x%, y%), board(x%, y%)
NEXT y%, x%

'add.bullets:      
REDIM SHARED bullets(a.max%(1), a.max%(2), 8) AS INTEGER
REDIM SHARED bullets.tot(a.max%(1), a.max%(2)) AS INTEGER
FOR x% = 1 TO a.max%(1)
FOR y% = 1 TO a.max%(2)
IF board(x%, y%) <> 0 THEN
b2% = board(x%, y%)
think.routine 0 'random
FOR I% = 0 TO 1
xtemp% = x% + bullet.loc.a%(I%, 1)
ytemp% = y% + bullet.loc.a%(I%, 2)
IF xtemp% < 1 THEN xtemp% = a.max%(1)
IF xtemp% > a.max%(1) THEN xtemp% = 1
IF ytemp% < 1 THEN ytemp% = a.max%(2)
IF ytemp% > a.max%(2) THEN ytemp% = 1
bullets(xtemp%, ytemp%, b2%) = bullets(xtemp%, ytemp%, b2%) + 1
NEXT I%
END IF
NEXT y%, x%

'update.cells:

FOR x% = 1 TO a.max%(1)
FOR y% = 1 TO a.max%(2)
IF bullets(x%, y%, 1) + bullets(x%, y%, 2) + bullets(x%, y%, 3) + bullets(x%, y%, 4) + bullets(x%, y%, 5) + bullets(x%, y%, 6) + bullets(x%, y%, 7) + bullets%(x%, y%, 8) <> 0 THEN

b.amount%(0) = bullets(x%, y%, 0)
best.amount% = b.amount%(0)
k% = 1

FOR I% = 2 TO 8
b.amount%(I%) = bullets(x%, y%, I%)
IF b.amount%(I%) > best.amount% THEN best.amount% = b.amount%(I%): k% = I%
NEXT I%

IF b.amount%(board(x%, y%)) <> best.amount% THEN
IF best.amount% <> 0 THEN
board(x%, y%) = k%
END IF
END IF

END IF
NEXT y%, x%

IF n1 = 100 THEN EXIT DO
LOOP UNTIL INKEY$ <> ""
LOCATE 13, 1: PRINT TIME$
time2$ = TIME$
SYSTEM

DEFINT A-Z
SUB think.routine (n%)
IF n% = 0 THEN
bullet.loc%(0) = INT(RND * 8) + 1
bullet.loc%(1) = INT(RND * 8) + 1
ELSE
END IF

'convert bullet loc to coordinates.
'xxx 345
'x x 2 6
'xxx 187

FOR I% = 0 TO 1
IF bullet.loc%(I%) = 8 THEN
bullet.loc.a%(I%, 1) = 0
bullet.loc.a%(I%, 2) = -1
ELSE
IF bullet.loc%(I%) = 4 THEN
bullet.loc.a%(I%, 1) = 0
bullet.loc.a%(I%, 2) = 1
ELSE
IF bullet.loc%(I%) < 4 THEN
bullet.loc.a%(I%, 1) = -1
bullet.loc.a%(I%, 2) = bullet.loc%(I%) - 2
ELSE
bullet.loc.a%(I%, 1) = 1
bullet.loc.a%(I%, 2) = -bullet.loc%(I%) + 6
END IF
END IF
END IF
NEXT I%
END SUB
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #1 on: February 11, 2003, 07:54:01 PM »

Sad

Um, never mind, I guess. I'll be done in a few days and post results in Projects..
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!