Changeset 7248
- Timestamp:
- 12/30/11 04:03:52 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.Benchmarks/3.3/BenchmarkAlgorithm.cs
r7246 r7248 40 40 [StorableClass] 41 41 public sealed class BenchmarkAlgorithm : IAlgorithm { 42 private Random random = new Random();43 42 private CancellationTokenSource cancellationTokenSource; 43 44 public string ItemName { 45 get { return ItemAttribute.GetName(this.GetType()); } 46 } 47 public string ItemDescription { 48 get { return ItemAttribute.GetDescription(this.GetType()); } 49 } 50 public Version ItemVersion { 51 get { return ItemAttribute.GetVersion(this.GetType()); } 52 } 53 public static Image StaticItemImage { 54 get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; } 55 } 56 public Image ItemImage { 57 get { 58 if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared; 59 else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted; 60 else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused; 61 else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped; 62 else return ItemAttribute.GetImage(this.GetType()); 63 } 64 } 44 65 45 66 [Storable] … … 115 136 116 137 public Type ProblemType { 117 get { return typeof(IProblem); } 118 } 119 120 [Storable] 121 private IProblem problem; 138 get { 139 // BenchmarkAlgorithm does not have a problem, so return a type which is no problem for sure 140 return typeof(BenchmarkAlgorithm); 141 } 142 } 143 122 144 public IProblem Problem { 123 get { return problem; } 124 set { 125 if (problem != value) { 126 if ((value != null) && !ProblemType.IsInstanceOfType(value)) throw new ArgumentException("Invalid problem type."); 127 if (problem != null) DeregisterProblemEvents(); 128 problem = value; 129 if (problem != null) RegisterProblemEvents(); 130 OnProblemChanged(); 131 Prepare(); 132 } 133 } 145 get { return null; } 146 set { throw new NotImplementedException("BenchmarkAlgorithm does not have a problem."); } 134 147 } 135 148 … … 150 163 } 151 164 } 152 153 165 public bool CanChangeName { 154 get { return false; }166 get { return true; } 155 167 } 156 168 … … 167 179 } 168 180 } 169 170 181 public bool CanChangeDescription { 171 get { return false; } 172 } 173 174 public string ItemName { 175 get { return ItemAttribute.GetName(this.GetType()); } 176 } 177 178 public string ItemDescription { 179 get { return ItemAttribute.GetDescription(this.GetType()); } 180 } 181 182 public Version ItemVersion { 183 get { return ItemAttribute.GetVersion(this.GetType()); } 184 } 185 186 public static Image StaticItemImage { 187 get { return HeuristicLab.Common.Resources.VSImageLibrary.Event; } 188 } 189 public Image ItemImage { 190 get { 191 if (ExecutionState == ExecutionState.Prepared) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePrepared; 192 else if (ExecutionState == ExecutionState.Started) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStarted; 193 else if (ExecutionState == ExecutionState.Paused) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutablePaused; 194 else if (ExecutionState == ExecutionState.Stopped) return HeuristicLab.Common.Resources.VSImageLibrary.ExecutableStopped; 195 else return ItemAttribute.GetImage(this.GetType()); 196 } 182 get { return true; } 197 183 } 198 184 199 185 [Storable] 200 186 private ParameterCollection parameters = new ParameterCollection(); 201 202 187 public IKeyedItemCollection<string, IParameter> Parameters { 203 188 get { return parameters; } 204 189 } 205 206 190 private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters; 207 208 191 IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters { 209 192 get { … … 218 201 219 202 #region Parameter Properties 220 221 203 public ConstrainedValueParameter<IBenchmark> BenchmarkParameter { 222 204 get { return (ConstrainedValueParameter<IBenchmark>)Parameters["Benchmark"]; } 223 205 } 224 225 206 private ValueParameter<IntValue> ChunkSizeParameter { 226 207 get { return (ValueParameter<IntValue>)Parameters["ChunkSize"]; } 227 208 } 228 229 209 private ValueParameter<DoubleValue> TimeLimitParameter { 230 210 get { return (ValueParameter<DoubleValue>)Parameters["TimeLimit"]; } 231 211 } 232 233 212 #endregion 234 213 235 214 #region Constructors 236 237 215 [StorableConstructor] 238 public BenchmarkAlgorithm(bool deserializing) { } 239 216 private BenchmarkAlgorithm(bool deserializing) { } 217 private BenchmarkAlgorithm(BenchmarkAlgorithm original, Cloner cloner) { 218 if (original.ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState)); 219 cloner.RegisterClonedObject(original, this); 220 name = original.name; 221 description = original.description; 222 parameters = cloner.Clone(original.parameters); 223 readOnlyParameters = null; 224 executionState = original.executionState; 225 executionTime = original.executionTime; 226 storeAlgorithmInEachRun = original.storeAlgorithmInEachRun; 227 runsCounter = original.runsCounter; 228 Runs = cloner.Clone(original.runs); 229 results = cloner.Clone(original.results); 230 } 240 231 public BenchmarkAlgorithm() { 241 232 name = ItemName; … … 253 244 Prepare(); 254 245 } 255 256 public BenchmarkAlgorithm(BenchmarkAlgorithm original, Cloner cloner) {257 cloner.RegisterClonedObject(original, this);258 name = original.name;259 description = original.description;260 parameters = cloner.Clone(original.parameters);261 readOnlyParameters = null;262 if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));263 executionState = original.executionState;264 executionTime = original.executionTime;265 storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;266 runsCounter = original.runsCounter;267 runs = cloner.Clone(original.runs);268 Initialize();269 270 results = cloner.Clone(original.results);271 DiscoverBenchmarks();272 Prepare();273 }274 275 246 #endregion 276 247 277 248 private void CreateParameters() { 278 Parameters.Add(new ValueParameter<IntValue>("ChunkSize", "The size (MB) of the chunk array that gets generated", new IntValue(0))); 279 Parameters.Add(new ValueParameter<DoubleValue>("TimeLimit", "The time limit (in minutes) for a benchmark run. Zero means a fixed number of iterations", new DoubleValue(0))); 280 } 281 249 Parameters.Add(new ValueParameter<IntValue>("ChunkSize", "The size in MB of the chunk data array that is generated.", new IntValue(0))); 250 Parameters.Add(new ValueParameter<DoubleValue>("TimeLimit", "The time limit in minutes for a benchmark run (zero means a fixed number of iterations).", new DoubleValue(0))); 251 } 282 252 private void DiscoverBenchmarks() { 283 253 var benchmarks = from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark)) … … 291 261 if (!Parameters.ContainsKey(paramName)) { 292 262 if (values.Count > 0) { 293 Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, values, values.First(a => a is IBenchmark)));263 Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, "The benchmark which should be executed.", values, values.First(a => a is IBenchmark))); 294 264 } else { 295 Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, values)); 296 } 297 } 298 } 299 300 private void Initialize() { 301 if (problem != null) RegisterProblemEvents(); 302 if (runs != null) RegisterRunsEvents(); 265 Parameters.Add(new ConstrainedValueParameter<IBenchmark>(paramName, "The benchmark which should be executed.", values)); 266 } 267 } 268 } 269 270 public IDeepCloneable Clone(Cloner cloner) { 271 return new BenchmarkAlgorithm(this, cloner); 272 } 273 public object Clone() { 274 return Clone(new Cloner()); 275 } 276 277 public override string ToString() { 278 return Name; 303 279 } 304 280 … … 309 285 OnPrepared(); 310 286 } 311 312 287 public void Prepare(bool clearRuns) { 313 288 if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped)) … … 316 291 Prepare(); 317 292 } 318 319 293 public void Pause() { 320 294 if (ExecutionState != ExecutionState.Started) … … 326 300 cancellationTokenSource.Cancel(); 327 301 } 328 329 302 public void Start() { 330 303 cancellationTokenSource = new CancellationTokenSource(); … … 362 335 int chunkSize = ((IntValue)ChunkSizeParameter.ActualValue).Value; 363 336 if (chunkSize > 0) { 364 Benchmark.ChunkData = Create DataChuck(chunkSize);337 Benchmark.ChunkData = CreateChunkData(chunkSize); 365 338 } else if (chunkSize < 0) { 366 339 throw new ArgumentException("ChunkSize must not be negativ."); … … 395 368 CollectResultsRecursively("", Results, values); 396 369 } 397 398 370 private void CollectResultsRecursively(string path, ResultCollection results, IDictionary<string, IItem> values) { 399 371 foreach (IResult result in results) { … … 405 377 } 406 378 } 407 408 379 public void CollectParameterValues(IDictionary<string, IItem> values) { 409 380 foreach (IValueParameter param in parameters.OfType<IValueParameter>()) { … … 418 389 } 419 390 420 private byte[][] Create DataChuck(int megaBytes) {391 private byte[][] CreateChunkData(int megaBytes) { 421 392 if (megaBytes <= 0) { 422 393 throw new ArgumentException("MegaBytes must be greater than zero", "megaBytes"); 423 394 } 395 Random random = new Random(); 424 396 byte[][] chunk = new byte[megaBytes][]; 425 397 for (int i = 0; i < chunk.Length; i++) { … … 431 403 432 404 #region Events 433 434 405 public event EventHandler ExecutionStateChanged; 435 406 private void OnExecutionStateChanged() { … … 442 413 if (handler != null) handler(this, EventArgs.Empty); 443 414 } 444 public event EventHandler ProblemChanged; 445 private void OnProblemChanged() { 446 EventHandler handler = ProblemChanged; 447 if (handler != null) handler(this, EventArgs.Empty); 448 } 415 public event EventHandler ProblemChanged { add { } remove { } } 449 416 public event EventHandler StoreAlgorithmInEachRunChanged; 450 417 private void OnStoreAlgorithmInEachRunChanged() { … … 521 488 } 522 489 523 private void DeregisterProblemEvents() {524 problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);525 problem.Reset -= new EventHandler(Problem_Reset);526 }527 private void RegisterProblemEvents() {528 problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);529 problem.Reset += new EventHandler(Problem_Reset);530 }531 private void Problem_OperatorsChanged(object sender, EventArgs e) { }532 private void Problem_Reset(object sender, EventArgs e) {533 Prepare();534 }535 536 490 private void DeregisterRunsEvents() { 537 491 runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset); … … 543 497 runsCounter = runs.Count; 544 498 } 545 546 #endregion547 548 #region Clone549 550 public IDeepCloneable Clone(Cloner cloner) {551 return new BenchmarkAlgorithm(this, cloner);552 }553 554 public object Clone() {555 return Clone(new Cloner());556 }557 558 499 #endregion 559 500 }
Note: See TracChangeset
for help on using the changeset viewer.