[11484] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14600] | 3 | * Copyright (C) 2002-2016 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;
|
---|
[11484] | 26 |
|
---|
[11949] | 27 | namespace HeuristicLab.Optimization {
|
---|
[11619] | 28 | public abstract class Individual {
|
---|
[11737] | 29 | protected IEncoding Encoding { get; private set; }
|
---|
[11598] | 30 | protected IScope Scope { get; private set; }
|
---|
[11619] | 31 | public string Name { get { return Encoding.Name; } }
|
---|
| 32 |
|
---|
| 33 | protected Individual(IEncoding encoding, IScope scope) {
|
---|
[11598] | 34 | Encoding = encoding;
|
---|
| 35 | Scope = scope;
|
---|
[11484] | 36 | }
|
---|
| 37 |
|
---|
[15256] | 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 | }
|
---|
[11813] | 46 | public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
|
---|
[11484] | 47 |
|
---|
[15256] | 48 | public abstract Individual Copy();
|
---|
| 49 | internal void CopyToScope(IScope scope) {
|
---|
| 50 | foreach (var val in Values)
|
---|
| 51 | SetScopeValue(val.Key, scope, val.Value);
|
---|
[11484] | 52 | }
|
---|
| 53 |
|
---|
[15256] | 54 | private static IItem ExtractScopeValue(string name, IScope scope) {
|
---|
| 55 | if (scope == null) throw new ArgumentNullException("scope");
|
---|
[11619] | 56 | if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
|
---|
[11598] | 57 | var value = scope.Variables[name].Value;
|
---|
[11619] | 58 | if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
|
---|
[11598] | 59 | return value;
|
---|
[11484] | 60 | }
|
---|
| 61 |
|
---|
[15256] | 62 | private static void SetScopeValue(string name, IScope scope, IItem value) {
|
---|
| 63 | if (scope == null) throw new ArgumentNullException("scope");
|
---|
[11598] | 64 | if (value == null) throw new ArgumentNullException("value");
|
---|
[15256] | 65 |
|
---|
[11598] | 66 | if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
|
---|
| 67 | else scope.Variables[name].Value = value;
|
---|
[11484] | 68 | }
|
---|
[11598] | 69 | }
|
---|
[11484] | 70 | }
|
---|