Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis.Base/Erc/ConstantsErcValue.cs @ 14952

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

#2665 Added IsNoop to Expression, Made Expressions storable, Fixed Debugger, Fixed and improved problem data and result visualisation, Added custom ErcOption view, Added problem difficulty to problem data name

File size: 1.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Base.Erc {
2  using HeuristicLab.Common;
3  using HeuristicLab.Core;
4  using HeuristicLab.Data;
5  using HeuristicLab.Parameters;
6  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8  [StorableClass]
9  public abstract class ConstantsWeightedErc<T, TArray> : WeightedErcItem<T>
10    where T : struct
11    where TArray : ValueTypeArray<T>, new() {
12
13    private const string ConstantsParameterName = "Constants";
14
15    protected ConstantsWeightedErc(TArray array) : this(true, array) { }
16    protected ConstantsWeightedErc(bool isEnabled, TArray array, double weight = 1d) : base(isEnabled, weight) {
17      Parameters.Add(new FixedValueParameter<TArray>(ConstantsParameterName, array));
18      array.ToStringChanged += (sender, args) => UpdateName();
19      UpdateName();
20    }
21
22    private void UpdateName() {
23      Name = Constants.ToString();
24    }
25
26    [StorableConstructor]
27    protected ConstantsWeightedErc(bool deserializing) : base(deserializing) { }
28
29    protected ConstantsWeightedErc(ConstantsWeightedErc<T, TArray> origin, Cloner cloner) : base(origin, cloner) { }
30
31    public IValueParameter<TArray> ConstantsParameter
32    {
33      get { return (IValueParameter<TArray>)Parameters[ConstantsParameterName]; }
34    }
35
36    public TArray Constants
37    {
38      get { return ConstantsParameter.Value; }
39    }
40
41    public override T GetErcValue(IRandom random) {
42      if (Constants.Length == 0) return default(T);
43
44      var x = random.Next(Constants.Length);
45      return Constants[x];
46    }
47  }
48}
Note: See TracBrowser for help on using the repository browser.