Changeset 11753 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/ProgrammableProblem.cs
- Timestamp:
- 01/13/15 18:47:19 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/ProgrammableProblem.cs
r11739 r11753 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Problems.Programmable.Interfaces; 30 31 31 32 namespace HeuristicLab.Problems.Programmable { … … 46 47 protected set { 47 48 if (value == null) throw new ArgumentNullException("Encoding must not be null."); 48 if (Encoding != null) Parameters.RemoveRange(Encoding.Parameters);49 49 EncodingParameter.Value = value; 50 Parameters.AddRange(Encoding.Parameters);51 50 } 52 51 } 52 //mkommend necessary for reuse of operators if the encoding changes 53 private TEncoding oldEncoding; 53 54 54 55 public virtual IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) { … … 57 58 58 59 protected override IEnumerable<IItem> GetOperators() { 60 if (Encoding == null) return base.GetOperators(); 59 61 return base.GetOperators().Concat(Encoding.Operators); 60 62 } 61 63 public override IEnumerable<IParameterizedItem> ExecutionContextItems { 62 get { return base.ExecutionContextItems.Concat(new[] { Encoding }); } 64 get { 65 if (Encoding == null) return base.ExecutionContextItems; 66 return base.ExecutionContextItems.Concat(new[] { Encoding }); 67 } 63 68 } 64 69 65 p ublicProgrammableProblem()70 protected ProgrammableProblem() 66 71 : base() { 67 72 Parameters.Add(new ValueParameter<TEncoding>("Encoding", "Describes the configuration of the encoding, what the variables are called, what type they are and their bounds if any.")); 73 oldEncoding = Encoding; 68 74 RegisterEvents(); 69 75 } … … 71 77 protected ProgrammableProblem(ProgrammableProblem<TEncoding, TEvaluator> original, Cloner cloner) 72 78 : base(original, cloner) { 79 oldEncoding = cloner.Clone(original.oldEncoding); 73 80 RegisterEvents(); 74 81 } 75 82 76 83 [StorableConstructor] 77 protected ProgrammableProblem(bool deserializing) 78 : base(deserializing) { 84 protected ProgrammableProblem(bool deserializing) : base(deserializing) { } 85 [StorableHook(HookType.AfterDeserialization)] 86 private void AfterDeserialization() { 87 oldEncoding = Encoding; 79 88 RegisterEvents(); 80 89 } … … 85 94 86 95 protected virtual void OnEncodingChanged() { 96 if (oldEncoding != null) AdaptEncodingOperators(oldEncoding, Encoding); 97 oldEncoding = Encoding; 98 87 99 var solutionCreatorType = Encoding.GetType().BaseType.GetGenericArguments(); 88 100 var paramType = typeof(ValueParameter<>).MakeGenericType(solutionCreatorType); … … 91 103 Parameters.Remove(SolutionCreatorParameter); 92 104 Parameters.Add(solutionCreatorParam); 105 106 OnOperatorsChanged(); 107 OnReset(); 93 108 } 94 109 … … 106 121 } 107 122 123 private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) { 124 if (oldEncoding.GetType() != newEncoding.GetType()) return; 125 126 if (oldEncoding.GetType() == typeof(MultiEncoding)) { 127 var oldMultiEncoding = (MultiEncoding)oldEncoding; 128 var newMultiEncoding = (MultiEncoding)newEncoding; 129 if (!oldMultiEncoding.Encodings.SequenceEqual(newMultiEncoding.Encodings, new TypeEqualityComparer<IEncoding>())) return; 130 131 var nestedEncodings = oldMultiEncoding.Encodings.Zip(newMultiEncoding.Encodings, (o, n) => new { oldEnc = o, newEnc = n }); 132 foreach (var multi in nestedEncodings) 133 AdaptEncodingOperators(multi.oldEnc, multi.newEnc); 134 135 foreach (var op in oldMultiEncoding.Operators.OfType<IMultiEncodingOperator>()) 136 foreach (var multi in nestedEncodings) 137 op.ReplaceEncoding(multi.oldEnc, multi.newEnc); 138 } 139 140 var comparer = new TypeEqualityComparer<IOperator>(); 141 var oldOperators = oldEncoding.Operators; 142 var newOperators = newEncoding.Operators; 143 144 var operators = oldOperators.Intersect(newOperators, comparer); 145 operators = operators.Union(newOperators, comparer); 146 147 newEncoding.ConfigureOperators(operators); 148 newEncoding.Operators = operators; 149 } 150 108 151 } 109 152 }
Note: See TracChangeset
for help on using the changeset viewer.