- Timestamp:
- 07/23/20 16:21:55 (4 years ago)
- Location:
- branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3
- Files:
-
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationEngineAlgorithm.cs
r17226 r17695 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using H EAL.Attic;26 using HeuristicLab.Parameters; 26 27 27 28 namespace HeuristicLab.Optimization { … … 37 38 } 38 39 39 protected HeuristicOptimizationEngineAlgorithm() : base() { } 40 protected HeuristicOptimizationEngineAlgorithm(string name) : base(name) { } 41 protected HeuristicOptimizationEngineAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { } 42 protected HeuristicOptimizationEngineAlgorithm(string name, string description) : base(name, description) { } 43 protected HeuristicOptimizationEngineAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { } 40 [Storable] public IConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; } 41 public ISolutionCreator SolutionCreator { 42 get => SolutionCreatorParameter.Value; 43 set { 44 if (!SolutionCreatorParameter.ValidValues.Contains(value)) 45 SolutionCreatorParameter.ValidValues.Add(value); 46 SolutionCreatorParameter.Value = value; 47 } 48 } 49 50 protected HeuristicOptimizationEngineAlgorithm() : base() { 51 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem.")); 52 53 RegisterEventHandlers(); 54 } 55 protected HeuristicOptimizationEngineAlgorithm(string name) : base(name) { 56 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem.")); 57 58 RegisterEventHandlers(); 59 } 60 protected HeuristicOptimizationEngineAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { 61 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem.")); 62 63 RegisterEventHandlers(); 64 } 65 protected HeuristicOptimizationEngineAlgorithm(string name, string description) : base(name, description) { 66 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem.")); 67 68 RegisterEventHandlers(); 69 } 70 protected HeuristicOptimizationEngineAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { 71 Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "An operator that creates a solution for a given problem.")); 72 73 RegisterEventHandlers(); 74 } 44 75 45 76 [StorableConstructor] 46 77 protected HeuristicOptimizationEngineAlgorithm(StorableConstructorFlag _) : base(_) { } 47 protected HeuristicOptimizationEngineAlgorithm(HeuristicOptimizationEngineAlgorithm original, Cloner cloner) : base(original, cloner) { } 78 protected HeuristicOptimizationEngineAlgorithm(HeuristicOptimizationEngineAlgorithm original, Cloner cloner) : base(original, cloner) { 79 SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter); 80 81 RegisterEventHandlers(); 82 } 83 84 [StorableHook(HookType.AfterDeserialization)] 85 private void AfterDeserialization() { 86 RegisterEventHandlers(); 87 } 48 88 49 89 #region Events 90 private void RegisterEventHandlers() { 91 ParameterChangeHandler<ISolutionCreator>.Create(SolutionCreatorParameter, SolutionCreatorOnChanged); 92 } 50 93 protected override void DeregisterProblemEvents() { 51 Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged); 52 Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged); 94 Problem.EvaluatorChanged -= Problem_EvaluatorChanged; 53 95 base.DeregisterProblemEvents(); 54 96 } 55 97 protected override void RegisterProblemEvents() { 56 98 base.RegisterProblemEvents(); 57 Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged); 58 Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged); 99 Problem.EvaluatorChanged += Problem_EvaluatorChanged; 100 } 101 protected override void OnProblemChanged() { 102 base.OnProblemChanged(); 103 SolutionCreatorParameter.Repopulate(Problem.Operators); 59 104 } 60 105 61 protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }106 protected virtual void SolutionCreatorOnChanged() { } 62 107 protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { } 108 109 protected override void Problem_OperatorsChanged(object sender, EventArgs e) { 110 base.Problem_OperatorsChanged(sender, e); 111 SolutionCreatorParameter.Repopulate(Problem.Operators); 112 } 63 113 #endregion 64 114 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Problem.cs
r17614 r17695 37 37 where TEvaluator : class, IEvaluator { 38 38 39 [Storable] protected ConstrainedValueParameter<ISolutionCreator> SolutionCreatorParameter { get; private set; }40 41 39 //TODO remove parameter for encoding? 42 40 protected IValueParameter<TEncoding> EncodingParameter { … … 50 48 if (value == null) throw new ArgumentNullException("Encoding must not be null."); 51 49 EncodingParameter.Value = value; 52 }53 }54 55 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get => SolutionCreatorParameter.Value; }56 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get => SolutionCreatorParameter; }57 58 event EventHandler IHeuristicOptimizationProblem.SolutionCreatorChanged {59 add {60 SolutionCreatorParameter.ValueChanged += value;61 }62 remove {63 SolutionCreatorParameter.ValueChanged -= value;64 50 } 65 51 } … … 103 89 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 }); 104 90 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.")); 106 91 107 92 oldEncoding = Encoding; 108 93 Parameterize(); … … 114 99 : base(original, cloner) { 115 100 oldEncoding = cloner.Clone(original.oldEncoding); 116 SolutionCreatorParameter = cloner.Clone(original.SolutionCreatorParameter);117 101 RegisterEvents(); 118 102 } … … 123 107 private void AfterDeserialization() { 124 108 oldEncoding = Encoding; 125 // TODO: remove below126 if (SolutionCreatorParameter == null) Parameters.Add(SolutionCreatorParameter = new ConstrainedValueParameter<ISolutionCreator>("SolutionCreator", "The operator used to create a solution."));127 128 109 RegisterEvents(); 129 110 } … … 160 141 Encoding.ConfigureOperators(Operators); 161 142 162 SolutionCreatorParameter.Repopulate(GetOperators());163 143 //var multiEncoding = Encoding as MultiEncoding; 164 144 //if (multiEncoding != null) multiEncoding.EncodingsChanged += MultiEncodingOnEncodingsChanged; -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r17594 r17695 203 203 <Compile Include="Algorithms\HeuristicOptimizationEngineAlgorithm.cs" /> 204 204 <Compile Include="Interfaces\ILocalImprovementOperator.cs" /> 205 <Compile Include="Algorithms\HeuristicOptimizationAlgorithm.cs" />206 205 <Compile Include="Interfaces\IMultiNeighborhoodShakingOperator.cs" /> 207 206 <Compile Include="MoveHelper.cs" /> -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Interfaces/IHeuristicOptimizationProblem.cs
r17513 r17695 30 30 /// </summary> 31 31 public interface IHeuristicOptimizationProblem : IEncodedProblem { 32 IParameter SolutionCreatorParameter { get; }33 ISolutionCreator SolutionCreator { get; }34 32 IParameter EvaluatorParameter { get; } 35 33 IEvaluator Evaluator { get; } 36 34 37 event EventHandler SolutionCreatorChanged;38 35 event EventHandler EvaluatorChanged; 39 36 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/HeuristicOptimizationProblem.cs
r17513 r17695 29 29 [Item("Heuristic Optimization Problem", "Represents the base class for a heuristic optimization problem.")] 30 30 [StorableType("DE0478BA-3797-4AC3-9A89-3734D2643823")] 31 public abstract class HeuristicOptimizationProblem<T, U> : EncodedProblem, IHeuristicOptimizationProblem 32 where T : class, IEvaluator 33 where U : class, ISolutionCreator { 31 public abstract class HeuristicOptimizationProblem<T> : EncodedProblem, IHeuristicOptimizationProblem 32 where T : class, IEvaluator { 34 33 private const string EvaluatorParameterName = "Evaluator"; 35 private const string SolutionCreateParameterName = "SolutionCreator";36 34 37 35 [StorableConstructor] 38 36 protected HeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { } 39 protected HeuristicOptimizationProblem(HeuristicOptimizationProblem<T , U> original, Cloner cloner)37 protected HeuristicOptimizationProblem(HeuristicOptimizationProblem<T> original, Cloner cloner) 40 38 : base(original, cloner) { 41 39 RegisterEventHandlers(); … … 45 43 : base() { 46 44 Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution.")); 47 Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution."));48 45 RegisterEventHandlers(); 49 46 } 50 47 51 protected HeuristicOptimizationProblem(T evaluator , U solutionCreator)48 protected HeuristicOptimizationProblem(T evaluator) 52 49 : base() { 53 50 Parameters.Add(new ValueParameter<T>(EvaluatorParameterName, "The operator used to evaluate a solution.", evaluator)); 54 Parameters.Add(new ValueParameter<U>(SolutionCreateParameterName, "The operator to create a solution.", solutionCreator));55 51 RegisterEventHandlers(); 56 52 } … … 63 59 private void RegisterEventHandlers() { 64 60 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 65 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);66 61 } 67 62 … … 76 71 IEvaluator IHeuristicOptimizationProblem.Evaluator { get { return Evaluator; } } 77 72 IParameter IHeuristicOptimizationProblem.EvaluatorParameter { get { return EvaluatorParameter; } } 78 79 public U SolutionCreator {80 get { return (U)SolutionCreatorParameter.Value; }81 protected set { SolutionCreatorParameter.Value = value; }82 }83 public IValueParameter SolutionCreatorParameter {84 get { return (IValueParameter)Parameters[SolutionCreateParameterName]; }85 }86 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator { get { return SolutionCreator; } }87 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter { get { return SolutionCreatorParameter; } }88 73 #endregion 89 74 … … 98 83 handler(this, EventArgs.Empty); 99 84 } 100 101 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {102 OnSolutionCreatorChanged();103 }104 public event EventHandler SolutionCreatorChanged;105 protected virtual void OnSolutionCreatorChanged() {106 EventHandler handler = SolutionCreatorChanged;107 if (handler != null)108 handler(this, EventArgs.Empty);109 }110 85 #endregion 111 86 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/MultiObjectiveHeuristicOptimizationProblem.cs
r17317 r17695 30 30 [Item("Multi-Objective Heuristic Optimization Problem", "A base class for multi-objective heuristic optimization problems.")] 31 31 [StorableType("C46643E3-7144-4884-A30A-5329BD80DC4E")] 32 public abstract class MultiObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, IMultiObjectiveHeuristicOptimizationProblem 33 where T : class, IMultiObjectiveEvaluator 34 where U : class, ISolutionCreator { 32 public abstract class MultiObjectiveHeuristicOptimizationProblem<T> : HeuristicOptimizationProblem<T>, IMultiObjectiveHeuristicOptimizationProblem 33 where T : class, IMultiObjectiveEvaluator { 35 34 private const string MaximizationParameterName = "Maximization"; 36 35 37 36 [StorableConstructor] 38 37 protected MultiObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { } 39 protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T , U> original, Cloner cloner)38 protected MultiObjectiveHeuristicOptimizationProblem(MultiObjectiveHeuristicOptimizationProblem<T> original, Cloner cloner) 40 39 : base(original, cloner) { 41 40 RegisterEventHandlers(); … … 48 47 } 49 48 50 protected MultiObjectiveHeuristicOptimizationProblem(T evaluator , U solutionCreator)51 : base(evaluator , solutionCreator) {49 protected MultiObjectiveHeuristicOptimizationProblem(T evaluator) 50 : base(evaluator) { 52 51 Parameters.Add(new ValueParameter<BoolArray>(MaximizationParameterName, "Determines for each objective whether it should be maximized or minimized.")); 53 52 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/SingleObjectiveHeuristicOptimizationProblem.cs
r17317 r17695 30 30 [Item("Single-Objective Heuristic OptimizationProblem", "A base class for single-objective heuristic optimization problems.")] 31 31 [StorableType("DFD5588E-6AB2-4712-9083-A405EF21226F")] 32 public abstract class SingleObjectiveHeuristicOptimizationProblem<T, U> : HeuristicOptimizationProblem<T, U>, ISingleObjectiveHeuristicOptimizationProblem 33 where T : class, ISingleObjectiveEvaluator 34 where U : class, ISolutionCreator { 32 public abstract class SingleObjectiveHeuristicOptimizationProblem<T> : HeuristicOptimizationProblem<T>, ISingleObjectiveHeuristicOptimizationProblem 33 where T : class, ISingleObjectiveEvaluator { 35 34 private const string MaximizationParameterName = "Maximization"; 36 35 private const string BestKnownQualityParameterName = "BestKnownQuality"; … … 38 37 [StorableConstructor] 39 38 protected SingleObjectiveHeuristicOptimizationProblem(StorableConstructorFlag _) : base(_) { } 40 protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T , U> original, Cloner cloner)39 protected SingleObjectiveHeuristicOptimizationProblem(SingleObjectiveHeuristicOptimizationProblem<T> original, Cloner cloner) 41 40 : base(original, cloner) { 42 41 RegisterEventHandlers(); … … 50 49 } 51 50 52 protected SingleObjectiveHeuristicOptimizationProblem(T evaluator , U solutionCreator)53 : base(evaluator , solutionCreator) {51 protected SingleObjectiveHeuristicOptimizationProblem(T evaluator) 52 : base(evaluator) { 54 53 Parameters.Add(new ValueParameter<BoolValue>(MaximizationParameterName, "Set to false if the problem should be minimized.", new BoolValue())); 55 54 Parameters.Add(new OptionalValueParameter<DoubleValue>(BestKnownQualityParameterName, "The quality of the best known solution of this problem.")); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/Problems/UserDefinedProblem.cs
r17612 r17695 62 62 get { return MaximizationParameter; } 63 63 } 64 public ValueParameter<ISolutionCreator> SolutionCreatorParameter {65 get { return (ValueParameter<ISolutionCreator>)Parameters["SolutionCreator"]; }66 }67 IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {68 get { return SolutionCreatorParameter; }69 }70 64 IParameter IHeuristicOptimizationProblem.EvaluatorParameter { 71 65 get { return EvaluatorParameter; } … … 89 83 get { return MaximizationParameter.Value; } 90 84 set { MaximizationParameter.Value = value; } 91 }92 public ISolutionCreator SolutionCreator {93 get { return SolutionCreatorParameter.Value; }94 set { SolutionCreatorParameter.Value = value; }95 }96 ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {97 get { return SolutionCreatorParameter.Value; }98 85 } 99 86 public ISingleObjectiveEvaluator Evaluator { … … 138 125 : base() { 139 126 Parameters.Add(new ValueParameter<ISingleObjectiveEvaluator>("Evaluator", "The evaluator that collects the values to exchange.", new EmptyUserDefinedProblemEvaluator())); 140 Parameters.Add(new ValueParameter<ISolutionCreator>("SolutionCreator", "An operator to create the solution components."));141 127 Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as most test functions are minimization problems.", new BoolValue(false))); 142 128 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this problem.")); … … 156 142 157 143 #region Events 158 public event EventHandler SolutionCreatorChanged;159 private void OnSolutionCreatorChanged() {160 EventHandler handler = SolutionCreatorChanged;161 if (handler != null) handler(this, EventArgs.Empty);162 }163 144 public event EventHandler EvaluatorChanged; 164 145 private void OnEvaluatorChanged() { … … 174 155 175 156 #region Event handlers 176 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {177 OnSolutionCreatorChanged();178 }179 157 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 180 158 if (Evaluator != null) … … 202 180 #region Helpers 203 181 private void RegisterEventHandlers() { 204 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);205 182 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 206 183 if (Evaluator != null)
Note: See TracChangeset
for help on using the changeset viewer.