From e446e856585bbaf0210e570e6c397066420b8040 Mon Sep 17 00:00:00 2001 From: Charlie Vanaret Date: Tue, 15 Oct 2024 23:16:29 +0200 Subject: [PATCH] Fixed template inference errors. Now compiles with Clang10 --- .../HomogeneousEqualityConstrainedModel.hpp | 16 ++++++++-------- uno/model/Model.hpp | 3 ++- uno/reformulation/OptimalityProblem.hpp | 3 ++- uno/reformulation/l1RelaxedProblem.hpp | 6 ++++-- uno/symbolic/CollectionAdapter.hpp | 13 +++++++++---- uno/symbolic/Concatenation.hpp | 4 ++-- uno/symbolic/VectorExpression.hpp | 8 ++++---- 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/uno/model/HomogeneousEqualityConstrainedModel.hpp b/uno/model/HomogeneousEqualityConstrainedModel.hpp index 9d645098..56c23891 100644 --- a/uno/model/HomogeneousEqualityConstrainedModel.hpp +++ b/uno/model/HomogeneousEqualityConstrainedModel.hpp @@ -66,10 +66,10 @@ namespace uno { std::vector upper_bounded_slacks; std::vector single_lower_bounded_slacks; std::vector single_upper_bounded_slacks; - Concatenation&, CollectionAdapter&>> lower_bounded_variables; - Concatenation&, CollectionAdapter&>> upper_bounded_variables; - Concatenation&, CollectionAdapter&>> single_lower_bounded_variables; - Concatenation&, CollectionAdapter&>> single_upper_bounded_variables; + Concatenation&, CollectionAdapter>> lower_bounded_variables; + Concatenation&, CollectionAdapter>> upper_bounded_variables; + Concatenation&, CollectionAdapter>> single_lower_bounded_variables; + Concatenation&, CollectionAdapter>> single_upper_bounded_variables; }; // Transform the problem into an equality-constrained problem with constraints c(x) = 0. This implies: @@ -88,10 +88,10 @@ namespace uno { slacks(this->model->get_inequality_constraints().size()), lower_bounded_slacks(this->slacks.size()), upper_bounded_slacks(this->slacks.size()), - lower_bounded_variables(concatenate(this->model->get_lower_bounded_variables(), CollectionAdapter(this->lower_bounded_slacks))), - upper_bounded_variables(concatenate(this->model->get_upper_bounded_variables(), CollectionAdapter(this->upper_bounded_slacks))), - single_lower_bounded_variables(concatenate(this->model->get_single_lower_bounded_variables(), CollectionAdapter(this->single_lower_bounded_slacks))), - single_upper_bounded_variables(concatenate(this->model->get_single_upper_bounded_variables(), CollectionAdapter(this->single_upper_bounded_slacks))){ + lower_bounded_variables(concatenate(this->model->get_lower_bounded_variables(), adapt(this->lower_bounded_slacks))), + upper_bounded_variables(concatenate(this->model->get_upper_bounded_variables(), adapt(this->upper_bounded_slacks))), + single_lower_bounded_variables(concatenate(this->model->get_single_lower_bounded_variables(), adapt(this->single_lower_bounded_slacks))), + single_upper_bounded_variables(concatenate(this->model->get_single_upper_bounded_variables(), adapt(this->single_upper_bounded_slacks))){ // register the inequality constraint of each slack size_t inequality_index = 0; for (const size_t constraint_index: this->model->get_inequality_constraints()) { diff --git a/uno/model/Model.hpp b/uno/model/Model.hpp index 4c2c4348..ee74a6d9 100644 --- a/uno/model/Model.hpp +++ b/uno/model/Model.hpp @@ -94,7 +94,8 @@ namespace uno { // compute ||c|| template double Model::constraint_violation(const Array& constraints, Norm residual_norm) const { - const VectorExpression constraint_violation{Range(constraints.size()), [&](size_t constraint_index) { + const Range constraints_range = Range(constraints.size()); + const VectorExpression constraint_violation{constraints_range, [&](size_t constraint_index) { return this->constraint_violation(constraints[constraint_index], constraint_index); }}; return norm(residual_norm, constraint_violation); diff --git a/uno/reformulation/OptimalityProblem.hpp b/uno/reformulation/OptimalityProblem.hpp index bbc7e0f0..1040d48f 100644 --- a/uno/reformulation/OptimalityProblem.hpp +++ b/uno/reformulation/OptimalityProblem.hpp @@ -94,7 +94,8 @@ namespace uno { inline double OptimalityProblem::complementarity_error(const Vector& primals, const std::vector& constraints, const Multipliers& multipliers, double shift_value, Norm residual_norm) const { // bound constraints - const VectorExpression variable_complementarity{Range(this->model.number_variables), [&](size_t variable_index) { + const Range variables_range = Range(this->model.number_variables); + const VectorExpression variable_complementarity{variables_range, [&](size_t variable_index) { if (0. < multipliers.lower_bounds[variable_index]) { return multipliers.lower_bounds[variable_index] * (primals[variable_index] - this->model.variable_lower_bound(variable_index)) - shift_value; } diff --git a/uno/reformulation/l1RelaxedProblem.hpp b/uno/reformulation/l1RelaxedProblem.hpp index 942f283d..4a4cf069 100644 --- a/uno/reformulation/l1RelaxedProblem.hpp +++ b/uno/reformulation/l1RelaxedProblem.hpp @@ -219,7 +219,8 @@ namespace uno { inline double l1RelaxedProblem::complementarity_error(const Vector& primals, const std::vector& constraints, const Multipliers& multipliers, double shift_value, Norm residual_norm) const { // bound constraints - const VectorExpression bounds_complementarity{Range(this->number_variables), [&](size_t variable_index) { + const Range variables_range = Range(this->number_variables); + const VectorExpression bounds_complementarity{variables_range, [&](size_t variable_index) { if (0. < multipliers.lower_bounds[variable_index]) { return multipliers.lower_bounds[variable_index] * (primals[variable_index] - this->variable_lower_bound(variable_index)) - shift_value; } @@ -231,7 +232,8 @@ namespace uno { // general constraints // TODO use the values of the relaxed constraints - const VectorExpression constraints_complementarity{Range(this->number_constraints), [&](size_t constraint_index) { + const Range constraints_range = Range(this->number_constraints); + const VectorExpression constraints_complementarity{constraints_range, [&](size_t constraint_index) { if (this->model.get_constraint_bound_type(constraint_index) != EQUAL_BOUNDS) { if (0. < multipliers.constraints[constraint_index]) { // lower bound return multipliers.constraints[constraint_index] * (constraints[constraint_index] - this->constraint_lower_bound(constraint_index)) - shift_value; diff --git a/uno/symbolic/CollectionAdapter.hpp b/uno/symbolic/CollectionAdapter.hpp index cb8debb3..7598a79c 100644 --- a/uno/symbolic/CollectionAdapter.hpp +++ b/uno/symbolic/CollectionAdapter.hpp @@ -11,19 +11,19 @@ namespace uno { template class CollectionAdapter: public Collection::value_type> { public: - explicit CollectionAdapter(Array&& array); + explicit CollectionAdapter(const Array& array); [[nodiscard]] size_t size() const override; [[nodiscard]] typename CollectionAdapter::value_type dereference_iterator(size_t index) const override; void increment_iterator(size_t& index) const override; protected: - const Array array; + const Array& array; }; template - CollectionAdapter::CollectionAdapter(Array&& array): - Collection::value_type>(), array(std::forward(array)) { + CollectionAdapter::CollectionAdapter(const Array& array): + Collection::value_type>(), array(array) { } template @@ -40,6 +40,11 @@ namespace uno { void CollectionAdapter::increment_iterator(size_t& index) const { index++; } + + template + CollectionAdapter adapt(const Array& array) { + return CollectionAdapter{array}; + } } // namespace #endif // UNO_COLLECTIONADAPTER_H diff --git a/uno/symbolic/Concatenation.hpp b/uno/symbolic/Concatenation.hpp index 85538bdb..5bc5a7a5 100644 --- a/uno/symbolic/Concatenation.hpp +++ b/uno/symbolic/Concatenation.hpp @@ -30,8 +30,8 @@ namespace uno { } protected: - Collection1 collection1; - Collection2 collection2; + const Collection1 collection1; + const Collection2 collection2; }; template diff --git a/uno/symbolic/VectorExpression.hpp b/uno/symbolic/VectorExpression.hpp index fbd0717e..061e7618 100644 --- a/uno/symbolic/VectorExpression.hpp +++ b/uno/symbolic/VectorExpression.hpp @@ -39,7 +39,7 @@ namespace uno { // compatible with algorithms that query the type of the elements using value_type = double; - VectorExpression(Indices&& indices, Callable&& component_function); + VectorExpression(const Indices& indices, Callable&& component_function); [[nodiscard]] size_t size() const { return this->indices.size(); } [[nodiscard]] double operator[](size_t index) const; @@ -47,13 +47,13 @@ namespace uno { iterator end() const { return iterator(*this, this->size()); } protected: - const Indices indices; // store const reference or rvalue (temporary) + const Indices& indices; // store const reference or rvalue (temporary) const Callable component_function; }; template - VectorExpression::VectorExpression(Indices&& indices, Callable&& component_function): - indices(std::forward(indices)), component_function(std::forward(component_function)) { + VectorExpression::VectorExpression(const Indices& indices, Callable&& component_function): + indices(indices), component_function(std::forward(component_function)) { } template