- Timestamp:
- 11/24/15 12:56:04 (9 years ago)
- Location:
- branches/ProblemRefactoring
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Individuals/MultiSolution.cs
r13351 r13356 28 28 29 29 namespace HeuristicLab.Optimization { 30 [Item(" MultiSolution", "A solution that consists of other solutions.")]30 [Item("CombinedSolution", "A solution that consists of other solutions.")] 31 31 [StorableClass] 32 public sealed class MultiSolution : Item, ISolution {32 public sealed class CombinedSolution : Item, ISolution { 33 33 34 34 private MultiEncoding Encoding { get; set; } … … 38 38 39 39 [StorableConstructor] 40 private MultiSolution(bool deserializing) : base(deserializing) { }40 private CombinedSolution(bool deserializing) : base(deserializing) { } 41 41 42 private MultiSolution(MultiSolution original, Cloner cloner)42 private CombinedSolution(CombinedSolution original, Cloner cloner) 43 43 : base(original, cloner) { 44 44 Encoding = cloner.Clone(original.Encoding); … … 46 46 solutions = original.solutions.ToDictionary(x => x.Key, x => cloner.Clone(x.Value)); 47 47 } 48 public MultiSolution(MultiEncoding encoding, IScope scope) {48 public CombinedSolution(IScope scope, MultiEncoding encoding) { 49 49 Encoding = encoding; 50 50 Scope = scope; … … 54 54 55 55 public override IDeepCloneable Clone(Cloner cloner) { 56 return new MultiSolution(this, cloner);56 return new CombinedSolution(this, cloner); 57 57 } 58 58 -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IMultiEncodingOperator.cs
r13351 r13356 24 24 namespace HeuristicLab.Optimization { 25 25 public interface IMultiEncodingOperator : IOperator { 26 void AddEncoding(IEncoding encoding); 27 bool RemoveEncoding(IEncoding encoding); 26 MultiEncoding Encoding { get; set; } 28 27 } 29 28 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/MultiEncoding.cs
r13351 r13356 31 31 [Item("MultiEncoding", "Describes a combined encoding consisting of multiple simpler encodings.")] 32 32 [StorableClass] 33 public sealed class MultiEncoding : Encoding< MultiSolution> {33 public sealed class MultiEncoding : Encoding<CombinedSolution> { 34 34 35 private readonly List<IEncoding> encodings; 35 private readonly ItemCollection<IEncoding> encodings; 36 36 37 [Storable] 37 p ublic IEnumerable<IEncoding>Encodings {38 private IEnumerable<IEncoding> StorableEncodings { 38 39 get { return encodings; } 39 private set { encodings.AddRange(value); } 40 set { encodings.AddRange(value); } 41 } 42 43 public ReadOnlyItemCollection<IEncoding> Encodings { 44 get { return encodings.AsReadOnly(); } 40 45 } 41 46 … … 43 48 private MultiEncoding(bool deserializing) 44 49 : base(deserializing) { 45 encodings = new List<IEncoding>();50 encodings = new ItemCollection<IEncoding>(); 46 51 } 47 52 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncoding(this, cloner); } 48 53 private MultiEncoding(MultiEncoding original, Cloner cloner) 49 54 : base(original, cloner) { 50 encodings = new List<IEncoding>(original.Encodings.Select(cloner.Clone));55 encodings = new ItemCollection<IEncoding>(original.Encodings.Select(cloner.Clone)); 51 56 } 52 57 public MultiEncoding() 53 58 : base("MultiEncoding") { 54 encodings = new List<IEncoding>();55 SolutionCreator = new MultiEncodingCreator() ;56 57 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>())59 encodings = new ItemCollection<IEncoding>(); 60 SolutionCreator = new MultiEncodingCreator() { Encoding = this }; 61 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) { 62 @operator.Encoding = this; 58 63 AddOperator(@operator); 64 } 59 65 } 60 66 … … 63 69 if (Encodings.Any(e => e.Name == encoding.Name)) throw new ArgumentException("Encoding name must be unique", "encoding.Name"); 64 70 encodings.Add(encoding); 65 66 71 Parameters.AddRange(encoding.Parameters); 67 68 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {69 @operator.AddEncoding(encoding);70 }71 OnEncodingsChanged();72 72 return this; 73 73 } … … 76 76 var success = encodings.Remove(encoding); 77 77 Parameters.RemoveRange(encoding.Parameters); 78 foreach (var @operator in Operators.OfType<IMultiEncodingOperator>()) {79 @operator.RemoveEncoding(encoding);80 }81 OnEncodingsChanged();82 78 return success; 83 79 } 84 80 85 public override void ConfigureOperators(IEnumerable<IOperator> operators) { 86 } 87 88 public event EventHandler EncodingsChanged; 89 private void OnEncodingsChanged() { 90 var handler = EncodingsChanged; 91 if (handler != null) handler(this, EventArgs.Empty); 92 } 81 public override void ConfigureOperators(IEnumerable<IOperator> operators) { } 93 82 } 94 83 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs
r13351 r13356 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Parameters; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 29 30 [Item("MultiEncodingCreator", "Contains solution creators that together create a multi-encoding.")] 30 31 [StorableClass] 31 public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<MultiSolution> { 32 public sealed class MultiEncodingCreator : MultiEncodingOperator<ISolutionCreator>, ISolutionCreator<CombinedSolution> { 33 32 34 [StorableConstructor] 33 35 private MultiEncodingCreator(bool deserializing) : base(deserializing) { } 34 36 private MultiEncodingCreator(MultiEncodingCreator original, Cloner cloner) : base(original, cloner) { } 35 public MultiEncodingCreator() { }37 public MultiEncodingCreator() : base() { } 36 38 37 39 public override IDeepCloneable Clone(Cloner cloner) { … … 39 41 } 40 42 41 p ublicoverride void AddEncoding(IEncoding encoding) {43 protected override void AddEncoding(IEncoding encoding) { 42 44 base.AddEncoding(encoding); 43 45 var parameter = GetParameter(encoding); … … 46 48 } 47 49 48 p ublicoverride bool RemoveEncoding(IEncoding encoding) {50 protected override bool RemoveEncoding(IEncoding encoding) { 49 51 var success = base.RemoveEncoding(encoding); 50 52 encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged; … … 61 63 parameter.Value = encoding.SolutionCreator; 62 64 } 65 66 67 public override IOperation InstrumentedApply() { 68 CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding); 69 return base.Apply(); 70 } 63 71 } 64 72 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCrossover.cs
r12012 r13356 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Parameters; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 … … 28 29 [StorableClass] 29 30 public sealed class MultiEncodingCrossover : MultiEncodingOperator<ICrossover>, ICrossover { 31 30 32 [StorableConstructor] 31 33 private MultiEncodingCrossover(bool deserializing) : base(deserializing) { } 32 34 private MultiEncodingCrossover(MultiEncodingCrossover original, Cloner cloner) : base(original, cloner) { } 33 public MultiEncodingCrossover() { }35 public MultiEncodingCrossover() : base() { } 34 36 35 37 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingCrossover(this, cloner); } 38 39 40 public override IOperation InstrumentedApply() { 41 CombinedSolutionParameter.ActualValue = new CombinedSolution(ExecutionContext.Scope, Encoding); 42 return base.Apply(); 43 } 36 44 } 37 45 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingManipulator.cs
r12012 r13356 30 30 [StorableConstructor] 31 31 private MultiEncodingManipulator(bool deserializing) : base(deserializing) { } 32 private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner) 33 : base(original, cloner) { } 34 public MultiEncodingManipulator() { } 32 private MultiEncodingManipulator(MultiEncodingManipulator original, Cloner cloner) : base(original, cloner) { } 33 public MultiEncodingManipulator() : base() { } 35 34 36 35 public override IDeepCloneable Clone(Cloner cloner) { return new MultiEncodingManipulator(this, cloner); } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingOperator.cs
r13351 r13356 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HeuristicLab.Collections; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 31 32 namespace HeuristicLab.Optimization { 32 33 [StorableClass] 33 public abstract class MultiEncodingOperator<T> : Operator, IMultiEncodingOperator where T : class, IOperator { 34 private List<IEncoding> encodings = new List<IEncoding>(); 35 [Storable(Name = "Encodings")] 36 private IEnumerable<IEncoding> StorableEncodings { 37 get { return encodings; } 38 set { encodings = new List<IEncoding>(value); } 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 } 47 } 48 49 public ILookupParameter<CombinedSolution> CombinedSolutionParameter { 50 get { return (ILookupParameter<CombinedSolution>)Parameters["CombinedSolution"]; } 39 51 } 40 52 41 53 [StorableConstructor] 42 protected MultiEncodingOperator(bool deserializing) 43 : base(deserializing) { 44 } 45 54 protected MultiEncodingOperator(bool deserializing) : base(deserializing) { } 46 55 protected MultiEncodingOperator(MultiEncodingOperator<T> original, Cloner cloner) 47 56 : base(original, cloner) { 48 encodings = new List<IEncoding>(original.encodings.Select(cloner.Clone)); 49 foreach (var encoding in encodings) 50 encoding.OperatorsChanged += Encoding_OperatorsChanged; 57 encoding = cloner.Clone(original.encoding); 58 RegisterEventHandlers(); 51 59 } 52 53 protected MultiEncodingOperator() : base() { } 60 protected MultiEncodingOperator() 61 : base() { 62 Parameters.Add(new LookupParameter<CombinedSolution>("CombinedSolution", "The combined solution that gets created.")); 63 } 54 64 55 65 [StorableHook(HookType.AfterDeserialization)] 56 66 private void AfterDeserialization() { 57 foreach (var encoding in encodings) 58 encoding.OperatorsChanged += Encoding_OperatorsChanged; 67 RegisterEventHandlers(); 59 68 } 60 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 } 61 77 62 public override IOperation Apply() { 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); 96 } 97 98 public override IOperation InstrumentedApply() { 63 99 var operations = Parameters.Select(p => p.ActualValue).OfType<IOperator>().Select(op => ExecutionContext.CreateOperation(op)); 64 100 return new OperationCollection(operations); 65 101 } 66 102 67 p ublicvirtual void AddEncoding(IEncoding encoding) {103 protected virtual void AddEncoding(IEncoding encoding) { 68 104 if (Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was already added.", encoding.Name)); 69 105 70 encodings.Add(encoding);71 106 encoding.OperatorsChanged += Encoding_OperatorsChanged; 72 107 … … 76 111 } 77 112 78 p ublicvirtual bool RemoveEncoding(IEncoding encoding) {79 if (! encodings.Remove(encoding)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name));113 protected virtual bool RemoveEncoding(IEncoding encoding) { 114 if (!Parameters.ContainsKey(encoding.Name)) throw new ArgumentException(string.Format("Encoding {0} was not added to the MultiEncoding.", encoding.Name)); 80 115 encoding.OperatorsChanged -= Encoding_OperatorsChanged; 81 116 return Parameters.Remove(encoding.Name); … … 89 124 90 125 private void Encoding_OperatorsChanged(object sender, EventArgs e) { 91 var enc oding= (IEncoding)sender;92 var param = GetParameter(enc oding);126 var enc = (IEncoding)sender; 127 var param = GetParameter(enc); 93 128 94 129 var oldParameterValue = param.Value; 95 130 param.ValidValues.Clear(); 96 foreach (var op in enc oding.Operators.OfType<T>())131 foreach (var op in enc.Operators.OfType<T>()) 97 132 param.ValidValues.Add(op); 98 133 -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r13351 r13356 122 122 <Compile Include="Algorithms\BasicAlgorithm.cs" /> 123 123 <Compile Include="BasicProblems\Individuals\MultiSolution.cs" /> 124 <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" /> 124 125 <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" /> 125 126 <Compile Include="BasicProblems\MultiEncoding.cs" /> … … 132 133 <Compile Include="BasicProblems\Interfaces\IEncoding.cs" /> 133 134 <Compile Include="BasicProblems\Interfaces\IEncodingOperator.cs" /> 134 <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" />135 135 <Compile Include="BasicProblems\Interfaces\IMultiObjectiveProblemDefinition.cs" /> 136 136 <Compile Include="BasicProblems\Interfaces\internal\IMultiObjectiveAnalysisOperator.cs" /> -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/MultiObjectiveProgrammableProblems.cs
r13351 r13356 56 56 [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)] 57 57 [StorableClass] 58 public sealed class MultiObjectiveMultiSolutionProgrammableProblem : MultiObjectiveProgrammableProblem<MultiEncoding, MultiSolution> {58 public sealed class MultiObjectiveMultiSolutionProgrammableProblem : MultiObjectiveProgrammableProblem<MultiEncoding, CombinedSolution> { 59 59 60 60 [StorableConstructor] -
branches/ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblems.cs
r13351 r13356 56 56 [Creatable(CreatableAttribute.Categories.Problems, Priority = 100)] 57 57 [StorableClass] 58 public sealed class SingleObjectiveMultiSolutionProgrammableProblem : SingleObjectiveProgrammableProblem<MultiEncoding, MultiSolution> {58 public sealed class SingleObjectiveMultiSolutionProgrammableProblem : SingleObjectiveProgrammableProblem<MultiEncoding, CombinedSolution> { 59 59 60 60 [StorableConstructor]
Note: See TracChangeset
for help on using the changeset viewer.