Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/23/15 22:18:35 (8 years ago)
Author:
abeham
Message:

#2521: Adapted multi-encoding for new infrastructure

TODO: Evaluator, Analyzer, ... need to be copied

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs

    r13350 r13351  
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
    2829namespace HeuristicLab.Optimization {
    29   public sealed class MultiEncodingIndividual : Individual {
    30     private new MultiEncoding Encoding {
    31       get { return (MultiEncoding)base.Encoding; }
     30  [Item("MultiSolution", "A solution that consists of other solutions.")]
     31  [StorableClass]
     32  public sealed class MultiSolution : Item, ISolution {
     33
     34    private MultiEncoding Encoding { get; set; }
     35    protected IScope Scope { get; private set; }
     36
     37    private readonly Dictionary<string, ISolution> solutions;
     38
     39    [StorableConstructor]
     40    private MultiSolution(bool deserializing) : base(deserializing) { }
     41
     42    private MultiSolution(MultiSolution original, Cloner cloner)
     43      : base(original, cloner) {
     44      Encoding = cloner.Clone(original.Encoding);
     45      Scope = cloner.Clone(original.Scope);
     46      solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
     47    }
     48    public MultiSolution(MultiEncoding encoding, IScope scope) {
     49      Encoding = encoding;
     50      Scope = scope;
     51      solutions = encoding.Encodings.Select(e => new { Name = e.Name, Solution = ScopeUtil.GetSolution(scope, e) })
     52                                    .ToDictionary(x => x.Name, x => x.Solution);
    3253    }
    3354
    34     private readonly IEnumerable<Individual> individuals;
    35 
    36     public MultiEncodingIndividual(MultiEncoding encoding, IScope scope)
    37       : base(encoding, scope) {
    38       individuals = encoding.Encodings.Select(e => e.GetIndividual(scope)).ToArray();
     55    public override IDeepCloneable Clone(Cloner cloner) {
     56      return new MultiSolution(this, cloner);
    3957    }
    4058
    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] {
     59    public ISolution this[string name] {
    4860      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];
     61        ISolution result;
     62        if (!solutions.TryGetValue(name, out result)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     63        return result;
    5264      }
    5365      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;
     66        if (!solutions.ContainsKey(name)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
     67        solutions[name] = value;
    5768      }
    5869    }
    5970
    60     public override TEncoding GetEncoding<TEncoding>() {
     71    public TEncoding GetEncoding<TEncoding>() where TEncoding : IEncoding {
    6172      TEncoding encoding;
    6273      try {
    6374        encoding = (TEncoding)Encoding.Encodings.SingleOrDefault(e => e is TEncoding);
    6475      } catch (InvalidOperationException) {
    65         throw new InvalidOperationException(string.Format("The individual uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
     76        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TEncoding).GetPrettyName()));
    6677      }
    67       if (encoding == null) throw new InvalidOperationException(string.Format("The individual does not use a {0}.", typeof(TEncoding).GetPrettyName()));
     78      if (encoding == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TEncoding).GetPrettyName()));
    6879      return encoding;
    6980    }
    7081
    71     public override Individual CopyToScope(IScope scope) {
    72       var copies = individuals.Select(i => i.CopyToScope(scope)).ToArray();
    73       return new MultiEncodingIndividual(Encoding, scope, copies);
     82    public TSolution GetSolution<TSolution>() where TSolution : class, ISolution {
     83      TSolution solution;
     84      try {
     85        solution = (TSolution)solutions.SingleOrDefault(s => s.Value is TSolution).Value;
     86      } catch (InvalidOperationException) {
     87        throw new InvalidOperationException(string.Format("The solution uses multiple {0} .", typeof(TSolution).GetPrettyName()));
     88      }
     89      if (solution == null) throw new InvalidOperationException(string.Format("The solution does not use a {0}.", typeof(TSolution).GetPrettyName()));
     90      return solution;
    7491    }
    7592  }
Note: See TracChangeset for help on using the changeset viewer.