Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/Encodings/MultiEncodingIndividual.cs @ 11598

Last change on this file since 11598 was 11598, checked in by mkommend, 9 years ago

#2174: Added first version of refactored individuals.

File size: 2.7 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.Programmable.Encodings {
31  public sealed class MultiEncodingIndividual : Individual {
32    public new MultiEncoding Encoding {
33      get { return (MultiEncoding)base.Encoding; }
34    }
35
36    public MultiEncodingIndividual(IEncoding encoding, IScope scope)
37      : base(encoding, scope) {
38      if (encoding.GetType() != typeof(MultiEncoding))
39        throw new ArgumentException("Encoding must be of type MultiEncoding.k");
40    }
41
42    [StorableConstructor]
43    private MultiEncodingIndividual(bool deserializing) : base(deserializing) { }
44
45    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingIndividual(this, cloner); }
46    private MultiEncodingIndividual(Individual original, Cloner cloner) : base(original, cloner) { }
47
48    public override IItem this[string name] {
49      get {
50        var encoding = Encoding.Encodings.FirstOrDefault(e => e.Name == name);
51        if (encoding == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
52        return ExtractScopeValue(name, encoding.Name, Scope);
53      }
54      set {
55        var encoding = Encoding.Encodings.FirstOrDefault(e => e.Name == name);
56        if (encoding == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
57        SetScopeValue(name, encoding.Name, Scope, value);
58      }
59    }
60
61    internal override Individual Copy(IScope scope) {
62      var individual = Encoding.CreateIndividual(scope);
63      foreach (var encoding in Encoding.Encodings)
64        individual[encoding.Name] = this[encoding.Name];
65      return individual;
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.