- Timestamp:
- 06/19/20 17:53:36 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs
r17517 r17614 185 185 protected override void DeregisterProblemEvents() { 186 186 Problem.Reset -= new EventHandler(Problem_Reset); 187 Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged); 187 188 } 188 189 protected override void RegisterProblemEvents() { 189 190 Problem.Reset += new EventHandler(Problem_Reset); 191 Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged); 190 192 } 191 193 protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/CombinedEncoding.cs
r17226 r17614 55 55 : base("CombinedEncoding") { 56 56 encodings = new ItemCollection<IEncoding>(); 57 SolutionCreator = new MultiEncodingCreator() { SolutionParameter = { ActualName = Name } };58 57 foreach (var @operator in ApplicationManager.Manager.GetInstances<IMultiEncodingOperator>()) { 59 58 @operator.SolutionParameter.ActualName = Name; -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r17571 r17614 52 52 encodingOperators.Clear(); 53 53 foreach (var op in value) encodingOperators.Add(op); 54 55 ISolutionCreator<TEncodedSolution> newSolutionCreator = (ISolutionCreator<TEncodedSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ??56 encodingOperators.OfType<ISolutionCreator<TEncodedSolution>>().First();57 SolutionCreator = newSolutionCreator;58 54 OnOperatorsChanged(); 59 }60 }61 62 public IValueParameter SolutionCreatorParameter {63 get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; }64 }65 66 ISolutionCreator IEncoding.SolutionCreator {67 get { return SolutionCreator; }68 }69 public ISolutionCreator<TEncodedSolution> SolutionCreator {70 get { return (ISolutionCreator<TEncodedSolution>)SolutionCreatorParameter.Value; }71 set {72 if (value == null) throw new ArgumentNullException("SolutionCreator must not be null.");73 encodingOperators.Remove(SolutionCreator);74 encodingOperators.Add(value);75 SolutionCreatorParameter.Value = value;76 OnSolutionCreatorChanged();77 55 } 78 56 } … … 82 60 protected Encoding(StorableConstructorFlag _) : base(_) { } 83 61 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 RegisterEventHandlers(); 86 } 62 private void AfterDeserialization() { } 87 63 88 64 protected Encoding(Encoding<TEncodedSolution> original, Cloner cloner) 89 65 : base(original, cloner) { 90 66 encodingOperators = cloner.Clone(original.encodingOperators); 91 92 RegisterEventHandlers();93 67 } 94 68 95 69 protected Encoding(string name) 96 70 : base(name) { 97 Parameters.Add(new ValueParameter<ISolutionCreator<TEncodedSolution>>(name + ".SolutionCreator", "The operator to create a solution.") {98 ReadOnly = true99 });100 71 Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly()) { 101 72 GetsCollected = false, ReadOnly = true 102 73 }); 103 104 RegisterEventHandlers();105 }106 107 private void RegisterEventHandlers() {108 SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged();109 74 } 110 75 … … 137 102 } 138 103 139 public event EventHandler SolutionCreatorChanged;140 protected virtual void OnSolutionCreatorChanged() {141 ConfigureOperator(SolutionCreator);142 var handler = SolutionCreatorChanged;143 if (handler != null) handler(this, EventArgs.Empty);144 }145 146 104 public event EventHandler OperatorsChanged; 147 105 protected virtual void OnOperatorsChanged() { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs
r17226 r17614 28 28 [StorableType("d70b2675-246c-489c-a91b-b2e19a1616a3")] 29 29 public interface IEncoding : IParameterizedNamedItem { 30 IValueParameter SolutionCreatorParameter { get; }31 ISolutionCreator SolutionCreator { get; }32 33 30 IEnumerable<IOperator> Operators { get; set; } 34 31 … … 37 34 38 35 event EventHandler OperatorsChanged; 39 event EventHandler SolutionCreatorChanged;40 36 } 41 37 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IProblem.cs
r17612 r17614 40 40 //TODO Intermediate class for compatibility 41 41 //TODO move members to generic IProblem after every problem used the new architecture 42 //TODO ABE: We can maybe use it as non-generic interface that exports IEncoding Encoding { get; } 43 //TODO ABE: and which is explicitely implemented in some base class 42 44 public interface IEncodedProblem : IProblem { 43 45 IEnumerable<IItem> Operators { get; } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/MultiEncodingCreator.cs
r17226 r17614 20 20 #endregion 21 21 22 using System;23 using System.Linq;24 22 using HEAL.Attic; 25 23 using HeuristicLab.Common; … … 57 55 } 58 56 59 public override void AddEncoding(IEncoding encoding) {60 base.AddEncoding(encoding);61 var parameter = GetParameter(encoding);62 parameter.Value = encoding.SolutionCreator;63 encoding.SolutionCreatorChanged += Encoding_SolutionCreatorChanged;64 }65 66 public override bool RemoveEncoding(IEncoding encoding) {67 var success = base.RemoveEncoding(encoding);68 encoding.SolutionCreatorChanged -= Encoding_SolutionCreatorChanged;69 return success;70 }71 72 private void Encoding_SolutionCreatorChanged(object sender, EventArgs e) {73 var encoding = (IEncoding)sender;74 var parameter = GetParameter(encoding);75 76 var oldCreator = parameter.ValidValues.Single(creator => creator.GetType() == encoding.SolutionCreator.GetType());77 parameter.ValidValues.Remove(oldCreator);78 parameter.ValidValues.Add(encoding.SolutionCreator);79 parameter.Value = encoding.SolutionCreator;80 }81 82 57 83 58 public override IOperation InstrumentedApply() { -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r17612 r17614 37 37 where TEvaluator : class, IEvaluator { 38 38 39 [Storable] protected ConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; } 39 40 40 41 //TODO remove parameter for encoding? … … 52 53 } 53 54 54 55 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { 56 get { return Encoding.SolutionCreator; } 57 } 58 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { 59 get { return Encoding.SolutionCreatorParameter; } 60 } 55 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get => SolutionCreatorParameter.Value; } 56 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get => SolutionCreatorParameter; } 57 61 58 event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged { 62 59 add { 63 if (Encoding != null) Encoding.SolutionCreatorChanged += value;60 SolutionCreatorParameter.ValueChanged += value; 64 61 } 65 62 remove { 66 if (Encoding != null) Encoding.SolutionCreatorChanged -= value;63 SolutionCreatorParameter.ValueChanged -= value; 67 64 } 68 65 } … … 106 103 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.", encoding) { Hidden = true }); 107 104 Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.") { Hidden = true }); 105 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution.")); 108 106 109 107 oldEncoding = Encoding; … … 116 114 : base(original, cloner) { 117 115 oldEncoding = cloner.Clone(original.oldEncoding); 116 SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter); 118 117 RegisterEvents(); 119 118 } … … 124 123 private void AfterDeserialization() { 125 124 oldEncoding = Encoding; 125 // TODO: remove below 126 if (SolutionCreatorParameter == null) Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution.")); 127 126 128 RegisterEvents(); 127 129 } … … 157 159 158 160 Encoding.ConfigureOperators(Operators); 161 162 SolutionCreatorParameter.Repopulate(GetOperators()); 159 163 //var multiEncoding = Encoding as MultiEncoding; 160 164 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged;
Note: See TracChangeset
for help on using the changeset viewer.