RM算法求解函数(随机近似)

@RM算法求解函数根

问题描述以及出处

RM(Robbins-Monro)算法求解g(w)= w**3 -5 =0的根
学习西湖大学赵世钰老师github网址b站课程视频强化学习的数学原理第6课时,对RM算法求根的算法进行编程浮现,发现一直提示g(w)太大超出可计算范围。

在这里插入图片描述
在这里插入图片描述

编程实现代码

考虑噪声、以及设置更新最大值之后的全部代码

import matplotlib.pyplot as plt
import numpy as np
import math
def g_w(w_k):
    result = math.pow(w_k, 3) - 5
    return result
def g_tilde(w_k,eta):
    result = g_w(w_k) + eta
    return result


def RM_algorithm_improved(w_initial, max_iterations=100, max_value=1000, max_update=10):
    w = w_initial
    ws = [w]  # to store all estimates
    etas = [w]
    for k in range(1, max_iterations + 1):
        eta = np.random.normal(0, 1)  # noise with mean 0 and standard deviation 1
        g_tilde_w = g_tilde(w, eta)
        
        # Update step with a check to prevent too large updates
        # update = alpha_k / k * g_tilde_w
        update = 1 / k * g_tilde_w
        if abs(update) > max_update:  # Limit the update to prevent drastic changes
            update = np.sign(update) * max_update
        
        w = w - update
        '''
        if abs(w) > max_value:  # Bailout if w becomes too large
            print(f"Bailing out at iteration {k} due to large value of w.")
            break
        '''
        etas.append(eta)
        ws.append(w)
    return ws, etas

# 初始化参数,求解函数
w_initial = 0
estimates_improved, etas = RM_algorithm_improved(w_initial)

# Plot the convergence of the RM algorithm with improvements
plt.figure(figsize=(15, 12))
plt.subplot(2,1, 1)
plt.plot(estimates_improved, label='w_k estimates')
plt.axhline(y=5**(1/3), color='r', linestyle='--', label='True root')
plt.xlabel('Iteration')
plt.ylabel('Estimate of w')
plt.title('Convergence of RM Algorithm (Improved)')
plt.legend()
plt.grid(True)

'''画出eta随迭代变化的情况'''
plt.subplot(2,1, 2)
plt.plot(etas, label='eta estimates')
plt.xlabel('Iteration')
plt.ylabel('Eta')
plt.title('noisy of RM Algorithm (Improved)')
plt.legend()
plt.grid(True)
plt.show()  # Show the length to see how many iterations were run before stopping

代码运行结果如下图所示

请添加图片描述

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值