#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double pi = acos(-1.0);
const double INF = 1e10;
const int Max = 4010;
int minNum = 500;
int maxNum = 2000;
int num = 0;
string rem[Max];
int numtra = 2000;//the number of trajectories
double coe1 = 0.2;
double coe2 = 0.2;
int len = 80;//the length of the subtrajectories
double down_ratio = 0.25;// the ratio of the down sample
double aggravate = 6.0;// the intensity of the interference
int TYPE = 0; //the type of the noisy
double step = 1.3;//the step of the learning
struct point{
double x;
double y;
point(double x1,double y1){
x=x1;
y=y1;
}
point(){};
};
const int LEN = 100000;
vector > trajecs(LEN);
vector > subs(LEN);
int be[LEN];
double mean[Max];
int beginA[Max][Max];
int beginB[Max][Max];
double dpA[Max][Max];
double dpB[Max][Max];
double dp[Max][Max];
int begin[Max][Max];
double dis(const point& p1,const point& p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double dis(const double& a,const double& b)
{
return fabs(a - b);
}
double randomDouble()
{
double den = 2147483647;
return rand()/den;
}
double randNormal(double std)//the mean value is equal to zero;
{
double t1 = randomDouble();
double t2 = randomDouble();
return std*sqrt(-2.0*log(t1))*cos(2.0*pi*t2);
}
double randUniform(double maxmum)
{
double section = maxmum*2;
double int_max = INT_MAX;
int_max/=section;
double ans = rand()/int_max - maxmum;
return ans;
}
int getIntersection(int l1,int r1,int l2,int r2)
{
if(r2& tra,vector& sub,int len,int& be)
{
len = min(int(tra.size()),len);
sub.resize(len);
be = rand()%(tra.size() - len + 1);
for(int i = 0;i < len;i++)
{
sub[i].x = tra[i+be].x ;
sub[i].y = tra[i+be].y ;
}
}
void add_noise(vector& vec,double down_ratio,double mean,int type)
{
int newnum = vec.size() * (1.0 - down_ratio);
int getout = vec.size() - newnum;
vector down;
vector help(vec.size(), 0);
vector rem(vec.size(), 1);
for (int j = 0; j < vec.size(); j++)
{
help[j] = j;
}
for (int j = 0; j < getout; j++)
{
int position = rand() % (vec.size() - j);
rem[help[position]] = 0;
swap(help[position], help[vec.size() - j - 1]);
}
vector now(newnum);
int add = 0;
for (int j = 0; j < vec.size(); j++)
{
if (rem[j])
now[add++] = vec[j];
}
for(int i=0;i& vec,double& mean)
{
int n = vec.size();
mean = 0;
for(int i = 2;i < n;i++)
{
mean += dis(vec[i],vec[i-1]);
}
mean /= (n-1);
}
void getNearestSub2(vector& tra,vector& sub,int& beg,int& end)
{
memset(begin,-1,sizeof begin);
int n=tra.size();
int m=sub.size();
for(int i=0;idp[m-1][i])
{
ans=dp[m-1][i];
beg=begin[m-1][i];
end=i;
}
}
}
void getNearestSub3(vector& tra,vector& sub,int& beg,int& end)
{
double ans = INF;
int n=tra.size();
int m=sub.size();
for(int i = 0;i disnow)
{
ans = disnow;
beg = i;
end = i + m - 1;
}
}
}
void getNearestSub(vector& tra,vector& sub,int& beg,int& end)
{
int n = tra.size();
int m = sub.size();
vector prefix(m+1,0);
for(int i = 1;i < m ;i++)
{
prefix[i] = prefix[i - 1] + dis(sub[i],sub[i - 1]);
}
dpB[0][0] = dis(sub[0],tra[0])*coe1;
beginB[0][0] = 0;
for(int i = 1;i < m;i++)
{
double be = dpA[i-1][0]+dis(sub[i],sub[i-1]);
double af = dpB[i-1][0]+dis(sub[i],tra[0])*coe1;
if(be <= af)
{
dpA[i][0] = be;
beginA[i][0] = beginA[i-1][0];
}
else
{
dpA[i][0] = af;
beginA[i][0] = 0;
}
dpB[i][0] = prefix[i]+dis(sub[i],tra[0])*coe1;
beginB[i][0] = 0;
}
for(int i = 0;i < n;i++)
{
dpA[0][i] = 0;
beginA[0][i] = -1;
dpB[0][i] = dis(sub[0],tra[i])*coe1;
beginB[0][i] = i;
}
for(int i = 1;i < m;i++)
for(int j = 1;j < n;j++)
{
double be = dpA[i - 1][j] + dis(sub[i - 1],sub[i]);
double af = dpB[i - 1][j] + dis(sub[i],tra[j]) * coe1;
if(be<=af)
{
dpA[i][j] = be;
beginA[i][j] = beginA[i - 1][j];
}
else
{
dpA[i][j] = af;
beginA[i][j] = beginB[i - 1][j];
}
be = dpA[i][j - 1]+dis(sub[i],tra[j])*coe1;
af = dpB[i][j - 1]+dis(tra[j],tra[j - 1])*coe1;//ceo2
if(be<=af)
{
dpB[i][j] = be;
beginB[i][j] = beginA[i][j - 1];
if(beginA[i][j - 1] == -1)
{
beginB[i][j] = j;
}
}
else
{
dpB[i][j] = af;
beginB[i][j] = beginB[i][j - 1];
}
}
double ans = prefix[m - 1];
for(int i= 0;i < n;i++)
{
if(dpA[m - 1][i] < ans)
{
ans = dpA[m - 1][i];
beg = beginA[m - 1][i];
end = i;
}
}
}
double getlen(vector& tra)
{
double ans = 0;
for(int i = 2;i vec;
vec.reserve(3000);
point fir(0,0);
bool flag = 1;
while(file.getline(s,1000))
{
sscanf(s,"%lf,%lf%s",&x,&y,help);
point p(x,y);
vec.push_back(p);
if(flag)
{
fir.x = x;
fir.y = y;
flag = 0;
}
vec[vec.size()-1].x = (vec[vec.size()-1].x - fir.x)*1000;
vec[vec.size()-1].y = (vec[vec.size()-1].y - fir.y)*1000;
if(vec.size() > maxNum)
return;
}
if(vec.size() >= minNum && vec.size() <= maxNum)
{
ma=max(ma,int(vec.size()));
swap(trajecs[num],vec);
getTramean(trajecs[num],mean[num]);
num++;
}
}
void get_file()
{
DIR * dir;
struct dirent * ptr;
int i;
string path = "/home/xiefubao/myproject/Data";
char s[10];
for(int i = 1;i < 20;i++)
{
sprintf(s,"/%3d",i);
if(i < 100)
s[1] = '0';
if(i < 10)
s[2] = '0';
string nowpath = path +string(s)+"/Trajectory";
dir = opendir(nowpath.c_str());
if(dir == NULL)
{
cout<< "not exist: " <d_type == 4)
continue;
char filename[100];
sprintf(filename,"/%s", ptr->d_name);
readfile(nowpath + string(filename));
if(num == numtra)
break;
}
closedir(dir);
if(num == numtra)
break;
}
}
double out=0;
double return_ans()
{
double md1 = 0;
double md2 = 0;
for(int i = 0;i=0.01;c-=0.01)
{
double now = return_ans();
if(best_ans=be)
times_of_down = 0;
else
{
times_of_down++;
if(times_of_down == 300)
{
break;
}
}
be = now;
}
cout<=0.01)
{
double sec = (right - left)/3;
double middle1 = left + sec*1;
double middle2 = left + sec*2;
c = middle1;
double ans_middle1 = return_ans();
c = middle2;
double ans_middle2 = return_ans();
if(ans_middle1 sub;
int begin = 0,end = 0 ;
getNearestSub(trajecs[i],subs[i],begin,end);
int inter = getIntersection(be[i],be[i]+len-1,begin,end);
md1 += inter*1.0/len;
md2 += inter*1.0/(end - begin + 1);
//cout<