Changeset 12174 for branches/ALPS
- Timestamp:
- 03/10/15 11:54:17 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ALPS/HeuristicLab.Algorithms.ALPS.SteadyState/3.3/AlpsSsMover.cs
r12162 r12174 34 34 [StorableClass] 35 35 public class AlpsSsMover : SingleSuccessorOperator, IStochasticOperator { 36 #region Parameter Properties 36 37 private ILookupParameter<IntValue> LayerParameter { 37 38 get { return (ILookupParameter<IntValue>)Parameters["Layer"]; } … … 40 41 get { return (ILookupParameter<IntValue>)Parameters["TargetIndex"]; } 41 42 } 42 private ILookupParameter<IntValue> PopulationSizeParameter {43 get { return (ILookupParameter<IntValue>)Parameters[" PopulationSize"]; }43 private ILookupParameter<IntValue> LayerSizeParameter { 44 get { return (ILookupParameter<IntValue>)Parameters["LayerSize"]; } 44 45 } 45 46 private ILookupParameter<IntValue> NumberOfLayersParameter { … … 55 56 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 56 57 } 58 public IScopeTreeLookupParameter<DoubleValue> QualityParameter { 59 get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; } 60 } 61 public ValueLookupParameter<BoolValue> MaximizationParameter { 62 get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; } 63 } 64 public IScopeTreeLookupParameter<IntValue> EvalsCreatedParameter { 65 get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsCreated"]; } 66 } 67 public IScopeTreeLookupParameter<IntValue> EvalsMovedQualityParameter { 68 get { return (IScopeTreeLookupParameter<IntValue>)Parameters["EvalsMoved"]; } 69 } 70 private ILookupParameter<IntValue> EvaluatedSolutionsParameter { 71 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; } 72 } 73 private ILookupParameter<IntArray> AgeLimitsParameter { 74 get { return (ILookupParameter<IntArray>)Parameters["AgeLimits"]; } 75 } 76 #endregion 57 77 58 78 [StorableConstructor] … … 69 89 Parameters.Add(new LookupParameter<IntValue>("Layer")); 70 90 Parameters.Add(new LookupParameter<IntValue>("TargetIndex")); 71 Parameters.Add(new LookupParameter<IntValue>(" PopulationSize"));91 Parameters.Add(new LookupParameter<IntValue>("LayerSize")); 72 92 Parameters.Add(new LookupParameter<IntValue>("NumberOfLayers")); 73 93 Parameters.Add(new LookupParameter<IScope>("WorkingScope")); 74 94 Parameters.Add(new LookupParameter<IScope>("Layers")); 75 95 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 96 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality")); 97 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 98 Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsCreated")); 99 Parameters.Add(new ScopeTreeLookupParameter<IntValue>("EvalsMoved")); 100 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions")); 101 Parameters.Add(new LookupParameter<IntArray>("AgeLimits")); 76 102 } 77 103 78 104 private int n; 79 105 private int m; 106 private int evals; 107 private int popSize; 80 108 private IScope layers; 81 109 private IRandom rand; 110 private string qualityVariableName; 111 private IntArray ageLimits; 112 private bool maximization; 82 113 public override IOperation Apply() { 83 114 int i = LayerParameter.ActualValue.Value; 84 115 int j = TargetIndexParameter.ActualValue.Value; 85 116 n = NumberOfLayersParameter.ActualValue.Value; 86 m = PopulationSizeParameter.ActualValue.Value; 117 m = LayerSizeParameter.ActualValue.Value; 118 evals = EvaluatedSolutionsParameter.ActualValue.Value; 119 popSize = n * m; 87 120 rand = RandomParameter.ActualValue; 121 qualityVariableName = QualityParameter.TranslatedName; 122 ageLimits = AgeLimitsParameter.ActualValue; 123 maximization = MaximizationParameter.ActualValue.Value; 88 124 89 125 layers = LayersParameter.ActualValue; … … 110 146 } 111 147 } 112 private int? FindReplaceable(IScope layer, IScope individual) { 148 private int? FindReplaceable(IScope layer, IScope individualToReplace) { 149 int layerNumber = ((IntValue)layer.Variables["Layer"].Value).Value; 150 151 var individuals = ( 152 from individual in layer.SubScopes 153 let quality = ((DoubleValue)individual.Variables[qualityVariableName].Value).Value 154 let evalsCreated = ((IntValue)individual.Variables["EvalsCreated"].Value).Value 155 let age = (evals - evalsCreated) / popSize 156 select new { individual, quality, age } 157 ).Select((x, index) => new { index, x.individual, x.quality, x.age }) 158 .ToList(); 159 160 var ageLimit = layerNumber < n - 1 ? ageLimits[layerNumber] : int.MaxValue; 161 162 var toOldIndividual = individuals.FirstOrDefault(x => x.age >= ageLimit); 163 if (toOldIndividual != null) 164 return toOldIndividual.index; 165 166 double replaceQuality = ((DoubleValue)individualToReplace.Variables[qualityVariableName].Value).Value; 167 var sortedIndividuals = maximization ? individuals.OrderBy(x => x.quality) : individuals.OrderByDescending(x => x.quality); 168 var worstIndividual = sortedIndividuals.FirstOrDefault(x => maximization ? x.quality < replaceQuality : x.quality > replaceQuality); 169 if (worstIndividual != null) 170 return worstIndividual.index; 171 113 172 return Enumerable.Range(0, layer.SubScopes.Count).SampleRandom(rand); 114 173 }
Note: See TracChangeset
for help on using the changeset viewer.