Changeset 17241 for branches/2521_ProblemRefactoring
- Timestamp:
- 09/10/19 21:55:35 (5 years ago)
- Location:
- branches/2521_ProblemRefactoring
- Files:
-
- 2 added
- 21 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2521_ProblemRefactoring/HeuristicLab.Optimizer/3.3/StartPage.cs
r17226 r17241 110 110 } 111 111 112 } catch { 113 } finally { 112 114 OnAllSamplesLoaded(); 113 } finally {114 115 Progress.HideFromControl(samplesListView); 115 116 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/HeuristicLab.Problems.TravelingSalesman.Views-3.3.csproj
r16723 r17241 128 128 </Compile> 129 129 <Compile Include="Plugin.cs" /> 130 <Compile Include="TravelingSalesmanProblemView.cs">131 <SubType>UserControl</SubType>132 </Compile>133 <Compile Include="TravelingSalesmanProblemView.Designer.cs">134 <DependentUpon>TravelingSalesmanProblemView.cs</DependentUpon>135 </Compile>136 130 <Compile Include="Properties\AssemblyInfo.cs" /> 137 131 </ItemGroup> -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman.Views/3.3/PathTSPTourView.cs
r17226 r17241 21 21 22 22 using System; 23 using System.ComponentModel; 23 24 using System.Drawing; 24 25 using System.Windows.Forms; … … 48 49 49 50 protected override void DeregisterContentEvents() { 50 Content.QualityChanged -= new EventHandler(Content_QualityChanged); 51 Content.CoordinatesChanged -= new EventHandler(Content_CoordinatesChanged); 52 Content.PermutationChanged -= new EventHandler(Content_PermutationChanged); 51 Content.PropertyChanged -= ContentOnPropertyChanged; 53 52 base.DeregisterContentEvents(); 54 53 } 55 54 protected override void RegisterContentEvents() { 56 55 base.RegisterContentEvents(); 57 Content.QualityChanged += new EventHandler(Content_QualityChanged); 58 Content.CoordinatesChanged += new EventHandler(Content_CoordinatesChanged); 59 Content.PermutationChanged += new EventHandler(Content_PermutationChanged); 56 Content.PropertyChanged += ContentOnPropertyChanged; 60 57 } 61 58 … … 67 64 tourViewHost.Content = null; 68 65 } else { 69 qualityViewHost.Content = Content. Quality;66 qualityViewHost.Content = Content.TourLength; 70 67 GenerateImage(); 71 tourViewHost.Content = Content. Permutation;68 tourViewHost.Content = Content.Tour; 72 69 } 73 70 } … … 86 83 } else { 87 84 DoubleMatrix coordinates = Content.Coordinates; 88 Permutation permutation = Content. Permutation;85 Permutation permutation = Content.Tour; 89 86 Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height); 90 87 … … 133 130 } 134 131 135 private void Content _QualityChanged(object sender,EventArgs e) {132 private void ContentOnPropertyChanged(object sender, PropertyChangedEventArgs e) { 136 133 if (InvokeRequired) 137 Invoke(new EventHandler(Content_QualityChanged), sender, e); 138 else 139 qualityViewHost.Content = Content.Quality; 140 } 141 private void Content_CoordinatesChanged(object sender, EventArgs e) { 142 if (InvokeRequired) 143 Invoke(new EventHandler(Content_CoordinatesChanged), sender, e); 144 else 145 GenerateImage(); 146 } 147 private void Content_PermutationChanged(object sender, EventArgs e) { 148 if (InvokeRequired) 149 Invoke(new EventHandler(Content_PermutationChanged), sender, e); 134 Invoke((Action<object, PropertyChangedEventArgs>)ContentOnPropertyChanged, sender, e); 150 135 else { 151 GenerateImage(); 152 tourViewHost.Content = Content.Permutation; 136 switch (e.PropertyName) { 137 case nameof(Content.Coordinates): 138 GenerateImage(); 139 break; 140 case nameof(Content.Tour): 141 GenerateImage(); 142 tourViewHost.Content = Content.Tour; 143 break; 144 case nameof(Content.TourLength): 145 qualityViewHost.Content = Content.TourLength; 146 break; 147 } 153 148 } 154 149 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Analyzers/TSPAlleleFrequencyAnalyzer.cs
r17226 r17241 20 20 #endregion 21 21 22 using System;22 using HEAL.Attic; 23 23 using HeuristicLab.Analysis; 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data;27 26 using HeuristicLab.Encodings.PermutationEncoding; 28 27 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 28 31 29 namespace HeuristicLab.Problems.TravelingSalesman { … … 34 32 /// </summary> 35 33 [Item("TSPAlleleFrequencyAnalyzer", "An operator for analyzing the frequency of alleles in solutions of Traveling Salesman Problems given in path representation.")] 36 [StorableType(" C1BBEC5A-27EF-4882-AEC6-0919FC2EF1DB")]34 [StorableType("43a9ec34-c917-43f8-bd14-4d42e2d0a458")] 37 35 public sealed class TSPAlleleFrequencyAnalyzer : AlleleFrequencyAnalyzer<Permutation> { 38 public LookupParameter<DoubleMatrix> CoordinatesParameter { 39 get { return (LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; } 40 } 41 public LookupParameter<DistanceMatrix> DistanceMatrixParameter { 42 get { return (LookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; } 43 } 36 [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; } 44 37 45 38 [StorableConstructor] 46 39 private TSPAlleleFrequencyAnalyzer(StorableConstructorFlag _) : base(_) { } 47 private TSPAlleleFrequencyAnalyzer(TSPAlleleFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { } 40 private TSPAlleleFrequencyAnalyzer(TSPAlleleFrequencyAnalyzer original, Cloner cloner) 41 : base(original, cloner) { 42 TSPDataParameter = cloner.Clone(original.TSPDataParameter); 43 } 48 44 public TSPAlleleFrequencyAnalyzer() 49 45 : base() { 50 Parameters.Add(new LookupParameter<DoubleMatrix>("Coordinates", "The x- and y-coordinates of the cities.")); 51 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 46 Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 52 47 } 53 48 … … 57 52 58 53 protected override Allele[] CalculateAlleles(Permutation solution) { 54 var tspData = TSPDataParameter.ActualValue; 59 55 Allele[] alleles = new Allele[solution.Length]; 60 DoubleMatrix coords = CoordinatesParameter.ActualValue;61 DistanceMatrix dm = DistanceMatrixParameter.ActualValue;62 if (dm == null && coords == null) throw new InvalidOperationException("Neither a distance matrix nor coordinates were given.");63 56 int source, target, h; 64 57 double impact; … … 68 61 target = solution[i + 1]; 69 62 if (source > target) { h = source; source = target; target = h; } 70 impact = dm != null ? dm[source, target] : CalculateLength(coords[source, 0], coords[source, 1], coords[target, 0], coords[target, 1]);63 impact = tspData.GetDistance(source, target); 71 64 alleles[i] = new Allele(source.ToString() + "-" + target.ToString(), impact); 72 65 } … … 74 67 target = solution[0]; 75 68 if (source > target) { h = source; source = target; target = h; } 76 impact = dm != null ? dm[source, target] : CalculateLength(coords[source, 0], coords[source, 1], coords[target, 0], coords[target, 1]);69 impact = tspData.GetDistance(source, target); 77 70 alleles[alleles.Length - 1] = new Allele(source.ToString() + "-" + target.ToString(), impact); 78 71 79 72 return alleles; 80 73 } 81 82 private double CalculateLength(double x1, double y1, double x2, double y2) {83 return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));84 }85 74 } 86 75 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/HeuristicLab.Problems.TravelingSalesman-3.3.csproj
r17239 r17241 118 118 </ItemGroup> 119 119 <ItemGroup> 120 <Compile Include="Analyzers\BestTSPSolutionAnalyzer.cs" />121 <Compile Include="Analyzers\TSPPopulationDiversityAnalyzer.cs" />122 120 <Compile Include="Analyzers\TSPAlleleFrequencyAnalyzer.cs" /> 123 <Compile Include="DistanceMatrix.cs" />124 <Compile Include="Evaluators\TSPDistanceMatrixEvaluator.cs" />125 <Compile Include="Evaluators\TSPEuclideanPathEvaluator.cs" />126 <Compile Include="Evaluators\TSPGeoPathEvaluator.cs" />127 <Compile Include="Evaluators\TSPUpperEuclideanPathEvaluator.cs" />128 121 <Compile Include="Improvers\TSPImprovementOperator.cs" /> 129 <Compile Include="Interfaces\ITSPDistanceMatrixEvaluator.cs" /> 130 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMoveDistanceMatrixEvaluator.cs" /> 131 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMoveEuclideanPathEvaluator.cs" /> 132 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMoveGeoPathEvaluator.cs" /> 133 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMovePathEvaluator.cs" /> 134 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMoveRoundedEuclideanPathEvaluator.cs" /> 135 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMoveEuclideanPathEvaluator.cs" /> 136 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMoveGeoPathEvaluator.cs" /> 137 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMoveDistanceMatrixEvaluator.cs" /> 138 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMovePathEvaluator.cs" /> 139 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMoveRoundedEuclideanPathEvaluator.cs" /> 122 <Compile Include="MoveEvaluators\ThreeOpt\TSPTranslocationMoveEvaluator.cs" /> 123 <Compile Include="MoveEvaluators\TSPMoveEvaluator.cs" /> 124 <Compile Include="MoveEvaluators\TwoOpt\TSPInversionMoveEvaluator.cs" /> 140 125 <Compile Include="PathRelinkers\TSPMultipleGuidesPathRelinker.cs" /> 141 126 <Compile Include="PathRelinkers\TSPPathRelinker.cs" /> 142 127 <Compile Include="PathRelinkers\TSPSimultaneousPathRelinker.cs" /> 143 128 <Compile Include="Plugin.cs" /> 144 <Compile Include="SimilarityCalculators\TSPSimilarityCalculator.cs" />145 <Compile Include="TravelingSalesmanProblem.cs" />146 <Compile Include="PathTSPTour.cs" />147 <Compile Include="Evaluators\TSPCoordinatesPathEvaluator.cs" />148 <Compile Include="Evaluators\TSPEvaluator.cs" />149 <Compile Include="Evaluators\TSPRoundedEuclideanPathEvaluator.cs" />150 <Compile Include="Interfaces\ITSPCoordinatesPathEvaluator.cs" />151 <Compile Include="Interfaces\ITSPEvaluator.cs" />152 <Compile Include="Interfaces\ITSPMoveEvaluator.cs" />153 <Compile Include="Interfaces\ITSPPathEvaluator.cs" />154 <Compile Include="Interfaces\ITSPPathMoveEvaluator.cs" />155 <Compile Include="MoveEvaluators\TSPPathMoveEvaluator.cs" />156 <Compile Include="MoveEvaluators\TSPMoveEvaluator.cs" />157 129 <Compile Include="Properties\AssemblyInfo.cs" /> 158 130 <Compile Include="TSP.cs" /> -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs
r17226 r17241 21 21 22 22 using System; 23 using HEAL.Attic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 28 29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Parameters; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.TravelingSalesman { … … 38 38 /// </remarks> 39 39 [Item("TSPImprovementOperator", "An operator that improves traveling salesman solutions. The operator tries to improve the traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")] 40 [StorableType(" 9C3B53A4-8FE7-45FC-833B-FF3DAE578010")]40 [StorableType("1b3cbc66-6dcc-4f61-9cbe-8b50cc54413a")] 41 41 public sealed class TSPImprovementOperator : SingleSuccessorOperator, ISingleObjectiveImprovementOperator { 42 #region Parameter properties 43 public ScopeParameter CurrentScopeParameter { 44 get { return (ScopeParameter)Parameters["CurrentScope"]; } 42 43 [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; } 44 [Storable] public IValueParameter<IntValue> ImprovementAttemptsParameter { get; private set; } 45 [Storable] public ILookupParameter<IRandom> RandomParameter { get; private set; } 46 [Storable] public IValueLookupParameter<IItem> SolutionParameter { get; private set; } 47 48 public int ImprovementAttempts { 49 get { return ImprovementAttemptsParameter.Value.Value; } 50 set { ImprovementAttemptsParameter.Value.Value = value; } 45 51 } 46 public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {47 get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }48 }49 public IValueParameter<IntValue> ImprovementAttemptsParameter {50 get { return (IValueParameter<IntValue>)Parameters["ImprovementAttempts"]; }51 }52 public ILookupParameter<IRandom> RandomParameter {53 get { return (ILookupParameter<IRandom>)Parameters["Random"]; }54 }55 public IValueLookupParameter<IItem> SolutionParameter {56 get { return (IValueLookupParameter<IItem>)Parameters["Solution"]; }57 }58 #endregion59 60 #region Properties61 public IScope CurrentScope {62 get { return CurrentScopeParameter.ActualValue; }63 }64 public DistanceMatrix DistanceMatrix {65 get { return DistanceMatrixParameter.ActualValue; }66 set { DistanceMatrixParameter.ActualValue = value; }67 }68 public IntValue ImprovementAttempts {69 get { return ImprovementAttemptsParameter.Value; }70 set { ImprovementAttemptsParameter.Value = value; }71 }72 public IRandom Random {73 get { return RandomParameter.ActualValue; }74 set { RandomParameter.ActualValue = value; }75 }76 #endregion77 52 78 53 [StorableConstructor] 79 54 private TSPImprovementOperator(StorableConstructorFlag _) : base(_) { } 80 private TSPImprovementOperator(TSPImprovementOperator original, Cloner cloner) : base(original, cloner) { } 55 private TSPImprovementOperator(TSPImprovementOperator original, Cloner cloner) 56 : base(original, cloner) { 57 TSPDataParameter = cloner.Clone(original.TSPDataParameter); 58 ImprovementAttemptsParameter = cloner.Clone(original.ImprovementAttemptsParameter); 59 RandomParameter = cloner.Clone(original.RandomParameter); 60 SolutionParameter = cloner.Clone(original.SolutionParameter); 61 } 81 62 public TSPImprovementOperator() 82 63 : base() { 83 64 #region Create parameters 84 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the solution to be improved.")); 85 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 86 Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100))); 87 Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator.")); 88 Parameters.Add(new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only.")); 65 Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 66 Parameters.Add(ImprovementAttemptsParameter = new ValueParameter<IntValue>("ImprovementAttempts", "The number of improvement attempts the operator should perform.", new IntValue(100))); 67 Parameters.Add(RandomParameter = new LookupParameter<IRandom>("Random", "A pseudo random number generator.")); 68 Parameters.Add(SolutionParameter = new ValueLookupParameter<IItem>("Solution", "The solution to be improved. This parameter is used for name translation only.")); 89 69 #endregion 90 70 } … … 95 75 96 76 public override IOperation Apply() { 97 var solution = CurrentScope.Variables[SolutionParameter.ActualName].Value as Permutation; 77 var random = RandomParameter.ActualValue; 78 var solution = ExecutionContext.Scope.Variables[SolutionParameter.ActualName].Value as Permutation; 98 79 if (solution == null) 99 80 throw new ArgumentException("Cannot improve solution because it has the wrong type."); 100 81 if (solution.PermutationType != PermutationTypes.RelativeUndirected) 101 82 throw new ArgumentException("Cannot improve solution because the permutation type is not supported."); 83 var tspData = TSPDataParameter.ActualValue; 102 84 103 for (int i = 0; i < ImprovementAttempts .Value; i++) {104 var move = StochasticInversionSingleMoveGenerator.Apply(solution, Random);105 double moveQualtiy = TSPInversionMove PathEvaluator.EvaluateByDistanceMatrix(solution, move, DistanceMatrix);85 for (int i = 0; i < ImprovementAttempts; i++) { 86 var move = StochasticInversionSingleMoveGenerator.Apply(solution, random); 87 double moveQualtiy = TSPInversionMoveEvaluator.CalculateTourLengthDelta(tspData, solution, move); 106 88 if (moveQualtiy < 0) 107 89 InversionManipulator.Apply(solution, move.Index1, move.Index2); 108 90 } 109 91 110 CurrentScope.Variables.Add(new Variable("LocalEvaluatedSolutions", ImprovementAttempts));92 ExecutionContext.Scope.Variables.Add(new Variable("LocalEvaluatedSolutions", new IntValue(ImprovementAttempts))); 111 93 112 94 return base.Apply(); -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/MoveEvaluators/TSPMoveEvaluator.cs
r17226 r17241 1 #region License Information 2 /* HeuristicLab 3 * Copyright (C) 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 22 using System; 1 using HEAL.Attic; 23 2 using HeuristicLab.Common; 24 3 using HeuristicLab.Core; 25 4 using HeuristicLab.Data; 5 using HeuristicLab.Encodings.PermutationEncoding; 26 6 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization;28 7 using HeuristicLab.Parameters; 29 using HEAL.Attic;30 8 31 9 namespace HeuristicLab.Problems.TravelingSalesman { 32 /// <summary> 33 /// A base class for operators which evaluate TSP solutions. 34 /// </summary> 35 [Item("TSPMoveEvaluator", "A base class for operators which evaluate TSP moves.")] 36 [StorableType("25573174-DE4B-4076-B439-CCB5F01CE652")] 37 public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator, IMoveOperator { 10 [StorableType("17477ad2-2c84-4b02-b5ac-f35ef6cc667f")] 11 public interface ITSPMoveEvaluator : IOperator { 12 ILookupParameter<Permutation> TSPTourParameter { get; } 13 ILookupParameter<ITSPData> TSPDataParameter { get; } 14 ILookupParameter<DoubleValue> TourLengthParameter { get; } 15 ILookupParameter<DoubleValue> TourLengthWithMoveParameter { get; } 16 } 38 17 39 public abstract Type EvaluatorType { get; } 40 public override bool CanChangeName { 41 get { return false; } 42 } 43 44 public ILookupParameter<DoubleValue> QualityParameter { 45 get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; } 46 } 47 public ILookupParameter<DoubleValue> MoveQualityParameter { 48 get { return (ILookupParameter<DoubleValue>)Parameters["MoveQuality"]; } 49 } 18 [Item("TSP Move Evaluator", "Base class for all move evaluators of the TSP.")] 19 [StorableType("44af3845-ccdf-4014-805a-5878c64f67f5")] 20 public abstract class TSPMoveEvaluator : SingleSuccessorOperator, ITSPMoveEvaluator { 21 [Storable] public ILookupParameter<Permutation> TSPTourParameter { get; private set; } 22 [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; } 23 [Storable] public ILookupParameter<DoubleValue> TourLengthParameter { get; private set; } 24 [Storable] public ILookupParameter<DoubleValue> TourLengthWithMoveParameter { get; private set; } 50 25 51 26 [StorableConstructor] 52 27 protected TSPMoveEvaluator(StorableConstructorFlag _) : base(_) { } 53 protected TSPMoveEvaluator(TSPMoveEvaluator original, Cloner cloner) : base(original, cloner) { } 54 protected TSPMoveEvaluator() 55 : base() { 56 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of a TSP solution.")); 57 Parameters.Add(new LookupParameter<DoubleValue>("MoveQuality", "The evaluated quality of a move on a TSP solution.")); 28 protected TSPMoveEvaluator(TSPMoveEvaluator original, Cloner cloner) 29 : base(original, cloner) { 30 TSPTourParameter = cloner.Clone(original.TSPTourParameter); 31 TSPDataParameter = cloner.Clone(original.TSPDataParameter); 32 TourLengthParameter = cloner.Clone(original.TourLengthParameter); 33 TourLengthWithMoveParameter = cloner.Clone(original.TourLengthWithMoveParameter); 58 34 } 35 public TSPMoveEvaluator() { 36 Parameters.Add(TSPTourParameter = new LookupParameter<Permutation>("TSPTour", "The tour that describes a solution to the TSP.")); 37 Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 38 Parameters.Add(TourLengthParameter = new LookupParameter<DoubleValue>("TourLength", "The length of a TSP tour.")); 39 Parameters.Add(TourLengthWithMoveParameter = new LookupParameter<DoubleValue>("TourLengthWithMove", "The length of the TSP tour if the move was applied.")); 40 } 41 42 public sealed override IOperation Apply() { 43 var tour = TSPTourParameter.ActualValue; 44 var tspData = TSPDataParameter.ActualValue; 45 var tourLength = TourLengthParameter.ActualValue.Value; 46 TourLengthWithMoveParameter.ActualValue = new DoubleValue( 47 CalculateTourLengthWithMove(tspData, tour, tourLength) 48 ); 49 return base.Apply(); 50 } 51 52 protected abstract double CalculateTourLengthWithMove(ITSPData tspData, Permutation tspTour, double tourLength); 59 53 } 60 54 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPMultipleGuidesPathRelinker.cs
r17226 r17241 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 29 30 using HeuristicLab.Optimization.Operators; 30 31 using HeuristicLab.Parameters; 31 using HEAL.Attic;32 32 33 33 namespace HeuristicLab.Problems.TravelingSalesman { … … 39 39 /// </remarks> 40 40 [Item("TSPMultipleGuidesPathRelinker", "An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy. The operator incrementally changes the initiating solution towards the guiding solution by correcting edges as needed. For each city it choses the best edge from all guiding solutions.")] 41 [StorableType(" 6B5B2622-AB1D-47E6-8BBC-C6088D393149")]41 [StorableType("13e879fd-7621-402e-9345-7dbfdd78e3b6")] 42 42 public sealed class TSPMultipleGuidesPathRelinker : SingleObjectivePathRelinker { 43 #region Parameter properties 44 public ILookupParameter<DistanceMatrix> DistanceMatrixParameter { 45 get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; } 46 } 47 #endregion 48 49 #region Properties 50 public DistanceMatrix DistanceMatrix { 51 get { return DistanceMatrixParameter.ActualValue; } 52 } 53 #endregion 43 [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; } 54 44 55 45 [StorableConstructor] 56 46 private TSPMultipleGuidesPathRelinker(StorableConstructorFlag _) : base(_) { } 57 private TSPMultipleGuidesPathRelinker(TSPMultipleGuidesPathRelinker original, Cloner cloner) : base(original, cloner) { } 47 private TSPMultipleGuidesPathRelinker(TSPMultipleGuidesPathRelinker original, Cloner cloner) 48 : base(original, cloner) { 49 TSPDataParameter = cloner.Clone(original.TSPDataParameter); 50 } 58 51 public TSPMultipleGuidesPathRelinker() 59 52 : base() { 60 #region Create parameters 61 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities.")); 62 #endregion 53 Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 63 54 } 64 55 … … 67 58 } 68 59 69 public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, DistanceMatrix distances, PercentValue n) {60 public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, ITSPData tspData, PercentValue n) { 70 61 if (!(initiator is Permutation) || guides.Any(x => !(x is Permutation))) 71 62 throw new ArgumentException("Cannot relink path because some of the provided solutions have the wrong type."); … … 84 75 int currCityIndex = i; 85 76 int bestCityIndex = (i + 1) % v1.Length; 86 double currDistance = distances[v1[currCityIndex], v1[bestCityIndex]];77 double currDistance = tspData.GetDistance(v1[currCityIndex], v1[bestCityIndex]); 87 78 // check each guiding solution 88 79 targets.ToList().ForEach(solution => { … … 92 83 int succ = solution[(node.Index + 1) % solution.Length]; 93 84 // get distances to neighbors 94 var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = distances[x, node.Id]});85 var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = tspData.GetDistance(x, node.Id) }); 95 86 var bestCity = results.Where(x => x.Distance < currDistance).OrderBy(x => x.Distance).FirstOrDefault(); 96 87 if (bestCity != null) { … … 129 120 if (parents.Length < 2) 130 121 throw new ArgumentException("The number of parents is smaller than 2."); 131 return Apply(parents[0], parents.Skip(1).ToArray(), DistanceMatrix, n);122 return Apply(parents[0], parents.Skip(1).ToArray(), TSPDataParameter.ActualValue, n); 132 123 } 133 124 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPPathRelinker.cs
r17226 r17241 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 28 29 using HeuristicLab.Encodings.PermutationEncoding; 29 30 using HeuristicLab.Optimization.Operators; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.TravelingSalesman { 33 33 34 /// <summary> 34 35 /// An operator that relinks paths between traveling salesman solutions. … … 38 39 /// </remarks> 39 40 [Item("TSPPathRelinker", "An operator that relinks paths between traveling salesman solutions. The operator incrementally assimilates the initiating solution into the guiding solution by correcting edges as needed.")] 40 [StorableType(" 0997A24C-0592-4CE4-A681-70C97890D3F7")]41 [StorableType("a2c5de50-a050-4269-b80e-1c995eb51626")] 41 42 public sealed class TSPPathRelinker : SingleObjectivePathRelinker { 42 43 [StorableConstructor] -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPSimultaneousPathRelinker.cs
r17226 r17241 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using HEAL.Attic; 25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; … … 28 29 using HeuristicLab.Encodings.PermutationEncoding; 29 30 using HeuristicLab.Optimization.Operators; 30 using HEAL.Attic;31 31 32 32 namespace HeuristicLab.Problems.TravelingSalesman { … … 38 38 /// </remarks> 39 39 [Item("TSPSimultaneousPathRelinker", "An operator that relinks paths between traveling salesman solutions starting from both ends. The operator incrementally assimilates the initiating solution into the guiding solution and vice versa by correcting edges as needed.")] 40 [StorableType(" D44D112E-5139-4848-8A2B-66CFC5784EE4")]40 [StorableType("73457ec0-1d71-4d9f-b646-2ba3639317e2")] 41 41 public sealed class TSPSimultaneousPathRelinker : SingleObjectivePathRelinker { 42 42 [StorableConstructor] -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSP.cs
r17239 r17241 1 1 using System; 2 using System.Linq; 2 3 using HEAL.Attic; 3 4 using HeuristicLab.Common; … … 6 7 using HeuristicLab.Encodings.PermutationEncoding; 7 8 using HeuristicLab.Optimization; 9 using HeuristicLab.Parameters; 10 using HeuristicLab.PluginInfrastructure; 8 11 using HeuristicLab.Problems.Instances; 9 12 … … 11 14 [Item("Traveling Salesman Problem (TSP)", "Represents a symmetric Traveling Salesman Problem.")] 12 15 [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 100)] 13 [StorableType("60511a03-b8b4-47cd-a119-78f97358a6b0")] 14 public class TSP : SingleObjectiveProblem<PermutationEncoding, Permutation>, IProblemInstanceConsumer<TSPData> { 16 [StorableType("8415476a-69de-45ad-95be-298ed7c97e84")] 17 public class TSP : PermutationProblem, IProblemInstanceConsumer<TSPData> { 18 /// <summary> 19 /// This limit governs when a distance matrix is used. For all problems smaller than that, the distance matrix is 20 /// computed. This greatly speeds up computation time. 21 /// </summary> 15 22 public static int DistanceMatrixSizeLimit { get; set; } = 1000; 16 23 17 24 public override bool Maximization => false; 18 25 19 [Storable] 20 private IValueParameter<ITSPData> tspDataParameter; 21 public IValueParameter<ITSPData> TSPDataParameter { 22 get { return tspDataParameter; } 23 } 24 [Storable] 25 private IValueParameter<Permutation> bestKnownSolutionParameter; 26 public IValueParameter<Permutation> BestKnownSolutionParameter { 27 get { return bestKnownSolutionParameter; } 28 } 26 [Storable] public IValueParameter<ITSPData> TSPDataParameter { get; private set; } 27 [Storable] public IValueParameter<Permutation> BestKnownSolutionParameter { get; private set; } 29 28 30 29 public ITSPData TSPData { 31 get { return tspDataParameter.Value; }32 set { tspDataParameter.Value = value; }30 get { return TSPDataParameter.Value; } 31 set { TSPDataParameter.Value = value; } 33 32 } 34 33 public Permutation BestKnownSolution { 35 get { return bestKnownSolutionParameter.Value; }36 set { bestKnownSolutionParameter.Value = value; }34 get { return BestKnownSolutionParameter.Value; } 35 set { BestKnownSolutionParameter.Value = value; } 37 36 } 38 37 … … 42 41 protected TSP(TSP original, Cloner cloner) 43 42 : base(original, cloner) { 44 tspDataParameter = cloner.Clone(original.tspDataParameter);45 bestKnownSolutionParameter = cloner.Clone(original.bestKnownSolutionParameter);43 TSPDataParameter = cloner.Clone(original.TSPDataParameter); 44 BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter); 46 45 } 47 46 48 47 public TSP() : base(new PermutationEncoding("Tour", 16, PermutationTypes.RelativeUndirected)) { 49 TSPData = new RoundedEuclideanCoordinatesTSPData() { 48 Parameters.Add(TSPDataParameter = new ValueParameter<ITSPData>("TSPData", "The main parameters of the TSP.")); 49 Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution.")); 50 51 TSPData = new EuclideanTSPData() { 50 52 Coordinates = new DoubleMatrix(new double[,] { 51 53 { 100, 100 }, { 100, 200 }, { 100, 300 }, { 100, 400 }, … … 53 55 { 300, 100 }, { 300, 200 }, { 300, 300 }, { 300, 400 }, 54 56 { 400, 100 }, { 400, 200 }, { 400, 300 }, { 400, 400 } 55 }) 57 }), 58 Rounding = EuclideanTSPData.RoundingMode.Midpoint 56 59 }; 60 61 InitializeOperators(); 57 62 } 58 63 … … 72 77 tourLength += TSPData.GetDistance(tour[tour.Length - 1], tour[0]); 73 78 return tourLength; 79 } 80 81 public override void Analyze(Permutation[] solutions, double[] qualities, ResultCollection results, IRandom random) { 82 base.Analyze(solutions, qualities, results, random); 83 int i = -1; 84 if (!Maximization) 85 i = qualities.Select((x, index) => new { index, Fitness = x }).OrderBy(x => x.Fitness).First().index; 86 else i = qualities.Select((x, index) => new { index, Fitness = x }).OrderByDescending(x => x.Fitness).First().index; 87 88 if (double.IsNaN(BestKnownQuality) || 89 Maximization && qualities[i] > BestKnownQuality || 90 !Maximization && qualities[i] < BestKnownQuality) { 91 BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i]); 92 BestKnownSolutionParameter.ActualValue = (Permutation)solutions[i].Clone(); 93 } 94 95 var solution = TSPData.GetSolution(solutions[i], qualities[i]); 96 results.AddOrUpdateResult("Best TSP Solution", solution); 74 97 } 75 98 … … 84 107 throw new System.IO.InvalidDataException("The coordinates of the given instance are not in the right format, there need to be one row for each customer and two columns for the x and y coordinates."); 85 108 109 Encoding.Length = data.Dimension; 86 110 Name = data.Name; 87 111 Description = data.Description; … … 97 121 switch (data.DistanceMeasure) { 98 122 case DistanceMeasure.Euclidean: 99 throw new NotImplementedException();123 TSPData = new EuclideanTSPData(data.Coordinates) { Rounding = EuclideanTSPData.RoundingMode.None }; 100 124 break; 101 125 case DistanceMeasure.RoundedEuclidean: 102 TSPData = new RoundedEuclideanCoordinatesTSPData(data.Coordinates);126 TSPData = new EuclideanTSPData(data.Coordinates) { Rounding = EuclideanTSPData.RoundingMode.Midpoint }; 103 127 break; 104 128 case DistanceMeasure.UpperEuclidean: 105 throw new NotImplementedException();129 TSPData = new EuclideanTSPData(data.Coordinates) { Rounding = EuclideanTSPData.RoundingMode.Ceiling }; 106 130 break; 107 131 case DistanceMeasure.Geo: 108 throw new NotImplementedException();132 TSPData = new GeoTSPData(data.Coordinates); 109 133 break; 110 134 default: … … 128 152 OnReset(); 129 153 } 154 155 protected override void OnEncodingChanged() { 156 base.OnEncodingChanged(); 157 ParameterizeOperators(); 158 } 159 160 protected override void OnEvaluatorChanged() { 161 base.OnEvaluatorChanged(); 162 ParameterizeOperators(); 163 } 164 165 private void InitializeOperators() { 166 Operators.Add(new TSPImprovementOperator()); 167 Operators.Add(new TSPMultipleGuidesPathRelinker()); 168 Operators.Add(new TSPPathRelinker()); 169 Operators.Add(new TSPSimultaneousPathRelinker()); 170 171 Operators.Add(new TSPAlleleFrequencyAnalyzer()); 172 foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>()) 173 Operators.Add(op); 174 175 ParameterizeOperators(); 176 } 177 178 private void ParameterizeOperators() { 179 foreach (var op in Operators.OfType<TSPAlleleFrequencyAnalyzer>()) { 180 op.MaximizationParameter.ActualName = MaximizationParameter.Name; 181 op.TSPDataParameter.ActualName = TSPDataParameter.Name; 182 op.SolutionParameter.ActualName = Encoding.Name; 183 op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 184 op.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 185 op.ResultsParameter.ActualName = "Results"; 186 } 187 foreach (var op in Operators.OfType<ITSPMoveEvaluator>()) { 188 op.TSPDataParameter.ActualName = TSPDataParameter.Name; 189 op.TSPDataParameter.Hidden = true; 190 op.TourLengthParameter.ActualName = Evaluator.QualityParameter.ActualName; 191 op.TourLengthParameter.Hidden = true; 192 op.TSPTourParameter.ActualName = Encoding.Name; 193 op.TSPTourParameter.Hidden = true; 194 } 195 foreach (var op in Operators.OfType<ISingleObjectiveImprovementOperator>()) { 196 op.SolutionParameter.ActualName = Encoding.Name; 197 op.SolutionParameter.Hidden = true; 198 } 199 foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) { 200 op.ParentsParameter.ActualName = Encoding.Name; 201 op.ParentsParameter.Hidden = true; 202 } 203 foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>()) { 204 op.SolutionVariableName = Encoding.Name; 205 op.QualityVariableName = Evaluator.QualityParameter.ActualName; 206 } 207 } 130 208 } 131 209 } -
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/TSPData.cs
r17239 r17241 1 1 using System; 2 using System.ComponentModel; 3 using System.Drawing; 2 4 using HEAL.Attic; 3 5 using HeuristicLab.Common; 4 6 using HeuristicLab.Core; 5 7 using HeuristicLab.Data; 8 using HeuristicLab.Encodings.PermutationEncoding; 6 9 7 10 namespace HeuristicLab.Problems.TravelingSalesman { … … 9 12 public interface ITSPData : IItem { 10 13 double GetDistance(int fromCity, int toCity); 11 } 12 13 [Item("Coordinates-based TSP Data", "TSP that is represented by coordinates of cities.")] 14 [StorableType("3955d07a-d43c-4a01-9505-d2effb1ea865")] 15 public abstract class CoordinatesTSPData : Item, ITSPData { 16 [Storable] 17 public DoubleMatrix Coordinates { get; set; } 18 19 [StorableConstructor] 20 protected CoordinatesTSPData(StorableConstructorFlag _) : base(_) { } 21 protected CoordinatesTSPData(CoordinatesTSPData original, Cloner cloner) : base(original, cloner) { 22 Coordinates = cloner.Clone(original.Coordinates); 23 } 24 protected CoordinatesTSPData() : base() { 25 Coordinates = new DoubleMatrix(); 26 } 27 protected CoordinatesTSPData(double[,] coordinates) : base() { 28 if (coordinates == null) throw new ArgumentNullException(nameof(coordinates)); 29 if (coordinates.GetLength(1) != 2) throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 30 Coordinates = new DoubleMatrix(coordinates); 31 } 32 33 public double GetDistance(int fromCity, int toCity) { 34 return GetDistance(Coordinates[fromCity, 0], Coordinates[fromCity, 1], 35 Coordinates[toCity, 0], Coordinates[toCity, 1]); 36 } 37 38 public abstract double GetDistance(double fromX, double fromY, double toX, double toY); 39 } 40 41 [Item("Euclidean (rounded) TSP Data", "TSP that is represented by coordinates and a rounded euclidean distance measure.")] 42 [StorableType("4bf58348-cd98-46c5-a4c0-55f486ca88b4")] 43 public sealed class RoundedEuclideanCoordinatesTSPData : CoordinatesTSPData { 44 45 [StorableConstructor] 46 private RoundedEuclideanCoordinatesTSPData(StorableConstructorFlag _) : base(_) { } 47 private RoundedEuclideanCoordinatesTSPData(RoundedEuclideanCoordinatesTSPData original, Cloner cloner) : base(original, cloner) { } 48 public RoundedEuclideanCoordinatesTSPData() : base() { } 49 public RoundedEuclideanCoordinatesTSPData(double[,] coordinates) : base(coordinates) { } 50 51 public override IDeepCloneable Clone(Cloner cloner) { 52 return new RoundedEuclideanCoordinatesTSPData(this, cloner); 53 } 54 55 public override double GetDistance(double fromX, double fromY, double toX, double toY) { 56 return Math.Round(Math.Sqrt((fromX - toX) * (fromX - toX) + (fromY - toY) * (fromY - toY))); 57 } 14 ITSPSolution GetSolution(Permutation tspTour, double tourLength); 15 } 16 17 [StorableType("f08a63d9-0b83-4944-9251-42925baeb872")] 18 public interface ITSPSolution : IItem { 19 DoubleMatrix Coordinates { get; } 20 Permutation Tour { get; } 21 DoubleValue TourLength { get; } 58 22 } 59 23 … … 84 48 if (DisplayCoordinates != null && DisplayCoordinates.Rows != Matrix.GetLength(0)) 85 49 throw new ArgumentException("Unequal number of rows in " + nameof(matrix) + " and " + nameof(coordinates) + "."); 50 } 51 52 public ITSPSolution GetSolution(Permutation tour, double tourLength) { 53 return new PathTSPTour(DisplayCoordinates, tour, new DoubleValue(tourLength)); 86 54 } 87 55 … … 111 79 public double GetDistance(int fromCity, int toCity) => Matrix[fromCity, toCity]; 112 80 } 81 82 [Item("Coordinates-based TSP Data", "TSP that is represented by coordinates of locations.")] 83 [StorableType("3955d07a-d43c-4a01-9505-d2effb1ea865")] 84 public abstract class CoordinatesTSPData : Item, ITSPData { 85 [Storable] 86 public DoubleMatrix Coordinates { get; set; } 87 88 [StorableConstructor] 89 protected CoordinatesTSPData(StorableConstructorFlag _) : base(_) { } 90 protected CoordinatesTSPData(CoordinatesTSPData original, Cloner cloner) : base(original, cloner) { 91 Coordinates = cloner.Clone(original.Coordinates); 92 } 93 protected CoordinatesTSPData() : base() { 94 Coordinates = new DoubleMatrix(); 95 } 96 protected CoordinatesTSPData(double[,] coordinates) : base() { 97 if (coordinates == null) throw new ArgumentNullException(nameof(coordinates)); 98 if (coordinates.GetLength(1) != 2) throw new ArgumentException("Argument must have exactly two columns.", nameof(coordinates)); 99 Coordinates = new DoubleMatrix(coordinates); 100 } 101 102 public double GetDistance(int fromCity, int toCity) { 103 return GetDistance(Coordinates[fromCity, 0], Coordinates[fromCity, 1], 104 Coordinates[toCity, 0], Coordinates[toCity, 1]); 105 } 106 107 public abstract double GetDistance(double fromX, double fromY, double toX, double toY); 108 109 public ITSPSolution GetSolution(Permutation tour, double tourLength) { 110 return new PathTSPTour(Coordinates, tour, new DoubleValue(tourLength)); 111 } 112 } 113 114 [Item("Euclidean TSP Data", "TSP that is represented by coordinates in an Euclidean plane.")] 115 [StorableType("4bf58348-cd98-46c5-a4c0-55f486ca88b4")] 116 public sealed class EuclideanTSPData : CoordinatesTSPData { 117 public enum RoundingMode { None, Midpoint, Ceiling } 118 119 [Storable] 120 public RoundingMode Rounding { get; set; } 121 122 [StorableConstructor] 123 private EuclideanTSPData(StorableConstructorFlag _) : base(_) { } 124 private EuclideanTSPData(EuclideanTSPData original, Cloner cloner) : base(original, cloner) { 125 Rounding = original.Rounding; 126 } 127 public EuclideanTSPData() : base() { } 128 public EuclideanTSPData(double[,] coordinates) : base(coordinates) { } 129 130 public override IDeepCloneable Clone(Cloner cloner) { 131 return new EuclideanTSPData(this, cloner); 132 } 133 134 public override double GetDistance(double fromX, double fromY, double toX, double toY) { 135 var dist = Math.Sqrt((fromX - toX) * (fromX - toX) + (fromY - toY) * (fromY - toY)); 136 switch (Rounding) { 137 case RoundingMode.None: return dist; 138 case RoundingMode.Midpoint: return Math.Round(dist); 139 case RoundingMode.Ceiling: return Math.Ceiling(dist); 140 default: throw new InvalidOperationException("Unknown rounding mode " + Rounding); 141 } 142 } 143 } 144 145 [Item("Geo TSP Data", "TSP that is represented by geo coordinates.")] 146 [StorableType("4bf58348-cd98-46c5-a4c0-55f486ca88b4")] 147 public sealed class GeoTSPData : CoordinatesTSPData { 148 public const double PI = 3.141592; 149 public const double RADIUS = 6378.388; 150 151 [StorableConstructor] 152 private GeoTSPData(StorableConstructorFlag _) : base(_) { } 153 private GeoTSPData(GeoTSPData original, Cloner cloner) : base(original, cloner) { } 154 public GeoTSPData() : base() { } 155 public GeoTSPData(double[,] coordinates) : base(coordinates) { } 156 157 public override IDeepCloneable Clone(Cloner cloner) { 158 return new GeoTSPData(this, cloner); 159 } 160 161 public override double GetDistance(double fromX, double fromY, double toX, double toY) { 162 double latitude1, longitude1, latitude2, longitude2; 163 double q1, q2, q3; 164 double length; 165 166 latitude1 = ConvertToRadian(fromX); 167 longitude1 = ConvertToRadian(fromY); 168 latitude2 = ConvertToRadian(toX); 169 longitude2 = ConvertToRadian(toY); 170 171 q1 = Math.Cos(longitude1 - longitude2); 172 q2 = Math.Cos(latitude1 - latitude2); 173 q3 = Math.Cos(latitude1 + latitude2); 174 175 length = (int)(RADIUS * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0); 176 return (length); 177 } 178 179 private double ConvertToRadian(double x) { 180 return PI * (Math.Truncate(x) + 5.0 * (x - Math.Truncate(x)) / 3.0) / 180.0; 181 } 182 } 183 /// <summary> 184 /// Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI. 185 /// </summary> 186 [Item("PathTSPTour", "Represents a tour of a Traveling Salesman Problem given in path representation which can be visualized in the GUI.")] 187 [StorableType("2CAE7C49-751B-4802-9025-62E2268E47AE")] 188 public sealed class PathTSPTour : Item, ITSPSolution, INotifyPropertyChanged { 189 public static new Image StaticItemImage { 190 get { return HeuristicLab.Common.Resources.VSImageLibrary.Image; } 191 } 192 193 [Storable] 194 private DoubleMatrix coordinates; 195 public DoubleMatrix Coordinates { 196 get { return coordinates; } 197 set { 198 if (coordinates == value) return; 199 coordinates = value; 200 OnPropertyChanged(nameof(Coordinates)); 201 } 202 } 203 204 [Storable(Name = "tour", OldName = "permutation")] 205 private Permutation tour; 206 public Permutation Tour { 207 get { return tour; } 208 set { 209 if (tour == value) return; 210 tour = value; 211 OnPropertyChanged(nameof(Tour)); 212 } 213 } 214 [Storable(Name = "tourLength", OldName = "quality")] 215 private DoubleValue tourLength; 216 public DoubleValue TourLength { 217 get { return tourLength; } 218 set { 219 if (tourLength == value) return; 220 tourLength = value; 221 OnPropertyChanged(nameof(TourLength)); 222 } 223 } 224 225 [StorableConstructor] 226 private PathTSPTour(StorableConstructorFlag _) : base(_) { } 227 private PathTSPTour(PathTSPTour original, Cloner cloner) 228 : base(original, cloner) { 229 this.coordinates = cloner.Clone(original.coordinates); 230 this.tour = cloner.Clone(original.tour); 231 this.tourLength = cloner.Clone(original.tourLength); 232 } 233 public PathTSPTour() : base() { } 234 public PathTSPTour(DoubleMatrix coordinates) 235 : base() { 236 this.coordinates = coordinates; 237 } 238 public PathTSPTour(DoubleMatrix coordinates, Permutation permutation) 239 : base() { 240 this.coordinates = coordinates; 241 this.tour = permutation; 242 } 243 public PathTSPTour(DoubleMatrix coordinates, Permutation permutation, DoubleValue quality) 244 : base() { 245 this.coordinates = coordinates; 246 this.tour = permutation; 247 this.tourLength = quality; 248 } 249 250 public override IDeepCloneable Clone(Cloner cloner) { 251 return new PathTSPTour(this, cloner); 252 } 253 254 public event PropertyChangedEventHandler PropertyChanged; 255 private void OnPropertyChanged(string property) { 256 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property)); 257 } 258 } 113 259 }
Note: See TracChangeset
for help on using the changeset viewer.