Program: 16677
Project: 16677
Buggy source:
/** 
 * Optimization method based in a Monte-Carlo approach. Starting from the refined alignment uses 4 types of moves: <p> 1- Shift Row: if there are enough freePool residues available.<p> 2- Expand Block: add another alignment column if there are residues available.<p> 3- Shrink Block: move a block column to the freePool.<p> 4- Insert gap: insert a gap in a random position of the alignment.
 */
private void optimizeMC(int maxIter) throws StructureException {
  lengthHistory=new ArrayList<Integer>();
  rmsdHistory=new ArrayList<Double>();
  scoreHistory=new ArrayList<Double>();
  int conv=0;
  int stepsToConverge=maxIter / 20;
  int i=1;
  while (i < maxIter && conv < stepsToConverge) {
    BlockSet lastBlock=block.clone();
    List<SortedSet<Integer>> lastFreePool=new ArrayList<SortedSet<Integer>>();
    for (int k=0; k < size; k++) {
      SortedSet<Integer> p=new TreeSet<Integer>();
      for (      Integer l : freePool.get(k))       p.add(l);
      lastFreePool.add(p);
    }
    double lastScore=mcScore;
    boolean moved=false;
    while (!moved) {
      int move=rnd.nextInt(4);
switch (move) {
case 0:
        moved=shiftRow();
      if (debug)       System.out.println("String_Node_Str");
    break;
case 1:
  moved=expandBlock();
if (debug) System.out.println("String_Node_Str");
break;
case 2:
moved=shrinkBlock();
if (debug) System.out.println("String_Node_Str");
break;
case 3:
moved=insertGap();
if (debug) System.out.println("String_Node_Str");
break;
}
}
block.clear();
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
mcScore=MultipleAlignmentScorer.getCEMCScore(msa);
double AS=mcScore - lastScore;
double prob=1.0;
if (AS < 0) {
prob=probabilityFunction(AS,i,maxIter);
double p=rnd.nextDouble();
if (p > prob) {
block=lastBlock;
freePool=lastFreePool;
mcScore=lastScore;
conv++;
}
 else conv=0;
}
 else conv=0;
if (debug) System.out.println("String_Node_Str" + i + "String_Node_Str"+ prob+ "String_Node_Str"+ AS+ "String_Node_Str"+ conv);
if (i % 100 == 1) {
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
lengthHistory.add(block.length());
rmsdHistory.add(MultipleAlignmentScorer.getRMSD(msa));
scoreHistory.add(mcScore);
}
i++;
}
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
MultipleAlignmentScorer.calculateScores(msa);
msa.putScore(MultipleAlignmentScorer.CEMC_SCORE,mcScore);
}

Fixed version (ground truth):
/** 
 * Optimization method based in a Monte-Carlo approach. Starting from the refined alignment uses 4 types of moves: <p> 1- Shift Row: if there are enough freePool residues available.<p> 2- Expand Block: add another alignment column if there are residues available.<p> 3- Shrink Block: move a block column to the freePool.<p> 4- Insert gap: insert a gap in a random position of the alignment.
 */
private void optimizeMC(int maxIter) throws StructureException {
  lengthHistory=new ArrayList<Integer>();
  rmsdHistory=new ArrayList<Double>();
  scoreHistory=new ArrayList<Double>();
  int conv=0;
  int stepsToConverge=Math.max(maxIter / 50,1000);
  int i=1;
  while (i < maxIter && conv < stepsToConverge) {
    BlockSet lastBlock=block.clone();
    List<SortedSet<Integer>> lastFreePool=new ArrayList<SortedSet<Integer>>();
    for (int k=0; k < size; k++) {
      SortedSet<Integer> p=new TreeSet<Integer>();
      for (      Integer l : freePool.get(k))       p.add(l);
      lastFreePool.add(p);
    }
    double lastScore=mcScore;
    boolean moved=false;
    while (!moved) {
      int move=rnd.nextInt(4);
switch (move) {
case 0:
        moved=shiftRow();
      if (debug)       System.out.println("String_Node_Str");
    break;
case 1:
  moved=expandBlock();
if (debug) System.out.println("String_Node_Str");
break;
case 2:
moved=shrinkBlock();
if (debug) System.out.println("String_Node_Str");
break;
case 3:
moved=insertGap();
if (debug) System.out.println("String_Node_Str");
break;
}
}
block.clear();
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
mcScore=MultipleAlignmentScorer.getCEMCScore(msa);
double AS=mcScore - lastScore;
double prob=1.0;
if (AS < 0) {
prob=probabilityFunction(AS,i,maxIter);
double p=rnd.nextDouble();
if (p > prob) {
block=lastBlock;
freePool=lastFreePool;
mcScore=lastScore;
conv++;
}
 else conv=0;
}
 else conv=0;
if (debug) System.out.println("String_Node_Str" + i + "String_Node_Str"+ prob+ "String_Node_Str"+ AS+ "String_Node_Str"+ conv);
if (i % 100 == 1) {
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
lengthHistory.add(block.length());
rmsdHistory.add(MultipleAlignmentScorer.getRMSD(msa));
scoreHistory.add(mcScore);
}
i++;
}
msa.setBlockSets(Arrays.asList(block));
imposer.superimpose(msa);
MultipleAlignmentScorer.calculateScores(msa);
msa.putScore(MultipleAlignmentScorer.CEMC_SCORE,mcScore);
}