Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/24/15 15:24:12 (9 years ago)
Author:
abeham
Message:

#2521: worked on multi-encoding (it works again). Some issues are still present:

  • The programmable template needs to be slightly updated for multi-encoding
  • The encoding is recreated every time it is compiled making it impossible to configure operators
Location:
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems
Files:
8 edited

Legend:

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

    r13356 r13359  
    3535    protected IScope Scope { get; private set; }
    3636
    37     private readonly Dictionary<string, ISolution> solutions;
    38 
    3937    [StorableConstructor]
    4038    private CombinedSolution(bool deserializing) : base(deserializing) { }
     
    4442      Encoding = cloner.Clone(original.Encoding);
    4543      Scope = cloner.Clone(original.Scope);
    46       solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));
    4744    }
    4845    public CombinedSolution(IScope scope, MultiEncoding encoding) {
    4946      Encoding = encoding;
    5047      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);
    5348    }
    5449
     
    5853
    5954    public ISolution this[string name] {
    60       get {
    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;
    64       }
    65       set {
    66         if (!solutions.ContainsKey(name)) throw new ArgumentException(string.Format("{0} is not part of the specified encoding.", name));
    67         solutions[name] = value;
    68       }
     55      get { return ScopeUtil.GetSolution(Scope, name); }
     56      set { ScopeUtil.CopySolutionToScope(Scope, name, value); }
    6957    }
    7058
     
    8068    }
    8169
    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;
     70    public TSolution GetSolution<TSolution>(string name) where TSolution : class, ISolution {
     71      return (TSolution)ScopeUtil.GetSolution(Scope, name);
    9172    }
    9273  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodingOperator.cs

    r13336 r13359  
    2323
    2424namespace HeuristicLab.Optimization {
    25   public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution{
     25  public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution {
    2626    ILookupParameter<IEncoding<TSolution>> EncodingParameter { get; }
    2727  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs

    r13356 r13359  
    2424namespace HeuristicLab.Optimization {
    2525  public interface IMultiEncodingOperator : IOperator {
    26     MultiEncoding Encoding { get; set; }
     26    ILookupParameter<CombinedSolution> SolutionParameter { get; }
     27
     28    void AddEncoding(IEncoding encoding);
     29    bool RemoveEncoding(IEncoding encoding);
    2730  }
    2831}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs

    r13356 r13359  
    3333  public sealed class MultiEncoding : Encoding<CombinedSolution> {
    3434
    35     private readonly ItemCollection<IEncoding> encodings;
     35    private ItemCollection<IEncoding> encodings;
    3636
    3737    [Storable]
    3838    private IEnumerable<IEncoding> StorableEncodings {
    3939      get { return encodings; }
    40       set { encodings.AddRange(value); }
     40      set { encodings = new ItemCollection<IEncoding>(value); }
    4141    }
    4242
     
    4646
    4747    [StorableConstructor]
    48     private MultiEncoding(bool deserializing)
    49       : base(deserializing) {
    50       encodings = new ItemCollection<IEncoding>();
    51     }
     48    private MultiEncoding(bool deserializing) : base(deserializing) { }
    5249    public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); }
    5350    private MultiEncoding(MultiEncoding original, Cloner cloner)
     
    5855      : base("MultiEncoding") {
    5956      encodings = new ItemCollection<IEncoding>();
    60       SolutionCreator = new MultiEncodingCreator() { Encoding = this };
     57      SolutionCreator = new MultiEncodingCreator() { SolutionParameter = { ActualName = Name } };
    6158      foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) {
    62         @operator.Encoding = this;
     59        @operator.SolutionParameter.ActualName = Name;
    6360        AddOperator(@operator);
    6461      }
     
    7067      encodings.Add(encoding);
    7168      Parameters.AddRange(encoding.Parameters);
     69
     70      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
     71        @operator.AddEncoding(encoding);
     72
    7273      return this;
    7374    }
     
    7677      var success = encodings.Remove(encoding);
    7778      Parameters.RemoveRange(encoding.Parameters);
     79
     80      foreach (var @operator in Operators.OfType<IMultiEncodingOperator>())
     81        @operator.RemoveEncoding(encoding);
     82
    7883      return success;
    7984    }
    8085
    81     public override void ConfigureOperators(IEnumerable<IOperator> operators) { }
     86    public override void ConfigureOperators(IEnumerable<IOperator> operators) {
     87      foreach (var encOp in operators.OfType<IMultiEncodingOperator>())
     88        encOp.SolutionParameter.ActualName = Name;
     89    }
    8290  }
    8391}
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs

    r13356 r13359  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
    26 using HeuristicLab.Parameters;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
     
    4140    }
    4241
    43     protected override void AddEncoding(IEncoding encoding) {
     42    public override void AddEncoding(IEncoding encoding) {
    4443      base.AddEncoding(encoding);
    4544      var parameter = GetParameter(encoding);
     
    4847    }
    4948
    50     protected override bool RemoveEncoding(IEncoding encoding) {
     49    public override bool RemoveEncoding(IEncoding encoding) {
    5150      var success = base.RemoveEncoding(encoding);
    5251      encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged;
     
    6665
    6766    public override IOperation InstrumentedApply() {
    68       CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding);
    69       return base.Apply();
     67      SolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, (MultiEncoding)EncodingParameter.ActualValue);
     68      return base.InstrumentedApply();
    7069    }
    7170  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCrossover.cs

    r13356 r13359  
    2222using HeuristicLab.Common;
    2323using HeuristicLab.Core;
    24 using HeuristicLab.Parameters;
    2524using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2625
     
    3938
    4039    public override IOperation InstrumentedApply() {
    41       CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding);
    42       return base.Apply();
     40      SolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, (MultiEncoding)EncodingParameter.ActualValue);
     41      return base.InstrumentedApply();
    4342    }
    4443  }
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs

    r13356 r13359  
    2121
    2222using System;
    23 using System.Collections.Generic;
    2423using System.Linq;
    25 using HeuristicLab.Collections;
    2624using HeuristicLab.Common;
    2725using HeuristicLab.Core;
     
    3230namespace HeuristicLab.Optimization {
    3331  [StorableClass]
    34   public abstract class MultiEncodingOperator<T> : InstrumentedOperator, IMultiEncodingOperator where T : class, IOperator {
    35     [Storable]
    36     private MultiEncoding encoding;
    37     public MultiEncoding Encoding {
    38       get { return encoding; }
    39       set {
    40         if (value == null) throw new ArgumentNullException("Encoding must not be null.");
    41         if (value == encoding) return;
    42         if (encoding != null) DeregisterEventHandlers();
    43         encoding = value;
    44         CombinedSolutionParameter.ActualName = encoding.Name;
    45         RegisterEventHandlers();
    46       }
     32  public abstract class MultiEncodingOperator<T> : InstrumentedOperator, IEncodingOperator<CombinedSolution>, IMultiEncodingOperator where T : class, IOperator {
     33    public ILookupParameter<CombinedSolution> SolutionParameter {
     34      get { return (ILookupParameter<CombinedSolution>)Parameters["Solution"]; }
    4735    }
    4836
    49     public ILookupParameter<CombinedSolution> CombinedSolutionParameter {
    50       get { return (ILookupParameter<CombinedSolution>)Parameters["CombinedSolution"]; }
     37    public ILookupParameter<IEncoding<CombinedSolution>> EncodingParameter {
     38      get { return (ILookupParameter<IEncoding<CombinedSolution>>)Parameters["Encoding"]; }
    5139    }
    5240
    5341    [StorableConstructor]
    5442    protected MultiEncodingOperator(bool deserializing) : base(deserializing) { }
    55     protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner)
    56       : base(original, cloner) {
    57       encoding = cloner.Clone(original.encoding);
    58       RegisterEventHandlers();
    59     }
     43    protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) : base(original, cloner) { }
    6044    protected MultiEncodingOperator()
    6145      : base() {
    62       Parameters.Add(new LookupParameter<CombinedSolution>("CombinedSolution", "The combined solution that gets created."));
    63     }
    64 
    65     [StorableHook(HookType.AfterDeserialization)]
    66     private void AfterDeserialization() {
    67       RegisterEventHandlers();
    68     }
    69 
    70     private void RegisterEventHandlers() {
    71       encoding.Encodings.ItemsAdded += EncodingsOnItemsChanged;
    72       encoding.Encodings.CollectionReset += EncodingsOnItemsChanged;
    73       encoding.Encodings.ItemsRemoved += EncodingsOnItemsRemoved;
    74       foreach (var enc in encoding.Encodings)
    75         enc.OperatorsChanged += Encoding_OperatorsChanged;
    76     }
    77 
    78     private void DeregisterEventHandlers() {
    79       encoding.Encodings.ItemsAdded -= EncodingsOnItemsChanged;
    80       encoding.Encodings.CollectionReset -= EncodingsOnItemsChanged;
    81       encoding.Encodings.ItemsRemoved -= EncodingsOnItemsRemoved;
    82       foreach (var enc in encoding.Encodings)
    83         enc.OperatorsChanged -= Encoding_OperatorsChanged;
    84     }
    85 
    86     private void EncodingsOnItemsChanged(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
    87       foreach (var enc in e.Items)
    88         AddEncoding(enc);
    89       foreach (var enc in e.OldItems)
    90         RemoveEncoding(enc);
    91     }
    92 
    93     private void EncodingsOnItemsRemoved(object sender, CollectionItemsChangedEventArgs<IEncoding> e) {
    94       foreach (var enc in e.Items)
    95         RemoveEncoding(enc);
     46      Parameters.Add(new LookupParameter<CombinedSolution>("Solution", "The solution that gets created."));
     47      Parameters.Add(new LookupParameter<IEncoding<CombinedSolution>>("Encoding", "The encoding."));
    9648    }
    9749
     
    10153    }
    10254
    103     protected virtual void AddEncoding(IEncoding encoding) {
     55    public virtual void AddEncoding(IEncoding encoding) {
    10456      if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name));
    10557
     
    11163    }
    11264
    113     protected virtual bool RemoveEncoding(IEncoding encoding) {
     65    public virtual bool RemoveEncoding(IEncoding encoding) {
    11466      if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));
    11567      encoding.OperatorsChanged -= Encoding_OperatorsChanged;
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs

    r13351 r13359  
    2323
    2424using System;
     25using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627
     
    3031    public static TSolution CopySolutionToScope<TSolution>(IScope scope, IEncoding<TSolution> encoding, TSolution solution)
    3132      where TSolution : class,ISolution {
    32       var name = encoding.Name;
     33      return CopySolutionToScope(scope, encoding.Name, solution);
     34    }
     35
     36    public static TSolution CopySolutionToScope<TSolution>(IScope scope, string name, TSolution solution)
     37      where TSolution : class,ISolution {
    3338      var copy = (TSolution)solution.Clone();
    3439      if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, copy));
     
    4247      if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
    4348      var value = scope.Variables[name].Value as TSolution;
    44       if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
     49      if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null or not of type {1}.", name, typeof(TSolution).GetPrettyName()));
    4550      return value;
    4651    }
    4752
    4853    public static ISolution GetSolution(IScope scope, IEncoding encoding) {
    49       var name = encoding.Name;
    50       if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name));
    51       var value = scope.Variables[name].Value as ISolution;
    52       if (value == null) throw new InvalidOperationException(string.Format("Value of {0} is null.", name));
    53       return value;
     54      return GetSolution(scope, encoding.Name);
     55    }
     56
     57    public static ISolution GetSolution(IScope scope, string name) {
     58      IVariable variable;
     59      if (!scope.Variables.TryGetValue(name, out variable)) throw new ArgumentException(string.Format("{0} cannot be found in the provided scope.", name));
     60      var solution = variable.Value as ISolution;
     61      if (solution == null) throw new InvalidOperationException(string.Format("{0} is null or not of type ISolution.", name));
     62      return solution;
    5463    }
    5564
Note: See TracChangeset for help on using the changeset viewer.