Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/Individual.cs @ 11484

Last change on this file since 11484 was 11484, checked in by abeham, 10 years ago

#2174: Major refactoring

  • Removed ProblemDefinitionHosts
  • Renamed ParameterVector to Individual
  • Renamed Configuration to Encoding
  • Changed handling of existing operators that they will not be removed and recreated, but only rewired
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Encodings.BinaryVectorEncoding;
26using HeuristicLab.Encodings.IntegerVectorEncoding;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Encodings.RealVectorEncoding;
29
30namespace HeuristicLab.Problems.Programmable {
31  public sealed class Individual : IDeepCloneable {
32    private readonly Dictionary<string, BinaryVector> binaryVectors;
33    private readonly Dictionary<string, IntegerVector> integerVectors;
34    private readonly Dictionary<string, RealVector> realVectors;
35    private readonly Dictionary<string, Permutation> permutationVectors;
36
37    public Individual(IEnumerable<KeyValuePair<string, BinaryVector>> binaryVectors = null,
38      IEnumerable<KeyValuePair<string, IntegerVector>> integerVectors = null,
39      IEnumerable<KeyValuePair<string, RealVector>> realVectors = null,
40      IEnumerable<KeyValuePair<string, Permutation>> permutations = null) {
41      // prevent unnecessary allocation of dictionaries for unused vectors
42      if (binaryVectors != null) this.binaryVectors = binaryVectors.ToDictionary(x => x.Key, x => x.Value);
43      if (integerVectors != null) this.integerVectors = integerVectors.ToDictionary(x => x.Key, x => x.Value);
44      if (realVectors != null) this.realVectors = realVectors.ToDictionary(x => x.Key, x => x.Value);
45      if (permutations != null) this.permutationVectors = permutations.ToDictionary(x => x.Key, x => x.Value);
46    }
47    private Individual(Individual original, Cloner cloner) {
48      cloner.RegisterClonedObject(original, this);
49      if (original.binaryVectors != null) {
50        binaryVectors = new Dictionary<string, BinaryVector>(original.binaryVectors.Comparer);
51        foreach (var param in original.binaryVectors)
52          binaryVectors[param.Key] = cloner.Clone(param.Value);
53      }
54      if (original.integerVectors != null) {
55        integerVectors = new Dictionary<string, IntegerVector>(original.integerVectors.Comparer);
56        foreach (var param in original.integerVectors)
57          integerVectors[param.Key] = cloner.Clone(param.Value);
58      }
59      if (original.realVectors != null) {
60        realVectors = new Dictionary<string, RealVector>(original.realVectors.Comparer);
61        foreach (var param in original.realVectors)
62          realVectors[param.Key] = cloner.Clone(param.Value);
63      }
64      if (original.permutationVectors != null) {
65        permutationVectors = new Dictionary<string, Permutation>(original.permutationVectors.Comparer);
66        foreach (var param in original.permutationVectors)
67          permutationVectors[param.Key] = cloner.Clone(param.Value);
68      }
69    }
70
71    public object Clone() {
72      return Clone(new Cloner());
73    }
74
75    public IDeepCloneable Clone(Cloner cloner) {
76      return new Individual(this, cloner);
77    }
78
79    public BinaryVector BinaryVector(string name) {
80      return binaryVectors[name];
81    }
82
83    public bool BinaryValue(string name, int index) {
84      return binaryVectors[name][index];
85    }
86
87    public IEnumerable<string> BinaryNames {
88      get { return binaryVectors != null ? binaryVectors.Keys : Enumerable.Empty<string>(); }
89    }
90
91    public IntegerVector IntegerVector(string name) {
92      return integerVectors[name];
93    }
94
95    public int IntegerValue(string name, int index) {
96      return integerVectors[name][index];
97    }
98
99    public IEnumerable<string> IntegerNames {
100      get { return integerVectors != null ? integerVectors.Keys : Enumerable.Empty<string>(); }
101    }
102
103    public RealVector RealVector(string name) {
104      return realVectors[name];
105    }
106
107    public double RealValue(string name, int index) {
108      return realVectors[name][index];
109    }
110
111    public IEnumerable<string> RealNames {
112      get { return realVectors != null ? realVectors.Keys : Enumerable.Empty<string>(); }
113    }
114
115    public Permutation Permutation(string name) {
116      return permutationVectors[name];
117    }
118
119    public IEnumerable<string> PermutationNames {
120      get { return permutationVectors != null ? permutationVectors.Keys : Enumerable.Empty<string>(); }
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.