Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 1.4 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc {
2  using System;
3
4  using HeuristicLab.Common;
5  using HeuristicLab.Core;
6  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7  using HeuristicLab.Problems.ProgramSynthesis.Base.Weighted;
8
9  [StorableClass]
10  public abstract class ErcOptionConvertible<TValue, TValueItem> : ItemCollection<IWeightedErcValueItem<TValueItem>>, IErcValueItem<TValue> {
11    private readonly Converter<TValueItem, TValue> converter;
12
13    protected ErcOptionConvertible(Converter<TValueItem, TValue> converter) : this(converter, false) { }
14
15    protected ErcOptionConvertible(Converter<TValueItem, TValue> converter, bool isEnabled, params IWeightedErcValueItem<TValueItem>[] values) {
16      this.converter = converter;
17      IsEnabled = isEnabled;
18      AddRange(values);
19    }
20
21    [StorableConstructor]
22    protected ErcOptionConvertible(bool deserializing)
23      : base(deserializing) {
24    }
25
26    protected ErcOptionConvertible(ErcOptionConvertible<TValue, TValueItem> origin, Cloner cloner)
27      : base(origin, cloner) {
28    }
29
30    public bool IsEnabled { get; set; }
31
32
33    public TValue GetErcValue(IRandom random) {
34      if (Count == 0 || !IsEnabled) return default(TValue);
35
36      var ercValueItem = list.RandomWeightedOrDefault(random);
37      return ercValueItem == null
38        ? default(TValue)
39        : converter(ercValueItem.GetErcValue(random));
40    }
41  }
42}
Note: See TracBrowser for help on using the repository browser.