Wolfe收敛准则MATLAB代码求助

 

求解交通流量分配模型的有效方法 #include "stdafx.h" #include #include #include "os.h" #include "my_types.h" #include "md_alloc.h" #include "my_util.h" #include "message.h" #include "tui.h" #include "meta.h" #include "link_functions.h" #include "od_table.h" #include "od_flow.h" #include "mincostroutes.h" #include "ls_bisect.h" #include "fw_status.h" extern int no_zones, no_nodes, no_links; /* Gloabal variables */ my_float **ODflow, TotalODflow; /* Local Declarations */ /* void FW(void); Should there be a function for fw, or should it be included in main? */ static void Init(char *tuiFileName); static void Close(char *tuiFileName); static void InitODflow(void); static void CloseODflow(void); /* End of local declarations. */ void main(int argc, char **argv ) { my_float *MainVolume, *SubVolume, *SDVolume, Lambda; int **MinPathPredLink; struct fw_status_struct fw_status; char *tuiFileName; StatusMessage("General", "Ready, set, go..."); switch(argc){ case 2: tuiFileName=argv[1]; break; case 1: tuiFileName="control.tui"; break; default: ExitMessage("Wrong number of command line arguments (%d). \n" "Syntax: fw .", argc-1); } Init(tuiFileName); MainVolume = (my_float*)Alloc_1D(no_links, sizeof(my_float) ); SDVolume = SubVolume = (my_float*)Alloc_1D(no_links, sizeof(my_float) ); /* Compute search direction and sub-volume in the same place. */ MinPathPredLink = (int**)Alloc_2D(no_zones,no_nodes, sizeof(int)); InitFWstatus(&fw_status); FindMinCostRoutes (MinPathPredLink, NULL); Assign (ODflow,MinPathPredLink,MainVolume); FirstFWstatus(MainVolume, &fw_status); UpdateLinkCost(MainVolume); for ( fw_status.Iteration = 1; ContinueFW(fw_status); fw_status.Iteration++) { FindMinCostRoutes (MinPathPredLink, NULL); Assign (ODflow,MinPathPredLink,SubVolume); VolumeDifference( SubVolume, MainVolume, SDVolume); /* Which yields the search direction. */ Lambda = LinksSDLineSearch ( MainVolume, SDVolum
Wolfe准则是一种用于求解无约束最优化问题的算法,主要用于确定搜索方向和步长。其实现代码如下: ```matlab function [alpha] = wolfe(x, f, g, p) % 输入: % x:当前点 % f:目标函数 % g:目标函数的梯度 % p:搜索方向 % 输出: % alpha:步长 c1 = 1e-4; c2 = 0.9; alpha = 1; amax = 10; amin = 1e-10; phi = @(alpha) f(x + alpha*p); phiprime = @(alpha) dot(g(x + alpha*p), p); phi0 = f(x); phiprime0 = dot(g(x), p); while true if phi(alpha) > phi0 + c1*alpha*phiprime0 || (phi(alpha) >= phi(alpha/2) && alpha ~= 1) alpha = zoom(x, f, g, p, alpha, alpha/2); break; end if abs(phiprime(alpha)) <= -c2*phiprime0 break; end if phiprime(alpha) >= 0 alpha = zoom(x, f, g, p, alpha/2, alpha); break; end if alpha >= amax alpha = amax; break; end if alpha <= amin alpha = amin; break; end alpha = 0.5*(alpha + amax); end end function [alpha] = zoom(x, f, g, p, alo, ahi) % 输入: % x:当前点 % f:目标函数 % g:目标函数的梯度 % p:搜索方向 % alo:步长下界 % ahi:步长上界 % 输出: % alpha:步长 c1 = 1e-4; c2 = 0.9; phi = @(alpha) f(x + alpha*p); phiprime = @(alpha) dot(g(x + alpha*p), p); while true alpha = 0.5*(alo + ahi); if phi(alpha) > phi(x) + c1*alpha*dot(g(x), p) || phi(alpha) >= phi(alo) ahi = alpha; else if abs(phiprime(alpha)) <= -c2*dot(g(x), p) break; end if phiprime(alpha)*(ahi - alo) >= 0 ahi = alo; end alo = alpha; end end end ``` 其中,`wolfe`函数实现了Wolfe准则的求解过程,`zoom`函数实现了Wolfe准则中的缩减步长过程。在输入参数中,`x`为当前点,`f`为目标函数,`g`为目标函数的梯度,`p`为搜索方向,输出参数`alpha`为步长。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值