Program: 58696
Project: 58696
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
 */
private IVirtualArray 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;
  if (eClustererType == EClustererType.GENE_CLUSTERING)   GeneralManager.get().getEventPublisher().triggerEvent(new RenameProgressBarEvent("String_Node_Str"));
 else   GeneralManager.get().getEventPublisher().triggerEvent(new RenameProgressBarEvent("String_Node_Str"));
  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,true));
      return null;
    }
  }
  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,true));
    return null;
  }
  if (bConverged == false) {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,true));
    return null;
  }
  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);
  IVirtualArray virtualArray=null;
  if (eClustererType == EClustererType.GENE_CLUSTERING)   virtualArray=new VirtualArray(EVAType.CONTENT,set.depth(),alIndexes);
 else   if (eClustererType == EClustererType.EXPERIMENTS_CLUSTERING)   virtualArray=new VirtualArray(EVAType.STORAGE,set.size(),alIndexes);
  set.setAlClusterSizes(count);
  set.setAlExamples(idxExamples);
  GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(50 * iProgressBarMultiplier + iProgressBarOffsetValue,true));
  return virtualArray;
}

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
 */
private IVirtualArray affinityPropagation(EClustererType eClustererType){
  ArrayList<Integer> alIndexes=new ArrayList<Integer>();
  ArrayList<Integer> alExamples=new ArrayList<Integer>();
  ArrayList<Integer> alClusterSizes=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;
  if (eClustererType == EClustererType.GENE_CLUSTERING)   GeneralManager.get().getEventPublisher().triggerEvent(new RenameProgressBarEvent("String_Node_Str"));
 else   GeneralManager.get().getEventPublisher().triggerEvent(new RenameProgressBarEvent("String_Node_Str"));
  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,true));
      return null;
    }
  }
  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,true));
    return null;
  }
  if (bConverged == false) {
    GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(100,true));
    return null;
  }
  ArrayList<Integer> idxExamples=new ArrayList<Integer>();
  for (int i=0; i < alExamples.size(); i++) {
    alClusterSizes.add(0);
  }
  ClusterHelper.sortClusters(set,iVAIdContent,iVAIdStorage,alExamples,eClustererType);
  alIndexes=getAl(alExamples,alClusterSizes,idxExamples,idx,eClustererType);
  IVirtualArray virtualArray=null;
  if (eClustererType == EClustererType.GENE_CLUSTERING)   virtualArray=new VirtualArray(EVAType.CONTENT,set.depth(),alIndexes);
 else   if (eClustererType == EClustererType.EXPERIMENTS_CLUSTERING)   virtualArray=new VirtualArray(EVAType.STORAGE,set.size(),alIndexes);
  set.setAlClusterSizes(alClusterSizes);
  set.setAlExamples(idxExamples);
  GeneralManager.get().getEventPublisher().triggerEvent(new ClusterProgressEvent(50 * iProgressBarMultiplier + iProgressBarOffsetValue,true));
  return virtualArray;
}