Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push.Base/Erc/ErcOptionConvertible.cs @ 17511

Last change on this file since 17511 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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