参考《 Edwin》最优化导论第四版,7.2章节,算法采用go语言实现
/***************************************** * FileName : golden_section_search.go * Author : fredric * Date : 2017.09.01 * Note : 黄金分割算法 * History : *****************************************/ package search import( "fmt" ) const( GOLDEN_SECTION_VALUE = 0.382 ) /* * 黄金分割法的用途在于求救一元函数的极值,前提要求该函数在区间内有唯一极值,即单峰 * 取作用域[a0,b0] * 每次迭代取[a1, b1],其中a1 - a0 = b0 - b1 = p*(b0 - a0) * 通过判断如果 * : f(b1) > f(a1) ---> 则极值在区间a0 ~ b1之间 * : f(b1) < f(a1) ---> 则极值在区间a1 ~ b0之间 * 迭代继续,直至满足循环条件 * 通过计算可得p = 0.382 */ func DoGoldenSectionSearch(){ //假设方程为法f(x) = (x -1)*(x-1),区间为0 ~ 2 a0 := 0.0 b0 := 2.0 delta := 0.02 count := 0 a1 := 0.0 b1 := 2.0 for b1 - a1 > delta { count++ a1 = a0 + GOLDEN_SECTION_VALUE * (b0 - a0) b1 = a0 + (1 - GOLDEN_SECTION_VALUE) * (b0 - a0) f_a1 := (a1 - 1) * (a1 - 1) f_b1 := (b1 - 1) * (b1 - 1) if f_b1 > f_a1 { b0 = b1 }else{ a0 = a1 } } fmt.Printf("a1 = %f, b1 = %f count=%d", a1, b1, count) }