Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2986


Ignore:
Timestamp:
03/10/10 03:37:36 (15 years ago)
Author:
swagner
Message:

Worked on linkage between algorithms and problems (#898)

  • finished SGA
Location:
trunk/sources
Files:
3 edited

Legend:

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

    r2975 r2986  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Core;
     
    3738  [Item("SGA", "A standard genetic algorithm.")]
    3839  [Creatable("Algorithms")]
     40  [EmptyStorableClass]
    3941  public sealed class SGA : EngineAlgorithm {
    40     //#region Private Members
    41     //// store operator references in order to be able to access them easily after deserialization
    42     //[Storable]
    43     //private PopulationCreator populationCreator;
    44     //[Storable]
    45     //private SGAOperator sgaOperator;
    46     //#endregion
    47 
    48     //#region Problem Properties
    49     //public override Type ProblemType {
    50     //  get { return typeof(ISingleObjectiveProblem); }
    51     //}
    52     //public new ISingleObjectiveProblem Problem {
    53     //  get { return (ISingleObjectiveProblem)base.Problem; }
    54     //  set { base.Problem = value; }
    55     //}
    56     //#endregion
    57 
    58     //#region Parameter Properties
    59     //private ValueParameter<IntData> SeedParameter {
    60     //  get { return (ValueParameter<IntData>)Parameters["Seed"]; }
    61     //}
    62     //private ValueParameter<BoolData> SetSeedRandomlyParameter {
    63     //  get { return (ValueParameter<BoolData>)Parameters["SetSeedRandomly"]; }
    64     //}
    65     //private ValueParameter<IntData> PopulationSizeParameter {
    66     //  get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; }
    67     //}
    68     //private ConstrainedValueParameter<ISelector> SelectorParameter {
    69     //  get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
    70     //}
    71     //private ConstrainedValueParameter<ICrossover> CrossoverParameter {
    72     //  get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
    73     //}
    74     //private ValueParameter<DoubleData> MutationProbabilityParameter {
    75     //  get { return (ValueParameter<DoubleData>)Parameters["MutationProbability"]; }
    76     //}
    77     //private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
    78     //  get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
    79     //}
    80     //private ValueParameter<IntData> ElitesParameter {
    81     //  get { return (ValueParameter<IntData>)Parameters["Elites"]; }
    82     //}
    83     //private ValueParameter<IntData> MaximumGenerationsParameter {
    84     //  get { return (ValueParameter<IntData>)Parameters["MaximumGenerations"]; }
    85     //}
    86     //#endregion
    87 
    88     //#region Properties
    89     //public IntData Seed {
    90     //  get { return SeedParameter.Value; }
    91     //  set { SeedParameter.Value = value; }
    92     //}
    93     //public BoolData SetSeedRandomly {
    94     //  get { return SetSeedRandomlyParameter.Value; }
    95     //  set { SetSeedRandomlyParameter.Value = value; }
    96     //}
    97     //public IntData PopulationSize {
    98     //  get { return PopulationSizeParameter.Value; }
    99     //  set { PopulationSizeParameter.Value = value; }
    100     //}
    101     //public ISelector Selector {
    102     //  get { return SelectorParameter.Value; }
    103     //  set { SelectorParameter.Value = value; }
    104     //}
    105     //public ICrossover Crossover {
    106     //  get { return CrossoverParameter.Value; }
    107     //  set { CrossoverParameter.Value = value; }
    108     //}
    109     //public DoubleData MutationProbability {
    110     //  get { return MutationProbabilityParameter.Value; }
    111     //  set { MutationProbabilityParameter.Value = value; }
    112     //}
    113     //public IManipulator Mutator {
    114     //  get { return MutatorParameter.Value; }
    115     //  set { MutatorParameter.Value = value; }
    116     //}
    117     //public IntData Elites {
    118     //  get { return ElitesParameter.Value; }
    119     //  set { ElitesParameter.Value = value; }
    120     //}
    121     //public IntData MaximumGenerations {
    122     //  get { return MaximumGenerationsParameter.Value; }
    123     //  set { MaximumGenerationsParameter.Value = value; }
    124     //}
    125     //#endregion
    126 
    127     //#region Persistence Properties
    128     //[Storable]
    129     //private object RestoreEvents {
    130     //  get { return null; }
    131     //  set { RegisterEvents(); }
    132     //}
    133     //#endregion
    134 
    135     //public SGA()
    136     //  : base() {
    137     //  Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
    138     //  Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
    139     //  Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100)));
    140     //  Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
    141     //  Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
    142     //  Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
    143     //  Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
    144     //  Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
    145     //  Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
    146 
    147     //  RandomCreator randomCreator = new RandomCreator();
    148     //  populationCreator = new PopulationCreator();
    149     //  sgaOperator = new SGAOperator();
    150 
    151     //  randomCreator.RandomParameter.ActualName = "Random";
    152     //  randomCreator.SeedParameter.ActualName = "Seed";
    153     //  randomCreator.SeedParameter.Value = null;
    154     //  randomCreator.SetSeedRandomlyParameter.ActualName = "SetSeedRandomly";
    155     //  randomCreator.SetSeedRandomlyParameter.Value = null;
    156     //  randomCreator.Successor = populationCreator;
    157 
    158     //  populationCreator.PopulationSizeParameter.ActualName = "PopulationSize";
    159     //  populationCreator.PopulationSizeParameter.Value = null;
    160     //  populationCreator.Successor = sgaOperator;
    161 
    162     //  sgaOperator.SelectorParameter.ActualName = "Selector";
    163     //  sgaOperator.CrossoverParameter.ActualName = "Crossover";
    164     //  sgaOperator.ElitesParameter.ActualName = "Elites";
    165     //  sgaOperator.MaximumGenerationsParameter.ActualName = "MaximumGenerations";
    166     //  sgaOperator.MutatorParameter.ActualName = "Mutator";
    167     //  sgaOperator.MutationProbabilityParameter.ActualName = "MutationProbability";
    168     //  sgaOperator.RandomParameter.ActualName = "Random";
    169     //  sgaOperator.ResultsParameter.ActualName = "Results";
    170 
    171     //  OperatorGraph.InitialOperator = randomCreator;
    172 
    173     //  if (ApplicationManager.Manager != null) {
    174     //    var selectors = ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name);
    175     //    foreach (ISelector selector in selectors)
    176     //      SelectorParameter.ValidValues.Add(selector);
    177     //    ParameterizeSelectors();
    178     //  }
    179 
    180     //  RegisterEvents();
    181     //}
    182 
    183     //public override IDeepCloneable Clone(Cloner cloner) {
    184     //  SGA clone = (SGA)base.Clone(cloner);
    185     //  clone.populationCreator = (PopulationCreator)cloner.Clone(populationCreator);
    186     //  clone.sgaOperator = (SGAOperator)cloner.Clone(sgaOperator);
    187     //  clone.RegisterEvents();
    188     //  return clone;
    189     //}
    190 
    191     //#region Events
    192     //protected override void OnProblemChanged() {
    193     //  if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
    194     //  if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
    195     //  foreach (IStochasticOperator op in Problem.Operators.OfType<IStochasticOperator>())
    196     //    op.RandomParameter.ActualName = "Random";
    197 
    198     //  populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
    199     //  populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
    200     //  sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    201     //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    202     //  sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
    203 
    204     //  foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
    205     //    op.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    206     //    op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    207     //  }
    208 
    209     //  ICrossover oldCrossover = CrossoverParameter.Value;
    210     //  CrossoverParameter.ValidValues.Clear();
    211     //  foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
    212     //    CrossoverParameter.ValidValues.Add(crossover);
    213     //  if (oldCrossover != null) {
    214     //    CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
    215     //  }
    216 
    217     //  IManipulator oldMutator = MutatorParameter.Value;
    218     //  MutatorParameter.ValidValues.Clear();
    219     //  foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
    220     //    MutatorParameter.ValidValues.Add(mutator);
    221     //  if (oldMutator != null) {
    222     //    MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
    223     //  }
    224 
    225     //  base.OnProblemChanged();
    226     //}
    227     //protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
    228     //  if (Problem.SolutionCreator is IStochasticOperator) ((IStochasticOperator)Problem.SolutionCreator).RandomParameter.ActualName = "Random";
    229     //  populationCreator.SolutionCreatorParameter.Value = Problem.SolutionCreator;
    230     //  base.Problem_SolutionCreatorChanged(sender, e);
    231     //}
    232     //protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
    233     //  if (Problem.Evaluator is IStochasticOperator) ((IStochasticOperator)Problem.Evaluator).RandomParameter.ActualName = "Random";
    234 
    235     //  foreach (ISingleObjectiveSelector op in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
    236     //    op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    237     //  }
    238 
    239     //  populationCreator.EvaluatorParameter.Value = Problem.Evaluator;
    240     //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    241     //  sgaOperator.EvaluatorParameter.Value = Problem.Evaluator;
    242     //  base.Problem_EvaluatorChanged(sender, e);
    243     //}
    244     //private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
    245     //  foreach (ISelector selector in SelectorParameter.ValidValues)
    246     //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
    247     //}
    248     //private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
    249     //  foreach (ISelector selector in SelectorParameter.ValidValues)
    250     //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
    251     //}
    252     //#endregion
    253 
    254     //#region Helpers
    255     //private void RegisterEvents() {
    256     //  PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
    257     //  ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
    258     //}
    259     //private void ParameterizeSelectors() {
    260     //  foreach (ISelector selector in SelectorParameter.ValidValues) {
    261     //    selector.CopySelected = new BoolData(true);
    262     //    selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
    263     //    if (selector is IStochasticOperator) ((IStochasticOperator)selector).RandomParameter.ActualName = "Random";
    264     //  }
    265     //}
    266     //private void ParameterizePopulationCreator() {
    267     //  populationCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
    268     //  populationCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    269     //}
    270     //private void ParameterizeSGAOperator() {
    271     //  sgaOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
    272     //  sgaOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
    273     //  sgaOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
    274     //}
    275     //private void ParameterizeSolutionCreator() {
    276     //}
    277     //#endregion
     42    #region Problem Properties
     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    #endregion
     51
     52    #region Parameter Properties
     53    private ValueParameter<IntData> SeedParameter {
     54      get { return (ValueParameter<IntData>)Parameters["Seed"]; }
     55    }
     56    private ValueParameter<BoolData> SetSeedRandomlyParameter {
     57      get { return (ValueParameter<BoolData>)Parameters["SetSeedRandomly"]; }
     58    }
     59    private ValueParameter<IntData> PopulationSizeParameter {
     60      get { return (ValueParameter<IntData>)Parameters["PopulationSize"]; }
     61    }
     62    private ConstrainedValueParameter<ISelector> SelectorParameter {
     63      get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
     64    }
     65    private ConstrainedValueParameter<ICrossover> CrossoverParameter {
     66      get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
     67    }
     68    private ValueParameter<DoubleData> MutationProbabilityParameter {
     69      get { return (ValueParameter<DoubleData>)Parameters["MutationProbability"]; }
     70    }
     71    private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
     72      get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
     73    }
     74    private ValueParameter<IntData> ElitesParameter {
     75      get { return (ValueParameter<IntData>)Parameters["Elites"]; }
     76    }
     77    private ValueParameter<IntData> MaximumGenerationsParameter {
     78      get { return (ValueParameter<IntData>)Parameters["MaximumGenerations"]; }
     79    }
     80    #endregion
     81
     82    #region Properties
     83    public IntData Seed {
     84      get { return SeedParameter.Value; }
     85      set { SeedParameter.Value = value; }
     86    }
     87    public BoolData SetSeedRandomly {
     88      get { return SetSeedRandomlyParameter.Value; }
     89      set { SetSeedRandomlyParameter.Value = value; }
     90    }
     91    public IntData PopulationSize {
     92      get { return PopulationSizeParameter.Value; }
     93      set { PopulationSizeParameter.Value = value; }
     94    }
     95    public ISelector Selector {
     96      get { return SelectorParameter.Value; }
     97      set { SelectorParameter.Value = value; }
     98    }
     99    public ICrossover Crossover {
     100      get { return CrossoverParameter.Value; }
     101      set { CrossoverParameter.Value = value; }
     102    }
     103    public DoubleData MutationProbability {
     104      get { return MutationProbabilityParameter.Value; }
     105      set { MutationProbabilityParameter.Value = value; }
     106    }
     107    public IManipulator Mutator {
     108      get { return MutatorParameter.Value; }
     109      set { MutatorParameter.Value = value; }
     110    }
     111    public IntData Elites {
     112      get { return ElitesParameter.Value; }
     113      set { ElitesParameter.Value = value; }
     114    }
     115    public IntData MaximumGenerations {
     116      get { return MaximumGenerationsParameter.Value; }
     117      set { MaximumGenerationsParameter.Value = value; }
     118    }
     119    private RandomCreator RandomCreator {
     120      get { return (RandomCreator)OperatorGraph.InitialOperator; }
     121    }
     122    private PopulationCreator PopulationCreator {
     123      get { return (PopulationCreator)RandomCreator.Successor; }
     124    }
     125    private SGAOperator SGAOperator {
     126      get { return (SGAOperator)PopulationCreator.Successor; }
     127    }
     128    private List<ISelector> selectors;
     129    private IEnumerable<ISelector> Selectors {
     130      get { return selectors; }
     131    }
     132    #endregion
     133
     134    public SGA()
     135      : base() {
     136      Parameters.Add(new ValueParameter<IntData>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntData(0)));
     137      Parameters.Add(new ValueParameter<BoolData>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolData(true)));
     138      Parameters.Add(new ValueParameter<IntData>("PopulationSize", "The size of the population of solutions.", new IntData(100)));
     139      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
     140      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
     141      Parameters.Add(new ValueParameter<DoubleData>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new DoubleData(0.05)));
     142      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
     143      Parameters.Add(new ValueParameter<IntData>("Elites", "The numer of elite solutions which are kept in each generation.", new IntData(1)));
     144      Parameters.Add(new ValueParameter<IntData>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntData(1000)));
     145
     146      RandomCreator randomCreator = new RandomCreator();
     147      PopulationCreator populationCreator = new PopulationCreator();
     148      SGAOperator sgaOperator = new SGAOperator();
     149      OperatorGraph.InitialOperator = randomCreator;
     150
     151      randomCreator.RandomParameter.ActualName = "Random";
     152      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
     153      randomCreator.SeedParameter.Value = null;
     154      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
     155      randomCreator.SetSeedRandomlyParameter.Value = null;
     156      randomCreator.Successor = populationCreator;
     157
     158      populationCreator.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
     159      populationCreator.Successor = sgaOperator;
     160
     161      sgaOperator.SelectorParameter.ActualName = SelectorParameter.Name;
     162      sgaOperator.CrossoverParameter.ActualName = CrossoverParameter.Name;
     163      sgaOperator.ElitesParameter.ActualName = ElitesParameter.Name;
     164      sgaOperator.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
     165      sgaOperator.MutatorParameter.ActualName = MutatorParameter.Name;
     166      sgaOperator.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
     167      sgaOperator.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
     168      sgaOperator.ResultsParameter.ActualName = "Results";
     169
     170      Initialze();
     171    }
     172    [StorableConstructor]
     173    private SGA(bool deserializing) : base() { }
     174
     175    public override IDeepCloneable Clone(Cloner cloner) {
     176      SGA clone = (SGA)base.Clone(cloner);
     177      clone.Initialze();
     178      return clone;
     179    }
     180
     181    #region Events
     182    protected override void OnProblemChanged() {
     183      ParameterizeStochasticOperator(Problem.SolutionCreator);
     184      ParameterizeStochasticOperator(Problem.Evaluator);
     185      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
     186      ParameterizePopulationCreator();
     187      ParameterizeSGAOperator();
     188      ParameterizeSelectors();
     189      UpdateCrossovers();
     190      UpdateMutators();
     191      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     192      base.OnProblemChanged();
     193    }
     194    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
     195      ParameterizeStochasticOperator(Problem.SolutionCreator);
     196      ParameterizePopulationCreator();
     197      base.Problem_SolutionCreatorChanged(sender, e);
     198    }
     199    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
     200      ParameterizeStochasticOperator(Problem.Evaluator);
     201      ParameterizePopulationCreator();
     202      ParameterizeSGAOperator();
     203      ParameterizeSelectors();
     204      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     205      base.Problem_EvaluatorChanged(sender, e);
     206    }
     207    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
     208      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
     209      UpdateCrossovers();
     210      UpdateMutators();
     211      base.Problem_OperatorsChanged(sender, e);
     212    }
     213    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
     214      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
     215      ParameterizeSelectors();
     216    }
     217    private void Elites_ValueChanged(object sender, EventArgs e) {
     218      ParameterizeSelectors();
     219    }
     220    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
     221      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
     222      ParameterizeSelectors();
     223    }
     224    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
     225      ParameterizeSelectors();
     226    }
     227    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
     228      ParameterizeSGAOperator();
     229      ParameterizeSelectors();
     230    }
     231    #endregion
     232
     233    #region Helpers
     234    [StorableHook(HookType.AfterDeserialization)]
     235    private void Initialze() {
     236      InitializeSelectors();
     237      UpdateSelectors();
     238      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
     239      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
     240      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
     241      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
     242      if (Problem != null)
     243        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
     244    }
     245
     246    private void ParameterizePopulationCreator() {
     247      PopulationCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     248      PopulationCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
     249    }
     250    private void ParameterizeSGAOperator() {
     251      SGAOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
     252      SGAOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     253      SGAOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     254    }
     255    private void ParameterizeStochasticOperator(IOperator op) {
     256      if (op is IStochasticOperator)
     257        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
     258    }
     259    private void InitializeSelectors() {
     260      selectors = new List<ISelector>();
     261      if (ApplicationManager.Manager != null) {
     262        selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
     263        ParameterizeSelectors();
     264      }
     265    }
     266    private void ParameterizeSelectors() {
     267      foreach (ISelector selector in Selectors) {
     268        selector.CopySelected = new BoolData(true);
     269        selector.NumberOfSelectedSubScopesParameter.Value = new IntData(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
     270        ParameterizeStochasticOperator(selector);
     271      }
     272      if (Problem != null) {
     273        foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
     274          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
     275          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
     276        }
     277      }
     278    }
     279    private void UpdateSelectors() {
     280      if (ApplicationManager.Manager != null) {
     281        ISelector oldSelector = SelectorParameter.Value;
     282        SelectorParameter.ValidValues.Clear();
     283        foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
     284          SelectorParameter.ValidValues.Add(selector);
     285        if (oldSelector != null)
     286          SelectorParameter.Value = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
     287      }
     288    }
     289    private void UpdateCrossovers() {
     290      ICrossover oldCrossover = CrossoverParameter.Value;
     291      CrossoverParameter.ValidValues.Clear();
     292      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
     293        CrossoverParameter.ValidValues.Add(crossover);
     294      if (oldCrossover != null)
     295        CrossoverParameter.Value = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
     296    }
     297    private void UpdateMutators() {
     298      IManipulator oldMutator = MutatorParameter.Value;
     299      MutatorParameter.ValidValues.Clear();
     300      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
     301        MutatorParameter.ValidValues.Add(mutator);
     302      if (oldMutator != null)
     303        MutatorParameter.Value = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
     304    }
     305    #endregion
    278306  }
    279307}
  • trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs

    r2975 r2986  
    3636  [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")]
    3737  [Creatable("Problems")]
     38  [EmptyStorableClass]
    3839  public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem {
    3940    public override Image ItemImage {
     
    9495      set { BestKnownQualityParameter.Value = value; }
    9596    }
    96     private OperatorSet operators;
     97    private List<IPermutationOperator> operators;
    9798    public IEnumerable<IOperator> Operators {
    98       get {
    99         if (operators == null) InitializeOperators();
    100         return operators;
    101       }
    102     }
    103     #endregion
    104 
    105     #region Persistence Properties
    106     [Storable]
    107     private object RestoreEvents {
    108       get { return null; }
    109       set { RegisterEvents(); }
     99      get { return operators.Cast<IOperator>(); }
    110100    }
    111101    #endregion
     
    127117      ParameterizeEvaluator();
    128118
    129       RegisterEvents();
    130     }
     119      Initialize();
     120    }
     121    [StorableConstructor]
     122    private TSP(bool deserializing) : base() { }
    131123
    132124    public override IDeepCloneable Clone(Cloner cloner) {
    133125      TSP clone = (TSP)base.Clone(cloner);
    134       clone.RegisterEvents();
     126      clone.Initialize();
    135127      return clone;
    136128    }
     
    187179
    188180    #region Helpers
    189     private void RegisterEvents() {
     181    [StorableHook(HookType.AfterDeserialization)]
     182    private void Initialize() {
     183      InitializeOperators();
    190184      CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);
    191185      Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);
     
    205199    }
    206200    private void InitializeOperators() {
    207       operators = new OperatorSet();
     201      operators = new List<IPermutationOperator>();
    208202      if (ApplicationManager.Manager != null) {
    209         foreach (IPermutationOperator op in ApplicationManager.Manager.GetInstances<IPermutationOperator>())
    210           operators.Add(op);
     203        operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>());
    211204        ParameterizeOperators();
    212205      }
Note: See TracChangeset for help on using the changeset viewer.