Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/01/10 08:52:32 (15 years ago)
Author:
gkronber
Message:

Created a specialized view for function library injectors which allows full configuration of the function library. #748 (FunctionLibraryView is empty)

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  
    3636    }
    3737
     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
    3849    public FunctionLibraryInjectorBase()
    3950      : base() {
    4051      AddVariableInfo(new VariableInfo(FUNCTIONLIBRARY, "Preconfigured default function library", typeof(FunctionLibrary), VariableKind.New));
     52      // create the default function library
     53      functionLibrary = CreateFunctionLibrary();
    4154    }
    4255
    4356    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));
    4558      return null;
    4659    }
     
    5972      }
    6073    }
     74
     75    public override IView CreateView() {
     76      return new FunctionLibraryInjectorView(this);
     77    }
    6178  }
    6279}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.Designer.cs

    r2701 r2728  
    6363      this.mutationListView = new System.Windows.Forms.ListView();
    6464      this.mutationVariableView = new HeuristicLab.Core.VariableView();
     65      this.testTabPage = new System.Windows.Forms.TabPage();
     66      this.outputTextBox = new System.Windows.Forms.TextBox();
    6567      this.tabControl.SuspendLayout();
    6668      this.functionsTabPage.SuspendLayout();
     
    7880      this.mutationSplitContainer.Panel2.SuspendLayout();
    7981      this.mutationSplitContainer.SuspendLayout();
     82      this.testTabPage.SuspendLayout();
    8083      this.SuspendLayout();
    8184      //
     
    8588      this.tabControl.Controls.Add(this.initializationTabPage);
    8689      this.tabControl.Controls.Add(this.mutationTabPage);
     90      this.tabControl.Controls.Add(this.testTabPage);
    8791      this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
    8892      this.tabControl.Location = new System.Drawing.Point(0, 0);
     
    9195      this.tabControl.Size = new System.Drawing.Size(677, 561);
    9296      this.tabControl.TabIndex = 1;
     97      this.tabControl.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl_Selected);
    9398      //
    9499      // functionsTabPage
     
    146151      this.functionsListView.View = System.Windows.Forms.View.List;
    147152      this.functionsListView.SelectedIndexChanged += new System.EventHandler(this.functionsListView_SelectedIndexChanged);
     153      this.functionsListView.KeyUp += new System.Windows.Forms.KeyEventHandler(this.functionsListView_KeyUp);
    148154      this.functionsListView.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.functionsListView_ItemDrag);
    149155      //
     
    299305      this.mutationVariableView.TabIndex = 0;
    300306      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;
    301327      //
    302328      // FunctionLibraryEditor
     
    322348      this.mutationSplitContainer.Panel2.ResumeLayout(false);
    323349      this.mutationSplitContainer.ResumeLayout(false);
     350      this.testTabPage.ResumeLayout(false);
     351      this.testTabPage.PerformLayout();
    324352      this.ResumeLayout(false);
    325353
     
    346374    private System.Windows.Forms.GroupBox availableFunctionsGroupBox;
    347375    private System.Windows.Forms.GroupBox functionDetailsGroupBox;
     376    private System.Windows.Forms.TabPage testTabPage;
     377    private System.Windows.Forms.TextBox outputTextBox;
    348378  }
    349379}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs

    r2701 r2728  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.GP.Interfaces;
     28using System.Text;
     29using HeuristicLab.Random;
    2830
    2931namespace HeuristicLab.GP {
     
    3537    }
    3638
    37     public FunctionLibraryEditor(FunctionLibrary library)
     39    public FunctionLibraryEditor()
    3840      : base() {
    3941      InitializeComponent();
     42    }
     43
     44    public FunctionLibraryEditor(FunctionLibrary library)
     45      : this() {
    4046      FunctionLibrary = library;
    4147    }
     
    4854    protected override void UpdateControls() {
    4955      base.UpdateControls();
     56      mutationListView.Items.Clear();
     57      initListView.Items.Clear();
    5058      functionsListView.Clear();
    5159      functionsComboBox.Items.Clear();
     
    92100      // delete from the end of the list
    93101      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;
    96110      }
    97111    }
     
    131145      }
    132146    }
     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
    133224  }
    134225}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs

    r2701 r2728  
    165165        if (e.Data.GetDataPresent("IFunction")) {
    166166          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) {
    167188          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            }
    170194          } else {
    171195            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++) {
    183196            foreach (var subFun in subFunctionsListBox.SelectedItems) {
    184197              function.RemoveAllowedSubFunction((IFunction)subFun, slot);
    185198            }
    186199          }
    187         } else {
    188           int slot = int.Parse(selectedSlot);
    189           foreach (var subFun in subFunctionsListBox.SelectedItems) {
    190             function.RemoveAllowedSubFunction((IFunction)subFun, slot);
    191           }
     200
    192201        }
    193 
     202      }
     203      finally {
     204        Cursor = Cursors.Default;
    194205      }
    195206    }
  • trunk/sources/HeuristicLab.GP/3.3/HeuristicLab.GP-3.3.csproj

    r2701 r2728  
    8989    <Compile Include="BaseClasses\Terminal.cs" />
    9090    <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>
    9197    <Compile Include="FunctionView.cs">
    9298      <SubType>UserControl</SubType>
     
    138144  </ItemGroup>
    139145  <ItemGroup>
     146    <EmbeddedResource Include="FunctionLibraryInjectorView.resx">
     147      <DependentUpon>FunctionLibraryInjectorView.cs</DependentUpon>
     148    </EmbeddedResource>
    140149    <EmbeddedResource Include="FunctionView.resx">
    141150      <DependentUpon>FunctionView.cs</DependentUpon>
Note: See TracChangeset for help on using the changeset viewer.