📄 context.hpp
字号:
/* * Open BEAGLE * Copyright (C) 2001-2005 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, G1K 7P4 * http://vision.gel.ulaval.ca * *//*! * \file beagle/GP/Context.hpp * \brief Definition of the type GP::Context. * \author Christian Gagne * \author Marc Parizeau * $Revision: 1.9 $ * $Date: 2005/09/30 15:04:53 $ */#ifndef Beagle_GP_Context_hpp#define Beagle_GP_Context_hpp#include "Util.hpp"#include "beagle/config.hpp"#include "beagle/macros.hpp"#include "beagle/Object.hpp"#include "beagle/AllocatorT.hpp"#include "beagle/PointerT.hpp"#include "beagle/ContainerT.hpp"#include "beagle/AssertException.hpp"#include "beagle/Context.hpp"#include "beagle/GP/Vivarium.hpp"#include "beagle/GP/Deme.hpp"#include "beagle/GP/Individual.hpp"#include "beagle/GP/Tree.hpp"#include "beagle/GP/System.hpp"#include "beagle/GP/MaxNodesExecutionException.hpp"#include "beagle/GP/MaxTimeExecutionException.hpp"namespace Beagle {namespace GP {/*! * \class Context beagle/GP/Context.hpp "beagle/GP/Context.hpp" * \brief The GP context class. * \ingroup GPF * \ingroup GPSys */class Context : public Beagle::Context {public: //! GP::Context allocator type. typedef AllocatorT<Context,Beagle::Context::Alloc> Alloc; //! GP::Context handle type. typedef PointerT<Context,Beagle::Context::Handle> Handle; //! GP::Context bag type. typedef ContainerT<Context,Beagle::Context::Bag> Bag; Context(); virtual ~Context() { } /*! * \brief Return the nth element of the call stack. * \param inN Index of the element to return. * \return Value of the nth element. * \throw Beagle::AssertException If the index is out of bound. */ inline unsigned int operator[](unsigned int inN) const { Beagle_StackTraceBeginM(); Beagle_BoundCheckAssertM(inN,0,mCallStack.size()-1); return mCallStack[inN]; Beagle_StackTraceEndM("unsigned int GP::Context::operator[](unsigned int inN) const"); } /*! * \brief Check if execution time is more or less than allowed execution time. Test is done * only when allowed execution time is greater than 0. * \throw Beagle::GP::MaxTimeExecutionException If execution time is more than allowed time. */ inline void checkExecutionTime() const { Beagle_StackTraceBeginM(); if(mAllowedExecutionTime > 0.0) { const double lTimeElapsed = mExecutionTimer.getValue(); if(lTimeElapsed > mAllowedExecutionTime) { throw GP::MaxTimeExecutionException( string("Elapsed for the individual execution is more than allowed time"), lTimeElapsed, mAllowedExecutionTime ); } } Beagle_StackTraceEndM("void GP::Context::checkExecutionTime() const"); } /*! * \brief Empty the call stack. */ inline void emptyCallStack() { Beagle_StackTraceBeginM(); mCallStack.clear(); Beagle_StackTraceEndM("void GP::Context::emptyCallStack()"); } /*! * \brief Return time allowed to individual evaluation, used to interrupt individuals evaluation. * \return Time allowed to execute a GP individual (in seconds). */ inline double getAllowedExecutionTime() const { Beagle_StackTraceBeginM(); return mAllowedExecutionTime; Beagle_StackTraceEndM("double GP::Context::getAllowedExecutionTime() const"); } /*! * \brief Return value of maximum execution counter, used to interrupt individuals evaluation. * \return Maximum number of primitives allowed to be evaluated. */ inline unsigned int getAllowedNodesExecution() const { Beagle_StackTraceBeginM(); return mAllowedNodesExecution; Beagle_StackTraceEndM("unsigned int GP::Context::getAllowedNodesExecution() const"); } /*! * \brief Return the call stack. * \return Cosntant reference to the call stack. */ inline const std::vector< unsigned int,BEAGLE_STLALLOCATOR<unsigned int> >& getCallStack() const { Beagle_StackTraceBeginM(); return mCallStack; Beagle_StackTraceEndM("const std::vector< unsigned int,BEAGLE_STLALLOCATOR<unsigned int> >& GP::Context::getCallStack() const"); } /*! * \brief Return the call stack. * \return Reference to the call stack. */ inline std::vector< unsigned int,BEAGLE_STLALLOCATOR<unsigned int> >& getCallStack() { Beagle_StackTraceBeginM(); return mCallStack; Beagle_StackTraceEndM("std::vector< unsigned int,BEAGLE_STLALLOCATOR<unsigned int> >& GP::Context::getCallStack()"); } /*! * \brief Return the nth element of the call stack. * \param inN Index of the element to return. * \return Value of the nth element. * \throw Beagle::AssertException If the index is out of bound. */ inline unsigned int getCallStackElement(unsigned int inN) const { Beagle_StackTraceBeginM(); Beagle_BoundCheckAssertM(inN,0,mCallStack.size()-1); return mCallStack[inN]; Beagle_StackTraceEndM("unsigned int GP::Context::getCallStackElement(unsigned int inN) const"); } /*! * \brief Return the call stack size. * \return Call stack size. */ inline unsigned int getCallStackSize() const { Beagle_StackTraceBeginM(); return mCallStack.size(); Beagle_StackTraceEndM("unsigned int GP::Context::getCallStackSize() const"); } /*! * \brief Get the top element of the call stack. * \return Top element of the call stack. * \throw Beagle::AssertException If the call stack is empty. */ inline unsigned int getCallStackTop() const { Beagle_StackTraceBeginM(); Beagle_AssertM(mCallStack.empty() == false); return mCallStack.back(); Beagle_StackTraceEndM("unsigned int GP::Context::getCallStackTop() const"); } /*! * \brief Return a constant reference to the actual deme. * \return Actual deme constant reference. */ inline const GP::Deme& getDeme() const { Beagle_StackTraceBeginM(); return castObjectT<const GP::Deme&>(Beagle::Context::getDeme()); Beagle_StackTraceEndM("const GP::Deme& GP::Context::getDeme() const"); } /*! * \brief Return a reference to the actual GP deme. * \return Actual GP deme reference. */ inline GP::Deme& getDeme() { Beagle_StackTraceBeginM(); return castObjectT<GP::Deme&>(Beagle::Context::getDeme()); Beagle_StackTraceEndM("GP::Deme& GP::Context::getDeme()"); } /*! * \brief Return a handle to the actual GP deme. * \return Actual GP deme handle. */ inline GP::Deme::Handle getDemeHandle() { Beagle_StackTraceBeginM(); return castHandleT<GP::Deme>(mDemeHandle); Beagle_StackTraceEndM("GP::Deme::Handle GP::Context::getDemeHandle()"); } /*! * \return Reference to context execution timer, used to time execution time of GP individuals. */ inline const PACC::Timer& getExecutionTimer() const { Beagle_StackTraceBeginM(); return mExecutionTimer; Beagle_StackTraceEndM("const PACC::Timer& GP::Context::getExecutionTimer() const"); } /*! * \return Reference to context execution timer, used to time execution time of GP individuals. */ inline PACC::Timer& getExecutionTimer() { Beagle_StackTraceBeginM(); return mExecutionTimer; Beagle_StackTraceEndM("PACC::Timer& GP::Context::getExecutionTimer()"); } /*! * \brief Return a constant reference to the actual GP tree. * \return Actual GP tree constant reference. */ inline const GP::Tree& getGenotype() const { Beagle_StackTraceBeginM();#ifndef BEAGLE_NDEBUG if(mGenotypeHandle == NULL) throw Beagle_RunTimeExceptionM( "The context does not have a handle to a genotype. Consider Beagle::Context::setGenotypeHandle().");#endif // BEAGLE_NDEBUG return castObjectT<const GP::Tree&>(*mGenotypeHandle); Beagle_StackTraceEndM("const GP::Tree& GP::Context::getGenotype() const"); } /*!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -