Thus, it turns out that the mutation function sometimes nullified some of my bitstrings, which caused the population to contain empty strings.
Before that, he looked like this:
public String mutate(String bits) { Random random = new Random(); StringBuffer buf = new StringBuffer(bits); for (int i = 0; i < bits.length(); i++) { if (random.nextFloat() < mutationRate) { if (bits.charAt(i) == '1') { buf.setCharAt(i, '0'); return buf.toString(); } else { buf.setCharAt(i, '1'); return buf.toString(); } } } return ""; }
And I changed it to this:
public String mutate(String bits) { Random random = new Random(); StringBuffer buf = new StringBuffer(bits); for (int i = 0; i < bits.length(); i++) { if (random.nextFloat() < mutationRate) { if (bits.charAt(i) == '1') { buf.setCharAt(i, '0'); } else { buf.setCharAt(i, '1'); } } } return buf.toString(); }
A careless mistake.
source share