Changeset 4098
- Timestamp:
- 07/25/10 01:04:14 (14 years ago)
- Location:
- trunk/sources
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs
r4068 r4098 42 42 [StorableClass] 43 43 public sealed class ArtificialAntProblem : ParameterizedNamedItem, ISingleObjectiveProblem { 44 44 45 public override Image ItemImage { 45 46 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Type; } … … 180 181 get { return BestKnownQualityParameter.Value; } 181 182 } 182 private List<IOperator> operators;183 183 public IEnumerable<IOperator> Operators { 184 184 get { return operators; } … … 190 190 #endregion 191 191 192 [Storable] 193 private List<IOperator> operators; 194 195 [StorableConstructor] 196 private ArtificialAntProblem(bool deserializing) : base() { } 192 197 public ArtificialAntProblem() 193 198 : base() { … … 212 217 ParameterizeSolutionCreator(); 213 218 ParameterizeEvaluator(); 214 Initialize(); 215 } 216 217 [StorableConstructor] 218 private ArtificialAntProblem(bool deserializing) : base() { } 219 InitializeOperators(); 220 AttachEventHandlers(); 221 } 219 222 220 223 public override IDeepCloneable Clone(Cloner cloner) { 221 224 ArtificialAntProblem clone = (ArtificialAntProblem)base.Clone(cloner); 222 clone.Initialize(); 225 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 226 clone.AttachEventHandlers(); 223 227 return clone; 224 228 } … … 274 278 #region Helpers 275 279 [StorableHook(HookType.AfterDeserialization)] 276 private void Initialize() { 277 InitializeOperators(); 280 private void AttachEventHandlers() { 281 // Start BackwardsCompatibility3.3 (remove with 3.4) 282 if (operators == null) InitializeOperators(); 283 // End BackwardsCompatibility3.3 278 284 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 279 285 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.FeatureSelection/3.3/FeatureSelectionProblem.cs
r4082 r4098 81 81 get { return EvaluatorParameter.Value; } 82 82 } 83 private List<IOperator> operators;84 83 public override IEnumerable<IOperator> Operators { 85 84 get { return operators; } … … 87 86 #endregion 88 87 88 [Storable] 89 private List<IOperator> operators; 90 91 [StorableConstructor] 92 private FeatureSelectionProblem(bool deserializing) : base() { } 89 93 public FeatureSelectionProblem() 90 94 : base() { … … 101 105 ParameterizeEvaluator(); 102 106 103 Initialize(); 104 } 105 106 [StorableConstructor] 107 private FeatureSelectionProblem(bool deserializing) : base() { } 107 InitializeOperators(); 108 AttachEventHandlers(); 109 } 108 110 109 111 [StorableHook(HookType.AfterDeserialization)] … … 115 117 public override IDeepCloneable Clone(Cloner cloner) { 116 118 FeatureSelectionProblem clone = (FeatureSelectionProblem)base.Clone(cloner); 117 clone.Initialize(); 119 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 120 clone.AttachEventHandlers(); 118 121 return clone; 119 122 } … … 165 168 166 169 #region Helpers 167 private void Initialize() { 168 InitializeOperators(); 170 private void AttachEventHandlers() { 171 // Start BackwardsCompatibility3.3 (remove with 3.4) 172 if (operators == null) InitializeOperators(); 173 // End BackwardsCompatibility3.3 169 174 RegisterParameterEvents(); 170 175 RegisterParameterValueEvents(); -
trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate/3.3/MultiVariateDataAnalysisProblem.cs
r4068 r4098 50 50 #endregion 51 51 52 [StorableConstructor] 53 private MultiVariateDataAnalysisProblem(bool deserializing) : base() { } 52 54 public MultiVariateDataAnalysisProblem() 53 55 : base() { … … 56 58 RegisterParameterValueEvents(); 57 59 } 58 59 [StorableConstructor]60 private MultiVariateDataAnalysisProblem(bool deserializing) : base() { }61 60 62 61 [StorableHook(HookType.AfterDeserialization)] -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs
r4068 r4098 147 147 get { return BestKnownQualityParameter.Value; } 148 148 } 149 private List<IOperator> operators;150 149 public override IEnumerable<IOperator> Operators { 151 150 get { return operators; } … … 180 179 #endregion 181 180 181 [Storable] 182 private List<IOperator> operators; 183 184 [StorableConstructor] 185 private SymbolicRegressionProblem(bool deserializing) : base() { } 182 186 public SymbolicRegressionProblem() 183 187 : base() { … … 208 212 UpdateGrammar(); 209 213 UpdateEstimationLimits(); 210 Initialize(); 211 } 212 213 [StorableConstructor] 214 private SymbolicRegressionProblem(bool deserializing) : base() { } 215 216 [StorableHook(HookType.AfterDeserialization)] 217 private void AfterDeserializationHook() { 218 RegisterParameterEvents(); 219 RegisterParameterValueEvents(); 214 InitializeOperators(); 215 AttachEventHandlers(); 220 216 } 221 217 222 218 public override IDeepCloneable Clone(Cloner cloner) { 223 219 SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner); 224 clone.Initialize(); 220 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 221 clone.AttachEventHandlers(); 225 222 return clone; 226 223 } … … 305 302 306 303 #region Helpers 307 private void Initialize() { 308 InitializeOperators(); 304 [StorableHook(HookType.AfterDeserialization)] 305 private void AttachEventHandlers() { 306 // Start BackwardsCompatibility3.3 (remove with 3.4) 307 if (operators == null) InitializeOperators(); 308 // End BackwardsCompatibility3.3 309 309 RegisterParameterEvents(); 310 310 RegisterParameterValueEvents(); -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/ParameterAdjustmentProblem/SupportVectorMachineParameterAdjustmentProblem.cs
r4068 r4098 107 107 set { BestKnownQualityParameter.Value = value; } 108 108 } 109 private List<IOperator> operators;110 109 public override IEnumerable<IOperator> Operators { 111 110 get { return operators; } … … 120 119 } 121 120 121 [Storable] 122 private List<IOperator> operators; 122 123 [Storable] 123 124 private StdDevStrategyVectorCreator strategyVectorCreator; … … 160 161 ParameterizeEvaluator(); 161 162 162 Initialize(); 163 InitializeOperators(); 164 AttachEventHandlers(); 163 165 UpdateStrategyVectorBounds(); 164 166 } … … 166 168 public override IDeepCloneable Clone(Cloner cloner) { 167 169 SupportVectorMachineParameterAdjustmentProblem clone = (SupportVectorMachineParameterAdjustmentProblem)base.Clone(cloner); 170 clone.operators = operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList(); 168 171 clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator); 172 clone.operators.Add(clone.strategyVectorCreator); 169 173 clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover); 174 clone.operators.Add(strategyVectorCrossover); 170 175 clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator); 171 clone.Initialize(); 176 clone.operators.Add(strategyVectorManipulator); 177 clone.AttachEventHandlers(); 172 178 return clone; 179 } 180 181 private bool IsNotFieldReferenced(IOperator x) { 182 return !(x == strategyVectorCreator 183 || x == strategyVectorCrossover 184 || x == strategyVectorManipulator); 173 185 } 174 186 … … 204 216 #region Helpers 205 217 [StorableHook(HookType.AfterDeserialization)] 206 private void Initialize() { 207 InitializeOperators(); 218 private void AttachEventHandlers() { 219 // Start BackwardsCompatibility3.3 (remove with 3.4) 220 if (operators == null) InitializeOperators(); 221 // End BackwardsCompatibility3.3 208 222 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 209 223 SolutionCreator.RealVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_RealVectorParameter_ActualNameChanged); -
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/ExternalEvaluationProblem.cs
r4068 r4098 192 192 193 193 #region Helpers 194 private void InitializeOperators() {195 ItemList<IOperator> operators = OperatorsParameter.Value;196 operators.Add(new BestScopeSolutionAnalyzer());197 ParameterizeAnalyzers();198 }199 194 [StorableHook(HookType.AfterDeserialization)] 200 195 private void AttachEventHandlers() { … … 206 201 OperatorsParameter.Value.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_ItemsRemoved); 207 202 OperatorsParameter.Value.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(OperatorsParameter_Value_CollectionReset); 203 } 204 private void InitializeOperators() { 205 ItemList<IOperator> operators = OperatorsParameter.Value; 206 operators.Add(new BestScopeSolutionAnalyzer()); 207 ParameterizeAnalyzers(); 208 208 } 209 209 private void ParameterizeAnalyzers() { -
trunk/sources/HeuristicLab.Problems.Knapsack/3.3/KnapsackProblem.cs
r3797 r4098 126 126 set { BestKnownSolutionParameter.Value = value; } 127 127 } 128 private List<IOperator> operators;129 128 public IEnumerable<IOperator> Operators { 130 129 get { return operators.Cast<IOperator>(); } … … 135 134 #endregion 136 135 137 private void InitializeRandomKnapsackInstance() { 138 System.Random rand = new System.Random(); 139 140 int itemCount = rand.Next(10, 100); 141 Weights = new IntArray(itemCount); 142 Values = new IntArray(itemCount); 143 144 double totalWeight = 0; 145 146 for (int i = 0; i < itemCount; i++ ) { 147 int value = rand.Next(1, 10); 148 int weight = rand.Next(1, 10); 149 150 Values[i] = value; 151 Weights[i] = weight; 152 totalWeight += weight; 153 } 154 155 int capacity = (int)Math.Round(0.7 * totalWeight); 156 KnapsackCapacity = new IntValue(capacity); 157 } 158 136 [Storable] 137 private List<IOperator> operators; 138 139 [StorableConstructor] 140 private KnapsackProblem(bool deserializing) : base() { } 159 141 public KnapsackProblem() 160 142 : base() { … … 179 161 ParameterizeEvaluator(); 180 162 181 Initialize(); 182 } 183 184 [StorableConstructor] 185 private KnapsackProblem(bool deserializing) : base() { } 163 InitializeOperators(); 164 AttachEventHandlers(); 165 } 186 166 187 167 public override IDeepCloneable Clone(Cloner cloner) { 188 168 KnapsackProblem clone = (KnapsackProblem)base.Clone(cloner); 189 clone.Initialize(); 169 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 170 clone.AttachEventHandlers(); 190 171 return clone; 191 172 } … … 274 255 #region Helpers 275 256 [StorableHook(HookType.AfterDeserialization)] 276 private void Initialize() { 277 InitializeOperators(); 257 private void AttachEventHandlers() { 258 // Start BackwardsCompatibility3.3 (remove with 3.4) 259 if (operators == null) InitializeOperators(); 260 // End BackwardsCompatibility3.3 278 261 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 279 262 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); … … 350 333 } 351 334 #endregion 335 336 private void InitializeRandomKnapsackInstance() { 337 System.Random rand = new System.Random(); 338 339 int itemCount = rand.Next(10, 100); 340 Weights = new IntArray(itemCount); 341 Values = new IntArray(itemCount); 342 343 double totalWeight = 0; 344 345 for (int i = 0; i < itemCount; i++) { 346 int value = rand.Next(1, 10); 347 int weight = rand.Next(1, 10); 348 349 Values[i] = value; 350 Weights[i] = weight; 351 totalWeight += weight; 352 } 353 354 int capacity = (int)Math.Round(0.7 * totalWeight); 355 KnapsackCapacity = new IntValue(capacity); 356 } 352 357 } 353 358 } -
trunk/sources/HeuristicLab.Problems.OneMax/3.3/OnemaxProblem.cs
r3797 r4098 97 97 get { return BestKnownQualityParameter.Value; } 98 98 } 99 private List<IOperator> operators;100 99 public IEnumerable<IOperator> Operators { 101 100 get { return operators.Cast<IOperator>(); } … … 106 105 #endregion 107 106 107 [Storable] 108 private List<IOperator> operators; 109 110 [StorableConstructor] 111 private OneMaxProblem(bool deserializing) : base() { } 108 112 public OneMaxProblem() 109 113 : base() { … … 122 126 ParameterizeEvaluator(); 123 127 124 Initialize(); 125 } 126 127 [StorableConstructor] 128 private OneMaxProblem(bool deserializing) : base() { } 128 InitializeOperators(); 129 AttachEventHandlers(); 130 } 129 131 130 132 public override IDeepCloneable Clone(Cloner cloner) { 131 133 OneMaxProblem clone = (OneMaxProblem)base.Clone(cloner); 132 clone.Initialize(); 134 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 135 clone.AttachEventHandlers(); 133 136 return clone; 134 137 } … … 195 198 #region Helpers 196 199 [StorableHook(HookType.AfterDeserialization)] 197 private void Initialize() { 198 InitializeOperators(); 200 private void AttachEventHandlers() { 201 // Start BackwardsCompatibility3.3 (remove with 3.4) 202 if (operators == null) InitializeOperators(); 203 // End BackwardsCompatibility3.3 199 204 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 200 205 SolutionCreator.BinaryVectorParameter.ActualNameChanged += new EventHandler(SolutionCreator_BinaryVectorParameter_ActualNameChanged); -
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Analyzers/BestSingleObjectiveTestFunctionSolutionAnalyzer.cs
r4086 r4098 90 90 [StorableHook(HookType.AfterDeserialization)] 91 91 private void CompatibilityMethod() { 92 // BackwardsCompatibility3.3 92 93 // Bounds are introduced in 3.3.0.3894 93 94 if (!Parameters.ContainsKey("Bounds")) -
trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/SingleObjectiveTestFunctionProblem.cs
r4068 r4098 119 119 set { BestKnownQualityParameter.Value = value; } 120 120 } 121 private List<IOperator> operators;122 121 public IEnumerable<IOperator> Operators { 123 122 get { return operators; } … … 127 126 } 128 127 #endregion 128 129 [Storable] 130 private List<IOperator> operators; 129 131 130 132 [StorableConstructor] … … 154 156 ParameterizeEvaluator(); 155 157 156 Initialize(); 158 InitializeOperators(); 159 AttachEventHandlers(); 157 160 UpdateStrategyVectorBounds(); 158 161 } … … 160 163 public override IDeepCloneable Clone(Cloner cloner) { 161 164 SingleObjectiveTestFunctionProblem clone = (SingleObjectiveTestFunctionProblem)base.Clone(cloner); 165 clone.operators = operators.Where(x => IsNotFieldReferenced(x)).Select(x => (IOperator)cloner.Clone(x)).ToList(); 162 166 clone.strategyVectorCreator = (StdDevStrategyVectorCreator)cloner.Clone(strategyVectorCreator); 167 clone.operators.Add(clone.strategyVectorCreator); 163 168 clone.strategyVectorCrossover = (StdDevStrategyVectorCrossover)cloner.Clone(strategyVectorCrossover); 169 clone.operators.Add(strategyVectorCrossover); 164 170 clone.strategyVectorManipulator = (StdDevStrategyVectorManipulator)cloner.Clone(strategyVectorManipulator); 165 clone.Initialize(); 171 clone.operators.Add(strategyVectorManipulator); 172 clone.AttachEventHandlers(); 166 173 return clone; 174 } 175 176 private bool IsNotFieldReferenced(IOperator x) { 177 return !(x == strategyVectorCreator 178 || x == strategyVectorCrossover 179 || x == strategyVectorManipulator); 167 180 } 168 181 … … 284 297 #region Helpers 285 298 [StorableHook(HookType.AfterDeserialization)] 286 private void Initialize() { 287 InitializeOperators(); 299 private void AttachEventHandlers() { 300 // Start BackwardsCompatibility3.3 (remove with 3.4) 301 if (operators == null) InitializeOperators(); 302 // End BackwardsCompatibility3.3 288 303 ProblemSizeParameter.ValueChanged += new EventHandler(ProblemSizeParameter_ValueChanged); 289 304 ProblemSize.ValueChanged += new EventHandler(ProblemSize_ValueChanged); -
trunk/sources/HeuristicLab.Problems.TravelingSalesman/3.3/TravelingSalesmanProblem.cs
r4047 r4098 120 120 set { BestKnownSolutionParameter.Value = value; } 121 121 } 122 private List<IOperator> operators;123 122 public IEnumerable<IOperator> Operators { 124 123 get { return operators; } … … 129 128 #endregion 130 129 130 [Storable] 131 private List<IOperator> operators; 132 133 [StorableConstructor] 134 private TravelingSalesmanProblem(bool deserializing) : base() { } 131 135 public TravelingSalesmanProblem() 132 136 : base() { … … 155 159 ParameterizeEvaluator(); 156 160 157 Initialize(); 158 } 159 [StorableConstructor] 160 private TravelingSalesmanProblem(bool deserializing) : base() { } 161 InitializeOperators(); 162 AttachEventHandlers(); 163 } 161 164 162 165 public override IDeepCloneable Clone(Cloner cloner) { 163 166 TravelingSalesmanProblem clone = (TravelingSalesmanProblem)base.Clone(cloner); 167 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 164 168 clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value; 165 clone. Initialize();169 clone.AttachEventHandlers(); 166 170 return clone; 167 171 } 172 173 #region Events 174 public event EventHandler SolutionCreatorChanged; 175 private void OnSolutionCreatorChanged() { 176 EventHandler handler = SolutionCreatorChanged; 177 if (handler != null) handler(this, EventArgs.Empty); 178 } 179 public event EventHandler EvaluatorChanged; 180 private void OnEvaluatorChanged() { 181 EventHandler handler = EvaluatorChanged; 182 if (handler != null) handler(this, EventArgs.Empty); 183 } 184 public event EventHandler OperatorsChanged; 185 private void OnOperatorsChanged() { 186 EventHandler handler = OperatorsChanged; 187 if (handler != null) handler(this, EventArgs.Empty); 188 } 189 public event EventHandler Reset; 190 private void OnReset() { 191 EventHandler handler = Reset; 192 if (handler != null) handler(this, EventArgs.Empty); 193 } 194 195 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) { 196 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 197 Coordinates.Reset += new EventHandler(Coordinates_Reset); 198 ParameterizeSolutionCreator(); 199 ClearDistanceMatrix(); 200 } 201 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) { 202 ClearDistanceMatrix(); 203 } 204 private void Coordinates_Reset(object sender, EventArgs e) { 205 ParameterizeSolutionCreator(); 206 ClearDistanceMatrix(); 207 } 208 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) { 209 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 210 ParameterizeSolutionCreator(); 211 ParameterizeEvaluator(); 212 ParameterizeAnalyzer(); 213 ParameterizeOperators(); 214 OnSolutionCreatorChanged(); 215 } 216 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) { 217 ParameterizeEvaluator(); 218 ParameterizeAnalyzer(); 219 ParameterizeOperators(); 220 } 221 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) { 222 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 223 ParameterizeEvaluator(); 224 UpdateMoveEvaluators(); 225 ParameterizeAnalyzer(); 226 ClearDistanceMatrix(); 227 OnEvaluatorChanged(); 228 } 229 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) { 230 ParameterizeAnalyzer(); 231 } 232 private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) { 233 string name = ((ILookupParameter<InversionMove>)sender).ActualName; 234 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) { 235 op.InversionMoveParameter.ActualName = name; 236 } 237 } 238 private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) { 239 string name = ((ILookupParameter<TranslocationMove>)sender).ActualName; 240 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) { 241 op.TranslocationMoveParameter.ActualName = name; 242 } 243 } 244 #endregion 245 246 #region Helpers 247 [StorableHook(HookType.AfterDeserialization)] 248 private void AttachEventHandlers() { 249 // Start BackwardsCompatibility3.3 (remove with 3.4) 250 if (operators == null) InitializeOperators(); 251 // End BackwardsCompatibility3.3 252 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 253 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged); 254 Coordinates.Reset += new EventHandler(Coordinates_Reset); 255 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 256 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged); 257 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged); 258 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 259 } 260 261 private void InitializeOperators() { 262 operators = new List<IOperator>(); 263 operators.Add(new BestTSPSolutionAnalyzer()); 264 ParameterizeAnalyzer(); 265 operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>()); 266 ParameterizeOperators(); 267 UpdateMoveEvaluators(); 268 InitializeMoveGenerators(); 269 } 270 private void InitializeMoveGenerators() { 271 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) { 272 if (op is IMoveGenerator) { 273 op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged); 274 } 275 } 276 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) { 277 if (op is IMoveGenerator) { 278 op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged); 279 } 280 } 281 } 282 private void UpdateMoveEvaluators() { 283 operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator); 284 foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>()) 285 if (op.EvaluatorType == Evaluator.GetType()) { 286 operators.Add(op); 287 } 288 ParameterizeOperators(); 289 OnOperatorsChanged(); 290 } 291 private void ParameterizeSolutionCreator() { 292 SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows); 293 SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected); 294 } 295 private void ParameterizeEvaluator() { 296 if (Evaluator is ITSPPathEvaluator) 297 ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 298 if (Evaluator is ITSPCoordinatesPathEvaluator) { 299 ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator; 300 evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 301 evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 302 evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; 303 } 304 } 305 private void ParameterizeAnalyzer() { 306 BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 307 BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 308 BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 309 BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results"; 310 BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 311 BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name; 312 BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name; 313 } 314 private void ParameterizeOperators() { 315 foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) { 316 op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 317 op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 318 } 319 foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) { 320 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 321 } 322 foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) { 323 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 324 } 325 foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) { 326 op.CoordinatesParameter.ActualName = CoordinatesParameter.Name; 327 op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name; 328 op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name; 329 op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName; 330 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName; 331 } 332 string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName; 333 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) 334 op.InversionMoveParameter.ActualName = inversionMove; 335 string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName; 336 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) 337 op.TranslocationMoveParameter.ActualName = translocationMove; 338 } 339 340 private void ClearDistanceMatrix() { 341 DistanceMatrixParameter.Value = null; 342 } 343 #endregion 168 344 169 345 public void ImportFromTSPLIB(string tspFileName, string optimalTourFileName) { … … 197 373 BestKnownQuality = new DoubleValue(bestKnownQuality); 198 374 } 199 200 #region Events201 public event EventHandler SolutionCreatorChanged;202 private void OnSolutionCreatorChanged() {203 EventHandler handler = SolutionCreatorChanged;204 if (handler != null) handler(this, EventArgs.Empty);205 }206 public event EventHandler EvaluatorChanged;207 private void OnEvaluatorChanged() {208 EventHandler handler = EvaluatorChanged;209 if (handler != null) handler(this, EventArgs.Empty);210 }211 public event EventHandler OperatorsChanged;212 private void OnOperatorsChanged() {213 EventHandler handler = OperatorsChanged;214 if (handler != null) handler(this, EventArgs.Empty);215 }216 public event EventHandler Reset;217 private void OnReset() {218 EventHandler handler = Reset;219 if (handler != null) handler(this, EventArgs.Empty);220 }221 222 private void CoordinatesParameter_ValueChanged(object sender, EventArgs e) {223 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);224 Coordinates.Reset += new EventHandler(Coordinates_Reset);225 ParameterizeSolutionCreator();226 ClearDistanceMatrix();227 }228 private void Coordinates_ItemChanged(object sender, EventArgs<int, int> e) {229 ClearDistanceMatrix();230 }231 private void Coordinates_Reset(object sender, EventArgs e) {232 ParameterizeSolutionCreator();233 ClearDistanceMatrix();234 }235 private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {236 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);237 ParameterizeSolutionCreator();238 ParameterizeEvaluator();239 ParameterizeAnalyzer();240 ParameterizeOperators();241 OnSolutionCreatorChanged();242 }243 private void SolutionCreator_PermutationParameter_ActualNameChanged(object sender, EventArgs e) {244 ParameterizeEvaluator();245 ParameterizeAnalyzer();246 ParameterizeOperators();247 }248 private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {249 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);250 ParameterizeEvaluator();251 UpdateMoveEvaluators();252 ParameterizeAnalyzer();253 ClearDistanceMatrix();254 OnEvaluatorChanged();255 }256 private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {257 ParameterizeAnalyzer();258 }259 private void MoveGenerator_InversionMoveParameter_ActualNameChanged(object sender, EventArgs e) {260 string name = ((ILookupParameter<InversionMove>)sender).ActualName;261 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {262 op.InversionMoveParameter.ActualName = name;263 }264 }265 private void MoveGenerator_TranslocationMoveParameter_ActualNameChanged(object sender, EventArgs e) {266 string name = ((ILookupParameter<TranslocationMove>)sender).ActualName;267 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {268 op.TranslocationMoveParameter.ActualName = name;269 }270 }271 #endregion272 273 #region Helpers274 [StorableHook(HookType.AfterDeserialization)]275 private void Initialize() {276 InitializeOperators();277 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged);278 Coordinates.ItemChanged += new EventHandler<EventArgs<int, int>>(Coordinates_ItemChanged);279 Coordinates.Reset += new EventHandler(Coordinates_Reset);280 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);281 SolutionCreator.PermutationParameter.ActualNameChanged += new EventHandler(SolutionCreator_PermutationParameter_ActualNameChanged);282 EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);283 Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);284 }285 286 private void InitializeOperators() {287 operators = new List<IOperator>();288 operators.Add(new BestTSPSolutionAnalyzer());289 ParameterizeAnalyzer();290 operators.AddRange(ApplicationManager.Manager.GetInstances<IPermutationOperator>().Cast<IOperator>());291 ParameterizeOperators();292 UpdateMoveEvaluators();293 InitializeMoveGenerators();294 }295 private void InitializeMoveGenerators() {296 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>()) {297 if (op is IMoveGenerator) {298 op.InversionMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_InversionMoveParameter_ActualNameChanged);299 }300 }301 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>()) {302 if (op is IMoveGenerator) {303 op.TranslocationMoveParameter.ActualNameChanged += new EventHandler(MoveGenerator_TranslocationMoveParameter_ActualNameChanged);304 }305 }306 }307 private void UpdateMoveEvaluators() {308 operators.RemoveAll(x => x is ISingleObjectiveMoveEvaluator);309 foreach (ITSPPathMoveEvaluator op in ApplicationManager.Manager.GetInstances<ITSPPathMoveEvaluator>())310 if (op.EvaluatorType == Evaluator.GetType()) {311 operators.Add(op);312 }313 ParameterizeOperators();314 OnOperatorsChanged();315 }316 private void ParameterizeSolutionCreator() {317 SolutionCreator.LengthParameter.Value = new IntValue(Coordinates.Rows);318 SolutionCreator.PermutationTypeParameter.Value = new PermutationType(PermutationTypes.RelativeUndirected);319 }320 private void ParameterizeEvaluator() {321 if (Evaluator is ITSPPathEvaluator)322 ((ITSPPathEvaluator)Evaluator).PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;323 if (Evaluator is ITSPCoordinatesPathEvaluator) {324 ITSPCoordinatesPathEvaluator evaluator = (ITSPCoordinatesPathEvaluator)Evaluator;325 evaluator.CoordinatesParameter.ActualName = CoordinatesParameter.Name;326 evaluator.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;327 evaluator.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;328 }329 }330 private void ParameterizeAnalyzer() {331 BestTSPSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;332 BestTSPSolutionAnalyzer.CoordinatesParameter.ActualName = CoordinatesParameter.Name;333 BestTSPSolutionAnalyzer.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;334 BestTSPSolutionAnalyzer.ResultsParameter.ActualName = "Results";335 BestTSPSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;336 BestTSPSolutionAnalyzer.BestKnownSolutionParameter.ActualName = BestKnownSolutionParameter.Name;337 BestTSPSolutionAnalyzer.MaximizationParameter.ActualName = MaximizationParameter.Name;338 }339 private void ParameterizeOperators() {340 foreach (IPermutationCrossover op in Operators.OfType<IPermutationCrossover>()) {341 op.ParentsParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;342 op.ChildParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;343 }344 foreach (IPermutationManipulator op in Operators.OfType<IPermutationManipulator>()) {345 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;346 }347 foreach (IPermutationMoveOperator op in Operators.OfType<IPermutationMoveOperator>()) {348 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;349 }350 foreach (ITSPPathMoveEvaluator op in Operators.OfType<ITSPPathMoveEvaluator>()) {351 op.CoordinatesParameter.ActualName = CoordinatesParameter.Name;352 op.DistanceMatrixParameter.ActualName = DistanceMatrixParameter.Name;353 op.UseDistanceMatrixParameter.ActualName = UseDistanceMatrixParameter.Name;354 op.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;355 op.PermutationParameter.ActualName = SolutionCreator.PermutationParameter.ActualName;356 }357 string inversionMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationInversionMoveOperator>().First().InversionMoveParameter.ActualName;358 foreach (IPermutationInversionMoveOperator op in Operators.OfType<IPermutationInversionMoveOperator>())359 op.InversionMoveParameter.ActualName = inversionMove;360 string translocationMove = Operators.OfType<IMoveGenerator>().OfType<IPermutationTranslocationMoveOperator>().First().TranslocationMoveParameter.ActualName;361 foreach (IPermutationTranslocationMoveOperator op in Operators.OfType<IPermutationTranslocationMoveOperator>())362 op.TranslocationMoveParameter.ActualName = translocationMove;363 }364 365 private void ClearDistanceMatrix() {366 DistanceMatrixParameter.Value = null;367 }368 #endregion369 375 } 370 376 } -
trunk/sources/HeuristicLab.Problems.VehicleRouting/3.3/VehicleRoutingProblem.cs
r4068 r4098 170 170 get { return EvaluatorParameter.Value; } 171 171 } 172 private List<IOperator> operators;173 172 public IEnumerable<IOperator> Operators { 174 173 get { return operators; } … … 179 178 #endregion 180 179 180 [Storable] 181 private List<IOperator> operators; 182 183 [StorableConstructor] 184 private VehicleRoutingProblem(bool deserializing) : base() { } 181 185 public VehicleRoutingProblem() 182 186 : base() { … … 209 213 ParameterizeEvaluator(); 210 214 211 Initialize(); 212 } 213 [StorableConstructor] 214 private VehicleRoutingProblem(bool deserializing) : base() { } 215 InitializeOperators(); 216 AttachEventHandlers(); 217 } 215 218 216 219 public override IDeepCloneable Clone(Cloner cloner) { 217 220 VehicleRoutingProblem clone = (VehicleRoutingProblem)base.Clone(cloner); 221 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 218 222 clone.DistanceMatrixParameter.Value = DistanceMatrixParameter.Value; 219 clone. Initialize();223 clone.AttachEventHandlers(); 220 224 return clone; 221 }222 223 private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) {224 double distance = 0.0;225 226 distance =227 Math.Sqrt(228 Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) +229 Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2));230 231 return distance;232 }233 234 private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) {235 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows);236 237 for (int i = 0; i < distanceMatrix.Rows; i++) {238 for (int j = i; j < distanceMatrix.Columns; j++) {239 double distance = CalculateDistance(i, j, coordinates);240 241 distanceMatrix[i, j] = distance;242 distanceMatrix[j, i] = distance;243 }244 }245 246 return distanceMatrix;247 }248 249 public static double GetDistance(int start, int end,250 DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) {251 double distance = 0.0;252 253 if (useDistanceMatrix.Value) {254 if (distanceMatrix.ActualValue == null) {255 distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates);256 }257 258 distance = distanceMatrix.ActualValue[start, end];259 } else {260 distance = CalculateDistance(start, end, coordinates);261 }262 263 return distance;264 }265 266 public static double GetDistance(int start, int end,267 DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) {268 double distance = 0.0;269 270 if (useDistanceMatrix.Value) {271 distance = distanceMatrix[start, end];272 } else {273 distance = CalculateDistance(start, end, coordinates);274 }275 276 return distance;277 }278 279 public void ImportFromSolomon(string solomonFileName) {280 SolomonParser parser = new SolomonParser(solomonFileName);281 parser.Parse();282 283 this.Name = parser.ProblemName;284 285 Coordinates = new DoubleMatrix(parser.Coordinates);286 Vehicles.Value = parser.Vehicles;287 Capacity.Value = parser.Capacity;288 Demand = new DoubleArray(parser.Demands);289 ReadyTime = new DoubleArray(parser.Readytimes);290 DueTime = new DoubleArray(parser.Duetimes);291 ServiceTime = new DoubleArray(parser.Servicetimes);292 293 OnReset();294 225 } 295 226 … … 364 295 #region Helpers 365 296 [StorableHook(HookType.AfterDeserialization)] 366 private void Initialize() { 367 InitializeOperators(); 297 private void AttachEventHandlers() { 298 // Start BackwardsCompatibility3.3 (remove with 3.4) 299 if (operators == null) InitializeOperators(); 300 // End BackwardsCompatibility3.3 368 301 CoordinatesParameter.ValueChanged += new EventHandler(CoordinatesParameter_ValueChanged); 369 302 Vehicles.ValueChanged += new EventHandler(VehiclesValue_ValueChanged); … … 471 404 } 472 405 #endregion 406 407 private static double CalculateDistance(int start, int end, DoubleMatrix coordinates) { 408 double distance = 0.0; 409 410 distance = 411 Math.Sqrt( 412 Math.Pow(coordinates[start, 0] - coordinates[end, 0], 2) + 413 Math.Pow(coordinates[start, 1] - coordinates[end, 1], 2)); 414 415 return distance; 416 } 417 418 private static DoubleMatrix CreateDistanceMatrix(DoubleMatrix coordinates) { 419 DoubleMatrix distanceMatrix = new DoubleMatrix(coordinates.Rows, coordinates.Rows); 420 421 for (int i = 0; i < distanceMatrix.Rows; i++) { 422 for (int j = i; j < distanceMatrix.Columns; j++) { 423 double distance = CalculateDistance(i, j, coordinates); 424 425 distanceMatrix[i, j] = distance; 426 distanceMatrix[j, i] = distance; 427 } 428 } 429 430 return distanceMatrix; 431 } 432 433 public static double GetDistance(int start, int end, 434 DoubleMatrix coordinates, ILookupParameter<DoubleMatrix> distanceMatrix, BoolValue useDistanceMatrix) { 435 double distance = 0.0; 436 437 if (useDistanceMatrix.Value) { 438 if (distanceMatrix.ActualValue == null) { 439 distanceMatrix.ActualValue = CreateDistanceMatrix(coordinates); 440 } 441 442 distance = distanceMatrix.ActualValue[start, end]; 443 } else { 444 distance = CalculateDistance(start, end, coordinates); 445 } 446 447 return distance; 448 } 449 450 public static double GetDistance(int start, int end, 451 DoubleMatrix coordinates, DoubleMatrix distanceMatrix, BoolValue useDistanceMatrix) { 452 double distance = 0.0; 453 454 if (useDistanceMatrix.Value) { 455 distance = distanceMatrix[start, end]; 456 } else { 457 distance = CalculateDistance(start, end, coordinates); 458 } 459 460 return distance; 461 } 462 463 public void ImportFromSolomon(string solomonFileName) { 464 SolomonParser parser = new SolomonParser(solomonFileName); 465 parser.Parse(); 466 467 this.Name = parser.ProblemName; 468 469 Coordinates = new DoubleMatrix(parser.Coordinates); 470 Vehicles.Value = parser.Vehicles; 471 Capacity.Value = parser.Capacity; 472 Demand = new DoubleArray(parser.Demands); 473 ReadyTime = new DoubleArray(parser.Readytimes); 474 DueTime = new DoubleArray(parser.Duetimes); 475 ServiceTime = new DoubleArray(parser.Servicetimes); 476 477 OnReset(); 478 } 473 479 } 474 480 }
Note: See TracChangeset
for help on using the changeset viewer.