Changeset 2729
- Timestamp:
- 02/01/10 16:40:24 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.GP/3.3
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/3.3/BaseClasses/Function.cs
r2700 r2729 227 227 public override object Clone(IDictionary<Guid, object> clonedObjects) { 228 228 Function clone = (Function)base.Clone(clonedObjects); 229 clone.initializer = (IOperator)Auxiliary.Clone(initializer, clonedObjects); 230 clone.manipulator = (IOperator)Auxiliary.Clone(manipulator, clonedObjects); 231 clone.maxArity = maxArity; 232 clone.minArity = minArity; 233 clone.minTreeHeight = minTreeHeight; 234 clone.minTreeSize = minTreeSize; 235 clone.tickets = tickets; 229 if (initializer != null) clone.initializer = (IOperator)Auxiliary.Clone(initializer, clonedObjects); 230 else clone.initializer = null; 231 if (manipulator != null) clone.manipulator = (IOperator)Auxiliary.Clone(manipulator, clonedObjects); 232 else clone.manipulator = null; 233 clone.MaxSubTrees = maxArity; 234 clone.MinSubTrees = minArity; 235 clone.Tickets = tickets; 236 clone.allowedSubFunctions.Clear(); 236 237 for (int i = 0; i < MaxSubTrees; i++) { 237 238 var allowedSubFunctionsForSlot = new List<IFunction>(); … … 252 253 node.Attributes.Append(minSubTreesAttr); 253 254 node.Attributes.Append(maxSubTreesAttr); 255 if (initializer != null) 256 node.AppendChild(PersistenceManager.Persist("Initializer", initializer, document, persistedObjects)); 257 if (manipulator != null) 258 node.AppendChild(PersistenceManager.Persist("Manipulator", manipulator, document, persistedObjects)); 254 259 for (int i = 0; i < MaxSubTrees; i++) { 255 260 XmlNode slotNode = document.CreateElement("AllowedSubFunctions"); … … 269 274 MinSubTrees = XmlConvert.ToInt32(node.Attributes["MinSubTrees"].Value); 270 275 MaxSubTrees = XmlConvert.ToInt32(node.Attributes["MaxSubTrees"].Value); 276 if (node.SelectSingleNode("Initializer") != null) { 277 initializer = (IOperator)PersistenceManager.Restore(node.SelectSingleNode("Initializer"), restoredObjects); 278 } 279 if (node.SelectSingleNode("Manipulator") != null) { 280 manipulator = (IOperator)PersistenceManager.Restore(node.SelectSingleNode("Manipulator"), restoredObjects); 281 } 282 foreach (var subFunctionsList in allowedSubFunctions) subFunctionsList.Clear(); 271 283 foreach (XmlNode allowedSubFunctionsNode in node.SelectNodes("AllowedSubFunctions")) { 272 284 int slot = XmlConvert.ToInt32(allowedSubFunctionsNode.Attributes["Slot"].Value); -
trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionLibraryInjectorBase.cs
r2728 r2729 76 76 return new FunctionLibraryInjectorView(this); 77 77 } 78 79 #region persistence 80 public override object Clone(IDictionary<Guid, object> clonedObjects) { 81 FunctionLibraryInjectorBase clone = (FunctionLibraryInjectorBase)base.Clone(clonedObjects); 82 clone.functionLibrary = (FunctionLibrary)Auxiliary.Clone(functionLibrary, clonedObjects); 83 return clone; 84 } 85 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 86 XmlNode node = base.GetXmlNode(name, document, persistedObjects); 87 node.AppendChild(PersistenceManager.Persist("FunctionLibrary", FunctionLibrary, document, persistedObjects)); 88 return node; 89 } 90 public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) { 91 base.Populate(node, restoredObjects); 92 functionLibrary = (FunctionLibrary)PersistenceManager.Restore(node.SelectSingleNode("FunctionLibrary"), restoredObjects); 93 } 94 #endregion 78 95 } 79 96 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibrary.cs
r2701 r2729 64 64 } 65 65 66 #region persistence 66 67 public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) { 67 68 XmlNode node = base.GetXmlNode(name, document, persistedObjects); … … 79 80 } 80 81 82 public override object Clone(IDictionary<Guid, object> clonedObjects) { 83 FunctionLibrary clone = (FunctionLibrary)base.Clone(clonedObjects); 84 foreach (var function in functions) { 85 clone.AddFunction((Function)Auxiliary.Clone(function, clonedObjects)); 86 } 87 return clone; 88 } 89 #endregion 90 81 91 public override IView CreateView() { 82 92 return new FunctionLibraryEditor(this); -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.Designer.cs
r2728 r2729 64 64 this.mutationVariableView = new HeuristicLab.Core.VariableView(); 65 65 this.testTabPage = new System.Windows.Forms.TabPage(); 66 this.testButton = new System.Windows.Forms.Button(); 66 67 this.outputTextBox = new System.Windows.Forms.TextBox(); 67 68 this.tabControl.SuspendLayout(); … … 95 96 this.tabControl.Size = new System.Drawing.Size(677, 561); 96 97 this.tabControl.TabIndex = 1; 97 this.tabControl.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl_Selected);98 98 // 99 99 // functionsTabPage … … 144 144 | System.Windows.Forms.AnchorStyles.Right))); 145 145 this.functionsListView.Location = new System.Drawing.Point(6, 19); 146 this.functionsListView.MultiSelect = false;147 146 this.functionsListView.Name = "functionsListView"; 148 147 this.functionsListView.Size = new System.Drawing.Size(218, 475); … … 308 307 // testTabPage 309 308 // 309 this.testTabPage.Controls.Add(this.testButton); 310 310 this.testTabPage.Controls.Add(this.outputTextBox); 311 311 this.testTabPage.Location = new System.Drawing.Point(4, 22); … … 317 317 this.testTabPage.UseVisualStyleBackColor = true; 318 318 // 319 // testButton 320 // 321 this.testButton.Location = new System.Drawing.Point(6, 6); 322 this.testButton.Name = "testButton"; 323 this.testButton.Size = new System.Drawing.Size(75, 23); 324 this.testButton.TabIndex = 1; 325 this.testButton.Text = "Run test"; 326 this.testButton.UseVisualStyleBackColor = true; 327 this.testButton.Click += new System.EventHandler(this.testButton_Click); 328 // 319 329 // outputTextBox 320 330 // 321 this.outputTextBox.Dock = System.Windows.Forms.DockStyle.Fill; 322 this.outputTextBox.Location = new System.Drawing.Point(3, 3); 331 this.outputTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 332 | System.Windows.Forms.AnchorStyles.Left) 333 | System.Windows.Forms.AnchorStyles.Right))); 334 this.outputTextBox.Location = new System.Drawing.Point(3, 35); 323 335 this.outputTextBox.Multiline = true; 324 336 this.outputTextBox.Name = "outputTextBox"; 325 this.outputTextBox.Size = new System.Drawing.Size(663, 529); 337 this.outputTextBox.ReadOnly = true; 338 this.outputTextBox.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 339 this.outputTextBox.Size = new System.Drawing.Size(663, 497); 326 340 this.outputTextBox.TabIndex = 0; 327 341 // … … 376 390 private System.Windows.Forms.TabPage testTabPage; 377 391 private System.Windows.Forms.TextBox outputTextBox; 392 private System.Windows.Forms.Button testButton; 378 393 } 379 394 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs
r2728 r2729 161 161 } 162 162 } 163 164 private void tabControl_Selected(object sender, TabControlEventArgs e) { 165 if (e.TabPage == testTabPage) { 166 outputTextBox.Text = TestFunctionLibrary(); 167 } 168 } 169 163 #region fun lib test 170 164 private string TestFunctionLibrary() { 171 165 int n = 1000; 172 IFunctionTree[] randomTrees = CreateRandomTrees(n, 1, 100); 173 174 StringBuilder builder = new StringBuilder(); 175 builder.AppendLine(CalculateFunctionFrequencies(randomTrees)); 176 return builder.ToString(); 166 try { 167 IFunctionTree[] randomTrees = CreateRandomTrees(n, 1, 100); 168 StringBuilder builder = new StringBuilder(); 169 builder.AppendLine("Function symbol frequencies:"); 170 builder.AppendLine(CalculateFunctionFrequencies(randomTrees)); 171 builder.AppendLine("-----------------------------------------"); 172 builder.AppendLine("Terminal symbol frequencies:"); 173 builder.AppendLine(CalculateTerminalFrequencies(randomTrees)); 174 builder.AppendLine("-----------------------------------------"); 175 builder.AppendLine("Function arity frequencies:"); 176 builder.AppendLine(CalculateFunctionArityFrequencies(randomTrees)); 177 builder.AppendLine("-----------------------------------------"); 178 builder.AppendLine("Tree size frequencies:"); 179 builder.AppendLine(CalculateTreeSizeFrequencies(randomTrees)); 180 builder.AppendLine("-----------------------------------------"); 181 builder.AppendLine("Tree height frequencies:"); 182 builder.AppendLine(CalculateTreeHeightFrequencies(randomTrees)); 183 return builder.ToString(); 184 } 185 catch (ArgumentException ex) { 186 return "Could not create random trees:" + Environment.NewLine + ex.Message + Environment.NewLine + ex.StackTrace; 187 } 177 188 } 178 189 … … 199 210 } 200 211 212 public string CalculateTreeSizeFrequencies(IFunctionTree[] randomTrees) { 213 int[] histogram = new int[105 / 5]; 214 for (int i = 0; i < randomTrees.Length; i++) { 215 histogram[randomTrees[i].GetSize() / 5]++; 216 } 217 StringBuilder strBuilder = new StringBuilder(); 218 for (int i = 0; i < histogram.Length; i++) { 219 strBuilder.Append(Environment.NewLine); 220 strBuilder.Append("< "); strBuilder.Append((i + 1) * 5); 221 strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length); 222 } 223 return strBuilder.ToString(); 224 } 225 226 public string CalculateTreeHeightFrequencies(IFunctionTree[] randomTrees) { 227 int[] histogram = new int[100]; 228 for (int i = 0; i < randomTrees.Length; i++) { 229 histogram[randomTrees[i].GetHeight()]++; 230 } 231 StringBuilder strBuilder = new StringBuilder(); 232 for (int i = 0; i < histogram.Length; i++) { 233 strBuilder.Append(Environment.NewLine); 234 strBuilder.Append("< "); strBuilder.Append((i + 1)); 235 strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Length); 236 } 237 return strBuilder.ToString(); 238 } 239 240 public string CalculateFunctionArityFrequencies(IFunctionTree[] randomTrees) { 241 Dictionary<int, int> occurances = new Dictionary<int, int>(); 242 double n = 0.0; 243 for (int i = 0; i < randomTrees.Length; i++) { 244 foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) { 245 if (!occurances.ContainsKey(node.SubTrees.Count)) 246 occurances[node.SubTrees.Count] = 0; 247 occurances[node.SubTrees.Count]++; 248 n++; 249 } 250 } 251 StringBuilder strBuilder = new StringBuilder(); 252 foreach (var arity in occurances.Keys) { 253 strBuilder.Append(Environment.NewLine); 254 strBuilder.Append(arity); strBuilder.Append(": "); 255 strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n); 256 } 257 return strBuilder.ToString(); 258 } 259 260 public string CalculateTerminalFrequencies(IFunctionTree[] randomTrees) { 261 Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>(); 262 double n = 0.0; 263 for (int i = 0; i < randomTrees.Length; i++) { 264 foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) { 265 if (node.SubTrees.Count == 0) { 266 if (!occurances.ContainsKey(node.Function)) 267 occurances[node.Function] = 0; 268 occurances[node.Function]++; 269 n++; 270 } 271 } 272 } 273 StringBuilder strBuilder = new StringBuilder(); 274 foreach (var function in occurances.Keys) { 275 strBuilder.Append(Environment.NewLine); 276 strBuilder.Append(function.Name); strBuilder.Append(": "); 277 strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n); 278 } 279 return strBuilder.ToString(); 280 } 281 201 282 private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) { 202 283 int maxHeight = 10; … … 206 287 for (int i = 0; i < randomTrees.Length; i++) { 207 288 int treeSize = twister.Next(minSize, maxSize); 208 IFunctionTree root ;289 IFunctionTree root = null; 209 290 int tries = 0; 210 291 TreeGardener gardener = new TreeGardener(twister, FunctionLibrary); 211 292 do { 212 root = gardener.PTC2(treeSize, maxSize); 293 try { 294 root = gardener.PTC2(treeSize, maxSize); 295 } 296 catch (ArgumentException) { 297 // try a different size 298 treeSize = twister.Next(minSize, maxSize); 299 tries = 0; 300 } 213 301 if (tries++ >= maxTries) { 214 302 // try a different size … … 216 304 tries = 0; 217 305 } 218 } while (root .GetSize() > maxSize || root.GetHeight() > maxHeight);306 } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight); 219 307 randomTrees[i] = root; 220 308 } … … 222 310 } 223 311 312 private void testButton_Click(object sender, EventArgs e) { 313 try { 314 Cursor = Cursors.WaitCursor; 315 outputTextBox.Text = TestFunctionLibrary(); 316 } 317 finally { 318 Cursor = Cursors.Default; 319 } 320 } 321 #endregion 224 322 } 225 323 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryInjectorView.Designer.cs
r2728 r2729 46 46 this.variableInfosTabPage.Size = new System.Drawing.Size(470, 411); 47 47 // 48 // variablesTabPage49 //50 this.variablesTabPage.Size = new System.Drawing.Size(470, 411);51 //52 // constrainedItemBaseView53 //54 this.constrainedItemBaseView.Size = new System.Drawing.Size(464, 405);55 //56 48 // operatorBaseVariableInfosView 57 49 // 58 50 this.operatorBaseVariableInfosView.Size = new System.Drawing.Size(464, 405); 59 //60 // operatorBaseVariablesView61 //62 this.operatorBaseVariablesView.Size = new System.Drawing.Size(464, 405);63 51 // 64 52 // loadButton … … 69 57 this.loadButton.Size = new System.Drawing.Size(75, 23); 70 58 this.loadButton.TabIndex = 0; 71 this.loadButton.Text = " Load...";59 this.loadButton.Text = "Import..."; 72 60 this.loadButton.UseVisualStyleBackColor = true; 73 61 this.loadButton.Click += new System.EventHandler(this.loadButton_Click); … … 80 68 this.saveButton.Size = new System.Drawing.Size(75, 23); 81 69 this.saveButton.TabIndex = 2; 82 this.saveButton.Text = " Save...";70 this.saveButton.Text = "Export..."; 83 71 this.saveButton.UseVisualStyleBackColor = true; 84 72 this.saveButton.Click += new System.EventHandler(this.saveButton_Click); -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryInjectorView.cs
r2728 r2729 37 37 private SaveFileDialog saveFileDialog; 38 38 39 public FunctionLibraryInjectorView() { 39 public FunctionLibraryInjectorView() 40 : base() { 40 41 InitializeComponent(); 42 CreateFileDialogs(); 43 } 44 45 public FunctionLibraryInjectorView(FunctionLibraryInjectorBase functionLibraryInjector) 46 : base(functionLibraryInjector) { 47 InitializeComponent(); 48 this.functionLibraryInjector = functionLibraryInjector; 49 functionLibraryEditor.FunctionLibrary = functionLibraryInjector.FunctionLibrary; 50 51 CreateFileDialogs(); 52 } 53 54 private void CreateFileDialogs() { 41 55 openFileDialog = new OpenFileDialog(); 42 56 openFileDialog.AddExtension = true; 43 57 openFileDialog.DefaultExt = ".hl"; 44 openFileDialog.Filter = "HeuristicLab Dateien (*.hl) | *.hl | Alle Dateien (*.*) |*.*";58 openFileDialog.Filter = "HeuristicLab Dateien (*.hl) |*.hl| Alle Dateien (*.*) |*.*"; 45 59 openFileDialog.Multiselect = false; 46 60 saveFileDialog = new SaveFileDialog(); 47 61 saveFileDialog.AddExtension = true; 48 62 saveFileDialog.DefaultExt = ".hl"; 49 saveFileDialog.Filter = "HeuristicLab Dateien (*.hl) | *.hl | Alle Dateien (*.*) | *.*"; 50 } 51 52 public FunctionLibraryInjectorView(FunctionLibraryInjectorBase functionLibraryInjector) 53 : this() { 54 this.functionLibraryInjector = functionLibraryInjector; 55 functionLibraryEditor.FunctionLibrary = functionLibraryInjector.FunctionLibrary; 63 saveFileDialog.Filter = "HeuristicLab Dateien (*.hl) |*.hl| Alle Dateien (*.*) |*.*"; 56 64 } 57 65 -
trunk/sources/HeuristicLab.GP/3.3/FunctionView.Designer.cs
r2701 r2729 149 149 this.subFunctionsListBox.Location = new System.Drawing.Point(6, 46); 150 150 this.subFunctionsListBox.Name = "subFunctionsListBox"; 151 this.subFunctionsListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; 151 152 this.subFunctionsListBox.Size = new System.Drawing.Size(405, 225); 152 153 this.subFunctionsListBox.TabIndex = 12; -
trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs
r2728 r2729 187 187 if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) { 188 188 if (selectedSlot == ALL_SLOTS) { 189 List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>()); 189 190 for (int slot = 0; slot < function.MaxSubTrees; slot++) { 190 foreach (var subFun in subFunctionsListBox.SelectedItems) {191 foreach (var subFun in removedSubFunctions) { 191 192 function.RemoveAllowedSubFunction((IFunction)subFun, slot); 192 193 } … … 194 195 } else { 195 196 int slot = int.Parse(selectedSlot); 196 foreach (var subFun in subFunctionsListBox.SelectedItems) { 197 function.RemoveAllowedSubFunction((IFunction)subFun, slot); 197 List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>()); 198 foreach (var subFun in removedSubFunctions) { 199 function.RemoveAllowedSubFunction(subFun, slot); 198 200 } 199 201 }
Note: See TracChangeset
for help on using the changeset viewer.