Changeset 17363 for branches/2521_ProblemRefactoring
- Timestamp:
- 11/21/19 17:58:32 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 2 added
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs
r17320 r17363 23 23 using System; 24 24 using System.Collections.Generic; 25 using System.Linq; 25 26 using System.Threading; 26 27 using HEAL.Attic; … … 110 111 } 111 112 113 public void Evaluate(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random) { 114 Evaluate(solutionContext, random, CancellationToken.None); 115 } 116 public void Evaluate(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random, CancellationToken cancellationToken) { 117 double quality = Evaluate(solutionContext.EncodedSolution, random, cancellationToken); 118 solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality); 119 } 120 112 121 public bool Maximization { 113 122 get { … … 125 134 } 126 135 136 public void Analyze(ISingleObjectiveSolutionContext<BinaryVector>[] solutionContexts, ResultCollection results, IRandom random) { 137 var solutions = solutionContexts.Select(c => c.EncodedSolution).ToArray(); 138 var qualities = solutionContexts.Select(c => c.EvaluationResult.Quality).ToArray(); 139 Analyze(solutions, qualities, results, random); 140 } 141 127 142 public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) { 128 143 return problem.GetNeighbors(individual, random); 129 144 } 145 public IEnumerable<ISingleObjectiveSolutionContext<BinaryVector>> GetNeighbors(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random) { 146 return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<BinaryVector>(n)); 147 } 130 148 } 131 149 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs
r17320 r17363 39 39 double Evaluate(TEncodedSolution solution, IRandom random); 40 40 double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken); 41 void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random); 42 void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken); 41 43 42 44 void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random); 45 void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random); 46 43 47 IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random); 48 IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random); 44 49 } 45 50 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISolutionContext.cs
r17357 r17363 24 24 25 25 namespace HeuristicLab.Optimization { 26 //TODO discuss renaming to solution scope? 26 27 [StorableType("383A35EB-03C9-473D-A20B-EE34E00BC174")] 27 28 public interface ISolutionContext : IItem { … … 36 37 } 37 38 38 public interface IS ingleObjectiveSolutionContext : ISolutionContext {39 //TODO discuss with abeham hiding of base property40 new ISingleObjectiveEvaluationResult EvaluationResult{ get; }39 public interface ISolutionContext<TEncodedSolution> : ISolutionContext 40 where TEncodedSolution : class, IEncodedSolution { 41 new TEncodedSolution EncodedSolution { get; } 41 42 } 42 43 43 public interface IMultiObjectiveSolutionContext : ISolutionContext { 44 new IMultiObjectiveEvaluationResult EvaluationResult { get; } 44 45 public interface ISingleObjectiveSolutionContext<TEncodedSolution> : ISolutionContext<TEncodedSolution> 46 where TEncodedSolution : class, IEncodedSolution { 47 //TODO discuss with abeham hiding of base property 48 new ISingleObjectiveEvaluationResult EvaluationResult { get; set; } 49 } 50 51 public interface IMultiObjectiveSolutionContext<TEncodedSolution> : ISolutionContext<TEncodedSolution> 52 where TEncodedSolution : class, IEncodedSolution { 53 new IMultiObjectiveEvaluationResult EvaluationResult { get; set; } 45 54 } 46 55 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/INeighborBasedOperator.cs
r17226 r17363 29 29 internal interface INeighborBasedOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution> 30 30 where TEncodedSolution : class, IEncodedSolution { 31 Func< TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc{ get; set; }31 Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; } 32 32 } 33 33 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs
r17226 r17363 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; 24 using HEAL.Attic;25 25 26 26 namespace HeuristicLab.Optimization { … … 28 28 internal interface ISingleObjectiveAnalysisOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution>, ISingleObjectiveOperator 29 29 where TEncodedSolution : class, IEncodedSolution { 30 Action< TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction{ get; set; }30 Action<ISingleObjectiveSolutionContext<TEncodedSolution>[], ResultCollection, IRandom> Analyze { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs
r17226 r17363 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Core; 24 using HEAL.Attic;25 25 26 26 namespace HeuristicLab.Optimization { … … 28 28 internal interface ISingleObjectiveEvaluationOperator<TEncodedSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TEncodedSolution> 29 29 where TEncodedSolution : class, IEncodedSolution { 30 Func<TEncodedSolution, IRandom, double> EvaluateFunc{ get; set; }30 Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; } 31 31 } 32 32 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs
r17226 r17363 53 53 } 54 54 55 public Action< TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction{ get; set; }55 public Action<ISingleObjectiveSolutionContext<TEncodedSolution>[], ResultCollection, IRandom> Analyze { get; set; } 56 56 57 57 [StorableConstructor] … … 78 78 scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)); 79 79 80 var individuals = scopes.Select(s => ScopeUtil.GetEncodedSolution(s, encoding)).ToArray(); 81 AnalyzeAction(individuals, QualityParameter.ActualValue.Select(x => x.Value).ToArray(), results, random); 80 var solutionContexts = scopes.Select(scope => { 81 var solution = ScopeUtil.GetEncodedSolution(scope, encoding); 82 var quality = ((DoubleValue)scope.Variables[QualityParameter.ActualName].Value).Value; 83 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(scope, solution); 84 solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality); 85 return solutionContext; 86 }).ToArray(); 87 88 Analyze(solutionContexts, results, random); 82 89 return base.Apply(); 83 90 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs
r17226 r17363 46 46 } 47 47 48 public Func<TEncodedSolution, IRandom, double> EvaluateFunc{ get; set; }48 public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; } 49 49 50 50 [StorableConstructor] … … 63 63 var encoding = EncodingParameter.ActualValue; 64 64 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 65 QualityParameter.ActualValue = new DoubleValue(EvaluateFunc(solution, random)); 65 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 66 67 Evaluate(solutionContext, random); 68 var qualityValue = solutionContext.EvaluationResult.Quality; 69 70 QualityParameter.ActualValue = new DoubleValue(qualityValue); 66 71 return base.InstrumentedApply(); 67 72 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs
r17226 r17363 63 63 } 64 64 65 public Func<TEncodedSolution, IRandom, double> EvaluateFunc{ get; set; }66 public Func< TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc{ get; set; }65 public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; } 66 public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; } 67 67 68 68 [StorableConstructor] … … 90 90 var sampleSize = SampleSizeParameter.ActualValue.Value; 91 91 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 92 var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value; 92 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 93 94 double quality; 95 if (QualityParameter.ActualValue == null) { 96 if (!solutionContext.IsEvaluated) Evaluate(solutionContext, random); 97 98 quality = solutionContext.EvaluationResult.Quality; 99 } else quality = QualityParameter.ActualValue.Value; 93 100 94 101 var count = 0; … … 96 103 TEncodedSolution best = default(TEncodedSolution); 97 104 var bestQuality = quality; 98 foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) { 99 var q = EvaluateFunc(neighbor, random); 105 foreach (var neighbor in GetNeighbors(solutionContext, random).Take(sampleSize)) { 106 Evaluate(neighbor, random); 107 var q = neighbor.EvaluationResult.Quality; 100 108 count++; 101 109 if (maximize && bestQuality > q || !maximize && bestQuality < q) continue; 102 best = neighbor ;110 best = neighbor.EncodedSolution; 103 111 bestQuality = q; 104 112 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs
r17226 r17363 50 50 } 51 51 52 public Func<TEncodedSolution, IRandom, double> EvaluateFunc{ get; set; }52 public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; } 53 53 54 54 [StorableConstructor] … … 69 69 var random = RandomParameter.ActualValue; 70 70 var encoding = EncodingParameter.ActualValue; 71 var individual = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 72 MoveQualityParameter.ActualValue = new DoubleValue(EvaluateFunc(individual, random)); 71 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 72 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 73 74 Evaluate(solutionContext, random); 75 var qualityValue = solutionContext.EvaluationResult.Quality; 76 77 MoveQualityParameter.ActualValue = new DoubleValue(qualityValue); 73 78 return base.Apply(); 74 79 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs
r17226 r17363 48 48 } 49 49 50 public Func< TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc{ get; set; }50 public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; } 51 51 52 52 [StorableConstructor] … … 69 69 var encoding = EncodingParameter.ActualValue; 70 70 var solution = ScopeUtil.GetEncodedSolution(ExecutionContext.Scope, encoding); 71 var nbhood = GetNeighborsFunc(solution, random).Take(sampleSize).ToList();71 var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(ExecutionContext.Scope, solution); 72 72 73 var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList(); 73 74 var moveScopes = new Scope[nbhood.Count]; 74 75 for (int i = 0; i < moveScopes.Length; i++) { 75 76 moveScopes[i] = new Scope(i.ToString(CultureInfo.InvariantCulture.NumberFormat)); 76 ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i] );77 ScopeUtil.CopyEncodedSolutionToScope(moveScopes[i], encoding, nbhood[i].EncodedSolution); 77 78 } 78 79 ExecutionContext.Scope.SubScopes.AddRange(moveScopes); -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs
r17356 r17363 117 117 } 118 118 public abstract double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken); 119 120 public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 121 Evaluate(solutionContext, random, CancellationToken.None); 122 } 123 public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) { 124 double quality = Evaluate(solutionContext.EncodedSolution, random, cancellationToken); 125 solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality); 126 } 127 119 128 public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { } 120 public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random) { 129 public virtual void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) { 130 var solutions = solutionContexts.Select(c => c.EncodedSolution).ToArray(); 131 var qualities = solutionContexts.Select(c => c.EvaluationResult.Quality).ToArray(); 132 Analyze(solutions, qualities, results, random); 133 } 134 135 public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) { 121 136 return Enumerable.Empty<TEncodedSolution>(); 122 137 } 138 public virtual IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 139 return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<TEncodedSolution>(n)); 140 } 141 142 123 143 124 144 public static bool IsBetter(bool maximization, double quality, double bestQuality) { … … 130 150 } 131 151 152 //TODO refactor to solution contexts 132 153 protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) { 133 154 return GetBestSolution(solutions, qualities, Maximization); … … 170 191 private void ParameterizeOperators() { 171 192 foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>()) 172 op.Evaluate Func= Evaluate;193 op.Evaluate = Evaluate; 173 194 foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>()) 174 op.Analyze Action= Analyze;195 op.Analyze = Analyze; 175 196 foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>()) 176 op.GetNeighbors Func= GetNeighbors;197 op.GetNeighbors = GetNeighbors; 177 198 } 178 199 -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs
r17358 r17363 30 30 namespace HeuristicLab.Optimization { 31 31 [StorableType("72638F28-11DD-440D-B7A2-79E16E0EFB83")] 32 public abstract class SolutionContext : Item { 32 public abstract class SolutionContext<TEncodedSolution> : Item, ISolutionContext 33 where TEncodedSolution : class, IEncodedSolution { 33 34 34 35 [Storable] 35 36 private readonly Dictionary<string, object> data = new Dictionary<string, object>(); 36 37 38 IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } } 39 37 40 [Storable] 38 public IEncodedSolution EncodedSolution { get; private set; }41 public TEncodedSolution EncodedSolution { get; private set; } 39 42 40 43 [Storable] … … 43 46 public bool IsEvaluated => EvaluationResult != null; 44 47 45 protected SolutionContext( IEncodedSolution encodedSolution) : base() {48 protected SolutionContext(TEncodedSolution encodedSolution) : base() { 46 49 EncodedSolution = encodedSolution ?? throw new ArgumentNullException("encodedSolution"); 47 50 } 48 51 49 protected SolutionContext( IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) {52 protected SolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) { 50 53 EvaluationResult = evaluationResult ?? throw new ArgumentNullException("evaluationResult"); 51 54 } … … 54 57 protected SolutionContext(StorableConstructorFlag _) : base(_) { } 55 58 56 public SolutionContext(SolutionContext original, Cloner cloner) : base(original, cloner) {59 public SolutionContext(SolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { 57 60 //TODO clone data dictionary 58 61 EncodedSolution = cloner.Clone(original.EncodedSolution); … … 69 72 70 73 [StorableType("DF6DA9C9-7EF4-4DC3-9855-6C43BDEDD735")] 71 public class SingleObjectiveSolutionContext : SolutionContext, ISingleObjectiveSolutionContext { 74 public class SingleObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISingleObjectiveSolutionContext<TEncodedSolution> 75 where TEncodedSolution : class, IEncodedSolution { 72 76 public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; } 73 77 74 public SingleObjectiveSolutionContext( IEncodedSolution encodedSolution) : base(encodedSolution) { }78 public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { } 75 79 76 80 77 public SingleObjectiveSolutionContext( IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }81 public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { } 78 82 79 83 [StorableConstructor] 80 public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { 84 public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { } 81 85 82 public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext original, Cloner cloner) : base(original, cloner) { }86 public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 83 87 84 88 85 89 public override IDeepCloneable Clone(Cloner cloner) { 86 return new SingleObjectiveSolutionContext (this, cloner);90 return new SingleObjectiveSolutionContext<TEncodedSolution>(this, cloner); 87 91 } 88 92 } 89 93 90 94 [StorableType("929868B3-8994-4D75-B363-CCF9C51410F9")] 91 public class MultiObjectiveSolutionContext : SolutionContext, IMultiObjectiveSolutionContext { 95 public class MultiObjectiveSolutionContext<TEncodedSolution> : SolutionContext<TEncodedSolution>, IMultiObjectiveSolutionContext<TEncodedSolution> 96 where TEncodedSolution : class, IEncodedSolution { 92 97 public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; } 93 98 94 public MultiObjectiveSolutionContext( IEncodedSolution encodedSolution) : base(encodedSolution) { }99 public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { } 95 100 96 101 97 public MultiObjectiveSolutionContext( IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }102 public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { } 98 103 99 104 [StorableConstructor] 100 105 public MultiObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { } 101 106 102 public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext original, Cloner cloner) : base(original, cloner) { }107 public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { } 103 108 104 109 105 110 public override IDeepCloneable Clone(Cloner cloner) { 106 return new MultiObjectiveSolutionContext (this, cloner);111 return new MultiObjectiveSolutionContext<TEncodedSolution>(this, cloner); 107 112 } 108 113 } -
branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj
r17357 r17363 126 126 <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" /> 127 127 <Compile Include="BasicProblems\Interfaces\IMultiObjectiveProblem.cs" /> 128 <Compile Include="BasicProblems\Interfaces\internal\ISolutionScope.cs" /> 128 129 <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" /> 129 130 <Compile Include="BasicProblems\CombinedEncoding.cs" /> … … 133 134 <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" /> 134 135 <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" /> 136 <Compile Include="BasicProblems\Operators\SolutionContextScope.cs" /> 135 137 <Compile Include="BasicProblems\Problem.cs" /> 136 138 <Compile Include="BasicProblems\Encoding.cs" /> -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/SingleObjectiveExternalEvaluationProblem.cs
r17360 r17363 113 113 public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) { 114 114 return Cache == null 115 ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken)115 ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken) 116 116 : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken); 117 117 } … … 120 120 OptimizationSupport.Analyze(solutions, qualities, results, random); 121 121 } 122 123 122 public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) { 124 123 return OptimizationSupport.GetNeighbors(solutions, random); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/ProbabilisticTSP.cs
r17335 r17363 124 124 try { 125 125 var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour); 126 var tourLength = Evaluate(tour, new MersenneTwister(1)); 126 var tourLength = Evaluate(tour, new MersenneTwister(1)); 127 127 BestKnownSolution = ProbabilisticTSPData.GetSolution(tour, tourLength); 128 128 BestKnownQuality = tourLength; -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs
r17320 r17363 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 24 25 using System.Threading; 25 26 using HeuristicLab.Core; … … 64 65 } 65 66 public abstract double Evaluate(TEncodedSolution solution, IRandom random, CancellationToken cancellationToken); 66 public abstract void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random); 67 public abstract IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random); 67 68 public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 69 Evaluate(solutionContext, random, CancellationToken.None); 70 } 71 public virtual void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) { 72 double quality = Evaluate(solutionContext.EncodedSolution, random, cancellationToken); 73 solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality); 74 } 75 76 public virtual void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) { } 77 public virtual void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) { 78 var solutions = solutionContexts.Select(c => c.EncodedSolution).ToArray(); 79 var qualities = solutionContexts.Select(c => c.EvaluationResult.Quality).ToArray(); 80 Analyze(solutions, qualities, results, random); 81 } 82 83 public virtual IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) { 84 return Enumerable.Empty<TEncodedSolution>(); 85 } 86 public virtual IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 87 return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<TEncodedSolution>(n)); 88 } 68 89 69 90 public bool IsBetter(double quality, double bestQuality) { -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemDefinitionScript.cs
r17320 r17363 59 59 } 60 60 61 void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 62 CompiledProblemDefinition.Evaluate(solutionContext, random, CancellationToken.None); 63 } 64 void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) { 65 double quality = CompiledProblemDefinition.Evaluate(solutionContext.EncodedSolution, random, cancellationToken); 66 solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality); 67 } 68 61 69 void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { 62 70 CompiledProblemDefinition.Analyze(individuals, qualities, results, random); 63 71 } 72 void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) { 73 CompiledProblemDefinition.Analyze(solutionContexts, results, random); 74 } 75 76 64 77 IEnumerable<TEncodedSolution> ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.GetNeighbors(TEncodedSolution individual, IRandom random) { 65 78 return CompiledProblemDefinition.GetNeighbors(individual, random); 79 } 80 IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 81 return CompiledProblemDefinition.GetNeighbors(solutionContext, random); 66 82 } 67 83 -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs
r17320 r17363 108 108 } 109 109 110 public override void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) { 111 ProblemDefinition.Evaluate(solutionContext, random, cancellationToken); 112 } 110 113 public override double Evaluate(TEncodedSolution individual, IRandom random, CancellationToken cancellationToken) { 111 114 return ProblemDefinition.Evaluate(individual, random, cancellationToken); 112 115 } 113 116 117 public override void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) { 118 ProblemDefinition.Analyze(solutionContexts, results, random); 119 } 114 120 public override void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) { 115 121 ProblemDefinition.Analyze(individuals, qualities, results, random); 122 } 123 124 public override IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random) { 125 return ProblemDefinition.GetNeighbors(solutionContext, random); 116 126 } 117 127 public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random) {
Note: See TracChangeset
for help on using the changeset viewer.