POJ 1379 模拟退火算法

求规定平面上一点到已知点的最小距离最大的点。

模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换。

进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<ctime>
using namespace std;
#define eps 1e-2
double pi=acos(-1.0);
double x[10004],y[10004];
double qx[10004],qy[10004];
double X,Y;
int n;
const int num=15;   //初始解空间大小
const int numf=30;  //扩展解空间大小
double dist(double x,double y,double xx,double yy)
{
    return sqrt((x-xx)*(x-xx)+(y-yy)*(y-yy));
}
double get(double xx,double yy)
{
    double mins=1111111111;
    for(int i=1;i<=n;i++)
        mins=min(mins,dist(xx,yy,x[i],y[i]));
    return mins;
}
double getr()   //返回[0,1]的浮点数
{
    return (rand()%10000+1.0)/10000.0;
}
int main()
{
    srand(time(NULL));  //多次提交= =防RP差
    int ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%lf%lf%d",&X,&Y,&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&x[i],&y[i]);
        }
        for(int i=1;i<=num;i++) //构造初始解空间
        {
            qx[i]=X*getr();
            qy[i]=Y*getr();
        }
        double dd=max(Y,X);     //最大温度
        while(dd>eps)
        {
            for(int i=1;i<=num;i++)
            {
                double now=get(qx[i],qy[i]),t;
                for(int j=1;j<=numf;j++)
                {
                    double dt=getr()*2*pi;      //随机角度
                    double tmpx=qx[i]+dd*cos(dt);
                    double tmpy=qy[i]+dd*sin(dt);
                    if(tmpx<0||tmpx>X||tmpy<0||tmpy>Y) continue;
                    if((t=get(tmpx,tmpy))>now) //扩展新解
                    {
                        qx[i]=tmpx;
                        qy[i]=tmpy;
                        now=t;
                    }
                }
            }
            dd*=0.9;            //冷却速度
        }
        double mins=0;
        int k;
        for(int i=1;i<=num;i++)
        {
            double tmp=get(qx[i],qy[i]);
            if(tmp>mins) {mins=tmp;k=i;}
        }
        printf("The safest point is (%.1lf, %.1lf).\n",qx[k],qy[k]);
    }
    return 0;
}
/*
3
1000 50 1
10 10
100 100 4
10 10
10 90
90 10
90 90
3000 3000 4
1200 85
63 2500
2700 2650
2990 100
*/

POJ 1379 模拟退火算法,布布扣,bubuko.com

时间: 2024-12-22 22:00:43

POJ 1379 模拟退火算法的相关文章

POJ 1379 模拟退火

模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举,那就模拟退火. #include <stdio.h> #include <math.h> #include <algorithm> using namespace std; const int maxn = 1005; int X,Y,M; int Kase; struct

POJ 1379 模拟退火法

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5631   Accepted: 1728 Description One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look complet

PKU 1379 Run Away(模拟退火算法)

题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用模拟退火算法解决. 不过个人感觉这个算法的实现过程中仍然采用了随机化算法.二者均属于概率算法.  参考链接 Point Goto_Rand_Dir(double key,Point temp)函数中,Point temp必须得定义在参数中,不能定义在函数内部, 否则temp没有初始值,无法进行后面的

POJ 1379

模拟退火算法. 随机MAX个点,然后,退火移动,选取距离所有点中最短中最长者即可.理解和我上一篇一样. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> using namespace std; const int MAXN=1010; const int MA

POJ 2420 模拟退火

链接: http://poj.org/problem?id=2420 题意: 给出n个点,找到一个点,使得它到所有的点的距离最小. 题解: 最近要做一个排课系统,需要用到模拟退火算法,之前虽然了解过这个算法,但是没有写过题.就先在POJ上找了一道学习一下. 代码: 1 #include <iomanip> 2 struct Point { double x, y; }; 3 4 const double eps = 1e-8; //搜索条件阀值 5 const double T = 100;

poj-2420 A Star not a Tree?(模拟退火算法)

题目链接: A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5219   Accepted: 2491 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allo

两种改进的模拟退火算法求解大值域约束满足问题1.0

0引言 约束满足问题(Constraint Satisfaction Problem,CSP)是人工智能研究领域中一个非常重要的分支,现已成为理论计算机科学.数学和统计物理学等交叉学科研究中的热点问题.人工智能.计算机科学和自动控制等领域中的许多问题都可以归结为约束满足问题.同时,约束满足问题在实际问题如模式识别.决策支持.物流调度及资源分配等领域也有着非常广泛的应用. CSP由一个变量集合和一个约束集合组成.每个变量都有一个非空的可能值域,每个约束描述了一个变量子集与子集内各变量的相容赋值,所

Matlab随笔之模拟退火算法

问题描述: 我方有一个基地,经度和纬度为( 70,40).假设我方飞机的速度为 1000 公里/小时. 我方派一架飞机从基地出发,侦察完敌方所有目标,再返回原来的基地.在敌方每一目 标点的侦察时间不计,求该架飞机所花费的时间(假设我方飞机巡航时间可以充分长). 这是一个旅行商问题.我们依次给基地编号为 1,敌方目标依次编号为 2, 3,…, 101, 最后我方基地再重复编号为 102(这样便于程序中计算). 距离矩阵 D = ( dij )102×102 , 其中 dij 表示表示 i, j 两

大白话解析模拟退火算法(转)

优化算法入门系列文章目录(更新中): 1. 模拟退火算法 2. 遗传算法 一. 爬山算法 ( Hill Climbing ) 介绍模拟退火前,先介绍爬山算法.爬山算法是一种简单的贪心搜索算法,该算法每次从当前解的临近解空间中选择一个最优解作为当前解,直到达到一个局部最优解. 爬山算法实现很简单,其主要缺点是会陷入局部最优解,而不一定能搜索到全局最优解.如图1所示:假设C点为当前解,爬山算法搜索到A点这个局部最优解就会停止搜索,因为在A点无论向那个方向小幅度移动都不能得到更优的解. 图1 二. 模