Commit 934e72a7 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

Applying clang-format formatting to radixmin.

parent 0397fd8f
Pipeline #48314 failed with stages
in 1 minute
......@@ -47,6 +47,7 @@ ELSE()
# Process subpackages
#
TRIBITS_PROCESS_SUBPACKAGES()
TRIBITS_PACKAGE_DEF()
TRIBITS_PACKAGE_POSTPROCESS()
ENDIF()
......
#include "radixmin/adapter.hh"
#include <sstream>
namespace radixmin
{
Adapter::Adapter(SPProblemInterface problem)
: Solver(problem)
, m_solver(NULL)
{
this->check_consistency();
}
namespace radixmin {
void Adapter::check_consistency() const
{
Solver::check_consistency();
ProblemInterface::check_consistency();
Adapter::Adapter(SPProblemInterface problem) : Solver(problem), m_solver(NULL) {
this->check_consistency();
}
FGH Adapter::obj(const Array& x, int flags)
{
return m_problem->obj(x, flags);
void Adapter::check_consistency() const {
Solver::check_consistency();
ProblemInterface::check_consistency();
}
void Adapter::refresh()
{
radix_check(m_solver);
m_solver->refresh();
this->check_consistency();
FGH Adapter::obj(const Array &x, int flags) { return m_problem->obj(x, flags); }
void Adapter::refresh() {
radix_check(m_solver);
m_solver->refresh();
this->check_consistency();
}
bool Adapter::converging_rapidly() const
{
this->check_consistency();
radix_check(m_solver);
return m_solver->converging_rapidly();
bool Adapter::converging_rapidly() const {
this->check_consistency();
radix_check(m_solver);
return m_solver->converging_rapidly();
}
bool Adapter::converged() const
{
this->check_consistency();
radix_check(m_solver);
return m_solver->converged();
bool Adapter::converged() const {
this->check_consistency();
radix_check(m_solver);
return m_solver->converged();
}
void Adapter::step()
{
this->check_consistency();
radix_check(m_solver);
m_solver->step();
void Adapter::step() {
this->check_consistency();
radix_check(m_solver);
m_solver->step();
}
//~ #####################################################################
SVDAdapter::SVDAdapter(SPProblemInterface problem)
: Adapter(problem)
, m_svd(problem->econ())
{
// force the initial solution to follow the econ constraints
Array x_new = m_svd.projection(m_problem->x());
m_problem->x(x_new);
this->check_consistency();
}
: Adapter(problem), m_svd(problem->econ()) {
// force the initial solution to follow the econ constraints
Array x_new = m_svd.projection(m_problem->x());
m_problem->x(x_new);
void SVDAdapter::check_consistency() const
{
Adapter::check_consistency();
radix_check(m_problem->icon().nrows() == 0);
m_svd.check_consistency();
// check that the svd is (roughly) consistent with m_problem->econ()
radix_check(m_svd.s().size() == m_problem->econ().nrows());
this->check_consistency();
}
FGH SVDAdapter::obj(const Array& y_noninflu, int flags)
{
radix_line("begin SVDAdapter::obj");
this->check_consistency();
Array x = m_svd.rtransform_x(y_noninflu);
FGH results = m_problem->obj(x, flags);
// transform the derivatives
if (flags & G)
{
results.g = m_svd.transform_g(results.g);
}
if (flags & H)
{
results.h = m_svd.transform_h(results.h);
}
radix_line("end SVDAdapter::obj");
return results;
void SVDAdapter::check_consistency() const {
Adapter::check_consistency();
radix_check(m_problem->icon().nrows() == 0);
m_svd.check_consistency();
// check that the svd is (roughly) consistent with m_problem->econ()
radix_check(m_svd.s().size() == m_problem->econ().nrows());
}
LinearSystem SVDAdapter::icon() const
{
return LinearSystem(0, this->x().size());
FGH SVDAdapter::obj(const Array &y_noninflu, int flags) {
radix_line("begin SVDAdapter::obj");
this->check_consistency();
Array x = m_svd.rtransform_x(y_noninflu);
FGH results = m_problem->obj(x, flags);
// transform the derivatives
if (flags & G) {
results.g = m_svd.transform_g(results.g);
}
if (flags & H) {
results.h = m_svd.transform_h(results.h);
}
radix_line("end SVDAdapter::obj");
return results;
}
LinearSystem SVDAdapter::econ() const
{
return LinearSystem(0, this->x().size());
LinearSystem SVDAdapter::icon() const {
return LinearSystem(0, this->x().size());
}
Array SVDAdapter::x() const
{
return m_svd.transform_x(m_problem->x());
LinearSystem SVDAdapter::econ() const {
return LinearSystem(0, this->x().size());
}
void SVDAdapter::x(const Array& newval)
{
Array x_new = m_svd.rtransform_x(newval);
m_problem->x(x_new);
Array SVDAdapter::x() const { return m_svd.transform_x(m_problem->x()); }
void SVDAdapter::x(const Array &newval) {
Array x_new = m_svd.rtransform_x(newval);
m_problem->x(x_new);
}
void SVDAdapter::refresh()
{
radix_line("begin SVDAdapter::refresh");
m_svd = SVD(m_problem->econ());
Adapter::refresh();
radix_line("end SVDAdapter::refresh");
void SVDAdapter::refresh() {
radix_line("begin SVDAdapter::refresh");
m_svd = SVD(m_problem->econ());
Adapter::refresh();
radix_line("end SVDAdapter::refresh");
}
//~ #####################################################################
ActiveSet::ActiveSet(SPProblemInterface problem)
: m_icon(problem->icon())
, m_econ(problem->econ())
, m_index_map(m_econ.nrows(), TRUE_ECON_SENTINEL)
{
this->check_consistency();
}
void ActiveSet::check_consistency() const
{
m_icon.check_consistency();
m_econ.check_consistency();
radix_check(m_index_map.size() == m_econ.nrows());
// check that the active icons match the econs and the entries are
// unique
VectorBool k_icon_used(m_icon.nrows(), false);
for (size_t k_econ = 0; k_econ < m_index_map.size(); ++k_econ)
{
size_t k_icon = m_index_map[k_econ];
if (k_icon == TRUE_ECON_SENTINEL)
{
continue;
}
radix_check(k_icon < m_icon.nrows());
radix_check(m_icon.lhs()[k_icon] == m_econ.lhs()[k_econ]);
radix_check(m_icon.rhs()[k_icon] == m_econ.rhs()[k_econ]);
radix_check(k_icon_used[k_icon] == false);
k_icon_used[k_icon] = true;
: m_icon(problem->icon()), m_econ(problem->econ()),
m_index_map(m_econ.nrows(), TRUE_ECON_SENTINEL) {
this->check_consistency();
}
void ActiveSet::check_consistency() const {
m_icon.check_consistency();
m_econ.check_consistency();
radix_check(m_index_map.size() == m_econ.nrows());
// check that the active icons match the econs and the entries are
// unique
VectorBool k_icon_used(m_icon.nrows(), false);
for (size_t k_econ = 0; k_econ < m_index_map.size(); ++k_econ) {
size_t k_icon = m_index_map[k_econ];
if (k_icon == TRUE_ECON_SENTINEL) {
continue;
}
radix_check(k_icon < m_icon.nrows());
radix_check(m_icon.lhs()[k_icon] == m_econ.lhs()[k_econ]);
radix_check(m_icon.rhs()[k_icon] == m_econ.rhs()[k_econ]);
radix_check(k_icon_used[k_icon] == false);
k_icon_used[k_icon] = true;
}
}
size_t ActiveSet::econ_index(size_t k_icon)
{
auto k_econ_it = std::find(
m_index_map.begin(),
m_index_map.end(),
k_icon);
if (k_econ_it == m_index_map.end())
{
throw EconNotActive("Not found in m_index_map");
}
return k_econ_it - m_index_map.begin();
size_t ActiveSet::econ_index(size_t k_icon) {
auto k_econ_it = std::find(m_index_map.begin(), m_index_map.end(), k_icon);
if (k_econ_it == m_index_map.end()) {
throw EconNotActive("Not found in m_index_map");
}
return k_econ_it - m_index_map.begin();
}
bool ActiveSet::contains(size_t k_icon) const
{
auto k_econ_it = std::find(
m_index_map.begin(),
m_index_map.end(),
k_icon);
return k_econ_it != m_index_map.end();
bool ActiveSet::contains(size_t k_icon) const {
auto k_econ_it = std::find(m_index_map.begin(), m_index_map.end(), k_icon);
return k_econ_it != m_index_map.end();
}
VectorBool ActiveSet::active_flags() const
{
this->check_consistency();
VectorBool flags(m_icon.nrows());
for (auto k_icon : m_index_map)
{
if (k_icon != TRUE_ECON_SENTINEL)
{
radix_check(k_icon < flags.size());
flags[k_icon] = true;
}
VectorBool ActiveSet::active_flags() const {
this->check_consistency();
VectorBool flags(m_icon.nrows());
for (auto k_icon : m_index_map) {
if (k_icon != TRUE_ECON_SENTINEL) {
radix_check(k_icon < flags.size());
flags[k_icon] = true;
}
return flags;
}
return flags;
}
VectorBool ActiveSet::inactive_flags() const
{
VectorBool flags = this->active_flags();
for (auto flag : flags)
{
flag = !flag;
}
return flags;
VectorBool ActiveSet::inactive_flags() const {
VectorBool flags = this->active_flags();
for (auto flag : flags) {
flag = !flag;
}
return flags;
}
size_t ActiveSet::activate(size_t k_icon)
{
this->check_consistency();
radix_check(!this->contains(k_icon));
m_econ.append_row(
m_icon.lhs()[k_icon],
m_icon.rhs()[k_icon]);
m_index_map.push_back(k_icon);
this->check_consistency();
return m_index_map.size() - 1;
size_t ActiveSet::activate(size_t k_icon) {
this->check_consistency();
radix_check(!this->contains(k_icon));
m_econ.append_row(m_icon.lhs()[k_icon], m_icon.rhs()[k_icon]);
m_index_map.push_back(k_icon);
this->check_consistency();
return m_index_map.size() - 1;
}
size_t ActiveSet::deactivate(size_t k_icon)
{
this->check_consistency();
radix_check(this->contains(k_icon));
size_t ActiveSet::deactivate(size_t k_icon) {
this->check_consistency();
radix_check(this->contains(k_icon));
size_t k_econ = this->econ_index(k_icon);
m_econ.delete_row(k_econ);
m_index_map.erase(m_index_map.begin() + k_econ);
return k_econ;
size_t k_econ = this->econ_index(k_icon);
m_econ.delete_row(k_econ);
m_index_map.erase(m_index_map.begin() + k_econ);
return k_econ;
}
LinearSystem ActiveSet::active_icon() const
{
this->check_consistency();
return m_icon.slice(this->active_flags());
LinearSystem ActiveSet::active_icon() const {
this->check_consistency();
return m_icon.slice(this->active_flags());
}
LinearSystem ActiveSet::inactive_icon() const
{
this->check_consistency();
return m_icon.slice(this->inactive_flags());
LinearSystem ActiveSet::inactive_icon() const {
this->check_consistency();
return m_icon.slice(this->inactive_flags());
}
//~ #####################################################################
ActiveSetAdapter::ActiveSetAdapter(SPProblemInterface problem)
: Adapter(problem)
, m_active_set(problem)
{
// force the initial solution to follow the icon constraints
Array x_new = m_problem->x();
// x needs to satisfy the problem econ to call project_inbounds
x_new = SVD(m_problem->econ()).projection(x_new);
this->project_inbounds(m_active_set, x_new);
m_problem->x(x_new);
this->check_consistency();
: Adapter(problem), m_active_set(problem) {
// force the initial solution to follow the icon constraints
Array x_new = m_problem->x();
// x needs to satisfy the problem econ to call project_inbounds
x_new = SVD(m_problem->econ()).projection(x_new);
this->project_inbounds(m_active_set, x_new);
m_problem->x(x_new);
this->check_consistency();
}
void ActiveSetAdapter::check_consistency() const
{
Adapter::check_consistency();
m_active_set.check_consistency();
// check that m_active_set is (roughly) consistent with
// m_problem->icon()
radix_check(m_active_set.icon().nrows() == m_problem->icon().nrows());
radix_check(m_active_set.econ().nrows() >= m_problem->econ().nrows());
void ActiveSetAdapter::check_consistency() const {
Adapter::check_consistency();
m_active_set.check_consistency();
// check that m_active_set is (roughly) consistent with
// m_problem->icon()
radix_check(m_active_set.icon().nrows() == m_problem->icon().nrows());
radix_check(m_active_set.econ().nrows() >= m_problem->econ().nrows());
}
/** Adjust x so that it obeys the icon. Update the active_set as well.
* Returns true if any changes were made. */
bool ActiveSetAdapter::project_inbounds(ActiveSet& active_set, Array& x)
{
radix_line("begin ActiveSetAdapter::project_inbounds");
this->check_consistency();
bool any_changes = false;
const LinearSystem& icon = active_set.icon();
// check whether any should be added to active set;
// use multiple passes in case a projection satisfies two constraints
// simultaneously (by accident)
int loop_exit_status = 1;
for (size_t i_iter = 0; i_iter <= 1000; ++i_iter)
{
Array margins = icon.rhs() - icon.lhs().dot(x);
bool pass_changes = false;
// if it's out of bounds, add to the active set
for (size_t k_icon = 0; k_icon < margins.size(); ++k_icon)
{
// violation -> activate, then start another pass
if (margins[k_icon] < 0. && !active_set.contains(k_icon))
{
radix_line("activating icon " << k_icon);
active_set.activate(k_icon);
x = SVD(active_set.econ()).projection(x);
pass_changes = true;
any_changes = true;
break;
}
}
// if no constraints were activated this pass, stop looping
if (!pass_changes)
{
loop_exit_status = 0;
break;
}
}
radix_insist(
loop_exit_status == 0,
"Cannot determine consistent active constraints");
// check whether any should be removed from active_set
Array g = m_problem->obj(x, G).g;
const Matrix& lhs = icon.lhs();
// traverse backward because the list might get shorter
for (size_t k_icon = lhs.nrows(); k_icon + 1 > 0; --k_icon)
{
if (!active_set.contains(k_icon) || lhs[k_icon].dot(g) < 0.)
{
continue;
}
radix_line("deactivating icon " << k_icon);
active_set.deactivate(k_icon);
bool ActiveSetAdapter::project_inbounds(ActiveSet &active_set, Array &x) {
radix_line("begin ActiveSetAdapter::project_inbounds");
this->check_consistency();
bool any_changes = false;
const LinearSystem &icon = active_set.icon();
// check whether any should be added to active set;
// use multiple passes in case a projection satisfies two constraints
// simultaneously (by accident)
int loop_exit_status = 1;
for (size_t i_iter = 0; i_iter <= 1000; ++i_iter) {
Array margins = icon.rhs() - icon.lhs().dot(x);
bool pass_changes = false;
// if it's out of bounds, add to the active set
for (size_t k_icon = 0; k_icon < margins.size(); ++k_icon) {
// violation -> activate, then start another pass
if (margins[k_icon] < 0. && !active_set.contains(k_icon)) {
radix_line("activating icon " << k_icon);
active_set.activate(k_icon);
x = SVD(active_set.econ()).projection(x);
pass_changes = true;
any_changes = true;
break;
}
}
// print the active set
std::stringstream ss;
ss << "Active set is now (";
for (size_t k_icon = 0; k_icon < icon.nrows(); ++k_icon)
{
if (active_set.contains(k_icon))
{
ss << k_icon << ", ";
}
// if no constraints were activated this pass, stop looping
if (!pass_changes) {
loop_exit_status = 0;
break;
}
}
radix_insist(loop_exit_status == 0,
"Cannot determine consistent active constraints");
// check whether any should be removed from active_set
Array g = m_problem->obj(x, G).g;
const Matrix &lhs = icon.lhs();
// traverse backward because the list might get shorter
for (size_t k_icon = lhs.nrows(); k_icon + 1 > 0; --k_icon) {
if (!active_set.contains(k_icon) || lhs[k_icon].dot(g) < 0.) {
continue;
}
radix_line("deactivating icon " << k_icon);
active_set.deactivate(k_icon);
any_changes = true;
}
// print the active set
std::stringstream ss;
ss << "Active set is now (";
for (size_t k_icon = 0; k_icon < icon.nrows(); ++k_icon) {
if (active_set.contains(k_icon)) {
ss << k_icon << ", ";
}
ss << ")";
radix_line(ss.str());
radix_line("end ActiveSetAdapter::project_inbounds");
return any_changes;
}
ss << ")";
radix_line(ss.str());
radix_line("end ActiveSetAdapter::project_inbounds");
return any_changes;
}
FGH ActiveSetAdapter::obj(const Array& x, int flags)
{
radix_line("begin ActiveSetAdapter::obj");
this->check_consistency();
// project x_ back in-bounds w/o updating m_active_set
ActiveSet as = m_active_set;
Array x_ = x;
this->project_inbounds(as, x_);
radix_line("end ActiveSetAdapter::obj");
return m_problem->obj(x_, flags);
}
FGH ActiveSetAdapter::obj(const Array &x, int flags) {
radix_line("begin ActiveSetAdapter::obj");
this->check_consistency();
LinearSystem ActiveSetAdapter::icon() const
{
return LinearSystem(0, m_problem->x().size());
// project x_ back in-bounds w/o updating m_active_set
ActiveSet as = m_active_set;
Array x_ = x;
this->project_inbounds(as, x_);
radix_line("end ActiveSetAdapter::obj");
return m_problem->obj(x_, flags);
}
LinearSystem ActiveSetAdapter::econ() const
{
return m_active_set.econ();
LinearSystem ActiveSetAdapter::icon() const {
return LinearSystem(0, m_problem->x().size());
}
void ActiveSetAdapter::x(const Array& newval)
{
radix_line("begin ActiveSetAdapter::x setter");
this->check_consistency();
Array x_new = newval;
bool any_changed = this->project_inbounds(m_active_set, x_new);
m_problem->x(x_new);
if (any_changed && m_solver)
{
m_solver->refresh();
}
radix_line("end ActiveSetAdapter::x setter");
}
LinearSystem ActiveSetAdapter::econ() const { return m_active_set.econ(); }
void ActiveSetAdapter::refresh()
{
Adapter::refresh();
m_active_set = ActiveSet(m_problem);
void ActiveSetAdapter::x(const Array &newval) {
radix_line("begin ActiveSetAdapter::x setter");
this->check_consistency();
Array x_new = newval;