-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.hpp
More file actions
155 lines (127 loc) · 4.68 KB
/
Copy pathTree.hpp
File metadata and controls
155 lines (127 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* BEAGLE Puppy
* Copyright (C) 2001-2004 by Christian Gagne and Marc Parizeau
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Contact:
* Laboratoire de Vision et Systemes Numeriques
* Departement de genie electrique et de genie informatique
* Universite Laval, Quebec, Canada, G1V 4J7
* http://vision.gel.ulaval.ca
*
*/
/*!
* \file puppy/Tree.hpp
* \brief Definition of the class Tree.
* \author Christian Gagne
* \author Marc Parizeau
* $Revision: 1.3 $
* $Date: 2004/02/11 17:16:33 $
*/
#ifndef Puppy_Tree_hpp
#define Puppy_Tree_hpp
#include <iostream>
#include <vector>
#include "PrimitiveHandle.hpp"
#include "Primitive.hpp"
#include "Context.hpp"
#include <QObject>
#include <QVector>
#include <QString>
namespace Puppy {
/*!
* \struct Node puppy/Tree.hpp "puppy/Tree.hpp"
* \brief GP tree node structure.
* \ingroup Puppy
*/
struct Node {
/*!
* \brief Construct a tree node.
* \param inPrimitive Reference to the associated primitive.
* \param inSubTreeSize Sub-tree size value.
*/
inline explicit Node(PrimitiveHandle inPrimitive=NULL, unsigned int inSubTreeSize=0) :
mPrimitive(inPrimitive),
mSubTreeSize(inSubTreeSize)
{ }
PrimitiveHandle mPrimitive; //!< Smart pointer to the associated primitive.
unsigned int mSubTreeSize; //!< Sub-tree size, including actual node.
};
/*!
* \class Tree puppy/Tree.hpp "puppy/Tree.hpp"
* \brief GP tree class.
* \ingroup Puppy
*
* The evolutionary context includes the execution context used when interpreting the trees
* along with the problem set-up defined with the function and terminal set, and the randomizer.
*/
class Tree : public std::vector<Node> {
private:
void tree2infix(QString& ioOS, std::vector<unsigned int> depthV, int index=0) const;
void extractparentsdepth(std::vector<unsigned int> &depthV, int index=0, int depth=0) const;
void extractleavesdepth(std::vector<unsigned int> &depthV, int index=0, int depth=0) const;
public:
explicit Tree(float inFitness=-1.0, bool inValid=false);
inline bool operator==(const Tree& inRightTree) const;
inline bool operator<(const Tree& inRightTree) const;
unsigned int getDepth(unsigned int inIndex=0) const;
void interpret(void* outResult, Context& ioContext);
void setStackToNode(unsigned int inIndex, std::vector<unsigned int>& outCallStack) const;
void write(std::ostream& ioOS, unsigned int inIndex=0) const;
void write_qstring(QString& ioOS, unsigned int inIndex=0) const;
void write_qstring_infix(QString& ioOS, unsigned int inIndex=0) const;
void write_qstring_latex(QString& ioOS, unsigned int inIndex=0) const;
float mFitness; //!< Fitness value of the GP tree
float mFitnessTest;
float rFitness;
float rFitnessTest;
bool mValid; //!< Flag indicating whether the fitness is valid.
int nNodes;
};
}
inline std::ostream& operator<<(std::ostream& ioOS, const Puppy::Tree& inTree);
/*!
* \brief Compare equality of two trees.
* \param inRightTree Second tree to compare to the actual.
* \return True is trees are equals, false if not.
*/
inline bool Puppy::Tree::operator==(const Puppy::Tree& inRightTree) const
{
return (mValid && inRightTree.mValid && (mFitness == inRightTree.mFitness));
}
/*!
* \brief Compare ranking of two trees.
* \param inRightTree Second tree to compare to the actual.
* \return True is actual tree is less than seconf, false if not.
*/
inline bool Puppy::Tree::operator<(const Puppy::Tree& inRightTree) const
{
return (mValid && inRightTree.mValid && (mFitness < inRightTree.mFitness));
}
/*!
* \brief Write tree into output stream with function Puppy::Tree::write.
* \param ioOS C++ output stream to write the tree into.
* \param inTree Tree to write into output stream.
* \return C++ output stream ioOS.
* \ingroup Puppy
* \relates Puppy::Tree
*/
inline std::ostream& operator<<(std::ostream& ioOS, const Puppy::Tree& inTree)
{
inTree.write(ioOS);
return ioOS;
}
#endif // Puppy_Tree_hpp