Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/23/10 06:50:50 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on algorithms
Location:
trunk/sources/HeuristicLab.SGA/3.3
Files:
3 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.SGA/3.3/SGA.cs

    r2851 r2852  
    2020#endregion
    2121
    22 using System;
    2322using HeuristicLab.Core;
    24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 using HeuristicLab.Collections;
    26 using HeuristicLab.Parameters;
    2723using HeuristicLab.Data;
     24using HeuristicLab.Evolutionary;
    2825using HeuristicLab.Operators;
    2926using HeuristicLab.Optimization;
    30 using HeuristicLab.Evolutionary;
     27using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using System;
    3130
    3231namespace HeuristicLab.SGA {
     
    3635  [Item("SGA", "A standard genetic algorithm.")]
    3736  [Creatable("Algorithms")]
    38   [EmptyStorableClass]
    3937  public sealed class SGA : EngineAlgorithm {
     38    [Storable]
     39    private PopulationCreator populationCreator;
     40    [Storable]
     41    private SGAOperator sgaOperator;
     42
     43    public override Type ProblemType {
     44      get { return typeof(ISingleObjectiveProblem); }
     45    }
     46    public new ISingleObjectiveProblem Problem {
     47      get { return (ISingleObjectiveProblem)base.Problem; }
     48      set { base.Problem = value; }
     49    }
     50
    4051    public new IScope GlobalScope {
    4152      get { return base.GlobalScope; }
     
    4455    public SGA()
    4556      : base() {
    46 //      Parameters.Add(new ValueLookupParameter<BoolData>("Maximization", "True if the problem is a maximization problem, otherwise false."));
    47 //      Parameters.Add(new SubScopesLookupParameter<DoubleData>("Quality", "The value which represents the quality of a solution."));
    4857      Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
    4958      Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
     
    5261      Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
    5362      Parameters.Add(new OperatorParameter("MutationOperator", "The operator used to mutate solutions."));
    54 //      Parameters.Add(new ValueLookupParameter<IOperator>("SolutionEvaluator", "The operator used to evaluate solutions."));
    5563      Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
    5664      Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
    5765
    5866      RandomCreator randomCreator = new RandomCreator();
    59       PopulationCreator populationCreator = new PopulationCreator();
    60       SGAOperator sgaOperator = new SGAOperator();
     67      populationCreator = new PopulationCreator();
     68      sgaOperator = new SGAOperator();
    6169
    6270      randomCreator.RandomParameter.ActualName = "Random";
     
    6977      populationCreator.PopulationSizeParameter.ActualName = "PopulationSize";
    7078      populationCreator.PopulationSizeParameter.Value = null;
    71       populationCreator.SolutionCreatorParameter.ActualName = "SolutionCreator";
    72       populationCreator.SolutionEvaluatorParameter.ActualName = "SolutionEvaluator";
    7379      populationCreator.Successor = sgaOperator;
    7480
    7581      sgaOperator.CrossoverOperatorParameter.ActualName = "CrossoverOperator";
    7682      sgaOperator.ElitesParameter.ActualName = "Elites";
    77       sgaOperator.MaximizationParameter.ActualName = "Maximization";
    7883      sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations";
    7984      sgaOperator.MutationOperatorParameter.ActualName = "MutationOperator";
    8085      sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability";
    81       sgaOperator.QualityParameter.ActualName = "Quality";
    8286      sgaOperator.RandomParameter.ActualName = "Random";
    83       sgaOperator.SolutionEvaluatorParameter.ActualName = "SolutionEvaluator";
    8487
    8588      OperatorGraph.InitialOperator = randomCreator;
    8689    }
     90
     91    public override IDeepCloneable Clone(Cloner cloner) {
     92      SGA clone = (SGA)base.Clone(cloner);
     93      clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
     94      clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
     95      return clone;
     96    }
     97
     98    protected override void DeregisterProblemEvents() {
     99      Problem.MaximizationChanged -= new EventHandler(Problem_MaximizationChanged);
     100      base.DeregisterProblemEvents();
     101    }
     102    protected override void RegisterProblemEvents() {
     103      base.RegisterProblemEvents();
     104      Problem.MaximizationChanged += new EventHandler(Problem_MaximizationChanged);
     105    }
     106
     107    protected override void OnProblemChanged() {
     108      Problem.SolutionCreator.RandomParameter.ActualName = "Random";
     109      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
     110      populationCreator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
     111      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
     112      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
     113      sgaOperator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
     114      base.OnProblemChanged();
     115    }
     116    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
     117      Problem.SolutionCreator.RandomParameter.ActualName = "Random";
     118      populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
     119      base.Problem_SolutionCreatorChanged(sender, e);
     120    }
     121    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     122      populationCreator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
     123      sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
     124      sgaOperator.SolutionEvaluatorParameter.Value = Problem.Evaluator;
     125      base.Problem_EvaluatorChanged(sender, e);
     126    }
     127    private void Problem_MaximizationChanged(object sender, EventArgs e) {
     128      sgaOperator.MaximizationParameter.Value = Problem.Maximization;
     129    }
    87130  }
    88131}
Note: See TracChangeset for help on using the changeset viewer.