- Timestamp:
- 02/01/10 08:52:32 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note: See TracChangeset
for help on using the changeset viewer.