Skip to content

Commit

Permalink
Game::EPEC::warmstart and Models::EPEC::readSolutionJSON
Browse files Browse the repository at this point in the history
Warmstarting from existing solution
  • Loading branch information
gdragotto committed Oct 1, 2019
1 parent aaee834 commit 05fc020
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/EPEC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
37 changes: 37 additions & 0 deletions src/Games.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<arma::vec> devns = std::vector<arma::vec>(this->nCountr);
std::vector<arma::vec> prevDevns = std::vector<arma::vec>(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
Expand Down
41 changes: 41 additions & 0 deletions src/Models.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/games.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/models.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 05fc020

Please sign in to comment.