Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
07/03/17 22:59:37 (7 years ago)
Author:
mkommend
Message:

#2774: Merged r14877 into stable.

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Optimization

  • stable/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/Individual.cs

    r14186 r15116  
    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;
  • stable/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiEncodingIndividual.cs

    r14186 r15116  
    3232    }
    3333
    34     private readonly IEnumerable<Individual> individuals;
     34    public MultiEncodingIndividual(MultiEncoding encoding, IScope scope)
     35      : base(encoding, scope) { }
    3536
    36     public MultiEncodingIndividual(MultiEncoding encoding, IScope scope)
    37       : base(encoding, scope) {
    38       individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray();
     37    private MultiEncodingIndividual(MultiEncodingIndividual copy) : base(copy.Encoding, new Scope()) {
     38      copy.CopyToScope(Scope);
    3939    }
    40 
    41     private MultiEncodingIndividual(MultiEncoding encoding, IScope scope, IEnumerable<Individual> individuals)
    42       : base(encoding, scope) {
    43       this.individuals = individuals;
    44     }
    45 
    46 
    47     public override IItem this[string name] {
    48       get {
    49         var individual = individuals.SingleOrDefault(i => i.Name == name);
    50         if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    51         return individual[name];
    52       }
    53       set {
    54         var individual = individuals.SingleOrDefault(i => i.Name == name);
    55         if (individual == null) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    56         individual[name] = value;
    57       }
     40    public override Individual Copy() {
     41      return new MultiEncodingIndividual(this);
    5842    }
    5943
     
    6246      try {
    6347        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
    64       } catch (InvalidOperationException) {
     48      }
     49      catch (InvalidOperationException) {
    6550        throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
    6651      }
     
    6853      return encoding;
    6954    }
    70 
    71     public override Individual CopyToScope(IScope scope) {
    72       var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray();
    73       return new MultiEncodingIndividual(Encoding, scope, copies);
    74     }
    7555  }
    7656}
  • stable/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/SingleEncodingIndividual.cs

    r14186 r15116  
    3232    }
    3333
    34     public override IItem this[string name] {
    35       get {
    36         if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name));
    37         return ExtractScopeValue(Name, Scope);
    38       }
    39       set {
    40         if (Name != name) throw new ArgumentException(string.Format("{0} is not part of the individual.", name));
    41         SetScopeValue(Name, Scope, value);
    42       }
     34    private SingleEncodingIndividual(SingleEncodingIndividual copy) : base(copy.Encoding, new Scope()) {
     35      copy.CopyToScope(Scope);
     36    }
     37    public override Individual Copy() {
     38      return new SingleEncodingIndividual(this);
    4339    }
    4440
     
    4844      return encoding;
    4945    }
    50 
    51     public override Individual CopyToScope(IScope scope) {
    52       SetScopeValue(Name, scope, (IItem)this[Name].Clone());
    53       return new SingleEncodingIndividual(Encoding, scope);
    54     }
    5546  }
    5647
Note: See TracChangeset for help on using the changeset viewer.