Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/19/10 15:24:32 (14 years ago)
Author:
abeham
Message:

#1040

  • Added first possibly working NSGA-II
  • Added Maximization parameter to IMultiObjectiveProblem and IMultiObjectiveSelector
File:
1 edited

Legend:

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

    r4017 r4045  
    3232using HeuristicLab.Optimization.Operators;
    3333using HeuristicLab.PluginInfrastructure;
     34using HeuristicLab.Selection;
     35using HeuristicLab.Operators;
    3436
    3537namespace HeuristicLab.Algorithms.NSGA2 {
     
    131133      get { return (SolutionsCreator)RandomCreator.Successor; }
    132134    }
     135    private RankAndCrowdingSorter RankAndCrowdingSorter {
     136      get { return (RankAndCrowdingSorter)SolutionsCreator.Successor; }
     137    }
    133138    private NSGA2MainLoop MainLoop {
    134       get { return (NSGA2MainLoop)SolutionsCreator.Successor; }
     139      get { return (NSGA2MainLoop)RankAndCrowdingSorter.Successor; }
    135140    }
    136141    #endregion
     
    143148      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
    144149      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)));
    146151      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
    147152      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
     
    152157      RandomCreator randomCreator = new RandomCreator();
    153158      SolutionsCreator solutionsCreator = new SolutionsCreator();
     159      RankAndCrowdingSorter rankAndCrowdingSorter = new RankAndCrowdingSorter();
    154160      NSGA2MainLoop mainLoop = new NSGA2MainLoop();
    155161      OperatorGraph.InitialOperator = randomCreator;
     
    163169
    164170      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;
    167178      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
    168179      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
     
    175186      mainLoop.ResultsParameter.ActualName = "Results";
    176187
    177       foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
     188      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is ISingleObjectiveSelector)).OrderBy(x => x.Name))
    178189        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 selectors
     190      ISelector tournamentSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("CrowdedTournamentSelector"));
     191      if (tournamentSelector != null) SelectorParameter.Value = tournamentSelector;
     192
     193      ParameterizeSelectors();
    183194
    184195      AttachEventHandlers();
     
    193204    #region Events
    194205    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);
    196218      base.OnProblemChanged();
    197219    }
    198220    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
    199       // parameterize SolutionCreator
     221      ParameterizeStochasticOperator(Problem.SolutionCreator);
     222      ParameterizeSolutionsCreator();
    200223      base.Problem_SolutionCreatorChanged(sender, e);
    201224    }
    202225    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
    203       // parameterize StochasticOperators
    204       // parameterize SolutionsCreator
    205       // parameterize MainLoop
    206       // parameterize Selectors
    207       // parameterize Analyzers
     226      ParameterizeStochasticOperator(Problem.Evaluator);
     227      ParameterizeSolutionsCreator();
     228      ParameterizeMainLoop();
     229      ParameterizeSelectors();
     230      ParameterizeAnalyzers();
    208231      Problem.Evaluator.QualitiesParameter.ActualNameChanged += new EventHandler(Evaluator_QualitiesParameter_ActualNameChanged);
    209232      base.Problem_EvaluatorChanged(sender, e);
    210233    }
    211234    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();
    212240      base.Problem_OperatorsChanged(sender, e);
    213241    }
     
    215243      base.Problem_Reset(sender, e);
    216244    }
    217 
    218245    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
    219246      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
    220       // parameterize Selectors
     247      ParameterizeSelectors();
    221248    }
    222249    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
    223       // parameterize Selectors
    224     }
    225 
     250      ParameterizeSelectors();
     251    }
    226252    private void Evaluator_QualitiesParameter_ActualNameChanged(object sender, EventArgs e) {
    227       // parameterize Analyzers
    228       // parameterize MainLoop
    229       // parameterize Selectors
     253      ParameterizeMainLoop();
     254      ParameterizeSelectors();
     255      ParameterizeAnalyzers();
    230256    }
    231257    #endregion
     
    240266      }
    241267    }
     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    }
    242335    #endregion
    243336  }
Note: See TracChangeset for help on using the changeset viewer.