Changeset 3689 for trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm
- Timestamp:
- 05/07/10 02:08:30 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs
r3673 r3689 185 185 set { IslandAnalyzerParameter.Value = value; } 186 186 } 187 private List<ISelector> selectors;188 private IEnumerable<ISelector> Selectors {189 get { return selectors; }190 }191 private List<ISelector> emigrantsSelectors;192 private List<IReplacer> immigrationReplacers;193 private List<IMigrator> migrators;194 187 private RandomCreator RandomCreator { 195 188 get { return (RandomCreator)OperatorGraph.InitialOperator; } … … 204 197 get { return (IslandGeneticAlgorithmMainLoop)IslandProcessor.Successor; } 205 198 } 199 [Storable] 206 200 private BestAverageWorstQualityAnalyzer islandQualityAnalyzer; 201 [Storable] 207 202 private BestAverageWorstQualityAnalyzer qualityAnalyzer; 208 203 #endregion … … 272 267 mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name; 273 268 mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name; 274 275 269 mainLoop.Successor = null; 270 271 foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name)) 272 SelectorParameter.ValidValues.Add(selector); 273 ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector")); 274 if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector; 275 276 foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name)) 277 EmigrantsSelectorParameter.ValidValues.Add(selector); 278 279 foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name)) 280 ImmigrationReplacerParameter.ValidValues.Add(replacer); 281 282 ParameterizeSelectors(); 283 284 foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) 285 MigratorParameter.ValidValues.Add(migrator); 286 287 qualityAnalyzer = new BestAverageWorstQualityAnalyzer(); 288 islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer(); 289 ParameterizeAnalyzers(); 290 UpdateAnalyzers(); 276 291 277 292 Initialize(); … … 280 295 public override IDeepCloneable Clone(Cloner cloner) { 281 296 IslandGeneticAlgorithm clone = (IslandGeneticAlgorithm)base.Clone(cloner); 297 clone.islandQualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(islandQualityAnalyzer); 298 clone.qualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(qualityAnalyzer); 282 299 clone.Initialize(); 283 300 return clone; … … 356 373 [StorableHook(HookType.AfterDeserialization)] 357 374 private void Initialize() { 358 InitializeSelectors();359 InitializeAnalyzers();360 UpdateSelectors();361 UpdateAnalyzers();362 InitializeMigrators();363 UpdateMigrators();364 375 PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged); 365 376 PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged); … … 369 380 Elites.ValueChanged += new EventHandler(Elites_ValueChanged); 370 381 if (Problem != null) { 371 UpdateCrossovers();372 UpdateMutators();373 382 Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 374 383 } … … 388 397 ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName; 389 398 } 390 private void InitializeSelectors() {391 selectors = new List<ISelector>();392 selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));393 emigrantsSelectors = new List<ISelector>();394 emigrantsSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));395 immigrationReplacers = new List<IReplacer>();396 immigrationReplacers.AddRange(ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name));397 ParameterizeSelectors();398 }399 private void InitializeAnalyzers() {400 islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();401 qualityAnalyzer = new BestAverageWorstQualityAnalyzer();402 ParameterizeAnalyzers();403 }404 private void InitializeMigrators() {405 migrators = new List<IMigrator>();406 migrators.AddRange(ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name));407 UpdateMigrators();408 }409 399 private void ParameterizeSelectors() { 410 foreach (ISelector selector in Selector s) {400 foreach (ISelector selector in SelectorParameter.ValidValues) { 411 401 selector.CopySelected = new BoolValue(true); 412 402 selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value)); 413 403 ParameterizeStochasticOperator(selector); 414 404 } 415 foreach (ISelector selector in emigrantsSelectors) {405 foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) { 416 406 selector.CopySelected = new BoolValue(true); 417 407 selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value)); 418 408 ParameterizeStochasticOperator(selector); 419 409 } 420 foreach (IReplacer replacer in immigrationReplacers) {410 foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) { 421 411 ParameterizeStochasticOperator(replacer); 422 412 } 423 413 if (Problem != null) { 424 foreach (ISingleObjectiveSelector selector in Selector s.OfType<ISingleObjectiveSelector>()) {414 foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 425 415 selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 426 416 selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 427 417 } 428 foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {418 foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) { 429 419 selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 430 420 selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 431 421 } 432 foreach (ISingleObjectiveReplacer selector in immigrationReplacers.OfType<ISingleObjectiveReplacer>()) {422 foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) { 433 423 selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 434 424 selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; … … 450 440 qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name; 451 441 } 452 }453 private void UpdateSelectors() {454 ISelector oldSelector = SelectorParameter.Value;455 SelectorParameter.ValidValues.Clear();456 foreach (ISelector selector in Selectors.OrderBy(x => x.Name))457 SelectorParameter.ValidValues.Add(selector);458 459 ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));460 if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;461 462 if (oldSelector != null) {463 ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());464 if (selector != null) SelectorParameter.Value = selector;465 }466 467 oldSelector = EmigrantsSelector;468 EmigrantsSelectorParameter.ValidValues.Clear();469 foreach (ISelector selector in emigrantsSelectors)470 EmigrantsSelectorParameter.ValidValues.Add(selector);471 if (oldSelector != null) {472 ISelector selector = EmigrantsSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());473 if (selector != null) EmigrantsSelectorParameter.Value = selector;474 }475 476 IReplacer oldReplacer = ImmigrationReplacerParameter.Value;477 ImmigrationReplacerParameter.ValidValues.Clear();478 foreach (IReplacer replacer in immigrationReplacers)479 ImmigrationReplacerParameter.ValidValues.Add(replacer);480 if (oldReplacer != null) {481 IReplacer replacer = ImmigrationReplacerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());482 if (replacer != null) ImmigrationReplacerParameter.Value = replacer;483 }484 }485 private void UpdateMigrators() {486 IMigrator oldMigrator = Migrator;487 MigratorParameter.ValidValues.Clear();488 foreach (IMigrator migrator in migrators)489 MigratorParameter.ValidValues.Add(migrator);490 if (oldMigrator != null) {491 IMigrator migrator = MigratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMigrator.GetType());492 if (migrator != null) MigratorParameter.Value = migrator;493 } else if (MigratorParameter.ValidValues.Count > 0) MigratorParameter.Value = MigratorParameter.ValidValues.First();494 442 } 495 443 private void UpdateCrossovers() { … … 519 467 Analyzer.Operators.Add(qualityAnalyzer); 520 468 if (Problem != null) { 521 /*foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {522 foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())523 param.Depth = 1;524 IslandAnalyzer.Operators.Add(analyzer);525 }*/526 469 foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) { 527 470 foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
Note: See TracChangeset
for help on using the changeset viewer.