Skip to content

Commit

Permalink
too early too late too jesus
Browse files Browse the repository at this point in the history
  • Loading branch information
francescoriccio committed Mar 12, 2014
1 parent d9ecaa6 commit 94b6fc2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 43 deletions.
108 changes: 72 additions & 36 deletions machineLearning/DiveHandler/DiveHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
bool stamp =false;
bool tooEarly=false;
bool estimatedTime=false;
bool goalDetected=false;

MAKE_MODULE(DiveHandler, SPQR-Modules)

Expand Down Expand Up @@ -612,25 +613,42 @@ void DiveHandler::update(DiveHandle& diveHandle)
// Update the DiveHandle
diveHandle.ballProjectionEstimate = ballProjectionIntercept;

if( ((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) > 10001 &&
((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) < 10050 &&
(int) timer.fallen != 0)
#ifdef DIVEHANDLER_TRAINING
if( timer.getTimeSince(timer.fallen) > 10000 && timer.getTimeSince(timer.fallen) < 10050 && timer.fallen != 0)
SPQR_SUCCESS("TooEarly time window START...");
#endif
if( ((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) > 14971 &&
((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) < 14999 &&
(int) timer.fallen != 0)
#ifdef DIVEHANDLER_TRAINING
if( timer.getTimeSince(timer.fallen) > 14971 && timer.getTimeSince(timer.fallen) < 14999 && timer.fallen != 0)
SPQR_SUCCESS("TooEarly time window END.");
#endif
if( ((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) > 10000 &&
((int) (clock() - timer.fallen)/(CLOCKS_PER_SEC/1000)) < 15000 &&
(int) timer.fallen != 0)

if(opponentScore != (int)theOpponentTeamInfo.score && !goalDetected)
{
if(opponentScore != (int)theOpponentTeamInfo.score)
tooEarly=true;
if( timer.getTimeSince(timer.fallen) > 10000 && timer.getTimeSince(timer.fallen) < 15000 &&
(unsigned int) timer.fallen != 0)
{
#ifdef DIVEHANDLER_TRAINING
SPQR_FAILURE("too FAST dude!");
#endif
estimatedInterval += 3000;
}
else
{
// if(goalTimer.setTimer)
{
#ifdef DIVEHANDLER_TRAINING
SPQR_FAILURE("too SLOW dude!");
#endif
estimatedInterval = goalTimer.getTimeSince(goalTimer.start) -500;
}
}
estimatedTime=true;
goalDetected=true;
}

if(theGameInfo.state == STATE_SET)
goalTimer.reset();

// Check whether the ball is close enough
if( (distanceBall2Goal < SPQR::FIELD_DIMENSION_X) && (fabs(ballProjectionIntercept) < SPQR::FIELD_DIMENSION_Y) )
{
Expand All @@ -640,50 +658,67 @@ void DiveHandler::update(DiveHandle& diveHandle)
if(state != notLearning)
{
// if not in playing state

if(theGameInfo.state != STATE_PLAYING)
timer.reset();
else
{
// if(goalTimer.setTimer)
// SPQR_INFO("time: "<< goalTimer.getTimeSince(goalTimer.start));

// if the ball is moving enough fast then set the timer
if( !timer.setTimer && (theBallModel.estimate.velocity.abs() > SPQR::MOVING_BALL_MIN_VELOCITY &&
if( (theBallModel.estimate.velocity.abs() > SPQR::MOVING_BALL_MIN_VELOCITY &&
theFrameInfo.getTimeSince(theBallModel.timeWhenLastSeen) < 1000) )
timer.set(clock());
// else reset it...
if( timer.setTimer && (theBallModel.estimate.velocity.abs() < SPQR::MOVING_BALL_MIN_VELOCITY ||
theFrameInfo.getTimeSince(theBallModel.timeWhenLastSeen) > 1000) )
timer.reset();

// if the goalie dives
if( (int)theFallDownState.state == (int)FallDownState::fallen )
{
timer.fallen=clock();
estimatedInterval = (int) (clock() - timer.start)/(CLOCKS_PER_SEC/1000);
if(!timer.setTimer)
{
timer.set(clock());
#ifdef DIVEHANDLER_TRAINING
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "set Timer!" << "\033[0m" << std::endl;
#endif
goalTimer.set(clock());
#ifdef DIVEHANDLER_TRAINING
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "set goal Timer!" << "\033[0m" << std::endl;
#endif
}
}

if(opponentScore != (int)theOpponentTeamInfo.score && !estimatedTime)
// else reset it...
if( (theBallModel.estimate.velocity.abs() < SPQR::MOVING_BALL_MIN_VELOCITY ||
theFrameInfo.getTimeSince(theBallModel.timeWhenLastSeen) > 4000) )
{
if( tooEarly )
if(timer.setTimer)
{
SPQR_FAILURE("too FAST dude!");
estimatedInterval = timer.fallen + 3000;
tooEarly=false;
timer.reset();
#ifdef DIVEHANDLER_TRAINING
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "reset Timer!" << "\033[0m" << std::endl;
#endif
}
else
if(goalTimer.setTimer)
{
SPQR_FAILURE("too SLOW dude!");
estimatedInterval += (int)(clock() - timer.fallen)/(CLOCKS_PER_SEC/1000) - 500;
goalTimer.reset();
#ifdef DIVEHANDLER_TRAINING
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "reset goal Timer!" << "\033[0m" << std::endl;
#endif
}
estimatedTime=true;

}

// if the goalie succeeded
else if(ownScore != (int)theOwnTeamInfo.score && !estimatedTime)
if(ownScore != (int)theOwnTeamInfo.score && !estimatedTime)
{
#ifdef DIVEHANDLER_TRAINING
SPQR_SUCCESS("SUPER!");
#endif
estimatedInterval -= 200;
estimatedTime=true;
}

// if the goalie dives
if( (int)theFallDownState.state == (int)FallDownState::fallen )
{
timer.fallen=clock();
estimatedInterval = timer.getTimeSince(timer.start);
}

}
}

Expand Down Expand Up @@ -715,7 +750,7 @@ void DiveHandler::update(DiveHandle& diveHandle)
else if( state == waitReward )
{
// The opponent team scores: the goalie failed and gets a negative reward
if(opponentScore != (int)theOpponentTeamInfo.score && estimatedTime)
if(goalDetected && estimatedTime)
{
// The learner obtains a negative reward
rewardHistory.push_front(NEGATIVE_REWARD);
Expand All @@ -731,10 +766,11 @@ void DiveHandler::update(DiveHandle& diveHandle)
#endif
// A reward has been received: re-enable learning
state = learning;
// Clear the pending rewardelse
// Clear the pending reward
if(!diveHandle.rewardAck)
diveHandle.rewardAck = true;

goalDetected=false;
estimatedTime=false;
stamp =true;
}
Expand Down
16 changes: 9 additions & 7 deletions machineLearning/DiveHandler/DiveHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ END_MODULE
#define BUFFER_DIM 10
#define REWARDS_HISTORY_SIZE 10
#define EPSILON 0.05
#define T 5
#define T 15
// Evaluation weight
#define LAMBDA1 0.9
//#define LAMBDA2 0.3
Expand Down Expand Up @@ -208,26 +208,28 @@ class DiveHandler : public DiveHandlerBase
bool setTimer;

Timer():start(0), fallen(0), setTimer(false){}

inline unsigned int getTimeSince(clock_t startTime)
{
return (unsigned int) ((clock() - startTime)/(CLOCKS_PER_SEC/1000));
}
inline void set(clock_t startTime)
{
if(!setTimer)
// if(!setTimer)
{
start = startTime;
setTimer = true;
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "set Timer!" << "\033[0m" << std::endl;
}
}
inline void reset()
{
if(setTimer)
{
// if(setTimer)
setTimer = false;
std::cerr << "\033[33;1m" <<"[DiveHandler] " << "reset Timer!" << "\033[0m" << std::endl;
}
}
};

Timer timer;
Timer goalTimer;
unsigned int estimatedInterval;

// Estimated intersection between the ball projection and the goal line
Expand Down

0 comments on commit 94b6fc2

Please sign in to comment.