首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >旋转优化器的域?

旋转优化器的域?
EN

Stack Overflow用户
提问于 2014-12-21 20:21:39
回答 1查看 771关注 0票数 0

我很难弄清楚如何在参数优化方法上设置界限(min/max)。旋转是优化算法:https://www.youtube.com/watch?v=2uQ2BSzDvXs。如何确保p(参数)保持在某个域中?下面是我在Python中的Twiddle实现:

代码语言:javascript
复制
def twiddle(objFunction, args, init=0.5, tolerance=0.00001, domain=(0,float("inf"))):
  """Optimize a single parameter given an objective function.

  This is a local hill-climbing algorithm. Here is a simple description of it:
  https://www.youtube.com/watch?v=2uQ2BSzDvXs

  @param args       (tuple)   Arguments necessary for the objective function.

  @param tolerance  (float)   Number used to determine when optimization has
                              converged to a sufficiently good score.

  @param objFunction(function)Objective Function used to quantify how good a
                              particular parameter choice is.

  @param init       (float)   Initial value of the parameter.

  @param domain     (tuple)   Domain of parameter values, as (min, max).

  @return (dict) Contains:
        "parameter" (float)   Threshold that returns the largest score from the
                              Objective function.

        "score"     (float)   The score from the objective function given the
                              threshold.
  """
  pastCalls = {}
  x = init
  delta = 0.1
  bestScore = objFunction(x, args)

  pastCalls[x] = bestScore

  while delta > tolerance: #and x >= domain[0]+tolerance and x <= domain[1]-tolerance:
    x += delta

    if x not in pastCalls:
      score = objFunction(x, args)
      pastCalls[x] = score

    score = pastCalls[x]

    if score > bestScore:
      bestScore = score
      delta *= 2

    else:
      x -= 2*delta

      if x not in pastCalls:
        score = objFunction(x, args)
        pastCalls[x] = score

      score = pastCalls[x]

      if score > bestScore:
        bestScore = score
        delta *= 2
      else:
        x += delta
        delta *= 0.5

    print "Parameter:", x
    print "Best score:", bestScore
    print "delta:", delta
    print

  return {"parameter": x,
          "score": bestScore}

例如,我想以objFunction的形式运行正弦函数,但是域设置为0,2*pi。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-22 23:34:25

明白了!与@AlexMartelli的评论类似,该算法需要防止每次递增/递减到x时超出界限。为了保持爬山算法的核心功能,必须将步长delta调整为当前位置与外部边界之间距离的一半。这样,算法仍然倾向于沿边界方向移动,如果最大值在边界处,则可以到达,必要时还可以从边界移回。代码如下:

代码语言:javascript
复制
def twiddle(objFunction, args, init=0.5, tolerance=0.00001, domain=(float("-inf"), float("inf"))):
  """Optimize a single parameter given an objective function.

  This is a local hill-climbing algorithm. Here is a simple description of it:
  https://www.youtube.com/watch?v=2uQ2BSzDvXs

  @param args       (tuple)   Arguments necessary for the objective function.

  @param tolerance  (float)   Number used to determine when optimization has
                              converged to a sufficiently good score.

  @param objFunction(function)Objective Function used to quantify how good a
                              particular parameter choice is.

  @param init       (float)   Initial value of the parameter.

  @param domain     (tuple)   Domain of parameter values, as (min, max).

  @return (dict) Contains:
        "parameter" (float)   Threshold that returns the largest score from the
                              Objective function.

        "score"     (float)   The score from the objective function given the
                              threshold.
  """
  pastCalls = {}
  x = init
  delta = 0.1
  bestScore = objFunction(x, args)

  pastCalls[x] = bestScore

  while delta > tolerance:

    # Keep x within bounds
    if x+delta > domain[1]:
      delta = abs(domain[1] - x) / 2
    x += delta

    if x not in pastCalls:
      score = objFunction(x, args)
      pastCalls[x] = score

    score = pastCalls[x]

    if score > bestScore:
      bestScore = score
      delta *= 2

    else:
      # Keep x within bounds
      if x-delta < domain[0]:
        delta = abs(domain[0] - x) / 2
      x -= 2*delta

      if x not in pastCalls:
        score = objFunction(x, args)
        pastCalls[x] = score

      score = pastCalls[x]

      if score > bestScore:
        bestScore = score
        delta *= 2
      else:
        x += delta
        delta *= 0.5

    print "Parameter:", x
    print "Best score:", bestScore
    print "Step size:", delta
    print

  return {"parameter": x,
          "score": bestScore}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27593423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档