我很难弄清楚如何在参数优化方法上设置界限(min/max)。旋转是优化算法:https://www.youtube.com/watch?v=2uQ2BSzDvXs。如何确保p(参数)保持在某个域中?下面是我在Python中的Twiddle实现:
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。
发布于 2014-12-22 23:34:25
明白了!与@AlexMartelli的评论类似,该算法需要防止每次递增/递减到x时超出界限。为了保持爬山算法的核心功能,必须将步长delta调整为当前位置与外部边界之间距离的一半。这样,算法仍然倾向于沿边界方向移动,如果最大值在边界处,则可以到达,必要时还可以从边界移回。代码如下:
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}https://stackoverflow.com/questions/27593423
复制相似问题