00001
00002
00003 #include <iostream>
00004
00005 #include <dtGame/gmcomponent.h>
00006 #include <controllers/fitnessFramework/choreoFitness.h>
00007 #include <controllers/fitnessFramework/almChoreoAgent.h>
00008
00009 namespace srAlmende {
00010
00024 ChoreoFitness::ChoreoFitness(const std::string &name) :
00025 dtGame::GMComponent(name),
00026 trial_length (300),
00027 trial_tick (0),
00028 enabled(false) {
00029 }
00030
00041 unsigned char ChoreoFitness::CalcFitness(unsigned int time_tick,
00042 unsigned char actual_success, unsigned char perceived_success) {
00043 if (time_tick == 0) return 0xFF;
00044 int diff = (int)actual_success - (int)perceived_success;
00045 if (diff < 0) diff = 0 - diff;
00046
00047 unsigned char periods = 5;
00048
00049 unsigned char ratio = trial_length * periods / time_tick;
00050
00051 return (ratio * diff) + (periods - ratio) * actual_success;
00052 }
00053
00059 unsigned char ChoreoFitness::Synchronized(std::vector <osg::Vec3f> orientations) {
00060 std::vector<osg::Vec3f>::const_iterator i;
00061 osg::Vec3f start = *orientations.begin(), result;
00062 for( i = orientations.begin()+1; i < orientations.end(); ++i){
00063 result = *i - start;
00064 }
00065 return result.x() + result.y() + result.z();
00066 }
00067
00071 unsigned char ChoreoFitness::Average(std::vector<unsigned char> values) {
00072 std::vector<unsigned char>::const_iterator i;
00073 unsigned int result = 0;
00074 for( i = values.begin(); i < values.end(); ++i){
00075 result += *i;
00076 }
00077 return (unsigned char)(result / values.size());
00078 }
00079
00087 void ChoreoFitness::ProcessMessage(const dtGame::Message& message) {
00088
00089 if ((message.GetMessageType() == dtGame::MessageType::TICK_LOCAL) && (enabled)) {
00090
00091 std::vector< osg::Vec3f> positions, orientations;
00092 std::vector< unsigned char> successes;
00093 unsigned char success, synchronized;
00094
00095
00096
00097
00098 if (trial_tick >= trial_length) {
00099 std::vector< dtGame::GMComponent* > allComponents;
00100 GetGameManager()->GetAllComponents(allComponents);
00101 for (unsigned int i = 0; i < allComponents.size(); i++) {
00102 std::string name = allComponents[i]->GetName();
00103 if(name.find("almChoreoAgent",0) == std::string::npos) continue;
00104 AlmChoreoAgent *agent = dynamic_cast<AlmChoreoAgent*>(allComponents[i]);
00105 dtCore::RefPtr<srExternal::NonEmulationMessage> tcpipMsg =
00106 new srExternal::NonEmulationMessage();
00107 GetGameManager()->GetMessageFactory().CreateMessage(
00108 srExternal::NonEmulationMessageType::TCPIP, tcpipMsg);
00109 if (GetGameManager()->GetMessageFactory().IsMessageTypeSupported(
00110 tcpipMsg->GetMessageType())) {
00111 dtCore::UniqueId *id = agent->getBusID();
00112 tcpipMsg->setBusID(id);
00113 std::ostringstream msg_stream;
00114 unsigned char msg_size = 5;
00115 unsigned char origin = 0xFF;
00116 msg_stream << msg_size;
00117 msg_stream << origin;
00118 msg_stream << fitness;
00119 tcpipMsg->setMessage(msg_stream.str());
00120 tcpipMsg->setMsgType(srExternal::NonEmulationMessageType::FITNESS);
00121 GetGameManager()->SendMessage(*tcpipMsg);
00122 std::cout << "Send a fitness message" << std::endl;
00123 }
00124 }
00125 enabled = false;
00126 fitness = 0;
00127 trial_tick = 0;
00128 }
00129
00130 positions.clear(); orientations.clear(); successes.clear();
00131
00132 std::vector< dtGame::GMComponent* > allComponents;
00133 GetGameManager()->GetAllComponents(allComponents);
00134 for (unsigned int i = 0; i < allComponents.size(); i++) {
00135 std::string name = allComponents[i]->GetName();
00136 if(name.find("almChoreoAgent",0) == std::string::npos) continue;
00137
00138 AlmChoreoAgent *agent = dynamic_cast<AlmChoreoAgent*>(allComponents[i]);
00139 successes.push_back(agent->getChoreoSignal());
00140 positions.push_back(agent->getRobotPosition());
00141 orientations.push_back(agent->getRobotRotation());
00142 }
00143
00144 if (successes.empty()) {
00145 std::cout << "Choreo Fitness module can't find any Choreo Agents." << std::endl;
00146 std::cout << " It will disable itself." << std::endl;
00147 enabled = false;
00148 return;
00149 }
00150
00151 synchronized = Synchronized(orientations);
00152
00153 success = Average(successes);
00154 fitness += CalcFitness(trial_tick, synchronized, success);
00155
00156
00157 }
00158
00162 if (message.GetMessageType() == srExternal::NonEmulationMessageType::TCPIP) {
00163 srExternal::NonEmulationMessage *neMsg = dynamic_cast<srExternal::NonEmulationMessage*>(
00164 const_cast<dtGame::Message*>(&message));
00165 srExternal::NonEmulationMessageType::msgType type = neMsg->getMsgType();
00166 switch(type) {
00167 case srExternal::NonEmulationMessageType::POSITION: {
00168
00169 trial_tick = 0;
00170 enabled = true;
00171 break;
00172 }
00173 case srExternal::NonEmulationMessageType::ACTUATOR: {
00174
00175 trial_tick++;
00176 break;
00177 }
00178 default: break;
00179 }
00180
00181 }
00182
00183 }
00184
00185 }
00186
00187