Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17363


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
Files:
2 added
19 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/EvaluationTracker.cs

    r17320 r17363  
    2323using System;
    2424using System.Collections.Generic;
     25using System.Linq;
    2526using System.Threading;
    2627using HEAL.Attic;
     
    110111    }
    111112
     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
    112121    public bool Maximization {
    113122      get {
     
    125134    }
    126135
     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
    127142    public IEnumerable<BinaryVector> GetNeighbors(BinaryVector individual, IRandom random) {
    128143      return problem.GetNeighbors(individual, random);
    129144    }
     145    public IEnumerable<ISingleObjectiveSolutionContext<BinaryVector>> GetNeighbors(ISingleObjectiveSolutionContext<BinaryVector> solutionContext, IRandom random) {
     146      return GetNeighbors(solutionContext.EncodedSolution, random).Select(n => new SingleObjectiveSolutionContext<BinaryVector>(n));
     147    }
    130148  }
    131149}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISingleObjectiveProblemDefinition.cs

    r17320 r17363  
    3939    double Evaluate(TEncodedSolution solution, IRandom random);
    4040    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);
    4143
    4244    void Analyze(TEncodedSolution[] solutions, double[] qualities, ResultCollection results, IRandom random);
     45    void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random);
     46
    4347    IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solution, IRandom random);
     48    IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>> GetNeighbors(ISingleObjectiveSolutionContext<TEncodedSolution> solutionContext, IRandom random);
    4449  }
    4550}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/ISolutionContext.cs

    r17357 r17363  
    2424
    2525namespace HeuristicLab.Optimization {
     26  //TODO discuss renaming to solution scope?
    2627  [StorableType("383A35EB-03C9-473D-A20B-EE34E00BC174")]
    2728  public interface ISolutionContext : IItem {
     
    3637  }
    3738
    38   public interface ISingleObjectiveSolutionContext : ISolutionContext {
    39     //TODO discuss with abeham hiding of base property
    40     new ISingleObjectiveEvaluationResult EvaluationResult { get; }
     39  public interface ISolutionContext<TEncodedSolution> : ISolutionContext
     40    where TEncodedSolution : class, IEncodedSolution {
     41    new TEncodedSolution EncodedSolution { get; }
    4142  }
    4243
    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; }
    4554  }
    4655
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/INeighborBasedOperator.cs

    r17226 r17363  
    2929  internal interface INeighborBasedOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution>
    3030  where TEncodedSolution : class, IEncodedSolution {
    31     Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; }
     31    Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
    3232  }
    3333}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveAnalysisOperator.cs

    r17226 r17363  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Core;
    24 using HEAL.Attic;
    2525
    2626namespace HeuristicLab.Optimization {
     
    2828  internal interface ISingleObjectiveAnalysisOperator<TEncodedSolution> : IEncodingOperator<TEncodedSolution>, ISingleObjectiveOperator
    2929  where TEncodedSolution : class, IEncodedSolution {
    30     Action<TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
     30    Action<ISingleObjectiveSolutionContext<TEncodedSolution>[], ResultCollection, IRandom> Analyze { get; set; }
    3131  }
    3232}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Interfaces/internal/ISingleObjectiveEvaluationOperator.cs

    r17226 r17363  
    2121
    2222using System;
     23using HEAL.Attic;
    2324using HeuristicLab.Core;
    24 using HEAL.Attic;
    2525
    2626namespace HeuristicLab.Optimization {
     
    2828  internal interface ISingleObjectiveEvaluationOperator<TEncodedSolution> : ISingleObjectiveEvaluator, IEncodingOperator<TEncodedSolution>
    2929  where TEncodedSolution : class, IEncodedSolution {
    30     Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; }
     30    Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
    3131  }
    3232}
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs

    r17226 r17363  
    5353    }
    5454
    55     public Action<TEncodedSolution[], double[], ResultCollection, IRandom> AnalyzeAction { get; set; }
     55    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>[], ResultCollection, IRandom> Analyze { get; set; }
    5656
    5757    [StorableConstructor]
     
    7878        scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b));
    7979
    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);
    8289      return base.Apply();
    8390    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveEvaluator.cs

    r17226 r17363  
    4646    }
    4747
    48     public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; }
     48    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
    4949
    5050    [StorableConstructor]
     
    6363      var encoding = EncodingParameter.ActualValue;
    6464      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);
    6671      return base.InstrumentedApply();
    6772    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs

    r17226 r17363  
    6363    }
    6464
    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; }
    6767
    6868    [StorableConstructor]
     
    9090      var sampleSize = SampleSizeParameter.ActualValue.Value;
    9191      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;
    93100
    94101      var count = 0;
     
    96103        TEncodedSolution best = default(TEncodedSolution);
    97104        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;
    100108          count++;
    101109          if (maximize && bestQuality > q || !maximize && bestQuality < q) continue;
    102           best = neighbor;
     110          best = neighbor.EncodedSolution;
    103111          bestQuality = q;
    104112        }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveEvaluator.cs

    r17226 r17363  
    5050    }
    5151
    52     public Func<TEncodedSolution, IRandom, double> EvaluateFunc { get; set; }
     52    public Action<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom> Evaluate { get; set; }
    5353
    5454    [StorableConstructor]
     
    6969      var random = RandomParameter.ActualValue;
    7070      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);
    7378      return base.Apply();
    7479    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveMoveGenerator.cs

    r17226 r17363  
    4848    }
    4949
    50     public Func<TEncodedSolution, IRandom, IEnumerable<TEncodedSolution>> GetNeighborsFunc { get; set; }
     50    public Func<ISingleObjectiveSolutionContext<TEncodedSolution>, IRandom, IEnumerable<ISingleObjectiveSolutionContext<TEncodedSolution>>> GetNeighbors { get; set; }
    5151
    5252    [StorableConstructor]
     
    6969      var encoding = EncodingParameter.ActualValue;
    7070      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);
    7272
     73      var nbhood = GetNeighbors(solutionContext, random).Take(sampleSize).ToList();
    7374      var moveScopes = new Scope[nbhood.Count];
    7475      for (int i = 0; i < moveScopes.Length; i++) {
    7576        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);
    7778      }
    7879      ExecutionContext.Scope.SubScopes.AddRange(moveScopes);
  • 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
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs

    r17358 r17363  
    3030namespace HeuristicLab.Optimization {
    3131  [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 {
    3334
    3435    [Storable]
    3536    private readonly Dictionary<string, object> data = new Dictionary<string, object>();
    3637
     38    IEncodedSolution ISolutionContext.EncodedSolution { get { return EncodedSolution; } }
     39
    3740    [Storable]
    38     public IEncodedSolution EncodedSolution { get; private set; }
     41    public TEncodedSolution EncodedSolution { get; private set; }
    3942
    4043    [Storable]
     
    4346    public bool IsEvaluated => EvaluationResult != null;
    4447
    45     protected SolutionContext(IEncodedSolution encodedSolution) : base() {
     48    protected SolutionContext(TEncodedSolution encodedSolution) : base() {
    4649      EncodedSolution = encodedSolution ?? throw new ArgumentNullException("encodedSolution");
    4750    }
    4851
    49     protected SolutionContext(IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) {
     52    protected SolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : this(encodedSolution) {
    5053      EvaluationResult = evaluationResult ?? throw new ArgumentNullException("evaluationResult");
    5154    }
     
    5457    protected SolutionContext(StorableConstructorFlag _) : base(_) { }
    5558
    56     public SolutionContext(SolutionContext original, Cloner cloner) : base(original, cloner) {
     59    public SolutionContext(SolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) {
    5760      //TODO clone data dictionary
    5861      EncodedSolution = cloner.Clone(original.EncodedSolution);
     
    6972
    7073  [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 {
    7276    public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; }
    7377
    74     public SingleObjectiveSolutionContext(IEncodedSolution encodedSolution) : base(encodedSolution) { }
     78    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
    7579
    7680
    77     public SingleObjectiveSolutionContext(IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
     81    public SingleObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
    7882
    7983    [StorableConstructor]
    80     public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) {    }
     84    public SingleObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
    8185
    82     public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext original, Cloner cloner) : base(original, cloner) { }
     86    public SingleObjectiveSolutionContext(SingleObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
    8387
    8488
    8589    public override IDeepCloneable Clone(Cloner cloner) {
    86       return new SingleObjectiveSolutionContext(this, cloner);
     90      return new SingleObjectiveSolutionContext<TEncodedSolution>(this, cloner);
    8791    }
    8892  }
    8993
    9094  [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 {
    9297    public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; }
    9398
    94     public MultiObjectiveSolutionContext(IEncodedSolution encodedSolution) : base(encodedSolution) { }
     99    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution) : base(encodedSolution) { }
    95100
    96101
    97     public MultiObjectiveSolutionContext(IEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
     102    public MultiObjectiveSolutionContext(TEncodedSolution encodedSolution, IEvaluationResult evaluationResult) : base(encodedSolution, evaluationResult) { }
    98103
    99104    [StorableConstructor]
    100105    public MultiObjectiveSolutionContext(StorableConstructorFlag _) : base(_) { }
    101106
    102     public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext original, Cloner cloner) : base(original, cloner) { }
     107    public MultiObjectiveSolutionContext(MultiObjectiveSolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) { }
    103108
    104109
    105110    public override IDeepCloneable Clone(Cloner cloner) {
    106       return new MultiObjectiveSolutionContext(this, cloner);
     111      return new MultiObjectiveSolutionContext<TEncodedSolution>(this, cloner);
    107112    }
    108113  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/HeuristicLab.Optimization-3.3.csproj

    r17357 r17363  
    126126    <Compile Include="BasicProblems\Interfaces\IMultiEncodingOperator.cs" />
    127127    <Compile Include="BasicProblems\Interfaces\IMultiObjectiveProblem.cs" />
     128    <Compile Include="BasicProblems\Interfaces\internal\ISolutionScope.cs" />
    128129    <Compile Include="BasicProblems\Interfaces\ISingleObjectiveProblem.cs" />
    129130    <Compile Include="BasicProblems\CombinedEncoding.cs" />
     
    133134    <Compile Include="BasicProblems\Operators\MultiEncodingManipulator.cs" />
    134135    <Compile Include="BasicProblems\Operators\MultiEncodingOperator.cs" />
     136    <Compile Include="BasicProblems\Operators\SolutionContextScope.cs" />
    135137    <Compile Include="BasicProblems\Problem.cs" />
    136138    <Compile Include="BasicProblems\Encoding.cs" />
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.ExternalEvaluation/3.4/SingleObjectiveExternalEvaluationProblem.cs

    r17360 r17363  
    113113    public virtual QualityMessage Evaluate(SolutionMessage solutionMessage, CancellationToken cancellationToken) {
    114114      return Cache == null
    115         ? EvaluateOnNextAvailableClient(solutionMessage,cancellationToken)
     115        ? EvaluateOnNextAvailableClient(solutionMessage, cancellationToken)
    116116        : Cache.GetValue(solutionMessage, EvaluateOnNextAvailableClient, GetQualityMessageExtensions(), cancellationToken);
    117117    }
     
    120120      OptimizationSupport.Analyze(solutions, qualities, results, random);
    121121    }
    122 
    123122    public override IEnumerable<TEncodedSolution> GetNeighbors(TEncodedSolution solutions, IRandom random) {
    124123      return OptimizationSupport.GetNeighbors(solutions, random);
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/ProbabilisticTSP.cs

    r17335 r17363  
    124124        try {
    125125          var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
    126           var tourLength = Evaluate(tour, new MersenneTwister(1));         
     126          var tourLength = Evaluate(tour, new MersenneTwister(1));
    127127          BestKnownSolution = ProbabilisticTSPData.GetSolution(tour, tourLength);
    128128          BestKnownQuality = tourLength;
  • 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.