最佳化演算法-斐波那契數列搜尋

來源:互聯網
上載者:User

標籤:擷取   fun   style   his   import   not   test   區間   等於   

斐波那契數列搜尋,參考Edwin《最佳化導論》第四版7.3章節,演算法採用go語言實現。

/***************************************** * FileName  : fibonacci_search.go * Author    : fredric * Date      : 2017.09.01 * Note      : 斐波那契數列搜尋演算法 * History   :*****************************************/package search import(    "fmt")func _get_fibonacci(i int) int{        if i == 1 {        return 1    }else if i == 2{        return 2    }else{        return _get_fibonacci(i - 1) + _get_fibonacci(i - 2)    }}func _test_func(x float64) float64 {    return x*x*x*x - 14*x*x*x + 60*x*x - 70*x}func _test_func_01(x float64) float64 {    return (x - 1) * (x - 1)}/** 基於黃金分割的思路對分割的比例係數p進行最佳化* p採用斐波那契數列,即* p1 = 1 - FN/FN+1* p2 = 1 - FN-1/FN* ...* PN = 1 - F1/F2* 總的壓縮比:p1*p2*..pN = 1/FN+1* 因此 F N+1 需要能夠滿足壓縮比*/func DoFibonnaciSearch(){    fmt.Println("DoFibonnaciSearch")    //最小區間為0.2    //此時需要斐波那契的壓縮比 1 + 2e/F N + 1 <= 最小區間長度/初始長度    //取e是一個很小的整數,如0.05    //則N等於第五次迭代可以滿足要求    a0 := 0.0    b0 := 2.0    p  := 0.0    delta := 0.05 //最後一次增加一個小整數做位移    for i := 5; i >=1; i-- {        if i != 1 {            fmt.Printf("a0 = %f, b0 = %f \n", a0, b0)            //擷取斐波那契數列            p1 := _get_fibonacci(i)            p0 := _get_fibonacci(i - 1)            p = 1 - float64(p0)/float64(p1)            a1 := a0 + p * (b0 - a0)            b1 := a0 + (1 - p) * (b0 - a0)            f_a1 := _test_func_01(a1)            f_b1 := _test_func_01(b1)            if f_b1 > f_a1 {                b0 = b1            }else{                a0 = a1            }            fmt.Printf("a1 = %f, b1 = %f f_a1 = %f, f_b1 = %f p = %f\n", a1, b1, f_a1, f_b1, p)        }else{            a1 := a0 + (1/2 - delta) * (b0 - a0)            b1 := a0 + (1/2 - delta) * (b0 - a0)            f_a1 := (a1 - 1) * (a1 - 1)            f_b1 := (b1 - 1) * (b1 - 1)            if f_b1 > f_a1 {                b0 = b1            }else{                a0 = a1            }        }    }//for i := 5; i >=1; i-- {    fmt.Printf("a0 = %f, b0 = %f", a0, b0)}

 

最佳化演算法-斐波那契數列搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.