source: trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs @ 3445

Last change on this file since 3445 was 3445, checked in by abeham, 12 years ago

Updated StochasticMultiOperator and Multicrossover/Multimutation #976

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Operators {
32  /// <summary>
33  /// A base class for operators which apply arbitrary many other operators of a specific type.
34  /// </summary>
35  [Item("MultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
36  [StorableClass]
37  public abstract class MultiOperator<T> : SingleSuccessorOperator where T : class, IOperator {
38    private List<IValueParameter<T>> operatorParameters;
39
40    [Storable]
41    private ItemList<T> operators;
42    public ItemList<T> Operators {
43      get { return operators; }
44    }
45
46    public MultiOperator()
47      : base() {
48      operators = new ItemList<T>();
49      Initialize();
50    }
51    [StorableConstructor]
52    protected MultiOperator(bool deserializing) : base(deserializing) { }
53
54    [StorableHook(HookType.AfterDeserialization)]
55    private void Initialize() {
56      if (operators != null) RegisterOperatorsEvents();
57      operatorParameters = new List<IValueParameter<T>>();
58      for (int i = 0; i < Operators.Count; i++) {
59        IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
60        operatorParameters.Add(opParam);
61        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
62      }
63    }
64
65    public override IDeepCloneable Clone(Cloner cloner) {
66      MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
67      clone.operators = (ItemList<T>)cloner.Clone(operators);
68      clone.Initialize();
69      return clone;
70    }
71
72    private void UpdateOperatorParameters() {
73      foreach (IValueParameter<T> opParam in operatorParameters) {
74        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
75        Parameters.Remove(opParam.Name);
76      }
77      operatorParameters.Clear();
78      for (int i = 0; i < Operators.Count; i++) {
79        IValueParameter<T> opParam = new ValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
80        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
81        Parameters.Add(opParam);
82        operatorParameters.Add(opParam);
83      }
84    }
85
86    #region Events
87    private void RegisterOperatorsEvents() {
88      operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
89      operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
90      operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
91      operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
92      operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
93    }
94    private void DeregisterOperatorsEvents() {
95      operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
96      operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
97      operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
98      operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
99      operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
100    }
101    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
102      UpdateOperatorParameters();
103    }
104    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
105      UpdateOperatorParameters();
106    }
107    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
108      foreach (IndexedItem<T> item in e.Items)
109        operatorParameters[item.Index].Value = item.Value;
110    }
111    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
112      foreach (IndexedItem<T> item in e.Items)
113        operatorParameters[item.Index].Value = item.Value;
114    }
115    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
116      UpdateOperatorParameters();
117    }
118    private void opParam_ValueChanged(object sender, EventArgs e) {
119      IValueParameter<T> opParam = (IValueParameter<T>)sender;
120      operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
121    }
122    #endregion
123  }
124}
Note: See TracBrowser for help on using the repository browser.