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

📄 controlpointgeneticweaklearnerinterface.cpp

📁 WeakLearner
💻 CPP
字号:
//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
#include "ControlPointGeneticWeakLearnerInterface.h"
using namespace learners::wlinterfaces;

#include "LibAdaBoost/modules/options/ModuleOptions.h"
using namespace modules::options;

#include <iostream>
using namespace std;

#define MY_RAND(range) (rand()%range)
#define CHECK_RANGE(v, minVal, maxVal) (((v) < (minVal))? (minVal): (((v) >= (maxVal))? (maxVal)-1: (v)))

ControlPointGeneticWeakLearnerInterface::ControlPointGeneticWeakLearnerInterface()
{
}

ControlPointGeneticWeakLearnerInterface::~ControlPointGeneticWeakLearnerInterface()
{
}

// WeakLearnerInterface
WeakClassifier* 
ControlPointGeneticWeakLearnerInterface::Mutate(const WeakClassifier &wc) const
{
	const ControlPointFeature &cpObject = static_cast<const ControlPointFeature &>(wc);

	// CLone the Current WeakClassifier
	ControlPointFeature *cpf = (ControlPointFeature *)(cpObject.Clone());

	// GENERATE RANDOM MUTATION TYPE
	t_MutationType mutationType;
	mutationType = (t_MutationType) MY_RAND( NUMBER_OF_MUTATION_TYPE );

	MutateSpecific( *cpf, mutationType);
	return cpf;
}

WeakClassifier* 
ControlPointGeneticWeakLearnerInterface::Randomize(const WeakClassifier &wc) const
{
	const ControlPointFeature &cpObject = static_cast<const ControlPointFeature &>(wc);

	ControlPointFeature *cpf = new ControlPointFeature();
    	
	ControlPointFeature::t_cpts bluePoints;
	ControlPointFeature::t_cpts redPoints;

	int width;
	int height;

	int channel;
	int NbrOfChannels;
	
	height = cpObject.GetObjectHeight();
	width  = cpObject.GetObjectWidth();

	channel = cpObject.Getchannel();
	NbrOfChannels = cpObject.GetNbrOfChannels();

	cpf->SetObjectHeight( height );
	cpf->SetObjectWidth( width );

	cpf->SetNbrOfChannels( NbrOfChannels );
	cpf->SetChannel( channel );

	int nPos = MY_RAND(CPCLASSIFIER_MAXPOINT) + 1;
	int nNeg = MY_RAND(CPCLASSIFIER_MAXPOINT) + 1;
	
	for(int i = 0; i < nPos; i++)
	{
		bluePoints.push_back(ControlPointFeature::t_cpt(MY_RAND(width), MY_RAND(height)));
	}

	for(int i = 0; i < nNeg; i++)
	{
		redPoints.push_back(ControlPointFeature::t_cpt( MY_RAND(width), MY_RAND(height)));
	}

	cpf->SetBluePoints(bluePoints);
	cpf->SetRedPoints(redPoints);
	cpf->SetResolution( 0 );

	bluePoints.clear();
	redPoints.clear();

	return cpf;
}

WeakClassifier* 
ControlPointGeneticWeakLearnerInterface::CrossOver(const WeakClassifier &wc1, const WeakClassifier &wc2) const
{
	return NULL;
}

void 
ControlPointGeneticWeakLearnerInterface::SetOptions(const modules::options::ModuleOptions &options)
{
	//DO Nothing
}

void 
ControlPointGeneticWeakLearnerInterface::MutateSpecific(ControlPointFeature &wcOut, ControlPointGeneticWeakLearnerInterface::t_MutationType mutationType) const
{
	int oWidth  = wcOut.m_ObjectWidth;
	int oHeight = wcOut.m_ObjectHeight;

	switch( mutationType )
	{
	case ControlPointGeneticWeakLearnerInterface::MUTATE_MOVE_POINT:
		{
			ControlPointFeature::t_ControlPointColor pointColor;
			int pointIndex;

			// RANDOM SIDE: RED OR BLUE
			pointColor = (ControlPointFeature::t_ControlPointColor) MY_RAND( 2 );

			// DEPENDS ON THE SIDE, ADD RANDOM DELTA TO RANDOM POINT
			switch( pointColor )
			{
			case ControlPointFeature::CP_RED_POINT:
				{
					pointIndex = MY_RAND( wcOut.m_NbrRedPoints );
					ControlPointFeature::t_ControlPoint pt;
					
					pt.x = wcOut.red[ pointIndex ].x;
					pt.y = wcOut.red[ pointIndex ].y;

                    pt.x = pt.x + ( 5 - MY_RAND(10) );
					pt.y = pt.y + ( 5 - MY_RAND(10) );

					pt.x = CHECK_RANGE(pt.x, 0, oWidth);
					pt.y = CHECK_RANGE(pt.y, 0, oHeight);

					wcOut.red[pointIndex].x = pt.x;
					wcOut.red[pointIndex].y = pt.y;

				}
				break;
			case ControlPointFeature::CP_BLUE_POINT:
				{
					pointIndex = MY_RAND( wcOut.m_NbrBluePoints );
					ControlPointFeature::t_ControlPoint pt;
					pt.x = wcOut.blue[ pointIndex ].x;
					pt.y = wcOut.blue[ pointIndex ].y;

                    pt.x = pt.x + ( 5 - MY_RAND(10) );
					pt.y = pt.y + ( 5 - MY_RAND(10) );

					pt.x = CHECK_RANGE(pt.x, 0, oWidth);
					pt.y = CHECK_RANGE(pt.y, 0, oHeight);

					wcOut.blue[pointIndex].x = pt.x;
					wcOut.blue[pointIndex].y = pt.y;
				}
				break;
			}

		}
		break;
	case ControlPointGeneticWeakLearnerInterface::MUTATE_ADD_POINT:
		{
			ControlPointFeature::t_ControlPointColor pointColor;

			// RANDOM SIDE: RED OR BLUE
			pointColor = (ControlPointFeature::t_ControlPointColor) MY_RAND( 2 );

			// DEPENDS ON THE SIDE, ADD RANDOM DELTA TO RANDOM POINT
			switch( pointColor )
			{
			case ControlPointFeature::CP_RED_POINT:
				{
					if(wcOut.m_NbrRedPoints < (CPCLASSIFIER_MAXPOINT-1)){
						// WE CAN ADD THEN THE NEW POINT : RANDOM
                        ControlPointFeature::t_ControlPoint pt;
						pt.x = MY_RAND(oWidth);
						pt.y = MY_RAND(oHeight);

						// ADD THE POINT
						wcOut.AddRedPoint( pt );
					}
				}
				break;
			case ControlPointFeature::CP_BLUE_POINT:
				{
					if(wcOut.m_NbrBluePoints < (CPCLASSIFIER_MAXPOINT-1)){
						// WE CAN ADD THEN THE NEW POINT : RANDOM
                        ControlPointFeature::t_ControlPoint pt;
						pt.x = MY_RAND(oWidth);
						pt.y = MY_RAND(oHeight);

						// ADD THE POINT
						wcOut.AddBluePoint( pt );
					}
				}
				break;
			}

		}
		break;
	case ControlPointGeneticWeakLearnerInterface::MUTATE_REMOVE_POINT:
		{
			ControlPointFeature::t_ControlPointColor pointColor;

			// RANDOM SIDE: RED OR BLUE
			pointColor = (ControlPointFeature::t_ControlPointColor) MY_RAND( 2 );

			// DEPENDS ON THE SIDE, ADD RANDOM DELTA TO RANDOM POINT
			switch( pointColor )
			{
			case ControlPointFeature::CP_RED_POINT:
				{
					wcOut.RemoveLastRedPoint();
				}
				break;
			case ControlPointFeature::CP_BLUE_POINT:
				{
					wcOut.RemoveLastBluePoint();
				}
				break;
			}

		}
		break;
	case ControlPointGeneticWeakLearnerInterface::MUTATE_CHANGE_CHANNEL:
		{
			// SELECT RANDOM CHANNEL
		}
		break;
	case ControlPointGeneticWeakLearnerInterface::MUTATE_CHANGE_THRESHOILD:
		{
			// SLECT RANDOM THRESHOLD
		}
		break;
	}
}

⌨️ 快捷键说明

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