Changeset 15089 for branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs
- Timestamp:
- 06/29/17 14:11:04 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MOCMAEvolutionStrategy/HeuristicLab.Algorithms.MOCMAEvolutionStrategy/3.3/MOCMAEvolutionStrategy.cs
r15045 r15089 77 77 [Storable] 78 78 private double successThreshold; //ptresh 79 79 80 #endregion 80 81 … … 309 310 random = cloner.Clone(original.random); 310 311 gauss = cloner.Clone(original.gauss); 311 solutions = original.solutions .Select(cloner.Clone).ToArray();312 solutions = original.solutions != null ? original.solutions.Select(cloner.Clone).ToArray() : null; 312 313 stepSizeLearningRate = original.stepSizeLearningRate; 313 314 stepSizeDampeningFactor = original.stepSizeDampeningFactor; … … 417 418 } 418 419 } 420 421 419 422 } 420 423 private void Iterate() { … … 478 481 private void SelectParents(IReadOnlyList<Individual> parents, int length) { 479 482 //perform a nondominated sort to assign the rank to every element 480 var fronts = NonDominatedSort(parents); 483 int[] ranks; 484 var fronts = DominationCalculator<Individual>.CalculateAllParetoFronts(parents.ToArray(), parents.Select(i => i.PenalizedFitness).ToArray(), Problem.Maximization, out ranks); 485 // NonDominatedSort(parents, individual => individual.PenalizedFitness); 481 486 482 487 //deselect the highest rank fronts until we would end up with less or equal mu elements … … 485 490 while (popSize - fronts[rank].Count >= length) { 486 491 var front = fronts[rank]; 487 foreach (var i in front) i. Selected = false;492 foreach (var i in front) i.Item1.Selected = false; 488 493 popSize -= front.Count; 489 494 rank--; … … 491 496 492 497 //now use the indicator to deselect the approximatingly worst elements of the last selected front 493 var front1 = fronts[rank].OrderBy(x => x. PenalizedFitness[0]).ToList();498 var front1 = fronts[rank].OrderBy(x => x.Item1.PenalizedFitness[0]).ToList(); 494 499 for (; popSize > length; popSize--) { 495 var lc = Indicator.LeastContributer(front1 , Problem);496 front1[lc]. Selected = false;500 var lc = Indicator.LeastContributer(front1.Select(i => i.Item1).ToArray(), Problem); 501 front1[lc].Item1.Selected = false; 497 502 front1.Swap(lc, front1.Count - 1); 498 503 front1.RemoveAt(front1.Count - 1); … … 546 551 } 547 552 548 #region FastNonDominatedSort549 //blatantly stolen form HeuristicLab.Optimization.Operators.FastNonDominatedSort550 //however: Operators.FastNonDominatedSort does not return ranked fronts => rerank after sorting would not be sensible551 552 private enum DominationResult { Dominates, IsDominated, IsNonDominated };553 private List<List<Individual>> NonDominatedSort(IReadOnlyList<Individual> individuals) {554 const bool dominateOnEqualQualities = false;555 var maximization = Problem.Maximization;556 if (individuals == null) throw new InvalidOperationException(Name + ": No qualities found.");557 var populationSize = individuals.Count;558 559 var fronts = new List<List<Individual>>();560 var dominatedScopes = new Dictionary<Individual, List<int>>();561 var dominationCounter = new int[populationSize];562 563 for (var pI = 0; pI < populationSize - 1; pI++) {564 var p = individuals[pI];565 List<int> dominatedScopesByp;566 if (!dominatedScopes.TryGetValue(p, out dominatedScopesByp))567 dominatedScopes[p] = dominatedScopesByp = new List<int>();568 for (var qI = pI + 1; qI < populationSize; qI++) {569 var test = Dominates(individuals[pI], individuals[qI], maximization, dominateOnEqualQualities);570 if (test == DominationResult.Dominates) {571 dominatedScopesByp.Add(qI);572 dominationCounter[qI] += 1;573 } else if (test == DominationResult.IsDominated) {574 dominationCounter[pI] += 1;575 if (!dominatedScopes.ContainsKey(individuals[qI]))576 dominatedScopes.Add(individuals[qI], new List<int>());577 dominatedScopes[individuals[qI]].Add(pI);578 }579 if (pI == populationSize - 2580 && qI == populationSize - 1581 && dominationCounter[qI] == 0) {582 AddToFront(individuals[qI], fronts, 0);583 }584 }585 if (dominationCounter[pI] == 0) {586 AddToFront(p, fronts, 0);587 }588 }589 var i = 0;590 while (i < fronts.Count && fronts[i].Count > 0) {591 var nextFront = new List<Individual>();592 foreach (var p in fronts[i]) {593 List<int> dominatedScopesByp;594 if (!dominatedScopes.TryGetValue(p, out dominatedScopesByp)) continue;595 foreach (var dominatedScope in dominatedScopesByp) {596 dominationCounter[dominatedScope] -= 1;597 if (dominationCounter[dominatedScope] != 0) continue;598 nextFront.Add(individuals[dominatedScope]);599 }600 }601 i += 1;602 fronts.Add(nextFront);603 }604 605 for (i = 0; i < fronts.Count; i++) {606 foreach (var p in fronts[i]) {607 p.Rank = i;608 }609 }610 return fronts;611 }612 private static void AddToFront(Individual p, IList<List<Individual>> fronts, int i) {613 if (i == fronts.Count) fronts.Add(new List<Individual>());614 fronts[i].Add(p);615 }616 private static DominationResult Dominates(Individual left, Individual right, bool[] maximizations, bool dominateOnEqualQualities) {617 return Dominates(left.PenalizedFitness, right.PenalizedFitness, maximizations, dominateOnEqualQualities);618 }619 private static DominationResult Dominates(IReadOnlyList<double> left, IReadOnlyList<double> right, IReadOnlyList<bool> maximizations, bool dominateOnEqualQualities) {620 //mkommend Caution: do not use LINQ.SequenceEqual for comparing the two quality arrays (left and right) due to performance reasons621 if (dominateOnEqualQualities) {622 var equal = true;623 for (var i = 0; i < left.Count; i++) {624 if (left[i] != right[i]) {625 equal = false;626 break;627 }628 }629 if (equal) return DominationResult.Dominates;630 }631 632 bool leftIsBetter = false, rightIsBetter = false;633 for (var i = 0; i < left.Count; i++) {634 if (IsDominated(left[i], right[i], maximizations[i])) rightIsBetter = true;635 else if (IsDominated(right[i], left[i], maximizations[i])) leftIsBetter = true;636 if (leftIsBetter && rightIsBetter) break;637 }638 639 if (leftIsBetter && !rightIsBetter) return DominationResult.Dominates;640 if (!leftIsBetter && rightIsBetter) return DominationResult.IsDominated;641 return DominationResult.IsNonDominated;642 }643 private static bool IsDominated(double left, double right, bool maximization) {644 return maximization && left < right645 || !maximization && left > right;646 }647 #endregion648 553 } 649 554 }
Note: See TracChangeset
for help on using the changeset viewer.