Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/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
RevLine 
[11484]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11484]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
[11598]22using System;
[14877]23using System.Collections.Generic;
24using System.Linq;
[11598]25using HeuristicLab.Core;
[16565]26using HEAL.Attic;
[11484]27
[11949]28namespace HeuristicLab.Optimization {
[16565]29  [StorableType("b1388a9d-c85c-49f6-9916-ef4c93dffee0")]
[11619]30  public abstract class Individual {
[16565]31    [Storable]
[11737]32    protected IEncoding Encoding { get; private set; }
[16565]33    [Storable]
[11598]34    protected IScope Scope { get; private set; }
[11619]35    public string Name { get { return Encoding.Name; } }
36
[16565]37
38    [StorableConstructor]
39    protected Individual(StorableConstructorFlag _) {
40    }
41
[11619]42    protected Individual(IEncoding encoding, IScope scope) {
[11598]43      Encoding = encoding;
44      Scope = scope;
[11484]45    }
46
[14877]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    }
[11813]55    public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
[11484]56
[14877]57    public abstract Individual Copy();
58    internal void CopyToScope(IScope scope) {
59      foreach (var val in Values)
60        SetScopeValue(val.Key, scope, val.Value);
[11484]61    }
62
[14877]63    private static IItem ExtractScopeValue(string name, IScope scope) {
64      if (scope == null) throw new ArgumentNullException("scope");
[11619]65      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
[11598]66      var value = scope.Variables[name].Value;
[11619]67      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
[11598]68      return value;
[11484]69    }
70
[14877]71    private static void SetScopeValue(string name, IScope scope, IItem value) {
72      if (scope == null) throw new ArgumentNullException("scope");
[11598]73      if (value == null) throw new ArgumentNullException("value");
[14877]74
[11598]75      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
76      else scope.Variables[name].Value = value;
[11484]77    }
[11598]78  }
[11484]79}
Note: See TracBrowser for help on using the repository browser.