00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "BeatReferee.h"
00020
00021
00022 using namespace std;
00023 using namespace Marsyas;
00024
00025 #define NONE 0.0
00026 #define BEAT 1.0
00027 #define EVAL 2.0
00028
00029 #define INNER 3.0
00030 #define OUTTER 4.0
00031
00032 #define MINIMUMREAL 0.000001 //(0.000001 minimum float recognized)
00033 #define NA -10000.0 //undefined value flag (just a big negative nr)
00034
00035 BeatReferee::BeatReferee(mrs_string name):MarSystem("BeatReferee", name)
00036 {
00037 addControls();
00038
00039 bestScore_ = NA;
00040 bestAgentIndex_ = -1;
00041 timeElapsed_ = 0;
00042 outputCount_ = 0;
00043 triggerCount_ = 0;
00044 lastBeatTime_ = (mrs_natural) NA;
00045 lastBeatPeriod_ = -1;
00046 bestFinalAgent_ = -1;
00047 processInduction_ = false;
00048 triggerInduction_ = false;
00049 startSystem_ = true;
00050 startTracking_ = false;
00051 lostGTBeatsCount_ = 0;
00052 lastGTBeatPos_ = -1;
00053 backtraceEndTime_ = -1;
00054 bestAgentBeforeTrigger_ = -1;
00055 lastGTFalsePos_ = false;
00056 }
00057
00058 BeatReferee::BeatReferee(const BeatReferee& a) : MarSystem(a)
00059 {
00060
00061
00062
00063 ctrl_mutedAgents_ = getctrl("mrs_realvec/mutedAgents");
00064 ctrl_inductionEnabler_ = getctrl("mrs_realvec/inductionEnabler");
00065 ctrl_firstHypotheses_ = getctrl("mrs_realvec/beatHypotheses");
00066 ctrl_inductionTime_ = getctrl("mrs_natural/inductionTime");
00067 ctrl_hopSize_ = getctrl("mrs_natural/hopSize");
00068 ctrl_srcFs_ = getctrl("mrs_real/srcFs");
00069 ctrl_maxPeriod_ = getctrl("mrs_natural/maxPeriod");
00070 ctrl_minPeriod_ = getctrl("mrs_natural/minPeriod");
00071 ctrl_agentControl_ = getctrl("mrs_realvec/agentControl");
00072 ctrl_beatDetected_ = getctrl("mrs_real/beatDetected");
00073 ctrl_tickCount_ = getctrl("mrs_natural/tickCount");
00074 ctrl_obsoleteFactor_ = getctrl("mrs_real/obsoleteFactor");
00075 ctrl_lostFactor_ = getctrl("mrs_natural/lostFactor");
00076 ctrl_childrenScoreFactor_ = getctrl("mrs_real/childrenScoreFactor");
00077 ctrl_bestFactor_ = getctrl("mrs_real/bestFactor");
00078 ctrl_eqPhase_ = getctrl("mrs_natural/eqPhase");
00079 ctrl_eqPeriod_ = getctrl("mrs_natural/eqPeriod");
00080 ctrl_corFactor_ = getctrl("mrs_real/corFactor");
00081 ctrl_child1Factor_ = getctrl("mrs_real/child1Factor");
00082 ctrl_child2Factor_ = getctrl("mrs_real/child2Factor");
00083 ctrl_child3Factor_ = getctrl("mrs_real/child3Factor");
00084 ctrl_backtrace_ = getctrl("mrs_bool/backtrace");
00085 ctrl_logFile_ = getctrl("mrs_string/logFile");
00086 ctrl_logFileName_= getctrl("mrs_string/logFileName");
00087 ctrl_soundFileSize_= getctrl("mrs_natural/soundFileSize");
00088 ctrl_bestFinalAgentHistory_= getctrl("mrs_realvec/bestFinalAgentHistory");
00089 ctrl_nonCausal_ = getctrl("mrs_bool/nonCausal");
00090 ctrl_triggerInduction_ = getctrl("mrs_bool/triggerInduction");
00091 ctrl_triggerInductionExternalRequest_ = getctrl("mrs_bool/triggerInductionExternalRequest");
00092 ctrl_gtInductionMode_ = getctrl("mrs_string/gtInductionMode");
00093 ctrl_triggerGtTolerance_ = getctrl("mrs_natural/triggerGtTolerance");
00094 ctrl_gtBeatsFile_ = getctrl("mrs_string/gtBeatsFile");
00095 ctrl_curBestScore_ = getctrl("mrs_real/curBestScore");
00096 ctrl_adjustment_ = getctrl("mrs_natural/adjustment");
00097 ctrl_inductionMode_ = getctrl("mrs_string/inductionMode");
00098 ctrl_beatTransitionTol_ = getctrl("mrs_real/beatTransitionTol");
00099 ctrl_destFileName_ = getctrl("mrs_string/destFileName");
00100 ctrl_triggerTimesFile_ = getctrl("mrs_string/triggerTimesFile");
00101 ctrl_resetAfterNewInduction_ = getctrl("mrs_bool/resetAfterNewInduction");
00102 ctrl_resetFeatWindow_ = getctrl("mrs_bool/resetFeatWindow");
00103 ctrl_supervisedTriggerThres_ = getctrl("mrs_real/supervisedTriggerThres");
00104
00105 beatTransitionTol_ = a.beatTransitionTol_;
00106 considerAgentTransitionBeat_ = a.considerAgentTransitionBeat_;
00107 considerFatherTransitionBeat_ = a.considerFatherTransitionBeat_;
00108 timeElapsed_ = a.timeElapsed_;
00109 lastBeatPeriod_ = a.lastBeatPeriod_;
00110 historyCount_ = a.historyCount_;
00111 historyBeatTimes_ = a.historyBeatTimes_;
00112 lastBeatTime_ = a.lastBeatTime_;
00113 bestScore_ = a.bestScore_;
00114 bestAgentIndex_ = a.bestAgentIndex_;
00115 outputCount_ = a.outputCount_;
00116 initPeriod_ = a.initPeriod_;
00117 corFactor_ = a.corFactor_;
00118 backtrace_ = a.backtrace_;
00119 logFile_ = a.logFile_;
00120 logFileName_ = a.logFileName_;
00121 logFileUnits_ = a.logFileUnits_;
00122 triggerInduction_ = a.triggerInduction_;
00123 triggerInductionExternalRequest_ = a.triggerInductionExternalRequest_;
00124 processInduction_ = a.processInduction_;
00125 triggerInductionTime_ = a.triggerInductionTime_;
00126 inductionMode_ = a.inductionMode_;
00127 startSystem_ = a.startSystem_;
00128 startTracking_ = a.startTracking_;
00129 agentsHistory_ = a.agentsHistory_;
00130 agentsFamilyHist_ = a.agentsFamilyHist_;
00131 lostGTBeatsCount_ = a.lostGTBeatsCount_;
00132 lastGTBeatPos_ = a.lastGTBeatPos_;
00133 lastGTFileBeat_ = a.lastGTFileBeat_;
00134 triggerGtTolerance_ = a.triggerGtTolerance_;
00135 backtraceEndTime_ = a.backtraceEndTime_;
00136 bestAgentBeforeTrigger_ = a.bestAgentBeforeTrigger_;
00137 frames2SecsAdjustment_ = a.frames2SecsAdjustment_;
00138 lastGTFalsePos_ = a.lastGTFalsePos_;
00139 triggerTimes_ = a.triggerTimes_;
00140 transitionTimes_ = a.transitionTimes_;
00141 transitionsConsidered_ = a.transitionsConsidered_;
00142 resetAfterNewInduction_ = a.resetAfterNewInduction_;
00143 supervisedTriggerThres_ = a.supervisedTriggerThres_;
00144 }
00145
00146 BeatReferee::~BeatReferee()
00147 {
00148 }
00149
00150 MarSystem*
00151 BeatReferee::clone() const
00152 {
00153 return new BeatReferee(*this);
00154 }
00155
00156 void
00157 BeatReferee::addControls()
00158 {
00159
00160 addctrl("mrs_realvec/mutedAgents", realvec(), ctrl_mutedAgents_);
00161 addctrl("mrs_realvec/inductionEnabler", realvec(), ctrl_inductionEnabler_);
00162 addctrl("mrs_realvec/beatHypotheses", realvec(), ctrl_firstHypotheses_);
00163 addctrl("mrs_natural/inductionTime", -1, ctrl_inductionTime_);
00164 addctrl("mrs_natural/hopSize", -1, ctrl_hopSize_);
00165 setctrlState("mrs_natural/hopSize", true);
00166 addctrl("mrs_real/srcFs", -1.0, ctrl_srcFs_);
00167 setctrlState("mrs_real/srcFs", true);
00168 addctrl("mrs_natural/maxPeriod", -1, ctrl_maxPeriod_);
00169 setctrlState("mrs_natural/maxPeriod", true);
00170 addctrl("mrs_natural/minPeriod", -1, ctrl_minPeriod_);
00171 setctrlState("mrs_natural/minPeriod", true);
00172 addctrl("mrs_realvec/agentControl", realvec(), ctrl_agentControl_);
00173 addctrl("mrs_real/beatDetected", 0.0, ctrl_beatDetected_);
00174 addctrl("mrs_natural/tickCount", 0, ctrl_tickCount_);
00175 addctrl("mrs_real/obsoleteFactor", 0.8, ctrl_obsoleteFactor_);
00176 setctrlState("mrs_real/obsoleteFactor", true);
00177 addctrl("mrs_natural/lostFactor", 4, ctrl_lostFactor_);
00178 setctrlState("mrs_natural/lostFactor", true);
00179 addctrl("mrs_real/childrenScoreFactor", 0.8, ctrl_childrenScoreFactor_);
00180 setctrlState("mrs_real/childrenScoreFactor", true);
00181 addctrl("mrs_real/bestFactor", 1.1, ctrl_bestFactor_);
00182 setctrlState("mrs_real/bestFactor", true);
00183 addctrl("mrs_natural/eqPhase", 2, ctrl_eqPhase_);
00184 setctrlState("mrs_natural/eqPhase", true);
00185 addctrl("mrs_natural/eqPeriod", 1, ctrl_eqPeriod_);
00186 setctrlState("mrs_natural/eqPeriod", true);
00187 addctrl("mrs_real/corFactor", 0.5, ctrl_corFactor_);
00188 setctrlState("mrs_real/corFactor", true);
00189 addctrl("mrs_real/child1Factor", 2.0, ctrl_child1Factor_);
00190 setctrlState("mrs_real/child1Factor", true);
00191 addctrl("mrs_real/child2Factor", 0.5, ctrl_child2Factor_);
00192 setctrlState("mrs_real/child2Factor", true);
00193 addctrl("mrs_real/child3Factor", 1.0, ctrl_child3Factor_);
00194 setctrlState("mrs_real/child3Factor", true);
00195 addctrl("mrs_bool/backtrace", false, ctrl_backtrace_);
00196 setctrlState("mrs_bool/backtrace", true);
00197 addctrl("mrs_string/logFile", "-1", ctrl_logFile_);
00198 addctrl("mrs_string/logFileName", "log.txt", ctrl_logFileName_);
00199 addctrl("mrs_natural/soundFileSize", 0, ctrl_soundFileSize_);
00200 setctrlState("mrs_natural/soundFileSize", true);
00201 addctrl("mrs_realvec/bestFinalAgentHistory", realvec(), ctrl_bestFinalAgentHistory_);
00202 addctrl("mrs_bool/nonCausal", false, ctrl_nonCausal_);
00203 setctrlState("mrs_bool/nonCausal", true);
00204 addctrl("mrs_bool/triggerInduction", false, ctrl_triggerInduction_);
00205 setctrlState("mrs_bool/triggerInduction", true);
00206 addctrl("mrs_bool/triggerInductionExternalRequest", false, ctrl_triggerInductionExternalRequest_);
00207 setctrlState("mrs_bool/triggerInductionExternalRequest", true);
00208 addctrl("mrs_string/gtInductionMode", "-1", ctrl_gtInductionMode_);
00209 setctrlState("mrs_string/gtInductionMode", true);
00210 addctrl("mrs_natural/triggerGtTolerance", 5, ctrl_triggerGtTolerance_);
00211 setctrlState("mrs_natural/triggerGtTolerance", true);
00212 addctrl("mrs_string/gtBeatsFile", "input.txt", ctrl_gtBeatsFile_);
00213 addctrl("mrs_real/curBestScore", NA, ctrl_curBestScore_);
00214 setctrlState("mrs_real/curBestScore", true);
00215 addctrl("mrs_natural/adjustment", 0, ctrl_adjustment_);
00216 setctrlState("mrs_natural/adjustment", true);
00217 addctrl("mrs_string/inductionMode", "single", ctrl_inductionMode_);
00218 addctrl("mrs_real/beatTransitionTol", 0.6, ctrl_beatTransitionTol_);
00219 addctrl("mrs_string/destFileName", "output", ctrl_destFileName_);
00220 addctrl("mrs_string/triggerTimesFile", "input_trigger.txt", ctrl_triggerTimesFile_);
00221 addctrl("mrs_bool/resetAfterNewInduction", true, ctrl_resetAfterNewInduction_);
00222 setctrlState("mrs_bool/resetAfterNewInduction", true);
00223 addctrl("mrs_bool/resetFeatWindow", true, ctrl_resetFeatWindow_);
00224 setctrlState("mrs_bool/resetFeatWindow", true);
00225 addctrl("mrs_real/supervisedTriggerThres", 0.0, ctrl_supervisedTriggerThres_);
00226 }
00227
00228 void
00229 BeatReferee::myUpdate(MarControlPtr sender)
00230 {
00231 MRSDIAG("BeatReferee.cpp - BeatReferee:myUpdate");
00232
00233 ctrl_onSamples_->setValue(1, NOUPDATE);
00234 ctrl_onObservations_->setValue(1, NOUPDATE);
00235 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00236
00237 lostFactor_ = ctrl_lostFactor_->to<mrs_natural>();
00238 obsoleteFactor_ = ctrl_obsoleteFactor_->to<mrs_real>();
00239 childrenScoreFactor_ = ctrl_childrenScoreFactor_->to<mrs_real>();
00240 bestFactor_ = ctrl_bestFactor_->to<mrs_real>();
00241 eqPhase_ = ctrl_eqPhase_->to<mrs_natural>();
00242 eqPeriod_ = ctrl_eqPeriod_->to<mrs_natural>();
00243 corFactor_ = ctrl_corFactor_->to<mrs_real>();
00244 child1Factor_ = ctrl_child1Factor_->to<mrs_real>();
00245 child2Factor_ = ctrl_child2Factor_->to<mrs_real>();
00246 child3Factor_ = ctrl_child3Factor_->to<mrs_real>();
00247 backtrace_ = ctrl_backtrace_->to<mrs_bool>();
00248 hopSize_ = ctrl_hopSize_->to<mrs_natural>();
00249 srcFs_ = ctrl_srcFs_->to<mrs_real>();
00250 maxPeriod_ = ctrl_maxPeriod_->to<mrs_natural>();
00251 minPeriod_ = ctrl_minPeriod_->to<mrs_natural>();
00252 nonCausal_ = ctrl_nonCausal_->to<mrs_bool>();
00253 inductionMode_ = ctrl_inductionMode_->to<mrs_string>();
00254 gtBeatsFile_ = ctrl_gtBeatsFile_->to<mrs_string>();
00255 triggerGtTolerance_ = ctrl_triggerGtTolerance_->to<mrs_natural>();
00256 frames2SecsAdjustment_ = ctrl_adjustment_->to<mrs_natural>();
00257 beatTransitionTol_ = ctrl_beatTransitionTol_->to<mrs_real>();
00258 triggerTimesFile_ = ctrl_triggerTimesFile_->to<mrs_string>();
00259 resetAfterNewInduction_ = ctrl_resetAfterNewInduction_->to<mrs_bool>();
00260 supervisedTriggerThres_ = ctrl_supervisedTriggerThres_->to<mrs_real>();
00261
00262
00263
00264
00265
00266
00267 nrAgents_ = inObservations_;
00268 historyCount_.create(nrAgents_);
00269 agentsJustCreated_.create(nrAgents_);
00270
00271 soundFileSize_ = ctrl_soundFileSize_->to<mrs_natural>();
00272
00273 maxNrBeats_ = (mrs_natural) (ceil(((mrs_real) soundFileSize_) / ((mrs_real) minPeriod_)) * 1.2);
00274
00275 inductionTime_ = ctrl_inductionTime_->to<mrs_natural>();
00276 inductionEnabler_ = ctrl_inductionEnabler_->to<mrs_realvec>();
00277
00278
00279 mrs_real timeBeforeKill = 0.0;
00280 timeBeforeKilling_ = (mrs_natural)(timeBeforeKill*srcFs_/hopSize_);
00281 }
00282
00283 mrs_bool
00284 BeatReferee::loadTriggerTimes(mrs_string triggerTimesFile)
00285 {
00286 if (fopen(triggerTimesFile.c_str(), "r"))
00287 {
00288 cerr << "TriggerTimes File: " << triggerTimesFile.c_str() << endl;
00289
00290 ifstream inStream;
00291 mrs_string line;
00292 inStream.open(triggerTimesFile.c_str());
00293 getline (inStream, line);
00294
00295 mrs_natural countTriggers = 0;
00296 while(strtod(line.c_str(), NULL) > 0.0)
00297 {
00298 getline (inStream, line);
00299 countTriggers++;
00300 }
00301 triggerTimes_.create(countTriggers);
00302 transitionTimes_.create(countTriggers);
00303 transitionsConsidered_.create(countTriggers);
00304 inStream.close();
00305 inStream.open(triggerTimesFile.c_str());
00306 mrs_natural validTriggersCount = 0;
00307 mrs_real indTimeSecs = ((inductionTime_ * hopSize_) - (frames2SecsAdjustment_)) / srcFs_;
00308 for(mrs_natural i = 0; i < countTriggers; i++)
00309 {
00310 getline (inStream, line);
00311 mrs_real triggerTime = strtod(line.c_str(), NULL);
00312
00313 if(triggerTime >= indTimeSecs)
00314 {
00315
00316
00317 transitionTimes_(validTriggersCount) = ((mrs_natural) (triggerTime * (srcFs_/hopSize_) + 0.5));
00318 triggerTimes_(validTriggersCount) = transitionTimes_(validTriggersCount) + inductionTime_;
00319 transitionsConsidered_(validTriggersCount) = 0.0;
00320 validTriggersCount++;
00321 }
00322 }
00323
00324
00325
00326
00327
00328
00329
00330
00331 inStream.close();
00332
00333 return true;
00334 }
00335 else
00336 {
00337 cerr << "Bad or nonexistent transition times file: " << triggerTimesFile.c_str() << "\nPlease specify a supported one." << endl;
00338 return false;
00339 }
00340 }
00341
00342 mrs_bool
00343 BeatReferee::isGTFileInLine(mrs_string line)
00344 {
00345
00346
00347 mrs_natural pos0;
00348 pos0 = (mrs_natural) line.find_first_of(" ", 0);
00349
00350
00351 return (pos0 > 0 ? true : false);
00352 }
00353
00354
00355
00356
00357
00358
00359 mrs_natural
00360 BeatReferee::checkBeatInGTFile()
00361 {
00362 mrs_natural localBeatErrorCount = 0;
00363 mrs_real fMeasureTol = 0.07;
00364 mrs_real beatTime = ((timeElapsed_ * hopSize_) - (frames2SecsAdjustment_)) / srcFs_;
00365
00366
00367 ifstream inStream;
00368 mrs_string line;
00369 inStream.open(gtBeatsFile_.c_str());
00370 getline (inStream, line);
00371
00372
00373 if(isGTFileInLine(line))
00374 {
00375 mrs_bool beatPosFound = false;
00376 mrs_natural curGTBeatPos, firstGTBeatPos;
00377 mrs_real lastGTBeatTime = 0, curGTBeatTime;
00378 mrs_real lastDiffBeats;
00379 mrs_real diffBeats = 0;
00380 mrs_real tolWinLft, tolWinRgt;
00381
00382
00383 if(lastGTBeatPos_ < 0)
00384 {
00385 std::istringstream iss(line);
00386 char c[10];
00387 while (iss >> c)
00388 {
00389
00390 lastGTFileBeat_ = atof(c);
00391 }
00392 iss.clear();
00393 }
00394
00395
00396 mrs_real beatTimeCheck = (((timeElapsed_+2) * hopSize_) - (frames2SecsAdjustment_)) / srcFs_;
00397 if(beatTimeCheck >= lastGTFileBeat_)
00398 return 0;
00399
00400
00401
00402
00403 if((backtraceEndTime_ == -1 && !backtrace_) || timeElapsed_ > backtraceEndTime_)
00404 {
00405 mrs_real indTimeSecs = ((inductionTime_ * hopSize_) - (frames2SecsAdjustment_)) / srcFs_;
00406 do
00407 {
00408
00409 curGTBeatPos = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_);
00410 lastGTBeatTime = strtod(line.substr(lastGTBeatPos_+1, curGTBeatPos).c_str(), NULL);
00411
00412 if(lastGTBeatTime >= indTimeSecs) break;
00413
00414 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00415
00416 }while(lastGTBeatTime < indTimeSecs);
00417 }
00418 else
00419 {
00420
00421
00422 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_);
00423 curGTBeatPos = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00424 lastGTBeatTime = strtod(line.substr(lastGTBeatPos_+1, curGTBeatPos).c_str(), NULL);
00425 }
00426
00427 firstGTBeatPos = lastGTBeatPos_;
00428
00429
00430
00431
00432 tolWinLft = lastGTBeatTime - fMeasureTol;
00433 tolWinRgt = lastGTBeatTime + fMeasureTol;
00434 if(beatTime >= tolWinLft && beatTime <= tolWinRgt)
00435 {
00436 localBeatErrorCount = 0;
00437 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00438
00439 }
00440 else
00441 {
00442
00443
00444 if(beatTime > tolWinRgt && lastGTFalsePos_)
00445 {
00446
00447 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00448 curGTBeatPos = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00449 lastGTBeatTime = strtod(line.substr(lastGTBeatPos_+1, curGTBeatPos).c_str(), NULL);
00450 tolWinLft = lastGTBeatTime - fMeasureTol;
00451
00452 tolWinRgt = lastGTBeatTime + fMeasureTol;
00453
00454
00455
00456 firstGTBeatPos = lastGTBeatPos_;
00457
00458 lastGTFalsePos_ = false;
00459 }
00460
00461 lastDiffBeats = abs(beatTime - lastGTBeatTime);
00462
00463
00464
00465 if(beatTime > tolWinRgt)
00466 {
00467
00468 mrs_natural beatErrorCount = 0;
00469
00470
00471
00472 do{
00473 beatErrorCount++;
00474
00475
00476
00477 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00478 curGTBeatPos = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00479 lastGTBeatTime = strtod(line.substr(lastGTBeatPos_+1, curGTBeatPos).c_str(), NULL);
00480 tolWinLft = lastGTBeatTime - fMeasureTol;
00481 tolWinRgt = lastGTBeatTime + fMeasureTol;
00482
00483
00484
00485
00486
00487
00488 if(beatTime >= tolWinLft && beatTime <= tolWinRgt)
00489 {
00490
00491 if((beatErrorCount + lostGTBeatsCount_) < triggerGtTolerance_)
00492 beatErrorCount = 0;
00493
00494
00495 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00496
00497 break;
00498 }
00499 else
00500 if(beatTime <= tolWinRgt) break;
00501
00502 }while(beatTime > tolWinRgt);
00503
00504 localBeatErrorCount = beatErrorCount;
00505
00506
00507
00508
00509 }
00510
00511 else
00512 {
00513
00514 do
00515 {
00516 lastGTBeatPos_ = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00517 curGTBeatPos = (mrs_natural) line.find_first_of(" ", lastGTBeatPos_+1);
00518 curGTBeatTime = strtod(line.substr(lastGTBeatPos_+1, curGTBeatPos).c_str(), NULL);
00519
00520 diffBeats = abs(beatTime - curGTBeatTime);
00521
00522
00523
00524 if(diffBeats > lastDiffBeats)
00525 {
00526
00527 tolWinLft = lastGTBeatTime - fMeasureTol;
00528 tolWinRgt = lastGTBeatTime + fMeasureTol;
00529
00530
00531 if(beatTime >= tolWinLft && beatTime <= tolWinRgt)
00532 {
00533
00534 localBeatErrorCount = 0;
00535
00536 }
00537 else
00538 {
00539 localBeatErrorCount = 1;
00540
00541 lastGTBeatPos_ = firstGTBeatPos;
00542 lastGTFalsePos_ = true;
00543 }
00544
00545 beatPosFound = true;
00546 break;
00547 }
00548 lastDiffBeats = diffBeats;
00549 lastGTBeatTime = curGTBeatTime;
00550
00551 } while(beatPosFound == false);
00552 }
00553 }
00554 }
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600 return localBeatErrorCount;
00601 }
00602
00603 void
00604 BeatReferee::grantPoolSpaceForTriggerAgents(mrs_realvec triggerAgentsHypotheses)
00605 {
00606 mrs_real agentInitScore;
00607 for(int a = 0; a < triggerAgentsHypotheses.getRows(); a++)
00608 {
00609 agentInitScore = triggerAgentsHypotheses(a, 2);
00610 grantPoolSpace(-1, agentInitScore);
00611
00612
00613
00614
00615
00616 }
00617 }
00618
00619
00620
00621 mrs_realvec
00622 BeatReferee::clusterIBIs()
00623 {
00624 mrs_natural similarityTol = 2;
00625 mrs_real minPerDiff = MAXREAL;
00626 mrs_natural cluster = -1;
00627 mrs_realvec periodClusters(nrAgents_, nrAgents_);
00628 mrs_realvec periodClustersInfo(nrAgents_, 2);
00629 mrs_real agentPeriod;
00630 mrs_natural nrClusterMembers;
00631 mrs_real clusterPerMean;
00632
00633 for(int a = 0; a < nrAgents_; a++)
00634 {
00635
00636
00637 if(!mutedAgentsTmp_(a) && !agentsJustCreated_(a) && agentsFamilyHist_(a, ((mrs_natural) beatCounter_(a)-1)) == a)
00638 {
00639 agentPeriod = lastPeriods_(a);
00640
00641
00642
00643 for(int c = 0; c < nrAgents_; c++)
00644 {
00645 nrClusterMembers = (mrs_natural)periodClustersInfo(c, 0);
00646 clusterPerMean = periodClustersInfo(c, 1);
00647
00648
00649
00650 if(nrClusterMembers > 0)
00651 {
00652
00653 mrs_real perDiff = fabs(agentPeriod - clusterPerMean);
00654
00655
00656
00657 if(perDiff <= similarityTol)
00658 {
00659
00660 if(perDiff < minPerDiff)
00661 {
00662 minPerDiff = perDiff;
00663 cluster = c;
00664
00665
00666 }
00667 }
00668 }
00669 }
00670
00671 if(cluster >= 0)
00672 {
00673 mrs_natural selectedClusterNrMembers = (mrs_natural)periodClustersInfo(cluster, 0);
00674 mrs_real selectedClusterMean = 0.0;
00675
00676 periodClusters(cluster, selectedClusterNrMembers) = a;
00677
00678 selectedClusterNrMembers++;
00679
00680 for(int m = 0; m < selectedClusterNrMembers; m++)
00681 {
00682 selectedClusterMean += lastPeriods_((mrs_natural)periodClusters(cluster, m));
00683
00684
00685
00686 }
00687 selectedClusterMean = selectedClusterMean / selectedClusterNrMembers;
00688
00689
00690
00691
00692
00693
00694 periodClustersInfo(cluster, 0) = selectedClusterNrMembers;
00695 periodClustersInfo(cluster, 1) = selectedClusterMean;
00696
00697 minPerDiff = MAXREAL;
00698 cluster = -1;
00699 }
00700 else
00701 {
00702
00703
00704 for(int c = 0; c < nrAgents_; c++)
00705 {
00706 nrClusterMembers = (mrs_natural)periodClustersInfo(c, 0);
00707 if(nrClusterMembers == 0)
00708 {
00709 periodClusters(c, nrClusterMembers) = a;
00710 periodClustersInfo(c, 0) = 1;
00711 periodClustersInfo(c, 1) = agentPeriod;
00712
00713
00714
00715
00716 break;
00717 }
00718 }
00719 }
00720 }
00721 }
00722
00723
00724
00725
00726
00727
00728 mrs_natural nrClusterMembers1, nrClusterMembers2, nrClusterMembersTot;
00729 mrs_real clusterPerMean1, clusterPerMean2;
00730 mrs_real clusterPerMeanTot = 0.0;
00731 for(int c = 0; c < nrAgents_; c++)
00732 {
00733 nrClusterMembers1 = (mrs_natural)periodClustersInfo(c, 0);
00734
00735
00736
00737 if(nrClusterMembers1 > 0)
00738 {
00739 clusterPerMean1 = periodClustersInfo(c, 1);
00740
00741
00742
00743 for(int cc = 0; cc < nrAgents_; cc++)
00744 {
00745 nrClusterMembers2 = (mrs_natural)periodClustersInfo(cc, 0);
00746
00747
00748
00749 if(cc != c && nrClusterMembers2 > 0)
00750 {
00751 clusterPerMean2 = periodClustersInfo(cc, 1);
00752
00753
00754
00755
00756 if(fabs(clusterPerMean1 - clusterPerMean2) <= similarityTol)
00757 {
00758
00759 nrClusterMembersTot = nrClusterMembers1 + nrClusterMembers2;
00760 for(int i = 0; i < nrClusterMembers2; i++)
00761 {
00762 periodClusters(c, nrClusterMembers1+i) = periodClusters(cc, i);
00763 periodClusters(cc, i) = 0.0;
00764 }
00765
00766
00767 for(int m = 0; m < nrClusterMembersTot; m++)
00768 clusterPerMeanTot += lastPeriods_((mrs_natural)periodClusters(c, m));
00769
00770 clusterPerMeanTot = clusterPerMeanTot / nrClusterMembersTot;
00771
00772
00773 periodClustersInfo(c, 0) = nrClusterMembersTot;
00774 periodClustersInfo(c, 1) = clusterPerMeanTot;
00775
00776 periodClustersInfo(cc, 0) = 0.0;
00777 periodClustersInfo(cc, 1) = 0.0;
00778
00779
00780 }
00781 }
00782 }
00783 }
00784 }
00785
00786
00787
00788
00789
00790
00791
00792 mrs_realvec completedClustersPer(nrAgents_, nrAgents_+2);
00793
00794 for(int i = 0; i < nrAgents_; i++)
00795 {
00796 for(int j = 0; j < nrAgents_; j++)
00797 {
00798 completedClustersPer(i, j) = periodClusters(i, j);
00799
00800 }
00801
00802
00803 completedClustersPer(i, nrAgents_) = periodClustersInfo(i, 0);
00804 completedClustersPer(i, nrAgents_+1) = periodClustersInfo(i, 1);
00805
00806
00807
00808
00809 }
00810
00811
00812
00813
00814
00815 return completedClustersPer;
00816 }
00817
00818
00819 mrs_natural
00820 BeatReferee::getBestSimilarAgent3(mrs_natural newAgentPeriod, mrs_realvec completedClustersPer)
00821 {
00822
00823
00824
00825
00826
00827
00828
00829 mrs_real minPerDiff = MAXREAL;
00830 mrs_natural selectedCluster = -1;
00831 for(int c = 0; c < nrAgents_; c++)
00832 {
00833 if(completedClustersPer(c, nrAgents_) > 0.0)
00834 {
00835 mrs_real clusterPeriod = completedClustersPer(c, nrAgents_+1);
00836 mrs_real perDiff = fabs(newAgentPeriod - clusterPeriod);
00837
00838
00839
00840
00841 if(perDiff < minPerDiff)
00842 {
00843 minPerDiff = perDiff;
00844 selectedCluster = c;
00845
00846
00847 }
00848 }
00849 }
00850
00851
00852 mrs_real bestClusterScore = NA;
00853 mrs_natural bestClusterAgent = -1;
00854 mrs_natural clusterAgent;
00855 mrs_natural nrSelectedClusterMembers = (mrs_natural) completedClustersPer(selectedCluster, nrAgents_);
00856
00857
00858
00859
00860
00861
00862 for(int a = 0; a < nrSelectedClusterMembers; a++)
00863 {
00864 clusterAgent = (mrs_natural) completedClustersPer(selectedCluster, a);
00865
00866
00867
00868
00869 if(!mutedAgents_(clusterAgent) && (clusterAgent) > bestClusterScore)
00870 {
00871 bestClusterScore = score_(clusterAgent);
00872 bestClusterAgent = clusterAgent;
00873
00874
00875 }
00876 }
00877
00878
00879
00880
00881 return bestClusterAgent;
00882 }
00883
00884
00885
00886 mrs_natural
00887 BeatReferee::getBestSimilarAgent(mrs_natural newAgentPeriod, mrs_natural newAgentInitPhase, mrs_real newAgentScore)
00888 {
00889 mrs_real bestSimilarScore = NA;
00890 mrs_natural bestSimilarAgent = -1;
00891 mrs_natural period, phaseRaw, phase, k;
00892 mrs_realvec periodDiffs(nrAgents_);
00893 mrs_realvec phaseDiffs(nrAgents_);
00894 mrs_realvec bestSimilarity(nrAgents_);
00895 mrs_real fraction;
00896
00897
00898
00899
00900
00901 grantPoolSpace(-1, newAgentScore);
00902
00903
00904
00905
00906 mrs_real maxHypDiff = (3 * (maxPeriod_ - minPeriod_) + minPeriod_);
00907
00908 for(int a = 0; a < nrAgents_; a++)
00909 {
00910
00911
00912 if(!mutedAgentsTmp_(a) && !agentsJustCreated_(a) && agentsFamilyHist_(a, ((mrs_natural) beatCounter_(a)-1)) == a)
00913 {
00914
00915 period = (mrs_natural) lastPeriods_(a);
00916 phaseRaw = (mrs_natural) lastPhases_(a);
00917
00918
00919 k = (mrs_natural) (((mrs_real)(newAgentInitPhase - phaseRaw) / period) + 0.5);
00920 phase = phaseRaw + k * period;
00921
00922 periodDiffs(a) = abs(newAgentPeriod - period);
00923 phaseDiffs(a) = abs(newAgentInitPhase - phase);
00924
00925
00926
00927
00928 fraction = (1 - ((3*periodDiffs(a) + phaseDiffs(a)) / maxHypDiff));
00929
00930 if(score_(a) > 0)
00931 bestSimilarity(a) = fraction * score_(a);
00932 else
00933 bestSimilarity(a) = score_(a) / fraction;
00934
00935
00936 if(bestSimilarity(a) > bestSimilarScore)
00937 {
00938
00939
00940 bestSimilarScore = bestSimilarity(a);
00941 bestSimilarAgent = a;
00942 }
00943
00944
00945
00946
00947
00948
00949 }
00950 }
00951
00952
00953
00954
00955 return bestSimilarAgent;
00956 }
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 mrs_natural
01009 BeatReferee::calcFirstBacktracedBeat(mrs_natural initPeriod, mrs_natural initPhase)
01010 {
01011 mrs_natural count = - (mrs_natural) ((initPhase / (mrs_real)initPeriod)-MINIMUMREAL);
01012 mrs_natural firstBeat = (initPhase + initPeriod * count);
01013
01014
01015 return firstBeat;
01016 }
01017
01018
01019
01020
01021
01022
01023
01024
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039
01040 mrs_natural
01041 BeatReferee::existEqualBetterAgents(mrs_natural agentIndex, mrs_natural agentPeriod, mrs_natural agentPhase, mrs_real newAgentScore)
01042 {
01043
01044
01045 mrs_natural firstEqualBetterAgent = -1;
01046 mrs_realvec equalAgents = getEqualAgents(agentIndex, agentPeriod, agentPhase);
01047
01048 for(mrs_natural a = 0; a < nrAgents_; a++)
01049 {
01050 if((mrs_natural) equalAgents(a) == 1)
01051 {
01052 if(score_(a) >= newAgentScore)
01053 {
01054 firstEqualBetterAgent = a;
01055 break;
01056 }
01057 }
01058 }
01059
01060 return firstEqualBetterAgent;
01061 }
01062
01063
01064 mrs_natural
01065 BeatReferee::existEqualAgents(mrs_natural agentIndex, mrs_natural agentPeriod, mrs_natural agentPhase)
01066 {
01067
01068
01069 mrs_natural firstEqualAgent = -1;
01070 mrs_realvec equalAgents = getEqualAgents(agentIndex, agentPeriod, agentPhase);
01071
01072 for(mrs_natural a = 0; a < nrAgents_; a++)
01073 {
01074 if((mrs_natural) equalAgents(a) == 1)
01075 {
01076 firstEqualAgent = a;
01077 break;
01078 }
01079 }
01080
01081 return firstEqualAgent;
01082 }
01083
01084
01085 mrs_realvec
01086 BeatReferee::getEqualAgents(mrs_natural agentIndex, mrs_natural agentPeriod, mrs_natural agentPhase)
01087 {
01088 mrs_realvec equalAgents(nrAgents_);
01089
01090 for(mrs_natural a = 0; a < nrAgents_; a++)
01091 {
01092
01093
01094
01095 if(agentIndex == -1 || (agentIndex != a && !mutedAgentsTmp_(a)))
01096 {
01097
01098
01099 mrs_real phaseDiff = abs((agentPhase - lastPhases_(a)) / agentPeriod);
01100 mrs_natural periodDiff = abs(agentPeriod - (mrs_natural)lastPeriods_(a));
01101
01102
01103
01104
01105
01106
01107
01108 if((periodDiff <= eqPeriod_) && ((phaseDiff - floor(phaseDiff)) <= ((mrs_real)eqPhase_/(mrs_real)agentPeriod)
01109 || (phaseDiff - floor(phaseDiff)) >= (1-((mrs_real)eqPhase_/(mrs_real)agentPeriod))))
01110 {
01111 equalAgents(a) = 1;
01112
01113
01114
01115 }
01116 }
01117 }
01118
01119 return equalAgents;
01120 }
01121
01122
01123 void
01124 BeatReferee::checkAndKillEqualAgents(mrs_natural agentIndex)
01125 {
01126
01127
01128
01129 mrs_realvec equalAgents = getEqualAgents(agentIndex, (mrs_natural) lastPeriods_(agentIndex), (mrs_natural) lastPhases_(agentIndex));
01130
01131 for(mrs_natural a = 0; a < nrAgents_; a++)
01132 {
01133 if((mrs_natural)equalAgents(a) == 1)
01134 {
01135
01136
01137
01138 ostringstream motif;
01139 if(score_(agentIndex) >= score_(a))
01140 {
01141 if(a != bestAgentIndex_)
01142 {
01143 motif << "EQ";
01144 killAgent(a, motif.str(), agentIndex);
01145
01146
01147
01148 }
01149 else
01150 {
01151 motif << "EQ";
01152 killAgent(agentIndex, motif.str(), a);
01153 break;
01154
01155
01156
01157 }
01158 }
01159 else
01160 {
01161 if(agentIndex != bestAgentIndex_)
01162 {
01163 motif << "EQ";
01164 killAgent(agentIndex, motif.str(), a);
01165 break;
01166
01167
01168
01169 }
01170 else
01171 {
01172 motif << "EQ";
01173 killAgent(a, motif.str(), agentIndex);
01174
01175
01176
01177 }
01178 }
01179 }
01180 }
01181 }
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257 mrs_natural
01258 BeatReferee::getFirstAliveAgent()
01259 {
01260 mrs_natural firstAlive = 0;
01261 for(mrs_natural a = 0; a < nrAgents_; a++)
01262 {
01263 if(!mutedAgentsTmp_(a))
01264 {
01265 firstAlive = a;
01266 break;
01267 }
01268 }
01269 return firstAlive;
01270 }
01271
01272
01273 mrs_natural
01274 BeatReferee::getWorstAgent(mrs_natural callAgent)
01275 {
01276
01277
01278
01279
01280 mrs_real lowestScore = MAXREAL;
01281 mrs_natural lowestIndex = 0;
01282
01283
01284
01285
01286 for(mrs_natural a = lowestIndex; a < nrAgents_; a++)
01287 {
01288
01289 if(a != bestAgentBeforeTrigger_ && !mutedAgentsTmp_(a) && a != callAgent && score_(a) < lowestScore)
01290 {
01291 lowestScore = score_(a);
01292 lowestIndex = a;
01293
01294
01295
01296
01297 }
01298 }
01299
01300
01301
01302
01303
01304 return lowestIndex;
01305 }
01306
01307 mrs_realvec
01308 BeatReferee::calculateNewHypothesis(mrs_natural agentIndex, mrs_natural oldPeriod, mrs_natural prevBeat, mrs_natural error)
01309 {
01310 mrs_natural newPeriod;
01311 mrs_natural nextBeat;
01312
01313
01314
01315
01316
01317
01318 newPeriod = oldPeriod + ((mrs_natural) ((error*corFactor_) + ((error/abs(error)) * 0.5)));
01319
01320
01321 if(newPeriod < minPeriod_ || newPeriod > maxPeriod_)
01322 newPeriod = oldPeriod;
01323
01324 nextBeat = prevBeat + newPeriod + ((mrs_natural) ((error*corFactor_) + ((error/abs(error))) * 0.5));
01325
01326
01327
01328
01329
01330
01331
01332
01333
01334
01335
01336
01337
01338
01339
01340 mrs_realvec newHypothesis(2);
01341 newHypothesis(0) = newPeriod;
01342 newHypothesis(1) = nextBeat;
01343
01344 return newHypothesis;
01345 }
01346
01347 mrs_natural
01348 BeatReferee::calcNewPeriod(mrs_natural oldPeriod, mrs_natural error, mrs_real beta)
01349 {
01350
01351 mrs_natural newPeriod = oldPeriod + ((mrs_natural) ((error * beta) + (error/abs(error)) * 0.5));
01352
01353 if(newPeriod < minPeriod_ || newPeriod > maxPeriod_)
01354 newPeriod = oldPeriod;
01355
01356
01357
01358
01359 return newPeriod;
01360 }
01361
01362 mrs_realvec
01363 BeatReferee::calcChildrenHypothesis(mrs_natural oldPeriod, mrs_natural prevBeat, mrs_natural error)
01364 {
01365 mrs_natural nextBeat1 = -100, nextBeat2 = -100, nextBeat3 = -100;
01366 mrs_natural newPeriod1 = -100, newPeriod2 = -100, newPeriod3 = -100;
01367 mrs_realvec newHypotheses(3,3);
01368
01369
01370
01371
01372 if(child1Factor_ == 2.0)
01373 {
01374 newPeriod1 = oldPeriod;
01375 nextBeat1 = prevBeat + newPeriod1 + error;
01376 }
01377 else
01378 {
01379 newPeriod1 = calcNewPeriod(oldPeriod, error, child1Factor_);
01380
01381
01382
01383
01384 nextBeat1 = prevBeat + newPeriod1 + ((mrs_natural) ((error*child1Factor_) + ((error/abs(error))) * 0.5));
01385
01386 }
01387 newHypotheses(0,0) = newPeriod1;
01388 newHypotheses(0,1) = nextBeat1;
01389 newHypotheses(0,2) = 1;
01390
01391 if(child2Factor_ == 2.0)
01392 {
01393 newPeriod2 = oldPeriod;
01394 nextBeat2 = prevBeat + newPeriod2 + error;
01395 }
01396 else
01397 {
01398
01399
01400
01401
01402
01403
01404
01405 newPeriod2 = calcNewPeriod(oldPeriod, error, child2Factor_);
01406
01407
01408
01409
01410 nextBeat2 = prevBeat + newPeriod2 + ((mrs_natural) ((error*child2Factor_) + ((error/abs(error))) * 0.5));
01411 }
01412 newHypotheses(1,0) = newPeriod2;
01413 newHypotheses(1,1) = nextBeat2;
01414
01415
01416 if(abs(newPeriod2 - newPeriod1) <= eqPeriod_ && abs(nextBeat2 - nextBeat1) <= eqPhase_)
01417 newHypotheses(1,2) = 0;
01418 else newHypotheses(1,2) = 1;
01419
01420 if(child3Factor_ == 2.0)
01421 {
01422 newPeriod3 = oldPeriod;
01423 nextBeat3 = prevBeat + newPeriod3 + error;
01424 }
01425 else
01426 {
01427 newPeriod3 = calcNewPeriod(oldPeriod, error, child3Factor_);
01428
01429
01430
01431
01432 nextBeat3 = prevBeat + newPeriod3 + ((mrs_natural) ((error*child3Factor_) + ((error/abs(error))) * 0.5));
01433 }
01434 newHypotheses(2,0) = newPeriod3;
01435 newHypotheses(2,1) = nextBeat3;
01436
01437
01438 if((abs(newPeriod3 - newPeriod1) <= eqPeriod_ && abs(nextBeat3 - nextBeat1) <= eqPhase_)
01439 || (abs(newPeriod3 - newPeriod2) <= eqPeriod_ && abs(nextBeat3 - nextBeat2) <= eqPhase_))
01440 newHypotheses(2,2) = 0;
01441 else newHypotheses(2,2) = 1;
01442
01443
01444
01445
01446 return newHypotheses;
01447 }
01448
01449
01450 void
01451 BeatReferee::createChildren(mrs_natural agentIndex, mrs_natural oldPeriod, mrs_natural prevBeat, mrs_natural error,
01452 mrs_real agentScore, mrs_real beatCount)
01453 {
01454
01455
01456
01457
01458
01459
01460 mrs_real newScore;
01461 if(agentScore >= 0.0)
01462 newScore = agentScore * childrenScoreFactor_;
01463 else
01464 newScore = agentScore / childrenScoreFactor_;
01465
01466 mrs_realvec newHypotheses = calcChildrenHypothesis(oldPeriod, prevBeat, error);
01467
01468
01469
01470
01471 if(child1Factor_ != -1.0)
01472 createNewAgent((mrs_natural) newHypotheses(0,0), (mrs_natural) newHypotheses(0,1), newScore, beatCount, agentIndex);
01473 if(child2Factor_ != -1.0 && newHypotheses(1,2) == 1)
01474 createNewAgent((mrs_natural) newHypotheses(1,0), (mrs_natural) newHypotheses(1,1), newScore, beatCount, agentIndex);
01475 if(child3Factor_ != -1.0 && newHypotheses(2,2) == 1)
01476 createNewAgent((mrs_natural) newHypotheses(2,0), (mrs_natural) newHypotheses(2,1), newScore, beatCount, agentIndex);
01477
01478
01479
01480
01481
01482
01483
01484
01485 }
01486
01487
01488
01489 void
01490 BeatReferee::updateAgentHypothesis(mrs_natural agentIndex, mrs_natural oldPeriod,
01491 mrs_natural prevBeat, mrs_natural error)
01492 {
01493
01494 if(!mutedAgentsTmp_(agentIndex))
01495 {
01496 mrs_realvec newHypothesis = calculateNewHypothesis(agentIndex, oldPeriod, prevBeat, error);
01497 setNewHypothesis(agentIndex, (mrs_natural) newHypothesis(0), (mrs_natural) newHypothesis(1));
01498 }
01499
01500
01501
01502
01503
01504 }
01505
01506
01507 void
01508 BeatReferee::setNewHypothesis(mrs_natural agentIndex, mrs_natural newPeriod, mrs_natural nextBeat)
01509 {
01510 agentControl_(agentIndex, 0) = 1.0;
01511 agentControl_(agentIndex, 1) = newPeriod;
01512 agentControl_(agentIndex, 2) = nextBeat;
01513 agentControl_(agentIndex, 3) = timeElapsed_;
01514
01515 updControl(ctrl_agentControl_, agentControl_);
01516
01517 lastPeriods_(agentIndex) = newPeriod;
01518 }
01519
01520
01521 void
01522 BeatReferee::grantPoolSpace2(mrs_natural callAgent, mrs_natural nrRequired, mrs_realvec newAgentsScore)
01523
01524 {
01525
01526
01527
01528
01529
01530
01531
01532 mrs_natural nrAvailable = 0;
01533 for(int a = 0; a < mutedAgentsTmp_.getSize(); a++)
01534 {
01535 if(mutedAgentsTmp_(a))
01536 nrAvailable++;
01537 }
01538
01539
01540
01541
01542 nrRequired = nrRequired - nrAvailable;
01543
01544
01545 while(nrRequired > 0)
01546 {
01547 mrs_natural agentInd2Kill = getWorstAgent(callAgent);
01548
01549
01550
01551
01552
01553 if(score_(agentInd2Kill) <= newAgentsScore(nrRequired-1))
01554 killAgent(agentInd2Kill, "POOL", callAgent);
01555
01556 nrRequired--;
01557 }
01558 }
01559
01560 void
01561 BeatReferee::grantPoolSpace(mrs_natural callAgent, mrs_real newAgentScore)
01562 {
01563 mrs_bool isAvailable = false;
01564
01565 for(int a = 0; a < mutedAgentsTmp_.getSize(); a++)
01566 {
01567
01568
01569
01570 if(mutedAgentsTmp_(a))
01571 {
01572
01573
01574
01575 isAvailable = true;
01576 break;
01577 }
01578 }
01579
01580
01581 if(!isAvailable)
01582 {
01583 mrs_natural agentInd2Kill = getWorstAgent(callAgent);
01584
01585
01586
01587
01588
01589 if(score_(agentInd2Kill) <= newAgentScore)
01590 killAgent(agentInd2Kill, "POOL", callAgent);
01591
01592
01593 }
01594 }
01595
01596
01597 void
01598 BeatReferee::handleAgentsTansition(mrs_natural agent)
01599 {
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609 mrs_natural fatherAgent, sonAgent;
01610 if(considerAgentTransitionBeat_(agent) == -1)
01611 {
01612 sonAgent = agent;
01613
01614
01615
01616
01617
01618
01619
01620
01621
01622
01623
01624 agentsHistory_(sonAgent, (mrs_natural) beatCounter_(sonAgent)) = -1;
01625 agentsFamilyHist_(agent, (mrs_natural) beatCounter_(sonAgent)) = -1;
01626
01627 beatCounter_(sonAgent)--;
01628
01629 considerAgentTransitionBeat_(agent) = NA;
01630 }
01631
01632 fatherAgent = agent;
01633
01634 for(int j = 0; j < considerFatherTransitionBeat_.getCols(); j++)
01635 {
01636 if(considerFatherTransitionBeat_(agent, j) >= 0)
01637 {
01638 sonAgent = (mrs_natural) considerFatherTransitionBeat_(agent, j);
01639
01640 agentsHistory_(sonAgent, (mrs_natural) beatCounter_(sonAgent))
01641 = agentsHistory_(fatherAgent, (mrs_natural) beatCounter_(fatherAgent));
01642 agentsFamilyHist_(sonAgent, (mrs_natural) beatCounter_(sonAgent))
01643 = agentsFamilyHist_(fatherAgent, (mrs_natural) beatCounter_(fatherAgent));
01644
01645
01646
01647
01648
01649
01650
01651
01652
01653
01654
01655 beatCounter_(sonAgent)++;
01656
01657 considerFatherTransitionBeat_(agent, j) = NA;
01658 }
01659 }
01660 }
01661
01662 void
01663 BeatReferee::cleanAgentAndFatherTransitionFlags(mrs_natural agentIndex)
01664 {
01665
01666
01667
01668 if(considerAgentTransitionBeat_(agentIndex) >= 0)
01669 {
01670 mrs_natural fatherRequested = (mrs_natural) considerAgentTransitionBeat_(agentIndex);
01671
01672
01673
01674
01675
01676 for(int j = 0; j < considerFatherTransitionBeat_.getCols(); j++)
01677 {
01678 if(considerFatherTransitionBeat_(fatherRequested, j) == agentIndex)
01679 {
01680
01681
01682
01683
01684 considerFatherTransitionBeat_(fatherRequested, j) = NA;
01685 break;
01686 }
01687 }
01688 }
01689 considerAgentTransitionBeat_(agentIndex) = NA;
01690 }
01691
01692
01693
01694 mrs_natural
01695 BeatReferee::createNewAgent(mrs_natural newPeriod, mrs_natural firstBeat,
01696 mrs_real newScore, mrs_real beatCount, mrs_natural fatherAgent)
01697 {
01698
01699 if(fatherAgent >= 0 && mutedAgentsTmp_(fatherAgent) == 1.0)
01700 {
01701 if(logFile_)
01702 debugAddEvent("CREATE_REF_KF", -1, newPeriod, firstBeat, newScore, bestScore_, fatherAgent);
01703 return -1;
01704 }
01705
01706 else if (timeElapsed_ > timeBeforeKilling_ && newScore < bestScore_ && fabs(bestScore_-newScore) > 0.1
01707 && fabs(bestScore_ - newScore) > fabs(bestScore_ * obsoleteFactor_))
01708 {
01709 if(logFile_)
01710 debugAddEvent("CREATE_REF_SCORE", -1, newPeriod, firstBeat, newScore, bestScore_, fatherAgent);
01711 return -1;
01712 }
01713
01714
01715
01716
01717
01718 grantPoolSpace(fatherAgent, newScore);
01719
01720 mrs_natural returnCreatedAgent = -1;
01721 for(int a = 0; a < mutedAgentsTmp_.getSize(); a++)
01722 {
01723
01724 if(mutedAgentsTmp_(a))
01725 {
01726
01727
01728
01729
01730 mutedAgentsTmp_(a) = 0.0;
01731 updControl(ctrl_mutedAgents_, mutedAgentsTmp_);
01732
01733
01734
01735
01736
01737
01738 setNewHypothesis(a, newPeriod, firstBeat);
01739
01740
01741 score_(a) = newScore;
01742
01743
01744
01745
01746
01747 beatCounter_(a) = beatCount;
01748
01749
01750
01751
01752
01753 lastPeriods_(a) = newPeriod;
01754
01755 lastPhases_(a) = (firstBeat - newPeriod);
01756
01757 initPeriod_(a) = newPeriod;
01758 missedBeatsCount_(a) = 0.0;
01759
01760
01761
01762
01763
01764
01765
01766
01767
01768 agentControl_(a, 3) = timeElapsed_+1;
01769 updControl(ctrl_agentControl_, agentControl_);
01770
01771 returnCreatedAgent = a;
01772
01773 agentsJustCreated_(a) = 1.0;
01774
01775 if(logFile_)
01776 debugAddEvent("CREATE", a, newPeriod, firstBeat, score_(a), bestScore_, fatherAgent);
01777
01778
01779 if(nonCausal_)
01780 {
01781
01782
01783
01784 if(fatherAgent >= 0)
01785 {
01786 mrs_natural lastFlagInd = 0;
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797 cleanAgentAndFatherTransitionFlags(a);
01798
01799
01800
01801
01802 if((abs(firstBeat - lastPhases_(fatherAgent)) / lastPeriods_(fatherAgent)) < beatTransitionTol_)
01803 {
01804
01805
01806
01807 considerAgentTransitionBeat_(a) = -1;
01808
01809 if(logFile_)
01810 debugAddEvent("TRANSITION_REM", a, (mrs_natural) lastPeriods_(a),
01811 (mrs_natural) (firstBeat), score_(a), bestScore_, fatherAgent);
01812
01813
01814
01815
01816
01817 }
01818
01819
01820
01821
01822 else if((abs(firstBeat - lastPhases_(fatherAgent)) / lastPeriods_(fatherAgent)) > (1+beatTransitionTol_))
01823 {
01824
01825
01826 lastFlagInd = 0;
01827 for(int j = 0; j < considerFatherTransitionBeat_.getCols(); j++)
01828 {
01829 if(considerFatherTransitionBeat_(fatherAgent, j) < 0)
01830 {
01831
01832
01833
01834 lastFlagInd = j;
01835 break;
01836 }
01837 }
01838 considerFatherTransitionBeat_(fatherAgent, lastFlagInd) = a;
01839
01840 considerAgentTransitionBeat_(a) = fatherAgent;
01841
01842 if(logFile_)
01843 debugAddEvent("TRANSITION_ADD", a, (mrs_natural) lastPeriods_(a),
01844 (mrs_natural) (firstBeat+lastPeriods_(a)), score_(a), bestScore_, fatherAgent);
01845
01846
01847
01848
01849
01850
01851
01852 }
01853
01854
01855 for(int i = 0; i < beatCount; i++)
01856 {
01857 agentsHistory_(a, i) = agentsHistory_(fatherAgent, i);
01858 agentsFamilyHist_(a, i) = agentsFamilyHist_(fatherAgent, i);
01859
01860
01861
01862
01863
01864
01865
01866
01867
01868
01869
01870 }
01871
01872
01873
01874
01875
01876
01877
01878
01879 }
01880 }
01881
01882 break;
01883 }
01884 }
01885
01886 return returnCreatedAgent;
01887 }
01888
01889
01890 void
01891 BeatReferee::resetSystem(mrs_natural saveAgent)
01892 {
01893
01894 for(int a = 0; a < mutedAgentsTmp_.getSize(); a++)
01895 {
01896 if(a != saveAgent) killAgent(a, "RESET");
01897 }
01898
01899
01900 bestScore_ = NA;
01901
01902 if(logFile_)
01903 debugAddEvent("RESET_SYSTEM", -1, -1, -1, -1, bestScore_, saveAgent);
01904 }
01905
01906
01907 void
01908 BeatReferee::killAgent(mrs_natural agentIndex, mrs_string motif, mrs_natural callAgent)
01909 {
01910
01911
01912 if(strcmp(motif.c_str(), "RESET") == 0 || (agentIndex != bestAgentBeforeTrigger_ && agentIndex != bestAgentIndex_ && !mutedAgentsTmp_(agentIndex)))
01913 {
01914
01915
01916
01917
01918 if(logFile_)
01919 {
01920 ostringstream killMotif;
01921 killMotif << "KILL_" << motif;
01922 debugAddEvent(killMotif.str(), agentIndex, (mrs_natural) lastPeriods_(agentIndex),
01923 (mrs_natural) lastPhases_(agentIndex), score_(agentIndex), bestScore_, callAgent);
01924 }
01925
01926
01927
01928 mutedAgentsTmp_(agentIndex) = 1.0;
01929 updControl(ctrl_mutedAgents_, mutedAgentsTmp_);
01930
01931 score_(agentIndex) = NA;
01932 beatCounter_(agentIndex) = 0.0;
01933
01934
01935 lastPeriods_(agentIndex) = 0.0;
01936 lastPhases_(agentIndex) = 0.0;
01937
01938 missedBeatsCount_(agentIndex) = 0.0;
01939
01940
01941 cleanAgentAndFatherTransitionFlags(agentIndex);
01942 for (int j = 0; j < considerFatherTransitionBeat_.getCols(); j++)
01943 considerFatherTransitionBeat_(agentIndex, j) = NA;
01944
01945
01946
01947
01948
01949 }
01950 else if(logFile_)
01951 {
01952 ostringstream killMotif;
01953 killMotif << "BESTKILL_" << motif;
01954 debugAddEvent(killMotif.str(), agentIndex, (mrs_natural) lastPeriods_(agentIndex),
01955 (mrs_natural) lastPhases_(agentIndex), score_(agentIndex), bestScore_, callAgent);
01956 }
01957 }
01958
01959 void
01960 BeatReferee::calcAbsoluteBestScore()
01961 {
01962 mrs_natural firstAliveAgent = getFirstAliveAgent();
01963 mrs_real bestLocalScore = score_(firstAliveAgent);
01964 mrs_natural bestLocalAgent = firstAliveAgent;
01965 for (mrs_natural o = firstAliveAgent+1; o < nrAgents_; o++)
01966 {
01967
01968 if(!mutedAgentsTmp_(o))
01969 {
01970 if(score_(o) > bestLocalScore)
01971 {
01972 bestLocalScore = score_(o);
01973 bestLocalAgent = o;
01974 }
01975 }
01976 }
01977
01978 if((bestScore_ >= 0 && bestLocalScore > bestFactor_ * bestScore_) ||
01979 (bestScore_ < 0 && bestLocalScore > bestScore_ / bestFactor_))
01980 {
01981 if(logFile_)
01982 debugAddEvent("BEST", bestLocalAgent, (mrs_natural) lastPeriods_(bestLocalAgent),
01983 (mrs_natural) lastPhases_(bestLocalAgent), bestLocalScore, bestScore_);
01984
01985 bestScore_ = bestLocalScore;
01986 bestAgentIndex_ = bestLocalAgent;
01987 bestFinalAgent_ = bestAgentIndex_;
01988 }
01989
01990 }
01991
01992 void
01993 BeatReferee::debugCreateFile()
01994 {
01995 mrs_string beatTrackingMode, triggerGTTol;
01996 if(nonCausal_)
01997 beatTrackingMode = "non-causal";
01998 else beatTrackingMode = "causal";
01999 if(strcmp(inductionMode_.c_str(), "groundtruth") == 0)
02000 {
02001 stringstream tmp;
02002 tmp << triggerGtTolerance_;
02003 triggerGTTol = tmp.str();
02004 }
02005 else triggerGTTol = "NA";
02006
02007 fstream outStream;
02008 outStream.open(logFileName_.c_str(), ios::out|ios::trunc);
02009 outStream << "Beat-Tracking: " << beatTrackingMode << "; Induction-Mode: " << inductionMode_ << "; TriggerGtTol: "
02010 << triggerGTTol << "; Induction: " << ctrl_gtInductionMode_->to<mrs_string>() << "; Induction-Time: " << inductionTime_ << "<<"
02011 << (((inductionTime_ * hopSize_) - frames2SecsAdjustment_) / srcFs_) << ">>" << endl;
02012 cerr << "Log File in " << logFileUnits_ << ": " << logFileName_ << endl;
02013 outStream.close();
02014 }
02015
02016 void
02017 BeatReferee::debugAddEvent(mrs_string ibtEvent, mrs_natural agentIndex,
02018 mrs_natural period, mrs_natural lastBeat, mrs_real score, mrs_real bestScore, mrs_natural callAgent)
02019 {
02020
02021 fstream outStream;
02022 outStream.open(logFileName_.c_str(), ios::out|ios::app);
02023
02024 if(strcmp(logFileUnits_.c_str(), "frames") == 0)
02025 {
02026 outStream << ibtEvent << "|" << timeElapsed_ << "|" << agentIndex << "|" << period << "|" << lastBeat
02027 << "|" << score << "|" << bestScore << "|" << callAgent << endl;
02028 }
02029 else if(strcmp(logFileUnits_.c_str(), "seconds") == 0)
02030 outStream << ibtEvent << "|" << (((timeElapsed_ * hopSize_) - frames2SecsAdjustment_) / srcFs_) << "|" << agentIndex << "|"
02031 << period << "|" << (((lastBeat * hopSize_) - frames2SecsAdjustment_) / srcFs_) << "|" << score << "|"
02032 << bestScore << "|" << callAgent << endl;
02033 else if(strcmp(logFileUnits_.c_str(), "frames+seconds") == 0)
02034 outStream << ibtEvent << "|" << timeElapsed_ << "<<" << (((timeElapsed_ * hopSize_) - frames2SecsAdjustment_) / srcFs_) << ">>|"
02035 << agentIndex << "|" << period << "|" << lastBeat << "<<" << (((lastBeat * hopSize_) - frames2SecsAdjustment_) / srcFs_)
02036 << ">>|" << score << "|" << bestScore << "|" << callAgent << endl;
02037
02038
02039
02040
02041 outStream.close();
02042 }
02043
02044 void
02045 BeatReferee::debugAddMsg(mrs_string ibtMsg)
02046 {
02047 fstream outStream;
02048 outStream.open(logFileName_.c_str(), ios::out|ios::app);
02049
02050 if(strcmp(logFileUnits_.c_str(), "frames") == 0)
02051 outStream << ibtMsg << "|" << timeElapsed_ << endl;
02052 else if(strcmp(logFileUnits_.c_str(), "seconds") == 0)
02053 outStream << ibtMsg << "|" << (((timeElapsed_ * hopSize_) - frames2SecsAdjustment_) / srcFs_) << endl;
02054 else if(strcmp(logFileUnits_.c_str(), "frames+seconds") == 0)
02055 outStream << ibtMsg << "|" << timeElapsed_ << "<<" << (((timeElapsed_ * hopSize_) - frames2SecsAdjustment_) / srcFs_) << ">>" << endl;
02056
02057 outStream.close();
02058
02059 }
02060
02061 void
02062 BeatReferee::initialization()
02063 {
02064 agentsHistory_.create(nrAgents_, maxNrBeats_);
02065 agentsFamilyHist_.create(nrAgents_, maxNrBeats_);
02066 generationCount_.create(nrAgents_);
02067 score_.create(nrAgents_);
02068 lastPeriods_.create(nrAgents_);
02069 lastPhases_.create(nrAgents_);
02070 mutedAgents_.create(nrAgents_);
02071 mutedAgentsTmp_.create(nrAgents_);
02072 beatCounter_.create(nrAgents_);
02073 initPeriod_.create(nrAgents_);
02074 missedBeatsCount_.create(nrAgents_);
02075 considerAgentTransitionBeat_.create(nrAgents_);
02076 considerFatherTransitionBeat_.create(nrAgents_,10);
02077
02078
02079
02080
02081 agentControl_.create(nrAgents_, 4);
02082 updControl(ctrl_agentControl_, agentControl_);
02083
02084
02085 for(int i = 0; i < nrAgents_; i++)
02086 {
02087 mutedAgents_(0, i) = 1.0;
02088 mutedAgentsTmp_(0, i) = 1.0;
02089
02090
02091 for (int j = 0; j < considerFatherTransitionBeat_.getCols(); j++)
02092 considerFatherTransitionBeat_(i, j) = NA;
02093
02094 considerAgentTransitionBeat_(i) = NA;
02095 }
02096 updControl(ctrl_mutedAgents_, mutedAgents_);
02097
02098
02099
02100 inductionEnabler_(1, 0) = 1.0;
02101
02102
02103 inductionEnabler_(0, 0) = 1.0;
02104 updControl(ctrl_inductionEnabler_, inductionEnabler_);
02105
02106 if(!strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "-1") == 0 &&
02107 !strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02108 {
02109 logFileName_ = ctrl_logFileName_->to<mrs_string>();
02110 logFileUnits_ = ctrl_logFile_->to<mrs_string>();
02111 logFile_ = true;
02112 debugCreateFile();
02113 }
02114
02115
02116 if(strcmp(inductionMode_.c_str(), "givetransitions") == 0)
02117 loadTriggerTimes(triggerTimesFile_);
02118 else if(strcmp(inductionMode_.c_str(), "supervised") == 0)
02119 {
02120 supervisedBestScores_.resize(1);
02121 supervisedBestScoresMeans_.resize(1);
02122 lastTriggerInductionTime_ = 0;
02123 lastBestScoreMeanDiff_ = supervisedTriggerThres_;
02124 }
02125
02126 triggerInductionTime_ = inductionTime_;
02127 }
02128
02129 void
02130 BeatReferee::myProcess(realvec& in, realvec& out)
02131 {
02132
02133 if(startSystem_)
02134 {
02135 initialization();
02136 startSystem_ = false;
02137 }
02138
02139
02140
02141
02142
02143
02144
02145
02146 out.setval(0.0);
02147 ctrl_beatDetected_->setValue(0.0);
02148
02149 agentControl_ = ctrl_agentControl_->to<mrs_realvec>();
02150
02151 for(mrs_natural i = 0; i < agentControl_.getRows(); i++)
02152 {
02153 agentControl_(i, 3) = timeElapsed_+1;
02154 updControl(ctrl_agentControl_, agentControl_);
02155 agentsJustCreated_(i) = 0.0;
02156 }
02157
02158 ctrl_tickCount_->setValue(timeElapsed_+1);
02159
02160
02161
02162
02163
02164 mutedAgents_ = ctrl_mutedAgents_->to<mrs_realvec>();
02165
02166 mutedAgentsTmp_ = ctrl_mutedAgents_->to<mrs_realvec>();
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181 if(startTracking_)
02182 {
02183
02184 mrs_real agentFlag;
02185 mrs_natural agentPeriod;
02186 mrs_natural agentPrevBeat;
02187 mrs_real agentTolerance;
02188 mrs_natural agentError;
02189 mrs_real agentDScore;
02190
02191
02192 for (mrs_natural o = 0; o < nrAgents_; o++)
02193 {
02194 agentFlag = in(o, 0);
02195
02196
02197
02198
02199
02200
02201
02202
02203
02204 if(!mutedAgents_(o) && !mutedAgentsTmp_(o) && agentFlag == EVAL)
02205 {
02206 agentDScore = in(o, 5);
02207 agentPeriod = (mrs_natural) in(o, 1);
02208 agentPrevBeat = (mrs_natural) in(o, 2);
02209
02210
02211 lastPeriods_(o) = agentPeriod;
02212 lastPhases_(o) = agentPrevBeat;
02213
02214
02215
02216
02217
02218 score_(o) += agentDScore;
02219
02220
02221
02222
02223
02224
02225
02226 if(score_(bestAgentIndex_) != bestScore_)
02227 {
02228
02229
02230 bestScore_ = score_(bestAgentIndex_);
02231
02232 }
02233
02234 if(missedBeatsCount_(o) >= lostFactor_)
02235 {
02236 killAgent(o, "LOST");
02237 }
02238
02239 else if(timeElapsed_ > timeBeforeKilling_ && score_(o) < bestScore_ && fabs(bestScore_-score_(o)) > 0.1
02240 && fabs(bestScore_ - score_(o)) > fabs(bestScore_ * obsoleteFactor_))
02241 {
02242
02243 killAgent(o, "OBS");
02244 }
02245 else
02246 checkAndKillEqualAgents(o);
02247
02248
02249
02250
02251
02252
02253
02254
02255
02256
02257
02258 }
02259 }
02260
02261
02262
02263 for (mrs_natural o = 0; o < nrAgents_; o++)
02264 {
02265
02266
02267 if(!mutedAgents_(o) && !mutedAgentsTmp_(o) && !agentsJustCreated_(o))
02268 {
02269 agentFlag = in(o, 0);
02270
02271 if(agentFlag == EVAL)
02272 {
02273 agentPeriod = (mrs_natural) in(o, 1);
02274 agentPrevBeat = (mrs_natural) in(o, 2);
02275 agentTolerance = in(o, 3);
02276 agentError = (mrs_natural) in(o, 4);
02277 agentDScore = in(o, 5);
02278
02279
02280
02281
02282
02283
02284 if(agentTolerance == INNER)
02285 {
02286 if(logFile_)
02287 debugAddEvent("UPDATE", o, agentPeriod, agentPrevBeat, score_(o), bestScore_, o);
02288
02289 missedBeatsCount_(o) = 0.0;
02290
02291 if(abs(agentError) > 0)
02292 {
02293 updateAgentHypothesis(o, agentPeriod, agentPrevBeat, agentError);
02294 }
02295 }
02296
02297
02298
02299
02300 if(agentTolerance == OUTTER)
02301 {
02302 if(logFile_)
02303 debugAddEvent("UPD_OUTTER", o, agentPeriod, agentPrevBeat, score_(o), bestScore_, o);
02304
02305
02306
02307
02308 createChildren(o, agentPeriod, agentPrevBeat, agentError, score_(o), beatCounter_(o));
02309
02310
02311 missedBeatsCount_(o)++;
02312 }
02313 }
02314
02315 if(agentFlag == BEAT)
02316 {
02317
02318 if(nonCausal_)
02319 {
02320 agentsHistory_(o, (mrs_natural) beatCounter_(o)) = timeElapsed_;
02321 agentsFamilyHist_(o, (mrs_natural) beatCounter_(o)) = o;
02322
02323
02324
02325
02326 handleAgentsTansition(o);
02327 }
02328
02329
02330
02331
02332
02333
02334 beatCounter_(o)++;
02335
02336
02337 lastPhases_(o) = timeElapsed_;
02338
02339
02340
02341
02342
02343
02344
02345
02346 if(o == bestAgentIndex_)
02347 {
02348
02349
02350
02351 if((timeElapsed_ - lastBeatTime_) >= beatTransitionTol_*lastBeatPeriod_)
02352 {
02353
02354
02355
02356
02357
02358
02359
02360 ctrl_beatDetected_->setValue(1.0);
02361 out.setval(1.0);
02362
02363
02364
02365 historyCount_(o)++;
02366
02367 outputCount_ ++;
02368 lastBeatTime_ = timeElapsed_;
02369 lastBeatPeriod_ = (mrs_natural) lastPeriods_(o);
02370
02371 if(logFile_)
02372 debugAddEvent("==========> BEAT", o, (mrs_natural) lastPeriods_(o),
02373 (mrs_natural) lastPhases_(o), score_(o), bestScore_);
02374
02375
02376 if(strcmp(inductionMode_.c_str(), "groundtruth") == 0)
02377 {
02378
02379 mrs_natural localBeatError = checkBeatInGTFile();
02380 localBeatError == 0 ? lostGTBeatsCount_ = 0 : lostGTBeatsCount_+=localBeatError;
02381
02382
02383
02384 if(lostGTBeatsCount_ == 0)
02385 {
02386 bestAgentBeforeTrigger_ = bestAgentIndex_;
02387
02388 if(logFile_)
02389 debugAddEvent("BEST_TRIGGER", bestAgentBeforeTrigger_, (mrs_natural) lastPeriods_(bestAgentBeforeTrigger_),
02390 (mrs_natural) lastPhases_(bestAgentBeforeTrigger_), score_(bestAgentBeforeTrigger_), bestScore_);
02391
02392
02393
02394 }
02395
02396
02397 if(lostGTBeatsCount_ >= triggerGtTolerance_)
02398 {
02399
02400 lostGTBeatsCount_ = 0;
02401 triggerInductionExternalRequest_ = true;
02402
02403
02404
02405
02406 if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02407 {
02408 triggerCount_++;
02409 ostringstream oss;
02410 oss << ctrl_destFileName_->to<mrs_string>() << "_logTrigger.txt";
02411 ofstream trigCountStream;
02412 trigCountStream.open(oss.str().c_str(), ios::out|ios::trunc);
02413 trigCountStream << triggerCount_ << endl;
02414 trigCountStream.close();
02415 }
02416 }
02417 }
02418 else if(strcmp(inductionMode_.c_str(), "repeated") == 0 ||
02419 (strcmp(inductionMode_.c_str(), "random") == 0) ||
02420 (strcmp(inductionMode_.c_str(), "supervised") == 0))
02421 {
02422
02423 bestAgentBeforeTrigger_ = bestAgentIndex_;
02424
02425 if(logFile_)
02426 debugAddEvent("BEST_TRIGGER", bestAgentBeforeTrigger_, (mrs_natural) lastPeriods_(bestAgentBeforeTrigger_),
02427 (mrs_natural) lastPhases_(bestAgentBeforeTrigger_), score_(bestAgentBeforeTrigger_), bestScore_);
02428 }
02429
02430 else if(strcmp(inductionMode_.c_str(), "givetransitions") == 0 &&
02431 (timeElapsed_ + (mrs_natural)lastPeriods_(bestAgentIndex_)) >= transitionTimes_(triggerCount_) && transitionsConsidered_(triggerCount_) == 0.0)
02432 {
02433 bestAgentBeforeTrigger_ = bestAgentIndex_;
02434 resetSystem(bestAgentBeforeTrigger_);
02435 transitionsConsidered_(triggerCount_) = 1;
02436
02437 if(logFile_)
02438 debugAddEvent("BEST_TRIGGER", bestAgentBeforeTrigger_, (mrs_natural) lastPeriods_(bestAgentBeforeTrigger_),
02439 (mrs_natural) lastPhases_(bestAgentBeforeTrigger_), score_(bestAgentBeforeTrigger_), bestScore_);
02440 }
02441 }
02442 else if(logFile_)
02443 debugAddEvent("BEAT CANCEL", o, (mrs_natural) lastPeriods_(o),
02444 (mrs_natural) lastPhases_(o), score_(o), bestScore_);
02445
02446
02447 calcAbsoluteBestScore();
02448 }
02449 }
02450 }
02451 }
02452 }
02453
02454
02455 if(processInduction_)
02456 {
02457 if(logFile_)
02458 debugAddMsg("=========== INDUCTION ===========");
02459
02460 firstHypotheses_ = ctrl_firstHypotheses_->to<mrs_realvec>();
02461
02462
02463 maxPeriod_ = ctrl_maxPeriod_->to<mrs_natural>();
02464 minPeriod_ = ctrl_minPeriod_->to<mrs_natural>();
02465
02466 maxNrBeats_ = (mrs_natural) (ceil(((mrs_real) soundFileSize_) / ((mrs_real) minPeriod_)) * 1.7);
02467 agentsHistory_.stretch(nrAgents_, maxNrBeats_);
02468 agentsFamilyHist_.stretch(nrAgents_, maxNrBeats_);
02469
02470 mrs_natural newAgentPeriod;
02471 mrs_natural newAgentPhase;
02472 mrs_real newAgentScore;
02473
02474 if(strcmp(inductionMode_.c_str(), "givetransitions") == 0)
02475 bestAgentBeforeTrigger_ = bestAgentIndex_;
02476 if(startTracking_ && resetAfterNewInduction_)
02477 {
02478 resetSystem(bestAgentBeforeTrigger_);
02479 lastBeatPeriod_ = 0;
02480 }
02481
02482
02483
02484
02485
02486 mrs_realvec newAgentsScore(firstHypotheses_.getRows());
02487 mrs_natural count = 0;
02488 for(int i = 0; i < firstHypotheses_.getRows(); i++)
02489 {
02490 if((mrs_natural) firstHypotheses_(i,0) > 0)
02491 {
02492
02493
02494 newAgentPeriod = (mrs_natural) firstHypotheses_(i,0);
02495 newAgentPhase = (mrs_natural) firstHypotheses_(i,1);
02496 newAgentScore = firstHypotheses_(i,2);
02497
02498 if(!resetAfterNewInduction_)
02499 {
02500
02501 if(existEqualBetterAgents(-1, newAgentPeriod, newAgentPhase, newAgentScore) == -1)
02502 {
02503 newAgentsScore(count) = newAgentScore;
02504
02505
02506
02507
02508
02509 count++;
02510 }
02511 else
02512 {
02513 firstHypotheses_(i,0) = 0.0;
02514 firstHypotheses_(i,1) = 0.0;
02515 firstHypotheses_(i,2) = 0.0;
02516
02517 if(logFile_)
02518 debugAddEvent("EQ_TRIGGER", -1, newAgentPeriod, newAgentPhase, newAgentScore,
02519 bestScore_, existEqualAgents(-1, newAgentPeriod, newAgentPhase));
02520 }
02521 }
02522 }
02523
02524
02525 grantPoolSpace2(-1, count, newAgentsScore);
02526 }
02527
02528
02529
02530 for(mrs_natural i = 0; i < firstHypotheses_.getRows(); i++)
02531 {
02532 if((mrs_natural) firstHypotheses_(i,0) > 0)
02533 {
02534
02535
02536 newAgentPeriod = (mrs_natural) firstHypotheses_(i,0);
02537 newAgentPhase = (mrs_natural) firstHypotheses_(i,1);
02538 newAgentScore = firstHypotheses_(i,2);
02539
02540
02541
02542
02543 mrs_natural createdAgent = -1;
02544 if(startTracking_)
02545 {
02546
02547 createdAgent = createNewAgent(newAgentPeriod, newAgentPhase, newAgentScore, beatCounter_(bestAgentBeforeTrigger_), bestAgentBeforeTrigger_);
02548 }
02549 else
02550 createdAgent = createNewAgent(newAgentPeriod, newAgentPhase, newAgentScore, 0, -1);
02551
02552
02553
02554
02555 if(createdAgent >= 0)
02556 {
02557 if(newAgentScore > bestScore_)
02558 {
02559 bestScore_ = newAgentScore;
02560 bestAgentIndex_ = createdAgent;
02561 bestFinalAgent_ = bestAgentIndex_;
02562
02563
02564 if(!startTracking_)
02565 {
02566 bestAgentBeforeTrigger_ = bestAgentIndex_;
02567
02568 if(logFile_)
02569 debugAddEvent("BEST_TRIGGER", bestAgentBeforeTrigger_, (mrs_natural) lastPeriods_(bestAgentBeforeTrigger_),
02570 (mrs_natural) lastPhases_(bestAgentBeforeTrigger_), score_(bestAgentBeforeTrigger_), bestScore_);
02571 }
02572
02573 if(logFile_)
02574 debugAddEvent("BEST", bestAgentIndex_, newAgentPeriod, newAgentPhase, newAgentScore, bestScore_);
02575 }
02576 }
02577
02578
02579 if(i == nrAgents_-1)
02580 {
02581 MRSWARN("Last hypotheses discarted because the nr. of hypotheses surpasses the nr. of BeatAgents");
02582 break;
02583 }
02584
02585 if(newAgentPeriod == 0)
02586 {
02587 MRSWARN("Last hypotheses discarted because no more periods considered");
02588 break;
02589 }
02590 }
02591
02592
02593
02594
02595
02596 if(strcmp(inductionMode_.c_str(), "supervised") == 0)
02597 {
02598 supervisedBestScores_.resize(1);
02599 supervisedBestScoresMeans_.resize(1);
02600 lastTriggerInductionTime_ = timeElapsed_;
02601 lastBestScoreMeanDiff_ = supervisedTriggerThres_;
02602 }
02603
02604 }
02605
02606
02607
02608 if(backtrace_)
02609 {
02610 backtraceEndTime_ = timeElapsed_;
02611
02612
02613 for(mrs_natural i = 0; i < agentControl_.getRows(); i++)
02614 {
02615 agentControl_(i, 3) = timeElapsed_-inductionTime_;
02616 updControl(ctrl_agentControl_, agentControl_);
02617 }
02618 ctrl_tickCount_->setValue(timeElapsed_-inductionTime_);
02619
02620 timeElapsed_ = timeElapsed_-inductionTime_-1;
02621
02622
02623
02624 updControl(ctrl_backtrace_, false);
02625 }
02626
02627 else
02628 timeBeforeKilling_+=inductionTime_;
02629
02630
02631 inductionEnabler_(0, 0) = 1.0;
02632 updControl(ctrl_inductionEnabler_, inductionEnabler_);
02633
02634 if(startTracking_ && resetAfterNewInduction_)
02635 {
02636 killAgent(bestAgentBeforeTrigger_, "RESET", -1);
02637 calcAbsoluteBestScore();
02638 }
02639
02640 processInduction_ = false;
02641 startTracking_ = true;
02642 updControl(ctrl_triggerInduction_, false);
02643
02644 }
02645
02646
02647
02648
02649
02650 if(timeElapsed_ > backtraceEndTime_ && ((ctrl_triggerInductionExternalRequest_->to<mrs_bool>() || timeElapsed_ == triggerInductionTime_-2)
02651 || (strcmp(inductionMode_.c_str(), "givetransitions") == 0 && timeElapsed_ == triggerTimes_(triggerCount_)-2)))
02652 {
02653 updControl(ctrl_resetFeatWindow_, true);
02654
02655 if(ctrl_triggerInductionExternalRequest_->to<mrs_bool>())
02656 {
02657 triggerInductionExternalRequest_ = false;
02658 triggerInduction_ = true;
02659 }
02660
02661 }
02662
02663
02664 else if(timeElapsed_ > backtraceEndTime_ && ((triggerInduction_ || timeElapsed_ == triggerInductionTime_-1)
02665 || (strcmp(inductionMode_.c_str(), "givetransitions") == 0 && timeElapsed_ == triggerTimes_(triggerCount_)-1)))
02666 {
02667 mrs_real outTmp = out(0,0);
02668
02669
02670
02671
02672 inductionEnabler_(0, 0) = 0.0;
02673 updControl(ctrl_inductionEnabler_, inductionEnabler_);
02674
02675
02676
02677
02678
02679
02680 if(startTracking_)
02681 {
02682 if(resetAfterNewInduction_) bestScore_ = NA;
02683 else
02684 {
02685 calcAbsoluteBestScore();
02686 }
02687 }
02688
02689 updControl(ctrl_curBestScore_, bestScore_);
02690
02691 updControl(ctrl_triggerInduction_, true);
02692
02693 processInduction_ = true;
02694 triggerInduction_ = false;
02695
02696
02697 out.setval(outTmp);
02698
02699 if(startTracking_)
02700 triggerCount_++;
02701
02702 if(strcmp(inductionMode_.c_str(), "repeated") == 0 ||
02703 strcmp(inductionMode_.c_str(), "random") == 0 ||
02704 strcmp(inductionMode_.c_str(), "supervised") == 0)
02705 {
02706 if(strcmp(inductionMode_.c_str(), "repeated") == 0)
02707 triggerInductionTime_ += 100;
02708 else if(strcmp(inductionMode_.c_str(), "random") == 0)
02709 {
02710 mrs_natural minTriggerRep = maxPeriod_;
02711 mrs_natural maxTriggerRep = inductionTime_ * 3;
02712 srand((mrs_natural)(timeElapsed_+time(NULL)));
02713
02714 mrs_natural randTriggerRep = (mrs_natural) ((rand() % (maxTriggerRep-minTriggerRep)) + minTriggerRep);
02715
02716
02717
02718 triggerInductionTime_ += randTriggerRep;
02719 }
02720
02721
02722 if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02723 {
02724 ostringstream oss;
02725 oss << ctrl_destFileName_->to<mrs_string>() << "_logTrigger.txt";
02726 ofstream trigCountStream;
02727 trigCountStream.open(oss.str().c_str(), ios::out|ios::trunc);
02728 trigCountStream << triggerCount_ << endl;
02729 trigCountStream.close();
02730 }
02731
02732 }
02733 else triggerInductionTime_ = -1;
02734 }
02735
02736
02737 if(nonCausal_)
02738 {
02739 soundFileSize_ = ctrl_soundFileSize_->to<mrs_natural>();
02740
02741
02742
02743
02744 if(timeElapsed_ == soundFileSize_-1)
02745 {
02746 mrs_natural bestAgentHistorySize = (mrs_natural) beatCounter_(bestFinalAgent_);
02747 bestFinalAgentHistory_.create(bestAgentHistorySize);
02748
02749 if(logFile_)
02750 debugAddMsg("=========== NON-CAUSAL BEAT-TIMES ===========");
02751
02752 for(int i = 0; i < bestAgentHistorySize; i++)
02753 {
02754
02755 bestFinalAgentHistory_(i) = agentsHistory_(bestFinalAgent_, i);
02756
02757 if(logFile_)
02758 {
02759
02760 mrs_natural agent = (mrs_natural) agentsFamilyHist_(bestFinalAgent_, i);
02761 debugAddEvent("BEAT", agent, -1, (mrs_natural) bestFinalAgentHistory_(i), -1, -1);
02762 }
02763 }
02764
02765 if(logFile_)
02766 debugAddMsg("==========================");
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776 if(bestAgentHistorySize == 0)
02777 {
02778 bestFinalAgentHistory_.create(1);
02779 bestFinalAgentHistory_(0) = -1.0;
02780 }
02781
02782 updControl(ctrl_bestFinalAgentHistory_, bestFinalAgentHistory_);
02783 }
02784 }
02785
02786
02787
02788
02789
02790
02791 if(strcmp(inductionMode_.c_str(), "supervised") == 0)
02792 {
02793 mrs_natural smoothResolution = 3;
02794 smoothResolution = (mrs_natural) (smoothResolution * (srcFs_/hopSize_));
02795
02796 ofstream diffBS, meanBS, rawBS;
02797 if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02798 {
02799 ostringstream ossDiff, ossMean, ossBS;
02800 ossDiff << ctrl_destFileName_->to<mrs_string>() << "_diffBestScore.txt";
02801 ossMean << ctrl_destFileName_->to<mrs_string>() << "_meanBestScore.txt";
02802 ossBS << ctrl_destFileName_->to<mrs_string>() << "_bestScore.txt";
02803
02804 if(timeElapsed_ == 0)
02805 {
02806 diffBS.open(ossDiff.str().c_str(), ios::out|ios::trunc);
02807 meanBS.open(ossMean.str().c_str(), ios::out|ios::trunc);
02808 rawBS.open(ossBS.str().c_str(), ios::out|ios::trunc);
02809 }
02810 else
02811 {
02812 diffBS.open(ossDiff.str().c_str(), ios::out|ios::app);
02813 meanBS.open(ossMean.str().c_str(), ios::out|ios::app);
02814 rawBS.open(ossBS.str().c_str(), ios::out|ios::app);
02815 }
02816
02817 rawBS << bestScore_ << endl;
02818 rawBS.close();
02819 }
02820
02821 if(bestScore_ == NA)
02822 supervisedBestScores_.push_back(0.0);
02823 else supervisedBestScores_.push_back(bestScore_);
02824
02825
02826
02827
02828
02829 if(timeElapsed_ >= lastTriggerInductionTime_ + inductionTime_)
02830 {
02831 mrs_natural stepTime = timeElapsed_ - (lastTriggerInductionTime_ + inductionTime_);
02832 mrs_natural stepSize = 1;
02833 stepSize = (mrs_natural) (stepSize * (srcFs_/hopSize_));
02834 if(!(stepTime % stepSize))
02835 {
02836 mrs_real sum = 0.0;
02837 for(mrs_natural s = (mrs_natural) (supervisedBestScores_.size()-smoothResolution); s < (mrs_natural) supervisedBestScores_.size(); s++)
02838 sum += supervisedBestScores_.at(s);
02839 mrs_real mean = sum /= smoothResolution;
02840 supervisedBestScoresMeans_.push_back(mean);
02841
02842 mrs_real bestScoreMeanDiff = 0.1;
02843 if(supervisedBestScoresMeans_.size() > 1)
02844
02845
02846 bestScoreMeanDiff = supervisedBestScoresMeans_.at(supervisedBestScoresMeans_.size()-1)-supervisedBestScoresMeans_.at(supervisedBestScoresMeans_.size()-2);
02847
02848
02849
02850
02851
02852
02853
02854 if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02855 {
02856 diffBS << bestScoreMeanDiff << endl;
02857 diffBS.close();
02858 meanBS << supervisedBestScoresMeans_.at(supervisedBestScoresMeans_.size()-1) << endl;
02859 meanBS.close();
02860 }
02861
02862
02863 if((bestScoreMeanDiff < (supervisedTriggerThres_*supervisedBestScoresMeans_.at(supervisedBestScoresMeans_.size()-1)))
02864 && (lastBestScoreMeanDiff_ > (supervisedTriggerThres_*supervisedBestScoresMeans_.at(supervisedBestScoresMeans_.size()-2))))
02865 {
02866
02867
02868
02869 triggerInductionTime_ = timeElapsed_ + 3;
02870
02871
02872 if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02873 {
02874 ostringstream oss;
02875 oss << ctrl_destFileName_->to<mrs_string>() << "_triggerTimes.txt";
02876 ofstream triggerBS;
02877 if(lastTriggerInductionTime_ == inductionTime_)
02878 triggerBS.open(oss.str().c_str(), ios::out|ios::trunc);
02879 else triggerBS.open(oss.str().c_str(), ios::out|ios::app);
02880
02881 triggerBS << triggerInductionTime_ << endl;
02882 triggerBS.close();
02883 }
02884
02885
02886
02887 }
02888
02889 lastBestScoreMeanDiff_ = bestScoreMeanDiff;
02890 }
02891 else if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02892 {
02893 diffBS << NA << endl;
02894 diffBS.close();
02895 meanBS << NA << endl;
02896 meanBS.close();
02897 }
02898 }
02899 else if(strcmp(ctrl_logFile_->to<mrs_string>().c_str(), "trigger") == 0)
02900 {
02901 diffBS << NA << endl;
02902 diffBS.close();
02903 meanBS << NA << endl;
02904 meanBS.close();
02905 }
02906 }
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929 timeElapsed_++;
02930
02931 }