注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Bioinformatics home

 
 
 

日志

 
 

simulated annealing (c# 版)  

2011-07-28 12:16:51|  分类: 生物信息编程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
       //使用模拟退火法求函数f(x,y) = 5sin(xy) + x2 + y2的最小值
        /*  解:根据题意,我们设计冷却表进度表为:
          即初始温度为100
          衰减参数为0.95
          马可夫链长度为10000
          Metropolis的步长为0.02
          */

        // 搜索的最大区间
        const double XMAX = 4;
        const double YMAX = 4;

        int MarkovLength = 10000; // 马可夫链长度
        double DecayScale = 0.95; // 衰减参数
        double StepFactor = 0.02; // 步长因子
        double Temperature = 100; // 初始温度
        double Tolerance = 1e-8; // 容差

        double PreX, NextX; // prior and next value of x
        double PreY, NextY; // prior and next value of y
        double PreBestX, PreBestY; // 上一个最优解
        double BestX, BestY; // 最终解
        double AcceptPoints = 0.0; // Metropolis过程中总接受点

        Random rnd = new Random();

        double ObjectFunction(double x, double y)
        {

            double z = 0.0;
            z = 5.0 * Math.Sin(x * y) + x * x + y * y;
            return z;
        }

      private void button1_Click(object sender, EventArgs e)
        {
            // 随机选点
            PreX = -XMAX * rnd.NextDouble();
            PreY = -YMAX * rnd.NextDouble();
            PreBestX = BestX = PreX;
            PreBestY = BestY = PreY;
            int i;

            do
            {

                Temperature *= DecayScale;
                AcceptPoints = 0.0;
                // 在当前温度T下迭代loop(即MARKOV链长度)次
                for (i = 0; i < MarkovLength; i++)
                {
                    do
                    {
                        NextX = PreX + StepFactor * XMAX * (rnd.NextDouble() - 0.5);
                        NextY = PreY + StepFactor * YMAX * (rnd.NextDouble() - 0.5);
                    }
                    while (!(NextX >= -XMAX && NextX <= XMAX && NextY >= -YMAX && NextY <= YMAX));

                    if (ObjectFunction(BestX, BestY) > ObjectFunction(NextX, NextY))
                    {

                        // 保留上一个最优解
                        PreBestX = BestX;
                        PreBestY = BestY;
                        // 此为新的最优解
                        BestX = NextX;
                        BestY = NextY;
                    }


                    // 3) Metropolis过程

                    if (ObjectFunction(PreX, PreY) - ObjectFunction(NextX, NextY) > 0)
                    {

                        // 接受, 此处lastPoint即下一个迭代的点以新接受的点开始
                        PreX = NextX;
                        PreY = NextY;
                        AcceptPoints++;
                    }
                    else
                    {

                        double change = -1 * (ObjectFunction(NextX, NextY) - ObjectFunction(PreX, PreY)) / Temperature;
                        if (Math.Exp(change) > rnd.NextDouble())
                        {

                            PreX = NextX;
                            PreY = NextY;
                            AcceptPoints++;
                        }
                        // 不接受, 保存原解
                    }
                }
            } while( Math.Abs( ObjectFunction( BestX,BestY) - ObjectFunction(PreBestX, PreBestY)) >this.Tolerance);
           textBox1.Text=""+ "最小值在点:"+BestX.ToString()+ ", " + BestY.ToString();
           textBox1.Text=  textBox1.Text+"\r\n最小值为: " + ObjectFunction(BestX, BestY).ToString();
           textBox1.Text = textBox1.Text + "\r\nAcceptPoints: " + AcceptPoints.ToString();
           textBox1.Text = textBox1.Text + "\r\nMakov: " + i.ToString();
        }
  评论这张
 
阅读(732)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017