- Timestamp:
- 11/19/15 13:34:42 (9 years ago)
- Location:
- stable
- Files:
-
- 7 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 13237,13261
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Optimization.Operators/3.3/ChildrenCopyCreator.cs
r13294 r13296 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 48 49 public ChildrenCopyCreator() 49 50 : base() { 50 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes represent the parents."));51 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope whose sub-scopes should be copied.")); 51 52 } 52 53 … … 56 57 57 58 public override IOperation Apply() { 58 int parents= CurrentScope.SubScopes.Count;59 int nChildren = CurrentScope.SubScopes.Count; 59 60 60 for (int i = 0; i < parents; i++) {61 IScope parent= CurrentScope.SubScopes[i];62 parent.SubScopes.Clear();61 for (int i = 0; i < nChildren; i++) { 62 IScope child = CurrentScope.SubScopes[i]; 63 if (child.SubScopes.Count > 0) throw new ArgumentException("The sub-scope that should be cloned has further sub-scopes."); 63 64 64 //copy parent65 IScope child = new Scope(i.ToString());66 foreach (IVariable var in parent.Variables)67 child .Variables.Add((IVariable)var.Clone());65 IScope childCopy = new Scope(i.ToString()); 66 var cloner = new Cloner(); 67 foreach (IVariable var in child.Variables) 68 childCopy.Variables.Add(cloner.Clone(var)); 68 69 69 parent.SubScopes.Add(child);70 child.SubScopes.Add(childCopy); 70 71 } 71 72 return base.Apply(); -
stable/HeuristicLab.Selection/3.3/EvolutionStrategyOffspringSelector.cs
r13294 r13296 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Runtime.InteropServices; 24 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; … … 34 35 public class EvolutionStrategyOffspringSelector : SingleSuccessorOperator { 35 36 36 private class QualityComparer : IComparer<IScope> {37 private class FitnessComparer : IComparer<IScope> { 37 38 38 39 #region IComparer<IScope> Member 39 40 40 41 private String qualityParameterName; 41 42 public QualityComparer(String qualityParamName) { 42 private bool maximization; 43 44 public FitnessComparer(String qualityParamName, bool maximization) { 43 45 this.qualityParameterName = qualityParamName; 44 } 45 46 this.maximization = maximization; 47 } 48 49 // less than zero if x is-better-than y 50 // larger than zero if y is-better-than x 51 // zero if both are the same 46 52 public int Compare(IScope x, IScope y) { 47 IVariable quality1, quality2; 48 49 if (x.Variables.TryGetValue(qualityParameterName, out quality1) 50 && y.Variables.TryGetValue(qualityParameterName, out quality2)) { 51 DoubleValue dblVal = quality1.Value as DoubleValue; 52 DoubleValue dblVal2 = quality2.Value as DoubleValue; 53 return dblVal.CompareTo(dblVal2); 54 } 55 else 56 throw new Exception("ERROR!!! Quality Param: "+qualityParameterName); 53 IVariable quality1, quality2; 54 55 if (x.Variables.TryGetValue(qualityParameterName, out quality1) 56 && y.Variables.TryGetValue(qualityParameterName, out quality2)) { 57 DoubleValue left = quality1.Value as DoubleValue; 58 DoubleValue right = quality2.Value as DoubleValue; 59 var res = left.CompareTo(right); 60 if (maximization) return -res; // in the maximization case the largest value should preceed all others in the sort order 61 else return res; 62 } else 63 throw new ArgumentException("Quality variable " + qualityParameterName + " not found."); 57 64 } 58 65 … … 95 102 public ILookupParameter<DoubleValue> QualityParameter { 96 103 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 104 } 105 public ILookupParameter<BoolValue> MaximizationParameter { 106 get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; } 97 107 } 98 108 … … 119 129 Parameters.Add(new ScopeTreeLookupParameter<BoolValue>("SuccessfulOffspring", "True if the offspring was more successful than its parents.", 2)); 120 130 Parameters.Add(new OperatorParameter("OffspringCreator", "The operator used to create new offspring.")); 121 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));131 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of solution evaluations.")); 122 132 Parameters.Add(new LookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).")); 123 133 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a child")); 134 Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Flag that indicates if the problem is a maximization problem")); 124 135 } 125 136 … … 165 176 // implement the ActualValue fetch here - otherwise the parent scope would also be included, given that there may be 1000 or more parents, this is quite unnecessary 166 177 string tname = SuccessfulOffspringParameter.TranslatedName; 167 double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize; 178 double tmpSelPress = selectionPressure.Value; 179 double tmpSelPressInc = 1.0 / populationSize; 168 180 for (int i = 0; i < offspring.SubScopes.Count; i++) { 169 181 // fetch value … … 176 188 if (tmp.Value) { 177 189 IScope currentOffspring = offspring.SubScopes[i]; 178 offspring.SubScopes.Remove (currentOffspring);179 i--; 190 offspring.SubScopes.RemoveAt(i); 191 i--; // next loop should continue with the subscope at index i which replaced currentOffspring 180 192 population.Add(currentOffspring); 181 193 successfulOffspringAdded++; 182 } 183 else { 194 } else { 184 195 IScope currentOffspring = offspring.SubScopes[i]; 185 offspring.SubScopes.Remove (currentOffspring);196 offspring.SubScopes.RemoveAt(i); 186 197 i--; 187 virtual_population.Add(currentOffspring); 198 virtual_population.Add(currentOffspring); // add to losers pool 188 199 } 189 200 tmpSelPress += tmpSelPressInc; 190 201 191 double tmpSuccessRatio = (successfulOffspring.Value +successfulOffspringAdded) / ((double)populationSize);202 double tmpSuccessRatio = (successfulOffspring.Value + successfulOffspringAdded) / ((double)populationSize); 192 203 if (tmpSuccessRatio >= successRatio && (population.Count + virtual_population.Count) >= populationSize) 193 204 break; … … 200 211 201 212 // check if enough children have been generated (or limit of selection pressure or evaluted solutions is reached) 202 if (((EvaluatedSolutionsParameter.ActualValue.Value < MaximumEvaluatedSolutionsParameter.ActualValue.Value) 203 && (selectionPressure.Value < maxSelPress) 204 && (currentSuccessRatio.Value < successRatio)) 213 if (((EvaluatedSolutionsParameter.ActualValue.Value < MaximumEvaluatedSolutionsParameter.ActualValue.Value) 214 && (selectionPressure.Value < maxSelPress) 215 && (currentSuccessRatio.Value < successRatio)) 205 216 || ((population.Count + virtual_population.Count) < populationSize)) { 206 217 // more children required -> reduce left and start children generation again … … 209 220 while (parents.SubScopes.Count > 0) { 210 221 IScope parent = parents.SubScopes[0]; 211 parents.SubScopes.RemoveAt(0); 212 scope.SubScopes.Add(parent); 213 } 214 215 IOperator moreOffspring= OffspringCreatorParameter.ActualValue as IOperator;216 if ( moreOffspring== null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them.");217 return ExecutionContext.CreateOperation( moreOffspring);222 parents.SubScopes.RemoveAt(0); // TODO: repeated call of RemoveAt(0) is inefficient? 223 scope.SubScopes.Add(parent); // order of subscopes is reversed 224 } 225 226 IOperator offspringCreator = OffspringCreatorParameter.ActualValue as IOperator; 227 if (offspringCreator == null) throw new InvalidOperationException(Name + ": More offspring are required, but no operator specified for creating them."); 228 return ExecutionContext.CreateOperation(offspringCreator); // this assumes that this operator will be called again indirectly or directly 218 229 } else { 219 230 // enough children generated 220 QualityComparer qualityComparer = new QualityComparer(QualityParameter.TranslatedName);221 population.Sort( qualityComparer);222 223 // only keep minimum best successful children in population231 var fitnessComparer = new FitnessComparer(QualityParameter.TranslatedName, MaximizationParameter.ActualValue.Value); 232 population.Sort(fitnessComparer); // sort individuals by descending fitness 233 234 // keeps only the best successRatio * populationSize solutions in the population (the remaining ones are added to the virtual population) 224 235 int removed = 0; 225 236 for (int i = 0; i < population.Count; i++) { 226 237 double tmpSuccessRatio = i / (double)populationSize; 227 238 if (tmpSuccessRatio > successRatio) { 228 229 239 virtual_population.Add(population[i]); 240 removed++; 230 241 } 231 242 } … … 233 244 234 245 //fill up population with best remaining children (successful or unsuccessful) 235 virtual_population.Sort( qualityComparer);246 virtual_population.Sort(fitnessComparer); 236 247 int offspringNeeded = populationSize - population.Count; 237 248 for (int i = 0; i < offspringNeeded && i < virtual_population.Count; i++) { 238 population.Add(virtual_population[i]); 249 population.Add(virtual_population[i]); // children are sorted by descending fitness 239 250 } 240 251 -
stable/HeuristicLab.Tests
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Tests merged: 13237
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Tests/HeuristicLab-3.3/Samples/SamplesUtils.cs
r13293 r13296 5 5 using HeuristicLab.Algorithms.EvolutionStrategy; 6 6 using HeuristicLab.Algorithms.GeneticAlgorithm; 7 using HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy; 7 8 using HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm; 8 9 using HeuristicLab.Data; … … 71 72 } 72 73 74 public static void ConfigureOffspringSelectionEvolutionStrategyParameters<R, M, SC, SR, SM>(OffspringSelectionEvolutionStrategy es, int popSize, 75 double successRatio, double comparisonFactor, double maxSelPres, int parentsPerChild, int maxGens, bool plusSelection) 76 where R : ICrossover 77 where M : IManipulator 78 where SC : IStrategyParameterCreator 79 where SR : IStrategyParameterCrossover 80 where SM : IStrategyParameterManipulator { 81 es.PopulationSize.Value = popSize; 82 es.ComparisonFactor.Value = comparisonFactor; 83 es.SuccessRatio.Value = successRatio; 84 es.MaximumSelectionPressure.Value = maxSelPres; 85 es.ParentsPerChild.Value = parentsPerChild; 86 es.SelectedParents.Value = popSize * parentsPerChild; 87 es.MaximumGenerations.Value = maxGens; 88 es.PlusSelection.Value = false; 89 90 es.Seed.Value = 0; 91 es.SetSeedRandomly.Value = true; 92 93 es.Recombinator = es.RecombinatorParameter.ValidValues 94 .OfType<R>() 95 .Single(); 96 97 es.Mutator = es.MutatorParameter.ValidValues 98 .OfType<M>() 99 .Single(); 100 101 es.StrategyParameterCreator = es.StrategyParameterCreatorParameter.ValidValues 102 .OfType<SC>() 103 .Single(); 104 es.StrategyParameterCrossover = es.StrategyParameterCrossoverParameter.ValidValues 105 .OfType<SR>() 106 .Single(); 107 es.StrategyParameterManipulator = es.StrategyParameterManipulatorParameter.ValidValues 108 .OfType<SM>() 109 .Single(); 110 es.Engine = new ParallelEngine.ParallelEngine(); 111 } 112 73 113 public static void ConfigureGeneticAlgorithmParameters<S, C, M>(GeneticAlgorithm ga, int popSize, int elites, int maxGens, double mutationRate, int tournGroupSize = 0) 74 114 where S : ISelector … … 194 234 195 235 ga.MaximumGenerations = maxGens; 196 236 197 237 ga.Engine = new ParallelEngine.ParallelEngine(); 198 238 } -
stable/HeuristicLab.Tests/HeuristicLab.Tests.csproj
r13293 r13296 131 131 <Reference Include="HeuristicLab.Algorithms.LocalSearch-3.3"> 132 132 <HintPath>..\bin\HeuristicLab.Algorithms.LocalSearch-3.3.dll</HintPath> 133 <Private>False</Private> 134 </Reference> 135 <Reference Include="HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 136 <HintPath>..\bin\HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy-3.3.dll</HintPath> 133 137 <Private>False</Private> 134 138 </Reference> … … 436 440 <Compile Include="HeuristicLab-3.3\PluginLoader.cs" /> 437 441 <Compile Include="HeuristicLab-3.3\Samples\AlpsSampleTest.cs" /> 442 <Compile Include="HeuristicLab-3.3\Samples\OSESGriewankSampleTest.cs" /> 438 443 <Compile Include="HeuristicLab-3.3\Samples\GAGroupingProblemSampleTest.cs" /> 439 444 <Compile Include="HeuristicLab-3.3\Samples\VnsOpSampleTest.cs" />
Note: See TracChangeset
for help on using the changeset viewer.