Changeset 4045 for trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2.cs
- Timestamp:
- 07/19/10 15:24:32 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.NSGA2/3.3/NSGA2.cs
r4017 r4045 32 32 using HeuristicLab.Optimization.Operators; 33 33 using HeuristicLab.PluginInfrastructure; 34 using HeuristicLab.Selection; 35 using HeuristicLab.Operators; 34 36 35 37 namespace HeuristicLab.Algorithms.NSGA2 { … … 131 133 get { return (SolutionsCreator)RandomCreator.Successor; } 132 134 } 135 private RankAndCrowdingSorter RankAndCrowdingSorter { 136 get { return (RankAndCrowdingSorter)SolutionsCreator.Successor; } 137 } 133 138 private NSGA2MainLoop MainLoop { 134 get { return (NSGA2MainLoop) SolutionsCreator.Successor; }139 get { return (NSGA2MainLoop)RankAndCrowdingSorter.Successor; } 135 140 } 136 141 #endregion … … 143 148 Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100))); 144 149 Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction.")); 145 Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on two parents.", new PercentValue(0. 05)));150 Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "The probability that the crossover operator is applied on two parents.", new PercentValue(0.9))); 146 151 Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions.")); 147 152 Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05))); … … 152 157 RandomCreator randomCreator = new RandomCreator(); 153 158 SolutionsCreator solutionsCreator = new SolutionsCreator(); 159 RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter(); 154 160 NSGA2MainLoop mainLoop = new NSGA2MainLoop(); 155 161 OperatorGraph.InitialOperator = randomCreator; … … 163 169 164 170 solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name; 165 solutionsCreator.Successor = mainLoop; 166 171 solutionsCreator.Successor = rankAndCrowdingSorter; 172 173 rankAndCrowdingSorter.CrowdingDistanceParameter.ActualName = "CrowdingDistance"; 174 rankAndCrowdingSorter.RankParameter.ActualName = "Rank"; 175 rankAndCrowdingSorter.Successor = mainLoop; 176 177 mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name; 167 178 mainLoop.SelectorParameter.ActualName = SelectorParameter.Name; 168 179 mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name; … … 175 186 mainLoop.ResultsParameter.ActualName = "Results"; 176 187 177 foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is I MultiObjectiveSelector)).OrderBy(x => x.Name))188 foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is ISingleObjectiveSelector)).OrderBy(x => x.Name)) 178 189 SelectorParameter.ValidValues.Add(selector); 179 ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));180 if ( proportionalSelector != null) SelectorParameter.Value = proportionalSelector;181 182 // TODO: parameterize selectors190 ISelector tournamentSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("CrowdedTournamentSelector")); 191 if (tournamentSelector != null) SelectorParameter.Value = tournamentSelector; 192 193 ParameterizeSelectors(); 183 194 184 195 AttachEventHandlers(); … … 193 204 #region Events 194 205 protected override void OnProblemChanged() { 195 // parameterize pretty much everything 206 ParameterizeStochasticOperator(Problem.SolutionCreator); 207 ParameterizeStochasticOperator(Problem.Evaluator); 208 foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op); 209 ParameterizeSolutionsCreator(); 210 ParameterizeMainLoop(); 211 ParameterizeSelectors(); 212 ParameterizeAnalyzers(); 213 ParameterizeIterationBasedOperators(); 214 UpdateCrossovers(); 215 UpdateMutators(); 216 UpdateAnalyzers(); 217 Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged); 196 218 base.OnProblemChanged(); 197 219 } 198 220 protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) { 199 // parameterize SolutionCreator 221 ParameterizeStochasticOperator(Problem.SolutionCreator); 222 ParameterizeSolutionsCreator(); 200 223 base.Problem_SolutionCreatorChanged(sender, e); 201 224 } 202 225 protected override void Problem_EvaluatorChanged(object sender, EventArgs e) { 203 // parameterize StochasticOperators204 // parameterize SolutionsCreator205 // parameterize MainLoop206 // parameterize Selectors207 // parameterize Analyzers226 ParameterizeStochasticOperator(Problem.Evaluator); 227 ParameterizeSolutionsCreator(); 228 ParameterizeMainLoop(); 229 ParameterizeSelectors(); 230 ParameterizeAnalyzers(); 208 231 Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged); 209 232 base.Problem_EvaluatorChanged(sender, e); 210 233 } 211 234 protected override void Problem_OperatorsChanged(object sender, EventArgs e) { 235 foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op); 236 ParameterizeIterationBasedOperators(); 237 UpdateCrossovers(); 238 UpdateMutators(); 239 UpdateAnalyzers(); 212 240 base.Problem_OperatorsChanged(sender, e); 213 241 } … … 215 243 base.Problem_Reset(sender, e); 216 244 } 217 218 245 private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) { 219 246 PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged); 220 // parameterize Selectors247 ParameterizeSelectors(); 221 248 } 222 249 private void PopulationSize_ValueChanged(object sender, EventArgs e) { 223 // parameterize Selectors 224 } 225 250 ParameterizeSelectors(); 251 } 226 252 private void Evaluator_QualitiesParameter_ActualNameChanged(object sender, EventArgs e) { 227 // parameterize Analyzers228 // parameterize MainLoop229 // parameterize Selectors253 ParameterizeMainLoop(); 254 ParameterizeSelectors(); 255 ParameterizeAnalyzers(); 230 256 } 231 257 #endregion … … 240 266 } 241 267 } 268 private void ParameterizeSolutionsCreator() { 269 SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 270 SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name; 271 } 272 private void ParameterizeMainLoop() { 273 MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name; 274 MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 275 MainLoop.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName; 276 } 277 private void ParameterizeStochasticOperator(IOperator op) { 278 if (op is IStochasticOperator) 279 ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName; 280 } 281 private void ParameterizeSelectors() { 282 foreach (ISelector selector in SelectorParameter.ValidValues) { 283 selector.CopySelected = new BoolValue(true); 284 selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * PopulationSizeParameter.Value.Value); 285 ParameterizeStochasticOperator(selector); 286 } 287 if (Problem != null) { 288 foreach (IMultiObjectiveSelector selector in SelectorParameter.ValidValues.OfType<IMultiObjectiveSelector>()) { 289 selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 290 selector.QualitiesParameter.ActualName = Problem.Evaluator.QualitiesParameter.ActualName; 291 } 292 } 293 } 294 private void ParameterizeAnalyzers() { 295 // TODO: Parameterize Analyzers 296 } 297 private void ParameterizeIterationBasedOperators() { 298 if (Problem != null) { 299 foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) { 300 op.IterationsParameter.ActualName = "Generations"; 301 op.MaximumIterationsParameter.ActualName = "MaximumGenerations"; 302 } 303 } 304 } 305 private void UpdateCrossovers() { 306 ICrossover oldCrossover = CrossoverParameter.Value; 307 CrossoverParameter.ValidValues.Clear(); 308 foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) 309 CrossoverParameter.ValidValues.Add(crossover); 310 if (oldCrossover != null) { 311 ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType()); 312 if (crossover != null) CrossoverParameter.Value = crossover; 313 } 314 } 315 private void UpdateMutators() { 316 IManipulator oldMutator = MutatorParameter.Value; 317 MutatorParameter.ValidValues.Clear(); 318 foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) 319 MutatorParameter.ValidValues.Add(mutator); 320 if (oldMutator != null) { 321 IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType()); 322 if (mutator != null) MutatorParameter.Value = mutator; 323 } 324 } 325 private void UpdateAnalyzers() { 326 Analyzer.Operators.Clear(); 327 if (Problem != null) { 328 foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) { 329 foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>()) 330 param.Depth = 1; 331 Analyzer.Operators.Add(analyzer); 332 } 333 } 334 } 242 335 #endregion 243 336 }
Note: See TracChangeset
for help on using the changeset viewer.