15 const ArcTailAction& action,
const std::vector<V>& initial,
double duration,
20 auto require = [](
bool value,
const char* text) {
21 if (!value)
throw std::invalid_argument(text);
23 const double period = action.
period();
25 const auto n = initial.size();
26 const auto k = constraints.
target.size();
28 n >= 2 && weights.size() + 1 == n && constraints.
normals.size() == n &&
29 k > 0 && constraints.
target.allFinite() &&
31 "Arc search requires nodes, weights, affine constraints and callbacks");
32 const auto d = initial.front().size();
33 require(d > 0,
"Arc search requires a nonempty mass metric");
34 require(period > 0 && duration > 0 && duration < period &&
35 std::isfinite(period) && std::isfinite(duration) &&
46 "Positive finite arc search scales required");
47 M gram = M::Zero(k, k);
49 for (std::size_t j = 0; j < n; ++j) {
50 require(initial[j].size() == d && initial[j].allFinite() &&
51 constraints.
normals[j].rows() == d &&
52 constraints.
normals[j].cols() == k &&
53 constraints.
normals[j].allFinite(),
54 "Arc search metric or normal dimensions differ");
55 gram += constraints.
normals[j].transpose() * constraints.
normals[j];
57 for (
double w: weights) {
58 require(w > 0 && std::isfinite(w),
59 "Positive finite arc time weights required");
63 std::abs(sum - 1) < 1e-12 && (gram - M::Identity(k, k)).norm() < 1e-10,
64 "Normalized arc weights and orthonormal constraints required");
65 auto coordinate = [&](
const std::vector<V>& points) {
67 for (std::size_t j = 0; j < n; ++j)
68 value += constraints.
normals[j].transpose() * points[j];
71 auto enforce = [&](std::vector<V>& points) {
72 const V correction = constraints.
target - coordinate(points);
73 for (std::size_t j = 0; j < n; ++j)
74 points[j] += constraints.
normals[j] * correction;
76 std::size_t evaluations = 0, batches = 0, hessians = 0, physicalActions = 0;
77 auto ownedEvaluate = [&](
const std::vector<V>& q) {
80 evaluations += q.size();
83 auto measure = [&](
const std::vector<V>& points,
double u) {
87 result.
native = ownedEvaluate(points);
90 potential, options, ownedEvaluate);
94 V normal = V::Zero(k);
95 for (std::size_t j = 0; j < n; ++j)
96 normal += constraints.
normals[j].transpose() *
98 for (std::size_t j = 0; j < n; ++j) {
101 ((j ? weights[j - 1] : 0) + (j + 1 < n ? weights[j] : 0));
106 constraints.
normals[j] * normal)
112 (coordinate(points) - constraints.
target).norm();
130 double referenceDuration) {
140 auto points = initial;
142 auto state = measure(points, duration);
143 if (retain) retain(state);
144 for (std::size_t iteration = 0;
148 hessians += state.path.size();
149 std::vector<M> thirdContractions;
150 if (state.forceGradient.enabled) {
153 "A finite positive search derivative step is required");
155 thirdContractions.assign(n, M::Zero(d, d));
156 std::vector<std::size_t> active;
157 std::vector<double> norms(n, 0.);
158 std::vector<V> probes;
159 probes.reserve(2 * n);
160 for (std::size_t j = 0; j < n; ++j) {
161 norms[j] = state.forceGradient.residualGradients[j].norm();
162 require(std::isfinite(norms[j]),
163 "Finite residual-gradient norms required");
164 if (norms[j] == 0)
continue;
165 const V displacement =
166 epsilon * state.forceGradient.residualGradients[j] /
169 probes.push_back(state.path[j] + displacement);
170 probes.push_back(state.path[j] - displacement);
172 if (!probes.empty()) {
174 hessians += probes.size();
175 require(measured.size() == probes.size(),
176 "Search Hessian probe batch shape differs");
177 for (
const auto& value: measured)
178 require(value.rows() == d && value.cols() == d &&
180 (value - value.transpose()).norm() <=
181 1e-12 * std::max(1., value.norm()),
182 "Finite symmetric search Hessian probes required");
183 for (std::size_t slot = 0; slot < active.size(); ++slot) {
184 const auto j = active[slot];
187 (measured[2 * slot] - measured[2 * slot + 1]) /
189 thirdContractions[j] =
190 .5 * (response + response.transpose());
191 require(thirdContractions[j].allFinite(),
192 "Finite search third contraction required");
197 action, state.path, state.native, state.arcDuration,
198 state.forceGradient, curvature, thirdContractions);
203 constraints.
target - coordinate(state.path));
204 }
catch (
const std::exception& error) {
205 state.failure = error.what();
210 maximum = std::max(maximum, value.norm());
215 std::min(state.arcDuration,
216 period - state.arcDuration) /
218 bool accepted =
false;
220 ++trial, scale *= .5) {
221 auto candidate = state.path;
222 for (std::size_t j = 0; j < n; ++j)
228 if (merit(next, state.arcDuration) <
229 merit(state, state.arcDuration)) {
230 next.acceptedSteps = state.acceptedSteps + 1;
231 state = std::move(next);
233 if (retain) retain(state);
239 "Arc-tail native residual does not decrease along the joint "
244 if (!state.converged && state.failure.empty())
245 state.failure =
"Maximum arc-tail iterations reached";
246 state.evaluations = evaluations;
247 state.batchEvaluations = batches;
248 state.searchHessianEvaluations = hessians;
249 state.physicalHessianActionEvaluations = physicalActions;