Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11564


Ignore:
Timestamp:
11/23/14 05:32:41 (10 years ago)
Author:
swagner
Message:

#2205: Continued working on programmable network items

Location:
branches/OptimizationNetworks
Files:
11 added
2 deleted
18 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NetworkItemView.cs

    r11538 r11564  
    2727  [View("NetworkItem View")]
    2828  [Content(typeof(NetworkItem), true)]
     29  [Content(typeof(ProgrammableNetworkItem), true)]
    2930  [Content(typeof(INetworkItem), false)]
    3031  public partial class NetworkItemView : NamedItemView {
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NetworkView.cs

    r11530 r11564  
    2626  [View("Network View")]
    2727  [Content(typeof(Network), true)]
     28  [Content(typeof(ProgrammableNetwork), true)]
    2829  [Content(typeof(INetwork), false)]
    2930  public partial class NetworkView : NetworkItemView {
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NodeView.cs

    r11528 r11564  
    2626  [View("Node View")]
    2727  [Content(typeof(Node), true)]
     28  [Content(typeof(ProgrammableNode), true)]
    2829  [Content(typeof(INode), false)]
    2930  public partial class NodeView : NetworkItemView {
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNetworkView.Designer.cs

    r11529 r11564  
    4545    /// </summary>
    4646    private void InitializeComponent() {
    47       this.components = new System.ComponentModel.Container();
     47      this.codeTabPage = new System.Windows.Forms.TabPage();
     48      this.programmableItemView = new HeuristicLab.Core.Views.ProgrammableItemView();
     49      this.tabControl.SuspendLayout();
     50      this.nodesTabPage.SuspendLayout();
     51      this.portsTabPage.SuspendLayout();
     52      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
     53      this.SuspendLayout();
     54      //
     55      // tabControl
     56      //
     57      this.tabControl.Controls.Add(this.codeTabPage);
     58      this.tabControl.Controls.SetChildIndex(this.codeTabPage, 0);
     59      this.tabControl.Controls.SetChildIndex(this.portsTabPage, 0);
     60      this.tabControl.Controls.SetChildIndex(this.nodesTabPage, 0);
     61      //
     62      // nameTextBox
     63      //
     64      this.errorProvider.SetIconAlignment(this.nameTextBox, System.Windows.Forms.ErrorIconAlignment.MiddleLeft);
     65      this.errorProvider.SetIconPadding(this.nameTextBox, 2);
     66      //
     67      // codeTabPage
     68      //
     69      this.codeTabPage.Controls.Add(this.programmableItemView);
     70      this.codeTabPage.Location = new System.Drawing.Point(4, 22);
     71      this.codeTabPage.Name = "codeTabPage";
     72      this.codeTabPage.Size = new System.Drawing.Size(637, 465);
     73      this.codeTabPage.TabIndex = 2;
     74      this.codeTabPage.Text = "Code";
     75      this.codeTabPage.UseVisualStyleBackColor = true;
     76      //
     77      // programmableItemView
     78      //
     79      this.programmableItemView.Caption = "ProgrammableItem View";
     80      this.programmableItemView.Content = null;
     81      this.programmableItemView.Dock = System.Windows.Forms.DockStyle.Fill;
     82      this.programmableItemView.Location = new System.Drawing.Point(3, 3);
     83      this.programmableItemView.Name = "programmableItemView";
     84      this.programmableItemView.ReadOnly = false;
     85      this.programmableItemView.Size = new System.Drawing.Size(631, 459);
     86      this.programmableItemView.TabIndex = 0;
     87      //
     88      // UserDefinedNetworkView
     89      //
     90      this.Name = "UserDefinedNetworkView";
     91      this.tabControl.ResumeLayout(false);
     92      this.nodesTabPage.ResumeLayout(false);
     93      this.portsTabPage.ResumeLayout(false);
     94      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit();
     95      this.ResumeLayout(false);
     96      this.PerformLayout();
     97
    4898    }
    4999
    50100    #endregion
     101
     102    protected System.Windows.Forms.TabPage codeTabPage;
     103    protected HeuristicLab.Core.Views.ProgrammableItemView programmableItemView;
    51104  }
    52105}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNetworkView.cs

    r11530 r11564  
    4141      nodeCollectionView.Content = Content == null ? null : Content.Nodes;
    4242      portCollectionView.Content = Content == null ? null : Content.Ports;
     43      programmableItemView.Content = Content;
    4344    }
    4445  }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNodeView.Designer.cs

    r11529 r11564  
    4545    /// </summary>
    4646    private void InitializeComponent() {
    47       this.components = new System.ComponentModel.Container();
     47      this.tabControl = new HeuristicLab.MainForm.WindowsForms.DragOverTabControl();
     48      this.portsTabPage = new System.Windows.Forms.TabPage();
     49      this.codeTabPage = new System.Windows.Forms.TabPage();
     50      this.portCollectionView = new HeuristicLab.Core.Networks.Views.PortCollectionView();
     51      this.programmableItemView = new HeuristicLab.Core.Views.ProgrammableItemView();
     52      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).BeginInit();
     53      this.tabControl.SuspendLayout();
     54      this.portsTabPage.SuspendLayout();
     55      this.SuspendLayout();
     56      //
     57      // nameTextBox
     58      //
     59      this.errorProvider.SetIconAlignment(this.nameTextBox, System.Windows.Forms.ErrorIconAlignment.MiddleLeft);
     60      this.errorProvider.SetIconPadding(this.nameTextBox, 2);
     61      //
     62      // tabControl
     63      //
     64      this.tabControl.AllowDrop = true;
     65      this.tabControl.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
     66            | System.Windows.Forms.AnchorStyles.Left)
     67            | System.Windows.Forms.AnchorStyles.Right)));
     68      this.tabControl.Controls.Add(this.portsTabPage);
     69      this.tabControl.Controls.Add(this.codeTabPage);
     70      this.tabControl.Location = new System.Drawing.Point(0, 26);
     71      this.tabControl.Name = "tabControl";
     72      this.tabControl.SelectedIndex = 0;
     73      this.tabControl.Size = new System.Drawing.Size(645, 491);
     74      this.tabControl.TabIndex = 3;
     75      //
     76      // portsTabPage
     77      //
     78      this.portsTabPage.Controls.Add(this.portCollectionView);
     79      this.portsTabPage.Location = new System.Drawing.Point(4, 22);
     80      this.portsTabPage.Name = "portsTabPage";
     81      this.portsTabPage.Padding = new System.Windows.Forms.Padding(3);
     82      this.portsTabPage.Size = new System.Drawing.Size(637, 465);
     83      this.portsTabPage.TabIndex = 0;
     84      this.portsTabPage.Text = "Ports";
     85      this.portsTabPage.UseVisualStyleBackColor = true;
     86      //
     87      // codeTabPage
     88      //
     89      this.codeTabPage.Controls.Add(this.programmableItemView);
     90      this.codeTabPage.Location = new System.Drawing.Point(4, 22);
     91      this.codeTabPage.Name = "codeTabPage";
     92      this.codeTabPage.Padding = new System.Windows.Forms.Padding(3);
     93      this.codeTabPage.Size = new System.Drawing.Size(637, 465);
     94      this.codeTabPage.TabIndex = 1;
     95      this.codeTabPage.Text = "Code";
     96      this.codeTabPage.UseVisualStyleBackColor = true;
     97      //
     98      // portCollectionView
     99      //
     100      this.portCollectionView.Caption = "PortCollection View";
     101      this.portCollectionView.Content = null;
     102      this.portCollectionView.Dock = System.Windows.Forms.DockStyle.Fill;
     103      this.portCollectionView.Location = new System.Drawing.Point(3, 3);
     104      this.portCollectionView.Name = "portCollectionView";
     105      this.portCollectionView.ReadOnly = false;
     106      this.portCollectionView.ShowDetails = true;
     107      this.portCollectionView.Size = new System.Drawing.Size(631, 459);
     108      this.portCollectionView.TabIndex = 0;
     109      //
     110      // programmableItemView
     111      //
     112      this.programmableItemView.Caption = "ProgrammableItem View";
     113      this.programmableItemView.Content = null;
     114      this.programmableItemView.Dock = System.Windows.Forms.DockStyle.Fill;
     115      this.programmableItemView.Location = new System.Drawing.Point(3, 3);
     116      this.programmableItemView.Name = "programmableItemView";
     117      this.programmableItemView.ReadOnly = false;
     118      this.programmableItemView.Size = new System.Drawing.Size(631, 459);
     119      this.programmableItemView.TabIndex = 0;
     120      //
     121      // UserDefinedNodeView
     122      //
     123      this.Controls.Add(this.tabControl);
     124      this.Name = "UserDefinedNodeView";
     125      this.Controls.SetChildIndex(this.nameLabel, 0);
     126      this.Controls.SetChildIndex(this.nameTextBox, 0);
     127      this.Controls.SetChildIndex(this.infoLabel, 0);
     128      this.Controls.SetChildIndex(this.tabControl, 0);
     129      ((System.ComponentModel.ISupportInitialize)(this.errorProvider)).EndInit();
     130      this.tabControl.ResumeLayout(false);
     131      this.portsTabPage.ResumeLayout(false);
     132      this.ResumeLayout(false);
     133      this.PerformLayout();
     134
    48135    }
    49136
    50137    #endregion
     138
     139    protected MainForm.WindowsForms.DragOverTabControl tabControl;
     140    protected System.Windows.Forms.TabPage portsTabPage;
     141    protected PortCollectionView portCollectionView;
     142    protected System.Windows.Forms.TabPage codeTabPage;
     143    protected HeuristicLab.Core.Views.ProgrammableItemView programmableItemView;
     144
    51145  }
    52146}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNodeView.cs

    r11529 r11564  
    2727  [Content(typeof(UserDefinedNode), true)]
    2828  [Content(typeof(IUserDefinedNode), false)]
    29   public partial class UserDefinedNodeView : NodeView {
     29  public partial class UserDefinedNodeView : NetworkItemView {
    3030    public new IUserDefinedNode Content {
    3131      get { return (IUserDefinedNode)base.Content; }
     
    4040      base.OnContentChanged();
    4141      portCollectionView.Content = Content == null ? null : Content.Ports;
     42      programmableItemView.Content = Content;
    4243    }
    4344  }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/HeuristicLab.Optimization.Networks.Views-3.3.csproj

    r11563 r11564  
    187187      <DependentUpon>PortView.cs</DependentUpon>
    188188    </Compile>
    189     <Compile Include="Core.Networks.Views\ProgrammableNetworkItemView.cs">
    190       <SubType>UserControl</SubType>
    191     </Compile>
    192     <Compile Include="Core.Networks.Views\ProgrammableNetworkItemView.Designer.cs">
    193       <DependentUpon>ProgrammableNetworkItemView.cs</DependentUpon>
     189    <Compile Include="Core.Views\ProgrammableItemView.cs">
     190      <SubType>UserControl</SubType>
     191    </Compile>
     192    <Compile Include="Core.Views\ProgrammableItemView.Designer.cs">
     193      <DependentUpon>ProgrammableItemView.cs</DependentUpon>
    194194    </Compile>
    195195    <Compile Include="Core.Networks.Views\UserDefinedNetworkView.cs">
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IProgrammableNetworkItem.cs

    r11563 r11564  
    2020#endregion
    2121
    22 using System;
    23 using System.CodeDom.Compiler;
    24 using System.Collections.Generic;
    25 using System.Reflection;
    26 
    2722namespace HeuristicLab.Core.Networks {
    28   public interface IProgrammableNetworkItem : INetworkItem {
    29     string Code { get; set; }
    30     CompilerErrorCollection CompileErrors { get; }
    31 
    32     void Compile();
    33     IEnumerable<Assembly> GetAssemblies();
    34 
    35     event EventHandler CodeChanged;
    36     event EventHandler CompileErrorsChanged;
    37   }
     23  public interface IProgrammableNetworkItem : INetworkItem, IProgrammableItem { }
    3824}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IUserDefinedNetwork.cs

    r11529 r11564  
    2121
    2222namespace HeuristicLab.Core.Networks {
    23   public interface IUserDefinedNetwork : INetwork {
     23  public interface IUserDefinedNetwork : IProgrammableNetwork {
    2424    new PortCollection Ports { get; }
    2525    new NodeCollection Nodes { get; }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IUserDefinedNode.cs

    r11529 r11564  
    2121
    2222namespace HeuristicLab.Core.Networks {
    23   public interface IUserDefinedNode : INode {
     23  public interface IUserDefinedNode : IProgrammableNode {
    2424    new PortCollection Ports { get; }
    2525  }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ProgrammableNetworkItem.cs

    r11563 r11564  
    3535  [Item("ProgrammableNetworkItem", "Abstract base class for programmable items of a network.")]
    3636  [StorableClass]
    37   public abstract class ProgrammableNetworkItem : NetworkItem, IProgrammableNetworkItem {
    38     #region Item Properties
     37  public abstract class ProgrammableNetworkItem : Item, IProgrammableNetworkItem {
    3938    public static new Image StaticItemImage {
    4039      get { return HeuristicLab.Common.Resources.VSImageLibrary.Script; }
    4140    }
     41
     42    #region Item Members
    4243    public override string ItemName {
    43       get { return CompiledNetworkItem != null ? CompiledNetworkItem.ItemName : base.ItemName; }
     44      get { return CompiledNetworkItem.ItemName; }
    4445    }
    4546    public override string ItemDescription {
    46       get { return CompiledNetworkItem != null ? CompiledNetworkItem.ItemDescription : base.ItemDescription; }
     47      get { return CompiledNetworkItem.ItemDescription; }
    4748    }
    4849    public override Image ItemImage {
    49       get { return CompiledNetworkItem != null ? CompiledNetworkItem.ItemImage : base.ItemImage; }
    50     }
    51     #endregion
    52 
    53     #region NamedItem Properties
    54     public override bool CanChangeName {
    55       get { return CompiledNetworkItem != null ? CompiledNetworkItem.CanChangeName : base.CanChangeName; }
    56     }
    57     public override bool CanChangeDescription {
    58       get { return CompiledNetworkItem != null ? CompiledNetworkItem.CanChangeDescription : base.CanChangeDescription; }
     50      get { return CompiledNetworkItem.ItemImage; }
     51    }
     52    #endregion
     53
     54    #region NamedItem Members
     55    [Storable]
     56    private string name;
     57    public string Name {
     58      get { return CompiledNetworkItem.Name; }
     59      set { CompiledNetworkItem.Name = value; }
     60    }
     61    public bool CanChangeName {
     62      get { return CompiledNetworkItem.CanChangeName; }
     63    }
     64    [Storable]
     65    private string description;
     66    public string Description {
     67      get { return CompiledNetworkItem.Description; }
     68      set { CompiledNetworkItem.Description = value; }
     69    }
     70    public virtual bool CanChangeDescription {
     71      get { return CompiledNetworkItem.CanChangeDescription; }
     72    }
     73    public event EventHandler<CancelEventArgs<string>> NameChanging;
     74    private void OnNameChanging(CancelEventArgs<string> e) {
     75      var handler = NameChanging;
     76      if (handler != null) handler(this, e);
     77    }
     78    public event EventHandler NameChanged;
     79    private void OnNameChanged() {
     80      var handler = NameChanged;
     81      if (handler != null) handler(this, EventArgs.Empty);
     82    }
     83    public event EventHandler DescriptionChanged;
     84    private void OnDescriptionChanged() {
     85      var handler = DescriptionChanged;
     86      if (handler != null) handler(this, EventArgs.Empty);
     87    }
     88    #endregion
     89
     90    #region NetworkItem Members
     91    public INetworkItem Parent {
     92      get { return CompiledNetworkItem.Parent; }
     93    }
     94    public IEnumerable<INetworkItem> Children {
     95      get { return CompiledNetworkItem.Children; }
    5996    }
    6097    #endregion
     
    79116    public CompilerErrorCollection CompileErrors {
    80117      get { return compileErrors; }
    81       private set {
     118      protected set {
    82119        compileErrors = value;
    83120        OnCompileErrorsChanged();
     
    85122    }
    86123
    87     private INetworkItem compiledNetworkItem;
    88     protected INetworkItem CompiledNetworkItem {
     124    private Type compiledNetworkItemType;
     125    private CompiledProgrammableNetworkItem compiledNetworkItem;
     126    protected CompiledProgrammableNetworkItem CompiledNetworkItem {
    89127      get {
    90         if (compiledNetworkItem == null) {
    91           Compile();
    92         }
     128        if (compiledNetworkItem == null)
     129          CompiledNetworkItem = (CompiledProgrammableNetworkItem)Activator.CreateInstance(compiledNetworkItemType, this);
    93130        return compiledNetworkItem;
    94131      }
    95       set {
     132      private set {
    96133        if (compiledNetworkItem != value) {
    97           if (compiledNetworkItem != null) DeregisterCompiledNetworkItemEvents();
     134          if (compiledNetworkItem != null) {
     135            DeregisterCompiledNetworkItemEvents();
     136            compiledNetworkItem.DeregisterEvents();
     137          }
    98138          compiledNetworkItem = value;
    99           if (compiledNetworkItem != null) RegisterCompiledNetworkItemEvents();
    100           OnCompiledNetworkItemChanged();
     139          if (compiledNetworkItem != null) {
     140            RegisterCompiledNetworkItemEvents();
     141            compiledNetworkItem.RegisterEvents();
     142          }
    101143          OnToStringChanged();
    102144          OnItemImageChanged();
     145          OnNameChanged();
     146          OnDescriptionChanged();
    103147        }
    104148      }
     
    109153    protected ProgrammableNetworkItem(ProgrammableNetworkItem original, Cloner cloner)
    110154      : base(original, cloner) {
     155      // name and description are cloned in CompiledProgrammableNetworkItem
    111156      code = original.code;
    112157      if (original.compileErrors != null)
     
    115160    }
    116161    protected ProgrammableNetworkItem()
    117       : base("ProgrammableNetworkItem") {
     162      : base() {
     163      name = "ProgrammableNetworkItem";
     164      description = string.Empty;
    118165      code = CodeTemplate;
     166      Compile();
    119167    }
    120168    protected ProgrammableNetworkItem(string name)
    121       : base(name) {
     169      : base() {
     170      this.name = string.IsNullOrEmpty(name) ? string.Empty : name;
     171      description = string.Empty;
    122172      code = CodeTemplate;
     173      Compile();
    123174    }
    124175    protected ProgrammableNetworkItem(string name, string description)
    125       : base(name, description) {
     176      : base() {
     177      this.name = string.IsNullOrEmpty(name) ? string.Empty : name;
     178      this.description = string.IsNullOrEmpty(description) ? string.Empty : description;
    126179      code = CodeTemplate;
     180      Compile();
     181    }
     182
     183    [StorableHook(HookType.AfterDeserialization)]
     184    private void AfterDeserialization() {
     185      Compile();
    127186    }
    128187
    129188    public override string ToString() {
    130       return CompiledNetworkItem != null ? CompiledNetworkItem.ToString() : base.ToString();
     189      return CompiledNetworkItem.ToString();
    131190    }
    132191
     
    159218          Name, Environment.NewLine, sb.ToString()));
    160219      }
    161       CompiledNetworkItem = (INetworkItem)Activator.CreateInstance(
    162         results.CompiledAssembly.GetTypes()
    163           .Single(x => typeof(CompiledProgrammableNetworkItem)
    164           .IsAssignableFrom(x)),
    165         this);
     220      compiledNetworkItemType = results.CompiledAssembly.GetTypes()
     221          .Single(x => typeof(CompiledProgrammableNetworkItem).IsAssignableFrom(x));
     222      CompiledNetworkItem = null;
    166223    }
    167224    public virtual IEnumerable<Assembly> GetAssemblies() {
     
    180237      if (handler != null) handler(this, EventArgs.Empty);
    181238    }
    182     public event EventHandler CompiledNetworkItemChanged;
    183     protected virtual void OnCompiledNetworkItemChanged() {
    184       var handler = CompiledNetworkItemChanged;
    185       if (handler != null) handler(this, EventArgs.Empty);
    186     }
    187239
    188240    #region CompiledNetworkItem Events
    189241    protected virtual void RegisterCompiledNetworkItemEvents() {
    190       if (compiledNetworkItem != null) {
    191         compiledNetworkItem.ToStringChanged += CompiledNetworkItem_ToStringChanged;
    192         compiledNetworkItem.ItemImageChanged += CompiledNetworkItem_ItemImageChanged;
    193       }
     242      CompiledNetworkItem.ToStringChanged += CompiledNetworkItem_ToStringChanged;
     243      CompiledNetworkItem.ItemImageChanged += CompiledNetworkItem_ItemImageChanged;
     244      CompiledNetworkItem.NameChanging += CompiledNetworkItem_NameChanging;
     245      CompiledNetworkItem.NameChanged += CompiledNetworkItem_NameChanged;
     246      CompiledNetworkItem.DescriptionChanged += CompiledNetworkItem_DescriptionChanged;
    194247    }
    195248    protected virtual void DeregisterCompiledNetworkItemEvents() {
    196       if (compiledNetworkItem != null) {
    197         compiledNetworkItem.ToStringChanged -= CompiledNetworkItem_ToStringChanged;
    198         compiledNetworkItem.ItemImageChanged -= CompiledNetworkItem_ItemImageChanged;
    199       }
     249      CompiledNetworkItem.ToStringChanged -= CompiledNetworkItem_ToStringChanged;
     250      CompiledNetworkItem.ItemImageChanged -= CompiledNetworkItem_ItemImageChanged;
     251      CompiledNetworkItem.NameChanging -= CompiledNetworkItem_NameChanging;
     252      CompiledNetworkItem.NameChanged -= CompiledNetworkItem_NameChanged;
     253      CompiledNetworkItem.DescriptionChanged -= CompiledNetworkItem_DescriptionChanged;
    200254    }
    201255    private void CompiledNetworkItem_ToStringChanged(object sender, EventArgs e) {
     
    204258    private void CompiledNetworkItem_ItemImageChanged(object sender, EventArgs e) {
    205259      OnItemImageChanged();
     260    }
     261    private void CompiledNetworkItem_NameChanging(object sender, CancelEventArgs<string> e) {
     262      OnNameChanging(e);
     263    }
     264    private void CompiledNetworkItem_NameChanged(object sender, EventArgs e) {
     265      OnNameChanged();
     266    }
     267    private void CompiledNetworkItem_DescriptionChanged(object sender, EventArgs e) {
     268      OnDescriptionChanged();
    206269    }
    207270    #endregion
     
    214277      }
    215278
    216       protected ProgrammableNetworkItem Context { get; private set; }
    217 
    218       protected CompiledProgrammableNetworkItem(CompiledProgrammableNetworkItem original, Cloner cloner)
    219         : base(original, cloner) {
    220         Context = cloner.Clone(original.Context);
    221         RegisterContextEvents();
    222       }
    223       protected CompiledProgrammableNetworkItem(ProgrammableNetworkItem context) {
    224         Context = context;
    225         RegisterContextEvents();
    226       }
    227 
    228       public override string ToString() {
    229         return Name;
    230       }
    231 
    232       #region INamedItem Members
     279      #region NamedItem Members
    233280      public string Name {
    234         get { return Context.Name; }
    235         set { Context.Name = value; }
     281        get { return Context.name; }
     282        set {
     283          if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
     284          if (!(Context.name.Equals(value) || (value == null) && (Context.name == string.Empty))) {
     285            CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
     286            OnNameChanging(e);
     287            if (!e.Cancel) {
     288              Context.name = value == null ? string.Empty : value;
     289              OnNameChanged();
     290            }
     291          }
     292        }
    236293      }
    237294      public virtual bool CanChangeName {
     
    239296      }
    240297      public string Description {
    241         get { return Context.Description; }
    242         set { Context.Description = value; }
     298        get { return Context.description; }
     299        set {
     300          if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
     301          if (!(Context.description.Equals(value) || (value == null) && (Context.description == string.Empty))) {
     302            Context.description = value == null ? string.Empty : value;
     303            OnDescriptionChanged();
     304          }
     305        }
    243306      }
    244307      public virtual bool CanChangeDescription {
     
    264327      #endregion
    265328
    266       #region INetworkItem Members
     329      #region NetworkItem Members
     330      private INetworkItem parent;
    267331      public INetworkItem Parent {
    268         get { return Context.Parent; }
    269       }
    270       public IEnumerable<INetworkItem> Children {
    271         get { return Context.Children; }
     332        get { return parent; }
     333        protected set {
     334          if (value != parent) {
     335            parent = value;
     336          }
     337        }
     338      }
     339      public virtual IEnumerable<INetworkItem> Children {
     340        get { return Enumerable.Empty<INetworkItem>(); }
    272341      }
    273342      #endregion
    274343
    275       #region Context Events
    276       protected virtual void RegisterContextEvents() {
    277         Context.NameChanging += Context_NameChanging;
    278         Context.NameChanged += Context_NameChanged;
    279         Context.DescriptionChanged += Context_DescriptionChanged;
    280         Context.CompiledNetworkItemChanged += Context_CompiledNetworkItemChanged;
    281       }
    282       protected virtual void DeregisterContextEvents() {
    283         Context.NameChanging -= Context_NameChanging;
    284         Context.NameChanged -= Context_NameChanged;
    285         Context.DescriptionChanged -= Context_DescriptionChanged;
    286         Context.CompiledNetworkItemChanged -= Context_CompiledNetworkItemChanged;
    287       }
    288       private void Context_NameChanging(object sender, CancelEventArgs<string> e) {
    289         OnNameChanging(e);
    290       }
    291       private void Context_NameChanged(object sender, EventArgs e) {
    292         OnNameChanged();
    293       }
    294       private void Context_DescriptionChanged(object sender, EventArgs e) {
    295         OnDescriptionChanged();
    296       }
    297       private void Context_CompiledNetworkItemChanged(object sender, EventArgs e) {
    298         DeregisterContextEvents();
    299       }
     344      protected ProgrammableNetworkItem Context { get; private set; }
     345
     346      protected CompiledProgrammableNetworkItem(CompiledProgrammableNetworkItem original, Cloner cloner)
     347        : base(original, cloner) {
     348        Context = cloner.Clone(original.Context);
     349        Context.name = original.Context.name;
     350        Context.description = original.Context.description;
     351      }
     352      protected CompiledProgrammableNetworkItem(ProgrammableNetworkItem context)
     353        : base() {
     354        Context = context;
     355      }
     356
     357      public virtual void Initialize() { }
     358
     359      public override string ToString() {
     360        return Name;
     361      }
     362
     363      #region Events
     364      public virtual void RegisterEvents() { }
     365      public virtual void DeregisterEvents() { }
    300366      #endregion
    301367    }
     
    303369
    304370    #region Helpers
    305     protected string ReadCodeTemplate(string templateName) {
    306       using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(templateName))
     371    protected virtual string ReadCodeTemplate(string templateName) {
     372      using (var stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(templateName))
    307373      using (var reader = new StreamReader(stream)) {
    308374        return reader.ReadToEnd();
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ProgrammableNode.cs

    r11563 r11564  
    2727
    2828namespace HeuristicLab.Core.Networks {
    29   [Item("ProgrammableNode", "A programmable node of a network.")]
     29  [Item("ProgrammableNode", "Abstract base class for programmable nodes of a network.")]
    3030  [StorableClass]
    31   public class ProgrammableNode : ProgrammableNetworkItem, INode {
    32     protected override string CodeTemplate {
    33       get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Core.Networks.ProgrammableNodeTemplate.cs"); }
    34     }
    35 
     31  public abstract class ProgrammableNode : ProgrammableNetworkItem, IProgrammableNode {
     32    #region Node Members
    3633    new public INetwork Parent {
    37       get { return (INetwork)base.Parent; }
     34      get { return CompiledNetworkItem.Parent; }
    3835      set {
    3936        // NOTE: never call setter directly as the Parent property is set by the network which contains the node
    40         base.Parent = value;
     37        CompiledNetworkItem.Parent = value;
    4138      }
    42     }
    43     public override IEnumerable<INetworkItem> Children {
    44       get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
    4539    }
    4640
     
    4842    private PortCollection ports;
    4943    protected PortCollection Ports {
    50       get { return ports; }
     44      get { return CompiledNetworkItem.Ports; }
    5145    }
    5246    private ReadOnlyKeyedItemCollection<string, IPort> readOnlyPorts;
    5347    IKeyedItemCollection<string, IPort> INode.Ports {
    5448      get {
    55         if (readOnlyPorts == null) readOnlyPorts = ports.AsReadOnly();
     49        if (readOnlyPorts == null) readOnlyPorts = Ports.AsReadOnly();
    5650        return readOnlyPorts;
    5751      }
    5852    }
     53    #endregion
     54
     55    protected override string CodeTemplate {
     56      get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Core.Networks.ProgrammableNodeTemplate.cs"); }
     57    }
     58
     59    new protected CompiledProgrammableNode CompiledNetworkItem {
     60      get { return (CompiledProgrammableNode)base.CompiledNetworkItem; }
     61    }
    5962
    6063    [StorableConstructor]
    61     private ProgrammableNode(bool deserializing) : base(deserializing) { }
    62     private ProgrammableNode(ProgrammableNode original, Cloner cloner)
     64    protected ProgrammableNode(bool deserializing) : base(deserializing) { }
     65    protected ProgrammableNode(ProgrammableNode original, Cloner cloner)
    6366      : base(original, cloner) {
    64       ports = cloner.Clone(original.ports);
    65       foreach (var p in Ports)
    66         p.Parent = this;
     67      // ports are cloned in CompiledProgrammableNode
    6768      readOnlyPorts = null;
    68       RegisterPortsEvents();
    6969    }
    70     public ProgrammableNode()
     70    protected ProgrammableNode()
    7171      : base("ProgrammableNode") {
    7272      ports = new PortCollection();
    7373      readOnlyPorts = null;
    74       RegisterPortsEvents();
    7574    }
    76     public ProgrammableNode(string name)
     75    protected ProgrammableNode(string name)
    7776      : base(name) {
    7877      ports = new PortCollection();
    7978      readOnlyPorts = null;
    80       RegisterPortsEvents();
    8179    }
    82     public ProgrammableNode(string name, string description)
     80    protected ProgrammableNode(string name, string description)
    8381      : base(name, description) {
    8482      ports = new PortCollection();
    8583      readOnlyPorts = null;
    86       RegisterPortsEvents();
    8784    }
    88 
    89     [StorableHook(HookType.AfterDeserialization)]
    90     private void AfterDeserialization() {
    91       foreach (var p in Ports)
    92         p.Parent = this;
    93       RegisterPortsEvents();
    94     }
    95 
    96     public override IDeepCloneable Clone(Cloner cloner) {
    97       return new ProgrammableNode(this, cloner);
    98     }
    99 
    100     #region Ports Events
    101     protected virtual void RegisterPortsEvents() {
    102       ports.ItemsAdded += Ports_ItemsAdded;
    103       ports.ItemsRemoved += Ports_ItemsRemoved;
    104       ports.ItemsReplaced += Ports_ItemsReplaced;
    105       ports.CollectionReset += Ports_CollectionReset;
    106     }
    107     protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    108       foreach (var p in e.Items)
    109         p.Parent = this;
    110     }
    111     protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    112       foreach (var p in e.Items)
    113         p.Parent = null;
    114     }
    115     protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    116       foreach (var p in e.OldItems)
    117         p.Parent = null;
    118       foreach (var p in e.Items)
    119         p.Parent = this;
    120     }
    121     protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    122       foreach (var p in e.OldItems)
    123         p.Parent = null;
    124       foreach (var p in e.Items)
    125         p.Parent = this;
    126     }
    127     #endregion
    12885
    12986    #region CompiledProgrammableNode
    13087    [Item("CompiledProgrammableNode", "Abstract base class for compiled programmable nodes of a network.")]
    13188    public abstract class CompiledProgrammableNode : CompiledProgrammableNetworkItem, INode {
    132       protected new ProgrammableNode Context {
     89      #region Node Members
     90      new public INetwork Parent {
     91        get { return (INetwork)base.Parent; }
     92        set {
     93          // NOTE: never call setter directly as the Parent property is set by the network which contains the node
     94          base.Parent = value;
     95        }
     96      }
     97      public override IEnumerable<INetworkItem> Children {
     98        get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }
     99      }
     100
     101      public PortCollection Ports {
     102        get { return Context.ports; }
     103      }
     104      IKeyedItemCollection<string, IPort> INode.Ports {
     105        get { return Ports; }
     106      }
     107      #endregion
     108
     109      new protected ProgrammableNode Context {
    133110        get { return (ProgrammableNode)base.Context; }
    134111      }
     
    136113      protected CompiledProgrammableNode(CompiledProgrammableNode original, Cloner cloner)
    137114        : base(original, cloner) {
     115        Context.ports = cloner.Clone(original.Context.ports);
    138116      }
    139117      protected CompiledProgrammableNode(ProgrammableNode context)
     
    141119      }
    142120
    143       #region INode Members
    144       public new INetwork Parent {
    145         get { return Context.Parent; }
    146         set {
    147           // NOTE: never call setter directly as the Parent property is set by the network which contains the node
    148           Context.Parent = value;
     121      protected virtual void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { }
     122
     123      #region Events
     124      public override void RegisterEvents() {
     125        base.RegisterEvents();
     126        Ports.ItemsAdded += Ports_ItemsAdded;
     127        Ports.ItemsRemoved += Ports_ItemsRemoved;
     128        Ports.ItemsReplaced += Ports_ItemsReplaced;
     129        Ports.CollectionReset += Ports_CollectionReset;
     130        foreach (var p in Ports) {
     131          p.Parent = this;
     132          RegisterPortEvents(p);
    149133        }
    150134      }
    151 
    152       public IKeyedItemCollection<string, IPort> Ports {
    153         get { return Context.Ports; }
     135      public override void DeregisterEvents() {
     136        foreach (var p in Ports) {
     137          DeregisterPortEvents(p);
     138          p.Parent = null;
     139        }
     140        Ports.ItemsAdded -= Ports_ItemsAdded;
     141        Ports.ItemsRemoved -= Ports_ItemsRemoved;
     142        Ports.ItemsReplaced -= Ports_ItemsReplaced;
     143        Ports.CollectionReset -= Ports_CollectionReset;
     144        base.DeregisterEvents();
     145      }
     146      protected virtual void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
     147        foreach (var p in e.Items) {
     148          p.Parent = this;
     149          RegisterPortEvents(p);
     150        }
     151      }
     152      protected virtual void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
     153        foreach (var p in e.Items) {
     154          p.Parent = null;
     155          DeregisterPortEvents(p);
     156        }
     157      }
     158      protected virtual void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
     159        foreach (var p in e.OldItems) {
     160          p.Parent = null;
     161          DeregisterPortEvents(p);
     162        }
     163        foreach (var p in e.Items) {
     164          p.Parent = this;
     165          RegisterPortEvents(p);
     166        }
     167      }
     168      protected virtual void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
     169        foreach (var p in e.OldItems) {
     170          p.Parent = null;
     171          DeregisterPortEvents(p);
     172        }
     173        foreach (var p in e.Items) {
     174          p.Parent = this;
     175          RegisterPortEvents(p);
     176        }
    154177      }
    155178      #endregion
    156179
    157       #region Context Events
    158       protected override void RegisterContextEvents() {
    159         base.RegisterContextEvents();
    160         Context.Ports.ItemsAdded += Ports_ItemsAdded;
    161         Context.Ports.ItemsRemoved += Ports_ItemsRemoved;
    162         Context.Ports.ItemsReplaced += Ports_ItemsReplaced;
    163         Context.Ports.CollectionReset += Ports_CollectionReset;
    164         foreach (var p in Context.Ports)
    165           RegisterPortEvents(p);
    166       }
    167       protected override void DeregisterContextEvents() {
    168         foreach (var p in Context.Ports)
    169           DeregisterPortEvents(p);
    170         Context.Ports.ItemsAdded -= Ports_ItemsAdded;
    171         Context.Ports.ItemsRemoved -= Ports_ItemsRemoved;
    172         Context.Ports.ItemsReplaced -= Ports_ItemsReplaced;
    173         Context.Ports.CollectionReset -= Ports_CollectionReset;
    174         base.DeregisterContextEvents();
    175       }
    176       private void Ports_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    177         foreach (var p in e.Items)
    178           RegisterPortEvents(p);
    179       }
    180       private void Ports_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    181         foreach (var p in e.Items)
    182           DeregisterPortEvents(p);
    183       }
    184       private void Ports_ItemsReplaced(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    185         foreach (var p in e.OldItems)
    186           DeregisterPortEvents(p);
    187         foreach (var p in e.Items)
    188           RegisterPortEvents(p);
    189       }
    190       private void Ports_CollectionReset(object sender, Collections.CollectionItemsChangedEventArgs<IPort> e) {
    191         foreach (var p in e.OldItems)
    192           DeregisterPortEvents(p);
    193         foreach (var p in e.Items)
    194           RegisterPortEvents(p);
    195       }
     180      #region Port Events
    196181      protected virtual void RegisterPortEvents(IPort port) {
    197182        var mp = port as IMessagePort;
     
    204189          mp.MessageReceived -= MessagePort_MessageReceived;
    205190      }
    206       protected virtual void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { }
    207191      #endregion
    208192    }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/UserDefinedNetwork.cs

    r11529 r11564  
    2727  [Creatable("Optimization Networks")]
    2828  [StorableClass]
    29   public sealed class UserDefinedNetwork : Network, IUserDefinedNetwork {
     29  public sealed class UserDefinedNetwork : ProgrammableNetwork, IUserDefinedNetwork {
    3030    new public PortCollection Ports {
    3131      get { return base.Ports; }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/UserDefinedNode.cs

    r11529 r11564  
    2626  [Item("User-Defined Node", "A node of a network which can be defined by the user.")]
    2727  [StorableClass]
    28   public sealed class UserDefinedNode : Node, IUserDefinedNode {
     28  public sealed class UserDefinedNode : ProgrammableNode, IUserDefinedNode {
    2929    new public PortCollection Ports {
    3030      get { return base.Ports; }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/HeuristicLab.Optimization.Networks-3.3.csproj

    r11563 r11564  
    9191  <ItemGroup>
    9292    <Compile Include="Core.Networks\IConnectablePort.cs" />
     93    <Compile Include="Core.Networks\IProgrammableNetwork.cs" />
     94    <Compile Include="Core.Networks\ProgrammableNetwork.cs" />
     95    <EmbeddedResource Include="Core.Networks\ProgrammableNetworkTemplate.cs" />
     96    <Compile Include="Core\IProgrammableItem.cs" />
     97    <Compile Include="Core.Networks\IProgrammableNode.cs" />
    9398    <Compile Include="Core.Networks\IProgrammableNetworkItem.cs" />
    9499    <Compile Include="Core.Networks\IUserDefinedMessagePort.cs" />
     
    128133    <Compile Include="Operators\HookOperator.cs" />
    129134    <Compile Include="Operators\IHookOperator.cs" />
     135    <EmbeddedResource Include="Optimization.Networks.KSPTSP\KSPTSPConnectorCode.cs" />
    130136    <Compile Include="Optimization.Networks.KSPTSP\KSPTSPConnector.cs" />
     137    <EmbeddedResource Include="Optimization.Networks.KSPTSP\KSPTSPNetworkCode.cs" />
    131138    <Compile Include="Optimization.Networks.KSPTSP\KSPTSPNetwork.cs" />
    132139    <Compile Include="Optimization.Networks\AlgorithmNode.cs" />
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnector.cs

    r11530 r11564  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Core.Networks;
    25 using HeuristicLab.Data;
    26 using HeuristicLab.Encodings.BinaryVectorEncoding;
    2725using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Problems.TravelingSalesman;
    29 using System.Linq;
    3026
    3127namespace HeuristicLab.Optimization.Networks.KSPTSP {
    3228  [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")]
    3329  [StorableClass]
    34   public class KSPTSPConnector : Node {
     30  public class KSPTSPConnector : ProgrammableNode {
     31    protected override string CodeTemplate {
     32      get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Optimization.Networks.KSPTSP.KSPTSPConnectorCode.cs"); }
     33    }
     34
    3535    [StorableConstructor]
    3636    protected KSPTSPConnector(bool deserializing) : base(deserializing) { }
    37     protected KSPTSPConnector(KSPTSPConnector original, Cloner cloner)
    38       : base(original, cloner) {
    39       RegisterEvents();
    40     }
    41     public KSPTSPConnector()
    42       : base("KSPTSPConnector") {
    43       Initialize();
    44       RegisterEvents();
    45     }
    46     public KSPTSPConnector(string name)
    47       : base(name) {
    48       Initialize();
    49       RegisterEvents();
    50     }
    51     public KSPTSPConnector(string name, string description)
    52       : base(name, description) {
    53       Initialize();
    54       RegisterEvents();
    55     }
    56 
    57     [StorableHook(HookType.AfterDeserialization)]
    58     private void AfterDeserialization() {
    59       RegisterEvents();
    60     }
     37    protected KSPTSPConnector(KSPTSPConnector original, Cloner cloner) : base(original, cloner) { }
     38    public KSPTSPConnector() : base("KSPTSPConnector") { }
     39    public KSPTSPConnector(string name) : base(name) { }
     40    public KSPTSPConnector(string name, string description) : base(name, description) { }
    6141
    6242    public override IDeepCloneable Clone(Cloner cloner) {
    6343      return new KSPTSPConnector(this, cloner);
    6444    }
    65 
    66     protected virtual void Initialize() {
    67       var parameters = new MessagePort("Parameters");
    68       Ports.Add(parameters);
    69       parameters.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") { Type = PortParameterType.Input });
    70       parameters.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") { Type = PortParameterType.Input });
    71 
    72       var ksp = new MessagePort("KSP Connector");
    73       Ports.Add(ksp);
    74       ksp.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") { Type = PortParameterType.Input });
    75       ksp.Parameters.Add(new PortParameter<DoubleValue>("Quality") { Type = PortParameterType.Input | PortParameterType.Output });
    76       ksp.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") { Type = PortParameterType.Output });
    77       ksp.Parameters.Add(new PortParameter<PathTSPTour>("Route") { Type = PortParameterType.Output });
    78 
    79       var tsp = new MessagePort("TSP Connector");
    80       Ports.Add(tsp);
    81       tsp.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") { Type = PortParameterType.Output });
    82       tsp.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") { Type = PortParameterType.Input });
    83       tsp.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") { Type = PortParameterType.Input });
    84     }
    85 
    86     protected virtual void RegisterEvents() {
    87       var ksp = (IMessagePort)Ports["KSP Connector"];
    88       ksp.MessageReceived += Knapsack_MessageReceived;
    89     }
    90 
    91     protected virtual void Knapsack_MessageReceived(object sender, EventArgs<IMessage, System.Threading.CancellationToken> e) {
    92       // get parameters
    93       var parametersPort = (IMessagePort)Ports["Parameters"];
    94       var parameters = parametersPort.PrepareMessage();
    95       parametersPort.SendMessage(parameters, e.Value2);
    96       var cities = (DoubleMatrix)parameters["Cities"];
    97       var factor = ((DoubleValue)parameters["TransportCostsFactor"]).Value;
    98 
    99       // build coordinates
    100       var kspMsg = e.Value;
    101       var kspSolution = (BinaryVector)kspMsg["KnapsackSolution"];
    102       var kspQuality = ((DoubleValue)kspMsg["Quality"]).Value;
    103       var coords = new DoubleMatrix(kspSolution.Count(x => x), 2);
    104       int j = 0;
    105       for (int i = 0; i < kspSolution.Length; i++) {
    106         if (kspSolution[i]) {
    107           coords[j, 0] = cities[i, 0];
    108           coords[j, 1] = cities[i, 1];
    109           j++;
    110         }
    111       }
    112 
    113       // solve TSP
    114       var tspConnectorPort = (IMessagePort)Ports["TSP Connector"];
    115       var tspMsg = tspConnectorPort.PrepareMessage();
    116       tspMsg["Coordinates"] = coords;
    117       tspConnectorPort.SendMessage(tspMsg, e.Value2);
    118       var tspSolution = (PathTSPTour)tspMsg["Best TSP Solution"];
    119       var tspQuality = ((DoubleValue)tspMsg["BestQuality"]).Value;
    120 
    121       // calculate transport costs
    122       ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;
    123       kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);
    124       kspMsg["Route"] = tspSolution;
    125     }
    12645  }
    12746}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs

    r11530 r11564  
    2020#endregion
    2121
    22 using HeuristicLab.Algorithms.GeneticAlgorithm;
    2322using HeuristicLab.Common;
    2423using HeuristicLab.Core;
    2524using HeuristicLab.Core.Networks;
    26 using HeuristicLab.Data;
    27 using HeuristicLab.Encodings.BinaryVectorEncoding;
    28 using HeuristicLab.Operators;
    29 using HeuristicLab.Parameters;
    3025using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    31 using HeuristicLab.Problems.Knapsack;
    32 using HeuristicLab.Problems.TravelingSalesman;
    33 using System.Linq;
     26using System.Drawing;
    3427
    3528namespace HeuristicLab.Optimization.Networks.KSPTSP {
     
    3730  [Creatable("Optimization Networks")]
    3831  [StorableClass]
    39   public class KSPTSPNetwork : Network {
     32  public class KSPTSPNetwork : ProgrammableNetwork {
     33    public static new Image StaticItemImage {
     34      get { return HeuristicLab.Common.Resources.VSImageLibrary.Module; }
     35    }
     36
     37    protected override string CodeTemplate {
     38      get { return ReadCodeTemplate("HeuristicLab.Optimization.Networks.Optimization.Networks.KSPTSP.KSPTSPNetworkCode.cs"); }
     39    }
     40
    4041    [StorableConstructor]
    4142    protected KSPTSPNetwork(bool deserializing) : base(deserializing) { }
    4243    protected KSPTSPNetwork(KSPTSPNetwork original, Cloner cloner) : base(original, cloner) { }
    43     public KSPTSPNetwork()
    44       : base("KSPTSPNetwork") {
    45       Initialize();
    46     }
    47     public KSPTSPNetwork(string name)
    48       : base(name) {
    49       Initialize();
    50     }
    51     public KSPTSPNetwork(string name, string description)
    52       : base(name, description) {
    53       Initialize();
    54     }
     44    public KSPTSPNetwork() : base("KSPTSPNetwork") { }
     45    public KSPTSPNetwork(string name) : base(name) { }
     46    public KSPTSPNetwork(string name, string description) : base(name, description) { }
    5547
    5648    public override IDeepCloneable Clone(Cloner cloner) {
    5749      return new KSPTSPNetwork(this, cloner);
    5850    }
    59 
    60     protected virtual void Initialize() {
    61       #region ParametersNode
    62       var paramsNode = new UserDefinedNode("ParametersNode");
    63       Nodes.Add(paramsNode);
    64 
    65       var paramsPort = new MessagePort("Parameters");
    66       paramsNode.Ports.Add(paramsPort);
    67 
    68       paramsPort.Parameters.Add(new PortParameter<DoubleMatrix>("Cities") {
    69         Type = PortParameterType.Output,
    70         DefaultValue = new DoubleMatrix(new double[,] {
    71           {00, 00}, {10, 00}, {20, 00}, {30, 00}, {40, 00},
    72           {00, 10}, {10, 10}, {20, 10}, {30, 10}, {40, 10},
    73           {00, 20}, {10, 20}, {20, 20}, {30, 20}, {40, 20},
    74           {00, 30}, {10, 30}, {20, 30}, {30, 30}, {40, 30},
    75           {00, 40}, {10, 40}, {20, 40}, {30, 40}, {40, 40}
    76         })
    77       });
    78       paramsPort.Parameters.Add(new PortParameter<DoubleValue>("TransportCostsFactor") {
    79         Type = PortParameterType.Output,
    80         DefaultValue = new DoubleValue(0.1)
    81       });
    82       paramsPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
    83         Type = PortParameterType.Output,
    84         DefaultValue = new IntValue(10)
    85       });
    86       paramsPort.Parameters.Add(new PortParameter<IntArray>("Values") {
    87         Type = PortParameterType.Output,
    88         DefaultValue = new IntArray(new int[] { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 })
    89       });
    90       paramsPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
    91         Type = PortParameterType.Output,
    92         DefaultValue = new IntArray(new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 })
    93       });
    94       #endregion
    95 
    96       #region KSPNode
    97       var kspNode = new AlgorithmNode("KSPNode");
    98       Nodes.Add(kspNode);
    99 
    100       var configPort = new ConfigurationPort("ConfigureKSP");
    101       kspNode.Ports.Add(configPort);
    102 
    103       configPort.Parameters.Add(new PortParameter<IntValue>("KnapsackCapacity") {
    104         Type = PortParameterType.Input
    105       });
    106       configPort.Parameters.Add(new PortParameter<IntArray>("Values") {
    107         Type = PortParameterType.Input
    108       });
    109       configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {
    110         Type = PortParameterType.Input
    111       });
    112 
    113       var evaluatePort = new MessagePort("EvaluateRoute");
    114       kspNode.Ports.Add(evaluatePort);
    115 
    116       evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {
    117         Type = PortParameterType.Output
    118       });
    119       evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {
    120         Type = PortParameterType.Output | PortParameterType.Input
    121       });
    122       evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {
    123         Type = PortParameterType.Input
    124       });
    125       evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {
    126         Type = PortParameterType.Input
    127       });
    128 
    129       var kspGA = new GeneticAlgorithm();
    130       kspGA.Problem = new KnapsackProblem();
    131       kspGA.MaximumGenerations.Value = 50;
    132       kspGA.PopulationSize.Value = 10;
    133       kspGA.Mutator = kspGA.MutatorParameter.ValidValues.Where(x => x.Name == "SinglePositionBitflipManipulator").First();
    134       kspNode.Algorithm = kspGA;
    135 
    136       var hook = new HookOperator();
    137       hook.Parameters.Add(new LookupParameter<BinaryVector>("KnapsackSolution") { Hidden = false });
    138       hook.Parameters.Add(new LookupParameter<DoubleValue>("Quality") { Hidden = false });
    139       hook.Parameters.Add(new LookupParameter<DoubleValue>("TransportCosts") { Hidden = false });
    140       hook.Parameters.Add(new LookupParameter<PathTSPTour>("Route") { Hidden = false });
    141       ((FixedValueParameter<OperatorList>)kspGA.Problem.Evaluator.Parameters["AfterExecutionOperators"]).Value.Add(hook);
    142       #endregion
    143 
    144       #region TSPNode
    145       var tspNode = new AlgorithmNode("TSPNode");
    146       Nodes.Add(tspNode);
    147 
    148       var executePort = new ExecutionPort("Execute");
    149       tspNode.Ports.Add(executePort);
    150 
    151       executePort.Parameters.Add(new PortParameter<DoubleMatrix>("Coordinates") {
    152         Type = PortParameterType.Input
    153       });
    154       executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {
    155         Type = PortParameterType.Output
    156       });
    157       executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {
    158         Type = PortParameterType.Output
    159       });
    160 
    161       var tspGA = new GeneticAlgorithm();
    162       tspGA.Problem = new TravelingSalesmanProblem();
    163       tspGA.MaximumGenerations.Value = 100;
    164       tspGA.PopulationSize.Value = 50;
    165       tspGA.Mutator = tspGA.MutatorParameter.ValidValues.Where(x => x.Name == "InversionManipulator").First();
    166       tspNode.Algorithm = tspGA;
    167       #endregion
    168 
    169       #region KSPTSPConnector
    170       var ksptspConnector = (INode)new KSPTSPConnector();
    171       Nodes.Add(ksptspConnector);
    172       #endregion
    173 
    174       #region Wire Ports
    175       hook.Port = evaluatePort;
    176       configPort.ConnectedPort = paramsPort;
    177       evaluatePort.ConnectedPort = (IMessagePort)ksptspConnector.Ports["KSP Connector"];
    178       ((IMessagePort)ksptspConnector.Ports["Parameters"]).ConnectedPort = paramsPort;
    179       ((IMessagePort)ksptspConnector.Ports["TSP Connector"]).ConnectedPort = executePort;
    180 
    181       paramsPort.SendMessage(paramsPort.PrepareMessage());
    182       #endregion
    183     }
    18451  }
    18552}
Note: See TracChangeset for help on using the changeset viewer.