CDMutation implements EvolutionaryOperator<CDRule> { protected CDRule mutate(CDRule rule, Random rng) { DataSet dataset = DataSet.getDataSet(); for (int condInd = 0; condInd < rule.getNbConditions(); condInd++) { if (rng.nextDouble() > rate) { continue; } int attrInd = CDRule.attributeIndex(condInd); rule.setW(condInd, rndDouble(rule.getW(condInd), 0.0, 1.0, rng)); if (dataset.isNumerical(attrInd)) { rule.setV(condInd, rndDouble(rule.getV(condInd), dataset .getMin(attrInd), dataset.getMax(attrInd), rng)); } else { rule.setV(condInd, rndInt(rule.getV(condInd), dataset .getNbValues(attrInd), rng)); } } return rule; } CDMutation(double rate, double range, int k); @Override List<CDRule> apply(List<CDRule> selectedCandidates, Random rng);  }
@Test public void testMutate() { DataSet dataset = DataSet.getDataSet(); boolean modified = false; int n = 100; for (int nloop = 0; nloop < n; nloop++) { mock.randomDataset(); double range = rng.nextDouble(); int k = rng.nextInt(1000); CDMutation mutation = new CDMutation(1.0, range, k); CDRule rule = new CDRule(0.0, rng); CDRule mutated = mutation.mutate(new CDRule(rule), rng); for (int condInd = 0; condInd < mutated.getNbConditions(); condInd++) { int attrInd = CDRule.attributeIndex(condInd); double value = rule.getV(condInd); double newval = mutated.getV(condInd); modified = modified || (value != newval); if (dataset.isNumerical(attrInd)) { double min = dataset.getMin(attrInd); double max = dataset.getMax(attrInd); assertInRange(newval, min, max); assertTrue(Math.abs(newval - value) <= (max - min) * range); } else { int nbcats = dataset.getNbValues(attrInd); assertInRange(newval, 0, nbcats); } } mock.verify(); } assertTrue(modified); }