- Timestamp:
- 10/30/15 17:10:35 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainLoop.cs
r13046 r13095 30 30 using HeuristicLab.Parameters; 31 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Selection;33 32 34 33 namespace HeuristicLab.Algorithms.ALPS { … … 61 60 get { return OperatorGraph.Iterate().OfType<EldersEmigrator>().First(); } 62 61 } 63 public LayerUpdator LayerUpdator {64 get { return OperatorGraph.Iterate().OfType<LayerUpdator>().First(); }65 }66 62 67 63 [StorableConstructor] … … 90 86 var matingPoolProcessor = new UniformSubScopesProcessor(); 91 87 var initializeLayer = new Assigner() { Name = "Reset LayerEvaluatedSolutions" }; 92 var mainOperator = CreatePreparedGeneticAlgorithmMainLoop();88 var mainOperator = AlpsGeneticAlgorithmMainOperator.Create(); 93 89 var generationsIcrementor = new IntCounter() { Name = "Increment Generations" }; 94 90 var evaluatedSolutionsReducer = new DataReducer() { Name = "Increment EvaluatedSolutions" }; 95 91 var eldersEmigrator = new EldersEmigrator() { Name = "Emigrate Elders" }; 96 var layerUpdator = new LayerUpdator(mainOperator) { Name = "Update Layers" }; 92 var layerOpener = new LayerOpener() { Name = "Open new Layer" }; 93 var layerReseeder = new LayerReseeder() { Name = "Reseed Layer Zero" }; 97 94 var layerAnalyzerProcessor = new UniformSubScopesProcessor(); 98 95 var layerAnalyzerPlaceholder = new Placeholder() { Name = "LayerAnalyzer (Placeholder)" }; … … 146 143 evaluatedSolutionsReducer.Successor = eldersEmigrator; 147 144 148 eldersEmigrator.Successor = layer Updator;145 eldersEmigrator.Successor = layerOpener; 149 146 150 layerUpdator.Successor = layerAnalyzerProcessor; 147 layerOpener.Successor = layerReseeder; 148 149 layerReseeder.Successor = layerAnalyzerProcessor; 151 150 152 151 layerAnalyzerProcessor.Operator = layerAnalyzerPlaceholder; … … 160 159 termination.ContinueBranch = matingPoolCreator; 161 160 } 162 163 private GeneticAlgorithmMainLoop CreatePreparedGeneticAlgorithmMainLoop() {164 var mainLoop = new GeneticAlgorithmMainLoop();165 166 var oldInitialOp = mainLoop.OperatorGraph.InitialOperator;167 var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");168 var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");169 var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();170 var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();171 172 // Operator starts with calculating number of selected scopes base on plus/comma-selection replacement scheme173 var numberOfSubScopesBranch = new ConditionalBranch() { Name = "PlusSelection?" };174 var numberOfSelectedSubScopesPlusCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = PopulationSize * 2" };175 var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = (PopulationSize - Elites) * 2" };176 var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };177 178 // Set new initial operator179 mainLoop.OperatorGraph.InitialOperator = numberOfSubScopesBranch;180 181 numberOfSubScopesBranch.ConditionParameter.ActualName = "PlusSelection";182 numberOfSubScopesBranch.TrueBranch = numberOfSelectedSubScopesPlusCalculator;183 numberOfSubScopesBranch.FalseBranch = numberOfSelectedSubScopesCalculator;184 numberOfSubScopesBranch.Successor = selector;185 186 numberOfSelectedSubScopesPlusCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));187 numberOfSelectedSubScopesPlusCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";188 numberOfSelectedSubScopesPlusCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 2 * toint");189 190 numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));191 numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));192 numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";193 numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize Elites - 2 * toint");194 195 // Use Elitism or Plus-Selection as replacement strategy196 var selectedProcessor = (SubScopesProcessor)selector.Successor;197 var elitismReplacement = selectedProcessor.Successor;198 selectedProcessor.Successor = replacementBranch;199 replacementBranch.ConditionParameter.ActualName = "PlusSelection";200 replacementBranch.FalseBranch = elitismReplacement;201 202 // Plus selection replacement203 var replacementMergingReducer = new MergingReducer();204 var replacementBestSelector = new BestSelector();205 var replacementRightReducer = new RightReducer();206 replacementBranch.TrueBranch = replacementMergingReducer;207 208 replacementMergingReducer.Successor = replacementBestSelector;209 210 replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";211 replacementBestSelector.CopySelected = new BoolValue(false);212 replacementBestSelector.Successor = replacementRightReducer;213 214 replacementRightReducer.Successor = null;215 216 // Increment ages of all individuals after replacement217 var incrementAgeProcessor = new UniformSubScopesProcessor();218 var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };219 replacementBranch.Successor = incrementAgeProcessor;220 incrementAgeProcessor.Operator = ageIncrementor;221 incrementAgeProcessor.Successor = null;222 ageIncrementor.ValueParameter.ActualName = "Age";223 ageIncrementor.Increment = new DoubleValue(1.0);224 225 // Insert AgeCalculator between crossover and its successor226 var crossoverSuccessor = crossover.Successor;227 var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };228 crossover.Successor = ageCalculator;229 230 ageCalculator.ParameterToReduce.ActualName = "Age";231 ageCalculator.TargetParameter.ActualName = "Age";232 ageCalculator.WeightParameter.ActualName = "AgeInheritance";233 ageCalculator.Successor = crossoverSuccessor;234 235 // When counting the evaluated solutions, write in LayerEvaluatedSolutions236 subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";237 subScopesCounter.AccumulateParameter.Value = new BoolValue(false);238 239 // Instead of generational loop after merging of elites, stop240 elitesMerger.Successor = null;241 242 // Parameterize243 foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())244 stochasticOperator.RandomParameter.ActualName = "LocalRandom";245 foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())246 stochasticBranch.RandomParameter.ActualName = "LocalRandom";247 248 // Remove unused operators249 var usedOperators = mainLoop.OperatorGraph.Iterate();250 var unusedOperators = mainLoop.OperatorGraph.Operators.Except(usedOperators);251 foreach (var op in unusedOperators.ToList())252 mainLoop.OperatorGraph.Operators.Remove(op);253 254 return mainLoop;255 }256 161 } 257 162 }
Note: See TracChangeset
for help on using the changeset viewer.