-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _api_Heuristics_RENS: | ||
|
||
RENS (Relaxation Enforced Neighbourhood Search) | ||
=============================================== | ||
|
||
.. doxygenclass:: idol::Heuristics::RENS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ Heuristics | |
|
||
SimpleRounding | ||
IntegerMaster | ||
RENS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// Created by henri on 16.10.23. | ||
// | ||
|
||
#ifndef IDOL_RENS_H | ||
#define IDOL_RENS_H | ||
|
||
#include "CallbackFactory.h" | ||
#include "Callback.h" | ||
#include "optimizers/OptimizerFactory.h" | ||
|
||
namespace idol::Heuristics { | ||
class RENS; | ||
} | ||
|
||
class idol::Heuristics::RENS : public CallbackFactory { | ||
|
||
std::unique_ptr<OptimizerFactory> m_optimizer_factory; | ||
double m_minimum_ratio_of_integer_variables_to_fix = .5; | ||
double m_minimum_ratio_of_variables_to_fix = .25; | ||
|
||
RENS(const RENS& t_src); | ||
public: | ||
RENS() = default; | ||
|
||
class Strategy : public Callback { | ||
double m_minimum_ratio_of_integer_variables_to_fix; | ||
double m_minimum_ratio_of_variables_to_fix; | ||
std::unique_ptr<OptimizerFactory> m_optimizer_factory; | ||
protected: | ||
void operator()(CallbackEvent t_event) override; | ||
public: | ||
Strategy(double t_minimum_ratio_of_integer_variables_to_fix, | ||
double t_minimum_ratio_of_variables_to_fix, | ||
OptimizerFactory* t_optimizer_factory); | ||
}; | ||
|
||
Callback *operator()() override { | ||
|
||
auto* result = new Strategy(m_minimum_ratio_of_integer_variables_to_fix, | ||
m_minimum_ratio_of_variables_to_fix, | ||
m_optimizer_factory->clone()); | ||
|
||
return result; | ||
} | ||
|
||
[[nodiscard]] CallbackFactory *clone() const override { | ||
return new RENS(*this); | ||
} | ||
|
||
RENS& with_optimizer(const OptimizerFactory& t_optimizer_factory); | ||
|
||
}; | ||
|
||
#endif //IDOL_RENS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,5 @@ class idol::Heuristics::SimpleRounding : public CallbackFactory { | |
} | ||
|
||
}; | ||
|
||
#endif //IDOL_SIMPLEROUNDING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Created by henri on 16.10.23. | ||
// | ||
#include "optimizers/callbacks/RENS.h" | ||
#include "modeling/models/Model.h" | ||
|
||
idol::Heuristics::RENS::RENS(const RENS& t_src) | ||
: m_optimizer_factory(t_src.m_optimizer_factory->clone()), | ||
m_minimum_ratio_of_variables_to_fix(t_src.m_minimum_ratio_of_variables_to_fix), | ||
m_minimum_ratio_of_integer_variables_to_fix(t_src.m_minimum_ratio_of_integer_variables_to_fix) | ||
{ | ||
|
||
} | ||
|
||
idol::Heuristics::RENS &idol::Heuristics::RENS::with_optimizer(const idol::OptimizerFactory& t_optimizer_factory) { | ||
|
||
if (m_optimizer_factory) { | ||
throw Exception("An optimizer for RENS has already been given."); | ||
} | ||
|
||
m_optimizer_factory.reset(t_optimizer_factory.clone()); | ||
|
||
return *this; | ||
} | ||
|
||
idol::Heuristics::RENS::Strategy::Strategy(double t_minimum_ratio_of_integer_variables_to_fix, | ||
double t_minimum_ratio_of_variables_to_fix, | ||
OptimizerFactory* t_optimizer_factory) | ||
: m_minimum_ratio_of_integer_variables_to_fix(t_minimum_ratio_of_integer_variables_to_fix), | ||
m_minimum_ratio_of_variables_to_fix(t_minimum_ratio_of_variables_to_fix), | ||
m_optimizer_factory(t_optimizer_factory) { | ||
|
||
} | ||
|
||
void idol::Heuristics::RENS::Strategy::operator()(idol::CallbackEvent t_event) { | ||
|
||
if (t_event != InvalidSolution) { | ||
return; | ||
} | ||
|
||
const auto& primal_solution = this->primal_solution(); | ||
|
||
std::unique_ptr<Model> model(original_model().clone()); | ||
|
||
model->unuse(); | ||
|
||
unsigned int n_fixed_variables = 0; | ||
unsigned int n_integer_variables = 0; | ||
for (const auto& var : model->vars()) { | ||
|
||
if (model->get_var_type(var) == Continuous) { | ||
continue; | ||
} | ||
|
||
++n_integer_variables; | ||
|
||
const double value = primal_solution.get(var); | ||
|
||
if (is_integer(value, Tolerance::Integer)) { | ||
++n_fixed_variables; | ||
} | ||
|
||
model->set_var_lb(var, std::floor(value)); | ||
model->set_var_ub(var, std::ceil(value)); | ||
|
||
} | ||
|
||
if (n_fixed_variables < m_minimum_ratio_of_integer_variables_to_fix * n_integer_variables | ||
|| n_fixed_variables < m_minimum_ratio_of_variables_to_fix * model->vars().size() ) { | ||
return; | ||
} | ||
|
||
model->use(*m_optimizer_factory); | ||
|
||
model->optimize(); | ||
|
||
if (const auto status = model->get_status() ; status != Optimal && status != Feasible) { | ||
return; | ||
} | ||
|
||
submit_heuristic_solution(save_primal(*model)); | ||
|
||
} | ||
|