28 #include "SuperBallLearningController.h"
43 this->m_initialLengths=initialLength;
44 this->m_totalTime=0.0;
54 const std::vector<tgBasicActuator*> muscles = subject.
getAllMuscles();
55 for (
size_t i = 0; i < muscles.size(); ++i)
58 assert(pMuscle != NULL);
64 double randomAngle=((rand() / (double)RAND_MAX) - 0.5) * 3.1415;
65 randomAngle=3.1415/2.0;
68 btDynamicsWorld *btworld=subject.getWorld();
69 boxTr.setOrigin(btVector3(1000*cos(randomAngle),50,1000*sin(randomAngle)));
70 btDefaultMotionState* motSt =
new btDefaultMotionState(boxTr);
71 btRigidBody::btRigidBodyConstructionInfo
const rbInfo1(1,motSt,
new btBoxShape(btVector3(1.0,1.0,1.0)));
72 goalPoint =
new btRigidBody(rbInfo1);
73 btworld->addRigidBody(goalPoint);
76 boxTr.setOrigin(btVector3(0,1.1,0));
77 btDefaultMotionState* motSt2 =
new btDefaultMotionState(boxTr);
78 btRigidBody::btRigidBodyConstructionInfo
const rbInfo2(0.0,motSt2,
new btBoxShape(btVector3(500.0,0.1,500.0)));
79 btRigidBody *groundBox =
new btRigidBody(rbInfo2);
80 btworld->addRigidBody(groundBox);
90 throw std::invalid_argument(
"dt is not positive");
95 if(initialPosition.empty())
97 btVector3 pos= subject.getCenter();
98 initialPosition.push_back(pos[0]);
99 initialPosition.push_back(pos[2]);
103 const std::vector<tgBasicActuator*> muscles = subject.
getAllMuscles();
104 for (
size_t i = 0; i < muscles.size(); ++i)
107 assert(pMuscle != NULL);
122 vector< vector< double> > actions = receiveActionsFromEvolution();
125 actions = transformActions(actions);
128 applyActions(subject,actions,state);
133 vector< vector <double> > SuperBallPrefLengthController::transformActions(vector< vector <double> > actions)
137 double range=max-min;
139 for(
int i=0;i<actions.size();i++)
141 for(
int j=0;j<actions[i].size();j++)
143 scaledAct=actions[i][j]*(range)+min;
144 actions[i][j]=scaledAct;
151 typedef std::pair<int,double> mypair;
152 bool comparator (
const mypair& l,
const mypair& r)
153 {
return l.second < r.second; }
157 void SuperBallPrefLengthController::applyActions(
SuperBallModel& subject, vector< vector <double> > actions, vector<double> nodeRayDistances)
162 vector< mypair > nodeHeightWithIndex;
163 for(
int i=0;i<nodePositions.size();i++)
166 nodeHeightWithIndex.push_back(make_pair(i,nodeRayDistances[i]));
168 std::sort(nodeHeightWithIndex.begin(),nodeHeightWithIndex.end(),comparator);
170 vector<int> groundNodes;
171 groundNodes.push_back(nodeHeightWithIndex[0].first);
172 groundNodes.push_back(nodeHeightWithIndex[1].first);
173 groundNodes.push_back(nodeHeightWithIndex[2].first);
175 int a=groundNodes[0];
176 int b=groundNodes[1];
177 int c=groundNodes[2];
179 int numberOfMusclesOnBase=0;
180 if(subject.getMusclesPerNodes()[a][b]!=NULL)
181 numberOfMusclesOnBase++;
182 if(subject.getMusclesPerNodes()[a][c]!=NULL)
183 numberOfMusclesOnBase++;
184 if(subject.getMusclesPerNodes()[b][c]!=NULL)
185 numberOfMusclesOnBase++;
188 if(numberOfMusclesOnBase==3)
192 double maxDistance=-10000;
193 double minDistance=10000;
199 double dist=goalPoint->getCenterOfMassPosition().distance(nodePositions[groundNodes[i]]);
211 int groundNodesInOrder[3]={-1,-1,-1};
215 groundNodesInOrder[2]=groundNodes[furthestNode];
216 else if(subject.getMusclesPerNodes()[groundNodes[i]][subject.getOtherEndOfTheRod(groundNodes[furthestNode])] != NULL)
217 groundNodesInOrder[1]=groundNodes[i];
219 groundNodesInOrder[0]=groundNodes[i];
223 subject.fillNodeMappingFromBasePoints(groundNodesInOrder[0],groundNodesInOrder[1],groundNodesInOrder[2]);
226 for(
int i=0;i<13;i++)
228 for(
int j=i+1;j<13;j++)
230 if(subject.muscleConnections[i][j]>=0)
232 int nodeStartOriginal=i;
233 int nodeEndOriginal=j;
235 int nodeStartNew=subject.nodeMappingReverse[nodeStartOriginal];
236 int nodeEndNew=subject.nodeMappingReverse[nodeEndOriginal];
237 tgBasicActuator * correspondingMuscle = subject.musclesPerNodes[nodeStartNew][nodeEndNew];
238 if(correspondingMuscle==NULL)
240 cout<<
"NO MUSCLE EXISTS ACCORDING TO THE NEW MAPPING"<<endl;
243 if(actionNo>=actions.size())
245 cout<<
"Warning: actions < number of active muscles, no input given"<<endl;
253 else if(numberOfMusclesOnBase==2)
258 double maxDistance=-50000;
259 double minDistance=50000;
265 double dist=goalPoint->getCenterOfMassPosition().distance(nodePositions[groundNodes[i]]);
268 furthestNode=groundNodes[i];
273 closestNode=groundNodes[i];
280 if(groundNodes[i]!=closestNode && groundNodes[i]!=furthestNode)
281 middleNode=groundNodes[i];
287 if(subject.musclesPerNodes[closestNode][middleNode]!=NULL)
289 flippingNode=middleNode;
293 flippingNode=furthestNode;
296 for(
int i=0;i<12;i++)
298 if(subject.musclesPerNodes[closestNode][i]!=NULL && subject.musclesPerNodes[flippingNode][i]!=NULL)
305 int triangle[3]={closestNode,flippingNode,landingNode};
306 int triangleInOrder[3]={-1,-1,-1};
307 triangleInOrder[2]=landingNode;
308 if(subject.musclesPerNodes[closestNode][subject.getOtherEndOfTheRod(landingNode)] != NULL)
310 triangleInOrder[1]=closestNode;
311 triangleInOrder[0]=flippingNode;
315 triangleInOrder[1]=flippingNode;
316 triangleInOrder[0]=closestNode;
318 subject.fillNodeMappingFromBasePoints(triangleInOrder[0],triangleInOrder[1],triangleInOrder[2]);
321 for(
int i=0;i<13;i++)
323 for(
int j=i+1;j<13;j++)
325 if(subject.muscleConnections[i][j]>=0)
327 int nodeStartOriginal=i;
328 int nodeEndOriginal=j;
330 int nodeStartNew=subject.nodeMappingReverse[nodeStartOriginal];
331 int nodeEndNew=subject.nodeMappingReverse[nodeEndOriginal];
332 tgBasicActuator * correspondingMuscle = subject.musclesPerNodes[nodeStartNew][nodeEndNew];
333 if(correspondingMuscle==NULL)
335 cout<<
"NO MUSCLE EXISTS ACCORDING TO THE NEW MAPPING"<<endl;
338 if(actionNo>=actions.size())
340 cout<<
"Warning: actions < number of active muscles, no input given"<<endl;
352 std::cout<<
"TEARDOWN CALLED"<<std::endl;
353 vector<double> scores;
354 scores.push_back(calculateDistanceMoved());
355 evolution->updateScores(scores);
356 std::cout<<
"Distance moved: "<<scores[0]<<std::endl;
358 initialPosition.clear();
361 vector<vector<double> > SuperBallPrefLengthController::receiveActionsFromEvolution()
363 vector<vector <double> > actions;
365 vector<AnnealEvoMember *> members = evolution->nextSetOfControllers();
366 for(
int i=0;i<members.size();i++)
368 actions.push_back(members[i]->statelessParameters);
375 double SuperBallPrefLengthController::calculateDistanceMoved()
377 vector<double> scores;
378 double x= m_subject->getCenter()[0] - goalPoint->getCenterOfMassPosition().getX();
379 double z= m_subject->getCenter()[2] - goalPoint->getCenterOfMassPosition().getZ();
380 double distanceNew=sqrt(x*x+z*z);
381 double xx=initialPosition[0]-goalPoint->getCenterOfMassPosition().getX();
382 double zz=initialPosition[1]-goalPoint->getCenterOfMassPosition().getZ();
383 double distanceOld=sqrt(xx*xx+zz*zz);
384 double distanceMoved=distanceOld-distanceNew;
389 return distanceMoved;
virtual void onTeardown(SuperBallModel &subject)
std::vector< double > getSensorInfo()
virtual void moveMotors(double dt)
std::vector< btVector3 > getSensorOrientations()
virtual void setControlInput(double input)
Contains the definition of class SuperBallModel. $Id$.
const std::vector< tgBasicActuator * > & getAllMuscles() const
SuperBallPrefLengthController(const double prefLength=5)
Contains the definition of class tgBasicActuator.
virtual void onSetup(SuperBallModel &subject)
std::vector< btVector3 > getSensorPositions()
virtual void onStep(SuperBallModel &subject, double dt)