Changeset 11564
- Timestamp:
- 11/23/14 05:32:41 (10 years ago)
- 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 27 27 [View("NetworkItem View")] 28 28 [Content(typeof(NetworkItem), true)] 29 [Content(typeof(ProgrammableNetworkItem), true)] 29 30 [Content(typeof(INetworkItem), false)] 30 31 public partial class NetworkItemView : NamedItemView { -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NetworkView.cs
r11530 r11564 26 26 [View("Network View")] 27 27 [Content(typeof(Network), true)] 28 [Content(typeof(ProgrammableNetwork), true)] 28 29 [Content(typeof(INetwork), false)] 29 30 public partial class NetworkView : NetworkItemView { -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/NodeView.cs
r11528 r11564 26 26 [View("Node View")] 27 27 [Content(typeof(Node), true)] 28 [Content(typeof(ProgrammableNode), true)] 28 29 [Content(typeof(INode), false)] 29 30 public partial class NodeView : NetworkItemView { -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNetworkView.Designer.cs
r11529 r11564 45 45 /// </summary> 46 46 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 48 98 } 49 99 50 100 #endregion 101 102 protected System.Windows.Forms.TabPage codeTabPage; 103 protected HeuristicLab.Core.Views.ProgrammableItemView programmableItemView; 51 104 } 52 105 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNetworkView.cs
r11530 r11564 41 41 nodeCollectionView.Content = Content == null ? null : Content.Nodes; 42 42 portCollectionView.Content = Content == null ? null : Content.Ports; 43 programmableItemView.Content = Content; 43 44 } 44 45 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNodeView.Designer.cs
r11529 r11564 45 45 /// </summary> 46 46 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 48 135 } 49 136 50 137 #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 51 145 } 52 146 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/Core.Networks.Views/UserDefinedNodeView.cs
r11529 r11564 27 27 [Content(typeof(UserDefinedNode), true)] 28 28 [Content(typeof(IUserDefinedNode), false)] 29 public partial class UserDefinedNodeView : N odeView {29 public partial class UserDefinedNodeView : NetworkItemView { 30 30 public new IUserDefinedNode Content { 31 31 get { return (IUserDefinedNode)base.Content; } … … 40 40 base.OnContentChanged(); 41 41 portCollectionView.Content = Content == null ? null : Content.Ports; 42 programmableItemView.Content = Content; 42 43 } 43 44 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks.Views/3.3/HeuristicLab.Optimization.Networks.Views-3.3.csproj
r11563 r11564 187 187 <DependentUpon>PortView.cs</DependentUpon> 188 188 </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>Programmable NetworkItemView.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> 194 194 </Compile> 195 195 <Compile Include="Core.Networks.Views\UserDefinedNetworkView.cs"> -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IProgrammableNetworkItem.cs
r11563 r11564 20 20 #endregion 21 21 22 using System;23 using System.CodeDom.Compiler;24 using System.Collections.Generic;25 using System.Reflection;26 27 22 namespace 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 { } 38 24 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IUserDefinedNetwork.cs
r11529 r11564 21 21 22 22 namespace HeuristicLab.Core.Networks { 23 public interface IUserDefinedNetwork : I Network {23 public interface IUserDefinedNetwork : IProgrammableNetwork { 24 24 new PortCollection Ports { get; } 25 25 new NodeCollection Nodes { get; } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/IUserDefinedNode.cs
r11529 r11564 21 21 22 22 namespace HeuristicLab.Core.Networks { 23 public interface IUserDefinedNode : I Node {23 public interface IUserDefinedNode : IProgrammableNode { 24 24 new PortCollection Ports { get; } 25 25 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ProgrammableNetworkItem.cs
r11563 r11564 35 35 [Item("ProgrammableNetworkItem", "Abstract base class for programmable items of a network.")] 36 36 [StorableClass] 37 public abstract class ProgrammableNetworkItem : NetworkItem, IProgrammableNetworkItem { 38 #region Item Properties 37 public abstract class ProgrammableNetworkItem : Item, IProgrammableNetworkItem { 39 38 public static new Image StaticItemImage { 40 39 get { return HeuristicLab.Common.Resources.VSImageLibrary.Script; } 41 40 } 41 42 #region Item Members 42 43 public override string ItemName { 43 get { return CompiledNetworkItem != null ? CompiledNetworkItem.ItemName : base.ItemName; }44 get { return CompiledNetworkItem.ItemName; } 44 45 } 45 46 public override string ItemDescription { 46 get { return CompiledNetworkItem != null ? CompiledNetworkItem.ItemDescription : base.ItemDescription; }47 get { return CompiledNetworkItem.ItemDescription; } 47 48 } 48 49 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; } 59 96 } 60 97 #endregion … … 79 116 public CompilerErrorCollection CompileErrors { 80 117 get { return compileErrors; } 81 pr ivateset {118 protected set { 82 119 compileErrors = value; 83 120 OnCompileErrorsChanged(); … … 85 122 } 86 123 87 private INetworkItem compiledNetworkItem; 88 protected INetworkItem CompiledNetworkItem { 124 private Type compiledNetworkItemType; 125 private CompiledProgrammableNetworkItem compiledNetworkItem; 126 protected CompiledProgrammableNetworkItem CompiledNetworkItem { 89 127 get { 90 if (compiledNetworkItem == null) { 91 Compile(); 92 } 128 if (compiledNetworkItem == null) 129 CompiledNetworkItem = (CompiledProgrammableNetworkItem)Activator.CreateInstance(compiledNetworkItemType, this); 93 130 return compiledNetworkItem; 94 131 } 95 set {132 private set { 96 133 if (compiledNetworkItem != value) { 97 if (compiledNetworkItem != null) DeregisterCompiledNetworkItemEvents(); 134 if (compiledNetworkItem != null) { 135 DeregisterCompiledNetworkItemEvents(); 136 compiledNetworkItem.DeregisterEvents(); 137 } 98 138 compiledNetworkItem = value; 99 if (compiledNetworkItem != null) RegisterCompiledNetworkItemEvents(); 100 OnCompiledNetworkItemChanged(); 139 if (compiledNetworkItem != null) { 140 RegisterCompiledNetworkItemEvents(); 141 compiledNetworkItem.RegisterEvents(); 142 } 101 143 OnToStringChanged(); 102 144 OnItemImageChanged(); 145 OnNameChanged(); 146 OnDescriptionChanged(); 103 147 } 104 148 } … … 109 153 protected ProgrammableNetworkItem(ProgrammableNetworkItem original, Cloner cloner) 110 154 : base(original, cloner) { 155 // name and description are cloned in CompiledProgrammableNetworkItem 111 156 code = original.code; 112 157 if (original.compileErrors != null) … … 115 160 } 116 161 protected ProgrammableNetworkItem() 117 : base("ProgrammableNetworkItem") { 162 : base() { 163 name = "ProgrammableNetworkItem"; 164 description = string.Empty; 118 165 code = CodeTemplate; 166 Compile(); 119 167 } 120 168 protected ProgrammableNetworkItem(string name) 121 : base(name) { 169 : base() { 170 this.name = string.IsNullOrEmpty(name) ? string.Empty : name; 171 description = string.Empty; 122 172 code = CodeTemplate; 173 Compile(); 123 174 } 124 175 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; 126 179 code = CodeTemplate; 180 Compile(); 181 } 182 183 [StorableHook(HookType.AfterDeserialization)] 184 private void AfterDeserialization() { 185 Compile(); 127 186 } 128 187 129 188 public override string ToString() { 130 return CompiledNetworkItem != null ? CompiledNetworkItem.ToString() : base.ToString();189 return CompiledNetworkItem.ToString(); 131 190 } 132 191 … … 159 218 Name, Environment.NewLine, sb.ToString())); 160 219 } 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; 166 223 } 167 224 public virtual IEnumerable<Assembly> GetAssemblies() { … … 180 237 if (handler != null) handler(this, EventArgs.Empty); 181 238 } 182 public event EventHandler CompiledNetworkItemChanged;183 protected virtual void OnCompiledNetworkItemChanged() {184 var handler = CompiledNetworkItemChanged;185 if (handler != null) handler(this, EventArgs.Empty);186 }187 239 188 240 #region CompiledNetworkItem Events 189 241 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; 194 247 } 195 248 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; 200 254 } 201 255 private void CompiledNetworkItem_ToStringChanged(object sender, EventArgs e) { … … 204 258 private void CompiledNetworkItem_ItemImageChanged(object sender, EventArgs e) { 205 259 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(); 206 269 } 207 270 #endregion … … 214 277 } 215 278 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 233 280 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 } 236 293 } 237 294 public virtual bool CanChangeName { … … 239 296 } 240 297 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 } 243 306 } 244 307 public virtual bool CanChangeDescription { … … 264 327 #endregion 265 328 266 #region INetworkItem Members 329 #region NetworkItem Members 330 private INetworkItem parent; 267 331 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>(); } 272 341 } 273 342 #endregion 274 343 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() { } 300 366 #endregion 301 367 } … … 303 369 304 370 #region Helpers 305 protected string ReadCodeTemplate(string templateName) {306 using (var stream = Assembly.Get ExecutingAssembly().GetManifestResourceStream(templateName))371 protected virtual string ReadCodeTemplate(string templateName) { 372 using (var stream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream(templateName)) 307 373 using (var reader = new StreamReader(stream)) { 308 374 return reader.ReadToEnd(); -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/ProgrammableNode.cs
r11563 r11564 27 27 28 28 namespace HeuristicLab.Core.Networks { 29 [Item("ProgrammableNode", "A programmable nodeof a network.")]29 [Item("ProgrammableNode", "Abstract base class for programmable nodes of a network.")] 30 30 [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 36 33 new public INetwork Parent { 37 get { return (INetwork)base.Parent; }34 get { return CompiledNetworkItem.Parent; } 38 35 set { 39 36 // 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; 41 38 } 42 }43 public override IEnumerable<INetworkItem> Children {44 get { return base.Children.Concat(Ports.AsEnumerable<INetworkItem>()); }45 39 } 46 40 … … 48 42 private PortCollection ports; 49 43 protected PortCollection Ports { 50 get { return ports; }44 get { return CompiledNetworkItem.Ports; } 51 45 } 52 46 private ReadOnlyKeyedItemCollection<string, IPort> readOnlyPorts; 53 47 IKeyedItemCollection<string, IPort> INode.Ports { 54 48 get { 55 if (readOnlyPorts == null) readOnlyPorts = ports.AsReadOnly();49 if (readOnlyPorts == null) readOnlyPorts = Ports.AsReadOnly(); 56 50 return readOnlyPorts; 57 51 } 58 52 } 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 } 59 62 60 63 [StorableConstructor] 61 pr ivateProgrammableNode(bool deserializing) : base(deserializing) { }62 pr ivateProgrammableNode(ProgrammableNode original, Cloner cloner)64 protected ProgrammableNode(bool deserializing) : base(deserializing) { } 65 protected ProgrammableNode(ProgrammableNode original, Cloner cloner) 63 66 : 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 67 68 readOnlyPorts = null; 68 RegisterPortsEvents();69 69 } 70 p ublicProgrammableNode()70 protected ProgrammableNode() 71 71 : base("ProgrammableNode") { 72 72 ports = new PortCollection(); 73 73 readOnlyPorts = null; 74 RegisterPortsEvents();75 74 } 76 p ublicProgrammableNode(string name)75 protected ProgrammableNode(string name) 77 76 : base(name) { 78 77 ports = new PortCollection(); 79 78 readOnlyPorts = null; 80 RegisterPortsEvents();81 79 } 82 p ublicProgrammableNode(string name, string description)80 protected ProgrammableNode(string name, string description) 83 81 : base(name, description) { 84 82 ports = new PortCollection(); 85 83 readOnlyPorts = null; 86 RegisterPortsEvents();87 84 } 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 Events101 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 #endregion128 85 129 86 #region CompiledProgrammableNode 130 87 [Item("CompiledProgrammableNode", "Abstract base class for compiled programmable nodes of a network.")] 131 88 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 { 133 110 get { return (ProgrammableNode)base.Context; } 134 111 } … … 136 113 protected CompiledProgrammableNode(CompiledProgrammableNode original, Cloner cloner) 137 114 : base(original, cloner) { 115 Context.ports = cloner.Clone(original.Context.ports); 138 116 } 139 117 protected CompiledProgrammableNode(ProgrammableNode context) … … 141 119 } 142 120 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); 149 133 } 150 134 } 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 } 154 177 } 155 178 #endregion 156 179 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 196 181 protected virtual void RegisterPortEvents(IPort port) { 197 182 var mp = port as IMessagePort; … … 204 189 mp.MessageReceived -= MessagePort_MessageReceived; 205 190 } 206 protected virtual void MessagePort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) { }207 191 #endregion 208 192 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/UserDefinedNetwork.cs
r11529 r11564 27 27 [Creatable("Optimization Networks")] 28 28 [StorableClass] 29 public sealed class UserDefinedNetwork : Network, IUserDefinedNetwork {29 public sealed class UserDefinedNetwork : ProgrammableNetwork, IUserDefinedNetwork { 30 30 new public PortCollection Ports { 31 31 get { return base.Ports; } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/UserDefinedNode.cs
r11529 r11564 26 26 [Item("User-Defined Node", "A node of a network which can be defined by the user.")] 27 27 [StorableClass] 28 public sealed class UserDefinedNode : Node, IUserDefinedNode {28 public sealed class UserDefinedNode : ProgrammableNode, IUserDefinedNode { 29 29 new public PortCollection Ports { 30 30 get { return base.Ports; } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/HeuristicLab.Optimization.Networks-3.3.csproj
r11563 r11564 91 91 <ItemGroup> 92 92 <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" /> 93 98 <Compile Include="Core.Networks\IProgrammableNetworkItem.cs" /> 94 99 <Compile Include="Core.Networks\IUserDefinedMessagePort.cs" /> … … 128 133 <Compile Include="Operators\HookOperator.cs" /> 129 134 <Compile Include="Operators\IHookOperator.cs" /> 135 <EmbeddedResource Include="Optimization.Networks.KSPTSP\KSPTSPConnectorCode.cs" /> 130 136 <Compile Include="Optimization.Networks.KSPTSP\KSPTSPConnector.cs" /> 137 <EmbeddedResource Include="Optimization.Networks.KSPTSP\KSPTSPNetworkCode.cs" /> 131 138 <Compile Include="Optimization.Networks.KSPTSP\KSPTSPNetwork.cs" /> 132 139 <Compile Include="Optimization.Networks\AlgorithmNode.cs" /> -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPConnector.cs
r11530 r11564 23 23 using HeuristicLab.Core; 24 24 using HeuristicLab.Core.Networks; 25 using HeuristicLab.Data;26 using HeuristicLab.Encodings.BinaryVectorEncoding;27 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 using HeuristicLab.Problems.TravelingSalesman;29 using System.Linq;30 26 31 27 namespace HeuristicLab.Optimization.Networks.KSPTSP { 32 28 [Item("KSPTSPConnector", "A node of an optimization network which connects a KSP and a TSP.")] 33 29 [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 35 35 [StorableConstructor] 36 36 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) { } 61 41 62 42 public override IDeepCloneable Clone(Cloner cloner) { 63 43 return new KSPTSPConnector(this, cloner); 64 44 } 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 parameters93 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 coordinates100 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 TSP114 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 costs122 ((DoubleValue)kspMsg["Quality"]).Value = kspQuality - factor * tspQuality;123 kspMsg["TransportCosts"] = new DoubleValue(factor * tspQuality);124 kspMsg["Route"] = tspSolution;125 }126 45 } 127 46 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Optimization.Networks.KSPTSP/KSPTSPNetwork.cs
r11530 r11564 20 20 #endregion 21 21 22 using HeuristicLab.Algorithms.GeneticAlgorithm;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; 25 24 using HeuristicLab.Core.Networks; 26 using HeuristicLab.Data;27 using HeuristicLab.Encodings.BinaryVectorEncoding;28 using HeuristicLab.Operators;29 using HeuristicLab.Parameters;30 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.Knapsack; 32 using HeuristicLab.Problems.TravelingSalesman; 33 using System.Linq; 26 using System.Drawing; 34 27 35 28 namespace HeuristicLab.Optimization.Networks.KSPTSP { … … 37 30 [Creatable("Optimization Networks")] 38 31 [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 40 41 [StorableConstructor] 41 42 protected KSPTSPNetwork(bool deserializing) : base(deserializing) { } 42 43 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) { } 55 47 56 48 public override IDeepCloneable Clone(Cloner cloner) { 57 49 return new KSPTSPNetwork(this, cloner); 58 50 } 59 60 protected virtual void Initialize() {61 #region ParametersNode62 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 #endregion95 96 #region KSPNode97 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.Input105 });106 configPort.Parameters.Add(new PortParameter<IntArray>("Values") {107 Type = PortParameterType.Input108 });109 configPort.Parameters.Add(new PortParameter<IntArray>("Weights") {110 Type = PortParameterType.Input111 });112 113 var evaluatePort = new MessagePort("EvaluateRoute");114 kspNode.Ports.Add(evaluatePort);115 116 evaluatePort.Parameters.Add(new PortParameter<BinaryVector>("KnapsackSolution") {117 Type = PortParameterType.Output118 });119 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("Quality") {120 Type = PortParameterType.Output | PortParameterType.Input121 });122 evaluatePort.Parameters.Add(new PortParameter<DoubleValue>("TransportCosts") {123 Type = PortParameterType.Input124 });125 evaluatePort.Parameters.Add(new PortParameter<PathTSPTour>("Route") {126 Type = PortParameterType.Input127 });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 #endregion143 144 #region TSPNode145 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.Input153 });154 executePort.Parameters.Add(new PortParameter<DoubleValue>("BestQuality") {155 Type = PortParameterType.Output156 });157 executePort.Parameters.Add(new PortParameter<PathTSPTour>("Best TSP Solution") {158 Type = PortParameterType.Output159 });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 #endregion168 169 #region KSPTSPConnector170 var ksptspConnector = (INode)new KSPTSPConnector();171 Nodes.Add(ksptspConnector);172 #endregion173 174 #region Wire Ports175 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 #endregion183 }184 51 } 185 52 }
Note: See TracChangeset
for help on using the changeset viewer.