Skip to content

Commit

Permalink
mip objective file, getter for mipvariable
Browse files Browse the repository at this point in the history
  • Loading branch information
payetvin committed Jul 29, 2024
1 parent 72f4758 commit e707ed1
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ namespace Antares::Solver::Optim::Api

class MipObjective
{
virtual void setCoefficient(MipVariable& var, double coefficient) = 0;
virtual void setCoefficient(MipVariable* var, double coefficient) = 0;

virtual void setMaximization() = 0;
virtual void setMinimization() = 0;

virtual double getCoefficient(MipVariable& var) = 0;
virtual double getCoefficient(MipVariable* var) = 0;

virtual bool getMaximization() = 0;
virtual bool getMinimization() = 0;

virtual double getValue() = 0;
};

} // namespace Antares::Solver::Optim::Api
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class MipSolution

protected:
MipStatus responseStatus_;
std::unique_ptr<MipObjective> objective_;

std::map<const MipVariable*, double> solution_;
};
Expand Down
2 changes: 2 additions & 0 deletions src/solver/optim/ortoolsImpl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ set(PROJ optim-ortools-impl)
set(HEADERS
include/antares/solver/optim/ortoolsImpl/mipVariable.h
include/antares/solver/optim/ortoolsImpl/mipSolution.h
include/antares/solver/optim/ortoolsImpl/mipObjective.h
)
set(SRC_ORTOOLS_IMPL
${HEADERS}
mipVariable.cpp
mipSolution.cpp
mipObjective.cpp
)

source_group("solver\\optim\\api" FILES ${SRC_ORTOOLS_IMPL})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SPDX-License-Identifier: MPL-2.0
* This file is part of Antares-Simulator,
* Adequacy and Performance assessment for interconnected energy networks.
*
* Antares_Simulator is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public Licence 2.0 as published by
* the Mozilla Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Antares_Simulator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public Licence 2.0 for more details.
*
* You should have received a copy of the Mozilla Public Licence 2.0
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#pragma once

#include <ortools/linear_solver/linear_solver.h>

#include <antares/solver/optim/api/mipObjective.h>
#include <antares/solver/optim/api/mipVariable.h>

namespace Antares::Solver::Optim::OrtoolsImpl
{

class OrtoolsMipObjective final: public virtual Api::MipObjective
{
public:
OrtoolsMipObjective(operations_research::MPObjective* objective);
~OrtoolsMipObjective() = default;

void setCoefficient(Api::MipVariable* var, double coefficient) override;

void setMaximization() override;
void setMinimization() override;

double getCoefficient(Api::MipVariable* var) override;

bool getMaximization() override;
bool getMinimization() override;

operations_research::MPObjective* objective_;
};

} // namespace Antares::Solver::Optim::OrtoolsImpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,21 @@
namespace Antares::Solver::Optim::OrtoolsImpl
{

class OrtoolsMipSolution final: public virtual Api::MipSolution
class OrtoolsMipSolution : virtual public Api::MipSolution
{
public:
explicit OrtoolsMipSolution(operations_research::MPSolver::ResultStatus responseStatus,
const std::map<Api::MipVariable*, double>& solution);
OrtoolsMipSolution(operations_research::MPSolver::ResultStatus responseStatus,
const std::map<Api::MipVariable*, double>& solution,
operations_research::MPObjective* objective);

~OrtoolsMipSolution() = default;

Api::MipStatus getStatus() override;
double getObjectiveValue() override;
double getOptimalValue(const Api::MipVariable& var) const override;
std::vector<double> getOptimalValue(const std::vector<Api::MipVariable>& vars) const override;

operations_research::MPObjective* objective_;
};

} // namespace Antares::Solver::Optim::OrtoolsImpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ class OrtoolsMipVariable: virtual Api::MipVariable
double getLb() override;
double getUb() override;

operations_research::MPVariable* get();

~OrtoolsMipVariable() = default;
private:
// TODO: add friend class
explicit OrtoolsMipVariable(operations_research::MPVariable&);
~OrtoolsMipVariable() = default;
explicit OrtoolsMipVariable(operations_research::MPVariable*);

operations_research::MPVariable& mpVar_;
operations_research::MPVariable* mpVar_;
};

} // namespace Antares::Solver::Optim::OrtoolsImpl
64 changes: 64 additions & 0 deletions src/solver/optim/ortoolsImpl/mipObjective.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2007-2024, RTE (https://www.rte-france.com)
* See AUTHORS.txt
* SPDX-License-Identifier: MPL-2.0
* This file is part of Antares-Simulator,
* Adequacy and Performance assessment for interconnected energy networks.
*
* Antares_Simulator is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public Licence 2.0 as published by
* the Mozilla Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Antares_Simulator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public Licence 2.0 for more details.
*
* You should have received a copy of the Mozilla Public Licence 2.0
* along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
*/

#include <antares/solver/optim/ortoolsImpl/mipObjective.h>
#include <antares/solver/optim/ortoolsImpl/mipVariable.h>

namespace Antares::Solver::Optim::OrtoolsImpl
{

OrtoolsMipObjective::OrtoolsMipObjective(operations_research::MPObjective* objective):
objective_(objective)
{}

void OrtoolsMipObjective::setCoefficient(Api::MipVariable* var, double coefficient)
{
auto* mpvar = dynamic_cast<OrtoolsMipVariable*>(var);
objective_->SetCoefficient(mpvar->get(), coefficient);
}

void OrtoolsMipObjective::setMaximization()
{
objective_->SetMaximization();
}

void OrtoolsMipObjective::setMinimization()
{
objective_->SetMinimization();
}

double OrtoolsMipObjective::getCoefficient(Api::MipVariable* var)
{
auto* mpvar = dynamic_cast<OrtoolsMipVariable*>(var);
return objective_->GetCoefficient(mpvar->get());
}

bool OrtoolsMipObjective::getMaximization()
{
return objective_->maximization();
}

bool OrtoolsMipObjective::getMinimization()
{
return objective_->minimization();
}

} // namespace Antares::Solver::Optim::OrtoolsImpl
13 changes: 8 additions & 5 deletions src/solver/optim/ortoolsImpl/mipSolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
*/

#include <antares/solver/optim/ortoolsImpl/mipSolution.h>
#include <antares/solver/optim/ortoolsImpl/mipObjective.h>

namespace Antares::Solver::Optim::OrtoolsImpl
{

using OrMPSolver = operations_research::MPSolver;

OrtoolsMipSolution::OrtoolsMipSolution(OrMPSolver::ResultStatus responseStatus,
const std::map<Api::MipVariable*, double>& solution)
const std::map<Api::MipVariable*, double>& solution,
operations_research::MPObjective* objective):
objective_(objective)
{
// Only store non-zero values
for (const auto& varAndValue : solution)
Expand Down Expand Up @@ -60,10 +63,10 @@ Api::MipStatus OrtoolsMipSolution::getStatus()
return responseStatus_;
}

/* double OrtoolsMipSolution::getObjectiveValue() */
/* { */

/* } */
double OrtoolsMipSolution::getObjectiveValue()
{
return objective_->Value();
}

double OrtoolsMipSolution::getOptimalValue(const Api::MipVariable& var) const
{
Expand Down
17 changes: 11 additions & 6 deletions src/solver/optim/ortoolsImpl/mipVariable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,39 @@
namespace Antares::Solver::Optim::OrtoolsImpl
{

OrtoolsMipVariable::OrtoolsMipVariable(operations_research::MPVariable& mpVar):
OrtoolsMipVariable::OrtoolsMipVariable(operations_research::MPVariable* mpVar):
mpVar_(mpVar)
{
}

void OrtoolsMipVariable::setLb(double lb)
{
mpVar_.SetLB(lb);
mpVar_->SetLB(lb);
}

void OrtoolsMipVariable::setUb(double ub)
{
mpVar_.SetUB(ub);
mpVar_->SetUB(ub);
}

void OrtoolsMipVariable::setBounds(double lb, double ub)
{
mpVar_.SetBounds(lb, ub);
mpVar_->SetBounds(lb, ub);
}

double OrtoolsMipVariable::getLb()
{
return mpVar_.lb();
return mpVar_->lb();
}

double OrtoolsMipVariable::getUb()
{
return mpVar_.ub();
return mpVar_->ub();
}

operations_research::MPVariable* OrtoolsMipVariable::get()
{
return mpVar_;
}

} // namespace Antares::Solver::Optim::OrtoolsImpl

0 comments on commit e707ed1

Please sign in to comment.