?? approximation.cpp
字號(hào):
//-------------------------------------------------------------------
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Revisions.....:
//===================================================================
#include <stdafx.h> // Precompiled headers.
#include <copyright.h>
#include <kernel/structures/approximation.h>
#include <kernel/utilities/iokit.h>
#include <kernel/utilities/creator.h>
//-------------------------------------------------------------------
// Static methods (file scope).
//===================================================================
//-------------------------------------------------------------------
// Method........: StaticSetPointers
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Sets the pointers (handles) in one array to point
// similarly as they do in the other. Used for
// duplication and assignment.
// Comments......: Assumes the array to set has been emptied.
// Revisions.....:
//===================================================================
static bool
StaticSetPointers(Handle<EquivalenceClasses> set1, const Handle<EquivalenceClasses> universe1,
const Handle<EquivalenceClasses> set2, const Handle<EquivalenceClasses> universe2) {
int i, no_classes = set2->GetNoEquivalenceClasses();
for (i = 0; i < no_classes; i++) {
// Find out to where into the universe the pointer points.
int index = universe2->FindMember(set2->GetEquivalenceClass(i));
if (index == Undefined::Integer()) {
Message::Error("Could not set pointer array properly.");
return false;
}
// Set the other pointer accordingly.
set1->AppendStructure(universe1->GetEquivalenceClass(index));
}
return true;
}
//-------------------------------------------------------------------
// Method........: StaticSaveSet
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Saves the size and indices only of the given set.
// Pointers can then easily be reconstructed later.
// Comments......:
// Revisions.....:
//===================================================================
static bool
StaticSaveSet(ofstream &stream, const Handle<EquivalenceClasses> set, const Handle<EquivalenceClasses> universe) {
int i, no_classes = set->GetNoEquivalenceClasses();
if (!IOKit::Save(stream, no_classes))
return false;
if (!IOKit::Save(stream, '\n'))
return false;
for (i = 0; i < no_classes; i++) {
// Find out to where into the universe the pointer points.
int index = universe->FindMember(set->GetEquivalenceClass(i));
if (index == Undefined::Integer()) {
Message::Error("Could not locate pointer into universe.");
return false;
}
// Save the index only.
if (!IOKit::Save(stream, index))
return false;
}
if (!IOKit::Save(stream, '\n'))
return false;
return true;
}
//-------------------------------------------------------------------
// Method........: StaticLoadSet
// Author........: Aleksander 豩rn
// Date..........:
// Description...: Inverse of StaticSaveSet.
// Comments......:
// Revisions.....:
//===================================================================
static bool
StaticLoadSet(ifstream &stream, Handle<EquivalenceClasses> set, const Handle<EquivalenceClasses> universe) {
int i, no_classes, index;
// Load the number of eq. classes to append.
if (!IOKit::Load(stream, no_classes))
return false;
for (i = 0; i < no_classes; i++) {
// Load the index.
if (!IOKit::Load(stream, index))
return false;
// Append an eq. class.
if (!set->AppendStructure(universe->GetEquivalenceClass(index)))
return false;
}
return true;
}
//-------------------------------------------------------------------
// Method........: StaticGetRatio
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Description...: Returns the ratio between the number of objects in
// the given eq. class set that are correctly classified
// by the approximation, and the total number of objects
// with the decision value that was attempted approximated.
//
// Comments......: The counter and denominator of the ratio can optionally
// be returned in-place.
//
// So that sensitivity and specificty computation share the
// same implementation.
// Revisions.....:
//===================================================================
static float
StaticGetRatio(Handle<EquivalenceClasses> set, int decision, bool equality,
int *no_correct, int *no_total, const DecisionTable *table) {
if (no_correct != NULL)
*no_correct = 0;
if (no_total != NULL)
*no_total = 0;
// Was the originating decision table supplied?
if (table == NULL) {
Message::Error("Could not acquire originating decision table.");
return Undefined::Float();
}
// Operate on a masked table.
bool masked = true;
// Acquire the index of the table's decision attribute.
int decision_attribute = table->GetDecisionAttribute(masked);
if (decision_attribute == Undefined::Integer()) {
Message::Error("Could not acquire the index of the decision attribute in the originating decision table.");
return Undefined::Float();
}
int no_classes = set->GetNoEquivalenceClasses();
int counter = 0;
int i, j;
// Run through the set and count how many objects that are correctly classified.
for (i = 0; i < no_classes; i++) {
Handle<EquivalenceClass> eqclass = set->GetEquivalenceClass(i);
int no_objects = eqclass->GetNoObjects();
for (j = 0; j < no_objects; j++) {
int object_no = eqclass->GetObject(j);
int entry = table->GetEntry(object_no, decision_attribute, masked);
if (equality && (entry == decision))
counter++;
else if (!equality && (entry != decision))
counter++;
}
}
if (no_correct != NULL)
*no_correct = counter;
int no_objects = table->GetNoObjects(masked);
int denominator = 0;
// Run through the table and count how many objects that actually belong (or don't belong)
// to the set of object with the decision that was attempted approximated.
for (i = 0; i < no_objects; i++) {
int entry = table->GetEntry(i, decision_attribute, masked);
if (equality && (entry == decision))
denominator++;
else if (!equality && (entry != decision))
denominator++;
}
if (no_total != NULL)
*no_total = denominator;
// Avoid division by zero.
if (denominator == 0)
return Undefined::Float();
// Safe-guard to avoid numerical quirks.
if (counter == denominator)
return 1.0;
return static_cast(float, counter) / denominator;
}
//-------------------------------------------------------------------
// Methods for class Approximation.
//===================================================================
//-------------------------------------------------------------------
// Constructors/destructor.
//===================================================================
//-------------------------------------------------------------------
// Method........: Copy constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Approximation::Approximation(const Approximation &in) : AnnotatedStructure(in) {
decision_ = in.decision_;
// Copy (i.e. physically duplicate) the equivalence class sets.
universe_ = dynamic_cast(EquivalenceClasses *, in.universe_->Duplicate());
// Instantiate eq. class sets.
lower_ = Creator::EquivalenceClasses();
upper_ = Creator::EquivalenceClasses();
boundary_ = Creator::EquivalenceClasses();
outside_ = Creator::EquivalenceClasses();
// 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_);
}
//-------------------------------------------------------------------
// Method........: Constructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
Approximation::Approximation() {
decision_ = Undefined::Integer();
// Instantiate eq. class sets.
universe_ = Creator::EquivalenceClasses();
lower_ = Creator::EquivalenceClasses();
upper_ = Creator::EquivalenceClasses();
boundary_ = Creator::EquivalenceClasses();
outside_ = Creator::EquivalenceClasses();
}
//-------------------------------------------------------------------
// Method........: Destructor.
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......: The handle mechanism should take care of the
// garbage collection.
// Revisions.....:
//===================================================================
Approximation::~Approximation() {
}
//-------------------------------------------------------------------
// Methods inherited from Identifier.
//===================================================================
IMPLEMENTIDMETHODS(Approximation, APPROXIMATION, AnnotatedStructure)
//-------------------------------------------------------------------
// Methods inherited from Persistent.
//===================================================================
//-------------------------------------------------------------------
// Method........: Load
// Author........: Aleksander 豩rn
// Date..........:
// Description...:
// Comments......:
// Revisions.....:
//===================================================================
bool
Approximation::Load(ifstream &stream) {
// Clear current contents.
Clear();
// Load stuff higher up.
if (!AnnotatedStructure::Load(stream))
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -