📄 approximation.cpp
字号:
return false;
// Load the decision value.
if (!IOKit::Load(stream, decision_))
return false;
// Load the universe (the basic building blocks).
if (!universe_->Load(stream))
return false;
// Load the indices and set the pointers.
if (!StaticLoadSet(stream, lower_, universe_) ||
!StaticLoadSet(stream, upper_, universe_) ||
!StaticLoadSet(stream, boundary_, universe_) ||
!StaticLoadSet(stream, outside_, universe_))
return false;
return true;
}
//-------------------------------------------------------------------
// Method........: Save
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Approximation::Save(ofstream &stream) const {
// Save stuff higher up.
if (!AnnotatedStructure::Save(stream))
return false;
// Save the decision value.
if (!IOKit::Save(stream, decision_))
return false;
// Save the universe (i.e. the primitive building blocks).
if (!universe_->Save(stream))
return false;
// Save sizes and indices into the universe only (eases later reconstruction).
if (!StaticSaveSet(stream, lower_, universe_) ||
!StaticSaveSet(stream, upper_, universe_) ||
!StaticSaveSet(stream, boundary_, universe_) ||
!StaticSaveSet(stream, outside_, universe_))
return false;
return true;
}
//-------------------------------------------------------------------
// Methods inherited from Structure.
//===================================================================
//-------------------------------------------------------------------
// Method........: Duplicate
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Structure *
Approximation::Duplicate() const {
return new Approximation(*this);
}
//-------------------------------------------------------------------
// Method........: Clear
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
void
Approximation::Clear() {
// Clear stuff higher up.
AnnotatedStructure::Clear();
// Clear the decision value.
decision_ = Undefined::Integer();
// Clear the eq. class sets.
universe_->Clear();
lower_->Clear();
upper_->Clear();
boundary_->Clear();
outside_->Clear();
}
//-------------------------------------------------------------------
// Method........: Assignment operator
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Assign annotation/parent stuff from higher up,
// too...?
// Revisions.....:
//===================================================================
Approximation &
Approximation::operator=(const Approximation &in) {
// Clear present contents.
Clear();
// Copy the decision value.
decision_ = in.decision_;
// Copy (i.e. physically duplicate) the equivalence class sets.
universe_ = dynamic_cast(EquivalenceClasses *, in.universe_->Duplicate());
// Set pointers into the universe.
StaticSetPointers(lower_, universe_, in.lower_, in.universe_);
StaticSetPointers(upper_, universe_, in.upper_, in.universe_);
StaticSetPointers(boundary_, universe_, in.boundary_, in.universe_);
StaticSetPointers(outside_, universe_, in.outside_, in.universe_);
return *this;
}
//-------------------------------------------------------------------
// Method........: Equality operator
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Don't bother to test the "redundant" sets.
// Revisions.....:
//===================================================================
bool
Approximation::operator==(const Approximation &in) const {
if (this == &in)
return true;
#if 0
// Is it the same set that's approximated?
if (GetDecisionValue() != in.GetDecisionValue())
return false;
#endif
int this_size = GetLowerApproximation()->GetNoEquivalenceClasses();
int that_size = in.GetLowerApproximation()->GetNoEquivalenceClasses();
if (this_size != that_size)
return false;
int i;
// Are the lower boundaries the same?
for (i = 0; i < this_size; i++) {
if (*(GetLowerApproximation()->GetEquivalenceClass(i)) != *(in.GetLowerApproximation()->GetEquivalenceClass(i)))
return false;
}
this_size = GetUpperApproximation()->GetNoEquivalenceClasses();
that_size = in.GetUpperApproximation()->GetNoEquivalenceClasses();
if (this_size != that_size)
return false;
// Are the upper boundaries the same?
for (i = 0; i < this_size; i++) {
if (*(GetUpperApproximation()->GetEquivalenceClass(i)) != *(in.GetUpperApproximation()->GetEquivalenceClass(i)))
return false;
}
this_size = GetUniverse()->GetNoEquivalenceClasses();
that_size = in.GetUniverse()->GetNoEquivalenceClasses();
if (this_size != that_size)
return false;
// Are the universes the same?
for (i = 0; i < this_size; i++) {
if (*(GetUniverse()->GetEquivalenceClass(i)) != *(in.GetUniverse()->GetEquivalenceClass(i)))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: Inequality operator
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Approximation::operator!=(const Approximation &in) const {
return !(*this == in);
}
//-------------------------------------------------------------------
// Method........: IsRough
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Approximation::IsRough() const {
if ((GetUpperApproximation() == NULL) || (GetLowerApproximation() == NULL)) {
Message::Error("The upper or lower approximation is NULL.");
return false;
}
int upper_no_classes = GetUpperApproximation()->GetNoEquivalenceClasses();
int lower_no_classes = GetLowerApproximation()->GetNoEquivalenceClasses();
return (upper_no_classes != lower_no_classes);
}
//-------------------------------------------------------------------
// Method........: IsCrisp
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Approximation::IsCrisp() const {
return !IsRough();
}
//-------------------------------------------------------------------
// Method........: GetSensitivity
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Computes the sensitivity of the approximation.
// Comments......:
// Revisions.....:
//===================================================================
float
Approximation::GetSensitivity(int *no_correct, int *no_total) const {
return StaticGetRatio(lower_, decision_, true, no_correct, no_total, dynamic_cast(DecisionTable *, FindParent(DECISIONTABLE)));
}
//-------------------------------------------------------------------
// Method........: GetSpecificity
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: Computes the specificity of the approximation.
// Revisions.....:
//===================================================================
float
Approximation::GetSpecificity(int *no_correct, int *no_total) const {
return StaticGetRatio(outside_, decision_, false, no_correct, no_total, dynamic_cast(DecisionTable *, FindParent(DECISIONTABLE)));
}
//-------------------------------------------------------------------
// Method........: GetAccuracy
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Computes the accuracy of the approximation.
// Comments......: Accuracy can be seen as a weighting between
// sensitivity and specificity. Can be optimized.
// Revisions.....:
//===================================================================
float
Approximation::GetAccuracy(int *no_correct, int *no_total) const {
if (no_correct != NULL)
*no_correct = 0;
if (no_total != NULL)
*no_total = 0;
int no_correct_sensitivity = 0;
int no_correct_specificity = 0;
int no_total_sensitivity = 0;
int no_total_specificity = 0;
// Calculate sensitivity.
float sensitivity = GetSensitivity(&no_correct_sensitivity, &no_total_sensitivity);
if (sensitivity == Undefined::Float())
return Undefined::Float();
// Calculate specificity.
float specificity = GetSpecificity(&no_correct_specificity, &no_total_specificity);
if (specificity == Undefined::Float())
return Undefined::Float();
int counter = no_correct_sensitivity + no_correct_specificity;
int denominator = no_total_sensitivity + no_total_specificity;
if (no_correct != NULL)
*no_correct = counter;
if (no_total != NULL)
*no_total = denominator;
return static_cast(float, counter) / denominator;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -