Changeset 7248 for trunk/sources


Ignore:
Timestamp:
12/30/11 04:03:52 (10 years ago)
Author:
swagner
Message:

Changes due to review of benchmark algorithms (#1659):

  • removed unnecessary problem-specific parts in BenchmarkAlgorithm
  • enabled name and description changes in BenchmarkAlgorithm
  • adapted parameter descriptions
  • implemented ToString in BenchmarkAlgorithm
  • renamed CreateDataChuck to CreateChunkData
  • adapted code formatting
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.Benchmarks/3.3/BenchmarkAlgorithm.cs

    r7246 r7248  
    4040  [StorableClass]
    4141  public sealed class BenchmarkAlgorithm : IAlgorithm {
    42     private Random random = new Random();
    4342    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    }
    4465
    4566    [Storable]
     
    115136
    116137    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
    122144    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."); }
    134147    }
    135148
     
    150163      }
    151164    }
    152 
    153165    public bool CanChangeName {
    154       get { return false; }
     166      get { return true; }
    155167    }
    156168
     
    167179      }
    168180    }
    169 
    170181    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; }
    197183    }
    198184
    199185    [Storable]
    200186    private ParameterCollection parameters = new ParameterCollection();
    201 
    202187    public IKeyedItemCollection<string, IParameter> Parameters {
    203188      get { return parameters; }
    204189    }
    205 
    206190    private ReadOnlyKeyedItemCollection<string, IParameter> readOnlyParameters;
    207 
    208191    IKeyedItemCollection<string, IParameter> IParameterizedItem.Parameters {
    209192      get {
     
    218201
    219202    #region Parameter Properties
    220 
    221203    public ConstrainedValueParameter<IBenchmark> BenchmarkParameter {
    222204      get { return (ConstrainedValueParameter<IBenchmark>)Parameters["Benchmark"]; }
    223205    }
    224 
    225206    private ValueParameter<IntValue> ChunkSizeParameter {
    226207      get { return (ValueParameter<IntValue>)Parameters["ChunkSize"]; }
    227208    }
    228 
    229209    private ValueParameter<DoubleValue> TimeLimitParameter {
    230210      get { return (ValueParameter<DoubleValue>)Parameters["TimeLimit"]; }
    231211    }
    232 
    233212    #endregion
    234213
    235214    #region Constructors
    236 
    237215    [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    }
    240231    public BenchmarkAlgorithm() {
    241232      name = ItemName;
     
    253244      Prepare();
    254245    }
    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 
    275246    #endregion
    276247
    277248    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    }
    282252    private void DiscoverBenchmarks() {
    283253      var benchmarks = from t in ApplicationManager.Manager.GetTypes(typeof(IBenchmark))
     
    291261      if (!Parameters.ContainsKey(paramName)) {
    292262        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)));
    294264        } 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;
    303279    }
    304280
     
    309285      OnPrepared();
    310286    }
    311 
    312287    public void Prepare(bool clearRuns) {
    313288      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
     
    316291      Prepare();
    317292    }
    318 
    319293    public void Pause() {
    320294      if (ExecutionState != ExecutionState.Started)
     
    326300      cancellationTokenSource.Cancel();
    327301    }
    328 
    329302    public void Start() {
    330303      cancellationTokenSource = new CancellationTokenSource();
     
    362335        int chunkSize = ((IntValue)ChunkSizeParameter.ActualValue).Value;
    363336        if (chunkSize > 0) {
    364           Benchmark.ChunkData = CreateDataChuck(chunkSize);
     337          Benchmark.ChunkData = CreateChunkData(chunkSize);
    365338        } else if (chunkSize < 0) {
    366339          throw new ArgumentException("ChunkSize must not be negativ.");
     
    395368      CollectResultsRecursively("", Results, values);
    396369    }
    397 
    398370    private void CollectResultsRecursively(string path, ResultCollection results, IDictionary<string, IItem> values) {
    399371      foreach (IResult result in results) {
     
    405377      }
    406378    }
    407 
    408379    public void CollectParameterValues(IDictionary<string, IItem> values) {
    409380      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
     
    418389    }
    419390
    420     private byte[][] CreateDataChuck(int megaBytes) {
     391    private byte[][] CreateChunkData(int megaBytes) {
    421392      if (megaBytes <= 0) {
    422393        throw new ArgumentException("MegaBytes must be greater than zero", "megaBytes");
    423394      }
     395      Random random = new Random();
    424396      byte[][] chunk = new byte[megaBytes][];
    425397      for (int i = 0; i < chunk.Length; i++) {
     
    431403
    432404    #region Events
    433 
    434405    public event EventHandler ExecutionStateChanged;
    435406    private void OnExecutionStateChanged() {
     
    442413      if (handler != null) handler(this, EventArgs.Empty);
    443414    }
    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 { } }
    449416    public event EventHandler StoreAlgorithmInEachRunChanged;
    450417    private void OnStoreAlgorithmInEachRunChanged() {
     
    521488    }
    522489
    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 
    536490    private void DeregisterRunsEvents() {
    537491      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     
    543497      runsCounter = runs.Count;
    544498    }
    545 
    546     #endregion
    547 
    548     #region Clone
    549 
    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 
    558499    #endregion
    559500  }
Note: See TracChangeset for help on using the changeset viewer.