[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16956] | 3 | * Copyright (C) 2002-2019 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] | 22 | using System;
|
---|
[15256] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[11598] | 25 | using HeuristicLab.Core;
|
---|
[16956] | 26 | using HEAL.Attic;
|
---|
[11484] | 27 |
|
---|
[11949] | 28 | namespace HeuristicLab.Optimization {
|
---|
[16956] | 29 | [StorableType("b1388a9d-c85c-49f6-9916-ef4c93dffee0")]
|
---|
[11619] | 30 | public abstract class Individual {
|
---|
[16956] | 31 | [Storable]
|
---|
[11737] | 32 | protected IEncoding Encoding { get; private set; }
|
---|
[16956] | 33 | [Storable]
|
---|
[11598] | 34 | protected IScope Scope { get; private set; }
|
---|
[11619] | 35 | public string Name { get { return Encoding.Name; } }
|
---|
| 36 |
|
---|
[16956] | 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 |
|
---|
[15256] | 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 |
|
---|
[15256] | 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 |
|
---|
[15256] | 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 |
|
---|
[15256] | 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");
|
---|
[15256] | 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 | }
|
---|