-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #359.
- Loading branch information
Showing
7 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) Sleipnir contributors | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <future> | ||
#include <span> | ||
#include <vector> | ||
|
||
#include "sleipnir/optimization/SolverStatus.hpp" | ||
#include "sleipnir/util/FunctionRef.hpp" | ||
|
||
namespace sleipnir { | ||
|
||
/** | ||
* The result of a multistart solve. | ||
* | ||
* @tparam DecisionVariables The type containing the decision variable initial | ||
* guess. | ||
*/ | ||
template <typename DecisionVariables> | ||
struct MultistartResult { | ||
SolverStatus status; | ||
DecisionVariables variables; | ||
}; | ||
|
||
/** | ||
* Solves an optimization problem from different starting points in parallel, | ||
* then returns the solution with the lowest cost. | ||
* | ||
* Each solve is performed on a separate thread. Solutions from successful | ||
* solves are always preferred over solutions from unsuccessful solves, and cost | ||
* (lower is better) is the tiebreaker between successful solves. | ||
* | ||
* @tparam DecisionVariables The type containing the decision variable initial | ||
* guess. | ||
* @param solve A user-provided function that takes a decision variable initial | ||
* guess and returns a MultistartResult. | ||
* @param initialGuesses A list of decision variable initial guesses to try. | ||
*/ | ||
template <typename DecisionVariables> | ||
MultistartResult<DecisionVariables> Multistart( | ||
function_ref<MultistartResult<DecisionVariables>(const DecisionVariables&)> | ||
solve, | ||
std::span<const DecisionVariables> initialGuesses) { | ||
std::vector<std::future<MultistartResult<DecisionVariables>>> futures; | ||
for (const auto& initialGuess : initialGuesses) { | ||
futures.emplace_back(std::async(std::launch::async, solve, initialGuess)); | ||
} | ||
|
||
std::vector<MultistartResult<DecisionVariables>> results; | ||
for (auto& future : futures) { | ||
results.emplace_back(future.get()); | ||
} | ||
|
||
return *std::min_element( | ||
results.cbegin(), results.cend(), [](const auto& a, const auto& b) { | ||
// Prioritize successful solve | ||
if (a.status.exitCondition == SolverExitCondition::kSuccess && | ||
b.status.exitCondition != SolverExitCondition::kSuccess) { | ||
return true; | ||
} | ||
|
||
// Otherwise prioritize solution with lower cost | ||
return a.status.cost < b.status.cost; | ||
}); | ||
} | ||
|
||
} // namespace sleipnir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,39 @@ | ||
import concurrent.futures | ||
|
||
from .._jormungandr.optimization import * | ||
|
||
|
||
def multistart(solve, initial_guesses): | ||
""" | ||
Solves an optimization problem from different starting points in parallel, | ||
then returns the solution with the lowest cost. | ||
Each solve is performed on a separate thread. Solutions from successful | ||
solves are always preferred over solutions from unsuccessful solves, and | ||
cost (lower is better) is the tiebreaker between successful solves. | ||
Parameter ``solve``: | ||
A user-provided function that takes a decision variable initial guess | ||
and returns a MultistartResult. | ||
Parameter ``initial_guesses``: | ||
A list of decision variable initial guesses to try. | ||
""" | ||
with concurrent.futures.ThreadPoolExecutor( | ||
max_workers=len(initial_guesses) | ||
) as executor: | ||
futures = [ | ||
executor.submit(solve, initial_guess) for initial_guess in initial_guesses | ||
] | ||
results = [ | ||
future.result() for future in concurrent.futures.as_completed(futures) | ||
] | ||
|
||
# Prioritize successful solve, otherwise prioritize solution with lower cost | ||
return min( | ||
results, | ||
key=lambda x: ( | ||
int(x[0].exit_condition != SolverExitCondition.SUCCESS), | ||
x[0].cost, | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters