//--------------------------------------------------------------------------- // Count the number of distortions. // Each time a record is generalized from a child value to a parent value, // we charge 1 unit of distortion. So if 100 records are involved in the // generalization, we charge 100 unit. //--------------------------------------------------------------------------- bool CTDEvalMgr::countNumDistortions(int& catDistortion, float& contDistortion) { cout << _T("Counting number of distortions...") << endl; catDistortion = 0; contDistortion = 0.0f; int nRecs = 0, nValues = 0; CTDRecord* pRec = NULL; CTDAttrib* pAttrib = NULL; CTDPartition* pPartition = NULL; CTDValue* pValue = NULL; CTDConcept* pCurrentConcept = NULL; CTDConcept* pRawConcept = NULL; CTDPartitions* pLeafPartitions = m_pPartitioner->getLeafPartitions(); // For each partition. for (POSITION leafPos = pLeafPartitions->GetHeadPosition(); leafPos != NULL;) { pPartition = pLeafPartitions->GetNext(leafPos); nRecs = pPartition->getNumRecords(); // For each record. for (int r = 0; r < nRecs; ++r) { pRec = pPartition->getRecord(r); nValues = pRec->getNumValues(); // For each value. for (int v = 0; v < nValues; ++v) { pAttrib = m_pAttribMgr->getAttribute(v); if (!pAttrib->m_bVirtualAttrib) continue; pValue = pRec->getValue(v); pCurrentConcept = pValue->getCurrentConcept(); if (pAttrib->isContinuous()) { CTDContConcept* pContConcept = (CTDContConcept*) pCurrentConcept; CTDContConcept* pRoot = (CTDContConcept*) pAttrib->getConceptRoot(); contDistortion += (pContConcept->m_upperBound - pContConcept->m_lowerBound) / (pRoot->m_upperBound - pRoot->m_lowerBound); } else { pRawConcept = ((CTDStringValue*) pValue)->getRawConcept(); #if defined(_TD_SCORE_FUNTION_TRANSACTION) // In case of transaction data, count a distortion only if suppressing "1". if (pRawConcept->m_conceptValue.CompareNoCase(TD_TRANSACTION_ITEM_PRESENT) != 0) continue; #endif if (pRawConcept->m_depth < 0 || pCurrentConcept->m_depth < 0) { cout << _T("CSAEvalMgr::countNumDistortions: Negative depth.") << endl; ASSERT(false); return false; } catDistortion += pRawConcept->m_depth - pCurrentConcept->m_depth; } } } } cout << _T("Counting number of distortions succeeded.") << endl; return true; }
//--------------------------------------------------------------------------- //--------------------------------------------------------------------------- bool CTDPartition::initGenRecords( CTDAttribs* pAttribs) { m_genRecords.cleanup(); int nAttribs = pAttribs->GetSize(); for (int classInd = 0; classInd < m_nClasses; ++classInd){ CTDAttrib* pAttrib = NULL; CTDValue* pNewValue = NULL; CTDRecord* pNewRecord = new CTDRecord(); for (int attribID = 0; attribID < nAttribs; ++attribID){ pNewValue = NULL; pAttrib = pAttribs->GetAt(attribID); if (pAttrib->isContinuous()) pNewValue = new CTDNumericValue(-1.0); else pNewValue = new CTDStringValue(); if (!pNewValue) { ASSERT(false); return false; } if (attribID == pAttribs->GetSize() - 1) { // Class attribute if (!pNewValue->assignGenClassValue(pAttrib, classInd)) return false; } else { // Ordinary attribute // Initialize the current concept to the root concept. if (!pNewValue->initConceptToRoot(pAttrib)) return false; } // Add the value to the record. if (!pNewRecord->addValue(pNewValue)) return false; } if (pNewRecord){ pNewRecord->setRecordID(m_genRecords.Add(pNewRecord)); } } if (m_genRecords.GetSize() != m_nClasses) { cerr << _T("CTDPartition::initGenRecords: Number of generalized record is not current.") << endl; return false; } return true; }
//--------------------------------------------------------------------------- // Distribute records from parent paritition to child partitions. //--------------------------------------------------------------------------- bool CTDPartitioner::testDistributeRecords(CTDPartition* pParentPartition, CTDAttrib* pSplitAttrib, CTDConcept* pSplitConcept, CTDPartitions& childPartitions) { childPartitions.RemoveAll(); // Construct a partition for each child concept. for (int childIdx = 0; childIdx < pSplitConcept->getNumChildConcepts(); ++childIdx) childPartitions.AddTail(new CTDPartition( gTestPartitionIndex++, m_pAttribMgr->getAttributes())); // Scan through each record in the parent partition and // add records to the corresponding child partition based // on the child concept. CTDRecord* pRec = NULL; CTDValue* pSplitValue = NULL; POSITION childPartitionPos = NULL; int childConceptIdx = -1; int splitIdx = pSplitAttrib->m_attribIdx; int nRecs = pParentPartition->getNumRecords(); ASSERT(nRecs > 0); for (int r = 0; r < nRecs; ++r) { pRec = pParentPartition->getRecord(r); pSplitValue = pRec->getValue(splitIdx); // Lower the concept by one level. if (!pSplitValue->lowerCurrentConcept()) { cerr << _T("CTDPartition: Should not specialize on this concept."); childPartitions.cleanup(); ASSERT(false); return false; } // Get the child concept of the current concept in this record. childConceptIdx = pSplitValue->getCurrentConcept()->m_childIdx; ASSERT(childConceptIdx != -1); childPartitionPos = childPartitions.FindIndex(childConceptIdx); ASSERT(childPartitionPos); // Add the record to this child partition. if (!childPartitions.GetAt(childPartitionPos)->addRecord(pRec)) { childPartitions.cleanup(); ASSERT(false); return false; } } // Delete empty child partitions. childPartitions.deleteEmptyPartitions(); return true; }