It is currently Fri Oct 19, 2018 5:51 am Advanced search

Exploit available calculation time optimally

Code won't compile? Found a bug? Post here!

Exploit available calculation time optimally

Postby superjhemp » Wed Feb 10, 2010 1:24 pm

Hi,

I wonder how I can tell my program (in C++) to continue calculating until the allowed 1s is over. It does a short calculation, checks the time, and if it thinks that there is enough time left for another calculation round, it goes for it.

Unfortunately I don't know how to implement this. Here's what I did (it's basically the starter-kit code plus something that doesn't seem to work), and this causes my code to be suspended!

clock_t start, stop; // from time.h
double t;
double MAXTIME 0.9;
int move;

int MakeMove(const Map& map)
{
do
{
// do some fancy calculations
stop = clock();
t = (double) (stop-start)/CLOCKS_PER_SEC;
} while (t < MAXTIME);

return move;
}

int main()
{
while (true)
{
start = clock();
Map map;
Map::MakeMove(MakeMove(map));
}
return 0;
}

A) The first problem (at least on my PC) is that 'Map map' takes already more than one second by itself
Strange, because then not even the starter packages would work

B) If I test my code on the java engine as player B, the stopwatch seems to start for player B already when player A reads its move, although player B can read its move only once A is done.

Can you please teach me how to use any time-measuring algorithm correctly with your judge?

Thanks a lot
superjhemp
Cadet
 
Posts: 7
Joined: Wed Feb 10, 2010 1:09 pm

Re: Exploit available calculation time optimally

Postby JJNoakes » Wed Feb 10, 2010 6:44 pm

Don't start timing before you create the Map object - that constructor has to wait until a map is provided to your bot on STDIN. This may wait around while the current turn is being finished before you get a map for the next turn.
JJNoakes
Lieutenant
 
Posts: 19
Joined: Fri Feb 05, 2010 1:13 am

Re: Exploit available calculation time optimally

Postby ebrahim » Wed Feb 10, 2010 10:45 pm

I haven't tried SIGALRM, but I guess it would work nicely and less intrusive.
ebrahim
Lieutenant-Colonel
 
Posts: 49
Joined: Mon Feb 08, 2010 7:03 pm

Re: Exploit available calculation time optimally

Postby superjhemp » Thu Feb 11, 2010 8:03 am

Thanks, the first reply helped! For the second one, I didn't try it either.
superjhemp
Cadet
 
Posts: 7
Joined: Wed Feb 10, 2010 1:09 pm

Re: Exploit available calculation time optimally

Postby barxool » Sun Feb 14, 2010 5:54 pm

Hi

i have two questions that will help everyone to manage time ressources:

1) Bots got 1s of CPU times or 1s even if they have CPU for half a second in that second ?
2) Is the server a linux machine or windows machine ? High resolution timer do not use the same function in linux (gettimeofday()) and windows (QueryPerformanceCounter())

cheers
barxool
Lieutenant
 
Posts: 13
Joined: Wed Feb 10, 2010 9:22 pm


Return to Technical Issues

Who is online

Users browsing this forum: No registered users and 1 guest

cron