Changeset 17241 for branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/Improvers/TSPImprovementOperator.cs
- Timestamp:
- 09/10/19 21:55:35 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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();
Note: See TracChangeset
for help on using the changeset viewer.