Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17381


Ignore:
Timestamp:
12/18/19 15:06:18 (5 years ago)
Author:
mkommend
Message:

#2521: Fixed single-objective evaluation results and scope contexts.

Location:
branches/2521_ProblemRefactoring
Files:
6 edited

Legend:

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

    r17357 r17381  
    2121
    2222using System.Collections.Generic;
     23using System.Linq;
    2324using HEAL.Attic;
    2425using HeuristicLab.Common;
     
    4243
    4344    protected EvaluationResult(EvaluationResult original, Cloner cloner) : base(original, cloner) {
    44       //TODO clone data dictionary
     45      data = original.data.ToDictionary(entry => entry.Key,
     46                                        entry => entry.Value is DeepCloneable ? cloner.Clone((DeepCloneable)entry.Value) : entry.Value);
    4547    }
    4648
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveAnalyzer.cs

    r17363 r17381  
    8282        var quality = ((DoubleValue)scope.Variables[QualityParameter.ActualName].Value).Value;
    8383        var solutionContext = new SingleObjectiveSolutionContextScope<TEncodedSolution>(scope, solution);
    84         solutionContext.EvaluationResult = new SingleObjectiveEvaluationResult(quality);
    8584        return solutionContext;
    8685      }).ToArray();
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SolutionContextScope.cs

    r17363 r17381  
    2727//TODO: don't derive from NamedItem, this has been done to have a fast working prototype
    2828namespace HeuristicLab.Optimization {
    29   internal class SolutionContextScope<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISolutionScope
     29  internal abstract class SolutionContextScope<TEncodedSolution> : SolutionContext<TEncodedSolution>, ISolutionScope
    3030   where TEncodedSolution : class, IEncodedSolution {
    31     private readonly IScope scope;
     31    protected static string EVALUATION_RESULT_VARIABLE_NAME = "Evaluation Result";
     32
     33    protected IScope Scope { get; }
    3234
    3335    public SolutionContextScope(IScope scope, TEncodedSolution solution) : base(solution) {
    34       this.scope = scope;
     36      Scope = scope;
    3537    }
    3638
     
    4042
    4143    IScope IScope.Parent {
    42       get { return scope.Parent; }
     44      get { return Scope.Parent; }
    4345      set { throw new NotSupportedException(); }
    4446    }
    4547
    46     VariableCollection IScope.Variables => scope.Variables;
    47     ScopeList IScope.SubScopes => scope.SubScopes;
     48    VariableCollection IScope.Variables => Scope.Variables;
     49    ScopeList IScope.SubScopes => Scope.SubScopes;
    4850
    4951    void IScope.Clear() { throw new NotImplementedException(); }
    5052
    51 
    52 
    5353    #region INamedItem members
    54     string INamedItem.Name { get { return scope.Name; } set => throw new NotImplementedException(); }
     54    string INamedItem.Name { get { return Scope.Name; } set => throw new NotImplementedException(); }
    5555    bool INamedItem.CanChangeName => false;
    5656
    57     string INamedItem.Description { get { return scope.Description; } set => throw new NotImplementedException(); }
     57    string INamedItem.Description { get { return Scope.Description; } set => throw new NotImplementedException(); }
    5858    bool INamedItem.CanChangeDescription => false;
    5959
     
    7878    where TEncodedSolution : class, IEncodedSolution {
    7979
    80     public new ISingleObjectiveEvaluationResult EvaluationResult { get; set; }
     80    public new ISingleObjectiveEvaluationResult EvaluationResult {
     81      get {
     82        return (ISingleObjectiveEvaluationResult)Scope.Variables[EVALUATION_RESULT_VARIABLE_NAME].Value;
     83      }
     84      set {
     85        Scope.Variables.Remove(EVALUATION_RESULT_VARIABLE_NAME);
     86        Scope.Variables.Add(new Variable(EVALUATION_RESULT_VARIABLE_NAME, value));
     87      }
     88    }
     89
    8190    public SingleObjectiveSolutionContextScope(IScope scope, TEncodedSolution solution) : base(scope, solution) {
    8291    }
     
    8695  where TEncodedSolution : class, IEncodedSolution {
    8796
    88     public new IMultiObjectiveEvaluationResult EvaluationResult { get; set; }
     97    public new IMultiObjectiveEvaluationResult EvaluationResult {
     98      get {
     99        return (IMultiObjectiveEvaluationResult)Scope.Variables[EVALUATION_RESULT_VARIABLE_NAME].Value;
     100      }
     101      set {
     102        Scope.Variables.Remove(EVALUATION_RESULT_VARIABLE_NAME);
     103        Scope.Variables.Add(new Variable(EVALUATION_RESULT_VARIABLE_NAME, value));
     104      }
     105    }
    89106    public MultiObjectiveSolutionContextScope(IScope scope, TEncodedSolution solution) : base(scope, solution) {
    90107    }
  • branches/2521_ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/SolutionContext.cs

    r17366 r17381  
    2323using System;
    2424using System.Collections.Generic;
    25 
     25using System.Linq;
    2626using HEAL.Attic;
    2727using HeuristicLab.Common;
     
    5858
    5959    public SolutionContext(SolutionContext<TEncodedSolution> original, Cloner cloner) : base(original, cloner) {
    60       //TODO clone data dictionary
    6160      EncodedSolution = cloner.Clone(original.EncodedSolution);
    6261      EvaluationResult = cloner.Clone(original.EvaluationResult);
     62
     63
     64      data = original.data.ToDictionary(entry => entry.Key,
     65                                        entry => entry.Value is DeepCloneable ? cloner.Clone((DeepCloneable)entry.Value) : entry.Value);
    6366    }
    6467
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/CompiledProblemDefinition.cs

    r17363 r17381  
    102102      : base(encoding) { }
    103103
    104     #region ISingleObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
     104    #region IMultiObjectiveProblemDefinition<TEncoding,TEncodedSolution> Members
    105105    public int Objectives => Maximization.Length;
    106106    public abstract bool[] Maximization { get; }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.Programmable/3.3/Templates/SingleObjectiveProblem_Template.cs

    r17366 r17381  
    2121    }
    2222
     23    //TODO add other methods
     24
     25    public override void Evaluate(ISingleObjectiveSolutionContext<SOLUTION_CLASS> solutionContext, IRandom random, CancellationToken cancellationToken) {
     26      var quality = Evaluate(solutionContext.EncodedSolution, random, cancellationToken);
     27      var evaluationResult = new SingleObjectiveEvaluationResult(quality);
     28      solutionContext.EvaluationResult = evaluationResult;
     29    }
     30
    2331    public override double Evaluate(SOLUTION_CLASS solution, IRandom random, CancellationToken cancellationToken) {
    2432      // Use vars.yourVariable to access variables in the variable store i.e. yourVariable
    2533      var quality = 0.0;
    2634      return quality;
     35    }
     36
     37    public override void Analyze(ISingleObjectiveSolutionContext<TEncodedSolution>[] solutionContexts, ResultCollection results, IRandom random) {
     38      var solutions = solutionContexts.Select(c => c.EncodedSolution).ToArray();
     39      var qualities = solutionContexts.Select(c => c.EvaluationResult.Quality).ToArray();
     40      Analyze(solutions, qualities, results, random);
    2741    }
    2842
Note: See TracChangeset for help on using the changeset viewer.