Skip to content

Commit

Permalink
Version with files (empty implementation tmp)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonMarechal25 committed Nov 9, 2022
1 parent 73f57a2 commit 5ba53ee
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 23 deletions.
71 changes: 55 additions & 16 deletions src/cpp/exe/lpnamer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,71 @@ int main(int argc, char **argv) {
std::vector<ActiveLink> links = linkBuilder.getLinks();

auto const mps_file_name = root / MPS_TXT;
auto lpDir_ = root / "lp";
auto reader = std::make_shared<ArchiveReader>(archivePath);
reader->Open();
const auto tmpArchiveName = MPS_ZIP_FILE + "-tmp" + ZIP_EXT;
const auto tmpArchivePath = lpDir_ / tmpArchiveName;
auto writer = std::make_shared<ArchiveWriter>(tmpArchivePath);
writer->Open();

LinkProblemsGenerator linkProblemsGenerator(links, solver_name, logger);
auto mpsList = linkProblemsGenerator.readMPSList(mps_file_name);

auto problem_writer = std::make_shared<ArchiveProblemWriter>(root, writer);
bool use_zip_implementation = true;
bool use_zip_implementation = false;
if (use_zip_implementation) {
/* 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;
auto writer = std::make_shared<ArchiveWriter>(tmpArchivePath);
writer->Open();

/* Zip problem writer */
auto problem_writer =
std::make_shared<ArchiveProblemWriter>(root, writer);

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

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

std::filesystem::remove(archivePath);
std::filesystem::rename(tmpArchivePath,
lpDir_ / (MPS_ZIP_FILE + ZIP_EXT));
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;
auto writer = std::make_shared<ArchiveWriter>(tmpArchivePath);
writer->Open();

/* Zip problem writer */
auto problem_writer =
std::make_shared<ArchiveProblemWriter>(root, writer);

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

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

std::filesystem::remove(archivePath);
std::filesystem::rename(tmpArchivePath,
lpDir_ / (MPS_ZIP_FILE + ZIP_EXT));
writer->Close();
writer->Delete();
}

std::filesystem::remove(archivePath);
std::filesystem::rename(tmpArchivePath, lpDir_ / (MPS_ZIP_FILE + ZIP_EXT));
reader->Close();
reader->Delete();
writer->Close();
writer->Delete();

MasterGeneration master_generation(root, links, additionalConstraints,
couplings, master_formulation,
solver_name, logger);
Expand Down
2 changes: 1 addition & 1 deletion src/cpp/lpnamer/problem_modifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ add_library(lp_namer_problem_modifier STATIC
${CMAKE_CURRENT_SOURCE_DIR}/ZipProblemProviderAdapter.h
${CMAKE_CURRENT_SOURCE_DIR}/MyAdapter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/IProblemProviderPort.h
IProblemWriter.h ArchiveProblemWriter.cpp ArchiveProblemWriter.h ProblemVariablesZipAdapter.cpp ProblemVariablesZipAdapter.h IProblemVariablesProviderPort.h)
IProblemWriter.h ArchiveProblemWriter.cpp ArchiveProblemWriter.h ProblemVariablesZipAdapter.cpp ProblemVariablesZipAdapter.h IProblemVariablesProviderPort.h ProblemVariablesFileAdapter.cpp ProblemVariablesFileAdapter.h MPSFileProblemProviderAdapter.cpp MPSFileProblemProviderAdapter.h)

target_include_directories (lp_namer_problem_modifier
PUBLIC
Expand Down
19 changes: 19 additions & 0 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "IProblemProviderPort.h"
#include "IProblemVariablesProviderPort.h"
#include "IProblemWriter.h"
#include "MPSFileProblemProviderAdapter.h"
#include "ProblemVariablesFileAdapter.h"
#include "ProblemVariablesZipAdapter.h"
#include "ZipProblemProviderAdapter.h"
#include "helpers/StringUtils.h"
Expand Down Expand Up @@ -110,3 +112,20 @@ void LinkProblemsGenerator::treatloop(const std::filesystem::path &root,
problem_variables_from_zip_adapter);
});
}

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);
});
}
8 changes: 6 additions & 2 deletions src/cpp/lpnamer/problem_modifier/LinkProblemsGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class LinkProblemsGenerator {
std::filesystem::path lpDir_ = "";
ProblemGenerationLog::ProblemGenerationLoggerSharedPointer logger_;
mutable std::mutex coupling_mutex_;
void Write_problem(const ProblemData& problemData, ArchiveWriter& writer,
std::shared_ptr<Problem>& in_prblm);

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);
};
12 changes: 12 additions & 0 deletions src/cpp/lpnamer/problem_modifier/MPSFileProblemProviderAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by marechaljas on 09/11/22.
//

#include "MPSFileProblemProviderAdapter.h"
std::shared_ptr<Problem> MPSFileProblemProviderAdapter::provide_problem(
const std::string& solver_name) const {
return std::shared_ptr<Problem>();
}
MPSFileProblemProviderAdapter::MPSFileProblemProviderAdapter(
const std::filesystem::path root, const std::string& problem_name)
: lp_dir_(root), problem_name_(problem_name) {}
16 changes: 16 additions & 0 deletions src/cpp/lpnamer/problem_modifier/MPSFileProblemProviderAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Created by marechaljas on 09/11/22.
//

#pragma once

#include "IProblemProviderPort.h"
class MPSFileProblemProviderAdapter : public IProblemProviderPort {
public:
MPSFileProblemProviderAdapter(const std::filesystem::path root,
const std::string& problem_name);
[[nodiscard]] std::shared_ptr<Problem> provide_problem(
const std::string& solver_name) const override;
const std::filesystem::path lp_dir_;
const std::string& problem_name_;
};
12 changes: 12 additions & 0 deletions src/cpp/lpnamer/problem_modifier/ProblemVariablesFileAdapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by marechaljas on 09/11/22.
//

#include "ProblemVariablesFileAdapter.h"
ProblemVariables ProblemVariablesFileAdapter::Provide() {
return ProblemVariables();
}
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) {}
19 changes: 19 additions & 0 deletions src/cpp/lpnamer/problem_modifier/ProblemVariablesFileAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by marechaljas on 09/11/22.
//

#pragma once

#include "IProblemVariablesProviderPort.h"
#include "LinkProblemsGenerator.h"
class ProblemVariablesFileAdapter : public IProblemVariablesProviderPort {
public:
ProblemVariablesFileAdapter(
const ProblemData data, const std::vector<struct ActiveLink> vector_1,
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger>
shared_ptr_1);
ProblemVariables Provide() override;
const ProblemData problem_data_;
const std::vector<struct ActiveLink> active_links_;
std::shared_ptr<ProblemGenerationLog::ProblemGenerationLogger> logger_;
};
10 changes: 6 additions & 4 deletions src/python/antares_xpansion/problem_generator_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ def _lp_step(self):
produces a file named with xpansionConfig.MPS_TXT
"""



start_time = datetime.now()
use_zip = False
if not use_zip:
flushed_print(f"Output pat {self.output_path}")
with zipfile.ZipFile(self.output_path, 'r') as study_archive:
study_archive.extractall(self.xpansion_output_dir)
os.remove(self.output_path)
flushed_print(f"LPNamer command {self._get_lp_namer_command()}")
returned_l = subprocess.run(self._get_lp_namer_command(), shell=False,
stdout=sys.stdout, stderr=sys.stderr)



end_time = datetime.now()
flushed_print('Post antares step duration: {}'.format(
end_time - start_time))
Expand Down

0 comments on commit 5ba53ee

Please sign in to comment.