Changeset 2728 for trunk/sources/HeuristicLab.GP/3.3
- Timestamp:
- 02/01/10 08:52:32 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.GP/3.3
- Files:
-
- 3 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionLibraryInjectorBase.cs
r2616 r2728 36 36 } 37 37 38 private FunctionLibrary functionLibrary; 39 public FunctionLibrary FunctionLibrary { 40 get { 41 return functionLibrary; 42 } 43 set { 44 this.functionLibrary = value; 45 FireChanged(); 46 } 47 } 48 38 49 public FunctionLibraryInjectorBase() 39 50 : base() { 40 51 AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default function library", typeof(FunctionLibrary), VariableKind.New)); 52 // create the default function library 53 functionLibrary = CreateFunctionLibrary(); 41 54 } 42 55 43 56 public override IOperation Apply(IScope scope) { 44 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), CreateFunctionLibrary()));57 scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName(FUNCTIONLIBRARY), functionLibrary)); 45 58 return null; 46 59 } … … 59 72 } 60 73 } 74 75 public override IView CreateView() { 76 return new FunctionLibraryInjectorView(this); 77 } 61 78 } 62 79 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.Designer.cs
r2701 r2728 63 63 this.mutationListView = new System.Windows.Forms.ListView(); 64 64 this.mutationVariableView = new HeuristicLab.Core.VariableView(); 65 this.testTabPage = new System.Windows.Forms.TabPage(); 66 this.outputTextBox = new System.Windows.Forms.TextBox(); 65 67 this.tabControl.SuspendLayout(); 66 68 this.functionsTabPage.SuspendLayout(); … … 78 80 this.mutationSplitContainer.Panel2.SuspendLayout(); 79 81 this.mutationSplitContainer.SuspendLayout(); 82 this.testTabPage.SuspendLayout(); 80 83 this.SuspendLayout(); 81 84 // … … 85 88 this.tabControl.Controls.Add(this.initializationTabPage); 86 89 this.tabControl.Controls.Add(this.mutationTabPage); 90 this.tabControl.Controls.Add(this.testTabPage); 87 91 this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill; 88 92 this.tabControl.Location = new System.Drawing.Point(0, 0); … … 91 95 this.tabControl.Size = new System.Drawing.Size(677, 561); 92 96 this.tabControl.TabIndex = 1; 97 this.tabControl.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl_Selected); 93 98 // 94 99 // functionsTabPage … … 146 151 this.functionsListView.View = System.Windows.Forms.View.List; 147 152 this.functionsListView.SelectedIndexChanged += new System.EventHandler(this.functionsListView_SelectedIndexChanged); 153 this.functionsListView.KeyUp += new System.Windows.Forms.KeyEventHandler(this.functionsListView_KeyUp); 148 154 this.functionsListView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.functionsListView_ItemDrag); 149 155 // … … 299 305 this.mutationVariableView.TabIndex = 0; 300 306 this.mutationVariableView.Variable = null; 307 // 308 // testTabPage 309 // 310 this.testTabPage.Controls.Add(this.outputTextBox); 311 this.testTabPage.Location = new System.Drawing.Point(4, 22); 312 this.testTabPage.Name = "testTabPage"; 313 this.testTabPage.Padding = new System.Windows.Forms.Padding(3); 314 this.testTabPage.Size = new System.Drawing.Size(669, 535); 315 this.testTabPage.TabIndex = 3; 316 this.testTabPage.Text = "Test function library"; 317 this.testTabPage.UseVisualStyleBackColor = true; 318 // 319 // outputTextBox 320 // 321 this.outputTextBox.Dock = System.Windows.Forms.DockStyle.Fill; 322 this.outputTextBox.Location = new System.Drawing.Point(3, 3); 323 this.outputTextBox.Multiline = true; 324 this.outputTextBox.Name = "outputTextBox"; 325 this.outputTextBox.Size = new System.Drawing.Size(663, 529); 326 this.outputTextBox.TabIndex = 0; 301 327 // 302 328 // FunctionLibraryEditor … … 322 348 this.mutationSplitContainer.Panel2.ResumeLayout(false); 323 349 this.mutationSplitContainer.ResumeLayout(false); 350 this.testTabPage.ResumeLayout(false); 351 this.testTabPage.PerformLayout(); 324 352 this.ResumeLayout(false); 325 353 … … 346 374 private System.Windows.Forms.GroupBox availableFunctionsGroupBox; 347 375 private System.Windows.Forms.GroupBox functionDetailsGroupBox; 376 private System.Windows.Forms.TabPage testTabPage; 377 private System.Windows.Forms.TextBox outputTextBox; 348 378 } 349 379 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs
r2701 r2728 26 26 using HeuristicLab.Core; 27 27 using HeuristicLab.GP.Interfaces; 28 using System.Text; 29 using HeuristicLab.Random; 28 30 29 31 namespace HeuristicLab.GP { … … 35 37 } 36 38 37 public FunctionLibraryEditor( FunctionLibrary library)39 public FunctionLibraryEditor() 38 40 : base() { 39 41 InitializeComponent(); 42 } 43 44 public FunctionLibraryEditor(FunctionLibrary library) 45 : this() { 40 46 FunctionLibrary = library; 41 47 } … … 48 54 protected override void UpdateControls() { 49 55 base.UpdateControls(); 56 mutationListView.Items.Clear(); 57 initListView.Items.Clear(); 50 58 functionsListView.Clear(); 51 59 functionsComboBox.Items.Clear(); … … 92 100 // delete from the end of the list 93 101 List<int> removeIndices = functionsListView.SelectedIndices.OfType<int>().OrderBy(x => 1.0 / x).ToList(); 94 foreach (int selectedIndex in removeIndices) { 95 FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag); 102 try { 103 Cursor = Cursors.WaitCursor; 104 foreach (int selectedIndex in removeIndices) { 105 FunctionLibrary.RemoveFunction((IFunction)functionsListView.Items[selectedIndex].Tag); 106 } 107 } 108 finally { 109 Cursor = Cursors.Default; 96 110 } 97 111 } … … 131 145 } 132 146 } 147 148 private void functionsListView_KeyUp(object sender, KeyEventArgs e) { 149 if (e.KeyCode == Keys.Delete && functionsListView.SelectedItems.Count > 0) { 150 List<IFunction> removedFunctions = new List<IFunction>(from x in functionsListView.SelectedItems.OfType<ListViewItem>() 151 select (IFunction)x.Tag); 152 try { 153 Cursor = Cursors.WaitCursor; 154 foreach (var fun in removedFunctions) { 155 FunctionLibrary.RemoveFunction(fun); 156 } 157 } 158 finally { 159 Cursor = Cursors.Default; 160 } 161 } 162 } 163 164 private void tabControl_Selected(object sender, TabControlEventArgs e) { 165 if (e.TabPage == testTabPage) { 166 outputTextBox.Text = TestFunctionLibrary(); 167 } 168 } 169 170 private string TestFunctionLibrary() { 171 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(); 177 } 178 179 private string CalculateFunctionFrequencies(IFunctionTree[] randomTrees) { 180 Dictionary<IFunction, int> occurances = new Dictionary<IFunction, int>(); 181 double n = 0.0; 182 for (int i = 0; i < randomTrees.Length; i++) { 183 foreach (var node in FunctionTreeIterator.IteratePrefix(randomTrees[i])) { 184 if (node.SubTrees.Count > 0) { 185 if (!occurances.ContainsKey(node.Function)) 186 occurances[node.Function] = 0; 187 occurances[node.Function]++; 188 n++; 189 } 190 } 191 } 192 StringBuilder strBuilder = new StringBuilder(); 193 foreach (var function in occurances.Keys) { 194 strBuilder.Append(Environment.NewLine); 195 strBuilder.Append(function.Name); strBuilder.Append(": "); 196 strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n); 197 } 198 return strBuilder.ToString(); 199 } 200 201 private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) { 202 int maxHeight = 10; 203 int maxTries = 100; 204 IFunctionTree[] randomTrees = new IFunctionTree[popSize]; 205 MersenneTwister twister = new MersenneTwister(); 206 for (int i = 0; i < randomTrees.Length; i++) { 207 int treeSize = twister.Next(minSize, maxSize); 208 IFunctionTree root; 209 int tries = 0; 210 TreeGardener gardener = new TreeGardener(twister, FunctionLibrary); 211 do { 212 root = gardener.PTC2(treeSize, maxSize); 213 if (tries++ >= maxTries) { 214 // try a different size 215 treeSize = twister.Next(minSize, maxSize); 216 tries = 0; 217 } 218 } while (root.GetSize() > maxSize || root.GetHeight() > maxHeight); 219 randomTrees[i] = root; 220 } 221 return randomTrees; 222 } 223 133 224 } 134 225 } -
trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs
r2701 r2728 165 165 if (e.Data.GetDataPresent("IFunction")) { 166 166 IFunction fun = (IFunction)e.Data.GetData("IFunction"); 167 try { 168 Cursor = Cursors.WaitCursor; 169 if (selectedSlot == ALL_SLOTS) { 170 for (int slot = 0; slot < function.MaxSubTrees; slot++) 171 function.AddAllowedSubFunction(fun, slot); 172 } else { 173 int slot = int.Parse(selectedSlot); 174 function.AddAllowedSubFunction(fun, slot); 175 } 176 } 177 finally { 178 Cursor = Cursors.Default; 179 } 180 } 181 } 182 } 183 184 private void subFunctionsListBox_KeyUp(object sender, KeyEventArgs e) { 185 try { 186 Cursor = Cursors.WaitCursor; 187 if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) { 167 188 if (selectedSlot == ALL_SLOTS) { 168 for (int slot = 0; slot < function.MaxSubTrees; slot++) 169 function.AddAllowedSubFunction(fun, slot); 189 for (int slot = 0; slot < function.MaxSubTrees; slot++) { 190 foreach (var subFun in subFunctionsListBox.SelectedItems) { 191 function.RemoveAllowedSubFunction((IFunction)subFun, slot); 192 } 193 } 170 194 } else { 171 195 int slot = int.Parse(selectedSlot); 172 function.AddAllowedSubFunction(fun, slot);173 }174 }175 }176 }177 178 private void subFunctionsListBox_KeyUp(object sender, KeyEventArgs e) {179 if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) {180 181 if (selectedSlot == ALL_SLOTS) {182 for (int slot = 0; slot < function.MaxSubTrees; slot++) {183 196 foreach (var subFun in subFunctionsListBox.SelectedItems) { 184 197 function.RemoveAllowedSubFunction((IFunction)subFun, slot); 185 198 } 186 199 } 187 } else { 188 int slot = int.Parse(selectedSlot); 189 foreach (var subFun in subFunctionsListBox.SelectedItems) { 190 function.RemoveAllowedSubFunction((IFunction)subFun, slot); 191 } 200 192 201 } 193 202 } 203 finally { 204 Cursor = Cursors.Default; 194 205 } 195 206 } -
trunk/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj
r2701 r2728 89 89 <Compile Include="BaseClasses\Terminal.cs" /> 90 90 <Compile Include="BaseClasses\UnaryFunction.cs" /> 91 <Compile Include="FunctionLibraryInjectorView.cs"> 92 <SubType>UserControl</SubType> 93 </Compile> 94 <Compile Include="FunctionLibraryInjectorView.Designer.cs"> 95 <DependentUpon>FunctionLibraryInjectorView.cs</DependentUpon> 96 </Compile> 91 97 <Compile Include="FunctionView.cs"> 92 98 <SubType>UserControl</SubType> … … 138 144 </ItemGroup> 139 145 <ItemGroup> 146 <EmbeddedResource Include="FunctionLibraryInjectorView.resx"> 147 <DependentUpon>FunctionLibraryInjectorView.cs</DependentUpon> 148 </EmbeddedResource> 140 149 <EmbeddedResource Include="FunctionView.resx"> 141 150 <DependentUpon>FunctionView.cs</DependentUpon>
Note: See TracChangeset
for help on using the changeset viewer.