Skip to content

Commit

Permalink
add more checks
Browse files Browse the repository at this point in the history
  • Loading branch information
a-zakir committed Jan 8, 2025
1 parent f2eb23b commit a71db79
Showing 1 changed file with 138 additions and 98 deletions.
236 changes: 138 additions & 98 deletions src/tests/src/solver/optimisation/constraints/constraints_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,155 +32,195 @@
#include "antares/antares/constants.h"



struct BB
{
int nombreDePasDeTempsPourUneOptimisation = 10;

std::vector <double> Pi = std::vector(2*nombreDePasDeTempsPourUneOptimisation, 0.0); // Placeholder for coefficients
std::vector<int> Colonne = std::vector (2*nombreDePasDeTempsPourUneOptimisation, 0); // Placeholder for column indices
int nombreDeContraintes = 0;
int nombreDeTermesDansLaMatriceDeContrainte = 0;
std::vector<int> IndicesDebutDeLigne = std::vector(4, 0);
SparseVector<double> CoefficientsDeLaMatriceDesContraintes; //= std::vector(100, 0.0); // Plain vector
SparseVector<int> IndicesColonnes; //= std::vector<int> (100, 0); // Plain vector for column indices
std::vector<int> NombreDeTermesDesLignes = std::vector<int>(4, 0);
std::string Sens = std::string(4, '='); // Placeholder for constraint senses
int IncrementDAllocationMatriceDesContraintes = 10;
std::vector<CORRESPONDANCES_DES_VARIABLES> CorrespondanceVarNativesVarOptim;;
std::vector<double> Pi = std::vector(2 * nombreDePasDeTempsPourUneOptimisation, 0.0);
// Placeholder for coefficients
std::vector<int> Colonne = std::vector(2 * nombreDePasDeTempsPourUneOptimisation, 0);
// Placeholder for column indices
int nombreDeContraintes = 0;
int nombreDeTermesDansLaMatriceDeContrainte = 0;
std::vector<int> IndicesDebutDeLigne = std::vector(4, 0);
SparseVector<double> CoefficientsDeLaMatriceDesContraintes;
//= std::vector(100, 0.0); // Plain vector
SparseVector<int> IndicesColonnes;
//= std::vector<int> (100, 0); // Plain vector for column indices
std::vector<int> NombreDeTermesDesLignes = std::vector<int>(4, 0);
std::string Sens = std::string(4, '='); // Placeholder for constraint senses
int IncrementDAllocationMatriceDesContraintes = 10;
std::vector<CORRESPONDANCES_DES_VARIABLES> CorrespondanceVarNativesVarOptim;;

void set_correspondances_des_variables()
{
CorrespondanceVarNativesVarOptim.resize(nombreDePasDeTempsPourUneOptimisation);
for (auto i = 0; i < nombreDePasDeTempsPourUneOptimisation; i++)
{
CorrespondanceVarNativesVarOptim[i].SIM_ShortTermStorage = {.InjectionVariable = {0,1}, .WithdrawalVariable = {2,3} };
CorrespondanceVarNativesVarOptim[i].SIM_ShortTermStorage = {
.InjectionVariable = {0, 1}, .WithdrawalVariable = {2, 3}};
}
}

//.S {{.SIM_ShortTermStorage = {.InjectionVariable = {0,1}, .WithdrawalVariable = {2,3} } }};
const int32_t NombreDePasDeTempsPourUneOptimisation = nombreDePasDeTempsPourUneOptimisation; // Example value
std::vector<int> NumeroDeVariableStockFinal = std::vector<int>(10, -1);
std::vector<std::vector<int> > NumeroDeVariableDeTrancheDeStock = std::vector<std::vector<int>> (10, std::vector<int>(5, -1));
std::vector<std::string> NomDesContraintes = std::vector<std::string>(100, "");
const bool NamedProblems = true;
const std::vector<const char*> NomsDesPays = {"CountryA", "CountryB"};
const uint32_t weekInTheYear = 1; // Example week
const uint32_t NombreDePasDeTemps = 168; // Example number of time steps in a week
const int32_t NombreDePasDeTempsPourUneOptimisation = nombreDePasDeTempsPourUneOptimisation;
// Example value
std::vector<int> NumeroDeVariableStockFinal = std::vector<int>(10, -1);
std::vector<std::vector<int> > NumeroDeVariableDeTrancheDeStock = std::vector<std::vector<
int> >(10, std::vector<int>(5, -1));
std::vector<std::string> NomDesContraintes = std::vector<std::string>(100, "");
const bool NamedProblems = true;
const std::vector<const char*> NomsDesPays = {"CountryA", "CountryB"};
const uint32_t weekInTheYear = 1; // Example week
const uint32_t NombreDePasDeTemps = 168; // Example number of time steps in a week

// Mock data storage


std::vector<Antares::Data::ShortTermStorage::SingleAdditionalConstraint> addc1_withdrawal_constraints = {
{.hours= {1,2},
.globalIndex = 0,
.localIndex = 0 },
{.hours= {3,4},
.globalIndex = 1,
.localIndex = 1 }};

std::vector<Antares::Data::ShortTermStorage::SingleAdditionalConstraint> addc2_injection_constraints = {
{.hours= {5,6},
.globalIndex = 2,
.localIndex = 0 },
{.hours= {7,8},
.globalIndex = 3,
.localIndex = 1 }};

std::vector<double> fill_rhs()
{
std::vector<double> ret (HOURS_PER_YEAR);
std::iota (ret.begin(), ret.end(), 0);
return ret;
};
Antares::Data::ShortTermStorage::AdditionalConstraints addc1_withdrawal = {.name = "addc1_withdrawal", .cluster_id= "cluster_1", .variable = "withdrawal", .operatorType = "less", .rhs = fill_rhs(),.constraints = addc1_withdrawal_constraints };
Antares::Data::ShortTermStorage::AdditionalConstraints addc2_injection = {.name = "addc2_injection", .cluster_id= "cluster_2", .variable = "injection", .operatorType = "greater", .rhs = fill_rhs(),.constraints = addc2_injection_constraints };


::ShortTermStorage::PROPERTIES storage1 = {.additionalConstraints = {addc1_withdrawal} ,.clusterGlobalIndex = 0, .name= "cluster_1"} ;
::ShortTermStorage::PROPERTIES storage2 = { .additionalConstraints = {addc2_injection},.clusterGlobalIndex = 1, .name= "cluster_2"};
std::vector<Antares::Data::ShortTermStorage::SingleAdditionalConstraint>
addc1_withdrawal_constraints = {
{.hours = {1, 2},
.globalIndex = 0,
.localIndex = 0},
{.hours = {3, 4},
.globalIndex = 1,
.localIndex = 1}};

std::vector<Antares::Data::ShortTermStorage::SingleAdditionalConstraint>
addc2_injection_constraints = {
{.hours = {5, 6},
.globalIndex = 2,
.localIndex = 0},
{.hours = {7, 8},
.globalIndex = 3,
.localIndex = 1}};

std::vector<double> fill_rhs()
{
std::vector<double> ret(HOURS_PER_YEAR);
std::iota(ret.begin(), ret.end(), 0);
return ret;
};
Antares::Data::ShortTermStorage::AdditionalConstraints addc1_withdrawal = {
.name = "addc1_withdrawal", .cluster_id = "cluster_1", .variable = "withdrawal",
.operatorType = "less", .rhs = fill_rhs(), .constraints = addc1_withdrawal_constraints};
Antares::Data::ShortTermStorage::AdditionalConstraints addc2_injection = {
.name = "addc2_injection", .cluster_id = "cluster_2", .variable = "injection",
.operatorType = "greater", .rhs = fill_rhs(),
.constraints = addc2_injection_constraints};


::ShortTermStorage::PROPERTIES storage1 = {.additionalConstraints = {addc1_withdrawal},
.clusterGlobalIndex = 0, .name = "cluster_1"};
::ShortTermStorage::PROPERTIES storage2 = {.additionalConstraints = {addc2_injection},
.clusterGlobalIndex = 1, .name = "cluster_2"};

std::vector<CORRESPONDANCES_DES_CONTRAINTES> CorrespondanceCntNativesCntOptim;
std::vector<::ShortTermStorage::AREA_INPUT> shortTermStorage = InitializeShortTermStorageData();
CORRESPONDANCES_DES_CONTRAINTES_HEBDOMADAIRES CorrespondanceCntNativesCntOptimHebdomadaires{{},{0,1,2}};
CORRESPONDANCES_DES_CONTRAINTES_HEBDOMADAIRES CorrespondanceCntNativesCntOptimHebdomadaires{
{}, {0, 0, 0}};


std::vector<ShortTermStorage::AREA_INPUT> InitializeShortTermStorageData()
{
return { {storage1} , {storage2 }};
return {{storage1}, {storage2}};
}


ShortTermStorageCumulativeConstraintData shorttermstoragecumulativeconstraintdata = InitializeShortTermStorageCumulativeConstraintData();


ShortTermStorageCumulativeConstraintData InitializeShortTermStorageCumulativeConstraintData(){
ShortTermStorageCumulativeConstraintData shorttermstoragecumulativeconstraintdata =
InitializeShortTermStorageCumulativeConstraintData();

return { CorrespondanceCntNativesCntOptim, shortTermStorage, CorrespondanceCntNativesCntOptimHebdomadaires };

}
ShortTermStorageCumulativeConstraintData InitializeShortTermStorageCumulativeConstraintData()
{
return {CorrespondanceCntNativesCntOptim, shortTermStorage,
CorrespondanceCntNativesCntOptimHebdomadaires};
}

ConstraintBuilderData constraint_builder_data= InitializeConstraintBuilderData();
ConstraintBuilderData constraint_builder_data = InitializeConstraintBuilderData();

ConstraintBuilderData InitializeConstraintBuilder()
{
return ConstraintBuilderData(constraint_builder_data);
}
ConstraintBuilderData InitializeConstraintBuilderData()
ConstraintBuilderData InitializeConstraintBuilder()
{
return ConstraintBuilderData(constraint_builder_data);
}

{
set_correspondances_des_variables();

// Create the mock ConstraintBuilderData object
return{
Pi,
Colonne,
nombreDeContraintes,
nombreDeTermesDansLaMatriceDeContrainte,
IndicesDebutDeLigne,
CoefficientsDeLaMatriceDesContraintes,
IndicesColonnes,
NombreDeTermesDesLignes,
Sens,
IncrementDAllocationMatriceDesContraintes,
CorrespondanceVarNativesVarOptim,
NombreDePasDeTempsPourUneOptimisation,
NumeroDeVariableStockFinal,
NumeroDeVariableDeTrancheDeStock,
NomDesContraintes,
NamedProblems,
NomsDesPays,
weekInTheYear,
NombreDePasDeTemps};
ConstraintBuilderData InitializeConstraintBuilderData()

}
{
set_correspondances_des_variables();

// Create the mock ConstraintBuilderData object
return {
Pi,
Colonne,
nombreDeContraintes,
nombreDeTermesDansLaMatriceDeContrainte,
IndicesDebutDeLigne,
CoefficientsDeLaMatriceDesContraintes,
IndicesColonnes,
NombreDeTermesDesLignes,
Sens,
IncrementDAllocationMatriceDesContraintes,
CorrespondanceVarNativesVarOptim,
NombreDePasDeTempsPourUneOptimisation,
NumeroDeVariableStockFinal,
NumeroDeVariableDeTrancheDeStock,
NomDesContraintes,
NamedProblems,
NomsDesPays,
weekInTheYear,
NombreDePasDeTemps};
}
};


BOOST_FIXTURE_TEST_CASE(AddWithdrawalConstraint, BB)
{
ConstraintBuilder builder ( constraint_builder_data);
ConstraintBuilder builder(constraint_builder_data);

ShortTermStorageCumulation cumulation(builder, shorttermstoragecumulativeconstraintdata);

// Call the add method for "CountryA" (index 0)
cumulation.add(0);
// Assert that the number of constraints has increased by the expected amount
// Assuming 2 additional constraints (from addc1_withdrawal) should be added
BOOST_CHECK_EQUAL(builder.data.nombreDeContraintes, 2);

// Verify that the constraint names are correctly generated and stored
BOOST_CHECK_EQUAL(builder.data.NomDesContraintes[0], "WithdrawalSum::area<CountryA>::ShortTermStorage<cluster_1>::Constraint<addc1_withdrawal_0>"); // Assuming this is the generated name
BOOST_CHECK_EQUAL(builder.data.NomDesContraintes[1], "WithdrawalSum::area<CountryA>::ShortTermStorage<cluster_1>::Constraint<addc1_withdrawal_1>"); // Check the second constraint
BOOST_CHECK_EQUAL(builder.data.NomDesContraintes[0],
"WithdrawalSum::area<CountryA>::ShortTermStorage<cluster_1>::Constraint<addc1_withdrawal_0>")
; // Assuming this is the generated name
BOOST_CHECK_EQUAL(builder.data.NomDesContraintes[1],
"WithdrawalSum::area<CountryA>::ShortTermStorage<cluster_1>::Constraint<addc1_withdrawal_1>")
; // Check the second constraint
//
// // Verify that the correct number of terms have been added to the matrix
BOOST_CHECK_EQUAL(builder.data.nombreDeTermesDansLaMatriceDeContrainte, 4);

// Verify that the correct indices for constraints have been set
BOOST_CHECK_EQUAL(builder.data.IndicesDebutDeLigne[0], 0); // Assuming this is the starting index
BOOST_CHECK_EQUAL(builder.data.IndicesDebutDeLigne[1], 2); // Check next line index
BOOST_CHECK_EQUAL(builder.data.IndicesDebutDeLigne[0], 0);
// Assuming this is the starting index
BOOST_CHECK_EQUAL(builder.data.IndicesDebutDeLigne[1], 2); // Check next line index

// Verify that the correct variables and values were updated in the matrix
BOOST_CHECK_EQUAL(builder.data.Pi[0], 1.0); // Verify the first term's coefficient (adjust based on actual expected values)
BOOST_CHECK_EQUAL(builder.data.Pi[1], 1.0); // Verify the first term's coefficient (adjust based on actual expected values)
BOOST_CHECK_EQUAL(builder.data.Colonne[0], 2); // Verify the first term's column index
BOOST_CHECK_EQUAL(builder.data.Colonne[1], 2); // Verify the first term's column index
}
BOOST_CHECK_EQUAL(builder.data.Pi[0], 1.0);
// Verify the first term's coefficient (adjust based on actual expected values)
BOOST_CHECK_EQUAL(builder.data.Pi[1], 1.0);
// Verify the first term's coefficient (adjust based on actual expected values)
BOOST_CHECK_EQUAL(builder.data.Colonne[0], 2); // Verify the first term's column index
BOOST_CHECK_EQUAL(builder.data.Colonne[1], 2); // Verify the first term's column index

// Check if the sense of constraints was updated correctly
BOOST_CHECK_EQUAL(builder.data.Sens[0], '<');
BOOST_CHECK_EQUAL(builder.data.Sens[1], '<');

// 4. Validate correspondence mapping
BOOST_CHECK_EQUAL(
shorttermstoragecumulativeconstraintdata
.CorrespondanceCntNativesCntOptimHebdomadaires.ShortTermStorageCumulation[0],
0);
BOOST_CHECK_EQUAL(
shorttermstoragecumulativeconstraintdata.
CorrespondanceCntNativesCntOptimHebdomadaires.ShortTermStorageCumulation[1],
1);
}

0 comments on commit a71db79

Please sign in to comment.