alpaqa 1.0.0a8
Nonconvex constrained optimization
Loading...
Searching...
No Matches
index-set.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <span>
5
6namespace alpaqa::detail {
7
8template <Config Conf>
9struct IndexSet {
11
12 IndexSet(length_t N, length_t n) : N{N}, n{n}, storage{N + N * n} {}
13
17
18 auto sizes() { return storage.segment(0, N); }
19 auto sizes() const { return storage.segment(0, N); }
20 auto indices() { return storage.segment(N, N * n); }
21 auto indices() const { return storage.segment(N, N * n); }
22
24 length_t nJ = sizes()(i);
25 return indices().segment(n * i, nJ);
26 }
27
29 length_t nJ = sizes()(i);
30 length_t nK = n - nJ;
31 return indices().segment(n * i + nJ, nK);
32 }
33
34 // Compute the complement of the index set `in`. Write the indices not in
35 // the input to 'out'.
36 static void compute_complement(std::span<const index_t> in,
37 std::span<index_t> out) {
38 compute_complement(in, out.data(),
39 static_cast<length_t>(in.size() + out.size()));
40 }
41 static void compute_complement(std::span<const index_t> in,
42 std::span<index_t> out, length_t n) {
43 assert(in.size() + out.size() == static_cast<size_t>(n));
44 compute_complement(in, out.data(), n);
45 }
47 assert(in.size() + out.size() == n);
48 compute_complement(std::span{in.data(), static_cast<size_t>(in.size())},
49 out.data(), n);
50 }
51
52 template <class F>
53 void update(const F &condition) {
54 // Evaluate the condition for all indices in the given 'time_step',
55 // append the indices evaluating to true to 'out', and return the number
56 // of indices that were appended.
57 auto build_Jt = [&](index_t time_step, index_t *out) {
58 index_t j = 0; // index into the array of inactive indices
59 for (index_t c = 0; c < n; ++c) { // components within time step
60 if (condition(time_step, c)) // if the component is active,
61 out[j++] = c; // append the index of this component to J
62 }
63 return j; // return the number of elements in J
64 };
65
66 auto sizes = this->sizes();
67 auto *indices = this->indices().data();
68 for (index_t t = 0; t < N; ++t) { // time steps
69 // Generate the set of inactive indices, J
70 index_t num_J = build_Jt(t, indices);
71 sizes(t) = num_J; // save number of inactive indices for later
72 std::span J{indices, static_cast<size_t>(num_J)};
73 // Generate the complement, the set of active indices, K
74 compute_complement(J, indices + num_J, n);
75 // Prepare for next time step
76 indices += n;
77 }
78 }
79
80 private:
81 static void compute_complement(std::span<const index_t> in, index_t *out,
82 length_t n) {
83 length_t c = 0; // components within time step
84 length_t k = 0; // index into the array of active indices
85 // iterate over the array with indices 'in'
86 for (index_t j : in) { //
87 for (; c < j; ++c) // for all indices not in J
88 out[k++] = c; // append the index of this component
89 ++c; // skip indices in J, i.e. c == j
90 }
91 // add final indices not in J
92 for (; c < n; ++c)
93 out[k++] = c;
94 }
95};
96
97} // namespace alpaqa::detail
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:42
typename Conf::indexvec indexvec
Definition: config.hpp:64
typename Conf::rindexvec rindexvec
Definition: config.hpp:65
typename Conf::index_t index_t
Definition: config.hpp:63
typename Conf::length_t length_t
Definition: config.hpp:62
typename Conf::crindexvec crindexvec
Definition: config.hpp:66
static void compute_complement(crindexvec in, rindexvec out, length_t n)
Definition: index-set.hpp:46
static void compute_complement(std::span< const index_t > in, std::span< index_t > out, length_t n)
Definition: index-set.hpp:41
static void compute_complement(std::span< const index_t > in, std::span< index_t > out)
Definition: index-set.hpp:36
IndexSet(length_t N, length_t n)
Definition: index-set.hpp:12
static void compute_complement(std::span< const index_t > in, index_t *out, length_t n)
Definition: index-set.hpp:81
crindexvec compl_indices(index_t i) const
Definition: index-set.hpp:28
void update(const F &condition)
Definition: index-set.hpp:53
crindexvec indices(index_t i) const
Definition: index-set.hpp:23