// Nimmt an, dass alle RectAttempts im gleichen Format sind (gleiche Menge an Rechtecken etc.) public IEvolvable Crossover(IEnumerable <IEvolvable> other, int crossovers, Random random) { List <RectAttempt> parents = other.Select(x => (RectAttempt)x).Union(new[] { this }).ToList(); List <int> crossoverSpots = new List <int>(crossovers); int geneCount = rects.Count * 2; for (int i = 0; i < crossovers; i++) { int nextSpot; while (crossoverSpots.Contains(nextSpot = random.Next(0, geneCount))) { ; } crossoverSpots.Add(nextSpot); } crossoverSpots.Sort(); RectAttempt output = new RectAttempt(this); int currentParent = 0; for (int i = 0; i < geneCount; i++) { if (crossoverSpots.Count > 0 && i > crossoverSpots[0]) { currentParent = (currentParent + 1) % parents.Count; crossoverSpots.RemoveAt(0); } if (i % 2 == 0) { output.rects[i / 2].XCenter = parents[currentParent].rects[i / 2].XCenter; } else { output.rects[i / 2].YCenter = parents[currentParent].rects[i / 2].YCenter; } } return(output); }
public SpaceFiller(int batchSize, int evolverCount, List <Rect> baseRects, RectFitness rectFitness, bool allowRotation) { random = new Random(); this.rectFitness = rectFitness; this.allowRotation = allowRotation; this.baseRects = baseRects; totalWidth = baseRects.Sum(x => x.Width); totalHeight = baseRects.Sum(x => x.Height); evolver = new SegregatedEvolver <RectAttempt>(evolverCount, Enumerable.Repeat((RectAttempt)null, batchSize) .Select(x => { RectAttempt rectAttempt = new RectAttempt( baseRects.Select(y => new Rect( (int)((random.NextDouble() - 0.5f) * totalWidth), (int)((random.NextDouble() - 0.5f) * totalHeight), y.Width, y.Height) ) .ToList(), rectFitness, allowRotation); return(rectAttempt); } ).ToList()) { genders = genders, crossovers = crossovers, mutatability = mutatability, survivalRate = survivalRate, failureImmunity = failureImmunity, suddenDeath = suddenDeath, }; }
public RectAttempt(RectAttempt toClone) : this(toClone.rects.Select(x => new Rect(x)).ToList(), toClone.rectFitness, toClone.allowRotation) { }
public bool EqualRects(RectAttempt obj) { return(allowRotation == obj.allowRotation && rects.SequenceEqual(obj.rects)); }