source: branches/2864_PermutationProblems/HeuristicLab.Problems.PFSP/3.3/Evaluators/MakeSpanEvaluator.cs @ 15521

Last change on this file since 15521 was 15521, checked in by fholzing, 21 months ago

#2864: First commit of new branch of Permutation based benchmark problems.

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.PFSP
31{
32    /// <summary>
33    /// A base class for operators which evaluate TSP solutions given in path representation using city coordinates.
34    /// </summary>
35    [Item("MakeSpanEvaluator", "A base class for operators which evaluate TSP solutions given in path representation using city coordinates.")]
36    [StorableClass]
37    public abstract class MakeSpanEvaluator : PFSPEvaluator, IMakespanEvaluator
38    {
39        private object locker = new object();
40
41        public ILookupParameter<Permutation> PermutationParameter { get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; } }
42        public ILookupParameter<JobMatrix> JobMatrixParameter { get { return (ILookupParameter<JobMatrix>)Parameters["JobMatrix"]; } }
43        public ILookupParameter<Permutation> BestKnownSolutionParameter { get { return (ILookupParameter<Permutation>)Parameters["BestKnownSolution"]; } }
44        public ILookupParameter<DoubleValue> BestKnownQualityParameter { get { return (ILookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } }
45        [StorableConstructor]
46        protected MakeSpanEvaluator(bool deserializing) : base(deserializing) { }
47        protected MakeSpanEvaluator(MakeSpanEvaluator original, Cloner cloner) : base(original, cloner) { }
48        protected MakeSpanEvaluator()
49          : base()
50        {
51            Parameters.Add(new LookupParameter<Permutation>("Permutation", "The TSP solution given in path representation which should be evaluated."));
52            Parameters.Add(new LookupParameter<JobMatrix>("JobMatrix", "The matrix which contains the distances between the cities."));
53            Parameters.Add(new LookupParameter<Permutation>("BestKnownSolution", "The currently best known solution."));
54            Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The currently best known quality."));
55        }
56
57        [StorableHook(HookType.AfterDeserialization)]
58        private void AfterDeserialization()
59        {
60            // BackwardsCompatibility3.3
61            #region Backwards compatible code (remove with 3.4)
62            LookupParameter<DoubleMatrix> oldDistanceMatrixParameter = Parameters["JobMatrix"] as LookupParameter<DoubleMatrix>;
63            if (oldDistanceMatrixParameter != null)
64            {
65                Parameters.Remove(oldDistanceMatrixParameter);
66                Parameters.Add(new LookupParameter<JobMatrix>("JobMatrix", "The matrix which contains the distances between the cities."));
67                JobMatrixParameter.ActualName = oldDistanceMatrixParameter.ActualName;
68            }
69            #endregion
70        }
71
72        public static double Apply(MakeSpanEvaluator evaluator, DoubleMatrix matrix, Permutation tour)
73        {
74            double length = evaluator.CalculateMakespan(matrix, tour);
75            return length;
76        }
77
78        public sealed override IOperation InstrumentedApply()
79        {
80            Permutation p = PermutationParameter.ActualValue;
81            DoubleMatrix c = JobMatrixParameter.ActualValue;
82            if (c == null) throw new InvalidOperationException("No coordinates were given.");
83
84            double makespan = CalculateMakespan(c, p);
85            QualityParameter.ActualValue = new DoubleValue(makespan);
86
87            if (BestKnownQualityParameter.ActualValue == null || BestKnownQualityParameter.ActualValue.Value > makespan)
88            {
89                BestKnownSolutionParameter.ActualValue = p;
90                BestKnownQualityParameter.ActualValue = new DoubleValue(makespan);
91            }
92            return base.InstrumentedApply();
93        }
94
95        protected abstract double CalculateMakespan(DoubleMatrix matrix, Permutation tour);
96    }
97}
Note: See TracBrowser for help on using the repository browser.