Skip to content

Commit

Permalink
Fixed template inference errors. Now compiles with Clang10
Browse files Browse the repository at this point in the history
  • Loading branch information
cvanaret committed Oct 15, 2024
1 parent 23056f9 commit e446e85
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
16 changes: 8 additions & 8 deletions uno/model/HomogeneousEqualityConstrainedModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ namespace uno {
std::vector<size_t> upper_bounded_slacks;
std::vector<size_t> single_lower_bounded_slacks;
std::vector<size_t> single_upper_bounded_slacks;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>&>> lower_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>&>> upper_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>&>> single_lower_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>&>> single_upper_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>>> lower_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>>> upper_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>>> single_lower_bounded_variables;
Concatenation<const Collection<size_t>&, CollectionAdapter<std::vector<size_t>>> single_upper_bounded_variables;
};

// Transform the problem into an equality-constrained problem with constraints c(x) = 0. This implies:
Expand All @@ -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()) {
Expand Down
3 changes: 2 additions & 1 deletion uno/model/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ namespace uno {
// compute ||c||
template <typename Array>
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);
Expand Down
3 changes: 2 additions & 1 deletion uno/reformulation/OptimalityProblem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ namespace uno {
inline double OptimalityProblem::complementarity_error(const Vector<double>& primals, const std::vector<double>& 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;
}
Expand Down
6 changes: 4 additions & 2 deletions uno/reformulation/l1RelaxedProblem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ namespace uno {
inline double l1RelaxedProblem::complementarity_error(const Vector<double>& primals, const std::vector<double>& 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;
}
Expand All @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions uno/symbolic/CollectionAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ namespace uno {
template <typename Array>
class CollectionAdapter: public Collection<typename std::remove_reference_t<Array>::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 <typename Array>
CollectionAdapter<Array>::CollectionAdapter(Array&& array):
Collection<typename std::remove_reference_t<Array>::value_type>(), array(std::forward<Array>(array)) {
CollectionAdapter<Array>::CollectionAdapter(const Array& array):
Collection<typename std::remove_reference_t<Array>::value_type>(), array(array) {
}

template <typename Array>
Expand All @@ -40,6 +40,11 @@ namespace uno {
void CollectionAdapter<Array>::increment_iterator(size_t& index) const {
index++;
}

template <typename Array>
CollectionAdapter<Array> adapt(const Array& array) {
return CollectionAdapter<Array>{array};
}
} // namespace

#endif // UNO_COLLECTIONADAPTER_H
4 changes: 2 additions & 2 deletions uno/symbolic/Concatenation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace uno {
}

protected:
Collection1 collection1;
Collection2 collection2;
const Collection1 collection1;
const Collection2 collection2;
};

template <typename Collection1, typename Collection2>
Expand Down
8 changes: 4 additions & 4 deletions uno/symbolic/VectorExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ 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;

iterator begin() const { return iterator(*this, 0); }
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 <typename Indices, typename Callable>
VectorExpression<Indices, Callable>::VectorExpression(Indices&& indices, Callable&& component_function):
indices(std::forward<Indices>(indices)), component_function(std::forward<Callable>(component_function)) {
VectorExpression<Indices, Callable>::VectorExpression(const Indices& indices, Callable&& component_function):
indices(indices), component_function(std::forward<Callable>(component_function)) {
}

template <typename Indices, typename Callable>
Expand Down

0 comments on commit e446e85

Please sign in to comment.