[2773] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2773] | 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 |
|
---|
| 22 | using System;
|
---|
[3211] | 23 | using System.Collections.Generic;
|
---|
[2773] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Collections;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[2773] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Operators {
|
---|
| 32 | /// <summary>
|
---|
[3407] | 33 | /// A base class for operators which apply arbitrary many other operators of a specific type.
|
---|
[2773] | 34 | /// </summary>
|
---|
[3407] | 35 | [Item("MultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
|
---|
[3017] | 36 | [StorableClass]
|
---|
[3407] | 37 | public abstract class MultiOperator<T> : SingleSuccessorOperator where T : class, IOperator {
|
---|
| 38 | private List<IValueParameter<T>> operatorParameters;
|
---|
[2773] | 39 |
|
---|
[3317] | 40 | [Storable]
|
---|
[3591] | 41 | private IItemList<T> operators;
|
---|
| 42 | public IItemList<T> Operators {
|
---|
[2773] | 43 | get { return operators; }
|
---|
[3591] | 44 | protected set {
|
---|
| 45 | if (operators != value) {
|
---|
| 46 | if (value == null) throw new ArgumentException();
|
---|
| 47 | DeregisterOperatorsEvents();
|
---|
| 48 | operators = value;
|
---|
| 49 | RegisterOperatorsEvents();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
[2773] | 52 | }
|
---|
| 53 |
|
---|
[3407] | 54 | public MultiOperator()
|
---|
[2773] | 55 | : base() {
|
---|
[3591] | 56 | this.operators = new ItemList<T>();
|
---|
[3211] | 57 | Initialize();
|
---|
[2773] | 58 | }
|
---|
[3591] | 59 |
|
---|
[3317] | 60 | [StorableConstructor]
|
---|
[3407] | 61 | protected MultiOperator(bool deserializing) : base(deserializing) { }
|
---|
[2773] | 62 |
|
---|
[3211] | 63 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 64 | private void Initialize() {
|
---|
[3317] | 65 | if (operators != null) RegisterOperatorsEvents();
|
---|
[3407] | 66 | operatorParameters = new List<IValueParameter<T>>();
|
---|
[3211] | 67 | for (int i = 0; i < Operators.Count; i++) {
|
---|
[3407] | 68 | IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
|
---|
[3211] | 69 | operatorParameters.Add(opParam);
|
---|
| 70 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[2773] | 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[3407] | 75 | MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
|
---|
[3591] | 76 | clone.operators = (IItemList<T>)cloner.Clone(operators);
|
---|
[3211] | 77 | clone.Initialize();
|
---|
[2773] | 78 | return clone;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | private void UpdateOperatorParameters() {
|
---|
[3407] | 82 | foreach (IValueParameter<T> opParam in operatorParameters) {
|
---|
[2773] | 83 | opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
|
---|
| 84 | Parameters.Remove(opParam.Name);
|
---|
| 85 | }
|
---|
[3211] | 86 | operatorParameters.Clear();
|
---|
[2773] | 87 | for (int i = 0; i < Operators.Count; i++) {
|
---|
[3729] | 88 | IValueParameter<T> opParam = new OptionalValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
|
---|
[2773] | 89 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
| 90 | Parameters.Add(opParam);
|
---|
[3211] | 91 | operatorParameters.Add(opParam);
|
---|
[2773] | 92 | }
|
---|
| 93 | }
|
---|
[3211] | 94 |
|
---|
| 95 | #region Events
|
---|
| 96 | private void RegisterOperatorsEvents() {
|
---|
[3445] | 97 | operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
|
---|
| 98 | operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
|
---|
| 99 | operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
|
---|
| 100 | operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
|
---|
| 101 | operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
|
---|
[3211] | 102 | }
|
---|
| 103 | private void DeregisterOperatorsEvents() {
|
---|
[3445] | 104 | operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
|
---|
| 105 | operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
|
---|
| 106 | operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
|
---|
| 107 | operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
|
---|
| 108 | operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
|
---|
[3211] | 109 | }
|
---|
[3445] | 110 | protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2773] | 111 | UpdateOperatorParameters();
|
---|
| 112 | }
|
---|
[3445] | 113 | protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2773] | 114 | UpdateOperatorParameters();
|
---|
| 115 | }
|
---|
[3445] | 116 | protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[3407] | 117 | foreach (IndexedItem<T> item in e.Items)
|
---|
[3211] | 118 | operatorParameters[item.Index].Value = item.Value;
|
---|
[2773] | 119 | }
|
---|
[3445] | 120 | protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[3407] | 121 | foreach (IndexedItem<T> item in e.Items)
|
---|
[3211] | 122 | operatorParameters[item.Index].Value = item.Value;
|
---|
[2773] | 123 | }
|
---|
[3445] | 124 | protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
|
---|
[2773] | 125 | UpdateOperatorParameters();
|
---|
| 126 | }
|
---|
| 127 | private void opParam_ValueChanged(object sender, EventArgs e) {
|
---|
[3407] | 128 | IValueParameter<T> opParam = (IValueParameter<T>)sender;
|
---|
[3729] | 129 | if (opParam.Value == null)
|
---|
| 130 | operators.RemoveAt(operatorParameters.IndexOf(opParam));
|
---|
| 131 | else
|
---|
| 132 | operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
|
---|
[2773] | 133 | }
|
---|
[3211] | 134 | #endregion
|
---|
[2773] | 135 | }
|
---|
| 136 | }
|
---|