Changeset 13469 for branches/ProblemRefactoring/HeuristicLab.Optimization
- Timestamp:
- 12/15/15 15:16:24 (9 years ago)
- Location:
- branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Encoding.cs
r13396 r13469 53 53 foreach (var op in value) encodingOperators.Add(op); 54 54 55 ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == solutionCreator.GetType()) ??55 ISolutionCreator<TSolution> newSolutionCreator = (ISolutionCreator<TSolution>)encodingOperators.FirstOrDefault(o => o.GetType() == SolutionCreator.GetType()) ?? 56 56 encodingOperators.OfType<ISolutionCreator<TSolution>>().First(); 57 57 SolutionCreator = newSolutionCreator; … … 60 60 } 61 61 62 ISolutionCreator IEncoding.SolutionCreator {63 get { return solutionCreator; }62 public IValueParameter SolutionCreatorParameter { 63 get { return (IValueParameter)Parameters[Name + ".SolutionCreator"]; } 64 64 } 65 65 66 [Storable] 67 private ISolutionCreator<TSolution> solutionCreator; 66 ISolutionCreator IEncoding.SolutionCreator { 67 get { return SolutionCreator; } 68 } 68 69 public ISolutionCreator<TSolution> SolutionCreator { 69 get { 70 return solutionCreator; 71 } 70 get { return (ISolutionCreator<TSolution>)SolutionCreatorParameter.Value; } 72 71 set { 73 72 if (value == null) throw new ArgumentNullException("SolutionCreator must not be null."); 74 if (solutionCreator == value) return; 75 encodingOperators.Remove(solutionCreator); 73 encodingOperators.Remove(SolutionCreator); 76 74 encodingOperators.Add(value); 77 solutionCreator= value;75 SolutionCreatorParameter.Value = value; 78 76 OnSolutionCreatorChanged(); 79 77 } 80 78 } 81 79 80 82 81 [StorableConstructor] 83 82 protected Encoding(bool deserializing) : base(deserializing) { } 83 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 RegisterEventHandlers(); 86 } 87 84 88 protected Encoding(Encoding<TSolution> original, Cloner cloner) 85 89 : base(original, cloner) { 86 90 encodingOperators = cloner.Clone(original.encodingOperators); 87 solutionCreator = cloner.Clone(original.solutionCreator); 91 92 RegisterEventHandlers(); 88 93 } 94 89 95 protected Encoding(string name) 90 96 : base(name) { 97 Parameters.Add(new ValueParameter<ISolutionCreator<TSolution>>(name + ".SolutionCreator", "The operator to create a solution.")); 91 98 Parameters.Add(new FixedValueParameter<ReadOnlyItemSet<IOperator>>(name + ".Operators", "The operators that the encoding specifies.", encodingOperators.AsReadOnly())); 99 100 RegisterEventHandlers(); 101 } 102 103 private void RegisterEventHandlers() { 104 SolutionCreatorParameter.ValueChanged += (o, e) => OnSolutionCreatorChanged(); 92 105 } 93 106 -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/IEncoding.cs
r13396 r13469 26 26 namespace HeuristicLab.Optimization { 27 27 public interface IEncoding : IParameterizedNamedItem { 28 IValueParameter SolutionCreatorParameter { get; } 28 29 ISolutionCreator SolutionCreator { get; } 30 29 31 IEnumerable<IOperator> Operators { get; set; } 30 32 … … 32 34 void ConfigureOperators(IEnumerable<IItem> operators); 33 35 36 event EventHandler OperatorsChanged; 34 37 event EventHandler SolutionCreatorChanged; 35 event EventHandler OperatorsChanged;36 38 } 37 39 38 40 public interface IEncoding<TSolution> : IEncoding 39 41 where TSolution : class, ISolution { 40 new ISolutionCreator<TSolution> SolutionCreator { get; set; }42 //new ISolutionCreator<TSolution> SolutionCreator { get; } 41 43 } 42 44 } -
branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r13396 r13469 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableClass] 32 public abstract class Problem<TEncoding, TSolution, TEvaluator> : HeuristicOptimizationProblem<TEvaluator, ISolutionCreator<TSolution>>, IProblemDefinition<TEncoding, TSolution>, IStorableContent 32 public abstract class Problem<TEncoding, TSolution, TEvaluator> : Problem, 33 IHeuristicOptimizationProblem, IProblemDefinition<TEncoding, TSolution>, IStorableContent 33 34 where TEncoding : class, IEncoding<TSolution> 34 35 where TSolution : class, ISolution 35 36 where TEvaluator : class, IEvaluator { 36 37 37 public string Filename { get; set; } // TODO: Really okay here? 38 public string Filename { get; set; } // TODO: Really okay here? should be in Problem (non-generic) 38 39 40 //TODO remove parametr for encoding? 39 41 protected IValueParameter<TEncoding> EncodingParameter { 40 42 get { return (IValueParameter<TEncoding>)Parameters["Encoding"]; } 41 43 } 42 43 44 //mkommend necessary for reuse of operators if the encoding changes 44 45 private TEncoding oldEncoding; 45 46 46 public TEncoding Encoding { 47 47 get { return EncodingParameter.Value; } … … 51 51 } 52 52 } 53 54 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { 55 get { return Encoding.SolutionCreator; } 56 } 57 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { 58 get { return Encoding.SolutionCreatorParameter; } 59 } 60 event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged { 61 add { Encoding.SolutionCreatorChanged += value; } 62 remove { Encoding.SolutionCreatorChanged -= value; } 63 } 64 65 //TODO is a parameter for the evaluator really necessary, only single-objective or multi-objective evulators calling the func are possible 66 public ValueParameter<TEvaluator> EvaluatorParameter { 67 get { return (ValueParameter<TEvaluator>)Parameters["Evaluator"]; } 68 } 69 public TEvaluator Evaluator { 70 get { return EvaluatorParameter.Value; } 71 protected set { EvaluatorParameter.Value = value; } 72 } 73 IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } } 74 IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } } 75 76 public event EventHandler EvaluatorChanged; 77 protected virtual void OnEvaluatorChanged() { 78 EventHandler handler = EvaluatorChanged; 79 if (handler != null) 80 handler(this, EventArgs.Empty); 81 } 82 53 83 54 84 protected override IEnumerable<IItem> GetOperators() { … … 66 96 : base() { 67 97 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.")); 98 Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.")); 99 68 100 if (Encoding != null) { 69 101 oldEncoding = Encoding; 70 SolutionCreator = Encoding.SolutionCreator;71 102 Parameterize(); 72 103 } … … 76 107 if (encoding == null) throw new ArgumentNullException("encoding"); 77 108 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)); 109 Parameters.Add(new ValueParameter<TEvaluator>("Evaluator", "The operator used to evaluate a solution.")); 110 78 111 oldEncoding = Encoding; 79 SolutionCreator = Encoding.SolutionCreator;80 112 Parameterize(); 81 113 … … 99 131 private void RegisterEvents() { 100 132 EncodingParameter.ValueChanged += (o, e) => OnEncodingChanged(); 133 EvaluatorParameter.ValueChanged += (o, e) => OnEvaluatorChanged(); 101 134 //var multiEncoding = Encoding as MultiEncoding; 102 135 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; … … 122 155 op.EncodingParameter.ActualName = EncodingParameter.Name; 123 156 124 SolutionCreator = Encoding.SolutionCreator;125 126 157 //var multiEncoding = Encoding as MultiEncoding; 127 158 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; 128 159 } 129 160 130 protected override void OnSolutionCreatorChanged() {131 base.OnSolutionCreatorChanged();132 Encoding.SolutionCreator = SolutionCreator;133 }161 //protected override void OnSolutionCreatorChanged() { 162 // base.OnSolutionCreatorChanged(); 163 // Encoding.SolutionCreator = SolutionCreator; 164 //} 134 165 135 166 private static void AdaptEncodingOperators(IEncoding oldEncoding, IEncoding newEncoding) { 136 167 if (oldEncoding.GetType() != newEncoding.GetType()) return; 137 168 138 if (oldEncoding .GetType() == typeof(CombinedEncoding)) {169 if (oldEncoding is CombinedEncoding) { 139 170 var oldMultiEncoding = (CombinedEncoding)oldEncoding; 140 171 var newMultiEncoding = (CombinedEncoding)newEncoding;
Note: See TracChangeset
for help on using the changeset viewer.