Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/08/10 04:28:19 (14 years ago)
Author:
swagner
Message:

Implemented reviewers' comments (#947)

Location:
trunk/sources/HeuristicLab.Optimization/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithm.cs

    r3694 r3716  
    2323using System.Collections.Generic;
    2424using System.Drawing;
     25using HeuristicLab.Collections;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    9798    public RunCollection Runs {
    9899      get { return runs; }
     100      protected set {
     101        if (value == null) throw new ArgumentNullException();
     102        if (runs != value) {
     103          if (runs != null) DeregisterRunsEvents();
     104          runs = value;
     105          if (runs != null) RegisterRunsEvents();
     106        }
     107      }
    99108    }
    100109
     
    104113      executionTime = TimeSpan.Zero;
    105114      runsCounter = 0;
    106       runs = new RunCollection();
     115      Runs = new RunCollection();
    107116    }
    108117    protected Algorithm(string name)
     
    111120      executionTime = TimeSpan.Zero;
    112121      runsCounter = 0;
    113       runs = new RunCollection();
     122      Runs = new RunCollection();
    114123    }
    115124    protected Algorithm(string name, ParameterCollection parameters)
     
    118127      executionTime = TimeSpan.Zero;
    119128      runsCounter = 0;
    120       runs = new RunCollection();
     129      Runs = new RunCollection();
    121130    }
    122131    protected Algorithm(string name, string description)
     
    125134      executionTime = TimeSpan.Zero;
    126135      runsCounter = 0;
    127       runs = new RunCollection();
     136      Runs = new RunCollection();
    128137    }
    129138    protected Algorithm(string name, string description, ParameterCollection parameters)
     
    132141      executionTime = TimeSpan.Zero;
    133142      runsCounter = 0;
    134       runs = new RunCollection();
     143      Runs = new RunCollection();
    135144    }
    136145    [StorableConstructor]
     
    140149    private void Initialize() {
    141150      if (problem != null) RegisterProblemEvents();
     151      if (runs != null) RegisterRunsEvents();
    142152    }
    143153
     
    176186      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    177187        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    178       if (clearRuns) {
    179         runsCounter = 0;
    180         runs.Clear();
    181       }
     188      if (clearRuns) runs.Clear();
    182189      Prepare();
    183190    }
     
    264271      problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
    265272    }
    266 
    267273    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
    268274    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
    269275    protected virtual void Problem_OperatorsChanged(object sender, EventArgs e) { }
     276
     277    protected virtual void DeregisterRunsEvents() {
     278      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     279    }
     280    protected virtual void RegisterRunsEvents() {
     281      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     282    }
     283    protected virtual void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     284      runsCounter = runs.Count;
     285    }
    270286    #endregion
    271287  }
  • trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs

    r3372 r3716  
    2222using System;
    2323using System.Drawing;
     24using HeuristicLab.Collections;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
     27using HeuristicLab.Data;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using HeuristicLab.Collections;
    2829
    2930namespace HeuristicLab.Optimization {
     
    8384          if (algorithm != null) RegisterAlgorithmEvents();
    8485          OnAlgorithmChanged();
    85           Prepare(true);
     86          Prepare();
    8687        }
    8788      }
     
    9697          repetitions = value;
    9798          OnRepetitionsChanged();
    98           if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
     99          if ((Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
    99100            Prepare();
    100101        }
    101102      }
    102103    }
     104    [Storable]
     105    private int repetitionsCounter;
    103106
    104107    [Storable]
     
    106109    public RunCollection Runs {
    107110      get { return runs; }
     111      private set {
     112        if (value == null) throw new ArgumentNullException();
     113        if (runs != value) {
     114          if (runs != null) DeregisterRunsEvents();
     115          runs = value;
     116          if (runs != null) RegisterRunsEvents();
     117        }
     118      }
    108119    }
    109120
     
    117128      executionTime = TimeSpan.Zero;
    118129      repetitions = 10;
    119       runs = new RunCollection();
     130      repetitionsCounter = 0;
     131      Runs = new RunCollection();
    120132      stopPending = false;
    121133    }
     
    126138      executionTime = TimeSpan.Zero;
    127139      repetitions = 10;
    128       runs = new RunCollection();
     140      repetitionsCounter = 0;
     141      Runs = new RunCollection();
    129142      stopPending = false;
    130143    }
     
    134147      executionTime = TimeSpan.Zero;
    135148      repetitions = 10;
    136       runs = new RunCollection();
     149      repetitionsCounter = 0;
     150      Runs = new RunCollection();
    137151      stopPending = false;
    138152    }
     
    146160    private void Initialize() {
    147161      if (algorithm != null) RegisterAlgorithmEvents();
     162      if (runs != null) RegisterRunsEvents();
    148163    }
    149164
     
    155170      clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
    156171      clone.repetitions = repetitions;
     172      clone.repetitionsCounter = repetitionsCounter;
    157173      clone.runs = (RunCollection)cloner.Clone(runs);
    158174      clone.stopPending = stopPending;
     
    168184        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    169185      if (Algorithm != null) {
    170         if (clearRuns) {
    171           ExecutionTime = TimeSpan.Zero;
    172           runs.Clear();
    173         }
     186        repetitionsCounter = 0;
     187        if (clearRuns) runs.Clear();
    174188        Algorithm.Prepare(clearRuns);
    175189      }
     
    183197      if (ExecutionState != ExecutionState.Started)
    184198        throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
    185       if (Algorithm != null) Algorithm.Pause();
     199      if ((Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Started))
     200        Algorithm.Pause();
    186201    }
    187202    public void Stop() {
     
    189204        throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
    190205      stopPending = true;
    191       if (Algorithm != null) Algorithm.Stop();
     206      if ((Algorithm != null) &&
     207          ((Algorithm.ExecutionState == ExecutionState.Started) || (Algorithm.ExecutionState == ExecutionState.Paused)))
     208        Algorithm.Stop();
    192209    }
    193210
     
    285302    private void Algorithm_Stopped(object sender, EventArgs e) {
    286303      ExecutionTime += Algorithm.ExecutionTime;
    287 
    288       if (!stopPending && (runs.Count < repetitions)) {
     304      repetitionsCounter++;
     305
     306      if (!stopPending && (repetitionsCounter < repetitions)) {
    289307        Algorithm.Prepare();
    290308        Algorithm.Start();
     
    303321      Runs.RemoveRange(e.Items);
    304322    }
     323
     324    private void RegisterRunsEvents() {
     325      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     326      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
     327    }
     328    private void DeregisterRunsEvents() {
     329      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     330      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
     331    }
     332    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     333      foreach (IRun run in e.OldItems) {
     334        IItem item;
     335        run.Results.TryGetValue("Execution Time", out item);
     336        TimeSpanValue executionTime = item as TimeSpanValue;
     337        if (executionTime != null) ExecutionTime -= executionTime.Value;
     338      }
     339      if (Algorithm != null) Algorithm.Runs.RemoveRange(e.OldItems);
     340      foreach (IRun run in e.Items) {
     341        IItem item;
     342        run.Results.TryGetValue("Execution Time", out item);
     343        TimeSpanValue executionTime = item as TimeSpanValue;
     344        if (executionTime != null) ExecutionTime += executionTime.Value;
     345      }
     346    }
     347    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     348      foreach (IRun run in e.Items) {
     349        IItem item;
     350        run.Results.TryGetValue("Execution Time", out item);
     351        TimeSpanValue executionTime = item as TimeSpanValue;
     352        if (executionTime != null) ExecutionTime -= executionTime.Value;
     353      }
     354      if (Algorithm != null) Algorithm.Runs.RemoveRange(e.Items);
     355    }
    305356    #endregion
    306357  }
  • trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs

    r3372 r3716  
    7979    public RunCollection Runs {
    8080      get { return runs; }
     81      private set {
     82        if (value == null) throw new ArgumentNullException();
     83        if (runs != value) {
     84          if (runs != null) DeregisterRunsEvents();
     85          runs = value;
     86          if (runs != null) RegisterRunsEvents();
     87        }
     88      }
    8189    }
    8290
     
    9098      executionTime = TimeSpan.Zero;
    9199      optimizers = new OptimizerList();
    92       runs = new RunCollection();
     100      Runs = new RunCollection();
    93101      stopPending = false;
    94102      Initialize();
     
    100108      executionTime = TimeSpan.Zero;
    101109      optimizers = new OptimizerList();
    102       runs = new RunCollection();
     110      Runs = new RunCollection();
    103111      stopPending = false;
    104112      Initialize();
     
    109117      executionTime = TimeSpan.Zero;
    110118      optimizers = new OptimizerList();
    111       runs = new RunCollection();
     119      Runs = new RunCollection();
    112120      stopPending = false;
    113121      Initialize();
     
    124132      foreach (IOptimizer optimizer in optimizers)
    125133        RegisterOptimizerEvents(optimizer);
     134      if (runs != null) RegisterRunsEvents();
    126135    }
    127136
     
    236245      foreach (IndexedItem<IOptimizer> item in e.Items) {
    237246        RegisterOptimizerEvents(item.Value);
     247        item.Value.Prepare();
    238248      }
    239249    }
     
    241251      foreach (IndexedItem<IOptimizer> item in e.Items) {
    242252        RegisterOptimizerEvents(item.Value);
     253        item.Value.Prepare();
    243254      }
    244255    }
     
    254265      foreach (IndexedItem<IOptimizer> item in e.Items) {
    255266        RegisterOptimizerEvents(item.Value);
     267        item.Value.Prepare();
    256268      }
    257269    }
     
    317329      Runs.RemoveRange(e.Items);
    318330    }
     331
     332    private void RegisterRunsEvents() {
     333      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     334      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
     335    }
     336    private void DeregisterRunsEvents() {
     337      runs.CollectionReset -= new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
     338      runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<IRun>(Runs_ItemsRemoved);
     339    }
     340    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     341      foreach (IOptimizer optimizer in Optimizers)
     342        optimizer.Runs.RemoveRange(e.OldItems);
     343    }
     344    private void Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
     345      foreach (IOptimizer optimizer in Optimizers)
     346        optimizer.Runs.RemoveRange(e.Items);
     347    }
    319348    #endregion
    320349  }
  • trunk/sources/HeuristicLab.Optimization/3.3/RunCollection.cs

    r3632 r3716  
    3030
    3131namespace HeuristicLab.Optimization {
     32  [Item("Run Collection", "Represents a collection of runs.")]
     33  [Creatable("Testing & Analysis")]
    3234  [StorableClass]
    33   [Item("RunCollection", "Represents a collection of runs.")]
    3435  public class RunCollection : ItemCollection<IRun>, IStringConvertibleMatrix {
    3536    public RunCollection() : base() { Initialize(); }
Note: See TracChangeset for help on using the changeset viewer.