Changeset 3591


Ignore:
Timestamp:
05/03/10 14:30:55 (12 years ago)
Author:
gkronber
Message:

Separated MultiOperator into two classes MultiOperator and CheckedMultiOperator. #992 (CheckedItemList and CheckedItemCollection is necessary)

Location:
trunk/sources
Files:
5 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators.Views/3.3/CheckedMultiOperatorView.Designer.cs

    r3588 r3591  
    2121
    2222namespace HeuristicLab.Operators.Views {
    23   partial class MultiOperatorView<T> {
     23  partial class CheckedMultiOperatorView<T> {
    2424    /// <summary>
    2525    /// Required designer variable.
  • trunk/sources/HeuristicLab.Operators.Views/3.3/CheckedMultiOperatorView.cs

    r3588 r3591  
    2727
    2828namespace HeuristicLab.Operators.Views {
    29   /// <summary>
    30   /// The base class for visual representations of items.
    31   /// </summary>
    32   [View("MultiOperator View")]
    33   [Content(typeof(MultiOperator<>), true)]
    34   public partial class MultiOperatorView<T> : NamedItemView where T : class, IOperator {
    35     public new MultiOperator<T> Content {
    36       get { return (MultiOperator<T>)base.Content; }
     29  [View("CheckedMultiOperator View")]
     30  [Content(typeof(CheckedMultiOperator<>), true)]
     31  public partial class CheckedMultiOperatorView<T> : NamedItemView where T : class, IOperator {
     32    public new CheckedMultiOperator<T> Content {
     33      get { return (CheckedMultiOperator<T>)base.Content; }
    3734      set { base.Content = value; }
    3835    }
    3936
    4037    /// <summary>
    41     /// Initializes a new instance of <see cref="ItemBaseView"/>.
     38    /// Initializes a new instance of <see cref="CheckedMultiOperatorView"/>.
    4239    /// </summary>
    43     public MultiOperatorView() {
     40    public CheckedMultiOperatorView() {
    4441      InitializeComponent();
    4542    }
    4643
    4744    /// <summary>
    48     /// Removes the eventhandlers from the underlying <see cref="IOperatorGraph"/>.
     45    /// Removes the eventhandlers from the underlying <see cref="CheckedMultiOperator"/>.
    4946    /// </summary>
    50     /// <remarks>Calls <see cref="ViewBase.RemoveItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
     47    /// <remarks>Calls <see cref="NamedItemView.DeregisterContentEvents"/> of base class <see cref="NamedItemView"/>.</remarks>
    5148    protected override void DeregisterContentEvents() {
    5249      Content.BreakpointChanged -= new EventHandler(Content_BreakpointChanged);
     
    5552
    5653    /// <summary>
    57     /// Adds eventhandlers to the underlying <see cref="IOperatorGraph"/>.
     54    /// Adds eventhandlers to the underlying <see cref="CheckedMultiOperator"/>.
    5855    /// </summary>
    59     /// <remarks>Calls <see cref="ViewBase.AddItemEvents"/> of base class <see cref="ViewBase"/>.</remarks>
     56    /// <remarks>Calls <see cref="NamedItemView.RegisterContentEvents"/> of base class <see cref="NamedItemView"/>.</remarks>
    6057    protected override void RegisterContentEvents() {
    6158      base.RegisterContentEvents();
  • trunk/sources/HeuristicLab.Operators.Views/3.3/HeuristicLab.Operators.Views-3.3.csproj

    r3437 r3591  
    9292    </Compile>
    9393    <None Include="HeuristicLabOperatorsViewsPlugin.cs.frame" />
     94    <Compile Include="CheckedMultiOperatorView.cs">
     95      <SubType>UserControl</SubType>
     96    </Compile>
     97    <Compile Include="CheckedMultiOperatorView.Designer.cs">
     98      <DependentUpon>CheckedMultiOperatorView.cs</DependentUpon>
     99    </Compile>
    94100    <Compile Include="MultiOperatorView.cs">
    95101      <SubType>UserControl</SubType>
  • trunk/sources/HeuristicLab.Operators.Views/3.3/MultiOperatorView.Designer.cs

    r3568 r3591  
    4747      this.tabControl = new System.Windows.Forms.TabControl();
    4848      this.operatorsTabPage = new System.Windows.Forms.TabPage();
    49       this.operatorListView = new HeuristicLab.Core.Views.CheckedItemListView<T>();
     49      this.operatorListView = new HeuristicLab.Core.Views.ItemListView<T>();
    5050      this.parametersTabPage = new System.Windows.Forms.TabPage();
    5151      this.parameterCollectionView = new HeuristicLab.Core.Views.ParameterCollectionView();
     
    178178    protected System.Windows.Forms.TabPage parametersTabPage;
    179179    protected HeuristicLab.Core.Views.ParameterCollectionView parameterCollectionView;
    180     protected HeuristicLab.Core.Views.CheckedItemListView<T> operatorListView;
     180    protected HeuristicLab.Core.Views.ItemListView<T> operatorListView;
    181181    protected System.Windows.Forms.CheckBox breakpointCheckBox;
    182182    protected System.Windows.Forms.Label breakpointLabel;
  • trunk/sources/HeuristicLab.Operators/3.3/CheckedMultiOperator.cs

    r3588 r3591  
    3131namespace HeuristicLab.Operators {
    3232  /// <summary>
    33   /// A base class for operators which apply arbitrary many other operators of a specific type.
     33  /// A base class for operators which apply arbitrary many other operators of a specific type, operators can be checked/unchecked.
    3434  /// </summary>
    35   [Item("MultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
     35  [Item("CheckedMultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
    3636  [StorableClass]
    37   public abstract class MultiOperator<T> : SingleSuccessorOperator where T : class, IOperator {
    38     private List<IValueParameter<T>> operatorParameters;
    39 
    40     [Storable]
    41     private CheckedItemList<T> operators;
    42     public CheckedItemList<T> Operators {
    43       get { return operators; }
     37  public abstract class CheckedMultiOperator<T> : MultiOperator<T> where T : class, IOperator {
     38    /// <summary>
     39    /// Gets the operators of the checked multi operator
     40    /// </summary>
     41    public new ICheckedItemList<T> Operators {
     42      get { return (ICheckedItemList<T>)base.Operators; }
     43      protected set { base.Operators = value; }
    4444    }
    45 
    46     public MultiOperator()
     45    /// <summary>
     46    /// Creates a new instance of CheckedMultiOperator
     47    /// </summary>
     48    public CheckedMultiOperator()
    4749      : base() {
    48       operators = new CheckedItemList<T>();
    49       Initialize();
     50      Operators = new CheckedItemList<T>();
    5051    }
    5152    [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 = (CheckedItemList<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
     53    protected CheckedMultiOperator(bool deserializing) : base(deserializing) { }
    12354  }
    12455}
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r3425 r3591  
    8585  </ItemGroup>
    8686  <ItemGroup>
     87    <Compile Include="CheckedMultiOperator.cs" />
    8788    <Compile Include="CombinedOperator.cs" />
    8889    <None Include="HeuristicLabOperatorsPlugin.cs.frame" />
  • trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs

    r3568 r3591  
    3939
    4040    [Storable]
    41     private CheckedItemList<T> operators;
    42     public CheckedItemList<T> Operators {
     41    private IItemList<T> operators;
     42    public IItemList<T> Operators {
    4343      get { return operators; }
     44      protected set {
     45        if (operators != value) {
     46          if (value == null) throw new ArgumentException();
     47          DeregisterOperatorsEvents();
     48          operators = value;
     49          RegisterOperatorsEvents();
     50        }
     51      }
    4452    }
    4553
    4654    public MultiOperator()
    4755      : base() {
    48       operators = new CheckedItemList<T>();
     56      this.operators = new ItemList<T>();
    4957      Initialize();
    5058    }
     59
    5160    [StorableConstructor]
    5261    protected MultiOperator(bool deserializing) : base(deserializing) { }
     
    6574    public override IDeepCloneable Clone(Cloner cloner) {
    6675      MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
    67       clone.operators = (CheckedItemList<T>)cloner.Clone(operators);
     76      clone.operators = (IItemList<T>)cloner.Clone(operators);
    6877      clone.Initialize();
    6978      return clone;
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiOperator.cs

    r3568 r3591  
    3636  [Item("StochasticMultiOperator<T>", "Base class for stochastic multi operators.")]
    3737  [StorableClass]
    38   public abstract class StochasticMultiOperator<T> : MultiOperator<T> where T : class, IOperator {
     38  public abstract class StochasticMultiOperator<T> : CheckedMultiOperator<T> where T : class, IOperator {
    3939    /// <summary>
    4040    /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor
     
    122122        throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
    123123      }
     124      IOperator successor = null;
    124125      var checkedOperators = Operators.CheckedItems;
    125       if (checkedOperators.Count() == 0) {
    126         throw new InvalidOperationException(Name + ": At least one operator must be checked.");
    127       }
    128       double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
    129       if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
    130       double r = random.NextDouble() * sum;
    131       sum = 0;
    132       IOperator successor = null;
    133       foreach (var indexedItem in checkedOperators) {
    134         sum += probabilities[indexedItem.Index];
    135         if (sum > r) {
    136           successor = indexedItem.Value;
    137           break;
     126      if (checkedOperators.Count() > 0) {
     127        // select a random operator from the checked operators
     128        double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
     129        if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
     130        double r = random.NextDouble() * sum;
     131        sum = 0;
     132        foreach (var indexedItem in checkedOperators) {
     133          sum += probabilities[indexedItem.Index];
     134          if (sum > r) {
     135            successor = indexedItem.Value;
     136            break;
     137          }
    138138        }
    139139      }
Note: See TracChangeset for help on using the changeset viewer.