- Timestamp:
- 11/24/15 15:24:12 (9 years ago)
- 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 35 35 protected IScope Scope { get; private set; } 36 36 37 private readonly Dictionary<string, ISolution> solutions;38 39 37 [StorableConstructor] 40 38 private CombinedSolution(bool deserializing) : base(deserializing) { } … … 44 42 Encoding = cloner.Clone(original.Encoding); 45 43 Scope = cloner.Clone(original.Scope); 46 solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value));47 44 } 48 45 public CombinedSolution(IScope scope, MultiEncoding encoding) { 49 46 Encoding = encoding; 50 47 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);53 48 } 54 49 … … 58 53 59 54 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); } 69 57 } 70 58 … … 80 68 } 81 69 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); 91 72 } 92 73 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncodingOperator.cs
r13336 r13359 23 23 24 24 namespace HeuristicLab.Optimization { 25 public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution {25 public interface IEncodingOperator<TSolution> : IOperator where TSolution : class, ISolution { 26 26 ILookupParameter<IEncoding<TSolution>> EncodingParameter { get; } 27 27 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs
r13356 r13359 24 24 namespace HeuristicLab.Optimization { 25 25 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); 27 30 } 28 31 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs
r13356 r13359 33 33 public sealed class MultiEncoding : Encoding<CombinedSolution> { 34 34 35 private readonlyItemCollection<IEncoding> encodings;35 private ItemCollection<IEncoding> encodings; 36 36 37 37 [Storable] 38 38 private IEnumerable<IEncoding> StorableEncodings { 39 39 get { return encodings; } 40 set { encodings .AddRange(value); }40 set { encodings = new ItemCollection<IEncoding>(value); } 41 41 } 42 42 … … 46 46 47 47 [StorableConstructor] 48 private MultiEncoding(bool deserializing) 49 : base(deserializing) { 50 encodings = new ItemCollection<IEncoding>(); 51 } 48 private MultiEncoding(bool deserializing) : base(deserializing) { } 52 49 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); } 53 50 private MultiEncoding(MultiEncoding original, Cloner cloner) … … 58 55 : base("MultiEncoding") { 59 56 encodings = new ItemCollection<IEncoding>(); 60 SolutionCreator = new MultiEncodingCreator() { Encoding = this};57 SolutionCreator = new MultiEncodingCreator() { SolutionParameter = { ActualName = Name } }; 61 58 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) { 62 @operator. Encoding = this;59 @operator.SolutionParameter.ActualName = Name; 63 60 AddOperator(@operator); 64 61 } … … 70 67 encodings.Add(encoding); 71 68 Parameters.AddRange(encoding.Parameters); 69 70 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) 71 @operator.AddEncoding(encoding); 72 72 73 return this; 73 74 } … … 76 77 var success = encodings.Remove(encoding); 77 78 Parameters.RemoveRange(encoding.Parameters); 79 80 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) 81 @operator.RemoveEncoding(encoding); 82 78 83 return success; 79 84 } 80 85 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 } 82 90 } 83 91 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs
r13356 r13359 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Parameters;27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 27 … … 41 40 } 42 41 43 p rotectedoverride void AddEncoding(IEncoding encoding) {42 public override void AddEncoding(IEncoding encoding) { 44 43 base.AddEncoding(encoding); 45 44 var parameter = GetParameter(encoding); … … 48 47 } 49 48 50 p rotectedoverride bool RemoveEncoding(IEncoding encoding) {49 public override bool RemoveEncoding(IEncoding encoding) { 51 50 var success = base.RemoveEncoding(encoding); 52 51 encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged; … … 66 65 67 66 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(); 70 69 } 71 70 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCrossover.cs
r13356 r13359 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Parameters;25 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 26 25 … … 39 38 40 39 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(); 43 42 } 44 43 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs
r13356 r13359 21 21 22 22 using System; 23 using System.Collections.Generic;24 23 using System.Linq; 25 using HeuristicLab.Collections;26 24 using HeuristicLab.Common; 27 25 using HeuristicLab.Core; … … 32 30 namespace HeuristicLab.Optimization { 33 31 [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"]; } 47 35 } 48 36 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"]; } 51 39 } 52 40 53 41 [StorableConstructor] 54 42 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) { } 60 44 protected MultiEncodingOperator() 61 45 : 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.")); 96 48 } 97 49 … … 101 53 } 102 54 103 p rotectedvirtual void AddEncoding(IEncoding encoding) {55 public virtual void AddEncoding(IEncoding encoding) { 104 56 if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name)); 105 57 … … 111 63 } 112 64 113 p rotectedvirtual bool RemoveEncoding(IEncoding encoding) {65 public virtual bool RemoveEncoding(IEncoding encoding) { 114 66 if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name)); 115 67 encoding.OperatorsChanged -= Encoding_OperatorsChanged; -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/ScopeUtil.cs
r13351 r13359 23 23 24 24 using System; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 … … 30 31 public static TSolution CopySolutionToScope<TSolution>(IScope scope, IEncoding<TSolution> encoding, TSolution solution) 31 32 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 { 33 38 var copy = (TSolution)solution.Clone(); 34 39 if (!scope.Variables.ContainsKey(name)) scope.Variables.Add(new Variable(name, copy)); … … 42 47 if (!scope.Variables.ContainsKey(name)) throw new ArgumentException(string.Format(" {0} cannot be found in the provided scope.", name)); 43 48 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())); 45 50 return value; 46 51 } 47 52 48 53 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; 54 63 } 55 64
Note: See TracChangeset
for help on using the changeset viewer.