Program: 54681
Project: 54681
Buggy source:
/** 
 * Implementation of Collapsed Gibbs Sampling algorithm.
 * @param dataset The list of points that we want to cluster
 * @param maxIterations The maximum number of iterations
 */
private int collapsedGibbsSampling(Dataframe dataset){
  AbstractModelParameters modelParameters=kb().getModelParameters();
  Map<Integer,CL> tempClusterMap=kb().getDbc().getBigMap("String_Node_Str",MapType.HASHMAP,StorageHint.IN_CACHE,false,true);
  tempClusterMap.putAll(modelParameters.getClusterMap());
  AbstractTrainingParameters trainingParameters=kb().getTrainingParameters();
  double alpha=trainingParameters.getAlpha();
  Integer newClusterId=tempClusterMap.size();
  if (trainingParameters.getInitializationMethod() == AbstractTrainingParameters.Initialization.ONE_CLUSTER_PER_RECORD) {
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      CL cluster=createNewCluster(newClusterId);
      cluster.add(r);
      tempClusterMap.put(newClusterId,cluster);
      r=new Record(r.getX(),r.getY(),newClusterId,r.getYPredictedProbabilities());
      dataset._unsafe_set(rId,r);
      ++newClusterId;
    }
  }
 else {
    int numberOfNewClusters=(int)(Math.max(alpha,1) * Math.log(dataset.size()));
    if (numberOfNewClusters <= 0) {
      numberOfNewClusters=1;
    }
    for (int i=0; i < numberOfNewClusters; ++i) {
      CL cluster=createNewCluster(newClusterId);
      tempClusterMap.put(newClusterId,cluster);
      ++newClusterId;
    }
    int clusterMapSize=newClusterId;
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      Integer assignedClusterId=PHPMethods.mt_rand(0,clusterMapSize - 1);
      r=new Record(r.getX(),r.getY(),assignedClusterId,r.getYPredictedProbabilities());
      dataset._unsafe_set(rId,r);
      CL c=getFromClusterMap(assignedClusterId,tempClusterMap);
      c.add(r);
      tempClusterMap.put(assignedClusterId,c);
    }
  }
  int n=tempClusterMap.size();
  int maxIterations=trainingParameters.getMaxIterations();
  boolean noChangeMade=false;
  int iteration=0;
  while (iteration < maxIterations && noChangeMade == false) {
    logger.debug("String_Node_Str",iteration);
    noChangeMade=true;
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      Integer pointClusterId=(Integer)r.getYPredicted();
      CL ci=getFromClusterMap(pointClusterId,tempClusterMap);
      ci.remove(r);
      if (ci.size() == 0) {
        tempClusterMap.remove(pointClusterId);
      }
 else {
        tempClusterMap.put(pointClusterId,ci);
      }
      AssociativeArray condProbCiGivenXiAndOtherCi=clusterProbabilities(r,n,tempClusterMap);
      CL cNew=createNewCluster(newClusterId);
      double priorLogPredictive=cNew.posteriorLogPdf(r);
      double probNewCluster=alpha / (alpha + n - 1.0);
      condProbCiGivenXiAndOtherCi.put(newClusterId,priorLogPredictive + Math.log(probNewCluster));
      Descriptives.normalizeExp(condProbCiGivenXiAndOtherCi);
      Integer sampledClusterId=(Integer)SimpleRandomSampling.weightedSampling(condProbCiGivenXiAndOtherCi,1,true).iterator().next();
      if (Objects.equals(sampledClusterId,newClusterId)) {
        r=new Record(r.getX(),r.getY(),newClusterId,r.getYPredictedProbabilities());
        dataset._unsafe_set(rId,r);
        cNew.add(r);
        tempClusterMap.put(newClusterId,cNew);
        noChangeMade=false;
        ++newClusterId;
      }
 else {
        if (!Objects.equals(pointClusterId,sampledClusterId)) {
          r=new Record(r.getX(),r.getY(),sampledClusterId,r.getYPredictedProbabilities());
          dataset._unsafe_set(rId,r);
          noChangeMade=false;
        }
        CL c=getFromClusterMap(sampledClusterId,tempClusterMap);
        c.add(r);
        tempClusterMap.put(sampledClusterId,c);
      }
    }
    ++iteration;
  }
  Map<Integer,CL> clusterMap=modelParameters.getClusterMap();
  newClusterId=clusterMap.size();
  for (  CL cluster : tempClusterMap.values()) {
    clusterMap.put(newClusterId,(CL)cluster.copy2new(newClusterId));
    newClusterId++;
  }
  kb().getDbc().dropBigMap("String_Node_Str",tempClusterMap);
  return iteration;
}

Fixed version (ground truth):
/** 
 * Implementation of Collapsed Gibbs Sampling algorithm.
 * @param dataset The list of points that we want to cluster
 * @param maxIterations The maximum number of iterations
 */
private int collapsedGibbsSampling(Dataframe dataset){
  AbstractModelParameters modelParameters=kb().getModelParameters();
  Map<Integer,CL> tempClusterMap=kb().getDbc().getBigMap("String_Node_Str",MapType.HASHMAP,StorageHint.IN_CACHE,false,true);
  tempClusterMap.putAll(modelParameters.getClusterMap());
  AbstractTrainingParameters trainingParameters=kb().getTrainingParameters();
  double alpha=trainingParameters.getAlpha();
  Integer newClusterId=tempClusterMap.size();
  if (trainingParameters.getInitializationMethod() == AbstractTrainingParameters.Initialization.ONE_CLUSTER_PER_RECORD) {
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      CL cluster=createNewCluster(newClusterId);
      cluster.add(r);
      tempClusterMap.put(newClusterId,cluster);
      r=new Record(r.getX(),r.getY(),newClusterId,r.getYPredictedProbabilities());
      dataset._unsafe_set(rId,r);
      ++newClusterId;
    }
  }
 else {
    int numberOfNewClusters=(int)(Math.max(alpha,1) * Math.log(dataset.size()));
    if (numberOfNewClusters <= 0) {
      numberOfNewClusters=1;
    }
    for (int i=0; i < numberOfNewClusters; ++i) {
      CL cluster=createNewCluster(newClusterId);
      tempClusterMap.put(newClusterId,cluster);
      ++newClusterId;
    }
    int clusterMapSize=newClusterId;
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      Integer assignedClusterId=PHPMethods.mt_rand(0,clusterMapSize - 1);
      r=new Record(r.getX(),r.getY(),assignedClusterId,r.getYPredictedProbabilities());
      dataset._unsafe_set(rId,r);
      CL c=getFromClusterMap(assignedClusterId,tempClusterMap);
      c.add(r);
      tempClusterMap.put(assignedClusterId,c);
    }
  }
  int n=tempClusterMap.size();
  int maxIterations=trainingParameters.getMaxIterations();
  boolean noChangeMade=false;
  int iteration=0;
  while (iteration < maxIterations && noChangeMade == false) {
    logger.debug("String_Node_Str",iteration);
    noChangeMade=true;
    for (    Map.Entry<Integer,Record> e : dataset.entries()) {
      Integer rId=e.getKey();
      Record r=e.getValue();
      Integer pointClusterId=(Integer)r.getYPredicted();
      CL ci=getFromClusterMap(pointClusterId,tempClusterMap);
      ci.remove(r);
      if (ci.size() == 0) {
        tempClusterMap.remove(pointClusterId);
      }
 else {
        tempClusterMap.put(pointClusterId,ci);
      }
      AssociativeArray condProbCiGivenXiAndOtherCi=clusterProbabilities(r,n,tempClusterMap);
      CL cNew=createNewCluster(newClusterId);
      double priorLogPredictive=cNew.posteriorLogPdf(r);
      double probNewCluster=alpha / (alpha + n - 1.0);
      condProbCiGivenXiAndOtherCi.put(newClusterId,priorLogPredictive + Math.log(probNewCluster));
      Descriptives.normalizeExp(condProbCiGivenXiAndOtherCi);
      Integer sampledClusterId=(Integer)SimpleRandomSampling.weightedSampling(condProbCiGivenXiAndOtherCi,1,true).iterator().next();
      if (Objects.equals(sampledClusterId,newClusterId)) {
        r=new Record(r.getX(),r.getY(),newClusterId,r.getYPredictedProbabilities());
        dataset._unsafe_set(rId,r);
        cNew.add(r);
        tempClusterMap.put(newClusterId,cNew);
        noChangeMade=false;
        ++newClusterId;
      }
 else {
        if (!Objects.equals(pointClusterId,sampledClusterId)) {
          r=new Record(r.getX(),r.getY(),sampledClusterId,r.getYPredictedProbabilities());
          dataset._unsafe_set(rId,r);
          noChangeMade=false;
        }
        CL c=getFromClusterMap(sampledClusterId,tempClusterMap);
        c.add(r);
        tempClusterMap.put(sampledClusterId,c);
      }
    }
    ++iteration;
  }
  Map<Integer,CL> clusterMap=modelParameters.getClusterMap();
  int cid=clusterMap.size();
  for (  CL cluster : tempClusterMap.values()) {
    clusterMap.put(cid,(CL)cluster.copy2new(cid));
    cid++;
  }
  kb().getDbc().dropBigMap("String_Node_Str",tempClusterMap);
  return iteration;
}