Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/17 14:42:41 (7 years ago)
Author:
mkommend
Message:

#2774: Allowed storing additional data in the scope of an individual.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs

    r14185 r14877  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325using HeuristicLab.Core;
    2426
     
    2729    protected IEncoding Encoding { get; private set; }
    2830    protected IScope Scope { get; private set; }
    29 
    3031    public string Name { get { return Encoding.Name; } }
    3132
     
    3536    }
    3637
    37     public abstract IItem this[string name] { get; set; }
     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    }
    3846    public abstract TEncoding GetEncoding<TEncoding>() where TEncoding : class, IEncoding;
    3947
    40     public Individual Copy() {
    41       return CopyToScope(new Scope());
     48    public abstract Individual Copy();
     49    internal void CopyToScope(IScope scope) {
     50      foreach (var val in Values)
     51        SetScopeValue(val.Key, scope, val.Value);
    4252    }
    4353
    44     public abstract Individual CopyToScope(IScope scope);
    45 
    46     protected static IItem ExtractScopeValue(string name, IScope scope) {
     54    private static IItem ExtractScopeValue(string name, IScope scope) {
     55      if (scope == null) throw new ArgumentNullException("scope");
    4756      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
    4857      var value = scope.Variables[name].Value;
     
    5160    }
    5261
    53     protected static void SetScopeValue(string name, IScope scope, IItem value) {
     62    private static void SetScopeValue(string name, IScope scope, IItem value) {
     63      if (scope == null) throw new ArgumentNullException("scope");
    5464      if (value == null) throw new ArgumentNullException("value");
     65
    5566      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, value));
    5667      else scope.Variables[name].Value = value;
Note: See TracChangeset for help on using the changeset viewer.