Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/27/14 10:53:57 (10 years ago)
Author:
pfleck
Message:

#2208 Added distance penalty calculation in OrienteeringEvaluator.

Location:
branches/HeuristicLab.Problems.Orienteering
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringProblemView.cs

    r11267 r11311  
    6363      } else {
    6464        orienteeringSolutionView.Content = new OrienteeringSolution(
    65           Content.BestKnownSolution, Content.Coordinates, Content.StartingPoint, Content.TerminusPoint, Content.Scores, Content.BestKnownQuality);
     65          Content.BestKnownSolution, Content.Coordinates, Content.StartingPoint, Content.TerminusPoint, Content.Scores, Content.BestKnownQuality, penalty: null);
    6666      }
    6767    }
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering.Views/3.3/OrienteeringSolutionView.cs

    r11276 r11311  
    4242    protected override void DeregisterContentEvents() {
    4343      Content.QualityChanged -= new EventHandler(Content_QualityChanged);
     44      Content.PenaltyChanged -= new EventHandler(Content_PenaltyChanged);
    4445      Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged);
    4546      Content.StartingPointChanged -= new EventHandler(Content_StartingPointChanged);
     
    5253      base.RegisterContentEvents();
    5354      Content.QualityChanged += new EventHandler(Content_QualityChanged);
     55      Content.PenaltyChanged += new EventHandler(Content_PenaltyChanged);
    5456      Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged);
    5557      Content.StartingPointChanged += new EventHandler(Content_StartingPointChanged);
     
    114116                  tour[i] = points[integerVector[i]];
    115117                }
    116                 graphics.DrawLines(Pens.Black, tour);
     118                bool visualizePenalty = Content.Penalty != null && Content.Penalty.Value > 0;
     119                graphics.DrawLines(visualizePenalty ? Pens.Red : Pens.Black, tour);
    117120              }
    118121
     
    154157        qualityViewHost.Content = Content.Quality;
    155158    }
     159    private void Content_PenaltyChanged(object sender, EventArgs e) {
     160      if (InvokeRequired)
     161        Invoke(new EventHandler(Content_PenaltyChanged), sender, e);
     162      else
     163        GenerateImage();
     164    }
    156165    private void Content_CoordinatesChanged(object sender, EventArgs e) {
    157166      if (InvokeRequired)
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Analyzers/BestOrienteeringSolutionAnalyser.cs

    r11307 r11311  
    5555      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
    5656    }
     57    public ScopeTreeLookupParameter<DoubleValue> PenaltyParameter {
     58      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Penalty"]; }
     59    }
    5760    public LookupParameter<OrienteeringSolution> BestSolutionParameter {
    5861      get { return (LookupParameter<OrienteeringSolution>)Parameters["BestSolution"]; }
     
    6669    public LookupParameter<IntegerVector> BestKnownSolutionParameter {
    6770      get { return (LookupParameter<IntegerVector>)Parameters["BestKnownSolution"]; }
     71    }
     72    public LookupParameter<DoubleValue> BestKnownSolutionPenaltyParameter {
     73      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownSolutionPenalty"]; }
    6874    }
    6975
     
    8288      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
    8389      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the Orienteering solutions which should be analyzed."));
     90      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Penalty", "The applied penalty of the Orienteering solutions."));
    8491      Parameters.Add(new LookupParameter<OrienteeringSolution>("BestSolution", "The best Orienteering solution."));
    8592      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the best Orienteering solution should be stored."));
    8693      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this Orienteering instance."));
    8794      Parameters.Add(new LookupParameter<IntegerVector>("BestKnownSolution", "The best known solution of this Orienteering instance."));
     95      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownSolutionPenalty", "The penalty of the best known solution of this Orienteering instance."));
    8896    }
    8997
     
    9199      var solutions = IntegerVector.ActualValue;
    92100      var qualities = QualityParameter.ActualValue;
     101      var penalties = PenaltyParameter.ActualValue;
    93102      var results = ResultsParameter.ActualValue;
    94103      var bestKnownQuality = BestKnownQualityParameter.ActualValue;
     
    99108        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[bestIndex].Value);
    100109        BestKnownSolutionParameter.ActualValue = (IntegerVector)solutions[bestIndex].Clone();
     110        BestKnownSolutionPenaltyParameter.ActualValue = new DoubleValue(penalties[bestIndex].Value);
    101111      }
    102112
     
    113123          terminusPoint,
    114124          scores,
    115           new DoubleValue(qualities[bestIndex].Value));
     125          new DoubleValue(qualities[bestIndex].Value),
     126          new DoubleValue(penalties[bestIndex].Value));
    116127        BestSolutionParameter.ActualValue = solution;
    117128        results.Add(new Result("Best Orienteering Solution", solution));
     129        results.Add(new Result("BestKnownSolutionPenalty", solution.Penalty));
    118130      } else {
    119131        if (solution.Quality.Value < qualities[bestIndex].Value) {
     
    122134          solution.IntegerVector = (IntegerVector)solutions[bestIndex].Clone();
    123135          solution.Quality.Value = qualities[bestIndex].Value;
     136          solution.Penalty.Value = penalties[bestIndex].Value;
     137          results["BestKnownSolutionPenalty"].Value = solution.Penalty;
    124138        }
    125139      }
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Evaluators/OrienteeringEvaluator.cs

    r11236 r11311  
    2020#endregion
    2121
     22using System.Linq;
     23using System.Security.Permissions;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    3032namespace HeuristicLab.Problems.Orienteering {
    3133  public class OrienteeringEvaluator : InstrumentedOperator, IOrienteeringEvaluator {
     34
     35    #region ParameterProperties
    3236    public ILookupParameter<DoubleValue> QualityParameter {
    3337      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
     38    }
     39    public ILookupParameter<DoubleValue> PenaltyParameter {
     40      get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; }
     41    }
     42    public ILookupParameter<DoubleValue> DistancePenaltyFactorParameter {
     43      get { return (ILookupParameter<DoubleValue>)Parameters["DistancePenaltyFactor"]; }
    3444    }
    3545    public ILookupParameter<IntegerVector> IntegerVectorParameter {
     
    3949      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
    4050    }
     51    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
     52      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
     53    }
     54    public ILookupParameter<DoubleValue> MaximumDistanceParameter {
     55      get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
     56    }
     57    public ILookupParameter<DoubleValue> FixedPenaltyParameter {
     58      get { return (ILookupParameter<DoubleValue>)Parameters["FixedPenalty"]; }
     59    }
     60    #endregion
    4161
    4262    [StorableConstructor]
     
    5373      : base() {
    5474      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The evaluated quality of the Orienteering solution."));
     75      Parameters.Add(new LookupParameter<DoubleValue>("Penalty", "The applied penalty"));
     76      Parameters.Add(new ValueLookupParameter<DoubleValue>("DistancePenaltyFactor", "The penalty applied per distance violation.", new DoubleValue(0)));
     77
    5578      Parameters.Add(new LookupParameter<IntegerVector>("IntegerVector", "The Orienteering Solution given in path representation."));
    5679      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
     80      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
     81      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
     82      Parameters.Add(new LookupParameter<DoubleValue>("FixedPenalty", "The penalty for each visited vertex."));
    5783    }
    5884
    5985    public class OrienteeringEvaluation {
    6086      public DoubleValue Quality;
     87      public DoubleValue Penalty;
    6188    }
    6289
    63     public static OrienteeringEvaluation Apply(IntegerVector solution, DoubleArray scores) {
    64       // TODO distance penalty
    65       double score = 0.0;
     90    public static OrienteeringEvaluation Apply(IntegerVector solution, DoubleArray scores,
     91      DistanceMatrix distances, DoubleValue maximumDistance, DoubleValue fixedPenalty, DoubleValue distancePenaltyFactor) {
    6692
    67       for (int i = 0; i < solution.Length; i++) {
    68         score += scores[solution[i]];
    69       }
     93      double score = solution.Sum(t => scores[t]);
     94      double distance = distances.CalculateTourLength(solution.ToList(), fixedPenalty.Value);
     95
     96      double distanceViolation = distance - maximumDistance.Value;
     97
     98      double penalty = 0.0;
     99      penalty += distanceViolation > 0 ? distanceViolation * distancePenaltyFactor.Value : 0;
     100
     101      double quality = score - penalty;
    70102
    71103      return new OrienteeringEvaluation {
    72         Quality = new DoubleValue(score)
     104        Quality = new DoubleValue(quality),
     105        Penalty = new DoubleValue(penalty)
    73106      };
    74107    }
    75108
    76109    public override IOperation InstrumentedApply() {
    77       var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue);
     110      var evaluation = Apply(IntegerVectorParameter.ActualValue, ScoresParameter.ActualValue,
     111        DistanceMatrixParameter.ActualValue, MaximumDistanceParameter.ActualValue,
     112        FixedPenaltyParameter.ActualValue, DistancePenaltyFactorParameter.ActualValue);
    78113
    79114      QualityParameter.ActualValue = evaluation.Quality;
     115      PenaltyParameter.ActualValue = evaluation.Penalty;
    80116
    81117      return base.InstrumentedApply();
  • branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/OrienteeringSolution.cs

    r11307 r11311  
    9090          if (quality != null) RegisterQualityEvents();
    9191          OnQualityChanged();
     92        }
     93      }
     94    }
     95    [Storable]
     96    private DoubleValue penalty;
     97    public DoubleValue Penalty {
     98      get { return penalty; }
     99      set {
     100        if (penalty != value) {
     101          if (penalty != null) DeregisterPenaltyEvents();
     102          penalty = value;
     103          if (penalty != null) RegisterPenaltyEvents();
     104          OnPenaltyChanged();
    92105        }
    93106      }
     
    102115      this.coordinates = cloner.Clone(original.coordinates);
    103116      this.quality = cloner.Clone(original.quality);
     117      this.penalty = cloner.Clone(original.penalty);
    104118      Initialize();
    105119    }
    106120    public OrienteeringSolution(IntegerVector integerVector, DoubleMatrix coordinates, IntValue startingPoint, IntValue terminusPoint,
    107       DoubleArray scores, DoubleValue quality)
     121      DoubleArray scores, DoubleValue quality, DoubleValue penalty)
    108122      : base() {
    109123      this.integerVector = integerVector;
     
    113127      this.scores = scores;
    114128      this.quality = quality;
     129      this.penalty = penalty;
    115130      Initialize();
    116131    }
     
    132147      if (scores != null) RegisterScoresEvents();
    133148      if (quality != null) RegisterQualityEvents();
     149      if (penalty != null) RegisterPenaltyEvents();
    134150    }
    135151
     
    177193    }
    178194
     195    public event EventHandler PenaltyChanged;
     196    private void OnPenaltyChanged() {
     197      var changed = PenaltyChanged;
     198      if (changed != null)
     199        changed(this, EventArgs.Empty);
     200    }
     201
    179202    private void RegisterIntegerVectorEvents() {
    180203      IntegerVector.ItemChanged += new EventHandler<EventArgs<int>>(IntegerVector_ItemChanged);
     
    219242      Quality.ValueChanged -= new EventHandler(Quality_ValueChanged);
    220243    }
     244    private void RegisterPenaltyEvents() {
     245      Penalty.ValueChanged += new EventHandler(Penalty_ValueChanged);
     246    }
     247    private void DeregisterPenaltyEvents() {
     248      Penalty.ValueChanged -= new EventHandler(Penalty_ValueChanged);
     249    }
    221250
    222251    private void IntegerVector_ItemChanged(object sender, EventArgs<int> e) {
     
    246275    private void Quality_ValueChanged(object sender, EventArgs e) {
    247276      OnQualityChanged();
     277    }
     278    private void Penalty_ValueChanged(object sender, EventArgs e) {
     279      OnPenaltyChanged();
    248280    }
    249281    #endregion
Note: See TracChangeset for help on using the changeset viewer.