模拟退火法求解非线性规划的解

import random
import math

XMAX = 4
YMAX = 4
Tolerance = 0.000001

MarkovLength = 10000
DecayScale = 0.95
StepFactor = 0.02
Temperature = 100
AcceptPoints = 0.0

PreX = -XMAX * random.random()
PreY = - YMAX * random.random()
BestX = PreX;
PreBestX = BestX
BestY = PreY
PreBestY = BestY

Temperature = DecayScale * Temperature

times = 1

def obj_function(x, y):
    return 5.0 * math.sin(x * y) + x * x + y * y

j = 1

while(times < 2 or (math.fabs(obj_function(BestX, BestY) - obj_function(PreBestX, PreBestY)) > Tolerance)):
    j += 1
    print j
    for i in range(MarkovLength):
        NextX = PreX + StepFactor * XMAX * (random.random() - 0.5)
        NextY = PreY + StepFactor * YMAX * (random.random() - 0.5)
        
        while not (NextX >= -XMAX and NextX <= XMAX and NextY >= -YMAX and NextY <= YMAX):
            NextX = PreX + StepFactor * XMAX * (random.random() - 0.5)
            NextY = PreY + StepFactor * YMAX * (random.random() - 0.5)        
        
        if obj_function(BestX, BestY) > obj_function(NextX, NextY):
            PreBestX = BestX
            PreBestY = BestY
            
            BestX = NextX
            BestY = NextY
            
        if obj_function(PreX, PreY) - obj_function(NextX, NextY) > 0:
            PreX = NextX
            PreY = NextY
            AcceptPoints += 1
            
        else:
            change = -1 * (obj_function(NextX, NextY) - obj_function(PreX, PreY)) / Temperature
            if math.exp(change) > random.random():
                PreX = NextX
                PreY = NextY
                AcceptPoints += 1
                
    times += 1


print BestX, BestY, obj_function(BestX, BestY)

 

posted on 2012-12-11 22:44  赵乐ACM 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/dollarzhaole/archive/2012/12/11/2813830.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值