|
| 1 | +#include<stdio.h> |
| 2 | +#include<string.h> |
| 3 | +#include<random> |
| 4 | +#include<string> |
| 5 | +#include<algorithm> |
| 6 | +#include<set> |
| 7 | +#include<random> |
| 8 | +#include<cmath> |
| 9 | + |
| 10 | +std::mt19937 generator(123456789); |
| 11 | +std::normal_distribution<double> dist(0.0,1.0); |
| 12 | +const int max_iter = 100; |
| 13 | + |
| 14 | +double cosine_similarity(const std::vector<double>& u,const std::vector<double>& v){ |
| 15 | + double ip = 0; |
| 16 | + double sumu2 = 0; |
| 17 | + double sumv2 = 0; |
| 18 | + for(int i = 0;i < u.size();++i){ |
| 19 | + ip += u[i] * v[i]; |
| 20 | + sumu2 += u[i] * u[i]; |
| 21 | + sumv2 += v[i] * v[i]; |
| 22 | + } |
| 23 | + return ip / sqrt(sumu2) / sqrt(sumv2); |
| 24 | +} |
| 25 | + |
| 26 | +void dump_func(int cas,int D,int N,int M,double T){ |
| 27 | + printf("dumping case %d\n",cas); |
| 28 | + std::string file_name = "sample" + std::to_string(cas) + ".in"; |
| 29 | + FILE* fp = fopen(file_name.c_str(),"w"); |
| 30 | + fprintf(fp,"%d %d %d %f\n",D,N,M,T); |
| 31 | + std::vector<std::vector<double>> data; |
| 32 | + for(int i = 0;i < N;++i){ |
| 33 | + std::vector<double> tmp; |
| 34 | + for(int j = 0;j < D;++j) |
| 35 | + tmp.push_back(dist(generator)); |
| 36 | + data.push_back(tmp); |
| 37 | + fprintf(fp,"%f",tmp[0]); |
| 38 | + for(int j = 1;j < D;++j) |
| 39 | + fprintf(fp," %f",tmp[j]); |
| 40 | + fprintf(fp,"\n"); |
| 41 | + } |
| 42 | + |
| 43 | + std::uniform_int_distribution<int> distint(0,N - 1); |
| 44 | + for(int i = 0;i < M;++i){ |
| 45 | + double eps = 1e-1; |
| 46 | + bool found = false; |
| 47 | + while(!found){ |
| 48 | + std::normal_distribution<double> disteps(0.0,eps); |
| 49 | + for(int iter = 0;iter < max_iter;++iter){ |
| 50 | + int idx = distint(generator); |
| 51 | + std::vector<double> q = data[idx]; |
| 52 | + for(int j = 0;j < D;++j) |
| 53 | + q[j] += disteps(generator); |
| 54 | + if(cosine_similarity(q,data[idx]) > T){ |
| 55 | + found = true; |
| 56 | + fprintf(fp,"%f",q[0]); |
| 57 | + for(int j = 1;j < D;++j) |
| 58 | + fprintf(fp," %f",q[j]); |
| 59 | + fprintf(fp,"\n"); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + eps /= 2; |
| 64 | + } |
| 65 | + } |
| 66 | + fclose(fp); |
| 67 | +} |
| 68 | + |
| 69 | +int main(){ |
| 70 | + // dump_func(12,128,10000, 10000,0.9); |
| 71 | + // Dimensions, Base Vectors, Queries, Targets |
| 72 | + dump_func(1,2,10,5,0.9); |
| 73 | + dump_func(2,4,50,10,0.9); |
| 74 | + dump_func(3,4,1000,100,0.9); |
| 75 | + // dump_func(3,128,1000000,10000,0.9); |
| 76 | + // dump_func(4,128,1000000,10000,0.95); |
| 77 | + // dump_func(5,256,1000000,10000,0.9); |
| 78 | + // dump_func(6,256,1000000,10000,0.95); |
| 79 | + // dump_func(7,512,500000,10000,0.9); |
| 80 | + // dump_func(8,512,500000,10000,0.95); |
| 81 | + // dump_func(9,512,1000000,10000,0.9); |
| 82 | + // dump_func(10,512,1000000,10000,0.95); |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
0 commit comments