Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/19/10 06:19:16 (15 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on operators, engines, and optimization
Location:
trunk/sources/HeuristicLab.Core/3.3
Files:
3 added
7 edited
2 moved

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Engine.cs

    r2796 r2834  
    7171
    7272    [Storable]
    73     private IProblem problem;
    74     /// <summary>
    75     /// Gets or sets the current problem.
    76     /// </summary>
    77     public IProblem Problem {
    78       get { return problem; }
    79       set {
    80         if (value == null) throw new ArgumentNullException();
    81         if (value != problem) {
    82           problem = value;
    83           OnProblemChanged();
    84           Prepare();
    85         }
    86       }
    87     }
    88 
    89     [Storable]
    9073    private TimeSpan executionTime;
    9174    /// <summary>
     
    10588    /// </summary>
    10689    [Storable]
    107     private Stack<IExecutionSequence> executionStack;
     90    private Stack<IOperation> executionStack;
    10891    /// <summary>
    10992    /// Gets the current execution stack.
    11093    /// </summary>
    111     protected Stack<IExecutionSequence> ExecutionStack {
     94    protected Stack<IOperation> ExecutionStack {
    11295      get { return executionStack; }
    11396    }
     
    146129    protected Engine() {
    147130      globalScope = new Scope("Global");
    148       problem = null;
    149       executionStack = new Stack<IExecutionSequence>();
     131      executionStack = new Stack<IOperation>();
    150132      OperatorGraph = new OperatorGraph();
    151133    }
     
    162144      clone.OperatorGraph = (OperatorGraph)cloner.Clone(operatorGraph);
    163145      clone.globalScope = (Scope)cloner.Clone(globalScope);
    164       clone.problem = (IProblem)cloner.Clone(problem);
    165146      clone.executionTime = executionTime;
    166       IExecutionSequence[] contexts = executionStack.ToArray();
     147      IOperation[] contexts = executionStack.ToArray();
    167148      for (int i = contexts.Length - 1; i >= 0; i--)
    168         clone.executionStack.Push((IExecutionSequence)cloner.Clone(contexts[i]));
     149        clone.executionStack.Push((IOperation)cloner.Clone(contexts[i]));
    169150      clone.running = running;
    170151      clone.canceled = canceled;
     
    172153    }
    173154
     155    public void Prepare(IOperation initialOperation) {
     156      canceled = false;
     157      running = false;
     158      globalScope.Clear();
     159      ExecutionTime = new TimeSpan();
     160      executionStack.Clear();
     161      if (initialOperation != null)
     162        executionStack.Push(initialOperation);
     163      OnPrepared();
     164    }
    174165    /// <inheritdoc/>
    175166    /// <remarks>Sets <c>myCanceled</c> and <c>myRunning</c> to <c>false</c>. The global scope is cleared,
     
    178169    /// Calls <see cref="OnPrepared"/>.</remarks>
    179170    public void Prepare() {
    180       canceled = false;
    181       running = false;
    182       globalScope.Clear();
    183       ExecutionTime = new TimeSpan();
    184       executionStack.Clear();
    185       if ((OperatorGraph.InitialOperator != null) && (Problem != null))
    186         executionStack.Push(new ExecutionContext(null, OperatorGraph.InitialOperator, GlobalScope, Problem));
    187       OnPrepared();
     171      if (OperatorGraph.InitialOperator != null)
     172        Prepare(new ExecutionContext(null, OperatorGraph.InitialOperator, GlobalScope));
    188173    }
    189174    /// <inheritdoc/>
     
    254239    }
    255240    /// <summary>
    256     /// Occurs when the problem was changed.
    257     /// </summary>
    258     public event EventHandler ProblemChanged;
    259     /// <summary>
    260     /// Fires a new <c>ProblemChanged</c> event.
    261     /// </summary>
    262     protected virtual void OnProblemChanged() {
    263       if (ProblemChanged != null)
    264         ProblemChanged(this, EventArgs.Empty);
    265     }
    266     /// <summary>
    267241    /// Occurs when the execution time changed.
    268242    /// </summary>
  • trunk/sources/HeuristicLab.Core/3.3/ExecutionContext.cs

    r2796 r2834  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
     23using HeuristicLab.Collections;
    2524using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2625
    2726namespace HeuristicLab.Core {
    28   public class ExecutionContext : DeepCloneable, IExecutionSequence {
     27  public class ExecutionContext : DeepCloneable, IExecutionContext, IAtomicOperation {
    2928    [Storable]
    30     private ExecutionContext parent;
    31     public ExecutionContext Parent {
     29    private IParameterizedItem parameterizedItem;
     30
     31    [Storable]
     32    private IExecutionContext parent;
     33    public IExecutionContext Parent {
    3234      get { return parent; }
    3335    }
    3436
    35     [Storable]
    36     private IOperator op;
     37    public IObservableKeyedCollection<string, IParameter> Parameters {
     38      get { return parameterizedItem.Parameters; }
     39    }
     40
    3741    public IOperator Operator {
    38       get { return op; }
     42      get { return parameterizedItem as IOperator; }
    3943    }
    4044
     
    4549    }
    4650
    47     [Storable]
    48     private IProblem problem;
    49     public IProblem Problem {
    50       get { return problem; }
    51     }
    52 
    5351    private ExecutionContext() {
    5452      parent = null;
    55       op = null;
     53      parameterizedItem = null;
    5654      scope = null;
    57       problem = null;
    5855    }
    59     public ExecutionContext(ExecutionContext parent, IOperator op, IScope scope, IProblem problem) {
    60       if ((op == null) || (scope == null) || (problem == null)) throw new ArgumentNullException();
     56    public ExecutionContext(IExecutionContext parent, IParameterizedItem parameterizedItem, IScope scope) {
     57      if ((parameterizedItem == null) || (scope == null)) throw new ArgumentNullException();
    6158      this.parent = parent;
    62       this.op = op;
     59      this.parameterizedItem = parameterizedItem;
    6360      this.scope = scope;
    64       this.problem = problem;
    6561    }
    6662
     
    6864      ExecutionContext clone = new ExecutionContext();
    6965      cloner.RegisterClonedObject(this, clone);
    70       clone.parent = (ExecutionContext)cloner.Clone(parent);
    71       clone.op = (IOperator)cloner.Clone(op);
     66      clone.parent = (IExecutionContext)cloner.Clone(parent);
     67      clone.parameterizedItem = (IParameterizedItem)cloner.Clone(parameterizedItem);
    7268      clone.scope = (IScope)cloner.Clone(scope);
    73       clone.problem = (IProblem)cloner.Clone(problem);
    7469      return clone;
    7570    }
    7671
    77     public ExecutionContext CreateContext(IOperator op) {
    78       return new ExecutionContext(parent, op, scope, problem);
     72    public IAtomicOperation CreateOperation(IOperator op) {
     73      return new ExecutionContext(parent, op, scope);
    7974    }
    80     public ExecutionContext CreateContext(IOperator op, IScope scope) {
    81       return new ExecutionContext(parent, op, scope, problem);
     75    public IAtomicOperation CreateOperation(IOperator op, IScope scope) {
     76      return new ExecutionContext(parent, op, scope);
    8277    }
    83     public ExecutionContext CreateChildContext(IOperator op) {
    84       return new ExecutionContext(this, op, scope, problem);
     78    public IAtomicOperation CreateChildOperation(IOperator op) {
     79      return new ExecutionContext(this, op, scope);
    8580    }
    86     public ExecutionContext CreateChildContext(IOperator op, IScope scope) {
    87       return new ExecutionContext(this, op, scope, problem);
     81    public IAtomicOperation CreateChildOperation(IOperator op, IScope scope) {
     82      return new ExecutionContext(this, op, scope);
    8883    }
    8984  }
  • trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r2805 r2834  
    104104    <Compile Include="ChangedEventArgs.cs" />
    105105    <None Include="HeuristicLabCorePlugin.cs.frame" />
     106    <Compile Include="Interfaces\IParameterizedItem.cs" />
     107    <Compile Include="Interfaces\IAtomicOperation.cs" />
     108    <Compile Include="Interfaces\IExecutionContext.cs" />
     109    <Compile Include="Interfaces\IOperation.cs" />
     110    <Compile Include="OperationCollection.cs" />
    106111    <Compile Include="ValueParameterCollection.cs" />
    107     <Compile Include="Interfaces\IProblem.cs" />
    108     <Compile Include="Interfaces\IExecutionSequence.cs" />
    109112    <Compile Include="Interfaces\IValueLookupParameter.cs" />
    110113    <Compile Include="Interfaces\IValueParameter.cs" />
     
    117120    <Compile Include="Item.cs" />
    118121    <Compile Include="NamedItem.cs" />
    119     <Compile Include="Problem.cs" />
    120122    <Compile Include="OperatorGraph.cs" />
    121123    <Compile Include="Interfaces\IParameter.cs" />
     
    128130      <SubType>Code</SubType>
    129131    </Compile>
    130     <Compile Include="ExecutionContextCollection.cs" />
    131132    <Compile Include="ExecutionContext.cs" />
    132133    <Compile Include="OperatorList.cs" />
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IEngine.cs

    r2796 r2834  
    3838    /// </summary>
    3939    IScope GlobalScope { get; }
    40     /// <summary>
    41     /// Gets the problem of the current instance.
    42     /// </summary>
    43     IProblem Problem { get; set; }
    4440
    4541    /// <summary>
     
    6258    void Prepare();
    6359    /// <summary>
     60    /// Prepares the engine with a given initial operation.
     61    /// </summary>
     62    void Prepare(IOperation initialOperation);
     63    /// <summary>
    6464    /// Executes the whole run.
    6565    /// </summary>
     
    7878    /// </summary>
    7979    event EventHandler OperatorGraphChanged;
    80     /// <summary>
    81     /// Occurs when the problem was changed.
    82     /// </summary>
    83     event EventHandler ProblemChanged;
    8480    /// <summary>
    8581    /// Occurs when the execution time was changed.
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperation.cs

    r2829 r2834  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.ComponentModel;
    25 using System.Text;
    26 using System.Drawing;
    27 
    2822namespace HeuristicLab.Core {
    2923  /// <summary>
    30   /// Interface which represents an execution context.
     24  /// Interface which represents an operation.
    3125  /// </summary>
    32   public interface IExecutionSequence : IDeepCloneable { }
     26  public interface IOperation : IDeepCloneable { }
    3327}
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IOperator.cs

    r2790 r2834  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using HeuristicLab.Common;
    26 using HeuristicLab.Collections;
    2723
    2824namespace HeuristicLab.Core {
     
    3127  /// a basic instruction of an algorithm.
    3228  /// </summary>
    33   public interface IOperator : INamedItem {
    34     IObservableKeyedCollection<string, IParameter> Parameters { get; }
    35 
     29  public interface IOperator : INamedItem, IParameterizedItem {
    3630    /// <summary>
    3731    /// Gets or sets a boolean value whether the engine should stop here during the run.
     
    4438    /// <param name="scope">The scope where to execute the current instance.</param>
    4539    /// <returns>The next operation.</returns>
    46     IExecutionSequence Execute(ExecutionContext context);
     40    IOperation Execute(IExecutionContext context);
    4741    /// <summary>
    4842    /// Aborts the current operator.
  • trunk/sources/HeuristicLab.Core/3.3/Interfaces/IParameter.cs

    r2790 r2834  
    2121
    2222using System;
    23 using System.Collections.Generic;
    24 using System.Text;
    25 using System.Xml;
    26 using HeuristicLab.Common;
    2723
    2824namespace HeuristicLab.Core {
     
    3026    Type DataType { get; }
    3127    IItem ActualValue { get; set; }
    32     ExecutionContext ExecutionContext { get; set; }
     28    IExecutionContext ExecutionContext { get; set; }
    3329  }
    3430}
  • trunk/sources/HeuristicLab.Core/3.3/NamedItemCollection.cs

    r2833 r2834  
    2121
    2222using System;
    23 using System.Linq;
    2423using System.Collections.Generic;
    2524using System.Drawing;
     25using System.Linq;
    2626using HeuristicLab.Collections;
    2727using HeuristicLab.Common;
  • trunk/sources/HeuristicLab.Core/3.3/OperationCollection.cs

    r2829 r2834  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
    25 using System.Text;
    26 using System.Xml;
    2724using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2825
    2926namespace HeuristicLab.Core {
    30   public class ExecutionContextCollection : DeepCloneable, IList<IExecutionSequence>, IExecutionSequence {
     27  public class OperationCollection : DeepCloneable, IList<IOperation>, IOperation {
    3128    [Storable]
    32     private IList<IExecutionSequence> contexts;
     29    private IList<IOperation> operations;
    3330
    3431    [Storable]
     
    3936    }
    4037
    41     public ExecutionContextCollection() {
    42       contexts = new List<IExecutionSequence>();
     38    public OperationCollection() {
     39      operations = new List<IOperation>();
    4340      parallel = false;
    4441    }
    45     public ExecutionContextCollection(IEnumerable<IExecutionSequence> collection) {
    46       contexts = new List<IExecutionSequence>(collection.Where(e => e != null));
     42    public OperationCollection(IEnumerable<IOperation> collection) {
     43      operations = new List<IOperation>(collection.Where(e => e != null));
    4744      parallel = false;
    4845    }
    49     public ExecutionContextCollection(params IExecutionSequence[] list) {
    50       contexts = new List<IExecutionSequence>(list.Where(e => e != null));
     46    public OperationCollection(params IOperation[] list) {
     47      operations = new List<IOperation>(list.Where(e => e != null));
    5148      parallel = false;
    5249    }
    5350
    5451    public override IDeepCloneable Clone(Cloner cloner) {
    55       ExecutionContextCollection clone = new ExecutionContextCollection();
     52      OperationCollection clone = new OperationCollection(this.Select(x => (IOperation)cloner.Clone(x)));
    5653      cloner.RegisterClonedObject(this, clone);
    5754      clone.parallel = parallel;
    58       for (int i = 0; i < contexts.Count; i++)
    59         clone.contexts.Add((IExecutionSequence)cloner.Clone(contexts[i]));
    6055      return clone;
    6156    }
    6257
    63     #region IList<IExecutionContext> Members
    64     public int IndexOf(IExecutionSequence item) {
    65       return contexts.IndexOf(item);
     58    #region IList<IOperation> Members
     59    public int IndexOf(IOperation item) {
     60      return operations.IndexOf(item);
    6661    }
    67     public void Insert(int index, IExecutionSequence item) {
    68       if (item != null) contexts.Insert(index, item);
     62    public void Insert(int index, IOperation item) {
     63      if (item != null) operations.Insert(index, item);
    6964    }
    7065    public void RemoveAt(int index) {
    71       contexts.RemoveAt(index);
     66      operations.RemoveAt(index);
    7267    }
    73     public IExecutionSequence this[int index] {
    74       get { return contexts[index]; }
    75       set { if (value != null) contexts[index] = value; }
     68    public IOperation this[int index] {
     69      get { return operations[index]; }
     70      set { if (value != null) operations[index] = value; }
    7671    }
    7772    #endregion
    7873
    79     #region ICollection<IExecutionContext> Members
    80     public void Add(IExecutionSequence item) {
    81       if (item != null) contexts.Add(item);
     74    #region ICollection<IOperation> Members
     75    public void Add(IOperation item) {
     76      if (item != null) operations.Add(item);
    8277    }
    8378    public void Clear() {
    84       contexts.Clear();
     79      operations.Clear();
    8580    }
    86     public bool Contains(IExecutionSequence item) {
    87       return contexts.Contains(item);
     81    public bool Contains(IOperation item) {
     82      return operations.Contains(item);
    8883    }
    89     public void CopyTo(IExecutionSequence[] array, int arrayIndex) {
    90       contexts.CopyTo(array, arrayIndex);
     84    public void CopyTo(IOperation[] array, int arrayIndex) {
     85      operations.CopyTo(array, arrayIndex);
    9186    }
    9287    public int Count {
    93       get { return contexts.Count; }
     88      get { return operations.Count; }
    9489    }
    9590    public bool IsReadOnly {
    96       get { return contexts.IsReadOnly; }
     91      get { return operations.IsReadOnly; }
    9792    }
    98     public bool Remove(IExecutionSequence item) {
    99       return contexts.Remove(item);
     93    public bool Remove(IOperation item) {
     94      return operations.Remove(item);
    10095    }
    10196    #endregion
    10297
    103     #region IEnumerable<IExecutionContext> Members
    104     public IEnumerator<IExecutionSequence> GetEnumerator() {
    105       return contexts.GetEnumerator();
     98    #region IEnumerable<IOperation> Members
     99    public IEnumerator<IOperation> GetEnumerator() {
     100      return operations.GetEnumerator();
    106101    }
    107102    #endregion
     
    109104    #region IEnumerable Members
    110105    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
    111       return contexts.GetEnumerator();
     106      return operations.GetEnumerator();
    112107    }
    113108    #endregion
Note: See TracChangeset for help on using the changeset viewer.