20 const Eigen::VectorXd& correction)
22 using M = Eigen::MatrixXd;
23 using V = Eigen::VectorXd;
24 const auto n = state.
gradient.size();
26 correction.size() < 1 || !correction.allFinite())
27 throw std::invalid_argument(
28 "Matching fixed-time arc gradients, links and constraints "
30 const auto d = state.
gradient.front().size(), k = correction.size();
33 for (std::size_t j = 0; j < n; ++j) {
35 normals[j].rows() != d || normals[j].cols() != k ||
36 !normals[j].allFinite())
37 throw std::invalid_argument(
38 "Fixed-time arc metric or constraints differ");
39 normal.middleRows(j * d, d) = normals[j];
40 gradient.segment(j * d, d) = state.
gradient[j];
42 if ((normal.transpose() * normal - M::Identity(k, k)).norm() > 1e-10)
43 throw std::invalid_argument(
44 "Fixed-time arc constraints must be jointly orthonormal");
45 auto apply = [&](
const V& q) {
46 V
out = V::Zero(n * d);
47 for (std::size_t j = 0; j < n; ++j) {
48 const auto next = (j + 1) % n;
50 out.segment(j * d, d) +=
51 link.A * q.segment(j * d, d) - link.L * q.segment(next * d, d);
52 out.segment(next * d, d) +=
53 link.rightEndpoint() * q.segment(next * d, d) -
54 link.L.transpose() * q.segment(j * d, d);
58 const V particular = normal * correction;
59 const M sources = -gradient - apply(particular);
62 const V step = particular + solved.values.col(0);
66 (apply(step) + normal * result.
multipliers + gradient).norm() /
67 std::max(1., gradient.norm());
71 throw std::runtime_error(
72 "Fixed-time arc Newton equations are unresolved");
73 for (std::size_t j = 0; j < n; ++j)
86 const Action& action,
const std::vector<Eigen::VectorXd>& initial,
91 double maximumDisplacement = std::numeric_limits<double>::infinity())
93 using V = Eigen::VectorXd;
94 const auto n = initial.size();
95 const auto k = constraints.
target.size();
96 if (n < 2 || action.arcWeights().size() + 1 != n ||
97 constraints.
normals.size() != n || k < 1 ||
98 !(duration > 0 && duration < action.period()) || !potential.
evaluate ||
100 !(options.forceTolerance > 0 && options.constraintTolerance > 0 &&
101 options.maximumStep > 0) ||
102 !std::isfinite(options.forceTolerance) ||
103 !std::isfinite(options.constraintTolerance) ||
104 !std::isfinite(options.maximumStep) || options.maximumBacktracks < 1 ||
105 !(maximumDisplacement > 0) || std::isnan(maximumDisplacement))
106 throw std::invalid_argument(
107 "Finite fixed-time arc controls and callbacks required");
108 if (options.reflectionEvenPath) {
109 if (options.referenceForceGradient)
110 throw std::invalid_argument(
111 "Reflection projection requires primitive fixed-time action");
113 options.constraintTolerance);
115 const auto d = initial.front().size();
116 Eigen::MatrixXd gram = Eigen::MatrixXd::Zero(k, k);
117 if (d < 1 || !constraints.
target.allFinite())
118 throw std::invalid_argument(
"Finite fixed-time arc metric required");
119 for (std::size_t j = 0; j < n; ++j) {
120 if (initial[j].size() != d || !initial[j].allFinite() ||
121 constraints.
normals[j].rows() != d ||
122 constraints.
normals[j].cols() != k ||
123 !constraints.
normals[j].allFinite())
124 throw std::invalid_argument(
125 "Fixed-time arc node or constraint metric differs");
126 gram += constraints.
normals[j].transpose() * constraints.
normals[j];
128 if ((gram - Eigen::MatrixXd::Identity(k, k)).norm() > 1e-10)
129 throw std::invalid_argument(
130 "Fixed-time arc constraints must be jointly orthonormal");
131 auto coordinate = [&](
const std::vector<V>& q) {
133 for (std::size_t j = 0; j < n; ++j)
134 out += constraints.
normals[j].transpose() * q[j];
137 auto enforce = [&](std::vector<V>& q) {
138 const V delta = constraints.
target - coordinate(q);
139 for (std::size_t j = 0; j < n; ++j)
140 q[j] += constraints.
normals[j] * delta;
143 std::size_t calls = 0, batches = 0, hessians = 0, physicalActions = 0;
144 auto ownedEvaluate = [&](
const std::vector<V>& q) {
150 auto measure = [&](
const std::vector<V>& q) {
153 out.arcDuration = duration;
154 out.native = ownedEvaluate(q);
156 action, q, duration,
out.native, potential, options, ownedEvaluate);
157 physicalActions +=
out.forceGradient.physicalHessianActionRequests;
159 action, q,
out.native, duration,
out.forceGradient);
160 V normal = V::Zero(k);
161 for (std::size_t j = 0; j < n; ++j)
163 constraints.
normals[j].transpose() *
out.evaluation.gradient[j];
164 for (std::size_t j = 0; j < n; ++j) {
165 const double weight = .5 * duration *
166 ((j ? action.arcWeights()[j - 1] : 0) +
167 (j + 1 < n ? action.arcWeights()[j] : 0));
168 out.fullForce = std::max(
169 out.fullForce,
out.evaluation.gradient[j].norm() / weight);
170 out.conditionalForce = std::max(
171 out.conditionalForce,
172 (
out.evaluation.gradient[j] - constraints.
normals[j] * normal)
176 out.energyResidual = std::abs(
out.evaluation.durationGradient);
177 out.constraintResidual = (coordinate(q) - constraints.
target).norm();
178 out.conditionalStationarityQualified =
179 out.conditionalForce <= options.forceTolerance &&
180 out.constraintResidual <= options.constraintTolerance;
181 out.fullStationarityQualified =
out.conditionalStationarityQualified &&
182 out.fullForce <= options.forceTolerance;
183 out.converged = options.requireFullStationarity
184 ?
out.fullStationarityQualified
185 :
out.conditionalStationarityQualified;
186 out.evaluations = calls;
187 out.batchEvaluations = batches;
188 out.searchHessianEvaluations = hessians;
189 out.physicalHessianActionEvaluations = physicalActions;
194 (options.requireFullStationarity ? q.fullForce
195 : q.conditionalForce) /
196 options.forceTolerance,
197 q.constraintResidual / options.constraintTolerance);
199 auto points = initial;
201 const auto center = points;
202 auto displacement = [&](
const std::vector<V>& q) {
204 for (std::size_t j = 0; j < n; ++j)
205 distance = std::max(distance, (q[j] - center[j]).norm());
208 auto restrict = [&](std::vector<V>& q) {
209 const double distance = displacement(q);
210 if (distance > maximumDisplacement) {
211 const double fraction = maximumDisplacement / distance;
212 for (std::size_t j = 0; j < n; ++j)
213 q[j] = center[j] + fraction * (q[j] - center[j]);
216 auto state = measure(points);
217 if (retain) retain(state);
218 for (std::size_t iteration = 0;
219 !state.converged && iteration < options.maximumIterations;
221 if (displacement(state.path) >=
222 maximumDisplacement *
223 (1 - 64 * std::numeric_limits<double>::epsilon())) {
225 "Fixed-time arc proposal reaches its trust boundary";
230 std::vector<Eigen::MatrixXd> third;
231 if (state.forceGradient.enabled) {
232 const double epsilon = options.physicalHessianStep;
233 if (!(epsilon > 0) || !std::isfinite(epsilon))
234 throw std::invalid_argument(
235 "Positive finite compact arc third-contraction step "
237 third.assign(n, Eigen::MatrixXd::Zero(d, d));
238 std::vector<V> probes;
239 std::vector<std::size_t> active;
240 std::vector<double> norms;
241 for (std::size_t j = 0; j < n; ++j) {
243 state.forceGradient.residualGradients[j].norm();
244 if (norm == 0)
continue;
246 epsilon * state.forceGradient.residualGradients[j] / norm;
248 norms.push_back(norm);
249 probes.push_back(state.path[j] + shift);
250 probes.push_back(state.path[j] - shift);
252 if (!probes.empty()) {
254 hessians += probes.size();
255 if (
measured.size() != probes.size())
256 throw std::invalid_argument(
257 "Compact arc search Hessian probe count differs");
258 for (std::size_t i = 0; i < active.size(); ++i) {
259 const Eigen::MatrixXd contraction =
263 (.5 * contraction + .5 * contraction.transpose())
269 action, state.path, state.native, duration, state.forceGradient,
271 Eigen::MatrixXd normal(n * d, k);
272 for (std::size_t j = 0; j < n; ++j)
273 normal.middleRows(j * d, d) = constraints.
normals[j];
275 quadratic.fluctuationSegments,
276 Eigen::MatrixXd::Identity(d, d), normal)
278 if (inertia.negative > 1 && !options.indexOneSearch) {
282 for (std::size_t j = 0; j < n; ++j)
284 std::max(radius, (state.path[j] - constraints.
normals[j] *
287 const double fraction =
288 std::max(.5, 1 - options.maximumStep / radius);
289 auto candidate = state.path;
290 for (std::size_t j = 0; j < n; ++j) {
291 const V origin = constraints.
normals[j] * constraints.
target;
292 candidate[j] = origin + fraction * (candidate[j] - origin);
297 measured.acceptedSteps = state.acceptedSteps + 1;
298 state = std::move(measured);
299 if (retain) retain(state);
304 auto search = quadratic;
305 if (options.indexOneSearch && inertia.negative != 1)
306 search.fluctuationSegments =
311 constraints.
target - coordinate(state.path));
312 }
catch (
const std::exception& error) {
313 state.failure = error.what();
317 for (
const auto& q: step.displacement)
318 maximum = std::max(maximum, q.norm());
320 maximum > options.maximumStep ? options.maximumStep / maximum : 1.;
321 bool accepted =
false;
322 for (std::size_t trial = 0; trial < options.maximumBacktracks;
323 ++trial, scale *= .5) {
324 auto candidate = state.path;
325 for (std::size_t j = 0; j < n; ++j)
326 candidate[j] += scale * step.displacement[j];
330 std::vector<V> displacement;
331 for (std::size_t j = 0; j < n; ++j)
332 displacement.push_back(candidate[j] - state.path[j]);
333 const bool agreement =
334 options.indexOneSearch &&
336 measured.evaluation.action - state.evaluation.action,
338 state.evaluation.action);
339 if (merit(measured) < merit(state) || agreement) {
340 measured.acceptedSteps = state.acceptedSteps + 1;
341 state = std::move(measured);
343 if (retain) retain(state);
349 "Native fixed-time arc residual does not decrease along the "
354 if (!state.converged && state.failure.empty())
355 state.failure =
"Maximum fixed-time arc iterations reached";
356 state.evaluations = calls;
357 state.batchEvaluations = batches;
358 state.searchHessianEvaluations = hessians;
359 state.physicalHessianActionEvaluations = physicalActions;