@@ -107,7 +107,7 @@ public void run()
107
107
randomS2 = (int )(Math .random () * population .size ());
108
108
while (randomS2 == randomS1 );
109
109
110
- parents [0 ] = population .get (randomS1 );
110
+ parents [0 ] = population .get (randomS1 );
111
111
parents [1 ] = population .get (randomS2 );
112
112
newGeneration .addAll (crossover (parents [0 ], parents [1 ]));
113
113
}
@@ -231,8 +231,21 @@ private ArrayList<RefactoringSequence> crossover(RefactoringSequence p1, Refacto
231
231
float finalScore [] = new float [this .c .length ];
232
232
233
233
// For each refactoring sequence passed in, a cut point is randomly chosen.
234
- int cutPoint1 = (Math .random () < 0.5 ) ? 1 : p1 .getRefactorings ().size () - 1 ;
235
- int cutPoint2 = (cutPoint1 == 1 ) ? 1 : p2 .getRefactorings ().size () - 1 ;
234
+ int cutPoint1 , cutPoint2 ;
235
+
236
+ // If the first parent solution has only 1 refactoring, the cut point will be at the end to
237
+ // avoid the possibility of producing an offspring with no refactorings
238
+ if (p1 .getRefactorings ().size () == 1 )
239
+ cutPoint1 = 1 ;
240
+ else
241
+ cutPoint1 = (Math .random () < 0.5 ) ? 1 : p1 .getRefactorings ().size () - 1 ;
242
+
243
+ // If the first parent solution has 2 refactorings, the cut point of the second solution will be
244
+ // chosen at random between the 2 possibilities. Otherwise, it will be the same as cut point 1.
245
+ if (p1 .getRefactorings ().size () == 2 )
246
+ cutPoint2 = (Math .random () < 0.5 ) ? 1 : p1 .getRefactorings ().size () - 1 ;
247
+ else
248
+ cutPoint2 = (cutPoint1 == 1 ) ? 1 : p2 .getRefactorings ().size () - 1 ;
236
249
237
250
// Initialise the program model to the original state and generate the first child solution.
238
251
this .refactorings = super .resetModel (this .refactorings , this .c [0 ], this .sourceFiles );
0 commit comments