Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/ErcOptionConvertible.cs @ 14905

Last change on this file since 14905 was 14905, checked in by pkimmesw, 8 years ago

#2665 Made ErcOptions checkable

File size: 3.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5
6  using HeuristicLab.Common;
7  using HeuristicLab.Core;
8  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9  using HeuristicLab.Problems.ProgramSynthesis.Base.Weighted;
10
11  [StorableClass]
12  public abstract class ErcOptionConvertible<TValue, TValueItem> : CheckedItemCollection<IWeightedErcValueItem<TValueItem>>, IErcValueItem<TValue> {
13    [Storable]
14    protected bool isEnabled;
15
16    public event EventHandler<bool> EnabledChanged;
17
18    protected ErcOptionConvertible() : this(false, new IWeightedErcValueItem<TValueItem>[0]) { }
19
20    protected ErcOptionConvertible(bool isEnabled, params IWeightedErcValueItem<TValueItem>[] values) : base(values) {
21      this.isEnabled = isEnabled;
22      SetEnabledChangedHandler(values);
23
24      foreach (var value in values)
25        SetItemCheckedState(value, value.IsEnabled);
26    }
27
28    [StorableConstructor]
29    protected ErcOptionConvertible(bool deserializing)
30      : base(deserializing) {
31    }
32
33    [StorableHook(HookType.AfterDeserialization)]
34    // ReSharper disable once UnusedMember.Local
35    private void AfterDeserialization() {
36      SetEnabledChangedHandler(list);
37    }
38
39    protected ErcOptionConvertible(ErcOptionConvertible<TValue, TValueItem> origin, Cloner cloner)
40      : base(origin, cloner) {
41      IsEnabled = origin.IsEnabled;
42    }
43
44    private void SetEnabledChangedHandler(IEnumerable<IWeightedErcValueItem<TValueItem>> values) {
45      foreach (var item in values)
46        item.EnabledChanged += ErcValueItemEnabledChanged;
47    }
48
49    protected override void OnItemsAdded(IEnumerable<IWeightedErcValueItem<TValueItem>> items) {
50      base.OnItemsAdded(items);
51      SetEnabledChangedHandler(items);
52    }
53
54    protected override void OnItemsRemoved(IEnumerable<IWeightedErcValueItem<TValueItem>> items) {
55      base.OnItemsRemoved(items);
56
57      foreach (var item in items)
58        item.EnabledChanged -= ErcValueItemEnabledChanged;
59    }
60
61    private void ErcValueItemEnabledChanged(object sender, bool state) {
62      SetItemCheckedState((IWeightedErcValueItem<TValueItem>)sender, state);
63    }
64
65    protected override void OnCheckedItemsChanged(IEnumerable<IWeightedErcValueItem<TValueItem>> items) {
66      base.OnCheckedItemsChanged(items);
67
68      foreach (var item in items) {
69        item.IsEnabled = ItemChecked(item);
70      }
71    }
72
73    public bool IsEnabled
74    {
75      get { return isEnabled; }
76      set
77      {
78        if (value == isEnabled)
79          return;
80
81        isEnabled = value;
82
83        if (EnabledChanged != null)
84          EnabledChanged(this, value);
85      }
86    }
87
88    public abstract TValue GetErcValue(IRandom random);
89
90    protected TValue GetErcValue(IRandom random, Converter<TValueItem, TValue> converter) {
91      if (Count == 0 || !IsEnabled) return default(TValue);
92
93      var ercValueItem = list
94        .Where(item => item.IsEnabled)
95        .RandomWeightedOrDefault(random);
96
97      return ercValueItem == null
98        ? default(TValue)
99        : converter(ercValueItem.GetErcValue(random));
100    }
101  }
102}
Note: See TracBrowser for help on using the repository browser.