//-------------------------------Update()-------------------------------- // // First we take sensor readings and feed these into the sweepers brain. // // The inputs are: // // The readings from the minesweeper's sensors // The readings from the minesweeper's feelers // bCollided // // We receive two outputs from the brain.. lTrack & rTrack. // So given a force for each track we calculate the resultant rotation // and acceleration and apply to current velocity vector. // //----------------------------------------------------------------------- bool CMinesweeper::Update(vector<SPoint> &objects) { //this will store all the inputs for the NN vector<double> inputs; //grab sensor readings TestSensors(objects); //input sensors into net for (int sr=0; sr<m_vecdSensors.size(); ++sr) { inputs.push_back(m_vecdSensors[sr]); inputs.push_back(m_vecFeelers[sr]); } inputs.push_back(m_bCollided); //update the brain and get feedback vector<double> output = m_ItsBrain.Update(inputs); //make sure there were no errors in calculating the //output if (output.size() < CParams::iNumOutputs) { return false; } //assign the outputs to the sweepers left & right tracks m_lTrack = output[0]; m_rTrack = output[1]; //calculate steering forces double RotForce = m_lTrack - m_rTrack; //clamp rotation Clamp(RotForce, -CParams::dMaxTurnRate, CParams::dMaxTurnRate); m_dRotation += RotForce; //update Look At m_vLookAt.x = -sin(m_dRotation); m_vLookAt.y = cos(m_dRotation); //if the sweepers haven't collided with an obstacle //update their position if (!m_bCollided) { m_dSpeed = (m_lTrack + m_rTrack); //update position m_vPosition += (m_vLookAt * m_dSpeed); } //update the memory map m_MemoryMap.Update(m_vPosition.x, m_vPosition.y); return true; }
//-------------------------------Update()-------------------------------- // // First we take sensor readings and feed these into the sweepers brain. // // The inputs are: // // The readings from the minesweepers sensors // // We receive two outputs from the brain.. lTrack & rTrack. // So given a force for each track we calculate the resultant rotation // and acceleration and apply to current velocity vector. // //----------------------------------------------------------------------- bool CMinesweeper::Update(vector<SPoint> &objects) { if (m_bActive) { //this will store all the inputs for the NN vector<double> inputs; //grab sensor readings TestSensors(objects); //input sensors into net for (int sr=0; sr<m_vecdSensors.size(); ++sr) { inputs.push_back(m_vecdSensors[sr]); // inputs.push_back(m_vecFeelers[sr]); // No need for feelers } // inputs.push_back(m_bCollided); double reward = m_MemoryMap.CheckReward(m_vPosition.x, m_vPosition.y, m_bReverse); if (reward > 0.9) { reward = 0.1; } else if (reward > 0) { reward = 1; } inputs.push_back(reward); double turningPoint = m_MemoryMap.CheckTurningPoint(m_vPosition.x, m_vPosition.y); inputs.push_back(turningPoint); vector<double> output; if (reward < 0.01) { //update the brain and get feedback output = m_pItsBrain->Update(inputs, CNeuralNet::active); } else { output = m_pItsBrain->Update(inputs, CNeuralNet::snapshot); } //make sure there were no errors in calculating the //output if (output.size() < CParams::iNumOutputs) { return false; } //assign the outputs to the sweepers left & right tracks // m_lTrack = output[0] * 2 - 1; //m_rTrack = output[1]; m_lTrack = output[0] * 2 - 1; //calculate steering forces //double RotForce = m_lTrack - m_rTrack; //clamp rotation //Clamp(RotForce, -CParams::dMaxTurnRate, CParams::dMaxTurnRate); //if(m_lTrack < -0.3) m_dRotation = 0;//3.14159265358979;//3.14159265358979f * (3.0 / 2.0); //else if(m_lTrack > 0.3) m_dRotation = 3.1415926358979f * 0.5; //else m_dRotation = 3.1415926358979f * 1.5; m_dRotation += m_lTrack; //update Look At m_vLookAt.x = -sin(m_dRotation); m_vLookAt.y = cos(m_dRotation); //if the sweepers haven't collided with an obstacle //update their position if (!m_bCollided) { m_dSpeed = 3;// + m_rTrack; //m_dSpeed *= 2; //update position m_vPosition += (m_vLookAt * m_dSpeed); //test range of x,y values - because of 'cheap' collision detection //this can go into error when using < 4 sensors TestRange(); } //update the memory map m_MemoryMap.Update(m_vPosition.x, m_vPosition.y); m_bActive = reward < 0.05; return true; } return true; }