Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/14 14:46:00 (11 years ago)
Author:
abeham
Message:

#2174: Major refactoring:

  • ParameterVector is only a temporary datastructure that is constructed for evaluation purposes only
  • Multiple permutations are allowed
  • Special support for single-vector encodings (to apply specialized algorithms such as PSO or CMA-ES)
  • General support for multi-vector encoding
Location:
branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures
Files:
7 added
1 copied

Legend:

Unmodified
Added
Removed
  • branches/SimSharp/HeuristicLab.Problems.Programmable/3.3/Datastructures/ParameterVector.cs

    r10796 r10850  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    24 using HeuristicLab.Collections;
    25 using HeuristicLab.Common;
    26 using HeuristicLab.Core;
    27 using HeuristicLab.Data;
     23using System.Linq;
    2824using HeuristicLab.Encodings.BinaryVectorEncoding;
    2925using HeuristicLab.Encodings.IntegerVectorEncoding;
    3026using HeuristicLab.Encodings.PermutationEncoding;
    3127using HeuristicLab.Encodings.RealVectorEncoding;
    32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3328
    34 namespace HeuristicLab.Encodings.ParameterVectorEncoding {
    35   [Item("ParameterVector", "Encodes a range of parameters of different types.")]
    36   [StorableClass]
    37   public class ParameterVector : Item {
    38     private bool isFeasible;
    39     [Storable]
    40     public bool IsFeasible {
    41       get { return isFeasible; }
    42       set {
    43         if (isFeasible == value) return;
    44         isFeasible = value;
    45         OnIsFeasibleChanged();
    46       }
     29namespace HeuristicLab.Problems.Programmable {
     30  public class ParameterVector {
     31    protected Dictionary<string, BinaryVector> BinaryParameters;
     32    protected Dictionary<string, IntegerVector> IntegerParameters;
     33    protected Dictionary<string, RealVector> RealParameters;
     34    protected Dictionary<string, Permutation> PermutationParameters;
     35
     36    public ParameterVector(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null,
     37      IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null,
     38      IEnumerable<KeyValuePair<string, RealVector>> realVectors = null,
     39      IEnumerable<KeyValuePair<string, Permutation>> permutations = null) {
     40      if (binaryVectors != null) BinaryParameters = binaryVectors.ToDictionary(x => x.Key, x => x.Value);
     41      if (integerVectors != null) IntegerParameters = integerVectors.ToDictionary(x => x.Key, x => x.Value);
     42      if (realVectors != null) RealParameters = realVectors.ToDictionary(x => x.Key, x => x.Value);
     43      if (permutations != null) PermutationParameters = permutations.ToDictionary(x => x.Key, x => x.Value);
    4744    }
    4845
    49     [Storable]
    50     public BinaryVector BooleanParameters { get; set; }
    51     [Storable]
    52     public IntegerVector IntegerParameters { get; set; }
    53     [Storable]
    54     public RealVector RealParameters { get; set; }
    55     [Storable]
    56     public Permutation PermutationParameter { get; set; }
    57 
    58     [Storable]
    59     protected BidirectionalDictionary<string, int> BoolMap;
    60     [Storable]
    61     protected BidirectionalDictionary<string, int> IntMap;
    62     [Storable]
    63     protected BidirectionalDictionary<string, int> RealMap;
    64 
    65     [Storable]
    66     public IntMatrix IntegerBounds { get; set; }
    67     [Storable]
    68     public DoubleMatrix RealBounds { get; set; }
    69 
    70     [StorableConstructor]
    71     protected ParameterVector(bool deserializing) : base(deserializing) { }
    72     protected ParameterVector(ParameterVector original, Cloner cloner)
    73       : base(original, cloner) {
    74       IsFeasible = original.IsFeasible;
    75       if (original.BooleanParameters != null) {
    76         BooleanParameters = cloner.Clone(original.BooleanParameters);
    77         BoolMap = new BidirectionalDictionary<string, int>(original.BoolMap);
    78       }
    79       if (original.IntegerParameters != null) {
    80         IntegerParameters = cloner.Clone(original.IntegerParameters);
    81         IntMap = new BidirectionalDictionary<string, int>(original.IntMap);
    82         IntegerBounds = cloner.Clone(original.IntegerBounds);
    83       }
    84       if (original.RealParameters != null) {
    85         RealParameters = cloner.Clone(original.RealParameters);
    86         RealMap = new BidirectionalDictionary<string, int>(original.RealMap);
    87         RealBounds = cloner.Clone(original.RealBounds);
    88       }
    89       PermutationParameter = cloner.Clone(original.PermutationParameter);
    90     }
    91     public ParameterVector() {
    92 
     46    public BinaryVector Binary(string name) {
     47      return BinaryParameters[name];
    9348    }
    9449
    95     public override IDeepCloneable Clone(Cloner cloner) {
    96       return new ParameterVector(this, cloner);
     50    public bool Binary(string name, int index) {
     51      return BinaryParameters[name][index];
    9752    }
    9853
    99     public bool Boolean(string name) {
    100       return BooleanParameters[BoolMap.GetByFirst(name)];
     54    public IEnumerable<string> BinaryNames {
     55      get { return BinaryParameters.Keys; }
    10156    }
    10257
    103     public bool Boolean(int index) {
    104       return BooleanParameters[index];
     58    public IntegerVector Integer(string name) {
     59      return IntegerParameters[name];
    10560    }
    10661
    107     public IEnumerable<string> BooleanParameterNames {
    108       get {
    109         for (int i = 0; i < BooleanParameters.Length; i++)
    110           yield return BoolMap.GetBySecond(i);
    111       }
     62    public int Integer(string name, int index) {
     63      return IntegerParameters[name][index];
    11264    }
    11365
    114     public int Integer(string name) {
    115       return IntegerParameters[IntMap.GetByFirst(name)];
     66    public IEnumerable<string> IntegerNames {
     67      get { return IntegerParameters.Keys; }
    11668    }
    11769
    118     public int Integer(int index) {
    119       return IntegerParameters[index];
     70    public RealVector Real(string name) {
     71      return RealParameters[name];
    12072    }
    12173
    122     public IEnumerable<string> IntegerParameterNames {
    123       get {
    124         for (int i = 0; i < IntegerParameters.Length; i++)
    125           yield return IntMap.GetBySecond(i);
    126       }
     74    public double Real(string name, int index) {
     75      return RealParameters[name][index];
    12776    }
    12877
    129     public double Real(string name) {
    130       return RealParameters[RealMap.GetByFirst(name)];
     78    public IEnumerable<string> RealNames {
     79      get { return RealParameters.Keys; }
    13180    }
    13281
    133     public double Real(int index) {
    134       return RealParameters[index];
     82    public Permutation Permutation(string name) {
     83      return PermutationParameters[name];
    13584    }
    13685
    137     public IEnumerable<string> RealParameterNames {
    138       get {
    139         for (int i = 0; i < RealParameters.Length; i++)
    140           yield return RealMap.GetBySecond(i);
    141       }
     86    public IEnumerable<string> PermutationNames {
     87      get { return PermutationParameters.Keys; }
    14288    }
    143 
    144     public Permutation Permutation() {
    145       return PermutationParameter;
    146     }
    147 
    148     public event EventHandler IsFeasibleChanged;
    149     protected virtual void OnIsFeasibleChanged() {
    150       var handler = IsFeasibleChanged;
    151       if (handler != null) handler(this, EventArgs.Empty);
    152     }
    153 
    154     #region Internal Methods for the Builder
    155     internal void SetBooleanParameters(HashSet<string> bools = null) {
    156       if (bools == null) {
    157         BooleanParameters = null;
    158         BoolMap = null;
    159       } else {
    160         BooleanParameters = new BinaryVector(bools.Count);
    161         BoolMap = new BidirectionalDictionary<string, int>();
    162         foreach (var p in bools) {
    163           BoolMap.Add(p, BoolMap.Count);
    164         }
    165       }
    166     }
    167 
    168     internal void SetIntegerParameters(Dictionary<string, Tuple<int, int, int?>> ints = null) {
    169       if (ints == null) {
    170         IntegerParameters = null;
    171         IntMap = null;
    172         IntegerBounds = null;
    173       } else {
    174         IntegerParameters = new IntegerVector(ints.Count);
    175         IntMap = new BidirectionalDictionary<string, int>();
    176         IntegerBounds = new IntMatrix(ints.Count, 3);
    177         var i = 0;
    178         foreach (var p in ints) {
    179           IntegerBounds[i, 0] = p.Value.Item1;
    180           IntegerBounds[i, 1] = p.Value.Item2;
    181           IntegerBounds[i, 2] = p.Value.Item3 ?? 1;
    182           IntegerParameters[i] = p.Value.Item1; // default to min
    183           IntMap.Add(p.Key, i++);
    184         }
    185       }
    186     }
    187 
    188     internal void SetRealParameters(Dictionary<string, Tuple<double, double>> reals = null) {
    189       if (reals == null) {
    190         RealParameters = null;
    191         RealMap = null;
    192         RealBounds = null;
    193       } else {
    194         RealParameters = new RealVector(reals.Count);
    195         RealMap = new BidirectionalDictionary<string, int>();
    196         RealBounds = new DoubleMatrix(reals.Count, 2);
    197         var i = 0;
    198         foreach (var p in reals) {
    199           RealBounds[i, 0] = p.Value.Item1;
    200           RealBounds[i, 1] = p.Value.Item2;
    201           RealParameters[i] = p.Value.Item1; // default to min
    202           RealMap.Add(p.Key, i++);
    203         }
    204       }
    205     }
    206 
    207     internal void SetPermutationParameters(Tuple<PermutationTypes, int> perms = null) {
    208       if (perms == null) PermutationParameter = null;
    209       else PermutationParameter = new Permutation(perms.Item1, perms.Item2);
    210     }
    211     #endregion
    21289  }
    21390}
Note: See TracChangeset for help on using the changeset viewer.