|
| 1 | +/** |
| 2 | + * Any-Point Closseness loop function |
| 3 | + * |
| 4 | + * @file <loop-functions/mate/APC/MateAPCLoopFunc.cpp> |
| 5 | + * |
| 6 | + * @author Fernando Mendiburu - <[email protected]> |
| 7 | + * |
| 8 | + * @package experiments-loop-functions |
| 9 | + * |
| 10 | + * @license MIT License |
| 11 | + */ |
| 12 | + |
| 13 | +#include "MateAPCLoopFunc.h" |
| 14 | + |
| 15 | +/****************************************/ |
| 16 | +/****************************************/ |
| 17 | + |
| 18 | +MateAPCLoopFunction::MateAPCLoopFunction() { |
| 19 | + |
| 20 | + m_fSideSquare = 1.0; |
| 21 | + |
| 22 | + m_cCoordSquareSpot = CVector2(0.6, 0); |
| 23 | + |
| 24 | + m_unNumberPoints = 1000; |
| 25 | + |
| 26 | + m_fObjectiveFunction = 0; |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +/****************************************/ |
| 31 | +/****************************************/ |
| 32 | + |
| 33 | +MateAPCLoopFunction::MateAPCLoopFunction(const MateAPCLoopFunction& orig) {} |
| 34 | + |
| 35 | +/****************************************/ |
| 36 | +/****************************************/ |
| 37 | + |
| 38 | +MateAPCLoopFunction::~MateAPCLoopFunction() {} |
| 39 | + |
| 40 | +/****************************************/ |
| 41 | +/****************************************/ |
| 42 | + |
| 43 | +void MateAPCLoopFunction::Destroy() {} |
| 44 | + |
| 45 | +/****************************************/ |
| 46 | +/****************************************/ |
| 47 | + |
| 48 | +void MateAPCLoopFunction::Reset() { |
| 49 | + CoreLoopFunctions::Reset(); |
| 50 | +} |
| 51 | + |
| 52 | +/****************************************/ |
| 53 | +/****************************************/ |
| 54 | + |
| 55 | +argos::CColor MateAPCLoopFunction::GetFloorColor(const argos::CVector2& c_position_on_plane) { |
| 56 | + CVector2 cCurrentPoint(c_position_on_plane.GetX(), c_position_on_plane.GetY()); |
| 57 | + |
| 58 | + if (IsOnSquareArea(cCurrentPoint)){ |
| 59 | + return CColor::BLACK; |
| 60 | + } else{ |
| 61 | + return CColor::GRAY50; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/****************************************/ |
| 66 | +/****************************************/ |
| 67 | + |
| 68 | +void MateAPCLoopFunction::PostExperiment() { |
| 69 | + m_fObjectiveFunction = ComputeObjectiveFunction(); |
| 70 | + LOG << "Score: " << GetObjectiveFunction() << std::endl; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +/****************************************/ |
| 76 | +/****************************************/ |
| 77 | + |
| 78 | +Real MateAPCLoopFunction::GetObjectiveFunction() { |
| 79 | + return m_fObjectiveFunction; |
| 80 | +} |
| 81 | + |
| 82 | +/****************************************/ |
| 83 | +/****************************************/ |
| 84 | + |
| 85 | +Real MateAPCLoopFunction::ComputeObjectiveFunction() { |
| 86 | + CVector2 cRandomPoint; |
| 87 | + Real dA=0, dP=0; |
| 88 | + CSpace::TMapPerType mEpucks = GetSpace().GetEntitiesByType("epuck"); |
| 89 | + CVector2 cEpuckPosition(0,0); |
| 90 | + Real fDistanceToRandomPoint = 0; |
| 91 | + |
| 92 | + // White square area |
| 93 | + for(UInt32 i = 0; i < m_unNumberPoints; i++){ |
| 94 | + |
| 95 | + Real fMinDistanceOnSquare = 0.67; // Correspond to worst case, only one robot in the corner of the square |
| 96 | + |
| 97 | + cRandomPoint = RandomPointOnSquareArea(); |
| 98 | + |
| 99 | + for (CSpace::TMapPerType::iterator it = mEpucks.begin(); it != mEpucks.end(); ++it) { |
| 100 | + CEPuckEntity* pcEpuck = any_cast<CEPuckEntity*> ((*it).second); |
| 101 | + cEpuckPosition.Set(pcEpuck->GetEmbodiedEntity().GetOriginAnchor().Position.GetX(), |
| 102 | + pcEpuck->GetEmbodiedEntity().GetOriginAnchor().Position.GetY()); |
| 103 | + if(IsOnSquareArea(cEpuckPosition)){ |
| 104 | + fDistanceToRandomPoint = (cRandomPoint - cEpuckPosition).Length(); |
| 105 | + if(fDistanceToRandomPoint < fMinDistanceOnSquare){ |
| 106 | + fMinDistanceOnSquare = fDistanceToRandomPoint; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + dA += fMinDistanceOnSquare; |
| 112 | + } |
| 113 | + dA /= m_unNumberPoints; |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + Real performance = 100*100*dA*dA; // in cm2 |
| 119 | + |
| 120 | + return performance; |
| 121 | +} |
| 122 | + |
| 123 | +/****************************************/ |
| 124 | +/****************************************/ |
| 125 | + |
| 126 | +CVector2 MateAPCLoopFunction::RandomPointOnSquareArea(){ |
| 127 | + return CVector2(m_pcRng->Uniform(CRange<Real>(m_cCoordSquareSpot.GetX() - m_fSideSquare/2.0f, m_cCoordSquareSpot.GetX() + m_fSideSquare/2.0f)), |
| 128 | + m_pcRng->Uniform(CRange<Real>(m_cCoordSquareSpot.GetY() - m_fSideSquare/2.0f, m_cCoordSquareSpot.GetY() + m_fSideSquare/2.0f))); |
| 129 | +} |
| 130 | + |
| 131 | +/****************************************/ |
| 132 | +/****************************************/ |
| 133 | + |
| 134 | +bool MateAPCLoopFunction::IsOnSquareArea(const CVector2& c_point){ |
| 135 | + CRange<Real> cRangeSquareX(m_cCoordSquareSpot.GetX() - m_fSideSquare/2.0f, m_cCoordSquareSpot.GetX() + m_fSideSquare/2.0f); |
| 136 | + CRange<Real> cRangeSquareY(m_cCoordSquareSpot.GetY() - m_fSideSquare/2.0f, m_cCoordSquareSpot.GetY() + m_fSideSquare/2.0f); |
| 137 | + |
| 138 | + if (cRangeSquareX.WithinMinBoundIncludedMaxBoundIncluded(c_point.GetX()) && |
| 139 | + cRangeSquareY.WithinMinBoundIncludedMaxBoundIncluded(c_point.GetY())) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + return false; |
| 143 | +} |
| 144 | + |
| 145 | + |
| 146 | +/****************************************/ |
| 147 | +/****************************************/ |
| 148 | + |
| 149 | +CVector3 MateAPCLoopFunction::GetRandomPosition() { |
| 150 | + |
| 151 | + CVector3 cPosition; |
| 152 | + |
| 153 | + |
| 154 | + do { |
| 155 | + cPosition = CVector3(m_pcRng->Uniform(CRange<Real>(-m_fDistributionRadius,-0.6)), |
| 156 | + m_pcRng->Uniform(CRange<Real>(-m_fDistributionRadius,m_fDistributionRadius)), |
| 157 | + 0); |
| 158 | + } while(cPosition.Length()>=m_fDistributionRadius); |
| 159 | + |
| 160 | + |
| 161 | + return cPosition; |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +REGISTER_LOOP_FUNCTIONS(MateAPCLoopFunction, "mate_apc_loop_functions"); |
0 commit comments