Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/21/19 17:58:32 (4 years ago)
Author:
mkommend
Message:

#2521: First version of contexts in problem evaluation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SingleObjectiveProblem.cs

    r17356 r17363  
    117117    }
    118118    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
    119128    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) {
    121136      return Enumerable.Empty<TEncodedSolution>();
    122137    }
     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
    123143
    124144    public static bool IsBetter(bool maximization, double quality, double bestQuality) {
     
    130150    }
    131151
     152    //TODO refactor to solution contexts
    132153    protected Tuple<TEncodedSolution, double> GetBestSolution(TEncodedSolution[] solutions, double[] qualities) {
    133154      return GetBestSolution(solutions, qualities, Maximization);
     
    170191    private void ParameterizeOperators() {
    171192      foreach (var op in Operators.OfType<ISingleObjectiveEvaluationOperator<TEncodedSolution>>())
    172         op.EvaluateFunc = Evaluate;
     193        op.Evaluate = Evaluate;
    173194      foreach (var op in Operators.OfType<ISingleObjectiveAnalysisOperator<TEncodedSolution>>())
    174         op.AnalyzeAction = Analyze;
     195        op.Analyze = Analyze;
    175196      foreach (var op in Operators.OfType<INeighborBasedOperator<TEncodedSolution>>())
    176         op.GetNeighborsFunc = GetNeighbors;
     197        op.GetNeighbors = GetNeighbors;
    177198    }
    178199
Note: See TracChangeset for help on using the changeset viewer.