[645] | 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 System.Xml;
|
---|
| 27 | using System.Diagnostics;
|
---|
[2210] | 28 | using HeuristicLab.GP.Interfaces;
|
---|
[645] | 29 |
|
---|
| 30 | namespace HeuristicLab.GP {
|
---|
[2202] | 31 | public abstract class FunctionBase : ItemBase, IFunction {
|
---|
| 32 | private List<List<IFunction>> allowedSubFunctions = new List<List<IFunction>>();
|
---|
[645] | 33 | private int minArity = -1;
|
---|
| 34 | private int maxArity = -1;
|
---|
[2202] | 35 | private double tickets = 1.0;
|
---|
| 36 | private IOperator initializer;
|
---|
| 37 | private IOperator manipulator;
|
---|
| 38 | private int minTreeHeight = -1;
|
---|
| 39 | private int minTreeSize = -1;
|
---|
[645] | 40 |
|
---|
[2202] | 41 | public virtual string Name {
|
---|
| 42 | get { return this.GetType().Name; }
|
---|
[645] | 43 | }
|
---|
| 44 |
|
---|
[2202] | 45 | public virtual string Description {
|
---|
| 46 | get { return "Description for this function is missing (TODO)"; }
|
---|
[645] | 47 | }
|
---|
| 48 |
|
---|
[2211] | 49 | public int MinSubTrees {
|
---|
[645] | 50 | get {
|
---|
| 51 | return minArity;
|
---|
| 52 | }
|
---|
[2202] | 53 | protected set {
|
---|
| 54 | minArity = value;
|
---|
| 55 | while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>());
|
---|
| 56 | }
|
---|
[645] | 57 | }
|
---|
| 58 |
|
---|
[2211] | 59 | public int MaxSubTrees {
|
---|
[645] | 60 | get {
|
---|
| 61 | return maxArity;
|
---|
| 62 | }
|
---|
[2202] | 63 | protected set {
|
---|
| 64 | maxArity = value;
|
---|
| 65 | while (allowedSubFunctions.Count > maxArity) allowedSubFunctions.RemoveAt(allowedSubFunctions.Count - 1);
|
---|
| 66 | while (maxArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>());
|
---|
| 67 | }
|
---|
[645] | 68 | }
|
---|
| 69 |
|
---|
[2202] | 70 |
|
---|
| 71 | public int MinTreeSize {
|
---|
| 72 | get {
|
---|
| 73 | if (minTreeSize <= 0) RecalculateMinimalTreeSize();
|
---|
| 74 | return minTreeSize;
|
---|
[645] | 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[2202] | 78 | public int MinTreeHeight {
|
---|
| 79 | get {
|
---|
| 80 | if (minTreeHeight <= 0) RecalculateMinimalTreeHeight();
|
---|
| 81 | return minTreeHeight;
|
---|
[645] | 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
[2202] | 85 | public double Tickets {
|
---|
| 86 | get { return tickets; }
|
---|
| 87 | set {
|
---|
| 88 | if (value < 0.0) throw new ArgumentException("Number of tickets must be positive");
|
---|
| 89 | else tickets = value;
|
---|
[645] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[2202] | 93 | public IOperator Initializer {
|
---|
| 94 | get { return initializer; }
|
---|
| 95 | set { initializer = value; }
|
---|
[645] | 96 | }
|
---|
| 97 |
|
---|
[2202] | 98 | public IOperator Manipulator {
|
---|
| 99 | get { return manipulator; }
|
---|
| 100 | set { manipulator = value; }
|
---|
[645] | 101 | }
|
---|
| 102 |
|
---|
[2202] | 103 | public virtual IFunctionTree GetTreeNode() {
|
---|
[2210] | 104 | return new FunctionTreeBase(this);
|
---|
[645] | 105 | }
|
---|
| 106 |
|
---|
[2202] | 107 | public ICollection<IFunction> GetAllowedSubFunctions(int index) {
|
---|
[2211] | 108 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
[2202] | 109 | return allowedSubFunctions[index];
|
---|
[645] | 110 | }
|
---|
| 111 |
|
---|
[2202] | 112 | public void AddAllowedSubFunction(IFunction function, int index) {
|
---|
[2211] | 113 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
[2202] | 114 | if (allowedSubFunctions[index] == null) {
|
---|
| 115 | allowedSubFunctions[index] = new List<IFunction>();
|
---|
| 116 | }
|
---|
| 117 | if (!allowedSubFunctions[index].Contains(function)) {
|
---|
| 118 | allowedSubFunctions[index].Add(function);
|
---|
| 119 | }
|
---|
[645] | 120 | }
|
---|
| 121 |
|
---|
[2202] | 122 | public void RemoveAllowedSubFunction(IFunction function, int index) {
|
---|
[2211] | 123 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
[2202] | 124 | allowedSubFunctions[index].Add(function);
|
---|
[645] | 125 | }
|
---|
| 126 |
|
---|
[2202] | 127 | public bool IsAllowedSubFunction(IFunction function, int index) {
|
---|
| 128 | return GetAllowedSubFunctions(index).Contains(function);
|
---|
[645] | 129 | }
|
---|
| 130 |
|
---|
[2202] | 131 | private void RecalculateMinimalTreeSize() {
|
---|
| 132 | minTreeSize = int.MaxValue;
|
---|
| 133 | int sum = 1;
|
---|
| 134 | int minSize = int.MaxValue;
|
---|
[2211] | 135 | for (int i = 0; i < MinSubTrees; i++) {
|
---|
[2202] | 136 | foreach (IFunction subFun in GetAllowedSubFunctions(i)) {
|
---|
| 137 | minSize = Math.Min(minSize, subFun.MinTreeSize);
|
---|
| 138 | }
|
---|
| 139 | sum += minSize;
|
---|
| 140 | }
|
---|
| 141 | minTreeSize = sum;
|
---|
[645] | 142 | }
|
---|
| 143 |
|
---|
[2202] | 144 | private void RecalculateMinimalTreeHeight() {
|
---|
| 145 | minTreeHeight = int.MaxValue;
|
---|
| 146 | int height = 0;
|
---|
| 147 | int minHeight = int.MaxValue;
|
---|
[2211] | 148 | for (int i = 0; i < MinSubTrees; i++) {
|
---|
[2202] | 149 | foreach (IFunction subFun in GetAllowedSubFunctions(i)) {
|
---|
| 150 | minHeight = Math.Min(minHeight, subFun.MinTreeHeight);
|
---|
| 151 | }
|
---|
| 152 | height = Math.Max(height, minHeight);
|
---|
| 153 | }
|
---|
| 154 | minTreeHeight = height + 1;
|
---|
[645] | 155 | }
|
---|
[2216] | 156 |
|
---|
| 157 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
[2219] | 158 | FunctionBase clone = (FunctionBase)base.Clone(clonedObjects);
|
---|
| 159 | clone.initializer = (IOperator)Auxiliary.Clone(initializer, clonedObjects);
|
---|
| 160 | clone.manipulator = (IOperator)Auxiliary.Clone(manipulator, clonedObjects);
|
---|
| 161 | clone.maxArity = maxArity;
|
---|
| 162 | clone.minArity = minArity;
|
---|
| 163 | clone.minTreeHeight = minTreeHeight;
|
---|
| 164 | clone.minTreeSize = minTreeSize;
|
---|
| 165 | clone.tickets = tickets;
|
---|
| 166 | for (int i = 0; i < MaxSubTrees; i++) {
|
---|
| 167 | var allowedSubFunctionsForSlot = new List<IFunction>();
|
---|
| 168 | foreach (IFunction f in GetAllowedSubFunctions(i)) {
|
---|
| 169 | allowedSubFunctionsForSlot.Add((IFunction)Auxiliary.Clone(f, clonedObjects));
|
---|
| 170 | }
|
---|
| 171 | clone.allowedSubFunctions.Add(allowedSubFunctionsForSlot);
|
---|
| 172 | }
|
---|
| 173 | return clone;
|
---|
[2216] | 174 | }
|
---|
| 175 |
|
---|
| 176 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 177 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 178 | XmlAttribute minSubTreesAttr = document.CreateAttribute("MinSubTrees");
|
---|
| 179 | minSubTreesAttr.Value = XmlConvert.ToString(MinSubTrees);
|
---|
| 180 | XmlAttribute maxSubTreesAttr = document.CreateAttribute("MaxSubTrees");
|
---|
| 181 | maxSubTreesAttr.Value = XmlConvert.ToString(MaxSubTrees);
|
---|
| 182 | node.Attributes.Append(minSubTreesAttr);
|
---|
| 183 | node.Attributes.Append(maxSubTreesAttr);
|
---|
| 184 | for (int i = 0; i < MaxSubTrees; i++) {
|
---|
| 185 | XmlNode slotNode = document.CreateElement("AllowedSubFunctions");
|
---|
| 186 | XmlAttribute slotAttr = document.CreateAttribute("Slot");
|
---|
| 187 | slotAttr.Value = XmlConvert.ToString(i);
|
---|
| 188 | slotNode.Attributes.Append(slotAttr);
|
---|
| 189 | node.AppendChild(slotNode);
|
---|
| 190 | foreach (IFunction f in GetAllowedSubFunctions(i)) {
|
---|
| 191 | slotNode.AppendChild(PersistenceManager.Persist(f, document, persistedObjects));
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | return node;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 198 | base.Populate(node, restoredObjects);
|
---|
| 199 | MinSubTrees = XmlConvert.ToInt32(node.Attributes["MinSubTrees"].Value);
|
---|
| 200 | MaxSubTrees = XmlConvert.ToInt32(node.Attributes["MaxSubTrees"].Value);
|
---|
| 201 | foreach (XmlNode allowedSubFunctionsNode in node.SelectNodes("AllowedSubFunctions")) {
|
---|
| 202 | int slot = XmlConvert.ToInt32(allowedSubFunctionsNode.Attributes["Slot"].Value);
|
---|
| 203 | foreach (XmlNode fNode in allowedSubFunctionsNode.ChildNodes) {
|
---|
| 204 | AddAllowedSubFunction((IFunction)PersistenceManager.Restore(fNode, restoredObjects), slot);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
[645] | 208 | }
|
---|
| 209 | }
|
---|