-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulateAnnealing.cpp
More file actions
53 lines (44 loc) · 907 Bytes
/
Copy pathsimulateAnnealing.cpp
File metadata and controls
53 lines (44 loc) · 907 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
typedef pair<double,double>pdd;
const int N = 101;
int n;
pdd p[N];
double ans=1e8;
double rand(double l,double r){
return (double)rand()/RAND_MAX*(r-l)+l;
}
double getDist(pdd a,pdd b){
double dx=a.first-b.first;
double dy=a.second-b.second;
return sqrt(dx*dx+dy*dy);
}
double sum(pdd randp){
double res=0;
for(int i=0;i<n;i++){
res+=getDist(randp,p[i]);
}
ans=min(ans,res);
return res;
}
void simulate_anneal(){
pdd curp(rand(0,10000),rand(0,10000));
for(double t=1e4;t>1e-4;t*=0.99){
pdd newp(rand(curp.first-t,curp.first+t),rand(curp.second-t,curp.second=t));
double delta=sum(newp)-sum(curp);
if(exp(-delta/t)>rand(0,1)){
curp=newp;
}
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lf%lf",&p[i].first,&p[i].second);
}
for(int i=0;i<100;i++){
simulate_anneal();
}
printf("%lf\n",ans);
return 0;
}