[2210] | 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;
|
---|
[2216] | 27 | using System.Xml;
|
---|
[2210] | 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 | Size = functionTree.GetSize();
|
---|
| 39 | Height = functionTree.GetHeight();
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | public int Size { get; set; }
|
---|
| 43 | public int Height { get; set; }
|
---|
| 44 |
|
---|
| 45 | public GeneticProgrammingModel()
|
---|
| 46 | : base() {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[2235] | 49 | public GeneticProgrammingModel(IFunctionTree tree)
|
---|
| 50 | : base() {
|
---|
[2210] | 51 | FunctionTree = tree;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 55 | GeneticProgrammingModel clone = (GeneticProgrammingModel)base.Clone(clonedObjects);
|
---|
[2218] | 56 | clone.FunctionTree = (IFunctionTree)FunctionTree.Clone();
|
---|
[2210] | 57 | return clone;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[2216] | 60 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 61 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
[2219] | 62 | PersistTree(node, document, persistedObjects, FunctionTree);
|
---|
[2216] | 63 | return node;
|
---|
[2210] | 64 | }
|
---|
| 65 |
|
---|
[2219] | 66 | private void PersistTree(XmlNode node, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects, IFunctionTree tree) {
|
---|
| 67 | XmlNode fNode = PersistenceManager.Persist(tree.Function, document, persistedObjects);
|
---|
| 68 | XmlAttribute subTreesAttr = document.CreateAttribute("Args");
|
---|
| 69 | subTreesAttr.Value = XmlConvert.ToString(tree.SubTrees.Count);
|
---|
| 70 | fNode.Attributes.Append(subTreesAttr);
|
---|
| 71 | node.AppendChild(fNode);
|
---|
| 72 | XmlNode treeNode = tree.GetXmlNode("Data", document, persistedObjects);
|
---|
| 73 | if (treeNode != null) fNode.AppendChild(treeNode);
|
---|
| 74 | foreach (IFunctionTree subTree in tree.SubTrees) {
|
---|
| 75 | PersistTree(node, document, persistedObjects, subTree);
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[2216] | 79 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 80 | base.Populate(node, restoredObjects);
|
---|
[2219] | 81 | int nodeIndex = 0;
|
---|
| 82 | FunctionTree = RestoreTree(node, ref nodeIndex, restoredObjects);
|
---|
[2210] | 83 | }
|
---|
| 84 |
|
---|
[2219] | 85 | private IFunctionTree RestoreTree(XmlNode node, ref int nodeIndex, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 86 | XmlNode fNode = node.ChildNodes[nodeIndex];
|
---|
| 87 | int subTrees = XmlConvert.ToInt32(fNode.Attributes["Args"].Value);
|
---|
| 88 | IFunction f = (IFunction)PersistenceManager.Restore(fNode, restoredObjects);
|
---|
| 89 | IFunctionTree tree = f.GetTreeNode();
|
---|
[2235] | 90 | if (fNode.ChildNodes.Count > 0) tree.Populate(fNode.ChildNodes[0], restoredObjects);
|
---|
[2219] | 91 | nodeIndex++;
|
---|
| 92 | for (int i = 0; i < subTrees; i++) {
|
---|
| 93 | tree.AddSubTree(RestoreTree(node, ref nodeIndex, restoredObjects));
|
---|
| 94 | }
|
---|
| 95 | return tree;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[2210] | 98 | public override IView CreateView() {
|
---|
[2328] | 99 | return new GeneticProgrammingModelView(this);
|
---|
[2210] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | }
|
---|