Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs @ 16189

Last change on this file since 16189 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

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