function [Sigma,S,CostNew] = graphupdate(KH,Sigma,GradNew,CostNew,option) gold = (sqrt(5)+1)/2 ; SigmaNew = SigmaInit= Sigma ; NormGrad = sum(abs(GradNew)); CostOld=CostNew=GradNew/NormGrad; [val,coord] = max(SigmaNew) ; GradNew = GradNew - GradNew(coord); desc = - GradNew.* ( (SigmaNew>0) | (GradNew<0) ); desc(coord) = - sum(desc); stepmin = 0; costmin = CostOld; costmax = 0; ind = find(desc<0); stepmax = min(-(SigmaNew(ind))./desc(ind)); deltmax = stepmax; if isempty(stepmax) || stepmax==0 Sigma = SigmaNew; return end if stepmax > 0.1 stepmax=0.1; end while costmax<costmin [costmax, S] = costgraph(KH,stepmax,desc,SigmaNew); if costmax<costmin costmin = costmax; SigmaNew = SigmaNew + stepmax * desc; desc = desc .* ( (SigmaNew>option.numericalprecision)|(desc>0)); desc(coord) = - sum(desc([[1:coord-1] [coord+1:end]])); ind = find(desc<0); if ~isempty(ind) stepmax = min(-(SigmaNew(ind))./desc(ind)); deltmax = stepmax; costmax = 0; else stepmax = 0; deltmax = 0; end end end Step = [stepmin stepmax]; Cost = [costmin costmax]; [val,coord] = min(Cost); while (stepmax-stepmin)>option.goldensearch_deltmax*(abs(deltmax)) && stepmax > eps stepmedr = stepmin+(stepmax-stepmin)/gold; stepmedl = stepmin+(stepmedr-stepmin)/gold; [costmedr, S1] = costgraph(KH,stepmedr,desc,SigmaNew); [costmedl, S2] = costgraph(KH,stepmedl,desc,SigmaNew); Step = [stepmin stepmedl stepmedr stepmax]; Cost = [costmin costmedl costmedr costmax]; [val,coord] = min(Cost); switch coord case 1 stepmax = stepmedl; costmax = costmedl; S = S2; case 2 stepmax = stepmedr; costmax = costmedr; S = S2; case 3 stepmin = stepmedl; costmin = costmedl; S = S2; case 4 stepmin = stepmedr; costmin = costmedr; S = S1; end end
时间: 2023-12-19 22:07:16 浏览: 114
c代码-求ax平方+bx+c+=0
5星 · 资源好评率100%
这段代码是一个用于更新图的算法,它使用了黄金分割法来找到最小化代价函数的步长。主要输入参数包括KH(图的邻接矩阵)、Sigma(节点的位置坐标)、GradNew(节点的梯度信息)、CostNew(节点代价函数值)和option(算法参数)。输出参数包括Sigma(更新后的节点位置坐标)、S(更新后的图形结构)和CostNew(更新后的节点代价函数值)。
该算法的核心思想是通过调整节点位置坐标来最小化整个图的代价函数。在每次迭代中,算法会计算当前节点位置的梯度信息,并根据梯度信息和当前节点位置坐标来计算出一个方向(desc),然后使用黄金分割法来找到最小化代价函数的步长。最终,通过更新节点位置坐标来更新整个图的结构,并计算出新的代价函数值。
阅读全文