Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/ErcOptionConvertible.cs @ 17507

Last change on this file since 17507 was 15273, checked in by pkimmesw, 7 years ago

#2665 Started Plush Encoding, Added Zero Error Individual Count Analyzer

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.Extensions;
10
11  [StorableClass]
12  public abstract class ErcOptionConvertible<TValue, TValueItem> : CheckedItemCollection<WeightedErcItem<TValueItem>>, IErcItem<TValue> {
13    [Storable]
14    protected bool isEnabled;
15
16    public event EventHandler<bool> EnabledChanged;
17
18    protected ErcOptionConvertible() : this(false, new WeightedErcItem<TValueItem>[0]) { }
19
20    protected ErcOptionConvertible(bool isEnabled, params WeightedErcItem<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<WeightedErcItem<TValueItem>> values) {
45      foreach (var item in values)
46        item.EnabledChanged += ErcValueItemEnabledChanged;
47    }
48
49    protected override void OnItemsAdded(IEnumerable<WeightedErcItem<TValueItem>> items) {
50      base.OnItemsAdded(items);
51      SetEnabledChangedHandler(items);
52    }
53
54    protected override void OnItemsRemoved(IEnumerable<WeightedErcItem<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((WeightedErcItem<TValueItem>)sender, state);
63    }
64
65    protected override void OnCheckedItemsChanged(IEnumerable<WeightedErcItem<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, x => x.Weight);
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.