Free cookie consent management tool by TermsFeed Policy Generator

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

#2521: First version of contexts in problem evaluation.

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs

    r17320 r17363  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Linq;
    2425using System.Threading;
    2526using HeuristicLab.Core;
     
    6465    }
    6566    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    }
    6889
    6990    public bool IsBetter(double quality, double bestQuality) {
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProblemDefinitionScript.cs

    r17320 r17363  
    5959    }
    6060
     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
    6169    void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    6270      CompiledProblemDefinition.Analyze(individuals, qualities, results, random);
    6371    }
     72    void ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
     73      CompiledProblemDefinition.Analyze(solutionContexts, results, random);
     74    }
     75
     76
    6477    IEnumerable<TEncodedSolution> ISingleObjectiveProblemDefinition<TEncoding, TEncodedSolution>.GetNeighbors(TEncodedSolution individual, IRandom random) {
    6578      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);
    6682    }
    6783
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/SingleObjectiveProgrammableProblem.cs

    r17320 r17363  
    108108    }
    109109
     110    public override void Evaluate(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random, CancellationToken cancellationToken) {
     111      ProblemDefinition.Evaluate(solutionContext, random, cancellationToken);
     112    }
    110113    public override double Evaluate(TEncodedSolution individual, IRandom random, CancellationToken cancellationToken) {
    111114      return ProblemDefinition.Evaluate(individual, random, cancellationToken);
    112115    }
    113116
     117    public override void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
     118      ProblemDefinition.Analyze(solutionContexts, results, random);
     119    }
    114120    public override void Analyze(TEncodedSolution[] individuals, double[] qualities, ResultCollection results, IRandom random) {
    115121      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);
    116126    }
    117127    public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution individual, IRandom random) {
Note: See TracChangeset for help on using the changeset viewer.