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

📄 brorthogonalscaler.cpp

📁 粗糙集应用软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================

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

#include <kernel/algorithms/brorthogonalscaler.h>
#include <kernel/algorithms/naivescaler.h>
#include <kernel/algorithms/johnsonreducer.h>
#include <kernel/algorithms/keyword.h>

#include <kernel/structures/decisiontable.h>
#include <kernel/structures/discernibilityfunction.h>
#include <kernel/structures/booleansopfunction.h>

#include <kernel/basic/set.h>
#include <kernel/basic/algorithm.h>
#include <kernel/basic/message.h>

//-------------------------------------------------------------------
// Static methods (file scope).
//===================================================================

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

static bool
StaticCopySetToVector(const STLSet(int) &s, Vector(int) &v) {

	v.erase(v.begin(), v.end());
	v.reserve(s.size());

	STLSet(int)::const_iterator i = s.begin();

	while (!(i == s.end())) {
		v.push_back(*i);
		i++;
	}

	return true;

}

//-------------------------------------------------------------------
// Methods for class BROrthogonalScaler.
//===================================================================

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

BROrthogonalScaler::BROrthogonalScaler() {
	ApproximateSolutions(false);
	SetHittingFraction(0.95f);
}

BROrthogonalScaler::~BROrthogonalScaler() {
}

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

IMPLEMENTIDMETHODS(BROrthogonalScaler, BRORTHOGONALSCALER, OrthogonalScaler)

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

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

String
BROrthogonalScaler::GetParameters() const {

	String parameters = OrthogonalScaler::GetParameters() + Keyword::Separator();

	// Approximate solutions.
	parameters += Keyword::Approximate();
	parameters += Keyword::Assignment();
	parameters += String::Format(ApproximateSolutions());

	if (ApproximateSolutions()) {

		// Hitting fraction.
		parameters += Keyword::Separator();
		parameters += Keyword::Fraction();
		parameters += Keyword::Assignment();
		parameters += String::Format(GetHittingFraction());

	}

	return parameters;

}

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

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

	if (OrthogonalScaler::SetParameter(keyword, value))
		return true;

	// Approximate solutions.
	if (keyword == Keyword::Approximate() && value.IsBoolean())
		return ApproximateSolutions(value.GetBoolean());

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

	return false;

}

//-------------------------------------------------------------------
// Methods inherited from OrthogonalScaler.
//===================================================================

//-------------------------------------------------------------------
// Method........: Discretize
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Computes the cuts and prepares the stage for
//                 calls to the FindCuts method from the
//                 OrthogonalScaler::Discretize method.
// Comments......:
// Revisions.....:
//===================================================================

DecisionTable *
BROrthogonalScaler::Discretize(DecisionTable &table) const {

	Message message;

	message.Notify("Computing candidate cuts and regions...");

	Vector(ACPair) candidates;
	Vector(Region) regions;

	// Find all candidate cuts.
	if (!FindCuts(table, candidates, regions))
		return NULL;

#ifdef _DEBUG
	Message::Debug("Found " + String::Format(candidates.size()) + " candidates.");
	Message::Debug("Found " + String::Format(regions.size()) + " regions.");
#endif

	message.Notify("Building discernibility function...");

	DiscernibilityFunction function;

	// Build a suitable discernibility function.
	if (!BuildFunction(table, candidates, regions, function))
		return NULL;

#ifdef _DEBUG
	Message::Debug("Function has " + String::Format(function.GetNoSums()) + " sums.");
#endif

	message.Notify("Computing prime implicant...");

	BooleanSOPFunction primeimplicant;

	// Compute the "optimal" candidate subset.
	if (!ComputePrimeImplicant(function, primeimplicant))
		return NULL;

	message.Notify("Filtering candidate set...");

	// This method is conceptually const only.
	BROrthogonalScaler *self = const_cast(BROrthogonalScaler *, this);

	// Select the winner cuts from the set of candidates.
	if (!self->FilterCuts(table, candidates, primeimplicant))
		return NULL;

	// Do the actual discretization.
	return OrthogonalScaler::Discretize(table);

}

//-------------------------------------------------------------------
// Method........: FindCuts
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the cuts for the specified attribute.
//                 The cuts were computed in the Discretize method.
// Comments......:
// Revisions.....:
//===================================================================

bool
BROrthogonalScaler::FindCuts(const DecisionTable &/*table*/, int attribute_no, bool masked, Cuts &cuts) const {

	// Masking has been assumed.
	if (!masked)
		return false;

#ifdef _DEBUG
	if (attribute_no < 0 || attribute_no >= cuts_.size()) {
		Message::Error("Index passed to FindCuts method outside allowed range.");
		return false;
	}
#endif

	cuts = cuts_[attribute_no];

	return true;

}

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

//-------------------------------------------------------------------
// Method........: FindCuts
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Computes and returns (in-place) all candidate cuts
//                 in a flattened list of (attribute, cut) pairs.
//
//                 Also returns (in-place) a set of representative
//                 objects per region, along with the regions'
//                 (generalized) decision values.
// Comments......:
// Revisions.....:
//===================================================================

bool
BROrthogonalScaler::FindCuts(const DecisionTable &table, Vector(ACPair) &candidates, Vector(Region) &regions) const {

	int i, j;

	// Operate on a masked table.

⌨️ 快捷键说明

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