Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xpansion split mps #233

Open
wants to merge 5 commits into
base: New-Antares-Emulator-4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/libs/antares/study/mps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ FILE* Study::createFileIntoOutputWithExtension(const YString& prefix,
const YString& extension,
uint numSpace) const
{
static int count_problem = 0;
static int count_criterion = 0;
static std::map<YString, int> count;

// Empty log entry
logs.info();
Expand Down Expand Up @@ -76,17 +75,15 @@ FILE* Study::createFileIntoOutputWithExtension(const YString& prefix,
// logs.debug() << " !fd_test = " << (!fd_test) << " : FOR " << buffer.c_str();
if (fd_test)
{
if (prefix == "problem")
outputFile << "-" << (++count_problem) << "." << extension;
else
outputFile << "-" << (++count_criterion) << "." << extension;
count[prefix]++;
outputFile << "-" << count[prefix] << "." << extension;

buffer.clear() << this->folderOutput << SEP << outputFile;
fclose(fd_test);
}
else
{
count_problem = count_criterion = 0;
count.clear();

outputFile << "." << extension;
buffer.clear() << this->folderOutput << SEP << outputFile;
Expand Down
302 changes: 279 additions & 23 deletions src/solver/optimisation/opt_appel_solveur_lineaire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void OPT_EcrireResultatFonctionObjectiveAuFormatTXT(void* Prob,
CoutOptimalDeLaSolution = Probleme->coutOptimalSolution2[NumeroDeLIntervalle];

auto& study = *Data::Study::Current::Get();
Flot = study.createCriterionFileIntoOutput(numSpace);
Flot = study.createFileIntoOutputWithExtension("criterion", "txt", numSpace);
if (!Flot)
exit(2);

Expand All @@ -363,6 +363,236 @@ void OPT_EcrireResultatFonctionObjectiveAuFormatTXT(void* Prob,
return;
}

void OPT_dump_spx_fixed_part(PROBLEME_SIMPLEXE* Pb, uint numSpace)
{
FILE* Flot;
int Cnt;
int Var;
int il;
int ilk;
int ilMax;
char* Nombre;
int* Cder;
int* Cdeb;
int* NumeroDeContrainte;
int* Csui;

for (ilMax = -1, Cnt = 0; Cnt < Pb->NombreDeContraintes; Cnt++)
{
if ((Pb->IndicesDebutDeLigne[Cnt] + Pb->NombreDeTermesDesLignes[Cnt] - 1) > ilMax)
{
ilMax = Pb->IndicesDebutDeLigne[Cnt] + Pb->NombreDeTermesDesLignes[Cnt] - 1;
}
}

ilMax += Pb->NombreDeContraintes;

Cder = (int*)malloc(Pb->NombreDeVariables * sizeof(int));
Cdeb = (int*)malloc(Pb->NombreDeVariables * sizeof(int));
NumeroDeContrainte = (int*)malloc(ilMax * sizeof(int));
Csui = (int*)malloc(ilMax * sizeof(int));
Nombre = (char*)malloc(1024);

if (Cder == NULL || Cdeb == NULL || NumeroDeContrainte == NULL || Csui == NULL
|| Nombre == NULL)
{
logs.fatal() << "Not enough memory";
AntaresSolverEmergencyShutdown();
}

for (Var = 0; Var < Pb->NombreDeVariables; Var++)
Cdeb[Var] = -1;

for (Cnt = 0; Cnt < Pb->NombreDeContraintes; Cnt++)
{
il = Pb->IndicesDebutDeLigne[Cnt];
ilMax = il + Pb->NombreDeTermesDesLignes[Cnt];
while (il < ilMax)
{
Var = Pb->IndicesColonnes[il];
if (Cdeb[Var] < 0)
{
Cdeb[Var] = il;
NumeroDeContrainte[il] = Cnt;
Csui[il] = -1;
Cder[Var] = il;
}
else
{
ilk = Cder[Var];
Csui[ilk] = il;
NumeroDeContrainte[il] = Cnt;
Csui[il] = -1;
Cder[Var] = il;
}

il++;
}
}

free(Cder);

auto& study = *Data::Study::Current::Get();
Flot = study.createFileIntoOutputWithExtension("fixed-part", "mps", numSpace);

if (!Flot)
exit(2);

fprintf(Flot, "* Number of variables: %d\n", Pb->NombreDeVariables);
fprintf(Flot, "* Number of constraints: %d\n", Pb->NombreDeContraintes);
fprintf(Flot, "NAME Pb Solve\n");
fprintf(Flot, "ROWS\n");
fprintf(Flot, " N OBJECTIF\n");

for (Cnt = 0; Cnt < Pb->NombreDeContraintes; Cnt++)
{
if (Pb->Sens[Cnt] == '=')
{
fprintf(Flot, " E R%07d\n", Cnt);
}
else if (Pb->Sens[Cnt] == '<')
{
fprintf(Flot, " L R%07d\n", Cnt);
}
else if (Pb->Sens[Cnt] == '>')
{
fprintf(Flot, " G R%07d\n", Cnt);
}
else
{
fprintf(Flot,
"Writing fixed part of MPS data : le sens de la contrainte %c ne fait pas "
"partie des sens reconnus\n",
Pb->Sens[Cnt]);
AntaresSolverEmergencyShutdown();
exit(0);
}
}

fprintf(Flot, "COLUMNS\n");
for (Var = 0; Var < Pb->NombreDeVariables; Var++)
{
il = Cdeb[Var];
while (il >= 0)
{
SNPRINTF(Nombre, 1024, "%-.10lf", Pb->CoefficientsDeLaMatriceDesContraintes[il]);
fprintf(Flot, " C%07d R%07d %s\n", Var, NumeroDeContrainte[il], Nombre);
il = Csui[il];
}
}

fprintf(Flot, "ENDATA\n");

free(Cdeb);
free(NumeroDeContrainte);
free(Csui);
free(Nombre);

fclose(Flot);
}

void OPT_dump_spx_variable_part(PROBLEME_SIMPLEXE* Pb, uint numSpace)
{
FILE* Flot;
int Cnt;
int Var;
char* Nombre;

Nombre = (char*)malloc(1024);

if (Nombre == NULL)
{
logs.fatal() << "Not enough memory";
AntaresSolverEmergencyShutdown();
}

// gp : to be changed
auto& study = *Data::Study::Current::Get();
Flot = study.createFileIntoOutputWithExtension("variable-part", "mps", numSpace);

if (!Flot)
exit(2);

fprintf(Flot, "* Number of variables: %d\n", Pb->NombreDeVariables);
fprintf(Flot, "* Number of constraints: %d\n", Pb->NombreDeContraintes);
fprintf(Flot, "NAME Pb Solve\n");

fprintf(Flot, "COLUMNS\n");
for (Var = 0; Var < Pb->NombreDeVariables; Var++)
{
if (Pb->CoutLineaire[Var] != 0.0)
{
SNPRINTF(Nombre, 1024, "%-.10lf", Pb->CoutLineaire[Var]);
fprintf(Flot, " C%07d OBJECTIF %s\n", Var, Nombre);
}
}

fprintf(Flot, "RHS\n");
for (Cnt = 0; Cnt < Pb->NombreDeContraintes; Cnt++)
{
if (Pb->SecondMembre[Cnt] != 0.0)
{
SNPRINTF(Nombre, 1024, "%-.9lf", Pb->SecondMembre[Cnt]);
fprintf(Flot, " RHSVAL R%07d %s\n", Cnt, Nombre);
}
}

fprintf(Flot, "BOUNDS\n");

for (Var = 0; Var < Pb->NombreDeVariables; Var++)
{
if (Pb->TypeDeVariable[Var] == VARIABLE_FIXE)
{
SNPRINTF(Nombre, 1024, "%-.9lf", Pb->Xmin[Var]);

fprintf(Flot, " FX BNDVALUE C%07d %s\n", Var, Nombre);
continue;
}

if (Pb->TypeDeVariable[Var] == VARIABLE_BORNEE_DES_DEUX_COTES)
{
if (Pb->Xmin[Var] != 0.0)
{
SNPRINTF(Nombre, 1024, "%-.9lf", Pb->Xmin[Var]);
fprintf(Flot, " LO BNDVALUE C%07d %s\n", Var, Nombre);
}

SNPRINTF(Nombre, 1024, "%-.9lf", Pb->Xmax[Var]);
fprintf(Flot, " UP BNDVALUE C%07d %s\n", Var, Nombre);
}

if (Pb->TypeDeVariable[Var] == VARIABLE_BORNEE_INFERIEUREMENT)
{
if (Pb->Xmin[Var] != 0.0)
{
SNPRINTF(Nombre, 1024, "%-.9lf", Pb->Xmin[Var]);
fprintf(Flot, " LO BNDVALUE C%07d %s\n", Var, Nombre);
}
}

if (Pb->TypeDeVariable[Var] == VARIABLE_BORNEE_SUPERIEUREMENT)
{
fprintf(Flot, " MI BNDVALUE C%07d\n", Var);
if (Pb->Xmax[Var] != 0.0)
{
SNPRINTF(Nombre, 1024, "%-.9lf", Pb->Xmax[Var]);
fprintf(Flot, " UP BNDVALUE C%07d %s\n", Var, Nombre);
}
}

if (Pb->TypeDeVariable[Var] == VARIABLE_NON_BORNEE)
{
fprintf(Flot, " FR BNDVALUE C%07d\n", Var);
}
}

fprintf(Flot, "ENDATA\n");

free(Nombre);

fclose(Flot);
}

void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(void* Prob, uint numSpace, char Type)
{
FILE* Flot;
Expand All @@ -378,6 +608,7 @@ void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(void* Prob, uint numSpace, char T
int* Csui;
double CoutOpt;
PROBLEME_SIMPLEXE* Probleme;
PROBLEME_A_RESOUDRE* ProblemePourPne;

int NombreDeVariables;
int* TypeDeBorneDeLaVariable;
Expand All @@ -394,25 +625,51 @@ void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(void* Prob, uint numSpace, char T
int ExistenceDUneSolution;
double* X;

Probleme = (PROBLEME_SIMPLEXE*)Prob;

ExistenceDUneSolution = Probleme->ExistenceDUneSolution;
if (ExistenceDUneSolution == OUI_SPX)
ExistenceDUneSolution = OUI_ANTARES;

NombreDeVariables = Probleme->NombreDeVariables;
TypeDeBorneDeLaVariable = Probleme->TypeDeVariable;
Xmax = Probleme->Xmax;
Xmin = Probleme->Xmin;
X = Probleme->X;
CoutLineaire = Probleme->CoutLineaire;
NombreDeContraintes = Probleme->NombreDeContraintes;
SecondMembre = Probleme->SecondMembre;
Sens = Probleme->Sens;
IndicesDebutDeLigne = Probleme->IndicesDebutDeLigne;
NombreDeTermesDesLignes = Probleme->NombreDeTermesDesLignes;
CoefficientsDeLaMatriceDesContraintes = Probleme->CoefficientsDeLaMatriceDesContraintes;
IndicesColonnes = Probleme->IndicesColonnes;
if (Type == ANTARES_SIMPLEXE)
{
Probleme = (PROBLEME_SIMPLEXE*)Prob;

ExistenceDUneSolution = Probleme->ExistenceDUneSolution;
if (ExistenceDUneSolution == OUI_SPX)
ExistenceDUneSolution = OUI_ANTARES;

NombreDeVariables = Probleme->NombreDeVariables;
TypeDeBorneDeLaVariable = Probleme->TypeDeVariable;
Xmax = Probleme->Xmax;
Xmin = Probleme->Xmin;
X = Probleme->X;
CoutLineaire = Probleme->CoutLineaire;
NombreDeContraintes = Probleme->NombreDeContraintes;
SecondMembre = Probleme->SecondMembre;
Sens = Probleme->Sens;
IndicesDebutDeLigne = Probleme->IndicesDebutDeLigne;
NombreDeTermesDesLignes = Probleme->NombreDeTermesDesLignes;
CoefficientsDeLaMatriceDesContraintes = Probleme->CoefficientsDeLaMatriceDesContraintes;
IndicesColonnes = Probleme->IndicesColonnes;
}
else
{
ProblemePourPne = (PROBLEME_A_RESOUDRE*)Prob;

ExistenceDUneSolution = ProblemePourPne->ExistenceDUneSolution;
if (ExistenceDUneSolution == SOLUTION_OPTIMALE_TROUVEE)
ExistenceDUneSolution = OUI_ANTARES;

NombreDeVariables = ProblemePourPne->NombreDeVariables;
TypeDeBorneDeLaVariable = ProblemePourPne->TypeDeBorneDeLaVariable;
Xmax = ProblemePourPne->Xmax;
Xmin = ProblemePourPne->Xmin;
X = ProblemePourPne->X;
CoutLineaire = ProblemePourPne->CoutLineaire;
NombreDeContraintes = ProblemePourPne->NombreDeContraintes;
SecondMembre = ProblemePourPne->SecondMembre;
Sens = ProblemePourPne->Sens;
IndicesDebutDeLigne = ProblemePourPne->IndicesDebutDeLigne;
NombreDeTermesDesLignes = ProblemePourPne->NombreDeTermesDesLignes;
CoefficientsDeLaMatriceDesContraintes
= ProblemePourPne->CoefficientsDeLaMatriceDesContraintes;
IndicesColonnes = ProblemePourPne->IndicesColonnes;
}

if (ExistenceDUneSolution == OUI_ANTARES)
{
Expand Down Expand Up @@ -477,7 +734,7 @@ void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(void* Prob, uint numSpace, char T
free(Cder);

auto& study = *Data::Study::Current::Get();
Flot = study.createMPSFileIntoOutput(numSpace);
Flot = study.createFileIntoOutputWithExtension("problem", "mps", numSpace);

if (!Flot)
exit(2);
Expand Down Expand Up @@ -505,9 +762,8 @@ void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(void* Prob, uint numSpace, char T
else
{
fprintf(Flot,
"%s : le sens de la contrainte %c ne fait pas partie "
"PNE_EcrireJeuDeDonneesMPS : le sens de la contrainte %c ne fait pas partie "
"des sens reconnus\n",
__FUNCTION__,
Sens[Cnt]);
AntaresSolverEmergencyShutdown();
exit(0);
Expand Down