public RuleSetSpecimen Crossover(RuleSetSpecimen p1, RuleSetSpecimen p2) { int before = (p2.RuleSet.Rules.Count + p1.RuleSet.Rules.Count) / 2; var offspringRules = new Dictionary<NeighborhoodState, CellState>(); var keys = p1.RuleSet.Rules.Keys.Union(p2.RuleSet.Rules.Keys).ToList(); for (int i = 0; i < before; i++) { var key = keys.RandomElement(true); CellState val1, val2; if (p1.RuleSet.Rules.TryGetValue(key, out val1)) { if (p2.RuleSet.Rules.TryGetValue(key, out val2)) { offspringRules.Add(key, (Random.NextBool() ? p1 : p2).RuleSet.Rules[key]); } else { offspringRules.Add(key, val1); } } else { offspringRules.Add(key, p2.RuleSet.Rules[key]); } } return new RuleSetSpecimen(new RuleSet(offspringRules)); }
public RuleSetSpecimen Crossover(RuleSetSpecimen p1, RuleSetSpecimen p2) { Dictionary<NeighborhoodState, CellState> rules = new Dictionary<NeighborhoodState, CellState>(); RuleSetSpecimen t; var keys = p1.RuleSet.Rules.Keys.Union(p2.RuleSet.Rules.Keys); foreach (var key in keys) { if (Random.NextBool()) { t = p1; p1 = p2; p2 = t; } CellState value; if (p1.RuleSet.Rules.TryGetValue(key, out value)) { rules[key] = value; } else { rules[key] = p2.RuleSet.Rules[key]; } } return new RuleSetSpecimen(new RuleSet(rules)); }
public RuleSetSpecimen Crossover(RuleSetSpecimen p1, RuleSetSpecimen p2) { int before = (p2.RuleSet.Rules.Count + p1.RuleSet.Rules.Count) / 2; var offspringRules = new Dictionary<NeighborhoodState, CellState>(); var keys = p1.RuleSet.Rules.Keys.Union(p2.RuleSet.Rules.Keys).ToList(); foreach (var key in p1.RuleSet.Rules.Keys.Except(p2.RuleSet.Rules.Keys)) { offspringRules.Add(key, p1.RuleSet.Rules[key]); } foreach (var key in p2.RuleSet.Rules.Keys.Except(p1.RuleSet.Rules.Keys)) { offspringRules.Add(key, p2.RuleSet.Rules[key]); } foreach (var key in p1.RuleSet.Rules.Keys.Intersect(p2.RuleSet.Rules.Keys)) { offspringRules.Add(key, (Random.NextBool() ? p1 : p2).RuleSet.Rules[key]); } while (offspringRules.Count > before) { offspringRules.Remove(offspringRules.Keys.ElementAt((int)(Random.NextDouble() * offspringRules.Count))); } return new RuleSetSpecimen(new RuleSet(offspringRules)); }
public void Mutate(RuleSetSpecimen specimen, double mutationRate) { List<NeighborhoodState> keys = new List<NeighborhoodState>(); foreach(var key in specimen.RuleSet.Rules.Keys) { if (Random.NextDouble() < mutationRate) keys.Add(key); } foreach (var key in keys) { specimen.RuleSet.Rules[key] = CellState.GetRandom(); } }
public void Mutate(RuleSetSpecimen specimen, double mutationRate) { int count = specimen.RuleSet.Rules.Count; bool[] bools = Random.NextBools((int)(count*mutationRate)); for (int i = 0; i < bools.Length; i++) { if (bools[i]) { specimen.RuleSet.Add(Rule.GetRandom()); } else { specimen.RuleSet.Remove(specimen.RuleSet.Rules.Keys.ElementAt((int)Random.NextDouble() * specimen.RuleSet.Rules.Count)); } } }
public RuleSetSpecimen Crossover(RuleSetSpecimen p1, RuleSetSpecimen p2) { int size = (p1.RuleSet.Rules.Count + p2.RuleSet.Rules.Count) / 2; int p1Segment, p2Segment, offset, remaining; Dictionary<NeighborhoodState,CellState> rules = new Dictionary<NeighborhoodState,CellState>(); do { remaining = size - rules.Count; p1Segment = (int)(Random.NextDouble() * remaining); if (Random.NextBool()) p1Segment = remaining - p1Segment; p2Segment = remaining - p1Segment; p1Segment = Math.Min(p1Segment, p1.RuleSet.Rules.Count); p2Segment = Math.Min(p2Segment, p2.RuleSet.Rules.Count); offset = Random.NextInt(0, p1.RuleSet.Rules.Count - p1Segment); foreach (var rule in p1.RuleSet.Rules.Skip(offset).Take(p1Segment)) { rules[rule.Key] = rule.Value; } offset = Random.NextInt(0, p2.RuleSet.Rules.Count - p2Segment); foreach (var rule in p2.RuleSet.Rules.Skip(offset).Take(p2Segment)) { rules[rule.Key] = rule.Value; } } while (rules.Count < size); return new RuleSetSpecimen(new RuleSet(rules)); }
public RuleSetSpecimen Crossover(RuleSetSpecimen p1, RuleSetSpecimen p2) { int before = (p2.RuleSet.Rules.Count + p1.RuleSet.Rules.Count) / 2; var offspring = new RuleSetSpecimen(new RuleSet(new Dictionary<NeighborhoodState, CellState>())); var keys = p1.RuleSet.Rules.Keys.Union(p2.RuleSet.Rules.Keys).ToList(); foreach (var key in keys) { RuleSet parent; if (Random.NextDouble() > .5) parent = p1.RuleSet; else parent = p2.RuleSet; CellState result; if (parent.Rules.TryGetValue(key, out result)) offspring.RuleSet.Add(new Rule(key, result)); } return offspring; }