Program: 16663
Project: 16663
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=Math.max(maxIter / 50,1000);
  int i=1;
  while (i < maxIter && conv < stepsToConverge) {
    MultipleAlignment lastMSA=msa.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) {
      double move=rnd.nextDouble();
      if (move < 0.5) {
        moved=shiftRow();
        if (debug)         System.out.println("String_Node_Str");
      }
 else       if (move < 0.8) {
        moved=expandBlock();
        if (debug)         System.out.println("String_Node_Str");
      }
 else       if (move < 0.9) {
        moved=shrinkBlock();
        if (debug)         System.out.println("String_Node_Str");
      }
 else {
        moved=insertGap();
        if (debug)         System.out.println("String_Node_Str");
      }
    }
    msa.clear();
    imposer.superimpose(msa);
    mcScore=MultipleAlignmentScorer.getMultipleMCScore(msa,Gopen,Gextend);
    double AS=mcScore - lastScore;
    double prob=1.0;
    if (AS < 0) {
      prob=probabilityFunction(AS,i,maxIter);
      double p=rnd.nextDouble();
      if (p > prob) {
        msa=lastMSA;
        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) {
      lengthHistory.add(msa.length());
      rmsdHistory.add(MultipleAlignmentScorer.getRMSD(msa));
      scoreHistory.add(mcScore);
    }
    i++;
  }
  imposer.superimpose(msa);
  MultipleAlignmentScorer.calculateScores(msa);
  msa.putScore(MultipleAlignmentScorer.MC_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) {
    MultipleAlignment lastMSA=msa.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) {
      double move=rnd.nextDouble();
      if (move < 0.4) {
        moved=shiftRow();
        if (debug)         System.out.println("String_Node_Str");
      }
 else       if (move < 0.7) {
        moved=expandBlock();
        if (debug)         System.out.println("String_Node_Str");
      }
 else       if (move < 0.85) {
        moved=shrinkBlock();
        if (debug)         System.out.println("String_Node_Str");
      }
 else {
        moved=insertGap();
        if (debug)         System.out.println("String_Node_Str");
      }
    }
    msa.clear();
    imposer.superimpose(msa);
    mcScore=MultipleAlignmentScorer.getMultipleMCScore(msa,Gopen,Gextend);
    double AS=mcScore - lastScore;
    double prob=1.0;
    if (AS < 0) {
      prob=probabilityFunction(AS,i,maxIter);
      double p=rnd.nextDouble();
      if (p > prob) {
        msa=lastMSA;
        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) {
      lengthHistory.add(msa.length());
      rmsdHistory.add(MultipleAlignmentScorer.getRMSD(msa));
      scoreHistory.add(mcScore);
    }
    i++;
  }
  imposer.superimpose(msa);
  MultipleAlignmentScorer.calculateScores(msa);
  msa.putScore(MultipleAlignmentScorer.MC_SCORE,mcScore);
}