Program: 58754
Project: 58754
Buggy source:
/** 
 * Java-implementation of the affinity propagation clustering algorithm. See BJ Frey and D Dueck, Science 315, 972-976, Feb 16, 2007, for a description of the algorithm. Copyright 2007, BJ Frey and Delbert Dueck. This software may be freely used and distributed for non-commercial purposes.
 * @param set
 * @return Integer
 */
public Integer affinityPropagation(ISet set,EClustererType eClustererType){
  ArrayList<Integer> AlIndexes=new ArrayList<Integer>();
  ArrayList<Integer> alExamples=new ArrayList<Integer>();
  ArrayList<Integer> count=new ArrayList<Integer>();
  int iNrIterations=0, decit=iConvIterations;
  boolean bIterate=true;
  boolean bConverged=false;
  float[] mx1=new float[iNrSamples];
  float[] mx2=new float[iNrSamples];
  float[] srp=new float[iNrSamples];
  float[] decsum=new float[iNrSamples];
  int[] idx=new int[iNrSamples];
  int[][] dec=new int[iConvIterations][iNrSamples];
  float tmp=0;
  int j=0;
  float[] dArResposibilities=new float[iNrSimilarities];
  float[] dArAvailabilities=new float[iNrSimilarities];
  int iPercentage=1;
  while (bIterate) {
    iNrIterations++;
    int tempPercentage=(int)((float)iNrIterations / iMaxIterations * 100);
    if (iPercentage == tempPercentage) {
      GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(iPercentage,false));
      iPercentage++;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
      mx2[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx2[i[j]]=mx1[i[j]];
        mx1[i[j]]=tmp;
      }
 else       if (tmp > mx2[i[j]]) {
        mx2[i[j]]=tmp;
      }
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp == mx1[i[j]]) {
        dArResposibilities[j]=dLambda * dArResposibilities[j] + (1 - dLambda) * (s[j] - mx2[i[j]]);
      }
 else {
        dArResposibilities[j]=dLambda * dArResposibilities[j] + (1 - dLambda) * (s[j] - mx1[i[j]]);
      }
    }
    for (j=0; j < iNrSimilarities - iNrSamples; j++)     if (dArResposibilities[j] > 0.0) {
      srp[k[j]]=srp[k[j]] + dArResposibilities[j];
    }
    for (j=iNrSimilarities - iNrSamples; j < iNrSimilarities; j++) {
      srp[k[j]]=srp[k[j]] + dArResposibilities[j];
    }
    for (j=0; j < iNrSimilarities - iNrSamples; j++) {
      if (dArResposibilities[j] > 0.0) {
        tmp=srp[k[j]] - dArResposibilities[j];
      }
 else {
        tmp=srp[k[j]];
      }
      if (tmp < 0.0) {
        dArAvailabilities[j]=dLambda * dArAvailabilities[j] + (1 - dLambda) * tmp;
      }
 else {
        dArAvailabilities[j]=dLambda * dArAvailabilities[j];
      }
    }
    for (j=iNrSimilarities - iNrSamples; j < iNrSimilarities; j++) {
      dArAvailabilities[j]=dLambda * dArAvailabilities[j] + (1 - dLambda) * (srp[k[j]] - dArResposibilities[j]);
    }
    decit++;
    if (decit >= iConvIterations) {
      decit=0;
    }
    for (j=0; j < iNrSamples; j++) {
      decsum[j]=decsum[j] - dec[decit][j];
    }
    for (j=0; j < iNrSamples; j++)     if (dArAvailabilities[iNrSimilarities - iNrSamples + j] + dArResposibilities[iNrSimilarities - iNrSamples + j] > 0.0) {
      dec[decit][j]=1;
    }
 else {
      dec[decit][j]=0;
    }
    iNrClusters=0;
    for (j=0; j < iNrSamples; j++) {
      iNrClusters=iNrClusters + dec[decit][j];
    }
    for (j=0; j < iNrSamples; j++) {
      decsum[j]=decsum[j] + dec[decit][j];
    }
    if ((iNrIterations >= iConvIterations) || (iNrIterations >= iMaxIterations)) {
      bConverged=true;
      for (j=0; j < iNrSamples; j++)       if ((decsum[j] != 0) && (decsum[j] != iConvIterations)) {
        bConverged=false;
      }
      if ((bConverged && (iNrClusters > 0)) || (iNrIterations == iMaxIterations)) {
        bIterate=false;
      }
    }
    processEvents();
    if (bClusteringCanceled) {
      GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
      return -1;
    }
  }
  if (iNrClusters > 0) {
    for (j=0; j < iNrSimilarities; j++)     if (dec[decit][k[j]] == 1) {
      dArAvailabilities[j]=0.0f;
    }
 else {
      dArAvailabilities[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx1[i[j]]=tmp;
        idx[i[j]]=k[j];
      }
    }
    for (j=0; j < iNrSamples; j++)     if (dec[decit][j] == 1) {
      idx[j]=j;
    }
    for (j=0; j < iNrSamples; j++) {
      srp[j]=0.0f;
    }
    for (j=0; j < iNrSimilarities; j++)     if (idx[i[j]] == idx[k[j]]) {
      srp[k[j]]=srp[k[j]] + s[j];
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++)     if (srp[j] > mx1[idx[j]]) {
      mx1[idx[j]]=srp[j];
    }
    for (j=0; j < iNrSamples; j++)     if (srp[j] == mx1[idx[j]]) {
      dec[decit][j]=1;
    }
 else {
      dec[decit][j]=0;
    }
    for (j=0; j < iNrSimilarities; j++)     if (dec[decit][k[j]] == 1) {
      dArAvailabilities[j]=0.0f;
    }
 else {
      dArAvailabilities[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx1[i[j]]=tmp;
        idx[i[j]]=k[j];
      }
    }
    for (j=0; j < iNrSamples; j++)     if (dec[decit][j] == 1) {
      idx[j]=j;
      alExamples.add(j);
    }
  }
 else {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
    return -1;
  }
  if (bConverged == false) {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
    return -1;
  }
  ArrayList<Integer> idxExamples=new ArrayList<Integer>();
  for (int i=0; i < alExamples.size(); i++) {
    count.add(0);
  }
  ClusterHelper.sortClusters(set,iVAIdContent,iVAIdStorage,alExamples,eClustererType);
  int counter=0;
  int idxCnt=0;
  for (  Integer example : alExamples) {
    for (int sampleNr=0; sampleNr < iNrSamples; sampleNr++) {
      if (idx[sampleNr] == example) {
        AlIndexes.add(sampleNr);
        count.set(counter,count.get(counter) + 1);
      }
      if (example == sampleNr) {
        idxExamples.add(idxCnt);
        idxCnt=0;
      }
      idxCnt++;
    }
    counter++;
  }
  Integer clusteredVAId=set.createStorageVA(AlIndexes);
  set.setAlClusterSizes(count);
  set.setAlExamples(idxExamples);
  GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
  return clusteredVAId;
}

Fixed version (ground truth):
/** 
 * Java-implementation of the affinity propagation clustering algorithm. See BJ Frey and D Dueck, Science 315, 972-976, Feb 16, 2007, for a description of the algorithm. Copyright 2007, BJ Frey and Delbert Dueck. This software may be freely used and distributed for non-commercial purposes.
 * @param set
 * @return Integer
 */
public Integer affinityPropagation(EClustererType eClustererType){
  ArrayList<Integer> AlIndexes=new ArrayList<Integer>();
  ArrayList<Integer> alExamples=new ArrayList<Integer>();
  ArrayList<Integer> count=new ArrayList<Integer>();
  int iNrIterations=0, decit=iConvIterations;
  boolean bIterate=true;
  boolean bConverged=false;
  float[] mx1=new float[iNrSamples];
  float[] mx2=new float[iNrSamples];
  float[] srp=new float[iNrSamples];
  float[] decsum=new float[iNrSamples];
  int[] idx=new int[iNrSamples];
  int[][] dec=new int[iConvIterations][iNrSamples];
  float tmp=0;
  int j=0;
  float[] dArResposibilities=new float[iNrSimilarities];
  float[] dArAvailabilities=new float[iNrSimilarities];
  int iPercentage=1;
  while (bIterate) {
    iNrIterations++;
    int tempPercentage=(int)((float)iNrIterations / iMaxIterations * 100);
    if (iPercentage == tempPercentage) {
      GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(iPercentage,false));
      iPercentage++;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
      mx2[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx2[i[j]]=mx1[i[j]];
        mx1[i[j]]=tmp;
      }
 else       if (tmp > mx2[i[j]]) {
        mx2[i[j]]=tmp;
      }
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp == mx1[i[j]]) {
        dArResposibilities[j]=dLambda * dArResposibilities[j] + (1 - dLambda) * (s[j] - mx2[i[j]]);
      }
 else {
        dArResposibilities[j]=dLambda * dArResposibilities[j] + (1 - dLambda) * (s[j] - mx1[i[j]]);
      }
    }
    for (j=0; j < iNrSimilarities - iNrSamples; j++)     if (dArResposibilities[j] > 0.0) {
      srp[k[j]]=srp[k[j]] + dArResposibilities[j];
    }
    for (j=iNrSimilarities - iNrSamples; j < iNrSimilarities; j++) {
      srp[k[j]]=srp[k[j]] + dArResposibilities[j];
    }
    for (j=0; j < iNrSimilarities - iNrSamples; j++) {
      if (dArResposibilities[j] > 0.0) {
        tmp=srp[k[j]] - dArResposibilities[j];
      }
 else {
        tmp=srp[k[j]];
      }
      if (tmp < 0.0) {
        dArAvailabilities[j]=dLambda * dArAvailabilities[j] + (1 - dLambda) * tmp;
      }
 else {
        dArAvailabilities[j]=dLambda * dArAvailabilities[j];
      }
    }
    for (j=iNrSimilarities - iNrSamples; j < iNrSimilarities; j++) {
      dArAvailabilities[j]=dLambda * dArAvailabilities[j] + (1 - dLambda) * (srp[k[j]] - dArResposibilities[j]);
    }
    decit++;
    if (decit >= iConvIterations) {
      decit=0;
    }
    for (j=0; j < iNrSamples; j++) {
      decsum[j]=decsum[j] - dec[decit][j];
    }
    for (j=0; j < iNrSamples; j++)     if (dArAvailabilities[iNrSimilarities - iNrSamples + j] + dArResposibilities[iNrSimilarities - iNrSamples + j] > 0.0) {
      dec[decit][j]=1;
    }
 else {
      dec[decit][j]=0;
    }
    iNrClusters=0;
    for (j=0; j < iNrSamples; j++) {
      iNrClusters=iNrClusters + dec[decit][j];
    }
    for (j=0; j < iNrSamples; j++) {
      decsum[j]=decsum[j] + dec[decit][j];
    }
    if ((iNrIterations >= iConvIterations) || (iNrIterations >= iMaxIterations)) {
      bConverged=true;
      for (j=0; j < iNrSamples; j++)       if ((decsum[j] != 0) && (decsum[j] != iConvIterations)) {
        bConverged=false;
      }
      if ((bConverged && (iNrClusters > 0)) || (iNrIterations == iMaxIterations)) {
        bIterate=false;
      }
    }
    processEvents();
    if (bClusteringCanceled) {
      GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
      return -1;
    }
  }
  if (iNrClusters > 0) {
    for (j=0; j < iNrSimilarities; j++)     if (dec[decit][k[j]] == 1) {
      dArAvailabilities[j]=0.0f;
    }
 else {
      dArAvailabilities[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx1[i[j]]=tmp;
        idx[i[j]]=k[j];
      }
    }
    for (j=0; j < iNrSamples; j++)     if (dec[decit][j] == 1) {
      idx[j]=j;
    }
    for (j=0; j < iNrSamples; j++) {
      srp[j]=0.0f;
    }
    for (j=0; j < iNrSimilarities; j++)     if (idx[i[j]] == idx[k[j]]) {
      srp[k[j]]=srp[k[j]] + s[j];
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++)     if (srp[j] > mx1[idx[j]]) {
      mx1[idx[j]]=srp[j];
    }
    for (j=0; j < iNrSamples; j++)     if (srp[j] == mx1[idx[j]]) {
      dec[decit][j]=1;
    }
 else {
      dec[decit][j]=0;
    }
    for (j=0; j < iNrSimilarities; j++)     if (dec[decit][k[j]] == 1) {
      dArAvailabilities[j]=0.0f;
    }
 else {
      dArAvailabilities[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSamples; j++) {
      mx1[j]=-Float.MAX_VALUE;
    }
    for (j=0; j < iNrSimilarities; j++) {
      tmp=dArAvailabilities[j] + s[j];
      if (tmp > mx1[i[j]]) {
        mx1[i[j]]=tmp;
        idx[i[j]]=k[j];
      }
    }
    for (j=0; j < iNrSamples; j++)     if (dec[decit][j] == 1) {
      idx[j]=j;
      alExamples.add(j);
    }
  }
 else {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
    return -1;
  }
  if (bConverged == false) {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
    return -1;
  }
  ArrayList<Integer> idxExamples=new ArrayList<Integer>();
  for (int i=0; i < alExamples.size(); i++) {
    count.add(0);
  }
  ClusterHelper.sortClusters(set,iVAIdContent,iVAIdStorage,alExamples,eClustererType);
  AlIndexes=getAl(alExamples,count,idxExamples,idx,eClustererType);
  Integer clusteredVAId=set.createStorageVA(AlIndexes);
  set.setAlClusterSizes(count);
  set.setAlExamples(idxExamples);
  GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,false));
  return clusteredVAId;
}