⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 geneticweaklearner.h

📁 WeakLearner,弱分类器
💻 H
字号:
//LAB_LicenseBegin==============================================================
//  Copyright (c) 2005-2006, Hicham GHORAYEB < ghorayeb@gmail.com >
//  All rights reserved.
//
//	This software is a Library for Adaptive Boosting. It provides a generic 
//	framework for the study of the Boosting algorithms. The framework provides 
//	the different tasks for boosting: Learning, Validation, Test, Profiling and 
//	Performance Analysis Tasks.
//
//	This Library was developped during my PhD studies at:
//	Ecole des Mines de Paris - Centre de Robotique( CAOR )
//	http://caor.ensmp.fr
//	under the supervision of Pr. Claude Laurgeau and Bruno Steux
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//      * Redistributions of source code must retain the above copyright
//        notice, this list of conditions and the following disclaimer.
//      * Redistributions in binary form must reproduce the above copyright
//        notice, this list of conditions and the following disclaimer
//        in the documentation and/or other materials provided with the distribution.
//      * Neither the name of the Ecole des Mines de Paris nor the names of
//        its contributors may be used to endorse or promote products
//        derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
//  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
//  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
//  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//================================================================LAB_LicenseEnd
#ifndef _LIBADABOOST_GENETIC_WEAK_LEARNER_H_
#define _LIBADABOOST_GENETIC_WEAK_LEARNER_H_

#include "LibAdaBoost/modules/ModuleFactoryBase.h"
using namespace modules;

#include "LibAdaBoost/learners/IWeakLearner.h"

#include <vector>
#include <string>
using namespace std;

#include "LibAdaBoost/classifiers/features/IRandomWeakClassifierGenerator.h"
using namespace classifiers::features;

namespace learners{
#define GA_DEFAULT_NBR_INDIV		50
#define GA_DEFAULT_WINNER_TO_KEEP	50
#define GA_DEFAULT_LOOSER_TO_KEEP	20
#define GA_DEFAULT_MAX_GENERATION	40
#define GA_DEFAULT_NBR_MUTATION		 5

	class GeneticWeakLearner: public IWeakLearner
	{
		typedef std::pair<WeakClassifier *, double> t_indiv;
		typedef std::vector<WeakClassifier *> t_population;

	public:
		GeneticWeakLearner();
		GeneticWeakLearner(ILearningSet *learningSet);

		virtual ~GeneticWeakLearner();

	public://Module
		std::string GetName(void) const { return "GeneticWeakLearner";};
		void SetOptions(const modules::options::ModuleOptions &options);

	public:// GA Properties
		unsigned GetNbrIndiv(void) const { return m_nb_indiv;}
		unsigned GetWinnerToKeep(void) const { return m_winner_to_keep;}
		unsigned GetLooserToKeep(void) const { return m_looser_to_keep;}
		unsigned GetMaxGeneration(void) const { return m_max_generation;}
		unsigned GetNbrMutation(void) const { return m_nb_mutation;}

		void SetNbrIndiv(unsigned nb_indiv) { m_nb_indiv = nb_indiv;/*m_population.resize(nb_indiv);*/}
		void SetWinnerToKeep(unsigned winner_to_keep) { m_winner_to_keep = winner_to_keep;}
		void SetLooserToKeep(unsigned looser_to_keep) { m_looser_to_keep = looser_to_keep;}
		void SetMaxGeneration(unsigned max_generation) { m_max_generation = max_generation;}
		void SetNbrMutation(unsigned nb_mutation) { m_nb_mutation = nb_mutation;}

	protected:// GA Protected Methods
		void TestWeakClassifier(WeakClassifier *feature, const ILearningSet *learningSet);
		virtual void InitializePopulation( void );

	public:// IWeakLearner
		virtual WeakClassifier *GetNewWeakClassifier(double &error);
		virtual void SetRandomWeakClassifierGenerator(IRandomWeakClassifierGenerator *randomGen);
		virtual IRandomWeakClassifierGenerator *GetRandomWeakClassifierGenerator(void);
		virtual const std::string &GetWeakLearnerLog() const;

		virtual const WeakLearnerInterface *GetWeakLearnerInterface(void);
		virtual void SetWeakLearnerInterface(const WeakLearnerInterface *wli);

	public:// ILearner
		virtual ILearningSet *GetLearningSet(void);
		virtual void SetLearningSet(ILearningSet *learningSet);

	protected:// ILearner
		virtual void Learn(void);
		virtual void Reset(void);
		virtual IClassifier *GetResult(void);
		virtual double GetResultError(void) const;
		virtual const std::string &GetLearnerLog() const;

	protected:// Learner related
		ILearningSet *m_LearningSet;

	protected:
		IRandomWeakClassifierGenerator *m_RandomGenerator;

	protected:
		const WeakLearnerInterface *m_interface;

		std::string m_StringLog;

		unsigned m_nb_indiv;
		unsigned m_winner_to_keep;
		unsigned m_looser_to_keep;
		unsigned m_max_generation;
		unsigned m_nb_mutation;

		t_indiv m_bestwc;

		t_population m_population;
	};

	typedef ModuleFactory<GeneticWeakLearner> GeneticWeakLearnerFactory;
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -