Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/06/10 03:37:29 (15 years ago)
Author:
swagner
Message:

Continued work on algorithm batch processing (#947).

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

Legend:

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

    r3274 r3275  
    9090    public abstract ResultCollection Results { get; }
    9191
     92    [Storable]
     93    private RunCollection runs;
     94    public RunCollection Runs {
     95      get { return runs; }
     96    }
     97
    9298    protected Algorithm()
    9399      : base() {
    94100      executionState = ExecutionState.Stopped;
    95101      executionTime = TimeSpan.Zero;
     102      runs = new RunCollection();
    96103    }
    97104    protected Algorithm(string name)
     
    99106      executionState = ExecutionState.Stopped;
    100107      executionTime = TimeSpan.Zero;
     108      runs = new RunCollection();
    101109    }
    102110    protected Algorithm(string name, ParameterCollection parameters)
     
    104112      executionState = ExecutionState.Stopped;
    105113      executionTime = TimeSpan.Zero;
     114      runs = new RunCollection();
    106115    }
    107116    protected Algorithm(string name, string description)
     
    109118      executionState = ExecutionState.Stopped;
    110119      executionTime = TimeSpan.Zero;
     120      runs = new RunCollection();
    111121    }
    112122    protected Algorithm(string name, string description, ParameterCollection parameters)
     
    114124      executionState = ExecutionState.Stopped;
    115125      executionTime = TimeSpan.Zero;
     126      runs = new RunCollection();
    116127    }
    117128
     
    121132      clone.executionTime = executionTime;
    122133      clone.Problem = (IProblem)cloner.Clone(problem);
     134      clone.runs = (RunCollection)cloner.Clone(runs);
    123135      return clone;
    124136    }
    125137
    126     public void Prepare() {
    127       Prepare(true);
    128     }
    129     public virtual void Prepare(bool clearResults) {
     138    public virtual void Prepare() {
    130139      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    131140        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    132       ExecutionTime = TimeSpan.Zero;
     141    }
     142    public void Prepare(bool clearRuns) {
     143      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
     144        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
     145      if (clearRuns) runs.Clear();
     146      Prepare();
    133147    }
    134148    public virtual void Start() {
     
    172186    public event EventHandler Prepared;
    173187    protected virtual void OnPrepared() {
     188      ExecutionTime = TimeSpan.Zero;
    174189      ExecutionState = ExecutionState.Prepared;
    175190      EventHandler handler = Prepared;
     
    190205    public event EventHandler Stopped;
    191206    protected virtual void OnStopped() {
     207      Run run = new Run("Run (" + ExecutionTime.ToString() + ")");
     208      CollectParameterValues(run.Parameters);
     209      CollectResultValues(run.Results);
     210      runs.Add(run);
    192211      ExecutionState = ExecutionState.Stopped;
    193212      EventHandler handler = Stopped;
  • TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs

    r3274 r3275  
    2525using HeuristicLab.Core;
    2626using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Collections;
    2728
    2829namespace HeuristicLab.Optimization {
     
    8384          if (algorithm != null) RegisterAlgorithmEvents();
    8485          OnAlgorithmChanged();
    85           Prepare();
     86          Prepare(true);
    8687        }
    8788      }
     
    9798          OnRepetitionsChanged();
    9899          if ((runs.Count < repetitions) && (Algorithm != null) && (Algorithm.ExecutionState == ExecutionState.Stopped))
    99             Prepare(false);
     100            Prepare();
    100101        }
    101102      }
     
    145146
    146147    public void Prepare() {
    147       Prepare(true);
    148     }
    149     public void Prepare(bool clearResults) {
     148      Prepare(false);
     149    }
     150    public void Prepare(bool clearRuns) {
    150151      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    151152        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    152153      if (Algorithm != null) {
    153         if (clearResults) {
     154        if (clearRuns) {
    154155          ExecutionTime = TimeSpan.Zero;
    155156          runs.Clear();
    156157        }
    157         Algorithm.Prepare();
     158        Algorithm.Prepare(clearRuns);
    158159      }
    159160    }
     
    233234      algorithm.Started += new EventHandler(Algorithm_Started);
    234235      algorithm.Stopped += new EventHandler(Algorithm_Stopped);
     236      algorithm.Runs.CollectionReset += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset);
     237      algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded);
     238      algorithm.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved);
    235239    }
    236240    private void DeregisterAlgorithmEvents() {
     
    241245      algorithm.Started -= new EventHandler(Algorithm_Started);
    242246      algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
    243     }
    244 
     247      algorithm.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_CollectionReset);
     248      algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsAdded);
     249      algorithm.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<Run>(Algorithm_Runs_ItemsRemoved);
     250    }
    245251    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    246252      OnExceptionOccurred(e.Value);
     
    261267    private void Algorithm_Stopped(object sender, EventArgs e) {
    262268      ExecutionTime += Algorithm.ExecutionTime;
    263       runs.Add(new Run("Run " + Algorithm.ExecutionTime.ToString(), Algorithm));
    264269      OnStopped();
    265270
     
    269274      }
    270275    }
     276    private void Algorithm_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<Run> e) {
     277      Runs.RemoveRange(e.OldItems);
     278      Runs.AddRange(e.Items);
     279    }
     280    private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<Run> e) {
     281      Runs.AddRange(e.Items);
     282    }
     283    private void Algorithm_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
     284      Runs.RemoveRange(e.Items);
     285    }
    271286    #endregion
    272287  }
  • TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/EngineAlgorithm.cs

    r3274 r3275  
    157157    }
    158158
    159     public override void Prepare(bool clearResults) {
    160       base.Prepare(clearResults);
    161 
    162       ResultCollection results = Results;
     159    public override void Prepare() {
     160      base.Prepare();
    163161      globalScope.Clear();
    164       if (clearResults)
    165         globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
    166       else
    167         globalScope.Variables.Add(new Variable("Results", results));
     162      globalScope.Variables.Add(new Variable("Results", new ResultCollection()));
    168163
    169164      if (engine != null) {
  • TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs

    r3274 r3275  
    7777    }
    7878
     79    [Storable]
     80    private RunCollection runs;
     81    public RunCollection Runs {
     82      get { return runs; }
     83    }
     84
    7985    private bool stopPending;
    8086
     
    8490      executionTime = TimeSpan.Zero;
    8591      Optimizers = new OptimizerList();
     92      runs = new RunCollection();
    8693      stopPending = false;
    8794    }
     
    9097      executionTime = TimeSpan.Zero;
    9198      Optimizers = new OptimizerList();
     99      runs = new RunCollection();
    92100      stopPending = false;
    93101    }
     
    96104      executionTime = TimeSpan.Zero;
    97105      Optimizers = new OptimizerList();
     106      runs = new RunCollection();
    98107      stopPending = false;
    99108    }
     
    104113      clone.executionTime = executionTime;
    105114      clone.Optimizers = (OptimizerList)cloner.Clone(optimizers);
     115      clone.runs = (RunCollection)cloner.Clone(runs);
    106116      clone.stopPending = stopPending;
    107117      return clone;
     
    109119
    110120    public void Prepare() {
    111       Prepare(true);
    112     }
    113     public void Prepare(bool clearResults) {
     121      Prepare(false);
     122    }
     123    public void Prepare(bool clearRuns) {
    114124      if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
    115125        throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
    116126      if (Optimizers.Count > 0) {
     127        if (clearRuns) runs.Clear();
    117128        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
    118           optimizer.Prepare(clearResults);
     129          optimizer.Prepare(clearRuns);
    119130      }
    120131    }
     
    233244      optimizer.Started += new EventHandler(optimizer_Started);
    234245      optimizer.Stopped += new EventHandler(optimizer_Stopped);
    235     }
    236 
     246      optimizer.Runs.CollectionReset += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_CollectionReset);
     247      optimizer.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsAdded);
     248      optimizer.Runs.ItemsRemoved += new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsRemoved);
     249    }
    237250    private void DeregisterOptimizerEvents(IOptimizer optimizer) {
    238251      optimizer.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(optimizer_ExceptionOccurred);
     
    242255      optimizer.Started -= new EventHandler(optimizer_Started);
    243256      optimizer.Stopped -= new EventHandler(optimizer_Stopped);
     257      optimizer.Runs.CollectionReset -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_CollectionReset);
     258      optimizer.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsAdded);
     259      optimizer.Runs.ItemsRemoved -= new CollectionItemsChangedEventHandler<Run>(optimizer_Runs_ItemsRemoved);
    244260    }
    245261    private void optimizer_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     
    275291      }
    276292    }
     293    private void optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<Run> e) {
     294      Runs.RemoveRange(e.OldItems);
     295      Runs.AddRange(e.Items);
     296    }
     297    private void optimizer_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<Run> e) {
     298      Runs.AddRange(e.Items);
     299    }
     300    private void optimizer_Runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<Run> e) {
     301      Runs.RemoveRange(e.Items);
     302    }
    277303    #endregion
    278304  }
  • TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/IOptimizer.cs

    r3274 r3275  
    2626  /// Interface to represent optimizers such as algorithms, batch runs, or experiments.
    2727  /// </summary>
    28   public interface IOptimizer : IExecutable {
    29     void Prepare(bool clearResults);
     28  public interface IOptimizer : INamedItem, IExecutable {
     29    RunCollection Runs { get; }
     30
     31    void Prepare(bool clearRuns);
    3032  }
    3133}
  • TabularUnified trunk/sources/HeuristicLab.Optimization/3.3/Run.cs

    r3260 r3275  
    5959      results = new Dictionary<string, IItem>();
    6060    }
    61     public Run(string name, IAlgorithm algorithm)
    62       : base(name) {
    63       parameters = new Dictionary<string, IItem>();
    64       results = new Dictionary<string, IItem>();
    65       algorithm.CollectParameterValues(parameters);
    66       algorithm.CollectResultValues(results);
    67     }
    6861    public Run(string name, string description)
    6962      : base(name, description) {
    7063      parameters = new Dictionary<string, IItem>();
    7164      results = new Dictionary<string, IItem>();
    72     }
    73     public Run(string name, string description, IAlgorithm algorithm)
    74       : base(name, description) {
    75       parameters = new Dictionary<string, IItem>();
    76       results = new Dictionary<string, IItem>();
    77       algorithm.CollectParameterValues(parameters);
    78       algorithm.CollectResultValues(results);
    7965    }
    8066
Note: See TracChangeset for help on using the changeset viewer.