Apollo
10.0
自动驾驶开放平台
search.cc
浏览该文件的文档.
1
/******************************************************************************
2
* Copyright 2017 The Apollo Authors. All Rights Reserved.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*****************************************************************************/
16
17
#include "
modules/common/math/search.h
"
18
19
#include <cmath>
20
21
namespace
apollo
{
22
namespace
common {
23
namespace
math {
24
25
double
GoldenSectionSearch
(
const
std::function<
double
(
double
)> &func,
26
const
double
lower_bound,
const
double
upper_bound,
27
const
double
tol) {
28
static
constexpr
double
gr = 1.618033989;
// (sqrt(5) + 1) / 2
29
30
double
a = lower_bound;
31
double
b = upper_bound;
32
33
double
t = (b - a) / gr;
34
double
c = b - t;
35
double
d = a + t;
36
37
while
(std::abs(c - d) > tol) {
38
if
(func(c) < func(d)) {
39
b = d;
40
}
else
{
41
a = c;
42
}
43
t = (b - a) / gr;
44
c = b - t;
45
d = a + t;
46
}
47
return
(a + b) * 0.5;
48
}
49
50
}
// namespace math
51
}
// namespace common
52
}
// namespace apollo
apollo::common::math::GoldenSectionSearch
double GoldenSectionSearch(const std::function< double(double)> &func, const double lower_bound, const double upper_bound, const double tol)
Given a unimodal function defined on the interval, find a value on the interval to minimize the funct...
Definition
search.cc:25
apollo
class register implement
Definition
arena_queue.h:37
search.h
Search-related functions.
modules
common
math
search.cc