00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BB2004Calibrator.h"
00010 #include "Tools/Actorics/Kinematics.h"
00011 #include "Representations/Perception/BodyPosture.h"
00012
00013 void CalibrationIndividual::init(const BBIndividual&)
00014 {
00015
00016 memcpy(&genes[0], (const void*) &getRobotConfiguration().getRobotCalibration(), sizeof(RobotCalibration));
00017 fitness = 1;
00018 mutate();
00019 }
00020
00021 void CalibrationIndividual::interpolate(const BBIndividual& other)
00022 {
00023 const CalibrationIndividual& o = (const CalibrationIndividual&) other;
00024 for(int i = 0; i < numOfGenes; ++i)
00025 {
00026 double f = random(),
00027 f1 = f * fitness,
00028 f2 = (1 - f) * o.fitness;
00029 f = f1 / (f1 + f2);
00030 genes[i] = genes[i] * f + o.genes[i] * (1 - f);
00031 }
00032 }
00033
00034 void CalibrationIndividual::mutate()
00035 {
00036 for(int i = 0; i < numOfGenes; ++i)
00037 genes[i] += 0.1 * (-0.5 + random());
00038 }
00039
00040 void CalibrationIndividual::extrapolate(const BBIndividual& other)
00041 {
00042 const CalibrationIndividual& o = (const CalibrationIndividual&) other;
00043 for(int i = 0; i < numOfGenes; ++i)
00044 {
00045 double f = random(),
00046 f1 = f * fitness,
00047 f2 = (1 - f) * o.fitness;
00048 f = f1 / (f1 + f2) * 2;
00049 if(f < 1)
00050 genes[i] = o.genes[i] + (o.genes[i] - genes[i]) * (1 - f);
00051 else
00052 genes[i] += (genes[i] - o.genes[i]) * (f - 1);
00053 }
00054 }
00055
00056 void CalibrationIndividual::select() const
00057 {
00058 RobotCalibration& r = const_cast<RobotCalibration&>((const RobotCalibration&) genes[0]);
00059
00060
00061
00062
00063 r.tiltFactor = r.panFactor = r.tilt2Factor = 1;
00064 getRobotConfiguration().setRobotCalibration(r);
00065
00066 }
00067
00068 void CalibrationIndividual::dump() const
00069 {
00070 for(int i = 0; i < numOfGenes; ++i)
00071 getDebugOut().text << " " << genes[i];
00072 getDebugOut().finishMessage(idText);
00073 }
00074
00075 BB2004Calibrator::BB2004Calibrator(const SensorBehaviorControlInterfaces& interfaces)
00076 : SensorBehaviorControl(interfaces),
00077 population(numOfIndividuals, CalibrationIndividual()),
00078 imageProcessor(ImageProcessorInterfaces(
00079 imageBuffer,
00080 cameraMatrix,
00081 const_cast<ColorTable&>(colorTable),
00082 robotPose,
00083 ballModel,
00084 playerPoseCollection,
00085 robotState,
00086 calibrationRequest,
00087 landmarksPercept,
00088 ballPercept,
00089 linesPercept,
00090 edgesPercept,
00091 playersPercept,
00092 obstaclesPercept,
00093 specialPercept)),
00094 evolutions(-1)
00095 {
00096 for(int i = 0; i < numOfIndividuals; ++i)
00097 count[i] = 0;
00098 }
00099
00100 void BB2004Calibrator::execute()
00101 {
00102 int i;
00103 for(i = 0; i < sensorDataBuffer.numOfFrames; ++i)
00104 buffer.add(sensorDataBuffer.frame[i]);
00105
00106 for(i = 0; i < buffer.getNumberOfEntries(); ++i)
00107 if(buffer[i].frameNumber == imageBuffer.frameNumber)
00108 break;
00109
00110 if(evolutions < 0)
00111 {
00112 if(--evolutions ==
00113 #ifdef _WIN32
00114 -2
00115 #else
00116 -300
00117 #endif
00118 )
00119 {
00120 timeStamp = SystemCall::getCurrentSystemTime();
00121 evolutions = 0;
00122 }
00123 }
00124 else if(i < buffer.getNumberOfEntries() && image.frameNumber != imageBuffer.frameNumber)
00125 evolve(buffer[i]);
00126
00127 imageBuffer = image;
00128 motionRequest.motionType = MotionRequest::walk;
00129 motionRequest.walkRequest.walkType = WalkRequest::normal;
00130 motionRequest.walkRequest.walkParams = Pose2D();
00131 motionRequest.tailRequest.tailRequestID = TailRequest::noTailWag;
00132 headControlMode.headControlMode = HeadControlMode::calibrate;
00133 }
00134
00135 void BB2004Calibrator::evolve(const SensorData& sensorData)
00136 {
00137 int i;
00138 for(i = 0; i < numOfIndividuals; ++i)
00139 {
00140 population[i].select();
00141 RobotVertices rob;
00142 Kinematics::calcNeckAndLegPositions(sensorData, rob);
00143 BodyPosture bodyPosture;
00144 bodyPosture.bodyTiltCalculatedFromLegSensors = rob.bodyTilt;
00145 bodyPosture.bodyRollCalculatedFromLegSensors = rob.bodyRoll;
00146 bodyPosture.neckHeightCalculatedFromLegSensors = rob.neckHeight;
00147
00148 double tilt = fromMicroRad(sensorData.data[SensorData::neckTilt]);
00149 double pan = fromMicroRad(sensorData.data[SensorData::headPan]);
00150 double roll = fromMicroRad(sensorData.data[SensorData::headTilt]);
00151 Kinematics::calculateCameraMatrix(tilt, pan, roll, bodyPosture.bodyTiltCalculatedFromLegSensors, bodyPosture.neckHeightCalculatedFromLegSensors, cameraMatrix);
00152 imageProcessor.execute();
00153 if(linesPercept.numberOfPoints[LinesPercept::border])
00154 {
00155 for(int j = 0; j < linesPercept.numberOfPoints[LinesPercept::border]; ++j)
00156 {
00157 Vector2<double> v(linesPercept.points[LinesPercept::border][j].x, linesPercept.points[LinesPercept::border][j].y);
00158 Vector2<double> vMin = observationTable[LinesPercept::border].getClosestPoint(v);
00159 if(vMin.x != 1000000)
00160 fitness[i] += v - vMin;
00161 else
00162 fitness[i] += Vector2<double>(10000,10000);
00163 ++count[i];
00164 }
00165 }
00166 }
00167
00168
00169 if(SystemCall::getTimeSince(timeStamp) > waitPerGeneration)
00170 {
00171 for(i = 0; i < numOfIndividuals; ++i)
00172 {
00173 population[i].setFitness(exp(-0.1 * (count[i] ? fitness[i].abs() / count[i] : 1)));
00174 fitness[i] = Vector2<double>();
00175 count[i] = 0;
00176 }
00177 const CalibrationIndividual& best = (const CalibrationIndividual&) population.getFittest();
00178 ++evolutions;
00179 OUTPUT(idText, text, "Generation " << evolutions << ", fitness " << best.getFitness() << ", genes:");
00180 best.dump();
00181
00182
00183 if(evolutions & 1)
00184 {
00185 population.interpolate();
00186 getDebugOut().text << "Interpolating:";
00187 }
00188 else
00189 {
00190 population.extrapolate();
00191 getDebugOut().text << "Extrapolating:";
00192 }
00193
00194
00195 Statistics& s = population.getStatistics();
00196 int num = s.analyze();
00197 for(i = 0; i < num; ++i)
00198 getDebugOut().text << " " << s[i];
00199 getDebugOut().finishMessage(idText);
00200 timeStamp = SystemCall::getCurrentSystemTime();
00201 }
00202 }
00203
00204 bool BB2004Calibrator::handleMessage(InMessage& message)
00205 {
00206 return false;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261