Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3027-NormalDistribution/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs @ 17476

Last change on this file since 17476 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HeuristicLab.Core;
26using HEAL.Attic;
27
28namespace HeuristicLab.Optimization {
29  [StorableType("b1388a9d-c85c-49f6-9916-ef4c93dffee0")]
30  public abstract class Individual {
31    [Storable]
32    protected IEncoding Encoding { get; private set; }
33    [Storable]
34    protected IScope Scope { get; private set; }
35    public string Name { get { return Encoding.Name; } }
36
37
38    [StorableConstructor]
39    protected Individual(StorableConstructorFlag _) {
40    }
41
42    protected Individual(IEncoding encoding, IScope scope) {
43      Encoding = encoding;
44      Scope = scope;
45    }
46
47    public IItem this[string name] {
48      get { return ExtractScopeValue(name, Scope); }
49      set { SetScopeValue(name, Scope, value); }
50    }
51
52    public IEnumerable<KeyValuePair<string, IItem>> Values {
53      get { return Scope.Variables.Select(v => new KeyValuePair<string, IItem>(v.Name, v.Value)); }
54    }
55    public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
56
57    public abstract Individual Copy();
58    internal void CopyToScope(IScope scope) {
59      foreach (var val in Values)
60        SetScopeValue(val.Key, scope, val.Value);
61    }
62
63    private static IItem ExtractScopeValue(string name, IScope scope) {
64      if (scope == null) throw new ArgumentNullException("scope");
65      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
66      var value = scope.Variables[name].Value;
67      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
68      return value;
69    }
70
71    private static void SetScopeValue(string name, IScope scope, IItem value) {
72      if (scope == null) throw new ArgumentNullException("scope");
73      if (value == null) throw new ArgumentNullException("value");
74
75      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
76      else scope.Variables[name].Value = value;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.