Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push.Base/Erc/ConstantsErcValue.cs @ 18183

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

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

File size: 1.6 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis {
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.