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 |
|
---|
28 | namespace HeuristicLab.GP {
|
---|
29 | public class FunctionTreeBase : IFunctionTree {
|
---|
30 | private List<IFunctionTree> subTrees;
|
---|
31 | private IFunction function;
|
---|
32 |
|
---|
33 | public FunctionTreeBase(IFunction function) {
|
---|
34 | subTrees = new List<IFunctionTree>();
|
---|
35 | this.function = function;
|
---|
36 | }
|
---|
37 |
|
---|
38 | protected FunctionTreeBase(FunctionTreeBase original) {
|
---|
39 | this.function = original.Function;
|
---|
40 | this.subTrees = new List<IFunctionTree>(original.SubTrees.Count);
|
---|
41 | foreach (IFunctionTree originalSubTree in original.SubTrees) {
|
---|
42 | this.SubTrees.Add(originalSubTree.Clone());
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region IFunctionTree Members
|
---|
47 | public virtual bool HasLocalParameters {
|
---|
48 | get { return false; }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public IList<IFunctionTree> SubTrees {
|
---|
52 | get { return subTrees; }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public IFunction Function {
|
---|
56 | get { return function; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int GetSize() {
|
---|
60 | int size = 1;
|
---|
61 | foreach (IFunctionTree tree in subTrees) size += tree.GetSize();
|
---|
62 | return size;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public int GetHeight() {
|
---|
66 | int maxHeight = 0;
|
---|
67 | foreach (IFunctionTree tree in subTrees) maxHeight = Math.Max(maxHeight, tree.GetHeight());
|
---|
68 | return maxHeight + 1;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public virtual IOperation CreateShakingOperation(IScope scope) {
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public virtual IOperation CreateInitOperation(IScope scope) {
|
---|
76 | return null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void AddSubTree(IFunctionTree tree) {
|
---|
80 | subTrees.Add(tree);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void InsertSubTree(int index, IFunctionTree tree) {
|
---|
84 | subTrees.Insert(index, tree);
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void RemoveSubTree(int index) {
|
---|
88 | subTrees.RemoveAt(index);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public virtual IFunctionTree Clone() {
|
---|
92 | return new FunctionTreeBase(this);
|
---|
93 | }
|
---|
94 |
|
---|
95 | #endregion
|
---|
96 | }
|
---|
97 | }
|
---|