Changeset 2975 for trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs
- Timestamp:
- 03/09/10 05:45:39 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.TSP/3.3/TSP.cs
r2917 r2975 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Drawing; 24 25 using System.Linq; 26 using HeuristicLab.Common; 25 27 using HeuristicLab.Core; 26 28 using HeuristicLab.Data; … … 34 36 [Item("TSP", "Represents a symmetric Traveling Salesman Problem.")] 35 37 [Creatable("Problems")] 36 [EmptyStorableClass]37 38 public sealed class TSP : ParameterizedNamedItem, ISingleObjectiveProblem { 38 39 public override Image ItemImage { … … 40 41 } 41 42 42 private IValueParameter<BoolData> MaximizationParameter { 43 get { return (IValueParameter<BoolData>)Parameters["Maximization"]; } 44 } 45 public IValueParameter<DoubleMatrixData> CoordinatesParameter { 46 get { return (IValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; } 47 } 48 public IValueParameter<DoubleData> BestKnownQualityParameter { 49 get { return (IValueParameter<DoubleData>)Parameters["BestKnownQuality"]; } 50 } 51 public IValueParameter<IPermutationCreator> SolutionCreatorParameter { 52 get { return (IValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; } 53 } 54 public IValueParameter<ITSPEvaluator> EvaluatorParameter { 55 get { return (IValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; } 56 } 57 58 public BoolData Maximization { 59 get { return MaximizationParameter.Value; } 60 } 61 public ISolutionCreator SolutionCreator { 43 #region Parameter Properties 44 public ValueParameter<BoolData> MaximizationParameter { 45 get { return (ValueParameter<BoolData>)Parameters["Maximization"]; } 46 } 47 IParameter ISingleObjectiveProblem.MaximizationParameter { 48 get { return MaximizationParameter; } 49 } 50 public ValueParameter<DoubleMatrixData> CoordinatesParameter { 51 get { return (ValueParameter<DoubleMatrixData>)Parameters["Coordinates"]; } 52 } 53 public ValueParameter<IPermutationCreator> SolutionCreatorParameter { 54 get { return (ValueParameter<IPermutationCreator>)Parameters["SolutionCreator"]; } 55 } 56 IParameter IProblem.SolutionCreatorParameter { 57 get { return SolutionCreatorParameter; } 58 } 59 public ValueParameter<ITSPEvaluator> EvaluatorParameter { 60 get { return (ValueParameter<ITSPEvaluator>)Parameters["Evaluator"]; } 61 } 62 IParameter IProblem.EvaluatorParameter { 63 get { return EvaluatorParameter; } 64 } 65 public OptionalValueParameter<DoubleData> BestKnownQualityParameter { 66 get { return (OptionalValueParameter<DoubleData>)Parameters["BestKnownQuality"]; } 67 } 68 #endregion 69 70 #region Properties 71 public DoubleMatrixData Coordinates { 72 get { return CoordinatesParameter.Value; } 73 set { CoordinatesParameter.Value = value; } 74 } 75 public IPermutationCreator SolutionCreator { 62 76 get { return SolutionCreatorParameter.Value; } 63 } 64 public ISingleObjectiveEvaluator Evaluator { 77 set { SolutionCreatorParameter.Value = value; } 78 } 79 ISolutionCreator IProblem.SolutionCreator { 80 get { return SolutionCreatorParameter.Value; } 81 } 82 public ITSPEvaluator Evaluator { 83 get { return EvaluatorParameter.Value; } 84 set { EvaluatorParameter.Value = value; } 85 } 86 ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator { 65 87 get { return EvaluatorParameter.Value; } 66 88 } … … 68 90 get { return EvaluatorParameter.Value; } 69 91 } 92 public DoubleData BestKnownQuality { 93 get { return BestKnownQualityParameter.Value; } 94 set { BestKnownQualityParameter.Value = value; } 95 } 70 96 private OperatorSet operators; 71 public OperatorSet Operators { 72 get { return operators; } 73 } 97 public IEnumerable<IOperator> Operators { 98 get { 99 if (operators == null) InitializeOperators(); 100 return operators; 101 } 102 } 103 #endregion 104 105 #region Persistence Properties 106 [Storable] 107 private object RestoreEvents { 108 get { return null; } 109 set { RegisterEvents(); } 110 } 111 #endregion 74 112 75 113 public TSP() … … 85 123 86 124 creator.PermutationParameter.ActualName = "TSPTour"; 87 creator.LengthParameter.Value = new IntData(0);88 evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;89 evaluator.PermutationParameter.ActualName = creator.PermutationParameter.ActualName;90 125 evaluator.QualityParameter.ActualName = "TSPTourLength"; 91 92 MaximizationParameter.ValueChanged += new EventHandler(MaximizationParameter_ValueChanged); 93 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 94 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 95 96 operators = new OperatorSet(); 97 if (ApplicationManager.Manager != null) { 98 var ops = ApplicationManager.Manager.GetInstances<IPermutationOperator>(); 99 foreach (IPermutationCrossover op in ops.OfType<IPermutationCrossover>()) { 100 op.ParentsParameter.ActualName = creator.PermutationParameter.ActualName; 101 op.ChildParameter.ActualName = creator.PermutationParameter.ActualName; 102 } 103 foreach (IPermutationManipulator op in ops.OfType<IPermutationManipulator>()) { 104 op.PermutationParameter.ActualName = creator.PermutationParameter.ActualName; 105 } 106 foreach (IPermutationOperator op in ops) 107 operators.Add(op); 108 } 126 ParameterizeSolutionCreator(); 127 ParameterizeEvaluator(); 128 129 RegisterEvents(); 130 } 131 132 public override IDeepCloneable Clone(Cloner cloner) { 133 TSP clone = (TSP)base.Clone(cloner); 134 clone.RegisterEvents(); 135 return clone; 109 136 } 110 137 … … 112 139 TSPLIBParser parser = new TSPLIBParser(filename); 113 140 parser.Parse(); 114 CoordinatesParameter.Value = new DoubleMatrixData(parser.Vertices); 115 int cities = CoordinatesParameter.Value.Rows; 116 SolutionCreatorParameter.Value.LengthParameter.Value = new IntData(cities); 117 } 118 119 private void MaximizationParameter_ValueChanged(object sender, EventArgs e) { 120 OnMaximizationChanged(); 121 } 122 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 123 OnSolutionCreatorChanged(); 124 } 125 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 126 OnEvaluatorChanged(); 127 } 128 129 public event EventHandler MaximizationChanged; 130 private void OnMaximizationChanged() { 131 if (MaximizationChanged != null) 132 MaximizationChanged(this, EventArgs.Empty); 133 } 141 Coordinates = new DoubleMatrixData(parser.Vertices); 142 } 143 144 #region Events 134 145 public event EventHandler SolutionCreatorChanged; 135 146 private void OnSolutionCreatorChanged() { … … 142 153 EvaluatorChanged(this, EventArgs.Empty); 143 154 } 155 public event EventHandler OperatorsChanged; 156 private void OnOperatorsChanged() { 157 if (OperatorsChanged != null) 158 OperatorsChanged(this, EventArgs.Empty); 159 } 160 161 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) { 162 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 163 Coordinates.Reset += new EventHandler(Coordinates_Reset); 164 ParameterizeSolutionCreator(); 165 } 166 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { 167 } 168 private void Coordinates_Reset(object sender, EventArgs e) { 169 ParameterizeSolutionCreator(); 170 } 171 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 172 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 173 ParameterizeSolutionCreator(); 174 ParameterizeEvaluator(); 175 ParameterizeOperators(); 176 OnSolutionCreatorChanged(); 177 } 178 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) { 179 ParameterizeEvaluator(); 180 ParameterizeOperators(); 181 } 182 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 183 ParameterizeEvaluator(); 184 OnEvaluatorChanged(); 185 } 186 #endregion 187 188 #region Helpers 189 private void RegisterEvents() { 190 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 191 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 192 Coordinates.Reset += new EventHandler(Coordinates_Reset); 193 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 194 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 195 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 196 } 197 private void ParameterizeSolutionCreator() { 198 SolutionCreator.LengthParameter.Value = new IntData(Coordinates.Rows); 199 } 200 private void ParameterizeEvaluator() { 201 if (Evaluator is ITSPPathEvaluator) 202 ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 203 if (Evaluator is ITSPCoordinatesPathEvaluator) 204 ((ITSPCoordinatesPathEvaluator)Evaluator).CoordinatesParameter.ActualName = CoordinatesParameter.Name; 205 } 206 private void InitializeOperators() { 207 operators = new OperatorSet(); 208 if (ApplicationManager.Manager != null) { 209 foreach (IPermutationOperator op in ApplicationManager.Manager.GetInstances<IPermutationOperator>()) 210 operators.Add(op); 211 ParameterizeOperators(); 212 } 213 } 214 private void ParameterizeOperators() { 215 foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) { 216 op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 217 op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 218 } 219 foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) { 220 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 221 } 222 } 223 #endregion 144 224 } 145 225 }
Note: See TracChangeset
for help on using the changeset viewer.