Skip to content

Commit

Permalink
Stable and performant SNB
Browse files Browse the repository at this point in the history
  • Loading branch information
folmos-at-orange committed Mar 15, 2024
1 parent 37acfcb commit 3357f95
Show file tree
Hide file tree
Showing 19 changed files with 4,329 additions and 2,538 deletions.
3 changes: 2 additions & 1 deletion src/Learning/KIInterpretation/KIDRPredictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ void KIDRClassifierInterpretation::Compile(KWClass* kwcOwnerClass)
const KWDRDataGridStats* dataGridStats = classifier->GetDataGridStatsAt(nDataGridIndex);

// Recherche de l'index de la partie cible de la grille
int nTargetIndex = classifier->GetDataGridSetTargetIndexAt(nDataGridIndex, nClassIndex);
int nTargetIndex =
classifier->GetDataGridSetTargetCellIndexAt(nDataGridIndex, nClassIndex);

// Parcours de toutes les parties sources
for (int nSourceIndex = 0; nSourceIndex < dataGridStats->GetDataGridSourceCellNumber();
Expand Down
19 changes: 18 additions & 1 deletion src/Learning/KWDataPreparation/KWDRDataGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,23 @@ int KWDRDataGrid::GetUncheckedAttributeNumber() const
return nUncheckedAttributeNumber;
}

int KWDRDataGrid::ComputeUncheckedTotalFrequency() const
{
KWDerivationRule* dataGridFrequenciesGenericRule;
KWDRFrequencies* dataGridFrequenciesRule;

require(GetOperandNumber() > 1);

// Erreur si pas de regle de derivation dans l'operande destinee aux frequences (le dernier)
dataGridFrequenciesGenericRule = GetOperandAt(GetOperandNumber() - 1)->GetDerivationRule();
if (dataGridFrequenciesGenericRule == NULL)
return -1;

// Calcul de l'effectif total de la grille de reference
dataGridFrequenciesRule = cast(KWDRFrequencies*, dataGridFrequenciesGenericRule);
return dataGridFrequenciesRule->ComputeTotalFrequency();
}

KWDerivationRule* KWDRDataGrid::Create() const
{
return new KWDRDataGrid;
Expand Down Expand Up @@ -1303,7 +1320,7 @@ boolean KWDRDataGridRule::CheckOperandsCompleteness(const KWClass* kwcOwnerClass
return bOk;
}

boolean KWDRDataGridRule::CheckPredictorCompletness(int nPredictorType, const KWClass* kwcOwnerClass) const
boolean KWDRDataGridRule::CheckPredictorCompleteness(int nPredictorType, const KWClass* kwcOwnerClass) const
{
boolean bOk = true;
int nDataGridAttributeNumber;
Expand Down
5 changes: 4 additions & 1 deletion src/Learning/KWDataPreparation/KWDRDataGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ class KWDRDataGrid : public KWDerivationRule
// Nombre de parties d'un attribut, en mode non checke
int GetUncheckedAttributePartNumberAt(int nAttributeIndex) const;

// Calcul de l'effectif total de la grille, en mode non checke (renvoie -1 si erreur)
int ComputeUncheckedTotalFrequency() const;

//////////////////////////////////////////////////////
// Redefinition des methodes standard

Expand Down Expand Up @@ -278,7 +281,7 @@ class KWDRDataGridRule : public KWDerivationRule
// (moins un de la grille), pour la prediction de la derniere dimension
// Verification egalement du type du dernier argument
// Symbol pour un classifier, Continuous pour un regresseur
boolean CheckPredictorCompletness(int nPredictorType, const KWClass* kwcOwnerClass) const;
boolean CheckPredictorCompleteness(int nPredictorType, const KWClass* kwcOwnerClass) const;

// Verification que la grille est univariee et qu'il y a un argument,
// ce qui correspond a la specification d'un partitionnement elementaire de l'attribut cible
Expand Down
190 changes: 114 additions & 76 deletions src/Learning/KWDataPreparation/KWDRDataGridBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ KWDRDataGridBlock::~KWDRDataGridBlock() {}

int KWDRDataGridBlock::GetUncheckedVarKeyNumber() const
{
KWDRContinuousValueSet continuousValueSetRule;
KWDRSymbolValueSet symbolValueSetRule;
KWDRContinuousValueSet refContinuousValueSetRule;
KWDRSymbolValueSet refSymbolValueSetRule;

// Erreur si pas d'operande
if (GetOperandNumber() == 0)
Expand All @@ -55,9 +55,9 @@ int KWDRDataGridBlock::GetUncheckedVarKeyNumber() const
GetFirstOperand()->GetDerivationRule() != NULL and
GetFirstOperand()->GetStructureName() == GetFirstOperand()->GetDerivationRule()->GetName())
{
if (GetFirstOperand()->GetStructureName() == continuousValueSetRule.GetName())
if (GetFirstOperand()->GetStructureName() == refContinuousValueSetRule.GetName())
return cast(KWDRContinuousValueSet*, GetFirstOperand()->GetDerivationRule())->GetValueNumber();
else if (GetFirstOperand()->GetStructureName() == symbolValueSetRule.GetName())
else if (GetFirstOperand()->GetStructureName() == refSymbolValueSetRule.GetName())
return cast(KWDRSymbolValueSet*, GetFirstOperand()->GetDerivationRule())->GetValueNumber();
else
return -1;
Expand Down Expand Up @@ -92,6 +92,21 @@ int KWDRDataGridBlock::GetUncheckedDataGridVarKeyType() const
return KWType::Unknown;
}

int KWDRDataGridBlock::ComputeUncheckedTotalFrequency(const KWClass* kwcOwnerClass) const
{
KWDRDataGrid* dataGridRule;

// Erreur si pas d'operandes data grid
if (GetOperandNumber() < 2)
return -1;

// On obtiens la frequence total non-checkee du premier operand data grid
// Le fait que tous les operands data grid ont la meme frequence total est verifie dans les Check*
dataGridRule = cast(KWDRDataGrid*, GetOperandAt(1)->GetReferencedDerivationRule(kwcOwnerClass));

return dataGridRule->ComputeUncheckedTotalFrequency();
}

KWDerivationRule* KWDRDataGridBlock::Create() const
{
return new KWDRDataGridBlock;
Expand Down Expand Up @@ -199,7 +214,8 @@ boolean KWDRDataGridBlock::CheckOperandsFamily(const KWDerivationRule* ruleFamil
{
bOk = false;
AddError(sTmp + "The number of VarKeys (" + IntToString(nVarKeyNumber) +
") in the first operand should be the same as the number of data grid operands (" +
") in the first operand " +
"should be the same as the number of data grid operands (" +
IntToString(GetOperandNumber() - 1) + ")");
}
}
Expand Down Expand Up @@ -284,73 +300,86 @@ boolean KWDRDataGridBlock::CheckOperandsFamily(const KWDerivationRule* ruleFamil
boolean KWDRDataGridBlock::CheckOperandsCompleteness(const KWClass* kwcOwnerClass) const
{
boolean bOk = true;
KWDRSymbolValueSet symbolValueSetRule;
KWDRContinuousValueSet continuousValueSetRule;
KWDRDataGridBlock datagridBlockSpecifiedFamily;
KWDRDataGrid* referencedDataGrid;
int nReferenceFirstType;
int nReferenceSecondType;
KWDRDataGrid* dataGridRule;
int nRefFirstType;
int nRefSecondType;
int nRefTotalFrequency;
int nDataGrid;
int nDataGridNumber;
int nFirstType;
int nSecondType;
int nIndex;
int nDataGridNumber;
int nTotalFrequency;
ALString sTmp;

require(kwcOwnerClass != NULL);
require(GetOperandNumber() > 0);

// Methode ancetre
// Appel a la methode ancetre
bOk = KWDerivationRule::CheckOperandsCompleteness(kwcOwnerClass);

// On verifie que chaque grille est bivariee (univarie supervise), avec les meme types pour toutes les grilles
// On verifie que :
// - chaque grille est bivariee (univariee supervisee), avec les meme types pour toutes les grilles
// - toutes les grilles ont le meme effectif
if (bOk)
{
nReferenceFirstType = KWType::Unknown;
nReferenceSecondType = KWType::Unknown;
nRefFirstType = KWType::Unknown;
nRefSecondType = KWType::Unknown;
nRefTotalFrequency = -1;
nDataGridNumber = GetOperandNumber() - 1;
for (nIndex = 0; nIndex < nDataGridNumber; nIndex++)
for (nDataGrid = 0; nDataGrid < nDataGridNumber; nDataGrid++)
{
referencedDataGrid =
cast(KWDRDataGrid*, GetOperandAt(nIndex + 1)->GetReferencedDerivationRule(kwcOwnerClass));
dataGridRule = cast(KWDRDataGrid*,
GetOperandAt(nDataGrid + 1)->GetReferencedDerivationRule(kwcOwnerClass));

// La grille doit avoir une seul attribut
if (bOk and referencedDataGrid->GetUncheckedAttributeNumber() != 2)
// La grille doit avoir seulement deux attributs (source et cible)
if (bOk and dataGridRule->GetUncheckedAttributeNumber() != 2)
{
bOk = false;
AddError(sTmp + "The data grid in the operand " + IntToString(nIndex + 2) +
AddError(sTmp + "The data grid in the operand " + IntToString(nDataGrid + 2) +
" should be bivariate");
}
if (not bOk)
break;

// Memorisation du type de la premiere grille rencontree
if (bOk and nIndex == 0)
if (nDataGrid == 0)
{
nReferenceFirstType = referencedDataGrid->GetUncheckedAttributeTypeAt(0);
nReferenceSecondType = referencedDataGrid->GetUncheckedAttributeTypeAt(1);
nRefFirstType = dataGridRule->GetUncheckedAttributeTypeAt(0);
nRefSecondType = dataGridRule->GetUncheckedAttributeTypeAt(1);
nRefTotalFrequency = dataGridRule->ComputeUncheckedTotalFrequency();
}

// Les deux attributs de cette grille doit etre du meme type pour toutes les grilles
if (bOk)
// Verification que les deux types de la grille courante soient les memes que ceux de la reference
nFirstType = dataGridRule->GetUncheckedAttributeTypeAt(0);
if (nFirstType != nRefFirstType)
{
nFirstType = referencedDataGrid->GetUncheckedAttributeTypeAt(0);
if (nFirstType != nReferenceFirstType)
{
bOk = false;
AddError(sTmp + "The type " + KWType::ToString(nFirstType) +
" of the first dimension of the data grid in the operand " +
IntToString(nIndex + 2) + " should be " +
KWType::ToString(nReferenceFirstType) +
" as in the first data grid operand");
}
nSecondType = referencedDataGrid->GetUncheckedAttributeTypeAt(1);
if (nSecondType != nReferenceSecondType)
{
bOk = false;
AddError(sTmp + "The type " + KWType::ToString(nSecondType) +
" of the second dimension of the data grid in the operand " +
IntToString(nIndex + 2) + " should be " +
KWType::ToString(nReferenceSecondType) +
" as in first data grid operand");
}
bOk = false;
AddError(sTmp + "The type " + KWType::ToString(nFirstType) +
" of the first dimension of the data grid at operand " +
IntToString(nDataGrid + 2) + " should be " + KWType::ToString(nRefFirstType) +
" as in the first data grid operand");
}
nSecondType = dataGridRule->GetUncheckedAttributeTypeAt(1);
if (nSecondType != nRefSecondType)
{
bOk = false;
AddError(sTmp + "The type " + KWType::ToString(nSecondType) +
" of the second dimension of the data grid at operand " +
IntToString(nDataGrid + 2) + " should be " + KWType::ToString(nRefSecondType) +
" as in first data grid operand");
}
if (not bOk)
break;

// Verification que l'effectif de la grille courant est egale a celui de la reference
nTotalFrequency = dataGridRule->ComputeUncheckedTotalFrequency();
if (nTotalFrequency != nRefTotalFrequency)
{
bOk = false;
AddError(sTmp + "The total frequency (" + IntToString(nTotalFrequency) + ") " +
" of the data grid at operand " + IntToString(nDataGrid + 2) +
" should be equal to that of the first data grid operand (" +
IntToString(nRefTotalFrequency) + ")");
}
if (not bOk)
break;
Expand Down Expand Up @@ -571,7 +600,7 @@ boolean KWDRDataGridBlockRule::CheckOperandsCompleteness(const KWClass* kwcOwner
return bOk;
}

boolean KWDRDataGridBlockRule::CheckPredictorCompletness(int nPredictorType, const KWClass* kwcOwnerClass) const
boolean KWDRDataGridBlockRule::CheckPredictorCompleteness(int nPredictorType, const KWClass* kwcOwnerClass) const
{
boolean bOk = true;
KWDRDataGridBlock* referencedDataGridBlock;
Expand Down Expand Up @@ -1165,10 +1194,7 @@ boolean KWDRCellIndexBlock::CheckBlockAttributesAt(const KWClass* kwcOwnerClass,
// Erreur si la VarKey de grille n'est pas trouvee
if (not bVarKeyFound)
{
// Preparation des informations sur la VarKey et la classe de scope
sExternalVarKey = attributeBlock->GetStringVarKey(checkedAttribute);

// Messages d'erreur
attributeBlock->AddError("Variable " + checkedAttribute->GetName() +
+" not found with its VarKey=" + sExternalVarKey +
" in data grid block first operand of rule " +
Expand Down Expand Up @@ -1321,11 +1347,22 @@ Object* KWDRDataGridStatsBlock::ComputeStructureResult(const KWObject* kwoObject
// correspondant a toutes les grilles
assert(ComputeUsedIndexNumber(&ivUsedRecodingDataGridIndexes) == oaAllRecodingDataGrids.GetSize());
if (GetSecondOperand()->GetType() == KWType::ContinuousValueBlock)
{
resultCellIndexBlock = BuildRecodedBlock(GetSecondOperand()->GetContinuousValueBlock(kwoObject),
KWType::ContinuousValueBlock, &ivUsedRecodingDataGridIndexes);
}
else
{
resultCellIndexBlock = BuildRecodedBlock(GetSecondOperand()->GetSymbolValueBlock(kwoObject),
KWType::SymbolValueBlock, &ivUsedRecodingDataGridIndexes);
}

// DDD
//cout << "INPUT" << endl;
//cout << *GetSecondOperand()->GetContinuousValueBlock(kwoObject) << endl;
//cout << "OUTPUT" << endl;
//cout << *resultCellIndexBlock << endl;
//cout << "------" << endl;

// On retourne la structure elle meme, pour disposer de ses services
return (Object*)this;
Expand All @@ -1337,14 +1374,14 @@ longint KWDRDataGridStatsBlock::GetUsedMemory() const

lUsedMemory = KWDRDataGridBlockRule::GetUsedMemory();
lUsedMemory += sizeof(KWDRDataGridStatsBlock) - sizeof(KWDRDataGridBlockRule);
lUsedMemory += oaAllDataGridStatsRules.GetOverallUsedMemory() - sizeof(ObjectArray);
lUsedMemory += oaDataGridStatsRules.GetOverallUsedMemory() - sizeof(ObjectArray);
return lUsedMemory;
}

void KWDRDataGridStatsBlock::Optimize(KWClass* kwcOwnerClass)
{
int i;
KWDRDataGridStats* dataGridStats;
int nDataGridStatsRule;
KWDRDataGridStats* dataGridStatsRule;
int nSourceValueType;

// Appel de la methode ancetre
Expand All @@ -1355,47 +1392,48 @@ void KWDRDataGridStatsBlock::Optimize(KWClass* kwcOwnerClass)

// Creation d'une regle DataGridStats par grille du DataGridBlock
CleanAllDataGridStatsRules();
oaAllDataGridStatsRules.SetSize(GetDataGridBlock()->GetDataGridNumber());
for (i = 0; i < oaAllDataGridStatsRules.GetSize(); i++)
oaDataGridStatsRules.SetSize(GetDataGridBlock()->GetDataGridNumber());
for (nDataGridStatsRule = 0; nDataGridStatsRule < oaDataGridStatsRules.GetSize(); nDataGridStatsRule++)
{
// Creation et memorisation d'une regle DataGridsStats
dataGridStats = new KWDRDataGridStats;
oaAllDataGridStatsRules.SetAt(i, dataGridStats);
dataGridStatsRule = new KWDRDataGridStats;
oaDataGridStatsRules.SetAt(nDataGridStatsRule, dataGridStatsRule);

// Parametregae de la grille en premier operande
dataGridStats->GetFirstOperand()->SetOrigin(KWDerivationRuleOperand::OriginRule);
dataGridStats->GetFirstOperand()->SetDerivationRule(GetDataGridBlock()->GetDataGridAt(i));
dataGridStatsRule->GetFirstOperand()->SetOrigin(KWDerivationRuleOperand::OriginRule);
dataGridStatsRule->GetFirstOperand()->SetDerivationRule(
GetDataGridBlock()->GetDataGridAt(nDataGridStatsRule));

// Parametrage du type de deuxieme operande
// Ce deuxieme operande ne sera pas utilise en pratique, mais on l'initialise correctement
// pour pouvoir compiler la regle
dataGridStats->GetSecondOperand()->SetOrigin(KWDerivationRuleOperand::OriginConstant);
dataGridStats->GetSecondOperand()->SetType(nSourceValueType);
dataGridStatsRule->GetSecondOperand()->SetOrigin(KWDerivationRuleOperand::OriginConstant);
dataGridStatsRule->GetSecondOperand()->SetType(nSourceValueType);

// Fianlsaition de la specification de la regle
dataGridStats->CompleteTypeInfo(kwcOwnerClass);
dataGridStatsRule->CompleteTypeInfo(kwcOwnerClass);

// Compilation de la regle
assert(dataGridStats->CheckCompleteness(kwcOwnerClass));
dataGridStats->Compile(kwcOwnerClass);
assert(dataGridStatsRule->CheckCompleteness(kwcOwnerClass));
dataGridStatsRule->Compile(kwcOwnerClass);
}
}

void KWDRDataGridStatsBlock::CleanAllDataGridStatsRules()
{
int i;
KWDRDataGridStats* dataGridStats;
int nDataGridStatsRule;
KWDRDataGridStats* dataGridStatsRule;

// On dereference prealablement les regles de type DataGrid en premier operande des DataGridStats
// pour quelques ne soit pas detruites une deuxieme fois par ces regles
for (i = 0; i < oaAllDataGridStatsRules.GetSize(); i++)
// Dereferencement au prealable des regles de type DataGrid en premier operande des DataGridStats
// pour qu'elles ne soient pas detruites une deuxieme fois
for (nDataGridStatsRule = 0; nDataGridStatsRule < oaDataGridStatsRules.GetSize(); nDataGridStatsRule++)
{
dataGridStats = cast(KWDRDataGridStats*, oaAllDataGridStatsRules.GetAt(i));
dataGridStats->GetFirstOperand()->SetDerivationRule(NULL);
dataGridStatsRule = cast(KWDRDataGridStats*, oaDataGridStatsRules.GetAt(nDataGridStatsRule));
dataGridStatsRule->GetFirstOperand()->SetDerivationRule(NULL);
}

// On peyut maintenant detruire ces regles
oaAllDataGridStatsRules.DeleteAll();
// Destruction des regles DataGridStats
oaDataGridStatsRules.DeleteAll();
}

///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1438,7 +1476,7 @@ Symbol KWDRDataGridStatsBlockTest::ComputeSymbolResult(const KWObject* kwoObject
check(referenceDataGridStatsBlock);

// Exploitation de ce resultats pour fabriquer un resultat en sortie
for (nValueIndex = 0; nValueIndex < referenceDataGridStatsBlock->GetCellIndexBlockSize(); nValueIndex++)
for (nValueIndex = 0; nValueIndex < referenceDataGridStatsBlock->GetValueNumber(); nValueIndex++)
{
if (nValueIndex > 0)
sDetailedResult += " ";
Expand Down
Loading

0 comments on commit 3357f95

Please sign in to comment.