//---------------------------------------------------------------------------- // Copyright (C) 2013-2015 Fabrice HARROUET (ENIB) // // Permission to use, copy, modify, distribute and sell this software // and its documentation for any purpose is hereby granted without fee, // provided that the above copyright notice appear in all copies and // that both that copyright notice and this permission notice appear // in supporting documentation. // The author makes no representations about the suitability of this // software for any purpose. // It is provided "as is" without express or implied warranty. //---------------------------------------------------------------------------- // nb: DTW applies to time series // using it on these random data is a total nonsense // #define USE_DTW using System; using System.Diagnostics; using ParallelRunner; //---------------------------------------------------------------------------- public class Work { public Work(int inputWidth, int tableLength, int threadCount) { _input=new float[inputWidth]; _table=new float[tableLength][]; _threadCount=threadCount; _rand=new Random(); for(int j=0;j<_table.Length;++j) { _table[j]=new float[_input.Length]; for(int i=0;i<_input.Length;++i) { _table[j][i]=(float)_rand.NextDouble(); } } _threadEuclidian=new Result[threadCount]; _euclidian=new Result(); _threadCosine=new Result[threadCount]; _cosine=new Result(); _threadTanimoto=new Result[threadCount]; _tanimoto=new Result(); _threadSorensen=new Result[threadCount]; _sorensen=new Result(); _threadJaccard=new Result[threadCount]; _jaccard=new Result(); #if USE_DTW _threadDtw=new Result[threadCount]; _dtw=new Result(); #endif for(int i=0;i<_threadCount;++i) { _threadEuclidian[i]=new Result(); _threadCosine[i]=new Result(); _threadTanimoto[i]=new Result(); _threadSorensen[i]=new Result(); _threadJaccard[i]=new Result(); #if USE_DTW _threadDtw[i]=new Result(); #endif } } public class Result { public const int SIZE=8; public int[] line; public float[] similarity; public float min; public float max; public Result() { line=new int[SIZE]; similarity=new float[SIZE]; reset(); } public void reset() { for(int i=0;ithis.similarity[i]) { insertAt(i,line,similarity); break; } } } public void insertAt(int index, int line, float similarity) { for(int i=SIZE-1;i>index;--i) { this.line[i]=this.line[i-1]; this.similarity[i]=this.similarity[i-1]; } this.line[index]=line; this.similarity[index]=similarity; min=(float)Math.Min(min,similarity); max=(float)Math.Max(max,similarity); } public void merge(Result other) { int firstPos=0; for(int i=0;isimilarity[pos]) { insertAt(pos,other.line[i],other.similarity[i]); firstPos=pos+1; break; } } if(pos==SIZE) break; // not inserted so next ones will not } min=(float)Math.Min(min,other.min); max=(float)Math.Max(max,other.max); } public override string ToString() { string s=String.Format("[{0:F3}%;{1:F3}%]",100.0f*min,100.0f*max); for(int i=0;i