在云上运行重组并行 python 代码的最佳方式

发布于 2025-01-11 16:58:53 字数 1356 浏览 0 评论 0原文

我有一个Python优化程序,它是这样的:

def optimize_score(alpha=0.5, init_point=tuple(np.ones(48)/48),total_paths=5000, iteration_size=0.02,iteration_limit=20):
    best_point=init_point
    point_size=np.size(init_point)
    scores=np.zeros(point_size)
    for i in range(iteration_limit):
        #improve init_point 20 times
        for j in range(point_size):
            #find best directional improvement of previous best point
            current_point=best_point*(1-iteration_size)
            current_point[j]=current_point[j]+iteration_size
            scores[j]=get_score(alpha=alpha, point=current_point,total_paths=total_paths)
        
        best_direction=scores.index(min(scores))
        best_point=best_point*(1-iteration_size)
        best_point[best_direction]=best_point[best_direction]+iteration_size


    
    return best_point

def get_score( alpha=0.5,point=tuple(np.ones(48)/48),total_paths=5000):

    result=np.ones(total_paths)
    for k in range(total_paths):
        result[k]=get_result(path=k, point=point)
    reward=result[0]
    risk=reward-min(result)
    score=risk-alpha*(risk+reward)
    return score

get_result 很长,所以我不会在这里发布它。但本质上它返回一个浮点数,并从一个少于 100,000 行数据的小型数据库中读取

我正在尝试使用云来分割模拟计算,因为模拟在单台计算机上花费的时间太长。我找到了一个使用谷歌云的例子,但教程特别说它无法处理重组并行计算。任何人都有关于如何执行此操作的示例以及完成此类任务的最佳服务是什么?

I have a python optimization program that goes something like this:

def optimize_score(alpha=0.5, init_point=tuple(np.ones(48)/48),total_paths=5000, iteration_size=0.02,iteration_limit=20):
    best_point=init_point
    point_size=np.size(init_point)
    scores=np.zeros(point_size)
    for i in range(iteration_limit):
        #improve init_point 20 times
        for j in range(point_size):
            #find best directional improvement of previous best point
            current_point=best_point*(1-iteration_size)
            current_point[j]=current_point[j]+iteration_size
            scores[j]=get_score(alpha=alpha, point=current_point,total_paths=total_paths)
        
        best_direction=scores.index(min(scores))
        best_point=best_point*(1-iteration_size)
        best_point[best_direction]=best_point[best_direction]+iteration_size


    
    return best_point

def get_score( alpha=0.5,point=tuple(np.ones(48)/48),total_paths=5000):

    result=np.ones(total_paths)
    for k in range(total_paths):
        result[k]=get_result(path=k, point=point)
    reward=result[0]
    risk=reward-min(result)
    score=risk-alpha*(risk+reward)
    return score

get_result is quite long so I won't post it here. But essentially it returns a single floating number and is reading from a small database of less than 100,000 lines of data

I'm trying to split the simulation calculations using the cloud because the simulation takes too long on a single computer. I found an example using google cloud, but the tutorial specifically said it cannot handle recombining parallel calculation. Anyone has an example on how to do this and what's the best service for this kind of task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文