Sleipnir C++ API
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
12
13namespace sleipnir {
14
21template <typename DecisionVariables>
26
41template <typename DecisionVariables>
45 solve,
46 std::span<const DecisionVariables> initialGuesses) {
49
50 for (const auto& initialGuess : initialGuesses) {
51 futures.emplace_back(std::async(std::launch::async, solve, initialGuess));
52 }
53
55 results.reserve(futures.size());
56
57 for (auto& future : futures) {
58 results.emplace_back(future.get());
59 }
60
61 return *std::min_element(
62 results.cbegin(), results.cend(), [](const auto& a, const auto& b) {
63 // Prioritize successful solve
64 if (a.status.exitCondition == SolverExitCondition::kSuccess &&
65 b.status.exitCondition != SolverExitCondition::kSuccess) {
66 return true;
67 }
68
69 // Otherwise prioritize solution with lower cost
70 return a.status.cost < b.status.cost;
71 });
72}
73
74} // namespace sleipnir
Definition FunctionRef.hpp:17
Definition small_vector.hpp:3616
constexpr void reserve(size_type new_capacity)
Definition small_vector.hpp:4082
Definition Expression.hpp:18
IntrusiveSharedPtr< T > AllocateIntrusiveShared(Alloc alloc, Args &&... args)
Definition IntrusiveSharedPtr.hpp:275
MultistartResult< DecisionVariables > Multistart(function_ref< MultistartResult< DecisionVariables >(const DecisionVariables &initialGuess)> solve, std::span< const DecisionVariables > initialGuesses)
Definition Multistart.hpp:42
Definition Multistart.hpp:22
SolverStatus status
Definition Multistart.hpp:23
DecisionVariables variables
Definition Multistart.hpp:24
Definition SolverStatus.hpp:15