From 05fc020753d46ef1ba774830fe2686cc753a2de7 Mon Sep 17 00:00:00 2001 From: Gabriele Dragotto Date: Tue, 1 Oct 2019 10:32:18 -0400 Subject: [PATCH] Game::EPEC::warmstart and Models::EPEC::readSolutionJSON Warmstarting from existing solution --- src/EPEC.cpp | 2 +- src/Games.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/Models.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/games.h | 3 +++ src/models.h | 2 ++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/EPEC.cpp b/src/EPEC.cpp index a317589..1747b6b 100644 --- a/src/EPEC.cpp +++ b/src/EPEC.cpp @@ -197,7 +197,7 @@ int main(int argc, char **argv) { results << " ];" << to_string(stat.status) << ";[ " << PolyT.str() << "];" << stat.numVar << ";" << stat.numConstraints << ";" << stat.numNonZero << ";" << WallClockTime << ";" << realThreads << ";" - << to_string(epec.indicators) << "\n"; + << to_string(epec.getIndicators()) << "\n"; results.close(); return EXIT_SUCCESS; diff --git a/src/Games.cpp b/src/Games.cpp index 091259d..bc5a4ac 100644 --- a/src/Games.cpp +++ b/src/Games.cpp @@ -1732,6 +1732,43 @@ bool Game::EPEC::computeNashEq( return foundNash; } +bool Game::EPEC::warmstart(const arma::vec x, const arma::vec z) { + + if (x.size() < this->getnVarinEPEC()) { + BOOST_LOG_TRIVIAL(error) + << "Exception in Game::EPEC::warmstart: number of variables " + "does not fit this instance."; + throw; + } + if (!this->finalized) { + BOOST_LOG_TRIVIAL(error) + << "Exception in Game::EPEC::warmstart: EPEC is not finalized."; + throw; + } + if (this->country_QP.front() == nullptr) { + BOOST_LOG_TRIVIAL(warning) + << "Game::EPEC::warmstart: Generating QP as of warmstart."; + } + + this->sol_x = x; + std::vector devns = std::vector(this->nCountr); + std::vector prevDevns = std::vector(this->nCountr); + this->getAllDevns(devns, this->sol_x, prevDevns); + unsigned int addedPoly = this->addDeviatedPolyhedron(devns); + this->make_country_QP(); + + unsigned int c; + arma::vec devn; + + if (this->isSolved(&c, &devn)) + BOOST_LOG_TRIVIAL(warning) << "Exception in Game::EPEC::warmstart: " + "The loaded solution is optimal."; + else + BOOST_LOG_TRIVIAL(warning) + << "Exception in Game::EPEC::warmstart: " + "The loaded solution is NOT optimal. Trying to repair."; +} + void Game::EPEC::findNashEq() { /** * @brief Computes Nash equilibrium using the algorithm set in diff --git a/src/Models.cpp b/src/Models.cpp index aac1394..22c2fb3 100644 --- a/src/Models.cpp +++ b/src/Models.cpp @@ -1204,6 +1204,47 @@ void Models::EPEC::writeSolutionJSON(string filename, const arma::vec x, file << s.GetString(); } +void Models::EPEC::readSolutionJSON(string filename) { + /** + * @brief Reads the solution file and load it in the current EPEC instance + * **/ + ifstream ifs(filename + ".json"); + if (ifs.good()) { + IStreamWrapper isw(ifs); + Document d; + try { + d.ParseStream(isw); + const Value &x = d["Solution"].GetObject()["x"]; + const Value &z = d["Solution"].GetObject()["z"]; + arma::vec new_x, new_z; + new_x.zeros(x.GetArray().Size()); + new_z.zeros(z.GetArray().Size()); + + for (SizeType i = 0; i < this->getnVarinEPEC(); i++) + new_x.at(i) = x[i].GetDouble(); + + for (SizeType i = 0; i < this->getnVarinEPEC(); i++) + new_z.at(i) = z[i].GetDouble(); + ifs.close(); + this->warmstart(new_x, new_z); + } catch (exception &e) { + cerr << "Exception in Models::readSolutionJSON : cannot read instance " + "file." + << e.what() << '\n'; + throw; + } catch (...) { + cerr + << "Exception in Models::readSolutionJSON: cannot read instance file." + << '\n'; + throw; + } + } else { + cerr << "Exception in Models::readSolutionJSON : file instance not found." + << '\n'; + throw; + } +} + void Models::EPEC::writeSolution(const int writeLevel, string filename) const { /** * @brief Writes the computed Nash Equilibrium in the EPEC instance diff --git a/src/games.h b/src/games.h index 954fed1..6cc6fc2 100644 --- a/src/games.h +++ b/src/games.h @@ -492,6 +492,9 @@ class EPEC { arma::vec sol_z, ///< Solution equation values sol_x; ///< Solution variable values + bool warmstart(const arma::vec x, + const arma::vec z); ///< Warmstarts EPEC with a solution + private: virtual void add_Dummy_Lead( const unsigned int i) final; ///< Add Dummy variables for the leaders diff --git a/src/models.h b/src/models.h index 96e6b5c..838a8ac 100644 --- a/src/models.h +++ b/src/models.h @@ -276,6 +276,8 @@ class EPEC : public Game::EPEC { void write(const std::string filename, bool append = true) const; + void readSolutionJSON(const std::string filename); + void gur_WriteCountry_conv(const unsigned int i, std::string filename) const; void gur_WriteEpecMip(const unsigned int i, std::string filename) const;