Example #1
0
 protected override IResult GetCandidates(int[] qseq, int maxcand)
 {
     var len_qseq = qseq.Length;
     var C = new Dictionary<int,int> ();
     var omega = this.R.DB.Count >> 1;
     // omega *= omega;
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         // Console.WriteLine ("seq: {0}/{1}, class: {2}, count1: {3}", i, len_qseq, this.seqindex, count1);
         for (int s = 1; s <= count1; ++s) {
             var pos = rs.Select1 (s);
             var docid = pos / this.K;
             int dist;
             if (!C.TryGetValue (docid, out dist)) {
                 dist = (short)(len_qseq * omega);
             }
             var diff = Math.Abs (i - (pos % this.K));
             C [docid] = dist + diff * diff - omega;
             //C [docid] = dist + diff - omega;
         }
     }
     //Chronos chronos = new Chronos ();
     //chronos.Begin ();
     var res = new ResultTies (Math.Abs (maxcand));
     foreach (var pair in C) {
         res.Push (pair.Key, (short)Math.Sqrt (pair.Value));
     }
     //chronos.End ();
     //chronos.PrintStats ();
     return res;
 }
Example #2
0
        protected override IResult GetCandidates(int[] qseq, int maxcand)
        {
            int knrbound = this.K;
            var len_qseq = qseq.Length;
            var partial_strings = new Dictionary<int,byte[]> (Math.Abs(maxcand) * 2);
            for (int i = 0; i < len_qseq; ++i) {
                byte mask = (byte)(1 << i);
                var rs = this.SEQ.Unravel (qseq [i]);
                var count1 = rs.Count1;
                for (int s = 1; s <= count1; ++s) {
                    var pos = rs.Select1 (s);
                    var docid = pos / knrbound;
                    var internalpos = pos - docid * knrbound;
                    byte[] useq;
                    if (partial_strings.TryGetValue (docid, out useq)) {
                        // useq [internalpos] = qseq[i] + symshift;
                        useq [internalpos] = mask;
                    } else {
                        useq = new byte[knrbound];
                        partial_strings [docid] = useq;
                        // useq [internalpos] = qseq[i] + symshift;
                        useq [internalpos] = mask;
                    }
                }
            }

            var res = new ResultTies (Math.Abs (maxcand));
            foreach (var p in partial_strings) {
                var useq = p.Value;
                var docID = p.Key;
                var newllcs = BitParallelKnr8LLCS.llcs_diggested_pattern(useq);
                res.Push (docID, -newllcs);
            }
            return res;
        }
Example #3
0
        protected override IResult GetCandidates(int[] qseq, int maxcand)
        {
            var len_qseq = qseq.Length;
            var C = new Dictionary<int,int> ();
            // var omega = this.IndexRefs.MainSpace.Count >> 1;
            var omega = len_qseq << 5;
            // var omega = qseq.Count;
            // var omega = 0;
            for (int i = 0; i < len_qseq; ++i) {
                var rs = this.SEQ.Unravel (qseq [i]);
                var count1 = rs.Count1;
                for (int s = 1; s <= count1; ++s) {
                    var pos = rs.Select1 (s);
                    var docid = pos / this.K;
                    var internal_position = pos - docid * this.K; // pos % this.K
                    int dist;
                    if (!C.TryGetValue (docid, out dist)) {
                        dist = len_qseq * omega;
                    }
                    var d = Math.Abs (i - internal_position);
                    C [docid] = dist + d - omega;
                }
            }
            var res = new ResultTies (Math.Abs (maxcand));
            foreach (var pair in C) {
                res.Push (pair.Key, pair.Value);
            }

            return res;
        }
Example #4
0
 /// <summary>
 /// Gets the candidates. 
 /// </summary>
 protected override IResult GetCandidates(IList<int> qseq, int maxcand)
 {
     // TODO store tsearch as an object property
     ITThresholdAlgorithm tsearch = new NTTArray8 (-1, false);
     // ITThresholdAlgorithm tsearch = new MergeTThreshold ();
     // int maxcand = Math.Abs (this.Maxcand);
     var len_qseq = qseq.Count;
     var lists = new IList<int>[ len_qseq];
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         lists [i] = new SortedListRSCache (rs, -i + len_qseq);
     }
     // lists [len_qseq] = new ListGen<int> ((int i) => i * knrbound, (int)Math.Ceiling(this.seqindex.Count * 1.0 / knrbound));
     IList<int> __C_docs;
     IList<short> __C_sim;
     tsearch.SearchTThreshold (lists, 1, out __C_docs, out __C_sim);
     var res = new ResultTies (Math.Abs (maxcand), false);
     for (int i = 0; i < __C_docs.Count; ++i) {
         var docid = __C_docs [i] - len_qseq;
         docid = docid / this.K;
         var sim = -__C_sim [i];
         res.Push (docid, sim);
     }
     return res;
 }
Example #5
0
 protected override IResult GetCandidates(IList<int> qseq, int maxcand)
 {
     int knrbound = this.K;
     var len_qseq = qseq.Count;
     // var C = new Dictionary<int,short> ();
     var C = new byte[this.DB.Count];
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int s = 1; s <= count1; ++s) {
             var pos = rs.Select1 (s);
             var docid = pos / knrbound;
             // short dist;
             /*if (!C.TryGetValue (docid, out dist)) {
                 dist = 0;
             }
             C [docid] = (short)(dist + 1);
             */
             C [docid] += 1;
         }
     }
     var res = new ResultTies (Math.Abs (maxcand), false);
     /*
     foreach (var pair in C) {
         res.Push (pair.Key, -pair.Value);
     }*/
     for (int i = 0; i < C.Length; ++i) {
         if (C [i] > 0) {
             res.Push (i, -C [i]);
         }
     }
     return res;
 }
Example #6
0
 protected override IResult GetCandidates(int[] qseq, int maxcand)
 {
     int knrbound = this.K;
     var len_qseq = qseq.Length;
     // var C = new Dictionary<int,short> ();
     var C = new byte[this.DB.Count];
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int s = 1; s <= count1; ++s) {
             var pos = rs.Select1 (s);
             var docid = pos / knrbound;
             C [docid] += 1;
         }
     }
     var res = new ResultTies (Math.Abs (maxcand), false);
     for (int i = 0; i < C.Length; ++i) {
         if (C [i] > 0) {
             var useq = this.GetStoredKnr (i);
             var lenlcs = GetLengthLCS (useq, qseq);
     //					var oldlenlcs = this.K * 2 - StringSpace<int>.LCS (useq, qseq);
     //					if (lenlcs != oldlenlcs/2) {
     //						var err =  String.Format("ERROR LOS VALORES DE LEN LCS SON DIFERENTES  seqs: {2} ~ {3}, {0} != {1}",
     //						                         lenlcs, oldlenlcs,
     //						                         String.Join<int> (", ", useq),
     //						                         String.Join<int> (", ", qseq));
     //						throw new ArgumentOutOfRangeException (err);
     //					}
             res.Push (i, -lenlcs);
         }
     }
     return res;
 }
Example #7
0
 /// <summary>
 /// Computes the candidates using a dissimilarity function preserving the order of the
 /// cosine similarity.
 /// </summary>
 protected override IResult GetCandidates(int[] qseq, int maxcand)
 {
     var len_qseq = qseq.Length;
     var C = new Dictionary<int,int> ();
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int s = 1; s <= count1; ++s) {
             var pos = rs.Select1 (s);
             var docID = pos / this.K;
             var internal_pos = pos - docID * this.K;
             int sim;
             if (!C.TryGetValue (docID, out sim)) {
                 sim = 0;
             }
             sim += (1 + len_qseq - i) * (1 + this.K - internal_pos);
             C [docID] = sim;
         }
     }
     var res = new ResultTies (Math.Abs (maxcand));
     foreach (var pair in C) {
         res.Push (pair.Key, -pair.Value);
     }
     return res;
 }
Example #8
0
 protected override IResult GetCandidates(int[] qseq, int maxcand)
 {
     int knrbound = this.K;
     var len_qseq = qseq.Length;
     // the symshift is a simple hack to reserve the zero symbol as wildcard
     // for all those symbols not in the query
     int symshift = 1;
     var partial_strings = new Dictionary<int,int[]> (Math.Abs(maxcand) * 2);
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int s = 1; s <= count1; ++s) {
             var pos = rs.Select1 (s);
             var docid = pos / knrbound;
             var internalpos = pos - docid * knrbound;
             int[] useq;
             if (partial_strings.TryGetValue (docid, out useq)) {
                 useq [internalpos] = qseq[i] + symshift;
             } else {
                 useq = new int[knrbound];
                 partial_strings [docid] = useq;
                 useq [internalpos] = qseq[i] + symshift;
             }
         }
     }
     var res = new ResultTies (Math.Abs (maxcand));
     BitParallelKnr8LLCS bpllcs = new BitParallelKnr8LLCS (qseq, symshift, this.R.DB.Count + symshift);
     foreach (var p in partial_strings) {
         var useq = p.Value;
         var docID = p.Key;
         var newllcs = bpllcs.llcs(useq);
         //Console.WriteLine ("lenlcs: {0}, newllcs: {1}", lenlcs, newllcs);
     //					if (lenlcs != newllcs) {
     //						var err =  String.Format("ERROR LOS VALORES DE LEN LCS SON DIFERENTES  seqs: {2} ~ {3}, {0} != {1}",
     //							lenlcs, newllcs,
     //							String.Join<int> (", ", useq),
     //							String.Join<int> (", ", qseq));
     //						throw new ArgumentOutOfRangeException (err);
     //					}
     //					res.Push (i, -lenlcs);
         res.Push (docID, -newllcs);
     }
     return res;
 }
Example #9
0
        protected override IResult GetCandidates(int[] qseq, int maxcand)
        {
            var len_qseq = qseq.Length;
            var C = new Dictionary<int,int> ();
            // var omega = this.IndexRefs.MainSpace.Count >> 1;
            var omega = len_qseq << 5;
            // var omega = qseq.Count;
            // var omega = 0;
            for (int i = 0; i < len_qseq; ++i) {
                var rs = this.SEQ.Unravel (qseq [i]);
                var count1 = rs.Count1;
                for (int s = 1; s <= count1; ++s) {
                    var pos = rs.Select1 (s);
                    var docid = pos / this.K;
                    int dist;
                    if (!C.TryGetValue (docid, out dist)) {
                        dist = len_qseq * omega;
                    }
                    // var d = (int) Math.Abs (Math.Log (i + 1) - Math.Log (1 + (pos % knrbound)));
                    var d = Math.Abs (i - (pos % this.K));
                    C [docid] = dist + d - omega;
                }
            }
            var res = new ResultTies (Math.Abs (maxcand), false);
            foreach (var pair in C) {
                res.Push (pair.Key, pair.Value);
            }

            return res;
            /*var first = this.GetKnrSeq (0);
            var smaller = this.GetKnrSeq (C_docs [0]);
            Console.WriteLine ("query: {0}, omega: {1}, smaller-docid: {2}", SeqToString (qseq), omega, C_docs [0]);
            Console.WriteLine ("first ==> {0}", DebugSeq (qseq, first, omega, -1));
            Console.WriteLine ("smaller: {0}, computed-dist: {1}", DebugSeq (qseq, smaller, omega, C_sim [0]), C_sim [0]);
            */
        }
Example #10
0
 /// <summary>
 /// Gets the candidates. 
 /// </summary>
 protected virtual IResult GetCandidatesSmallDB(int[] qseq, int maxcand)
 {
     var len_qseq = qseq.Length;
     var n = this.DB.Count;
     var A = new byte[this.DB.Count];
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int j = 1; j <= count1; ++j) {
             var pos = rs.Select1 (j);
             if (pos % this.K == i) {
                 var docid = pos / this.K;
                 if (A [docid] == i) {
                     A [docid] += 1;
                 }
             }
         }
     }
     var res = new ResultTies (Math.Abs (maxcand), false);
     for (int i = 0; i < n; ++i) {
         if (A [i] == 0) {
             continue;
         }
         res.Push (i, -A [i]);
     }
     return res;
 }
Example #11
0
 protected virtual IResult GetCandidates(int[] qseq, int maxcand)
 {
     //			var n = this.DB.Count;
     //			if (n < 500000) {
     //				//return this.GetCandidatesSmallDB (qseq, maxcand);
     //			}
     var len_qseq = qseq.Length;
     var ialg = new BaezaYatesIntersection<int> (new DoublingSearch<int> ());
     IList<int> C = new SortedListRSCache (this.SEQ.Unravel (qseq [0]));
     int i = 1;
     while (i < len_qseq && C.Count > maxcand) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var I = new SortedListRSCache (rs, -i);
         var L = new List<IList<int>> () {C, I};
         var tmp = ialg.Intersection (L);
         ++i;
         if (tmp.Count < maxcand) {
             break;
         }
         C = tmp;
     }
     var res = new ResultTies (int.MaxValue, false);
     foreach (var c in C) {
         if (c % this.K == 0) {
             res.Push (c / this.K, 0);
         }
     }
     return res;
 }
Example #12
0
 public override IResult SearchKNN(object q, int K, IResult res)
 {
     //int maxcand = 1+Math.Abs (this.MAXCAND) / this.Indexes.Length;
     //Console.WriteLine ("MAXCAND: {0}, numindexes: {1}, maxcand-static: {2}", maxcand, this.Indexes.Length, this.MAXCAND);
     var map = new Dictionary<int,double> ();
     foreach (var knrindex in this.Indexes) {
         var seq = knrindex.GetKnr (q);
         foreach (var c in knrindex.SearchKNN (seq, q, null, -this.MAXCAND)) {
             double dist;
             if (map.TryGetValue (c.ObjID, out dist)) {
                 map [c.ObjID] = Math.Min (dist, c.Dist);
             } else {
                 map.Add (c.ObjID, dist);
             }
         }
     }
     var cand = new ResultTies (this.MAXCAND);
     foreach (var p in map) {
         cand.Push (p.Key, p.Value);
     }
     foreach (var p in cand) {
         double d = this.DB.Dist (q, this.DB [p.ObjID]);
         res.Push (p.ObjID, d);
     }
     return res;
 }
Example #13
0
 public virtual void ShowRandomImages(int K)
 {
     var R = new ResultTies (K, true);
     for (int i = 0; i < K; i++) {
         R.Push (Global.Cophir.GetRandomQueryId (), 0);
     }
     Welcome.InnerHtml = String.Format ("Showing {0} random seed images", R.Count);
     ShowImages (R, true, null);
 }
Example #14
0
 protected virtual IResult GetCandidates(int[] qseq, int maxcand)
 {
     var len_qseq = qseq.Length;
     var n = this.DB.Count;
     var A = new Dictionary<int,int>(1024);
     for (int i = 0; i < len_qseq; ++i) {
         var rs = this.SEQ.Unravel (qseq [i]);
         var count1 = rs.Count1;
         for (int j = 1; j <= count1; ++j) {
             var pos = rs.Select1 (j);
             var docid = pos / this.K;
             var internal_pos = pos - docid * this.K;
             if (internal_pos == i) {
                 int value;
                 if (i == 0) {
                     A [docid] = 1;
                 } else if (A.TryGetValue(docid, out value) && value == i) {
                     A [docid] = value + 1;
                 }
             }
         }
     }
     var res = new ResultTies (Math.Abs (maxcand));
     foreach (var p in A) {
         res.Push (p.Key, -p.Value);
     }
     return res;
 }
Example #15
0
        public static ResultTies Search1(IList<byte> qext, Dictionary<int,double> acc, int numqgrams, LSC<IList<byte>> I, double probabilidad)
        {
            Chronos time = new Chronos();
            var aspace = (AudioSpace)I.MainSpace;
            int numsampleq = numqgrams;
            int skip = numqgrams/numsampleq;
            Random r = new Random();

            time.Begin();
            for(int sindex=0; sindex <numsampleq; sindex++){
                int qindex = sindex * skip;
                BinQGram qgram = new BinQGram(qext, qindex * aspace.SymbolSize, aspace.Q);
                IResult R = new Result(int.MaxValue, false);

                if(r.NextDouble() > probabilidad){
                    continue;
                }

                I.KNNSearch(qgram,-1,R);
                HashSet<int> docId = new HashSet<int>();

                foreach (var u in R){
                    docId.Add(aspace.GetDocIdFromBlockId(u.docid));
                }

                foreach (var d in docId){

                    double dist;
                    if(!acc.TryGetValue(d ,out dist)){
                        acc[d] = -1;
                    }
                    else{
                        acc[d]--;
                    }
                }
            }
            time.End();
            //time.PrintStats("Tiempo de busqueda");

            var Rf = new ResultTies(100 , false);
            foreach (var u in acc){
                Rf.Push(u.Key, u.Value);
            }

            return Rf;
        }
Example #16
0
        public static IResult SearchAudio(CommandQuery qname,AudioSpace aspace,HammingMLSC I)
        {
            ResultTies Respuesta = new ResultTies(candidatos,false);
            var Rfull = new ResultTies(100,false);
            List<IResult> res_list = new List<IResult>();
            var qext = BinaryHammingSpace.ParseAndLoadFromFile(qname.QRaw,false);

            foreach(var idx in I.Indexes){
                Console.WriteLine("XXXXXX: {0}",qname.QRaw);

                int numqgrams = (qext.Count - aspace.Q) / aspace.SymbolSize;
                double probabilidad = 0.05;
                var acc = new Dictionary<int, double>();

                Rfull = Search1(qext,acc,numqgrams,idx,probabilidad);
                res_list.Add(Rfull);

                int count=0;
                double panterior = -1;
                foreach(var p in Rfull){

                    if(count > 0){
                        if(p.dist - panterior > 10){
                            break;
                        }
                    }
                    count++;
                    panterior = p.dist;

                }

                if(count < 10){
                    foreach(var p in Rfull){
                        Respuesta.Push(p.docid,p.dist);
                        count--;
                        if(count==0)
                            return Respuesta;
                    }
                }
            }

            return Respuesta;    //Eliminar esto para que todo quede normal

            if(Respuesta.Count==0){
                Dictionary<int,double> rcc = new Dictionary<int,double>();
                foreach(var rl in res_list){
                    double dist;
                    foreach(var respair in rl){
                        if(!rcc.TryGetValue(respair.docid,out dist)){
                            rcc[respair.docid] = -1;
                        }
                        else{
                            rcc[respair.docid]--;
                        }
                    }
                }
                Rfull = new ResultTies(100,false);
                foreach(var pair in rcc){
                    Rfull.Push(pair.Key,pair.Value);
                }

                var RFinal = new Result(100, false);

                foreach (var pair in Rfull){
                    var audio = aspace.GetAudio (pair.docid);
                    var dist = BinaryHammingSpace.DistMinHamming (audio, qext,aspace.SymbolSize);
                    RFinal.Push (pair.docid, dist);
                }

                var error = (qext.Count/aspace.SymbolSize * 24) * perror;
                //Console.WriteLine ("Error: {0}",error);

                foreach(var p in RFinal){
                    if(p.dist < error){
                        Respuesta.Push(p.docid,p.dist);
                    }
                    else{
                        break;
                    }

                }

            }
            return Respuesta;
        }