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;
|
---|
28 | using HeuristicLab.GP.Interfaces;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.GP {
|
---|
31 | public abstract class FunctionBase : ItemBase, IFunction {
|
---|
32 | private List<List<IFunction>> allowedSubFunctions = new List<List<IFunction>>();
|
---|
33 | private int minArity = -1;
|
---|
34 | private int maxArity = -1;
|
---|
35 | private double tickets = 1.0;
|
---|
36 | private IOperator initializer;
|
---|
37 | private IOperator manipulator;
|
---|
38 | private int minTreeHeight = -1;
|
---|
39 | private int minTreeSize = -1;
|
---|
40 |
|
---|
41 | public virtual string Name {
|
---|
42 | get { return this.GetType().Name; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public virtual string Description {
|
---|
46 | get { return "Description for this function is missing (TODO)"; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public int MinSubTrees {
|
---|
50 | get {
|
---|
51 | return minArity;
|
---|
52 | }
|
---|
53 | protected set {
|
---|
54 | minArity = value;
|
---|
55 | while (minArity > allowedSubFunctions.Count) allowedSubFunctions.Add(new List<IFunction>());
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int MaxSubTrees {
|
---|
60 | get {
|
---|
61 | return maxArity;
|
---|
62 | }
|
---|
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 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | public int MinTreeSize {
|
---|
72 | get {
|
---|
73 | if (minTreeSize <= 0) RecalculateMinimalTreeSize();
|
---|
74 | return minTreeSize;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public int MinTreeHeight {
|
---|
79 | get {
|
---|
80 | if (minTreeHeight <= 0) RecalculateMinimalTreeHeight();
|
---|
81 | return minTreeHeight;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
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;
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | public IOperator Initializer {
|
---|
94 | get { return initializer; }
|
---|
95 | set { initializer = value; }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public IOperator Manipulator {
|
---|
99 | get { return manipulator; }
|
---|
100 | set { manipulator = value; }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public virtual IFunctionTree GetTreeNode() {
|
---|
104 | return new FunctionTreeBase(this);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public ICollection<IFunction> GetAllowedSubFunctions(int index) {
|
---|
108 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
109 | return allowedSubFunctions[index];
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void AddAllowedSubFunction(IFunction function, int index) {
|
---|
113 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
114 | if (allowedSubFunctions[index] == null) {
|
---|
115 | allowedSubFunctions[index] = new List<IFunction>();
|
---|
116 | }
|
---|
117 | if (!allowedSubFunctions[index].Contains(function)) {
|
---|
118 | allowedSubFunctions[index].Add(function);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void RemoveAllowedSubFunction(IFunction function, int index) {
|
---|
123 | if (index < 0 || index > MaxSubTrees) throw new ArgumentException("Index outside of allowed range. index = " + index);
|
---|
124 | allowedSubFunctions[index].Add(function);
|
---|
125 | }
|
---|
126 |
|
---|
127 | public bool IsAllowedSubFunction(IFunction function, int index) {
|
---|
128 | return GetAllowedSubFunctions(index).Contains(function);
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void RecalculateMinimalTreeSize() {
|
---|
132 | minTreeSize = int.MaxValue;
|
---|
133 | int sum = 1;
|
---|
134 | int minSize = int.MaxValue;
|
---|
135 | for (int i = 0; i < MinSubTrees; i++) {
|
---|
136 | foreach (IFunction subFun in GetAllowedSubFunctions(i)) {
|
---|
137 | minSize = Math.Min(minSize, subFun.MinTreeSize);
|
---|
138 | }
|
---|
139 | sum += minSize;
|
---|
140 | }
|
---|
141 | minTreeSize = sum;
|
---|
142 | }
|
---|
143 |
|
---|
144 | private void RecalculateMinimalTreeHeight() {
|
---|
145 | minTreeHeight = int.MaxValue;
|
---|
146 | int height = 0;
|
---|
147 | int minHeight = int.MaxValue;
|
---|
148 | for (int i = 0; i < MinSubTrees; i++) {
|
---|
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;
|
---|
155 | }
|
---|
156 |
|
---|
157 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
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;
|
---|
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 | }
|
---|
208 | }
|
---|
209 | }
|
---|