Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/13/15 15:00:15 (9 years ago)
Author:
abeham
Message:

#2174, #2282: merged revisions r11961,r11963,r11967,r11970,r11971,r11982,r11984,r11998,r12001,r12002,r12003,r12004,r11939,r11945,r11956,r11958,r11959,r11960,r11983,r11987,r11988,r11990,r11993,r11994,r11996,r11999,r12000 to stable

Location:
stable
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.Binary/3.3/BinaryProblem.cs

    r11987 r12005  
    2222#endregion
    2323
     24using System;
     25using System.Linq;
    2426using HeuristicLab.Common;
    2527using HeuristicLab.Core;
     
    3335  [StorableClass]
    3436  public abstract class BinaryProblem : SingleObjectiveBasicProblem<BinaryVectorEncoding> {
    35 
    3637    public virtual int Length {
    3738      get { return Encoding.Length; }
     
    3940    }
    4041
     42    private IFixedValueParameter<IntValue> LengthParameter {
     43      get { return (IFixedValueParameter<IntValue>)Parameters["Length"]; }
     44    }
     45
    4146    [StorableConstructor]
    4247    protected BinaryProblem(bool deserializing) : base(deserializing) { }
    43     protected BinaryProblem(BinaryProblem original, Cloner cloner) : base(original, cloner) { }
    44     protected BinaryProblem() : base() { }
     48    [StorableHook(HookType.AfterDeserialization)]
     49    private void AfterDeserialization() {
     50      RegisterEventHandlers();
     51    }
     52
     53    protected BinaryProblem(BinaryProblem original, Cloner cloner)
     54      : base(original, cloner) {
     55      RegisterEventHandlers();
     56    }
     57
     58    protected BinaryProblem()
     59      : base() {
     60      var lengthParameter = new FixedValueParameter<IntValue>("Length", "The length of the BinaryVector.", new IntValue(10));
     61      Parameters.Add(lengthParameter);
     62      Encoding.LengthParameter = lengthParameter;
     63      RegisterEventHandlers();
     64    }
    4565
    4666    public virtual bool IsBetter(double quality, double bestQuality) {
     
    4868    }
    4969
     70    public abstract double Evaluate(BinaryVector vector, IRandom random);
    5071    public sealed override double Evaluate(Individual individual, IRandom random) {
    5172      return Evaluate(individual.BinaryVector(), random);
    5273    }
    5374
    54     public abstract double Evaluate(BinaryVector vector, IRandom random);
     75    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
     76      base.Analyze(individuals, qualities, results, random);
     77      var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
     78      var best = Maximization ? orderedIndividuals.Last().Individual : orderedIndividuals.First().Individual;
     79
     80      if (!results.ContainsKey("Best Solution")) {
     81        results.Add(new Result("Best Solution", typeof(BinaryVector)));
     82      }
     83      results["Best Solution"].Value = (IItem)best.BinaryVector().Clone();
     84    }
     85
     86    protected override void OnEncodingChanged() {
     87      base.OnEncodingChanged();
     88      Encoding.LengthParameter = LengthParameter;
     89    }
     90
     91
     92    private void RegisterEventHandlers() {
     93      LengthParameter.Value.ValueChanged += LengthParameter_ValueChanged;
     94    }
     95
     96    protected virtual void LengthParameter_ValueChanged(object sender, EventArgs e) { }
    5597  }
    5698}
Note: See TracChangeset for help on using the changeset viewer.