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

📄 rule.cpp

📁 粗糙集应用软件
💻 CPP
📖 第 1 页 / 共 2 页
字号:

//-------------------------------------------------------------------
// Method........: GetCoverage
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the ratio between the total support of the
//                 decision values and the total number of objects in
//                 the originating table that belong to the same
//                 decision classes.
//
//                 An estimate of Pr(LHS | RHS).
//
// Comments......: A map that maps decision values to their total
//                 counts in the originating table must be supplied.
// Revisions.....:
//===================================================================

float
Rule::GetCoverage(const Vector(int) &decisions, const Vector(int) &cardinalities) const {

	int i, no_decision_values = GetNoDecisionValues();

	// No decision values?
	if (no_decision_values == 0)
		return Undefined::Float();

	int total_support = 0;
	int total_cardinality = 0;

	// Iterate over RHS.
	for (i = 0; i < no_decision_values; i++) {

		// Get decision value.
		int decision = GetDecisionValue(i);

		int cardinality;

		// Lookup cardinality.
		if (!StaticLookup(decision, decisions, cardinalities, cardinality))
			return Undefined::Float();

		// Get support value.
		int support = GetSupport(i);

		// Accumulate.
		total_support += support;
		total_cardinality += cardinality;

	}

	// Avoid numerical quirks.
	if (total_support == 0)
		return 0.0;
	else if (total_support == total_cardinality)
		return 1.0;

	// Avoid division by zero.
	if (total_cardinality == 0)
		return Undefined::Float();

	// Return coverage.
	return static_cast(float, total_support) / total_cardinality;

}

//-------------------------------------------------------------------
// Method........: GetCoverage
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Returns the ratio between the support of the
//                 decision value and the total number of objects in
//                 the originating table that belong to the same
//                 decision class.
//
//                 An estimate of Pr(LHS | RHS(i)).
//
// Comments......: A map that maps decision values to their total
//                 counts in the originating table must be supplied.
// Revisions.....:
//===================================================================

float
Rule::GetCoverage(int position_no, const Vector(int) &decisions, const Vector(int) &cardinalities) const {

	int cardinality;

	// Get decision value.
	int decision = GetDecisionValue(position_no);

	// Lookup cardinality.
	if (!StaticLookup(decision, decisions, cardinalities, cardinality))
		return Undefined::Float();

	// Get support value.
	int support = GetSupport(position_no);

	// Avoid numerical quirks.
	if (support == 0)
		return 0.0;
	else if (support == cardinality)
		return 1.0;

	// Avoid division by zero.
	if (cardinality == 0)
		return Undefined::Float();

	// Return coverage.
	return static_cast(float, support) / cardinality;

}

//------------------------------------------------------------------
// Method........: GetCoverage
// Author........:
// Date..........:
// Description...: Given the total number of objects in the table the
//                 rule/pattern was derived from, returns the
//                 rule's/pattern's LHS support divided by this number.
//
//                 An estimate of Pr(LHS).
// Comments......:
// Revisions.....:
//===================================================================

float
Rule::GetCoverage(int no_objects) const {

	if (no_objects <= 0)
		return Undefined::Float();

	return static_cast(float, GetSupport()) / no_objects;

}

//------------------------------------------------------------------
// Method........: GetCoverage
// Author........:
// Date..........:
// Description...:
// Comments......: A vector that contains the total counts for all
//                 possible decision classes in the originating table
//                 must be supplied.
// Revisions.....:
//===================================================================

float
Rule::GetCoverage(const Vector(int) &cardinalities) const {
	return GetCoverage(MathKit::Sum(cardinalities));
}

//-------------------------------------------------------------------
// Operators
//===================================================================

//-------------------------------------------------------------------
// Operator......: Assignment operator
// Author........:
// Date..........:
// Description...:
// Comments......: Presently, does not copy all associated
//                 quantitative rule/pattern data.
// Revisions.....:
//===================================================================

Rule &
Rule::operator=(const Rule &in) {

	int i;

	// Clear this rule.
	Clear();

	int no_condition_descriptors = in.GetNoConditionDescriptors();

	// Copy the condition attributes and their values.
	for (i = 0; i < no_condition_descriptors; i++)
		AppendConditionDescriptor(in.GetConditionAttribute(i), in.GetConditionValue(i));

	// Copy the decision attribute.
	SetDecisionAttribute(in.GetDecisionAttribute());

	int no_decision_values = in.GetNoDecisionValues();

	// Copy all decision values.
	for (i = 0; i < no_decision_values; i++)
		AppendDecisionValue(in.GetDecisionValue(i));

	Message::Warning("Not all data copied in rule assignment operator.");

	return *this;

}

//-------------------------------------------------------------------
// Operator......: Equality operator
// Author........:
// Date..........:
// Description...:
// Comments......: Does not compare associated numerical data.
// Revisions.....:
//===================================================================

bool
Rule::operator==(const Rule &in) const {

	// Are the two objects physically equal?
	if (&in == this)
		return true;

	int no_condition_descriptors = GetNoConditionDescriptors();

	// Are the conditional terms equal? (Assuming same order).
	if (no_condition_descriptors != in.GetNoConditionDescriptors())
		return false;

	int i;

	for (i = 0; i < no_condition_descriptors; i++)
		if (GetConditionAttribute(i) != in.GetConditionAttribute(i))
			return false;

	for (i = 0; i < no_condition_descriptors; i++)
		if (GetConditionValue(i) != in.GetConditionValue(i))
			return false;

	// Are the decision attributes equal?
	if (GetDecisionAttribute() != in.GetDecisionAttribute())
		return false;

	int no_decision_values = GetNoDecisionValues();

  // Are decision terms equal? (Assuming same order).
	if(GetNoDecisionValues() != in.GetNoDecisionValues())
		return false;

	for (i = 0; i < no_decision_values; i++)
		if (GetDecisionValue(i) != in.GetDecisionValue(i))
			return false;

  return true;

}

//-------------------------------------------------------------------
// Operator......: Inequality operator
// Author........:
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================

bool
Rule::operator!=(const Rule &in) const {
	return !(*this == in);
}

//-------------------------------------------------------------------
// Operator......: < operator
// Author........:
// Date..........:
// Description...: A rule R is said to be less general (or more
//                 specific) than a rule S, written R < S, if:
//
//                 i)  R's LHS is a proper superset of S's LHS, and
//                 ii) R's RHS is a subset of S's RHS
//
//                 Example:
//
//                   R1: (a, 1)           => (d, 0) || (d, 1)
//                   R2: (a, 1) && (b, 0) => (d, 0)
//                   R3: (a, 1) && (b, 1) =>           (d, 1)
//                   R4: (b, 0)           => (d, 0) || (d, 1)
//
//                   R1 "covers" R2, so R2 < R1.
//                   R1 "covers" R3, so R3 < R1.
//                   R4 "covers" R2, so R2 < R4.
//
// Comments......:
// Revisions.....:
//===================================================================

bool
Rule::operator<(const Rule &in) const {

	if (&in == this)
		return false;

	// Get LHS dimensions.
	int no_descriptors_this = GetNoConditionDescriptors();
	int no_descriptors_that = in.GetNoConditionDescriptors();

	// Can we exclude the LHS proper superset condition already now?
	if (no_descriptors_this <= no_descriptors_that)
		return false;

	int i, j;

	// Check LHS superset condition.
	for (i = 0; i < no_descriptors_that; i++) {

		int a = in.GetConditionAttribute(i);
		int v = in.GetConditionValue(i);

		bool found = false;

		for (j = 0; j < no_descriptors_this; j++) {
			if (GetConditionAttribute(j) == a && GetConditionValue(j) == v) {
				found = true;
				break;
			}
		}

		if (!found)
			return false;

	}

	// Get RHS dimensions.
	int no_decisions_this = GetNoDecisionValues();
	int no_decisions_that = in.GetNoDecisionValues();

	// Can we exclude the RHS subset condition already now?
	if (no_decisions_this > no_decisions_that)
		return false;

	if (GetDecisionAttribute() != in.GetDecisionAttribute())
		return false;

	// Check RHS subset condition.
	for (i = 0; i < no_decisions_this; i++) {

		int d = GetDecisionValue(i);

		bool found = false;

		for (j = 0; j < no_decisions_that; j++) {
			if (GetDecisionValue(j) == d) {
				found = true;
				break;
			}
		}

		if (!found)
			return false;

	}

	return true;

}

//-------------------------------------------------------------------
// Operator......: > operator
// Author........:
// Date..........:
// Description...: See < operator.
// Comments......:
// Revisions.....:
//===================================================================

bool
Rule::operator>(const Rule &in) const {
	return in < *this;
}

//-------------------------------------------------------------------
// Formatting methods.
//===================================================================

//-------------------------------------------------------------------
// Method........: Format
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: If a decision table with an associated dictionary
//                 is supplied, this is used.
//
// Revisions.....: A

⌨️ 快捷键说明

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