Skip to content

Commit

Permalink
Add interface for problem naming
Browse files Browse the repository at this point in the history
  • Loading branch information
flomnes committed Sep 5, 2023
1 parent 6104052 commit c2ecafe
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/solver/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ set(SRC
named_problem.cpp
mps_utils.h
mps_utils.cpp
name_translator.h
name_translator.cpp
opt_period_string_generator.h
opt_period_string_generator.cpp
)
Expand Down
10 changes: 6 additions & 4 deletions src/solver/utils/mps_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ constexpr size_t OPT_APPEL_SOLVEUR_BUFFER_SIZE = 256;
#include <algorithm>
#include "filename.h"
#include "../optimisation/opt_constants.h"
#include "name_translator.h"

using namespace Yuni;

Expand All @@ -57,7 +58,7 @@ using namespace Yuni;
class ProblemConverter
{
public:
void copyProbSimplexeToProbMps(PROBLEME_MPS* dest, PROBLEME_SIMPLEXE_NOMME* src)
void copyProbSimplexeToProbMps(PROBLEME_MPS* dest, PROBLEME_SIMPLEXE_NOMME* src, NameTranslator& nameTranslator)
{
// Variables
dest->NbVar = src->NombreDeVariables;
Expand Down Expand Up @@ -85,8 +86,8 @@ class ProblemConverter
dest->SensDeLaContrainte = src->Sens;

// Names
dest->LabelDeLaVariable = src->VariableNamesAsCharPP(mVariableNames);
dest->LabelDeLaContrainte = src->ConstraintNamesAsCharPP(mConstraintNames);
dest->LabelDeLaVariable = nameTranslator.translate(src->VariableNames(), mVariableNames);
dest->LabelDeLaContrainte = nameTranslator.translate(src->ConstraintNames(), mConstraintNames);
}

private:
Expand All @@ -105,9 +106,10 @@ void OPT_EcrireJeuDeDonneesLineaireAuFormatMPS(PROBLEME_SIMPLEXE_NOMME* Prob,

auto mps = std::make_shared<PROBLEME_MPS>();
{
auto translator = NameTranslator::create(Prob->UseNamedProblems());
ProblemConverter
converter; // This object must not be destroyed until SRSwritempsprob has been run
converter.copyProbSimplexeToProbMps(mps.get(), Prob);
converter.copyProbSimplexeToProbMps(mps.get(), Prob, *translator);
SRSwritempsprob(mps.get(), tmpPath.c_str());
}

Expand Down
27 changes: 27 additions & 0 deletions src/solver/utils/name_translator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "name_translator.h"
#include <algorithm>

char** RealName::translate(const std::vector<std::string>& src,
std::vector<char*>& pointerVec)
{
std::transform(src.begin(),
src.end(),
std::back_inserter(pointerVec),
[](const std::string& str) { return str.empty() ? nullptr : const_cast<char*>(str.data()); });
return pointerVec.data();
}

char** NullName::translate(const std::vector<std::string>& src,
std::vector<char*>& pointerVec)
{
pointerVec.assign(src.size(), nullptr);
return pointerVec.data();
}

std::unique_ptr<NameTranslator> NameTranslator::create(bool useRealNames)
{
if (useRealNames)
return std::make_unique<RealName>();
else
return std::make_unique<NullName>();
}
25 changes: 25 additions & 0 deletions src/solver/utils/name_translator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <vector>
#include <string>
#include <memory>

class NameTranslator
{
public:
virtual char** translate(const std::vector<std::string>& src,
std::vector<char*>& pointerVec) = 0;
static std::unique_ptr<NameTranslator> create(bool useRealNames);
};

class RealName : public NameTranslator
{
char** translate(const std::vector<std::string>& src,
std::vector<char*>& pointerVec) override;
};

class NullName : public NameTranslator
{
char** translate(const std::vector<std::string>& src,
std::vector<char*>& pointerVec) override;
};
37 changes: 0 additions & 37 deletions src/solver/utils/named_problem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@ namespace Antares
{
namespace Optimization
{
static inline char** VectorOfStringToCharPP(std::vector<std::string>& in,
std::vector<char*>& pointerVec)
{
std::transform(in.begin(),
in.end(),
std::back_inserter(pointerVec),
[](std::string& str) { return str.empty() ? nullptr : str.data(); });
return pointerVec.data();
}

static inline char** CharPP(size_t Size, std::vector<char*>& pointerVec)
{
pointerVec = std::vector<char*>(Size, nullptr);
return pointerVec.data();
}

struct PROBLEME_SIMPLEXE_NOMME : public PROBLEME_SIMPLEXE
{
public:
Expand Down Expand Up @@ -59,27 +43,6 @@ struct PROBLEME_SIMPLEXE_NOMME : public PROBLEME_SIMPLEXE
useNamedProblems_ = useNamedProblems;
}

char** VariableNamesAsCharPP(std::vector<char*>& pointerVec)
{
if (useNamedProblems_)
{
return VectorOfStringToCharPP(const_cast<std::vector<std::string>&>(NomDesVariables),
pointerVec);
}

return CharPP(NomDesVariables.size(), pointerVec);
}

char** ConstraintNamesAsCharPP(std::vector<char*>& pointerVec)
{
if (useNamedProblems_)
{
return VectorOfStringToCharPP(const_cast<std::vector<std::string>&>(NomDesContraintes),
pointerVec);
}
return CharPP(NomDesContraintes.size(), pointerVec);
}

const std::vector<std::string>& VariableNames() const
{
return NomDesVariables;
Expand Down

0 comments on commit c2ecafe

Please sign in to comment.