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.Optimization/3.3/BasicProblems/Operators
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • 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);
Note: See TracChangeset for help on using the changeset viewer.