非線性優化 高翔

2021-10-08 07:04:03 字數 4404 閱讀 8162

高斯牛頓法

#include

#include

#include

#include

#include

using

namespace std;

intmain()

// 開始gauss-newton 迭代

int iterations =

100;

// 迭代次數

double cost =

0, lastcost =0;

// 本次迭代的cost和上一次迭代的cost

chrono::steady_clock::time_point t1 = chrono::steady_clock::

now();

for(

int iter =

0; iter < iterations; iter++

) j in gauss-newton

eigen::matrix3d h = eigen::matrix3d::

zero()

;// bias

eigen::vector3d b = eigen::vector3d::

zero()

; cost =0;

for(

int i =

0; i < n; i++

)// 求解線性方程 hx = b

eigen::vector3d dx = h.

ldlt()

.solve

(b);if(

isnan

(dx[0]

))if(iter >

0&& cost >= lastcost)

ae +

= dx[0]

; be +

= dx[1]

; ce +

= dx[2]

; lastcost = cost;

cout <<

"total cost: "

<< cost <<

", \t\tupdata: "

<< dx.

transpose()

<<

"\t\testimated params: "

<< ae <<

","<< be <<

","<< ce << endl;

} chrono::steady_clock::time_point t2 = chrono::steady_clock::

now();

chrono::duration<

double

> time_used = chrono::duration_cast<:duration>

double

>>

( t2 - t1)

; cout <<

"solve time cost = "

<< time_used.

count()

<<

" seconds. "

<< endl;

cout <<

"estimated abc = "

<< ae <<

" , "

<< be <<

", "

<< ce << endl;

return0;

}

cmake_minimum_required

(version 3.16

)project

(gaussnewton)

set(cmake_cxx_standard 11

)find_package

(opencv required)

include_directories($)

include_directories

("/usr/include/eigen3"

)add_executable

(gaussnewton main.cpp)

target_link_libraries

(gaussnewton $

)

使用ceres進行優化

這個**沒有看明白,日後再來!!!參考部落格

#include

#include

#include

#include

using

namespace std;

// 代價函式的計算模型

struct curve_fitting_cost

//殘差的計算

template

<

typename t>

bool

operator()

(const t *

const abc,

// 模型引數,有3維

t *residual)

const

const

double _x, _y;

// x,y資料};

intmain()

double abc[3]

=;// 構建最小二乘問題

ceres::problem problem;

for(

int i =

0; i < n; i++

)// 配置求解器

ceres::solver::options options;

// 這裡有很多配置項可以填

options.linear_solver_type = ceres::dense_normal_cholesky;

//增量方程如何求解

options.minimizer_progress_to_stdout =

true

;//輸出到cout

ceres::solver::summary summary;

// 優化資訊

chrono::steady_clock::time_point t1 = chrono::steady_clock::

now();

ceres::

solve

(options,

&problem,

&summary)

;// 開始優化

chrono::steady_clock::time_point t2 = chrono::steady_clock::

now();

chrono::duration<

double

> time_used = chrono::duration_cast<:duration>

double

>>

(t2 - t1)

; cout <<

"solve time cost = "

<< time_used.

count()

<<

" seconds. "

<< endl;

// 輸出結果

cout << summary.

briefreport()

<< endl;

cout <<

"estimated a, b, c = "

;for

(auto a:abc) cout << a <<

" ";

cout << endl;

return0;

}

cmake_minimum_required

(version 3.16

)project

(cerescurvefitting)

set(cmake_cxx_standard 11

)find_package

(opencv required)

include_directories($)

find_package

(ceres required)

include_directories($)

include_directories

("/usr/include/eigen3"

)add_executable

(cerescurvefitting main.cpp)

target_link_libraries

(cerescurvefitting $ $

)

日後分析

非線性優化

問題的一般表達 x 是 n 維實向量 x x x t in r n,s是 r n 的子集,f 0 x f m x 是x相關的實值函式。我們所要解決的主要是以下最小問題的變體 min f 0 x s.t.f j x 0,j 1.m,x in s,其中 可以是 geq,leq or f 0 是問題的目標...

非線性優化

目的 因為雜訊!為了減少雜訊帶來的影響。現實世界點p,在相機座標系下座標p x,y,z 落在投影平面上的座標為 u,v 一般把 u,v 寫成齊次座標 所謂齊次座標,是為了方便進行矩陣的運算,在向量上面加乙個維度,一般把增加的維度預設成1 焦距fx,fy,相機座標系和成像平面的平移cx,cy都作為內參...

matlab 非線性優化

求解非線性問題 min z f x s.t.c x 0,ceqx 0,ax b,aeqx beq,lb x ub.x,fval,exitflag,output,lambda,grad,hessian fmincon fun,x0,a,b,aeq,beq,lb,ub,nonlcon,options,p...