Python双变量搜索算法

时间:2013-03-21 07:07:57

标签: python search

我有一个算法:

  • 取两个输入(x,y),其中x和y是两个独立泊松分布中的'lambda'变量(http://en.wikipedia.org/wiki/Poisson_distribution#Definition
  • 计算x和y中的每一个的泊松向量,
  • 计算两个泊松向量的矩阵乘积
  • 返回[sum(upper_quadrant),sum(lower_quadrant),sum(diagonal)]

所以:

import math, random

def poisson(m, n):
    p=math.exp(-m)
    r=[p]
    for i in range(1, n):
        p*=m/float(i)
        r.append(p)
    return r

def simulate(mx, my, n):
    r=[0.0 for i in range(3)]
    px, py = (poisson(mx, n), 
              poisson(my, n))
    for i in range(n):
        for j in range(n):
            if i > j:
                k=0
            elif i < j:
                k=1
            else:
                k=2
            r[k]+=px[i]*py[j]
    return r

现在我需要在给定一组特定输出的情况下求解x和y。我已经将以下解算器功能混合在一起:

def solve(p, n, generations, decay, tolerance):
    def rms_error(X, Y):
        return (sum([(x-y)**2 
                     for x, y in zip(X, Y)])/float(len(X)))**0.5
    def calc_error(x, y, n, target):
        guess=simulate(x, y, n)
        return rms_error(target, guess)    
    q=[0, 0]
    err=calc_error(math.exp(q[0]), math.exp(q[1]), n, p)
    bestq, besterr = q, err
    for i in range(generations):
        if besterr < tolerance:
            break
        q=list(bestq)
        if random.random() < 0.5:
            j=0
        else:
            j=1
        fac=((generations-i+1)/float(generations))**decay
        q[j]+=random.gauss(0, 1)*fac
        err=calc_error(math.exp(q[0]), math.exp(q[1]), n, p)
        if err < besterr:
            bestq, besterr = q, err
            # print (i, bestq, besterr)
    q, err = [math.exp(q) for q in bestq], besterr
    return (i, q, err)

有效,但似乎需要进行相对大量的尝试才能返回非常优化的响应:

if __name__=="__main__":
    p, n = [0.5, 0.2, 0.3], 10
    q, err = solve_match_odds(p, n, 
                              generations=1000,
                              decay=2,
                              tolerance=1e-5)
    print q
    print simulate_match_odds(q[0], q[1], n)
    print (i, err)

justin@justin-ThinkPad-X220:~/work/$ python solve.py 
[0.5, 0.2, 0.3]
[0.5000246335218251, 0.20006624338256798, 0.29990837191131686]
(999, 6.680993630511076e-05)
justin@justin-ThinkPad-X220:~/work/$

我不是CS专业,我觉得我错过了一系列搜索文献。有人可以建议一个更好的方法来搜索二维空间中的变量吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用scipy.optimize库来解决此问题:

import math, random

def poisson(m, n):
    p=math.exp(-m)
    r=[p]
    for i in range(1, n):
        p*=m/float(i)
        r.append(p)
    return r

def simulate(mx, my, n):
    r=[0.0 for i in range(3)]
    px, py = (poisson(mx, n), 
              poisson(my, n))
    for i in range(n):
        for j in range(n):
            if i > j:
                k=0
            elif i < j:
                k=1
            else:
                k=2
            r[k]+=px[i]*py[j]
    return r

from scipy import optimize

Target, N = [0.5, 0.2, 0.3], 10

def error(p, target, n):
    r = simulate(p[0], p[1], n)
    return np.sum(np.subtract(target, r)**2)

r = optimize.fmin(error, (0, 0), args=(Target, N))
print simulate(r[0], r[1], n)

输出:

Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 67
         Function evaluations: 125
[0.49999812501285623, 0.20000418001288445, 0.2999969464616799]