1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.GP.Interfaces;
|
---|
27 | using System.Xml;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.GP {
|
---|
30 | public class GeneticProgrammingModel : ItemBase, IGeneticProgrammingModel {
|
---|
31 | private IFunctionTree functionTree;
|
---|
32 | public IFunctionTree FunctionTree {
|
---|
33 | get {
|
---|
34 | return functionTree;
|
---|
35 | }
|
---|
36 | set {
|
---|
37 | functionTree = value;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | public int Size { get { return functionTree.GetSize(); } }
|
---|
41 | public int Height { get { return functionTree.GetHeight(); } }
|
---|
42 |
|
---|
43 | public GeneticProgrammingModel()
|
---|
44 | : base() {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public GeneticProgrammingModel(IFunctionTree tree)
|
---|
48 | : base() {
|
---|
49 | FunctionTree = tree;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
53 | GeneticProgrammingModel clone = (GeneticProgrammingModel)base.Clone(clonedObjects);
|
---|
54 | clone.FunctionTree = (IFunctionTree)FunctionTree.Clone();
|
---|
55 | return clone;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
59 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
60 | // persist the tree in linear form
|
---|
61 | PersistTree(node, document, persistedObjects, FunctionTree);
|
---|
62 | return node;
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void PersistTree(XmlNode node, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects, IFunctionTree tree) {
|
---|
66 | XmlNode fNode = PersistenceManager.Persist(tree.Function, document, persistedObjects);
|
---|
67 | // save the number of sub-trees
|
---|
68 | XmlAttribute subTreesAttr = document.CreateAttribute("Args");
|
---|
69 | subTreesAttr.Value = XmlConvert.ToString(tree.SubTrees.Count);
|
---|
70 | fNode.Attributes.Append(subTreesAttr);
|
---|
71 | // save the function symbol
|
---|
72 | node.AppendChild(fNode);
|
---|
73 | // if the tree node has local data save it into a child element called "data"
|
---|
74 | XmlNode treeNode = tree.GetXmlNode("Data", document, persistedObjects);
|
---|
75 | if (treeNode != null) fNode.AppendChild(treeNode);
|
---|
76 | // recursivly store the children into the same linear form
|
---|
77 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
78 | PersistTree(node, document, persistedObjects, subTree);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
83 | base.Populate(node, restoredObjects);
|
---|
84 | int nodeIndex = 0;
|
---|
85 | // restore linear form back into tree form
|
---|
86 | FunctionTree = RestoreTree(node, ref nodeIndex, restoredObjects);
|
---|
87 | }
|
---|
88 |
|
---|
89 | private IFunctionTree RestoreTree(XmlNode node, ref int nodeIndex, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
90 | XmlNode fNode = node.ChildNodes[nodeIndex];
|
---|
91 | // restore the number of child nodes
|
---|
92 | int subTrees = XmlConvert.ToInt32(fNode.Attributes["Args"].Value);
|
---|
93 | // restore the function symbol
|
---|
94 | IFunction f = (IFunction)PersistenceManager.Restore(fNode, restoredObjects);
|
---|
95 | // create a tree node from the function
|
---|
96 | IFunctionTree tree = f.GetTreeNode();
|
---|
97 | // check if there is data for the tree node that needs to be restored and restore the data if needed
|
---|
98 | var dataNode = fNode.SelectSingleNode("Data");
|
---|
99 | if (dataNode != null) tree.Populate(dataNode, restoredObjects);
|
---|
100 | nodeIndex++;
|
---|
101 | for (int i = 0; i < subTrees; i++) {
|
---|
102 | // recursively read children from linear representation
|
---|
103 | tree.AddSubTree(RestoreTree(node, ref nodeIndex, restoredObjects));
|
---|
104 | }
|
---|
105 | return tree;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public override IView CreateView() {
|
---|
109 | return new GeneticProgrammingModelView(this);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|