|
| 1 | +#include "GiandujaBeaconStopLoopFunc.h" |
| 2 | + |
| 3 | +/****************************************/ |
| 4 | +/****************************************/ |
| 5 | + |
| 6 | +GiandujaBeaconStopLoopFunction::GiandujaBeaconStopLoopFunction() { |
| 7 | + m_fRadius = 0.10; |
| 8 | + //m_cCoordSpot1 = CVector2(0.6,0.8); |
| 9 | + m_CCoordRect1Pos = CVector2(0.8,-0.5); |
| 10 | + m_CCoordRect1 = CVector2(1.25,-0.5); |
| 11 | + |
| 12 | + m_CCoordRect2Pos = CVector2(-0.8,-1); |
| 13 | + //m_CCoordRect2 = CVector2(-1.25,-1.25); |
| 14 | + |
| 15 | + m_unCostI = 0; |
| 16 | + m_unCostO = 0; |
| 17 | + m_unState = 0; |
| 18 | + m_unTbar = 0; |
| 19 | + m_fObjectiveFunction = 0; |
| 20 | + m_unTime = 0; |
| 21 | +} |
| 22 | + |
| 23 | +/****************************************/ |
| 24 | +/****************************************/ |
| 25 | + |
| 26 | +GiandujaBeaconStopLoopFunction::GiandujaBeaconStopLoopFunction(const GiandujaBeaconStopLoopFunction& orig) {} |
| 27 | + |
| 28 | +/****************************************/ |
| 29 | +/****************************************/ |
| 30 | + |
| 31 | +GiandujaBeaconStopLoopFunction::~GiandujaBeaconStopLoopFunction() {} |
| 32 | + |
| 33 | +/****************************************/ |
| 34 | +/****************************************/ |
| 35 | + |
| 36 | +void GiandujaBeaconStopLoopFunction::Destroy() {} |
| 37 | + |
| 38 | +/****************************************/ |
| 39 | +/****************************************/ |
| 40 | + |
| 41 | +void GiandujaBeaconStopLoopFunction::Reset() { |
| 42 | + CoreLoopFunctions::Reset(); |
| 43 | + m_unCostI = 0; |
| 44 | + m_unCostO = 0; |
| 45 | + m_unState = 0; |
| 46 | + m_unTbar = 0; |
| 47 | + m_unTime = 0; |
| 48 | + m_fObjectiveFunction = 0; |
| 49 | + ExtractTime(); |
| 50 | + PlaceBeacon(); |
| 51 | + //Real a =m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 52 | + //Real b =m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 53 | + //m_cCoordSpot1 = CVector2(-0.7+a*1.4,0.6+b*0.4); |
| 54 | +} |
| 55 | + |
| 56 | +/****************************************/ |
| 57 | +/****************************************/ |
| 58 | + |
| 59 | +void GiandujaBeaconStopLoopFunction::Init(TConfigurationNode& t_tree) { |
| 60 | + CoreLoopFunctions::Init(t_tree); |
| 61 | + ExtractTime(); |
| 62 | + PlaceBeacon(); |
| 63 | + //Real a =m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 64 | + //Real b =m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 65 | + //m_cCoordSpot1 = CVector2(-0.7+a*1.4,0.6+b*0.4); |
| 66 | + //LOG << "Spot is at :" << m_cCoordSpot1 << std::endl; |
| 67 | +} |
| 68 | + |
| 69 | +argos::CColor GiandujaBeaconStopLoopFunction::GetFloorColor(const argos::CVector2& c_position_on_plane) { |
| 70 | + CVector2 vCurrentPoint(c_position_on_plane.GetX(), c_position_on_plane.GetY()); |
| 71 | + |
| 72 | + // Real d = (m_cCoordSpot1 - vCurrentPoint).Length(); |
| 73 | + // |
| 74 | + // if (d <= m_fRadius) { |
| 75 | + // return CColor::WHITE; |
| 76 | + // } |
| 77 | + |
| 78 | + if ( (vCurrentPoint.GetX()<=m_CCoordRect1Pos.GetX()) && (vCurrentPoint.GetX()>=m_CCoordRect2Pos.GetX()) && (vCurrentPoint.GetY()>=m_CCoordRect2Pos.GetY()) && (vCurrentPoint.GetY()<=m_CCoordRect1Pos.GetY()) ) { |
| 79 | + return CColor::GREEN; |
| 80 | + } |
| 81 | + |
| 82 | + if ( (vCurrentPoint.GetX()<=m_CCoordRect1.GetX()) && (vCurrentPoint.GetX()>=m_CCoordRect2.GetX()) && (vCurrentPoint.GetY()>=m_CCoordRect2.GetY()) && (vCurrentPoint.GetY()<=m_CCoordRect1.GetY()) ) { |
| 83 | + return CColor::WHITE; |
| 84 | + } |
| 85 | + |
| 86 | + return CColor::GRAY50; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +void GiandujaBeaconAggregationLoopFunction::ExtractTime() { |
| 91 | + try { |
| 92 | + CEPuckEntity& cEntity = dynamic_cast<CEPuckEntity&>(GetSpace().GetEntity("beacon0")); |
| 93 | + CEPuckBeacon& cController = dynamic_cast<CEPuckBeacon&>(cEntity.GetControllableEntity().GetController()); |
| 94 | + m_Tbar = cController.getTBar(); |
| 95 | + //LOG << "Time=" << m_Tbar << std::endl; |
| 96 | + } catch (std::exception& ex) { |
| 97 | + LOGERR << "Error while casting ExtractTime: " << ex.what() << std::endl; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +void GiandujaBeaconStopLoopFunction::PlaceBeacon() { |
| 103 | + try { |
| 104 | + CEPuckEntity& cEpuck = dynamic_cast<CEPuckEntity&>(GetSpace().GetEntity("beacon0")); |
| 105 | + MoveEntity(cEpuck.GetEmbodiedEntity(), |
| 106 | + CVector3(0, -0.5, 0), |
| 107 | + CQuaternion().FromEulerAngles(CRadians::ZERO, |
| 108 | + CRadians::ZERO,CRadians::ZERO),false); |
| 109 | + } catch (std::exception& ex) { |
| 110 | + LOGERR << "Error while casting PlaceBeacon: " << ex.what() << std::endl; |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +CVector3 GiandujaBeaconStopLoopFunction::GetRandomPosition() { |
| 117 | + Real a; |
| 118 | + Real b; |
| 119 | + |
| 120 | + a = m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 121 | + b = m_pcRng->Uniform(CRange<Real>(0.0f, 1.0f)); |
| 122 | + |
| 123 | + Real fPosX = m_CCoordRect2Pos.GetX() + a*fabs(m_CCoordRect2Pos.GetX() - m_CCoordRect1Pos.GetX()); |
| 124 | + Real fPosY = m_CCoordRect2Pos.GetY() + b*fabs(m_CCoordRect2Pos.GetY() - m_CCoordRect1Pos.GetY()); |
| 125 | + |
| 126 | + return CVector3(fPosX, fPosY, 0); |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +/****************************************/ |
| 131 | +/****************************************/ |
| 132 | + |
| 133 | +void GiandujaBeaconStopLoopFunction::PostStep() { |
| 134 | + CSpace::TMapPerType& tEpuckMap = GetSpace().GetEntitiesByType("epuck"); |
| 135 | + CVector2 cEpuckPosition(0,0); |
| 136 | + UInt8 un_trigger = 0; |
| 137 | + for (CSpace::TMapPerType::iterator it = tEpuckMap.begin(); it != tEpuckMap.end(); ++it) { |
| 138 | + CEPuckEntity* pcEpuck = any_cast<CEPuckEntity*>(it->second); |
| 139 | + cEpuckPosition.Set(pcEpuck->GetEmbodiedEntity().GetOriginAnchor().Position.GetX(), |
| 140 | + pcEpuck->GetEmbodiedEntity().GetOriginAnchor().Position.GetY()); |
| 141 | + |
| 142 | + Real fDistanceSpot1 = (m_cCoordSpot1 - cEpuckPosition).Length(); |
| 143 | + if (m_unTime > m_unTbar) { |
| 144 | + if (m_unState == 0) { |
| 145 | + if (fDistanceSpot1 <= m_fRadius) { |
| 146 | + un_trigger = 1; |
| 147 | + } |
| 148 | + else if ( fabs(m_tOldPosPoints[pcEpuck].GetX() - cEpuckPosition.GetX()) < 0.005 && fabs(m_tOldPosPoints[pcEpuck].GetY() - cEpuckPosition.GetY()) < 0.005) { |
| 149 | + m_unCostI+=1; |
| 150 | + } |
| 151 | + m_unTbar +=1; |
| 152 | + } |
| 153 | + else if (m_unState == 1) { |
| 154 | + if ( fabs(m_tOldPosPoints[pcEpuck].GetX() - cEpuckPosition.GetX()) > 0.005 && fabs(m_tOldPosPoints[pcEpuck].GetY() - cEpuckPosition.GetY()) > 0.005) { |
| 155 | + m_unCostO+=1; |
| 156 | + } |
| 157 | + } |
| 158 | + m_tOldPosPoints[pcEpuck] = cEpuckPosition; |
| 159 | + } |
| 160 | + else { |
| 161 | + if ( (cEpuckPosition.GetX()<=m_CCoordRect1.GetX()) && (cEpuckPosition.GetX()>=m_CCoordRect2.GetX()) && (cEpuckPosition.GetY()>=m_CCoordRect2.GetY()) && (cEpuckPosition.GetY()<=m_CCoordRect1.GetY()) ) { |
| 162 | + m_unCostI+=1 |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (m_unState == 0 && un_trigger == 1) { |
| 168 | + m_unState = 1; |
| 169 | + LOG << "Found spot triggered." << std::endl; |
| 170 | + } |
| 171 | + m_unTime+=1; |
| 172 | +} |
| 173 | + |
| 174 | +/****************************************/ |
| 175 | +/****************************************/ |
| 176 | + |
| 177 | +void GiandujaBeaconStopLoopFunction::PostExperiment() { |
| 178 | + LOG<< "CostI :" << m_unCostI << " / CostO :" << m_unCostO << " / Tbar:" << m_unTbar << std::endl; |
| 179 | + LOG<< 48000 - (m_unCostI + m_unCostO + m_unTbar) << std::endl; |
| 180 | + m_fObjectiveFunction = (Real) 48000 - (m_unCostI + m_unCostO + m_unTbar); |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | +Real GiandujaBeaconStopLoopFunction::GetObjectiveFunction() { |
| 185 | + return (m_fObjectiveFunction); |
| 186 | +} |
| 187 | + |
| 188 | +REGISTER_LOOP_FUNCTIONS(GiandujaBeaconStopLoopFunction, "gianduja_stop_loop_functions"); |
0 commit comments