Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/07/10 02:08:30 (15 years ago)
Author:
abeham
Message:

#893

  • fixed wiring in the algorithms
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.GeneticAlgorithm/3.3/IslandGeneticAlgorithm.cs

    r3673 r3689  
    185185      set { IslandAnalyzerParameter.Value = value; }
    186186    }
    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;
    194187    private RandomCreator RandomCreator {
    195188      get { return (RandomCreator)OperatorGraph.InitialOperator; }
     
    204197      get { return (IslandGeneticAlgorithmMainLoop)IslandProcessor.Successor; }
    205198    }
     199    [Storable]
    206200    private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
     201    [Storable]
    207202    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
    208203    #endregion
     
    272267      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
    273268      mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
    274 
    275269      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();
    276291
    277292      Initialize();
     
    280295    public override IDeepCloneable Clone(Cloner cloner) {
    281296      IslandGeneticAlgorithm clone = (IslandGeneticAlgorithm)base.Clone(cloner);
     297      clone.islandQualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(islandQualityAnalyzer);
     298      clone.qualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(qualityAnalyzer);
    282299      clone.Initialize();
    283300      return clone;
     
    356373    [StorableHook(HookType.AfterDeserialization)]
    357374    private void Initialize() {
    358       InitializeSelectors();
    359       InitializeAnalyzers();
    360       UpdateSelectors();
    361       UpdateAnalyzers();
    362       InitializeMigrators();
    363       UpdateMigrators();
    364375      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
    365376      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
     
    369380      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
    370381      if (Problem != null) {
    371         UpdateCrossovers();
    372         UpdateMutators();
    373382        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
    374383      }
     
    388397        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
    389398    }
    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     }
    409399    private void ParameterizeSelectors() {
    410       foreach (ISelector selector in Selectors) {
     400      foreach (ISelector selector in SelectorParameter.ValidValues) {
    411401        selector.CopySelected = new BoolValue(true);
    412402        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
    413403        ParameterizeStochasticOperator(selector);
    414404      }
    415       foreach (ISelector selector in emigrantsSelectors) {
     405      foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
    416406        selector.CopySelected = new BoolValue(true);
    417407        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
    418408        ParameterizeStochasticOperator(selector);
    419409      }
    420       foreach (IReplacer replacer in immigrationReplacers) {
     410      foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
    421411        ParameterizeStochasticOperator(replacer);
    422412      }
    423413      if (Problem != null) {
    424         foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
     414        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
    425415          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    426416          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    427417        }
    428         foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {
     418        foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
    429419          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    430420          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    431421        }
    432         foreach (ISingleObjectiveReplacer selector in immigrationReplacers.OfType<ISingleObjectiveReplacer>()) {
     422        foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
    433423          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    434424          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     
    450440        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
    451441      }
    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();
    494442    }
    495443    private void UpdateCrossovers() {
     
    519467      Analyzer.Operators.Add(qualityAnalyzer);
    520468      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         }*/
    526469        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
    527470          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
Note: See TracChangeset for help on using the changeset viewer.