WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
Multistart.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <algorithm>
6#include <future>
7#include <span>
8
9#include <wpi/SmallVector.h>
10
13
14namespace sleipnir {
15
16/**
17 * The result of a multistart solve.
18 *
19 * @tparam DecisionVariables The type containing the decision variable initial
20 * guess.
21 */
22template <typename DecisionVariables>
25 DecisionVariables variables;
26};
27
28/**
29 * Solves an optimization problem from different starting points in parallel,
30 * then returns the solution with the lowest cost.
31 *
32 * Each solve is performed on a separate thread. Solutions from successful
33 * solves are always preferred over solutions from unsuccessful solves, and cost
34 * (lower is better) is the tiebreaker between successful solves.
35 *
36 * @tparam DecisionVariables The type containing the decision variable initial
37 * guess.
38 * @param solve A user-provided function that takes a decision variable initial
39 * guess and returns a MultistartResult.
40 * @param initialGuesses A list of decision variable initial guesses to try.
41 */
42template <typename DecisionVariables>
45 const DecisionVariables& initialGuess)>
46 solve,
47 std::span<const DecisionVariables> initialGuesses) {
49 futures.reserve(initialGuesses.size());
50
51 for (const auto& initialGuess : initialGuesses) {
52 futures.emplace_back(std::async(std::launch::async, solve, initialGuess));
53 }
54
56 results.reserve(futures.size());
57
58 for (auto& future : futures) {
59 results.emplace_back(future.get());
60 }
61
62 return *std::min_element(
63 results.cbegin(), results.cend(), [](const auto& a, const auto& b) {
64 // Prioritize successful solve
65 if (a.status.exitCondition == SolverExitCondition::kSuccess &&
66 b.status.exitCondition != SolverExitCondition::kSuccess) {
67 return true;
68 }
69
70 // Otherwise prioritize solution with lower cost
71 return a.status.cost < b.status.cost;
72 });
73}
74
75} // namespace sleipnir
This file defines the SmallVector class.
An implementation of std::function_ref, a lightweight non-owning reference to a callable.
Definition FunctionRef.hpp:17
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition SmallVector.h:1212
reference emplace_back(ArgTypes &&... Args)
Definition SmallVector.h:953
void reserve(size_type N)
Definition SmallVector.h:679
size_t size() const
Definition SmallVector.h:99
Definition Hessian.hpp:18
MultistartResult< DecisionVariables > Multistart(function_ref< MultistartResult< DecisionVariables >(const DecisionVariables &initialGuess)> solve, std::span< const DecisionVariables > initialGuesses)
Solves an optimization problem from different starting points in parallel, then returns the solution ...
Definition Multistart.hpp:43
The result of a multistart solve.
Definition Multistart.hpp:23
SolverStatus status
Definition Multistart.hpp:24
DecisionVariables variables
Definition Multistart.hpp:25
Return value of OptimizationProblem::Solve() containing the cost function and constraint types and so...
Definition SolverStatus.hpp:15