Ignore:
Timestamp:
11/23/15 15:15:27 (5 years ago)
Author:
mkommend
Message:

#2521: Refactored encodings and problems.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemRefactoring/HeuristicLab.Optimization/3.3/BasicProblems/Operators/SingleObjectiveImprover.cs

    r12012 r13336  
    3333  [Item("Single-objective Improver", "Improves a solution by calling GetNeighbors and Evaluate of the corresponding problem definition.")]
    3434  [StorableClass]
    35   public sealed class SingleObjectiveImprover : SingleSuccessorOperator, INeighborBasedOperator, IImprovementOperator, ISingleObjectiveEvaluationOperator, IStochasticOperator {
     35  public sealed class SingleObjectiveImprover<TSolution> : SingleSuccessorOperator, INeighborBasedOperator<TSolution>, IImprovementOperator, ISingleObjectiveEvaluationOperator<TSolution>, IStochasticOperator
     36    where TSolution : class, ISolution {
    3637    public ILookupParameter<IRandom> RandomParameter {
    3738      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
    3839    }
    3940
    40     public ILookupParameter<IEncoding> EncodingParameter {
    41       get { return (ILookupParameter<IEncoding>)Parameters["Encoding"]; }
     41    public ILookupParameter<IEncoding<TSolution>> EncodingParameter {
     42      get { return (ILookupParameter<IEncoding<TSolution>>)Parameters["Encoding"]; }
    4243    }
    4344
     
    6263    }
    6364
    64     public Func<Individual, IRandom, double> EvaluateFunc { get; set; }
    65     public Func<Individual, IRandom, IEnumerable<Individual>> GetNeighborsFunc { get; set; }
     65    public Func<TSolution, IRandom, double> EvaluateFunc { get; set; }
     66    public Func<TSolution, IRandom, IEnumerable<TSolution>> GetNeighborsFunc { get; set; }
    6667
    6768    [StorableConstructor]
    6869    private SingleObjectiveImprover(bool deserializing) : base(deserializing) { }
    69     private SingleObjectiveImprover(SingleObjectiveImprover original, Cloner cloner) : base(original, cloner) { }
     70    private SingleObjectiveImprover(SingleObjectiveImprover<TSolution> original, Cloner cloner) : base(original, cloner) { }
    7071    public SingleObjectiveImprover() {
    7172      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    72       Parameters.Add(new LookupParameter<IEncoding>("Encoding", "An item that holds the problem's encoding."));
     73      Parameters.Add(new LookupParameter<IEncoding<TSolution>>("Encoding", "An item that holds the problem's encoding."));
    7374      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the parameter vector."));
    7475      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the problem should be minimized or maximized."));
     
    7980
    8081    public override IDeepCloneable Clone(Cloner cloner) {
    81       return new SingleObjectiveImprover(this, cloner);
     82      return new SingleObjectiveImprover<TSolution>(this, cloner);
    8283    }
    8384
     
    8889      var maxAttempts = ImprovementAttemptsParameter.ActualValue.Value;
    8990      var sampleSize = SampleSizeParameter.ActualValue.Value;
    90       var individual = encoding.GetIndividual(ExecutionContext.Scope);
    91       var quality = QualityParameter.ActualValue == null ? EvaluateFunc(individual, random) : QualityParameter.ActualValue.Value;
     91      var solution = ScopeUtil.GetSolution(ExecutionContext.Scope, encoding);
     92      var quality = QualityParameter.ActualValue == null ? EvaluateFunc(solution, random) : QualityParameter.ActualValue.Value;
    9293
    9394      var count = 0;
    9495      for (var i = 0; i < maxAttempts; i++) {
    95         Individual best = null;
     96        TSolution best = default(TSolution);
    9697        var bestQuality = quality;
    97         foreach (var neighbor in GetNeighborsFunc(individual, random).Take(sampleSize)) {
     98        foreach (var neighbor in GetNeighborsFunc(solution, random).Take(sampleSize)) {
    9899          var q = EvaluateFunc(neighbor, random);
    99100          count++;
     
    103104        }
    104105        if (best == null) break;
    105         individual = best;
     106        solution = best;
    106107        quality = bestQuality;
    107108      }
     
    109110      LocalEvaluatedSolutionsParameter.ActualValue = new IntValue(count);
    110111      QualityParameter.ActualValue = new DoubleValue(quality);
    111       individual.CopyToScope(ExecutionContext.Scope);
     112
     113      ScopeUtil.CopySolutionToScope(ExecutionContext.Scope, encoding, solution);
    112114      return base.Apply();
    113115    }
Note: See TracChangeset for help on using the changeset viewer.