evoStream.cpp 18.9 KB
Newer Older
Matthias Carnein's avatar
Matthias Carnein committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
#include <Rcpp.h>
#include <limits>
#include <ctime>

#define VERBOSE 0

using namespace Rcpp;

inline int randWrapper(const int n) { return floor(unif_rand()*n); }


class MC {
public:
  Rcpp::NumericVector centroid;
  int lastUpdate;
  double weight;

  MC(Rcpp::NumericVector centroid, int lastUpdate, double weight) {
    this->centroid=centroid;
    this->lastUpdate=lastUpdate;
    this->weight=weight;
  }

  MC(Rcpp::NumericVector centroid, int lastUpdate) {
    this->centroid=centroid;
    this->lastUpdate=lastUpdate;
    this->weight=1;
  }

  Rcpp::NumericVector getCentroid(){
    return(centroid);
  }

  void merge(MC mc, int t, double lambda, double r) {
    mc.fade(t, lambda);
    this->fade(t, lambda);

    // update statistics
    this->weight += mc.weight;

    // competetive learning
    double d = this->distance(mc);
    this->centroid += exp(-pow(d/r*3.0, 2.0) /2.0) * (mc.getCentroid() - this->getCentroid());
  }


  void fade(int t, double lambda){
    // apply fading
    this->weight *= pow(2,(-lambda * (t-this->lastUpdate)));
    // update time
    this-> lastUpdate = t;
  }

  double distance(MC mc){
    return(sqrt(sum(pow(this->getCentroid() - mc.getCentroid(), 2))));
  }

  double distance(Rcpp::NumericVector x){
    return(sqrt(sum(pow(this->getCentroid() - x, 2))));
  }

};












class EvoStream {
public:

  double r;
  double lambda;
  int tgap;
  unsigned int k;
  double crossoverRate;
  double mutationRate;
  unsigned int populationSize;
  unsigned int initializeAfter;
  int incrementalGenerations;
  int reclusterGenerations;

  double omega;
  int t;
  int init;
  int upToDate;
  double delay;
  int initTime;
  std::vector<MC> micro;
  std::vector<Rcpp::NumericMatrix> macro;
  Rcpp::NumericVector macroFitness;


  // since exposed constructors have a limited number of parameters, we expose a setter function and use the default constructor
  void setFields(double r, double lambda, int tgap, unsigned int k, double crossoverRate, double mutationRate, int populationSize, unsigned int initializeAfter, int incrementalGenerations, int reclusterGenerations){
    this->r=r;
    this->lambda=lambda;
    this->tgap=tgap;
    this->k=k;
    this->crossoverRate=crossoverRate;
    this->mutationRate=mutationRate;
    this->populationSize=populationSize;
    this->initializeAfter=initializeAfter;
    this->incrementalGenerations=incrementalGenerations;
    this->reclusterGenerations = reclusterGenerations;

    this->macroFitness = Rcpp::NumericVector(this->populationSize);
    this->omega = pow(2, (-1*lambda * tgap));
    this->t=0;
    this->upToDate=0;
  }


  // constructor for stand-alone offline component
  void reclusterInitialize(Rcpp::NumericMatrix micro, Rcpp::NumericVector weights, unsigned int k, double crossoverRate, double mutationRate, int populationSize){
    this->k=k;
    this->crossoverRate=crossoverRate;
    this->mutationRate=mutationRate;
    this->populationSize=populationSize;
    this->macroFitness = Rcpp::NumericVector(this->populationSize);

    for(int i=0; i<micro.nrow(); i++){
      MC mc(micro(i,_), 1, weights(i));
      this->micro.push_back(mc);
    }

    this->initialize();
  }




  ////////////////////////// Interfacer


  Rcpp::NumericMatrix get_microclusters(){
    int d = this->ndimensions();
    Rcpp::NumericMatrix x(this->micro.size(),d);
    for(unsigned int i=0; i<this->micro.size();i++){
      Rcpp::NumericVector mc = this->micro[i].getCentroid();
      for(int j=0; j<mc.size();j++){
        x(i,j)=mc[j];
      }
    }
    return(x);
  }


  Rcpp::NumericVector get_microweights(){
    Rcpp::NumericVector x(this->micro.size());
    for(unsigned int i=0; i<this->micro.size();i++){
      x[i]=this->micro[i].weight;
    }
    return(x);
  }



  Rcpp::NumericMatrix get_macroclusters(){

    if(!this->init){
      return(Rcpp::NumericMatrix(0,this->ndimensions()));
    }

    if(this->reclusterGenerations!=0 && this->upToDate==0){
      this->recluster(reclusterGenerations);
      this->upToDate=1;
    }

    // find max fitness
    int maxIdx=-1;
    double max=-1*std::numeric_limits<double>::max();

    for(unsigned int i=0; i<this->macroFitness.size(); i++){
      if(this->macroFitness[i]>max){
        max=this->macroFitness[i];
        maxIdx = i;
      }
    }
    return(this->macro[maxIdx]);
  }



  Rcpp::NumericVector get_macroweights(){

    if(!this->init){
      return(Rcpp::NumericVector(0));
    }
    if(reclusterGenerations!=0 && this->upToDate==0){
      this->recluster(reclusterGenerations);
      this->upToDate=1;
    }

    Rcpp::IntegerVector clusterAssignment = this->microToMacro();
    Rcpp::NumericVector microWeights = this->get_microweights();
    Rcpp::NumericVector macroWeights(this->k);

    // for every cluster
    for(int i=0; i<clusterAssignment.size(); i++){
      macroWeights(clusterAssignment(i)) += microWeights[i];
    }

    return(macroWeights);
  }


  Rcpp::IntegerVector microToMacro(){

    if(!this->macro.size()){
      return(Rcpp::IntegerVector(0));
    }

    Rcpp::IntegerVector assignment = this->getAssignment(this->get_macroclusters());
    return(assignment);

  }







  ////////////////////////////// Online component



  void cluster(Rcpp::NumericMatrix data){

    this->upToDate=0;

    for(int i=0; i<data.nrow(); i++){

#if VERBOSE >= 1
      std::cout << "--------------------------" << std::endl;
#endif

      // increment time
      this->t++;

      // create temporary mc
      MC mc(data(i,_), this->t);

      // get distance to all other mcs
      Rcpp::NumericVector distances = this->getDistanceVector(mc, this->micro);

      // insert into closest or init new mc
      this->insert(distances, mc);

      // cleanup every tgap
      if(this->t % this->tgap == 0){
        this->cleanup();
      }


      // Incremental Reclustering part
      if(this->init){

#if VERBOSE >= 2
        std::cout << "Perform evolutionary steps" << std::endl;
#endif

        // fixed number of generations
        for(int i=0; i<incrementalGenerations; i++){
          this->evolution();
          Rcpp::checkUserInterrupt();
        }

      } else if(this->micro.size()==this->initializeAfter){
        this->initialize();
      }

#if VERBOSE >= 3
      std::cout << "Current fitness:" << std::endl;
      Rf_PrintValue(Rcpp::wrap(this->macroFitness));
#endif

    }

  }




  void cleanup(){

#if VERBOSE >= 2
    std::cout << "Cleanup" << std::endl;
#endif
    this->updateWeights();

    // merge close mcs
    for(int i = this->micro.size()-1; i >=0; i--){
      for(int j = i-1; j>=0; j--){
        double d = this->micro[i].distance(this->micro[j]); // calc distance
        if(d <= r){
#if VERBOSE >= 2
          std::cout << "Merging cluster" << i << " into " << j << std::endl;
#endif
          this->micro[j].merge(this->micro[i], this->t, this->lambda, this->r); //merge mc into the other
          this->removeMicroCluster(i); // remove the old mc
          break;
        }
      }
    }
  }

  void updateWeights(){

    // fade Clusters
    for(int i=micro.size()-1; i>=0; i--){
      this->micro[i].fade(this->t, this->lambda);
      // remove insufficient weight
      if( this->micro[i].weight <= this->omega){
        this->removeMicroCluster(i);
      }
    }
  }



  void removeMicroCluster(int i){
#if VERBOSE >= 2
    std::cout << "Remove Cluster " << i << std::endl;
#endif
    this->micro.erase(this->micro.begin()+i);
  }



  void insert(Rcpp::NumericVector distances, MC mc){

    // merge if within radius
    bool merged = 0;
    for(unsigned int i=0; i < micro.size(); i++){
      if(distances[i] <= r){
#if VERBOSE >= 2
        std::cout << "Merge observation " << t << " into Micro Cluster " << i  << std::endl;
#endif
        this->micro[i].merge(mc, this->t, this->lambda, r);
        merged=1;
      }
    }

    // if none close enough create new
    if(!merged){
#if VERBOSE >= 2
      std::cout << "Use observation " << t <<" to create new Micro Cluster" << std::endl;
#endif
      this->micro.push_back(mc);
    }
  }

















  //////////////////////////////////// Offline component




  void evolution(){

    this->calculateFitness();

    // perform evolutionary step
    std::vector<Rcpp::NumericMatrix> selected = this->selection(); // select parents
    std::vector<Rcpp::NumericMatrix> offsprings = this->recombination(selected); // recombine parents
    std::vector<Rcpp::NumericMatrix> mutants = this->mutation(offsprings); // mutate offspring




    for(unsigned int i=0; i<mutants.size(); i++){
      double fit = this->fitness(mutants[i]); // evaluate new solution

#if VERBOSE >= 1
      std::cout << "Fitness of child: " << fit << std::endl;
#endif

      // replace weakest solution so far
      int minIdx = 0;
      double min = std::numeric_limits<double>::max();
      for(unsigned int j=0; j<this->macroFitness.size(); j++){
        if(this->macroFitness[j] < min){
          min = this->macroFitness[j];
          minIdx = j;
        }
      }

      if(min < fit){
#if VERBOSE >= 1
        std::cout << "Use new solution to replace solution " << minIdx << " with fitness " << this->macroFitness[minIdx] << std::endl;
#endif

        this->macro[minIdx] = mutants[i];
        this->macroFitness[minIdx] = fit;
      }
    }


#if VERBOSE >= 1
    std::cout << "Highest Fitness is  " <<  this->getMaxFitness(); << std::endl;
#endif

  }



  void recluster(int generations){
    // fixed number of generations
    for(int i=0; i<generations; i++){
#if VERBOSE >= 1
      std::cout << "Generation " << i << std::endl;
#endif
      this->evolution();
      Rcpp::checkUserInterrupt();
    }
  }




  void calculateFitness(){

#if VERBOSE >= 3
    std::cout << "Calcualte fitness:" << std::endl;
#endif
    // for every solution
    for(unsigned int i=0; i<this->macro.size(); i++){
      this->macroFitness[i] = this->fitness(this->macro[i]);
    }
#if VERBOSE >= 3
    Rf_PrintValue(Rcpp::wrap(this->macroFitness));
#endif
  }



  double fitness(Rcpp::NumericMatrix centres){
    double result=0.0;

    Rcpp::IntegerVector assignment = this->getAssignment(centres);

    // calc distance to centers
    for(int i=0; i<assignment.size(); i++){
467
      result += pow(this->micro[i].distance(centres(assignment[i],_)),2) * this->micro[i].weight;
Matthias Carnein's avatar
Matthias Carnein committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    }

    return(1/result);
  }



  void initialize(){
#if VERBOSE >= 2
    std::cout << "Initialize Solutions" << std::endl;
#endif
    this->initTime = this->t;

    // for entire population
    for(unsigned int i=0; i<this->populationSize; i++){
      //init
      macro.push_back(Rcpp::NumericMatrix(this->k, this->ndimensions()));

      // choose k centers
      Rcpp::IntegerVector x = randomShuffle(Rcpp::seq(0, this->micro.size()-1)); // Todo: this is potentially large, depending on when called
      for(unsigned int j=0; j<this->k; j++){
        int l = j % this->micro.size();
        macro[i](j,_) = this->micro[x[l]].getCentroid();
      }
    }

    this->init=1;
  }


  std::vector<Rcpp::NumericMatrix> selection(){

    // declare return value before rngscope http://gallery.rcpp.org/articles/random-number-generation/
    std::vector<Rcpp::NumericMatrix> individuals;
    individuals.reserve(2);

    RNGScope rngScope;

    // Select proportionally to fitness
    double sum=0.0;
    Rcpp::NumericVector probability(this->macroFitness.size());
    for(unsigned int i=0; i<this->macroFitness.size(); i++){
      sum += this->macroFitness[i];
      probability[i] = this->macroFitness[i];
    }


    // sample two parents
    Rcpp::IntegerVector selected(2);
    for(int i=0; i<2; i++){
      double rand = R::runif(0,1);

      for(int j=0; j<probability.size(); j++){

        double val = probability(j)/sum;

        if(val > rand){
          selected(i)=j;
          sum -= probability(j);
          probability.erase(j);
          break;
        }
        rand -= val;
      }
    }

    // since we deleted one element
    if(selected(1) >= selected(0)){
      selected(1)++;
    }

#if VERBOSE >= 1
    std::cout << "Select " << selected(0) << " and " << selected(1) << " with fitness " << this->macroFitness(selected(0)) << " and " << this->macroFitness(selected(1)) << " as parents" << std::endl;
#endif

    individuals.push_back(clone(this->macro[selected(0)]));
    individuals.push_back(clone(this->macro[selected(1)]));

#if VERBOSE >= 3
    std::cout << "Parent1:" << individuals[0] << std::endl;
    std::cout << "Parent2:" << individuals[1] << std::endl;
#endif

    return(individuals);
  }


  // wrapper for easier change of recombination algorithm
  std::vector<Rcpp::NumericMatrix> recombination(std::vector<Rcpp::NumericMatrix> individuals){
    return(recombinationGAclust(individuals));
  }

  // wrapper for easier change of mutation algorithm
  std::vector<Rcpp::NumericMatrix> mutation(std::vector<NumericMatrix> individuals){
    return(mutationGAclust(individuals));
  }


  // GA Clustering recombination approach
  std::vector<Rcpp::NumericMatrix> recombinationGAclust(std::vector<Rcpp::NumericMatrix> individuals){

    RNGScope rngScope;

    if(R::runif(0,1) < this->crossoverRate){
      int size =individuals[0].nrow()*individuals[0].ncol();
      int crossoverPoint = R::runif(0,1)*(size-1); // range 1 - length-1
#if VERBOSE >= 2
      std::cout << "CrossoverPoint: " << crossoverPoint << std::endl;
#endif

      for(int pos=0; pos < size; pos++){
        if(pos>crossoverPoint){
          double temp = individuals[0][pos];
          individuals[0][pos] = individuals[1][pos];
          individuals[1][pos] = temp;
        }
      }
    }

#if VERBOSE >= 3
    std::cout << "Offsprpring1:" << individuals[0] << std::endl;
    std::cout << "Offsprpring2:" << individuals[1] << std::endl;
#endif

    return(individuals);
  }


  // PESAII reclustering approach
  std::vector<Rcpp::NumericMatrix> recombinationPESAII(std::vector<Rcpp::NumericMatrix> individuals){

    RNGScope rngScope;

    for(unsigned int i=0; i<individuals.size(); i++){
      int size =individuals[0].nrow()*individuals[0].ncol();
      for(int pos=0; pos < size; pos++){
        if(R::runif(0,1) < this->crossoverRate){
          double temp = individuals[0][pos];
          individuals[0][pos] = individuals[1][pos];
          individuals[1][pos] = temp;
        }
      }
    }


#if VERBOSE >= 3
    std::cout << "Offsprpring1:" << individuals[0] << std::endl;
    std::cout << "Offsprpring2:" << individuals[1] << std::endl;
#endif

    return(individuals);
  }



  // GA Clustering mutation approach
  std::vector<Rcpp::NumericMatrix> mutationGAclust(std::vector<NumericMatrix> individuals){

    RNGScope rngScope;

    // for both individuals
    for(unsigned int i=0; i<individuals.size(); i++){
      int size=individuals[i].nrow() * individuals[i].ncol();
      for(int pos=0; pos < size; pos++){
        if(R::runif(0,1) < this->mutationRate){
          double val=0.0;

          if(individuals[i][pos]!=0){
            val = 2 * R::runif(0,1) * individuals[i][pos];
          } else{
            val = 2 * R::runif(0,1);
          }

          // + or - with equal change
          if(R::runif(0,1)>0.5){
            individuals[i][pos] += val;
          } else{
            individuals[i][pos] -= val;
          }
        }
      }
    }

#if VERBOSE >= 3
    std::cout << "Mutant1:" << individuals[0] << std::endl;
    std::cout << "Mutant2:" << individuals[1] << std::endl;
#endif

    return(individuals);
  }

  // PESAII mutation approach
  std::vector<Rcpp::NumericMatrix> mutationPESAII(std::vector<NumericMatrix> individuals){

    RNGScope rngScope;

    // for both individuals
    for(unsigned int i=0; i<individuals.size(); i++){
      int size=individuals[i].nrow() * individuals[i].ncol();
      for(int pos=0; pos < size; pos++){
        if(R::runif(0,1) < this->mutationRate){
          individuals[i][pos] += R::rnorm(0,.3);
        }
      }
    }

#if VERBOSE >= 3
    std::cout << "Mutant1:" << individuals[0] << std::endl;
    std::cout << "Mutant2:" << individuals[1] << std::endl;
#endif

    return(individuals);
  }






  ///////////////////////// Helper

  Rcpp::IntegerVector getAssignment(Rcpp::NumericMatrix centres){
    Rcpp::IntegerVector assignment(this->micro.size());
    // for every cluster
    for(unsigned int i=0; i<this->micro.size(); i++){
      double min=std::numeric_limits<double>::max();
      // find closest centre
      for(int j=0; j<centres.nrow(); j++){
        double dist = this->micro[i].distance(centres(j,_));
        if(dist < min){
          min=dist;
          assignment(i)=j;
        }
      }
    }
    return(assignment);
  }



  double getMaxFitness(){
    // find max fitness
    if(!this->init){
      return(0);
    }

    double max=-1*std::numeric_limits<double>::max();
    for(unsigned int i=0; i<this->macroFitness.size(); i++){
      if(this->macroFitness[i]>max){
        max=this->macroFitness[i];
      }
    }
    return(max);
  }



  int ndimensions(){
    if(!micro.size()){
      return(0);
    } else{
      return(this->micro[0].getCentroid().size());
    }
  }



  int sampleProportionally(Rcpp::NumericVector data){

    // declare return value before rngScope http://gallery.rcpp.org/articles/random-number-generation/
    int j=0;
    RNGScope rngScope;

    // sample proportionally to distance
    double cumsum=0.0;
    for(j=0; j<data.size(); j++){
      cumsum += data(j);
      if(cumsum >= R::runif(0,1)){
        return(j);
      }
    }
    return(-1);
  }



  Rcpp::NumericVector getDistanceVector(MC mc, std::vector<MC> cluster){

    Rcpp::NumericVector distances(cluster.size());
    for(unsigned int i=0; i < cluster.size(); i++){
      distances(i) = mc.distance(cluster[i]);
    }
    return(distances);
  }


  // http://gallery.rcpp.org/articles/stl-random-shuffle/
  Rcpp::IntegerVector randomShuffle(Rcpp::IntegerVector a) {
    // clone a into b to leave a alone
    Rcpp::IntegerVector b = Rcpp::clone(a);

    // declare return value before rngscope http://gallery.rcpp.org/articles/random-number-generation/
    RNGScope scope;

    std::random_shuffle(b.begin(), b.end(), randWrapper);

    return(b);
  }


};








// Allows to return class objects to R
RCPP_EXPOSED_CLASS(EvoStream)
  RCPP_EXPOSED_CLASS(MC)

  // Expose class members and methods to R
  RCPP_MODULE(MOD_evoStream){
    using namespace Rcpp;


    class_<EvoStream>("EvoStream")
      .constructor()

    .method("setFields", &EvoStream::setFields)
    .method("get_microclusters", &EvoStream::get_microclusters)
    .method("get_microweights", &EvoStream::get_microweights)
    .method("get_macroclusters", &EvoStream::get_macroclusters)
    .method("get_macroweights", &EvoStream::get_macroweights)
    .method("microToMacro", &EvoStream::microToMacro)
    .method("cluster", &EvoStream::cluster)
    .method("recluster", &EvoStream::recluster)
    .method("reclusterInitialize", &EvoStream::reclusterInitialize)
    ;

    class_<MC>("MC")
      .constructor<Rcpp::NumericVector, int, double>()
      .constructor<Rcpp::NumericVector, int>()
      .field("centroid", &MC::centroid)
      .field("lastUpdate", &MC::lastUpdate)
      .field("weight", &MC::weight)

    .method("merge", &MC::merge)
    .method("fade", &MC::fade)
    ;


  }