Skip to content

Commit

Permalink
Implementation to read file
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Nov 10, 2022
1 parent 5ba53ee commit f9f8c2c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 37 deletions.
11 changes: 2 additions & 9 deletions src/cpp/exe/lpnamer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,11 @@ int main(int argc, char **argv) {
writer->Close();
writer->Delete();
} else {
/* Instantiate Zip reader */
auto reader = std::make_shared<ArchiveReader>(archivePath);
reader->Open();

/*Instantiate zip writer */
auto lpDir_ = root / "lp";
const auto tmpArchiveName = MPS_ZIP_FILE + "-tmp" + ZIP_EXT;
const auto tmpArchivePath = lpDir_ / tmpArchiveName;
std::filesystem::remove(tmpArchivePath);
auto writer = std::make_shared<ArchiveWriter>(tmpArchivePath);
writer->Open();

Expand All @@ -155,11 +152,7 @@ int main(int argc, char **argv) {

/* Main stuff */
linkProblemsGenerator.treatloop_files(root, couplings, mpsList,
problem_writer, reader);

/* Clean up */
reader->Close();
reader->Delete();
problem_writer);

std::filesystem::remove(archivePath);
std::filesystem::rename(tmpArchivePath,
Expand Down
18 changes: 10 additions & 8 deletions src/cpp/lpnamer/input_reader/VariableFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ void updateMapColumn(const std::vector<ActiveLink>& links, int link_id,
}

VariableFileReader::VariableFileReader(
const std::string& fileName, const std::vector<ActiveLink>& links,
const std::filesystem::path& path, const std::vector<ActiveLink>& links,
const VariableFileReadNameConfiguration& variable_name_config,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger)
: logger_(logger) {
std::string line;
std::ifstream file(fileName.c_str());
std::ifstream file(path);
if (!file.good()) {
auto errMsg = std::string("Unable to open '") + fileName + "'";
auto errMsg = std::string("Unable to open '") + path.string() + "'";
(*logger_)(ProblemGenerationLog::LOGLEVEL::FATAL) << errMsg;
throw std::runtime_error(errMsg);
}
Expand All @@ -51,26 +51,28 @@ void VariableFileReader::ReadVarsFromStream(

std::istringstream buffer(line);
colId id;
std::string variable;
std::string type_de_variable;
buffer >> id;
buffer >> variable;
buffer >> type_de_variable;

if (variable == variable_name_config.ntc_variable_name) {
if (type_de_variable == variable_name_config.ntc_variable_name) {
int pays;
int link_id;
int time_step;
buffer >> pays;
buffer >> link_id;
buffer >> time_step;
updateMapColumn(links, link_id, id, time_step, _ntc_p_var_columns);
} else if (variable == variable_name_config.cost_extremite_variable_name) {
} else if (type_de_variable ==
variable_name_config.cost_extremite_variable_name) {
int link_id;
int time_step;
buffer >> link_id;
buffer >> time_step;
updateMapColumn(links, link_id, id, time_step,
_indirect_cost_p_var_columns);
} else if (variable == variable_name_config.cost_origin_variable_name) {
} else if (type_de_variable ==
variable_name_config.cost_origin_variable_name) {
int link_id;
int time_step;
buffer >> link_id;
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lpnamer/input_reader/VariableFileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct VariableFileReadNameConfiguration {
class VariableFileReader {
public:
VariableFileReader(
const std::string& fileName, const std::vector<ActiveLink>& links,
const std::filesystem::path& path, const std::vector<ActiveLink>& links,
const VariableFileReadNameConfiguration& variable_name_config,
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger);
VariableFileReader(
Expand Down
24 changes: 12 additions & 12 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ void LinkProblemsGenerator::treatloop(const std::filesystem::path &root,
void LinkProblemsGenerator::treatloop_files(
const std::filesystem::path &root, Couplings &couplings,
const std::vector<ProblemData> &mps_list,
std::shared_ptr<IProblemWriter> writer,
std::shared_ptr<ArchiveReader> reader) {
std::for_each(
std::execution::par, mps_list.begin(), mps_list.end(),
[&](const auto &mps) {
auto adapter = std::make_shared<MPSFileProblemProviderAdapter>(
root, mps._problem_mps);
auto problem_variables_from_zip_adapter =
std::make_shared<ProblemVariablesFileAdapter>(mps, _links, logger_);
treat(mps._problem_mps, couplings, writer, adapter,
problem_variables_from_zip_adapter);
});
std::shared_ptr<IProblemWriter> writer) {
std::for_each(std::execution::par, mps_list.begin(), mps_list.end(),
[&](const auto &mps) {
auto adapter =
std::make_shared<MPSFileProblemProviderAdapter>(
root, mps._problem_mps);
auto variables_file_adapter =
std::make_shared<ProblemVariablesFileAdapter>(
mps, _links, logger_, root);
treat(mps._problem_mps, couplings, writer, adapter,
variables_file_adapter);
});
}
3 changes: 1 addition & 2 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,5 @@ class LinkProblemsGenerator {
public:
void treatloop_files(const std::filesystem::path& root, Couplings& couplings,
const std::vector<ProblemData>& mps_list,
std::shared_ptr<IProblemWriter> writer,
std::shared_ptr<ArchiveReader> reader);
std::shared_ptr<IProblemWriter> writer);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
//

#include "MPSFileProblemProviderAdapter.h"

#include "multisolver_interface/SolverFactory.h"
std::shared_ptr<Problem> MPSFileProblemProviderAdapter::provide_problem(
const std::string& solver_name) const {
return std::shared_ptr<Problem>();
SolverFactory factory;
auto const lp_mps_name = lp_dir_ / problem_name_;
auto in_prblm = std::make_shared<Problem>(factory.create_solver(solver_name));

in_prblm->read_prob_mps(lp_mps_name);
return in_prblm;
}
MPSFileProblemProviderAdapter::MPSFileProblemProviderAdapter(
const std::filesystem::path root, const std::string& problem_name)
Expand Down
37 changes: 34 additions & 3 deletions src/cpp/lpnamer/problem_modifier/ProblemVariablesFileAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,41 @@
//

#include "ProblemVariablesFileAdapter.h"

void ProblemVariablesFileAdapter::extract_variables(
const std::filesystem::path& file, std::vector<std::string>& var_names,
std::map<colId, ColumnsToChange>& p_ntc_columns,
std::map<colId, ColumnsToChange>& p_direct_cost_columns,
std::map<colId, ColumnsToChange>& p_indirect_cost_columns) const {
// List of variables
VariableFileReadNameConfiguration variable_name_config;
variable_name_config.ntc_variable_name = "ValeurDeNTCOrigineVersExtremite";
variable_name_config.cost_origin_variable_name =
"CoutOrigineVersExtremiteDeLInterconnexion";
variable_name_config.cost_extremite_variable_name =
"CoutExtremiteVersOrigineDeLInterconnexion";

auto variableReader =
VariableFileReader(file, active_links_, variable_name_config, logger_);
var_names = variableReader.getVariables();
p_ntc_columns = variableReader.getNtcVarColumns();
p_direct_cost_columns = variableReader.getDirectCostVarColumns();
p_indirect_cost_columns = variableReader.getIndirectCostVarColumns();
}

ProblemVariables ProblemVariablesFileAdapter::Provide() {
return ProblemVariables();
ProblemVariables result;
extract_variables(lpdir_ / problem_data_._variables_txt,
result.variable_names, result.ntc_columns,
result.direct_cost_columns, result.indirect_cost_columns);
return result;
}

ProblemVariablesFileAdapter::ProblemVariablesFileAdapter(
const ProblemData data, const std::vector<struct ActiveLink> vector_1,
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger> shared_ptr_1)
: problem_data_(data), active_links_(vector_1), logger_(shared_ptr_1) {}
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger> shared_ptr_1,
const std::filesystem::path path)
: problem_data_(data),
active_links_(vector_1),
logger_(shared_ptr_1),
lpdir_(path) {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ class ProblemVariablesFileAdapter : public IProblemVariablesProviderPort {
ProblemVariablesFileAdapter(
const ProblemData data, const std::vector<struct ActiveLink> vector_1,
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger>
shared_ptr_1);
shared_ptr_1,
const std::filesystem::path path);
ProblemVariables Provide() override;
const ProblemData problem_data_;
const std::vector<struct ActiveLink> active_links_;
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger> logger_;
void extract_variables(
const std::filesystem::path& file, std::vector<std::string>& var_names,
std::map<colId, ColumnsToChange>& p_ntc_columns,
std::map<colId, ColumnsToChange>& p_direct_cost_columns,
std::map<colId, ColumnsToChange>& p_indirect_cost_columns) const;
const std::filesystem::path lpdir_;
};

0 comments on commit f9f8c2c

Please sign in to comment.