Skip to content

Commit

Permalink
WIP step 6
Browse files Browse the repository at this point in the history
Nettoyage du code
Repport de la correction
Synchornisation avec Carine pour la classe KWDataGridOptimizer
  • Loading branch information
marcboulle committed Oct 18, 2023
1 parent 78adba2 commit ecb8361
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 136 deletions.
31 changes: 16 additions & 15 deletions src/Learning/KWDataPreparation/KWDataGridOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,10 +1666,10 @@ double KWDataGridVNSOptimizer::VNSOptimizeDataGrid(const KWDataGrid* initialData
// CH IV Begin
double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid* initialDataGrid,
KWDataGridMerger* neighbourDataGrid,
double dNeighbourDataGridCost, KWDataGrid* mergedDataGrid,
double& dNeighbourDataGridCost,
KWDataGrid* mergedDataGrid,
KWDataGrid* partitionedReferencePostMergedDataGrid) const
{
double dCost;
double dMergedCost;
double dFusionDeltaCost;
KWDataGridManager dataGridManager;
Expand All @@ -1682,9 +1682,7 @@ double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid*
require(neighbourDataGrid->GetDataGridCosts() == dataGridCosts);
require(fabs(dNeighbourDataGridCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);

// Initialisations
dataGridManager.SetSourceDataGrid(initialDataGrid);
dCost = dNeighbourDataGridCost;
// Initialisation du cout
dMergedCost = dNeighbourDataGridCost;

// CH AB AF a voir avec Marc
Expand All @@ -1698,28 +1696,30 @@ double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid*
// Tri des attributs
neighbourDataGrid->SortAttributeParts();

// Initialisation du datagrid manager
dataGridManager.SetSourceDataGrid(neighbourDataGrid);

// Creation d'une nouvelle grille avec nouvelle description des PV fusionnees
dFusionDeltaCost =
dataGridManager.ExportDataGridWithVarPartMergeOptimization(mergedDataGrid, dataGridCosts);
assert(not mergedDataGrid->GetVarPartsShared());

// Calcul et verification du cout
dMergedCost = dCost + dFusionDeltaCost;
dMergedCost = dNeighbourDataGridCost + dFusionDeltaCost;

// Cas ou le cout de la grille avec PV voisines fusionnees est plus eleve que le cout avant
// fusion
if (dMergedCost > dCost * (1 + dEpsilon) and bDisplayResults)
if (dMergedCost > dNeighbourDataGridCost * (1 + dEpsilon) and bDisplayResults)
{
sLabel = "PROBLEME : degradation du cout lors de la fusion des parties de variables "
"contigues";
sLabel += DoubleToString(dMergedCost);
sLabel += "\t";
sLabel += DoubleToString(dMergedCost - dCost);
sLabel += DoubleToString(dMergedCost - dNeighbourDataGridCost);
AddWarning(sLabel);
cout << "PROBLEME : degradation du cout lors de la fusion des parties de variables "
"contigues : cout fusionne\t "
<< dMergedCost << "\tcout\t" << dCost << "\n";
<< dMergedCost << "\tcout\t" << dNeighbourDataGridCost << "\n";
cout << "Grille avant fusion\n";
neighbourDataGrid->Write(cout);
cout << "Grille apres fusion\n";
Expand Down Expand Up @@ -1792,14 +1792,14 @@ double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid*
}

// Parametrage du profiling
KWDataGridOptimizer::GetProfiler()->BeginMethod("Post-optimization IV");
KWDataGridOptimizer::GetProfiler()->WriteKeyString("Improvement number",
IntToString(nImprovementNumber));
NEWKWDataGridOptimizer::GetProfiler()->BeginMethod("Post-optimization IV");
NEWKWDataGridOptimizer::GetProfiler()->WriteKeyString("Improvement number",
IntToString(nImprovementNumber));

// Exploration des deplacements pour tous les attributs
bImprovement = varPartDataGridPostOptimizer.PostOptimizeLightVarPartDataGrid(
partitionedReferencePostMergedDataGrid, mergedDataGrid, &ivGroups);
KWDataGridOptimizer::GetProfiler()->EndMethod("Post-optimization IV");
NEWKWDataGridOptimizer::GetProfiler()->EndMethod("Post-optimization IV");

if (bImprovement)
{
Expand Down Expand Up @@ -1856,7 +1856,8 @@ double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid*
<< endl;
neighbourDataGrid->Write(cout);
}
dCost = dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid);
dNeighbourDataGridCost =
dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid);
}

// On remplace mergedDataGrid par la nouvelle grille integrant les
Expand All @@ -1882,7 +1883,7 @@ double KWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(const KWDataGrid*
cout << "Fin PostOptimisation VarPart" << endl;
}
}
ensure(fabs(dCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);
ensure(fabs(dNeighbourDataGridCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);
return dMergedCost;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Learning/KWDataPreparation/KWDataGridOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ class KWDataGridVNSOptimizer : public Object
// - neighbourDataGrid: grille courante en cours d'optimisation
// - dNeighbourDataGridCost: cout de la grille courante
// - mergedDataGrid: grille optimisee si amelioration
// En sortie, on rend la nouvelle version de la grille optimisee (ou courante) integrant la post-ptimisation et on renvoie son cout
// En sortie, la grille courante et son cout sont modifies suite a optimisation.
// On rend la grille issue de la post-opimisation VarPart, ainsi que sa grille partitionned re drefrence
// Le code retour est le meilleurs cout optenu apres post-optimisation VarPart
// Attention, celui-ci est different de celui de la grille courante optimisee
//
// A terme, il faudra isoler ce service de post-optimisation pour le deplacer en quatrime sous-methode
// en fin de la methode OptimizeSolution
Expand All @@ -261,7 +264,7 @@ class KWDataGridVNSOptimizer : public Object
// - Post-optimization
// - Post-optimization IV
double VNSDataGridPostOptimizeVarPart(const KWDataGrid* initialDataGrid, KWDataGridMerger* neighbourDataGrid,
double dNeighbourDataGridCost, KWDataGrid* mergedDataGrid,
double& dNeighbourDataGridCost, KWDataGrid* mergedDataGrid,
KWDataGrid* partitionedReferencePostMergedDataGrid) const;

// CH IV Begin
Expand Down
110 changes: 14 additions & 96 deletions src/Learning/KWDataPreparation/NEWKWDataGridOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,8 +1640,8 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeDataGrid(const KWDataGrid* initialD

// CH IV Begin
double NEWKWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(
const KWDataGrid* initialDataGrid, KWDataGridMerger* neighbourDataGrid, double& dCost, KWDataGrid* mergedDataGrid,
KWDataGrid* partitionedReferencePostMergedDataGrid) const
const KWDataGrid* initialDataGrid, KWDataGridMerger* neighbourDataGrid, double& dNeighbourDataGridCost,
KWDataGrid* mergedDataGrid, KWDataGrid* partitionedReferencePostMergedDataGrid) const
{
double dMergedCost;
double dFusionDeltaCost;
Expand All @@ -1653,11 +1653,10 @@ double NEWKWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(
require(initialDataGrid != NULL);
require(neighbourDataGrid != NULL);
require(neighbourDataGrid->GetDataGridCosts() == dataGridCosts);
require(fabs(dCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);
require(fabs(dNeighbourDataGridCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);

// Initialisations
dataGridManager.SetSourceDataGrid(initialDataGrid);
dMergedCost = dCost;
// Initialisation du cout
dMergedCost = dNeighbourDataGridCost;

// CH AB AF a voir avec Marc
// Cas ou l'on peut fusionner les parties de variable des cluster
Expand All @@ -1670,28 +1669,30 @@ double NEWKWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(
// Tri des attributs
neighbourDataGrid->SortAttributeParts();

// Initialisation du datagrid manager
dataGridManager.SetSourceDataGrid(neighbourDataGrid);

// Creation d'une nouvelle grille avec nouvelle description des PV fusionnees
dFusionDeltaCost =
dataGridManager.ExportDataGridWithVarPartMergeOptimization(mergedDataGrid, dataGridCosts);
assert(not mergedDataGrid->GetVarPartsShared());

// Calcul et verification du cout
dMergedCost = dCost + dFusionDeltaCost;
dMergedCost = dNeighbourDataGridCost + dFusionDeltaCost;

// Cas ou le cout de la grille avec PV voisines fusionnees est plus eleve que le cout avant
// fusion
if (dMergedCost > dCost * (1 + dEpsilon) and bDisplayResults)
if (dMergedCost > dNeighbourDataGridCost * (1 + dEpsilon) and bDisplayResults)
{
sLabel = "PROBLEME : degradation du cout lors de la fusion des parties de variables "
"contigues";
sLabel += DoubleToString(dMergedCost);
sLabel += "\t";
sLabel += DoubleToString(dMergedCost - dCost);
sLabel += DoubleToString(dMergedCost - dNeighbourDataGridCost);
AddWarning(sLabel);
cout << "PROBLEME : degradation du cout lors de la fusion des parties de variables "
"contigues : cout fusionne\t "
<< dMergedCost << "\tcout\t" << dCost << "\n";
<< dMergedCost << "\tcout\t" << dNeighbourDataGridCost << "\n";
cout << "Grille avant fusion\n";
neighbourDataGrid->Write(cout);
cout << "Grille apres fusion\n";
Expand Down Expand Up @@ -1828,7 +1829,8 @@ double NEWKWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(
<< endl;
neighbourDataGrid->Write(cout);
}
dCost = dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid);
dNeighbourDataGridCost =
dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid);
}

// On remplace mergedDataGrid par la nouvelle grille integrant les
Expand All @@ -1854,7 +1856,7 @@ double NEWKWDataGridVNSOptimizer::VNSDataGridPostOptimizeVarPart(
cout << "Fin PostOptimisation VarPart" << endl;
}
}
ensure(fabs(dCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);
ensure(fabs(dNeighbourDataGridCost - dataGridCosts->ComputeDataGridTotalCost(neighbourDataGrid)) < dEpsilon);
return dMergedCost;
}

Expand Down Expand Up @@ -1886,8 +1888,6 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
// Test du remplacement de la methode actuelle, par son proto
boolean bDeprecatedVersion = false;
//DDD bDeprecatedVersion = true;
cout << "DEPRECATED: (G=" << initialDataGrid->GetGranularity()
<< ", T=" << initialDataGrid->GetInnerAttributes()->GetVarPartGranularity() << ")" << endl;
if (bDeprecatedVersion)
return DEPRECATED_VNSOptimizeVarPartDataGrid(initialDataGrid, dDecreaseFactor, nMinIndex, nMaxIndex,
optimizedDataGrid, dOptimizedDataGridCost,
Expand Down Expand Up @@ -1925,9 +1925,6 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
dCost = OptimizeSolution(initialDataGrid, &neighbourDataGrid);
NEWKWDataGridOptimizer::GetProfiler()->EndMethod("Optimize solution");

//DDD
cout << "dCost\t" << dCost << endl;

// Post-optimisation des parties de variables de la grille
// A terme, a deplacer dans OptimizeSolution
dMergedCost = dCost;
Expand All @@ -1936,21 +1933,12 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
VNSDataGridPostOptimizeVarPart(initialDataGrid, &neighbourDataGrid, dCost, &mergedDataGrid,
&partitionedReferencePostMergedDataGrid);

//DDD
cout << dOptimizedDataGridCost << "\t" << dCost << "\t" << dMergedCost << "\t" << dBestMergedCost
<< "\t" << (dMergedCost < dBestMergedCost - dEpsilon) << endl;

// Si amelioration: on la memorise
if (dMergedCost < dBestMergedCost - dEpsilon)
{
dBestCost = dCost;
dBestMergedCost = dMergedCost;

//DDD
cout << "\tNEW BEST\t" << dBestCost << "\t" << dBestMergedCost << endl;
cout << "\tneighbourDataGrid(" << neighbourDataGrid.GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(&neighbourDataGrid) << ")" << endl;

// Sauvegarde de la meilleure solution
if (bCleanNonInformativeVariables)
{
Expand All @@ -1961,27 +1949,17 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
// reference est necessaire pour HandleOptimizationStep
dataGridManager.CopyInformativeDataGrid(&neighbourDataGrid, optimizedDataGrid);
}

else
{
dataGridManager.CopyDataGrid(&neighbourDataGrid, optimizedDataGrid);
}

//DDD
cout << "\toptimizedDataGrid(" << optimizedDataGrid->GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(optimizedDataGrid) << ")" << endl;

// Gestion de la meilleure solution
if (dataGridOptimizer != NULL)
{
//DDD
cout << "\tmergedDataGrid(" << mergedDataGrid.GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(&mergedDataGrid) << ")" << endl;

if (mergedDataGrid.GetInformativeAttributeNumber() == 0)
dataGridOptimizer->HandleOptimizationStep(optimizedDataGrid, initialDataGrid,
false);

else
{
dataGridManager.SetSourceDataGrid(
Expand All @@ -1991,22 +1969,6 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
dataGridOptimizer->HandleOptimizationStep(
&mergedDataGrid, &partitionedReferencePostMergedDataGrid, false);

//DDD
cout << "\tGetDataGridOptimizer()->GetInitialVarPartDataGrid()("
<< GetDataGridOptimizer()
->GetInitialVarPartDataGrid()
->GetInformativeAttributeNumber()
<< ", "
<< dataGridCosts->ComputeDataGridTotalCost(
GetDataGridOptimizer()->GetInitialVarPartDataGrid())
<< ")" << endl;
cout << "\tpartitionedReferencePostMergedDataGrid("
<< partitionedReferencePostMergedDataGrid.GetInformativeAttributeNumber()
<< ", "
<< dataGridCosts->ComputeDataGridTotalCost(
&partitionedReferencePostMergedDataGrid)
<< ")" << endl;

// Nettoyage
partitionedReferencePostMergedDataGrid.DeleteAll();
}
Expand All @@ -2032,10 +1994,6 @@ double NEWKWDataGridVNSOptimizer::VNSOptimizeVarPartDataGrid(const KWDataGrid* i
break;
}

//DDD
cout << "=>\t" << dBestMergedDataGridCost << "\t" << dBestMergedCost << "\t" << dBestCost << "\t"
<< dataGridCosts->ComputeDataGridTotalCost(optimizedDataGrid) << endl;

// Memorisation du meilleur cout parmi les grilles post-fusionnees
dBestMergedDataGridCost = dBestMergedCost;
ensure(fabs(dBestCost - dataGridCosts->ComputeDataGridTotalCost(optimizedDataGrid)) < dEpsilon);
Expand Down Expand Up @@ -2100,9 +2058,6 @@ double NEWKWDataGridVNSOptimizer::DEPRECATED_VNSOptimizeVarPartDataGrid(const KW
dCost = OptimizeSolution(initialDataGrid, &neighbourDataGrid);
NEWKWDataGridOptimizer::GetProfiler()->EndMethod("Optimize solution");

//DDD
cout << "dCost\t" << dCost << endl;

// CH AB AF a voir avec Marc
// Cas ou l'on peut fusionner les parties de variable des cluster
// Probleme rencontre pour fusionner une grille non informative
Expand Down Expand Up @@ -2309,21 +2264,12 @@ double NEWKWDataGridVNSOptimizer::DEPRECATED_VNSOptimizeVarPartDataGrid(const KW
else
dMergedCost = dCost;

//DDD
cout << dOptimizedDataGridCost << "\t" << dCost << "\t" << dMergedCost << "\t" << dBestMergedCost
<< "\t" << (dMergedCost < dBestMergedCost - dEpsilon) << endl;

// Si amelioration: on la memorise
if (dMergedCost < dBestMergedCost - dEpsilon)
{
dBestCost = dCost;
dBestMergedCost = dMergedCost;

//DDD
cout << "\tNEW BEST\t" << dBestCost << "\t" << dBestMergedCost << endl;
cout << "\tneighbourDataGrid(" << neighbourDataGrid.GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(&neighbourDataGrid) << ")" << endl;

// Sauvegarde de la meilleure solution
if (bCleanNonInformativeVariables)
{
Expand All @@ -2339,17 +2285,9 @@ double NEWKWDataGridVNSOptimizer::DEPRECATED_VNSOptimizeVarPartDataGrid(const KW
dataGridManager.CopyDataGrid(&neighbourDataGrid, optimizedDataGrid);
}

//DDD
cout << "\toptimizedDataGrid(" << optimizedDataGrid->GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(optimizedDataGrid) << ")" << endl;

// Gestion de la meilleure solution
if (dataGridOptimizer != NULL)
{
//DDD
cout << "\tmergedDataGrid(" << mergedDataGrid.GetInformativeAttributeNumber() << ", "
<< dataGridCosts->ComputeDataGridTotalCost(&mergedDataGrid) << ")" << endl;

if (mergedDataGrid.GetInformativeAttributeNumber() == 0)
dataGridOptimizer->HandleOptimizationStep(optimizedDataGrid, initialDataGrid,
false);
Expand All @@ -2362,22 +2300,6 @@ double NEWKWDataGridVNSOptimizer::DEPRECATED_VNSOptimizeVarPartDataGrid(const KW
dataGridOptimizer->HandleOptimizationStep(
&mergedDataGrid, &partitionedReferencePostMergedDataGrid, false);

//DDD
cout << "\tGetDataGridOptimizer()->GetInitialVarPartDataGrid()("
<< GetDataGridOptimizer()
->GetInitialVarPartDataGrid()
->GetInformativeAttributeNumber()
<< ", "
<< dataGridCosts->ComputeDataGridTotalCost(
GetDataGridOptimizer()->GetInitialVarPartDataGrid())
<< ")" << endl;
cout << "\tpartitionedReferencePostMergedDataGrid("
<< partitionedReferencePostMergedDataGrid.GetInformativeAttributeNumber()
<< ", "
<< dataGridCosts->ComputeDataGridTotalCost(
&partitionedReferencePostMergedDataGrid)
<< ")" << endl;

// Nettoyage
partitionedReferencePostMergedDataGrid.DeleteAll();
}
Expand All @@ -2404,10 +2326,6 @@ double NEWKWDataGridVNSOptimizer::DEPRECATED_VNSOptimizeVarPartDataGrid(const KW
break;
}

//DDD
cout << "=>\t" << dBestMergedDataGridCost << "\t" << dBestMergedCost << "\t" << dBestCost << "\t"
<< dataGridCosts->ComputeDataGridTotalCost(optimizedDataGrid) << endl;

// Memorisation du meilleur cout parmi les grilles post-fusionnees
dBestMergedDataGridCost = dBestMergedCost;

Expand Down
Loading

0 comments on commit ecb8361

Please sign in to comment.