genetics.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:HTM_experiments 作者: ctrl-z-9000-times 项目源码 文件源码
def memory_fitness(threshold=2e9, maximum=3e9):
    """
    Returns a penalty for using too much memory.  Add this to your fitness
    function.  This measures the current processes maximum resident set (maxrss)
    which is the all time peak memory usage for the calling process.

    Argument threshold is where the this penalty begins.
    Argument maximum is where this penalty becomes an error (ValueError).

    Returns in the range [0, 1] where 0 is no penalty and 1 is the maximum
            memory usage.  Linear ramp from threshold to maximum.
    """
    rsc = resource.getrusage(resource.RUSAGE_SELF)
    size = rsc.ru_maxrss * 1024
    fit  = (size - threshold) / (maximum - threshold)
    if fit > 1:
        raise ValueError("Individual exceded memory limit (size %d bytes, maximum %d)."%(size, maximum))
    return max(0, fit)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号