1 /*
2  *  Open BEAGLE
3  *  Copyright (C) 2001-2007 by Christian Gagne and Marc Parizeau
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2.1 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Contact:
20  *  Laboratoire de Vision et Systemes Numeriques
21  *  Departement de genie electrique et de genie informatique
22  *  Universite Laval, Quebec, Canada, G1K 7P4
23  *  http://vision.gel.ulaval.ca
24  *
25  */
26 
27 /*!
28  *  \file   beagle/GP/src/System.cpp
29  *  \brief  Implementation of the type GP::System.
30  *  \author Christian Gagne
31  *  \author Marc Parizeau
32  *  $Revision: 1.13.2.1 $
33  *  $Date: 2007/05/09 01:51:07 $
34  */
35 
36 #include "beagle/GP.hpp"
37 
38 using namespace Beagle;
39 
40 
41 /*!
42  *  \brief Construct a new GP system.
43  */
System()44 GP::System::System() :
45   Beagle::System(new GP::Context::Alloc),
46   mPrimitiveSuperSet(new GP::PrimitiveSuperSet)
47 {
48   Beagle_StackTraceBeginM();
49   addComponent(mPrimitiveSuperSet);
50   addComponent(new ModuleVectorComponent);
51   Beagle_StackTraceEndM("GP::System::System()");
52 }
53 
54 
55 /*!
56  *  \brief Construct a new GP system.
57  *  \param inSuperSet Handle to the primitive super set to use.
58  */
System(GP::PrimitiveSuperSet::Handle inSuperSet)59 GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet) :
60   Beagle::System(new GP::Context::Alloc),
61   mPrimitiveSuperSet(inSuperSet)
62 {
63   Beagle_StackTraceBeginM();
64   addComponent(mPrimitiveSuperSet);
65   addComponent(new ModuleVectorComponent);
66   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet)");
67 }
68 
69 
70 /*!
71  *  \brief Construct a new GP system.
72  *  \param inSuperSet Handle to the primitive super set to use.
73  *  \param inGPContextAllocator Context allocator to use.
74  */
System(GP::PrimitiveSuperSet::Handle inSuperSet,Beagle::Context::Alloc::Handle inGPContextAllocator)75 GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet,
76                    Beagle::Context::Alloc::Handle inGPContextAllocator) :
77   Beagle::System(inGPContextAllocator),
78   mPrimitiveSuperSet(inSuperSet)
79 {
80   Beagle_StackTraceBeginM();
81   addComponent(mPrimitiveSuperSet);
82   addComponent(new ModuleVectorComponent);
83   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet, Beagle::Context::Alloc::Handle inGPContextAllocator)");
84 }
85 
86 
87 /*!
88  *  \brief Construct a new GP system.
89  *  \param inSuperSet Handle to the primitive super set to use.
90  *  \param inGPContextAllocator Context allocator to use.
91  *  \param inRandomizer Handle to the randomizer used.
92  *  \param inRegister Handle to the register used.
93  *  \param inLogger Handle to the logger used.
94  *  \param inModuleVector Module vector component to use.
95  */
System(GP::PrimitiveSuperSet::Handle inSuperSet,Beagle::Context::Alloc::Handle inGPContextAllocator,Randomizer::Handle inRandomizer,Register::Handle inRegister,Logger::Handle inLogger,GP::ModuleVectorComponent::Handle inModuleVector)96 GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet,
97                    Beagle::Context::Alloc::Handle inGPContextAllocator,
98                    Randomizer::Handle inRandomizer,
99                    Register::Handle inRegister,
100                    Logger::Handle inLogger,
101                    GP::ModuleVectorComponent::Handle inModuleVector) :
102   Beagle::System(inGPContextAllocator, inRandomizer, inRegister, inLogger),
103   mPrimitiveSuperSet(inSuperSet)
104 {
105   Beagle_StackTraceBeginM();
106   addComponent(mPrimitiveSuperSet);
107   addComponent(inModuleVector);
108   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSuperSet::Handle inSuperSet, Beagle::Context::Alloc::Handle inGPContextAllocator, Randomizer::Handle inRandomizer, Register::Handle inRegister, Logger::Handle inLogger, GP::ModuleVectorComponent::Handle inModuleVector)");
109 }
110 
111 
112 /*!
113  *  \brief Construct a new GP system.
114  *  \param inSet Handle to the primitive set to use.
115  */
System(GP::PrimitiveSet::Handle inSet)116 GP::System::System(GP::PrimitiveSet::Handle inSet) :
117   Beagle::System(new GP::Context::Alloc),
118   mPrimitiveSuperSet(new PrimitiveSuperSet)
119 {
120   Beagle_StackTraceBeginM();
121   addComponent(mPrimitiveSuperSet);
122   mPrimitiveSuperSet->insert(inSet);
123   addComponent(new ModuleVectorComponent);
124   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSet::Handle inSet)");
125 }
126 
127 
128 /*!
129  *  \brief Construct a new GP system.
130  *  \param inSet Handle to the primitive set to use.
131  *  \param inGPContextAllocator Context allocator to use.
132  */
System(GP::PrimitiveSet::Handle inSet,Beagle::Context::Alloc::Handle inGPContextAllocator)133 GP::System::System(GP::PrimitiveSet::Handle inSet,
134                    Beagle::Context::Alloc::Handle inGPContextAllocator) :
135   Beagle::System(inGPContextAllocator),
136   mPrimitiveSuperSet(new PrimitiveSuperSet)
137 {
138   Beagle_StackTraceBeginM();
139   addComponent(mPrimitiveSuperSet);
140   mPrimitiveSuperSet->insert(inSet);
141   addComponent(new ModuleVectorComponent);
142   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSet::Handle inSet, Beagle::Context::Alloc::Handle inGPContextAllocator)");
143 }
144 
145 
146 /*!
147  *  \brief Construct a new GP system.
148  *  \param inSet Handle to the primitive set to use.
149  *  \param inGPContextAllocator Context allocator to use.
150  *  \param inRandomizer Handle to the randomizer used.
151  *  \param inRegister Handle to the register used.
152  *  \param inLogger Handle to the logger used.
153  *  \param inModuleVector Module vector component to use.
154  */
System(GP::PrimitiveSet::Handle inSet,Beagle::Context::Alloc::Handle inGPContextAllocator,Randomizer::Handle inRandomizer,Register::Handle inRegister,Logger::Handle inLogger,GP::ModuleVectorComponent::Handle inModuleVector)155 GP::System::System(GP::PrimitiveSet::Handle inSet,
156                    Beagle::Context::Alloc::Handle inGPContextAllocator,
157                    Randomizer::Handle inRandomizer,
158                    Register::Handle inRegister,
159                    Logger::Handle inLogger,
160                    GP::ModuleVectorComponent::Handle inModuleVector) :
161   Beagle::System(inGPContextAllocator, inRandomizer, inRegister, inLogger),
162   mPrimitiveSuperSet(new PrimitiveSuperSet)
163 {
164   Beagle_StackTraceBeginM();
165   addComponent(mPrimitiveSuperSet);
166   mPrimitiveSuperSet->insert(inSet);
167   addComponent(inModuleVector);
168   Beagle_StackTraceEndM("GP::System::System(GP::PrimitiveSet::Handle inSet, Beagle::Context::Alloc::Handle inGPContextAllocator, Randomizer::Handle inRandomizer, Register::Handle inRegister, Logger::Handle inLogger, GP::ModuleVectorComponent::Handle inModuleVector)");
169 }
170 
171 
172 /*!
173  *  \brief Read evolutionary system from XML subtree.
174  *  \param inIter XML iterator used to read system.
175  */
read(PACC::XML::ConstIterator inIter)176 void GP::System::read(PACC::XML::ConstIterator inIter)
177 {
178   Beagle_StackTraceBeginM();
179   if((inIter->getType()!=PACC::XML::eData) || (inIter->getValue()!="System"))
180     throw Beagle_IOExceptionNodeM(*inIter, "tag <System> expected!");
181 
182   // First reading pass, read randomizer, register, logger and primitive super set.
183   for(PACC::XML::ConstIterator lChild=inIter->getFirstChild(); lChild; ++lChild) {
184     if(lChild->getType() == PACC::XML::eData) {
185       if(lChild->getValue() == "Randomizer")    mRandomizer->readWithSystem(lChild, *this);
186       else if(lChild->getValue() == "Register") mRegister->readWithSystem(lChild, *this);
187       else if(lChild->getValue() == "Logger")   mLogger->readWithSystem(lChild, *this);
188       else if(lChild->getValue() == "PrimitiveSuperSet")
189         mPrimitiveSuperSet->readWithSystem(lChild, *this);
190     }
191   }
192 
193   // Second reading pass, read other components.
194   for(PACC::XML::ConstIterator lChild=inIter->getFirstChild(); lChild; ++lChild) {
195     if(lChild->getType() == PACC::XML::eData) {
196       if((lChild->getValue() != "Randomizer") &&
197          (lChild->getValue() != "Register") &&
198          (lChild->getValue() != "Logger") &&
199          (lChild->getValue() != "PrimitiveSuperSet")) {
200         if(find(lChild->getValue().c_str())==end())
201           throw Beagle_ObjectExceptionM(string("The component named \"")+
202                                         string(lChild->getValue().c_str())+
203                                         string("\" is not present in the system."));
204         (*this)[lChild->getValue().c_str()]->readWithSystem(lChild, *this);
205       }
206     }
207   }
208   Beagle_StackTraceEndM("void GP::System::read(PACC::XML::ConstIterator inIter)");
209 }
210 
211