Changeset 16751 for branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
- Timestamp:
- 04/03/19 15:37:38 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r16723 r16751 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; 28 29 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 30 31 31 namespace HeuristicLab.Optimization { 32 32 [StorableType("2697320D-0259-44BB-BD71-7EE1B10F664C")] 33 public abstract class SingleObjectiveProblem<TEncoding, T Solution> :34 Problem<TEncoding, T Solution, SingleObjectiveEvaluator<TSolution>>,35 ISingleObjectiveProblem<TEncoding, T Solution>,36 ISingleObjectiveProblemDefinition<TEncoding, T Solution>37 where TEncoding : class, IEncoding<T Solution>38 where T Solution : class, ISolution {33 public abstract class SingleObjectiveProblem<TEncoding, TEncodedSolution> : 34 Problem<TEncoding, TEncodedSolution, SingleObjectiveEvaluator<TEncodedSolution>>, 35 ISingleObjectiveProblem<TEncoding, TEncodedSolution>, 36 ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution> 37 where TEncoding : class, IEncoding<TEncodedSolution> 38 where TEncodedSolution : class, IEncodedSolution { 39 39 40 40 protected IValueParameter<DoubleValue> BestKnownQualityParameter { … … 64 64 protected SingleObjectiveProblem(StorableConstructorFlag _) : base(_) { } 65 65 66 protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, T Solution> original, Cloner cloner)66 protected SingleObjectiveProblem(SingleObjectiveProblem<TEncoding, TEncodedSolution> original, Cloner cloner) 67 67 : base(original, cloner) { 68 68 ParameterizeOperators(); … … 75 75 76 76 Operators.Add(Evaluator); 77 Operators.Add(new SingleObjectiveAnalyzer<T Solution>());78 Operators.Add(new SingleObjectiveImprover<T Solution>());79 Operators.Add(new SingleObjectiveMoveEvaluator<T Solution>());80 Operators.Add(new SingleObjectiveMoveGenerator<T Solution>());81 Operators.Add(new SingleObjectiveMoveMaker<T Solution>());77 Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>()); 78 Operators.Add(new SingleObjectiveImprover<TEncodedSolution>()); 79 Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>()); 80 Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>()); 81 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 82 82 83 83 ParameterizeOperators(); … … 90 90 91 91 Operators.Add(Evaluator); 92 Operators.Add(new SingleObjectiveAnalyzer<T Solution>());93 Operators.Add(new SingleObjectiveImprover<T Solution>());94 Operators.Add(new SingleObjectiveMoveEvaluator<T Solution>());95 Operators.Add(new SingleObjectiveMoveGenerator<T Solution>());96 Operators.Add(new SingleObjectiveMoveMaker<T Solution>());92 Operators.Add(new SingleObjectiveAnalyzer<TEncodedSolution>()); 93 Operators.Add(new SingleObjectiveImprover<TEncodedSolution>()); 94 Operators.Add(new SingleObjectiveMoveEvaluator<TEncodedSolution>()); 95 Operators.Add(new SingleObjectiveMoveGenerator<TEncodedSolution>()); 96 Operators.Add(new SingleObjectiveMoveMaker<TEncodedSolution>()); 97 97 98 98 ParameterizeOperators(); … … 105 105 106 106 public abstract bool Maximization { get; } 107 public abstract double Evaluate(T Solution solution, IRandom random);108 public virtual void Analyze(T Solution[] solutions, double[] qualities, ResultCollection results, IRandom random) { }109 public virtual IEnumerable<T Solution> GetNeighbors(TSolution solution, IRandom random) {110 return Enumerable.Empty<T Solution>();107 public abstract double Evaluate(TEncodedSolution solution, IRandom random); 108 public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { } 109 public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random) { 110 return Enumerable.Empty<TEncodedSolution>(); 111 111 } 112 112 … … 115 115 } 116 116 117 protected Tuple<T Solution, double> GetBestSolution(TSolution[] solutions, double[] qualities) {117 protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) { 118 118 return GetBestSolution(solutions, qualities, Maximization); 119 119 } 120 public static Tuple<T Solution, double> GetBestSolution(TSolution[] solutions, double[] qualities, bool maximization) {120 public static Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities, bool maximization) { 121 121 var zipped = solutions.Zip(qualities, (s, q) => new { Solution = s, Quality = q }); 122 122 var best = (maximization ? zipped.OrderByDescending(z => z.Quality) : zipped.OrderBy(z => z.Quality)).First(); … … 154 154 155 155 private void ParameterizeOperators() { 156 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<T Solution>>())156 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>()) 157 157 op.EvaluateFunc = Evaluate; 158 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<T Solution>>())158 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>()) 159 159 op.AnalyzeAction = Analyze; 160 foreach (var op in Operators.OfType<INeighborBasedOperator<T Solution>>())160 foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>()) 161 161 op.GetNeighborsFunc = GetNeighbors; 162 162 }
Note: See TracChangeset
for help on using the changeset viewer.