Changeset 2729


Ignore:
Timestamp:
02/01/10 16:40:24 (10 years ago)
Author:
gkronber
Message:

Fixed problems in persistence and cloning of functions and function libraries. Added test-functionality to editor for function libraries. Fixed bugs in editor for function libraries. #748 (FunctionLibraryView is empty)

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  
    227227    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    228228      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();
    236237      for (int i = 0; i < MaxSubTrees; i++) {
    237238        var allowedSubFunctionsForSlot = new List<IFunction>();
     
    252253      node.Attributes.Append(minSubTreesAttr);
    253254      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));
    254259      for (int i = 0; i < MaxSubTrees; i++) {
    255260        XmlNode slotNode = document.CreateElement("AllowedSubFunctions");
     
    269274      MinSubTrees = XmlConvert.ToInt32(node.Attributes["MinSubTrees"].Value);
    270275      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();
    271283      foreach (XmlNode allowedSubFunctionsNode in node.SelectNodes("AllowedSubFunctions")) {
    272284        int slot = XmlConvert.ToInt32(allowedSubFunctionsNode.Attributes["Slot"].Value);
  • trunk/sources/HeuristicLab.GP/3.3/BaseClasses/FunctionLibraryInjectorBase.cs

    r2728 r2729  
    7676      return new FunctionLibraryInjectorView(this);
    7777    }
     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
    7895  }
    7996}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibrary.cs

    r2701 r2729  
    6464    }
    6565
     66    #region persistence
    6667    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    6768      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     
    7980    }
    8081
     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
    8191    public override IView CreateView() {
    8292      return new FunctionLibraryEditor(this);
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.Designer.cs

    r2728 r2729  
    6464      this.mutationVariableView = new HeuristicLab.Core.VariableView();
    6565      this.testTabPage = new System.Windows.Forms.TabPage();
     66      this.testButton = new System.Windows.Forms.Button();
    6667      this.outputTextBox = new System.Windows.Forms.TextBox();
    6768      this.tabControl.SuspendLayout();
     
    9596      this.tabControl.Size = new System.Drawing.Size(677, 561);
    9697      this.tabControl.TabIndex = 1;
    97       this.tabControl.Selected += new System.Windows.Forms.TabControlEventHandler(this.tabControl_Selected);
    9898      //
    9999      // functionsTabPage
     
    144144                  | System.Windows.Forms.AnchorStyles.Right)));
    145145      this.functionsListView.Location = new System.Drawing.Point(6, 19);
    146       this.functionsListView.MultiSelect = false;
    147146      this.functionsListView.Name = "functionsListView";
    148147      this.functionsListView.Size = new System.Drawing.Size(218, 475);
     
    308307      // testTabPage
    309308      //
     309      this.testTabPage.Controls.Add(this.testButton);
    310310      this.testTabPage.Controls.Add(this.outputTextBox);
    311311      this.testTabPage.Location = new System.Drawing.Point(4, 22);
     
    317317      this.testTabPage.UseVisualStyleBackColor = true;
    318318      //
     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      //
    319329      // outputTextBox
    320330      //
    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);
    323335      this.outputTextBox.Multiline = true;
    324336      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);
    326340      this.outputTextBox.TabIndex = 0;
    327341      //
     
    376390    private System.Windows.Forms.TabPage testTabPage;
    377391    private System.Windows.Forms.TextBox outputTextBox;
     392    private System.Windows.Forms.Button testButton;
    378393  }
    379394}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryEditor.cs

    r2728 r2729  
    161161      }
    162162    }
    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
    170164    private string TestFunctionLibrary() {
    171165      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      }
    177188    }
    178189
     
    199210    }
    200211
     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
    201282    private IFunctionTree[] CreateRandomTrees(int popSize, int minSize, int maxSize) {
    202283      int maxHeight = 10;
     
    206287      for (int i = 0; i < randomTrees.Length; i++) {
    207288        int treeSize = twister.Next(minSize, maxSize);
    208         IFunctionTree root;
     289        IFunctionTree root = null;
    209290        int tries = 0;
    210291        TreeGardener gardener = new TreeGardener(twister, FunctionLibrary);
    211292        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          }
    213301          if (tries++ >= maxTries) {
    214302            // try a different size
     
    216304            tries = 0;
    217305          }
    218         } while (root.GetSize() > maxSize || root.GetHeight() > maxHeight);
     306        } while (root == null || root.GetSize() > maxSize || root.GetHeight() > maxHeight);
    219307        randomTrees[i] = root;
    220308      }
     
    222310    }
    223311
     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
    224322  }
    225323}
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryInjectorView.Designer.cs

    r2728 r2729  
    4646      this.variableInfosTabPage.Size = new System.Drawing.Size(470, 411);
    4747      //
    48       // variablesTabPage
    49       //
    50       this.variablesTabPage.Size = new System.Drawing.Size(470, 411);
    51       //
    52       // constrainedItemBaseView
    53       //
    54       this.constrainedItemBaseView.Size = new System.Drawing.Size(464, 405);
    55       //
    5648      // operatorBaseVariableInfosView
    5749      //
    5850      this.operatorBaseVariableInfosView.Size = new System.Drawing.Size(464, 405);
    59       //
    60       // operatorBaseVariablesView
    61       //
    62       this.operatorBaseVariablesView.Size = new System.Drawing.Size(464, 405);
    6351      //
    6452      // loadButton
     
    6957      this.loadButton.Size = new System.Drawing.Size(75, 23);
    7058      this.loadButton.TabIndex = 0;
    71       this.loadButton.Text = "Load...";
     59      this.loadButton.Text = "Import...";
    7260      this.loadButton.UseVisualStyleBackColor = true;
    7361      this.loadButton.Click += new System.EventHandler(this.loadButton_Click);
     
    8068      this.saveButton.Size = new System.Drawing.Size(75, 23);
    8169      this.saveButton.TabIndex = 2;
    82       this.saveButton.Text = "Save...";
     70      this.saveButton.Text = "Export...";
    8371      this.saveButton.UseVisualStyleBackColor = true;
    8472      this.saveButton.Click += new System.EventHandler(this.saveButton_Click);
  • trunk/sources/HeuristicLab.GP/3.3/FunctionLibraryInjectorView.cs

    r2728 r2729  
    3737    private SaveFileDialog saveFileDialog;
    3838
    39     public FunctionLibraryInjectorView() {
     39    public FunctionLibraryInjectorView()
     40      : base() {
    4041      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() {
    4155      openFileDialog = new OpenFileDialog();
    4256      openFileDialog.AddExtension = true;
    4357      openFileDialog.DefaultExt = ".hl";
    44       openFileDialog.Filter = "HeuristicLab Dateien (*.hl) | *.hl | Alle Dateien (*.*) | *.*";
     58      openFileDialog.Filter = "HeuristicLab Dateien (*.hl) |*.hl| Alle Dateien (*.*) |*.*";
    4559      openFileDialog.Multiselect = false;
    4660      saveFileDialog = new SaveFileDialog();
    4761      saveFileDialog.AddExtension = true;
    4862      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 (*.*) |*.*";
    5664    }
    5765
  • trunk/sources/HeuristicLab.GP/3.3/FunctionView.Designer.cs

    r2701 r2729  
    149149      this.subFunctionsListBox.Location = new System.Drawing.Point(6, 46);
    150150      this.subFunctionsListBox.Name = "subFunctionsListBox";
     151      this.subFunctionsListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
    151152      this.subFunctionsListBox.Size = new System.Drawing.Size(405, 225);
    152153      this.subFunctionsListBox.TabIndex = 12;
  • trunk/sources/HeuristicLab.GP/3.3/FunctionView.cs

    r2728 r2729  
    187187        if (subFunctionsListBox.SelectedItems.Count > 0 && e.KeyCode == Keys.Delete) {
    188188          if (selectedSlot == ALL_SLOTS) {
     189            List<IFunction> removedSubFunctions = new List<IFunction>(subFunctionsListBox.SelectedItems.Cast<IFunction>());
    189190            for (int slot = 0; slot < function.MaxSubTrees; slot++) {
    190               foreach (var subFun in subFunctionsListBox.SelectedItems) {
     191              foreach (var subFun in removedSubFunctions) {
    191192                function.RemoveAllowedSubFunction((IFunction)subFun, slot);
    192193              }
     
    194195          } else {
    195196            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);
    198200            }
    199201          }
Note: See TracChangeset for help on using the changeset viewer.