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

📄 voter.cpp

📁 粗慥集成算法集合 ,并有详细的文档资料和测试数据处
💻 CPP
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........: 9608/9609
// Description...:
// Revisions.....:
//===================================================================

#include <stdafx.h> // Precompiled headers.
#include <copyright.h>

#include <kernel/algorithms/voter.h>
#include <kernel/algorithms/keyword.h>

#include <kernel/structures/informationvector.h>
#include <kernel/structures/rulebasedclassification.h>

#include <kernel/utilities/creator.h>
#include <kernel/utilities/discerner.h>

#include <kernel/basic/message.h>

//-------------------------------------------------------------------
// Methods for class Voter.
//===================================================================

//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================

//-------------------------------------------------------------------
// Method........: Constructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Voter::Voter() {
	fraction_        = 0.0;
	idg_             = false;
	filename_idg_    = Undefined::String();
}

//-------------------------------------------------------------------
// Method........: Destructor
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

Voter::~Voter() {
}

//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================

IMPLEMENTIDMETHODS(Voter, VOTER, RuleBasedClassifier)

//-------------------------------------------------------------------
// Methods inherited from Algorithm.
//===================================================================

//-------------------------------------------------------------------
// Method........: GetParameters
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

String
Voter::GetParameters() const {

	String parameters;

	// Get parameters higher up.
	parameters += RuleBasedClassifier::GetParameters();

	parameters += Keyword::Separator();

	// Min. matching fraction.
	parameters += Keyword::Fraction();
	parameters += Keyword::Assignment();
	parameters += String::Format(GetFraction());

	parameters += Keyword::Separator();

	// Use IDG?
	parameters += Keyword::IDG();
	parameters += Keyword::Assignment();
	parameters += String::Format(UseIDG());

	// IDG filename.
	if (UseIDG()) {
		parameters += Keyword::Separator();
		parameters += Keyword::IDG() + Keyword::Dot() + Keyword::Filename();
		parameters += Keyword::Assignment();
		parameters += GetIDGFilename();
	}

	return parameters;

}

//-------------------------------------------------------------------
// Method........: SetParameter
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
Voter::SetParameter(const String &keyword, const String &value) {

	// Fraction.
	if (keyword == Keyword::Fraction() && value.IsFloat())
		return SetFraction(value.GetFloat());

	// Fraction, backwards compatibility..
	if (keyword == Keyword::Tolerance() && value.IsFloat())
		return SetFraction(1 - value.GetFloat());

	// IDG.
	if (keyword == Keyword::IDG() && value.IsBoolean())
		return UseIDG(value.GetBoolean());

	// IDG filename.
	if (keyword == Keyword::IDG() + Keyword::Dot() + Keyword::Filename())
		return SetIDGFilename(value);

	// Perhaps higher up?
	return RuleBasedClassifier::SetParameter(keyword, value);

}

//-------------------------------------------------------------------
// Method........: Apply
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Takes an information vector as input, classifies
//                 it according to the preset rules, and creates and
//                 returns a rule-based classification structure.
//
// Comments......: Assumes that library clients use handles.
// Revisions.....:
//===================================================================

Structure *
Voter::Apply(Structure &structure) const {

	// Check input type.
	if (!IsApplicable(structure))
		return NULL;

	// Cast to verified type.
	Handle<InformationVector> inf = dynamic_cast(InformationVector *, &structure);

	// Are any rules present?
	if (GetRules() == NULL) {
		Message::Warning("Cannot classify, no classification rules set.", false);
		return NULL;
	}

	if (GetRules()->GetNoRules() == 0) {
		Message::Warning("Empty rule set.", false);
		return NULL;
	}

  /*
#ifdef _DEBUG
	// Check dimensions.
  Handle<DecisionTable> table = dynamic_cast(DecisionTable *, GetRules()->FindParent(DECISIONTABLE));

	if (table.IsNULL()) {
		Message::Debug("Unable to access the rules' originating decision table.");
		return NULL;
	}

	bool masked = true;

	if (inf->GetNoAttributes() != table->GetNoAttributes(masked)) {
		Message::Debug("Dimensionality mismatch between the information vector\n"
			             "to classify and the rules' originating decision table.");
		return NULL;
	}
#endif
  */

	return Classify(*inf);

}

//-------------------------------------------------------------------
// Methods inherited from RuleBasedClassifier.
//===================================================================

//-------------------------------------------------------------------
// Method........: SetRules
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
Voter::SetRules(const Rules *rules) {
	rules_ = rules;
	return true;
}

//-------------------------------------------------------------------
// New virtual methods.
//===================================================================

//-------------------------------------------------------------------
// Method........: Classify
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Takes an information vector as input, classifies
//                 it according to the preset rules, and creates and
//                 returns a rule-based classification structure.
// Comments......:
// Revisions.....:
//===================================================================

RuleBasedClassification *
Voter::Classify(const InformationVector &inf) const {

#ifdef _DEBUG
	if (GetRules() == NULL) {
		Message::Warning("Cannot classify, no classification rules set.", false);
		return NULL;
	}

	if (GetRules()->GetNoRules() == 0) {
		Message::Warning("Empty rule set.", false);
		return NULL;
	}
#endif

	Vector(int) indices;

	// Determine the set of rules that fire.
	if (!EliminateRules(indices, inf, *GetRules(), GetFraction()))
		return NULL;

	return Classify(inf, indices);

}

//-------------------------------------------------------------------
// Method........: Classify
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Called from other Classify method. Explicitly
//                 provides the set of firing rules (or rather, their
//                 indices).
// Comments......:
// Revisions.....:
//===================================================================

RuleBasedClassification *
Voter::Classify(const InformationVector &/*inf*/, const Vector(int) &indices) const {

#ifdef _DEBUG
	if (GetRules() == NULL) {
		Message::Warning("Cannot classify, no classification rules set.", false);
		return NULL;
	}

	if (GetRules()->GetNoRules() == 0) {
		Message::Warning("Empty rules set.", false);
		return NULL;
	}
#endif

	// Did any rules fire?
	if (indices.empty()) {
		Message::Debug("No rules fired.");
		return NULL;
	}

	VoteMap votemap; // Maps from decision values to total number of votes (for that decision).
	RuleMap rulemap; // Maps from decision values to firing rules.

	// Fill maps.
	if (!FillMaps(votemap, rulemap, indices, *GetRules()))
		return NULL;

	Handle<RuleBasedClassification> classification = Creator::RuleBasedClassification();

	// Fill classification structure.
	if (!FillClassification(*classification, votemap, rulemap, *GetRules()))
		return NULL;

	classification->Sort();

	return classification.Release();

}

//-------------------------------------------------------------------
// Local methods.
//===================================================================

//-------------------------------------------------------------------
// Method........: EliminateRules
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns (in-place) the indices of those rules that
//                 fire.
//
// Comments......:
// Revisions.....: A

⌨️ 快捷键说明

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