Skip to content

Commit

Permalink
Print solution values in the logs if a solution exists (#2562)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Payet <vincent.payet@rte-france.com>
  • Loading branch information
flomnes and payetvin authored Jan 8, 2025
1 parent 69a5225 commit 306d8f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#pragma once

#include <map>
#include <string>
#include <vector>

#include "mipVariable.h"
Expand Down Expand Up @@ -51,6 +53,7 @@ class IMipSolution
virtual double getObjectiveValue() const = 0;
virtual double getOptimalValue(const IMipVariable* var) const = 0;
virtual std::vector<double> getOptimalValues(const std::vector<IMipVariable*>& vars) const = 0;
virtual const std::map<std::string, double>& solutionValues() const = 0;
};

} // namespace Antares::Solver::Modeler::Api
22 changes: 22 additions & 0 deletions src/solver/modeler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#include <fstream>

#include <antares/logs/logs.h>
#include <antares/solver/modeler/api/linearProblemBuilder.h>
#include <antares/solver/modeler/loadFiles/loadFiles.h>
Expand Down Expand Up @@ -75,6 +77,26 @@ int main(int argc, const char** argv)

logs.info() << "Number of variables: " << pb.variableCount();
logs.info() << "Number of constraints: " << pb.constraintCount();

logs.info() << "Launching resolution...";
auto* solution = pb.solve(parameters.solverLogs);
switch (solution->getStatus())
{
case Antares::Solver::Modeler::Api::MipStatus::OPTIMAL:
case Antares::Solver::Modeler::Api::MipStatus::FEASIBLE:
if (!parameters.noOutput)
{
logs.info() << "Writing variables...";
std::ofstream sol_out(std::filesystem::current_path() / "solution.csv");
for (const auto& [name, value]: solution->solutionValues())
{
sol_out << name << " " << value << std::endl;
}
}
break;
default:
logs.error() << "Problem during linear optimization";
}
}
catch (const LoadFiles::ErrorLoadingYaml&)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class OrtoolsMipSolution final: public Api::IMipSolution
double getOptimalValue(const Api::IMipVariable* var) const override;
std::vector<double> getOptimalValues(
const std::vector<Api::IMipVariable*>& vars) const override;
const std::map<std::string, double>& solutionValues() const override;

private:
operations_research::MPSolver::ResultStatus status_;
Expand Down
5 changes: 5 additions & 0 deletions src/solver/modeler/ortoolsImpl/mipSolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ std::vector<double> OrtoolsMipSolution::getOptimalValues(
return solution;
}

const std::map<std::string, double>& OrtoolsMipSolution::solutionValues() const
{
return solution_;
}

} // namespace Antares::Solver::Modeler::OrtoolsImpl

0 comments on commit 306d8f3

Please sign in to comment.