// TODO: exceptions public Vector2D[] Update(int numDequeue, IEnumerable <SparseVector <double> > newInst, bool test, LayoutSettings settings, ref PtInfo[] ptInfo, int _count) { // clustering mLogger.Info("Update", "Clustering ..."); /*prof*/ StopWatch sw = new StopWatch(); mKMeans.Eps = mKMeansEps; int iter = 0; mKMeans.Update(numDequeue, newInst, ref iter); /*prof*/ sw.Save("cl.txt", _count, iter.ToString()); // determine reference instances /*prof*/ sw.Reset(); UnlabeledDataset <SparseVector <double> > dsRefInst = new UnlabeledDataset <SparseVector <double> >(); UnlabeledDataset <SparseVector <double> > dsNewInst = new UnlabeledDataset <SparseVector <double> >(newInst); foreach (SparseVector <double> centroid in mKMeans.GetCentroids()) { dsRefInst.Add(centroid); // dataset of reference instances dsNewInst.Add(centroid); // dataset of new instances } // position reference instances mLogger.Info("Update", "Positioning reference instances ..."); SparseMatrix <double> simMtx = ModelUtils.GetDotProductSimilarity(dsRefInst, mSimThresh, /*fullMatrix=*/ false); StressMajorizationLayout sm = new StressMajorizationLayout(dsRefInst.Count, new DistFunc(simMtx)); sm.Random = mRandom; sm.MaxSteps = int.MaxValue; sm.MinDiff = 1E-3; mRefPos = sm.ComputeLayout(/*settings=*/ null, mRefPos /*make this a property!!!*/); /*prof*/ sw.Save("sm.txt", _count); // k-NN /*prof*/ sw.Reset(); DateTime t = DateTime.Now; mLogger.Info("Update", "Computing similarities ..."); // update list of neighborhoods mPatches.RemoveRange(mDataset.Count - mKClust, mKClust); mPatches.RemoveRange(0, numDequeue); // remove instances from [dataset and] neighborhoods foreach (Patch patch in mPatches) { if (patch.Min != null && (patch.Min.Idx < numDequeue || patch.Max.Idx >= mDataset.Count - mKClust)) { int oldCount = patch.List.Count; ArrayList <KeyDat <double, Patch> > tmp = new ArrayList <KeyDat <double, Patch> >(); foreach (KeyDat <double, Patch> item in patch.List) { if (item.Dat.Idx >= numDequeue && item.Dat.Idx < mDataset.Count - mKClust) { tmp.Add(item); } //else //{ // Console.WriteLine("Remove {0}", item.Dat.Idx - numDequeue); //} } patch.List = tmp; patch.ProcessList(); patch.NeedUpdate = patch.List.Count < mKNn && oldCount >= mKNn; } } // update dataset mDataset.RemoveRange(mDataset.Count - mKClust, mKClust); mDataset.RemoveRange(0, numDequeue); // add new instances to dataset int preAddCount = mDataset.Count; mDataset.AddRange(dsNewInst); // precompute transposed matrices SparseMatrix <double> trNewInst = ModelUtils.GetTransposedMatrix(dsNewInst); SparseMatrix <double> trDataset = ModelUtils.GetTransposedMatrix(mDataset); // add new instances to neighborhoods for (int i = 0; i < dsNewInst.Count; i++) { mPatches.Add(new Patch(-1)); mPatches.Last.NeedUpdate = true; } for (int i = 0; i < mPatches.Count; i++) { mPatches[i].Idx = i; } for (int i = 0; i < mPatches.Count; i++) { Patch patch = mPatches[i]; SparseVector <double> vec = mDataset[i]; if (vec != null) { if (patch.NeedUpdate) // full update required { //if (i == 1347) { Console.WriteLine("full update"); } SparseVector <double> simVec = ModelUtils.GetDotProductSimilarity(trDataset, mDataset.Count, vec, mSimThresh); ArrayList <KeyDat <double, int> > tmp = new ArrayList <KeyDat <double, int> >(); foreach (IdxDat <double> item in simVec) { if (item.Idx != i) { tmp.Add(new KeyDat <double, int>(item.Dat, item.Idx)); } } tmp.Sort(new Comparer2()); int count = Math.Min(tmp.Count, mKNnExt); patch.List.Clear(); for (int j = 0; j < count; j++) { patch.List.Add(new KeyDat <double, Patch>(tmp[j].Key, mPatches[tmp[j].Dat])); } patch.ProcessList(); patch.NeedUpdate = false; } else // only new instances need to be considered { //if (i == 1347) { Console.WriteLine("partial update"); } SparseVector <double> simVec = ModelUtils.GetDotProductSimilarity(trNewInst, dsNewInst.Count, vec, mSimThresh); // check if further processing is needed bool needMerge = false; if (test) { foreach (IdxDat <double> item in simVec) { if (item.Dat >= patch.MinSim) { needMerge = true; //Console.WriteLine("{0} {1}", item.Dat, patch.MinSim); break; } } } else { foreach (IdxDat <double> item in simVec) { if (item.Dat > patch.MinSim) { needMerge = true; //Console.WriteLine("{0} {1}", item.Dat, patch.MinSim); break; } } } if (needMerge || patch.List.Count < mKNn) { //if (i == 1347) { Console.WriteLine("merge"); } int oldCount = patch.List.Count; ArrayList <KeyDat <double, Patch> > tmp = new ArrayList <KeyDat <double, Patch> >(); foreach (IdxDat <double> item in simVec) { tmp.Add(new KeyDat <double, Patch>(item.Dat, mPatches[item.Idx + preAddCount])); } // merge the two lists // TODO: speed this up patch.List.AddRange(tmp); patch.List.Sort(new Comparer()); // trim list to size if (oldCount >= mKNn) { patch.List.RemoveRange(oldCount, patch.List.Count - oldCount); } patch.ProcessList(); } } } } /*prof*/ sw.Save("knn.txt", _count); // *** Test *** sw.Reset(); ModelUtils.GetDotProductSimilarity(mDataset, mSimThresh, /*fullMatrix=*/ true); sw.Save("selfSim.txt", _count, mDataset.Count.ToString()); if (test) { simMtx = ModelUtils.GetDotProductSimilarity(mDataset, mSimThresh, /*fullMatrix=*/ true); ArrayList <Patch> patches = new ArrayList <Patch>(); for (int i = 0; i < mDataset.Count; i++) { patches.Add(new Patch(i)); } foreach (IdxDat <SparseVector <double> > simMtxRow in simMtx) { if (simMtxRow.Dat.Count <= 1) { mLogger.Warn("Update", "Instance #{0} has no neighborhood.", simMtxRow.Idx); } ArrayList <KeyDat <double, int> > knn = new ArrayList <KeyDat <double, int> >(simMtxRow.Dat.Count); foreach (IdxDat <double> item in simMtxRow.Dat) { if (item.Idx != simMtxRow.Idx) { knn.Add(new KeyDat <double, int>(item.Dat, item.Idx)); } } knn.Sort(new Comparer2()); int count = Math.Min(knn.Count, mKNnExt); for (int i = 0; i < count; i++) { patches[simMtxRow.Idx].List.Add(new KeyDat <double, Patch>(knn[i].Key, patches[knn[i].Dat])); } patches[simMtxRow.Idx].ProcessList(); } // compare if (patches.Count != mPatches.Count) { throw new Exception("Count mismatch."); } for (int i = 0; i < mPatches.Count; i++) { if (patches[i].List.Count < mKNn && patches[i].List.Count != mPatches[i].List.Count) { Console.WriteLine(mPatches[i].List.Count); Console.WriteLine(patches[i].List.Count); Output(mPatches[i].List); Output(patches[i].List); Console.WriteLine(i); throw new Exception("List count mismatch."); } int count = Math.Min(mPatches[i].List.Count, mKNn); for (int j = 0; j < count; j++) { //Console.WriteLine("{4} {0}-{1} {2}-{3}", mPatches[i].List[j].Key, mPatches[i].List[j].Dat.Idx, patches[i].List[j].Key, patches[i].List[j].Dat.Idx, i); if (mPatches[i].List[j].Key != patches[i].List[j].Key || mPatches[i].List[j].Dat.Idx != patches[i].List[j].Dat.Idx) { Console.WriteLine("i:{4} fast:{0}-{1} slow:{2}-{3}", mPatches[i].List[j].Key, mPatches[i].List[j].Dat.Idx, patches[i].List[j].Key, patches[i].List[j].Dat.Idx, i); int idxFast = mPatches[i].List[j].Dat.Idx; int idxSlow = patches[i].List[j].Dat.Idx; Console.WriteLine("slow @ fast idx: {0}", GetKey(patches[i].List, idxFast)); Console.WriteLine("fast @ slow idx: {0}", GetKey(mPatches[i].List, idxSlow)); throw new Exception("Patch item mismatch."); } } } } // *** End of test *** //Console.WriteLine("Number of patches: {0}", mPatches.Count); //int waka = 0; //foreach (Patch patch in mPatches) //{ // waka += patch.List.Count; //} //Console.WriteLine("Avg list size: {0}", (double)waka / (double)mPatches.Count); Console.WriteLine((DateTime.Now - t).TotalMilliseconds); /*prof*/ sw.Reset(); mLogger.Info("Update", "Constructing system of linear equations ..."); LabeledDataset <double, SparseVector <double> > lsqrDs = new LabeledDataset <double, SparseVector <double> >(); Vector2D[] layout = new Vector2D[mDataset.Count - mKClust]; foreach (Patch patch in mPatches) { int count = Math.Min(patch.List.Count, mKNn); SparseVector <double> eq = new SparseVector <double>(); double wgt = 1.0 / (double)count; for (int i = 0; i < count; i++) { eq.InnerIdx.Add(patch.List[i].Dat.Idx); eq.InnerDat.Add(-wgt); } eq.InnerIdx.Sort(); // *** sort only indices eq[patch.Idx] = 1; lsqrDs.Add(0, eq); } for (int i = mDataset.Count - mKClust, j = 0; i < mDataset.Count; i++, j++) { SparseVector <double> eq = new SparseVector <double>(new IdxDat <double>[] { new IdxDat <double>(i, 1) }); lsqrDs.Add(mRefPos[j].X, eq); } LSqrModel lsqr = new LSqrModel(); mSolX.RemoveRange(0, numDequeue); double[] aux = new double[mKClust]; mSolX.CopyTo(mSolX.Count - mKClust, aux, 0, mKClust); mSolX.RemoveRange(mSolX.Count - mKClust, mKClust); foreach (SparseVector <double> newVec in newInst) { mSolX.Add(0); } mSolX.AddRange(aux); lsqr.InitialSolution = mSolX.ToArray(); lsqr.Train(lsqrDs); mSolX = lsqr.Solution.GetWritableCopy(); //for (int i = 0; i < lsqr.InitialSolution.Length; i++) //{ // Console.WriteLine("{0}\t{1}", lsqr.InitialSolution[i], lsqr.Solution[i]); //} for (int i = 0; i < layout.Length; i++) { layout[i].X = lsqr.Solution[i]; } for (int i = lsqrDs.Count - mKClust, j = 0; i < lsqrDs.Count; i++, j++) { lsqrDs[i].Label = mRefPos[j].Y; } mSolY.RemoveRange(0, numDequeue); aux = new double[mKClust]; mSolY.CopyTo(mSolY.Count - mKClust, aux, 0, mKClust); mSolY.RemoveRange(mSolY.Count - mKClust, mKClust); foreach (SparseVector <double> newVec in newInst) { mSolY.Add(0); } mSolY.AddRange(aux); lsqr.InitialSolution = mSolY.ToArray(); lsqr.Train(lsqrDs); mSolY = lsqr.Solution.GetWritableCopy(); for (int i = 0; i < layout.Length; i++) { layout[i].Y = lsqr.Solution[i]; } /*prof*/ sw.Save("lsqr.txt", _count); // ----------------------------------------------------------------- // make ptInfo // ----------------------------------------------------------------- ptInfo = new PtInfo[layout.Length]; int ii = 0; foreach (Vector2D pt in layout) { ptInfo[ii] = new PtInfo(); ptInfo[ii].X = pt.X; ptInfo[ii].Y = pt.Y; ptInfo[ii].Vec = mDataset[ii]; ii++; } // ----------------------------------------------------------------- return(settings == null ? layout : settings.AdjustLayout(layout)); }
public string addPt(PtInfo c_pt) { string succ = ""; string connectionString = this.Connect(); SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO pt_Info (reg_number,xtype,title_of_invention,pt_desc,spec_doc,loa_no,loa_doc,claim_no,claim_doc,pct_no,pct_doc,doa_no,doa_doc,log_staff,reg_date,xvisible) VALUES (@reg_number,@xtype,@title_of_invention,@pt_desc,@spec_doc,@loa_no,@loa_doc,@claim_no,@claim_doc,@pct_no,@pct_doc,@doa_no,@doa_doc,@log_staff,@reg_date,@xvisible) SELECT SCOPE_IDENTITY()"; connection.Open(); command.Parameters.Add("@reg_number", SqlDbType.NVarChar, 50); command.Parameters.Add("@xtype", SqlDbType.NVarChar); command.Parameters.Add("@title_of_invention", SqlDbType.NVarChar); command.Parameters.Add("@pt_desc", SqlDbType.Text); command.Parameters.Add("@spec_doc", SqlDbType.Text); command.Parameters.Add("@loa_no", SqlDbType.NVarChar, 20); command.Parameters.Add("@loa_doc", SqlDbType.Text); command.Parameters.Add("@claim_no", SqlDbType.NVarChar, 20); command.Parameters.Add("@claim_doc", SqlDbType.Text); command.Parameters.Add("@pct_no", SqlDbType.NVarChar, 20); command.Parameters.Add("@pct_doc", SqlDbType.Text); command.Parameters.Add("@doa_no", SqlDbType.NVarChar, 20); command.Parameters.Add("@doa_doc", SqlDbType.Text); command.Parameters.Add("@log_staff", SqlDbType.NVarChar, 50); command.Parameters.Add("@reg_date", SqlDbType.NVarChar, 50); command.Parameters.Add("@xvisible", SqlDbType.NVarChar, 10); command.Parameters["@reg_number"].Value = c_pt.reg_number; command.Parameters["@xtype"].Value = c_pt.xtype; command.Parameters["@title_of_invention"].Value = ConvertApos2Tab(c_pt.title_of_invention); command.Parameters["@pt_desc"].Value = ConvertApos2Tab(c_pt.pt_desc); command.Parameters["@spec_doc"].Value = ""; command.Parameters["@loa_no"].Value = c_pt.loa_no; command.Parameters["@loa_doc"].Value = ""; command.Parameters["@claim_no"].Value = c_pt.claim_no; command.Parameters["@claim_doc"].Value = ""; command.Parameters["@pct_no"].Value = c_pt.pct_no; command.Parameters["@pct_doc"].Value = ""; command.Parameters["@doa_no"].Value = c_pt.doa_no; command.Parameters["@doa_doc"].Value = ""; command.Parameters["@log_staff"].Value = c_pt.log_staff; command.Parameters["@reg_date"].Value = c_pt.reg_date; command.Parameters["@xvisible"].Value = c_pt.xvisible; succ = command.ExecuteScalar().ToString(); connection.Close(); return succ; }
public string addNewPatent(List<Applicant> lt_app, List<pt.Priority_info> lt_pri, List<pt.Inventor> lt_inv, PtInfo c_pt, pt.Assignment_info c_assinfo, Representative c_rep) { string xID = ""; foreach (Applicant c_app in lt_app) { if ((c_app.xname != null) && (c_app.xname != "")) { this.addApplicant(c_app); } } foreach (Priority_info c_pri in lt_pri) { if ((c_pri.app_no != null) && (c_pri.app_no != "")) { this.addPriority_info(c_pri); } } foreach (Inventor c_inv in lt_inv) { if ((c_inv.xname != null) && (c_inv.xname != "")) { this.addInventor(c_inv); } } if ((c_assinfo.assignee_name != null) && (c_assinfo.assignee_name != "") && (c_assinfo.date_of_assignment != null) && (c_assinfo.date_of_assignment != "") && (c_assinfo.ID != null) && (c_assinfo.ID != "")) { this.addAssignment_info(c_assinfo); } xID = this.addPt(c_pt); this.updatePtReg(xID, c_pt.xtype); this.addRepresentative(c_rep); this.updatePwalletStatus(c_pt.log_staff, "0"); return xID; }
public string updatePtInfo(PtInfo x) { string connectionString = this.Connect(); string str2 = ""; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = connection.CreateCommand(); command.CommandText = "UPDATE [dbo].[pt_info] SET [reg_number] ='" + x.reg_number + "',[xtype] = '" + x.xtype + "',[title_of_invention] = '" + ConvertApos2Tab(x.title_of_invention) + "', "; command.CommandText += " [pt_desc] = '" + ConvertApos2Tab(x.pt_desc) + "',[reg_date] = '" + x.reg_date + "', "; command.CommandText += " [log_staff] = '" + x.log_staff + "',[xvisible] = '" + x.xvisible + "' WHERE xID ='" + x.xID + "' "; connection.Open(); str2 = command.ExecuteNonQuery().ToString(); connection.Close(); return str2; }
public List<PtInfo> getSearchPtInfoRS(string kword, List<string> fulltext, string cri) { List<PtInfo> list = new List<PtInfo>(); new PtInfo(); string cmdText = ""; string str2 = ""; string str3 = ""; string str4 = ""; int num = 0; SqlConnection connection = new SqlConnection(this.Connect()); if (fulltext == null) { if (cri == "0") { cmdText = "select * from pt_info LEFT OUTER JOIN pwallet ON pt_info.log_staff=pwallet.ID WHERE (pwallet.status >'3') AND (title_of_invention like '%" + kword + "%') ORDER BY xID ASC"; } else { cmdText = "select * from pt_info LEFT OUTER JOIN pwallet ON pt_info.log_staff=pwallet.ID WHERE (pwallet.status >'3') AND (title_of_invention like '%" + kword + "%') ORDER BY xID ASC"; } } else { num = fulltext.Count - 1; if (cri == "0") { str2 = "select * from pt_info LEFT OUTER JOIN pwallet ON pt_info.log_staff=pwallet.ID WHERE (pwallet.status >'3') AND "; } else { str2 = "select * from pt_info LEFT OUTER JOIN pwallet ON pt_info.log_staff=pwallet.ID WHERE (pwallet.status >'3') AND "; } for (int i = 0; i < fulltext.Count; i++) { if (fulltext.Count == 1) { str3 = str3 + " ( title_of_invention like '%" + fulltext[i] + "%' ) "; } else if (num == i) { str3 = str3 + " ( title_of_invention like '%" + fulltext[i] + "%' ) "; } else { str3 = str3 + " ( title_of_invention like '%" + fulltext[i] + "%' ) OR"; } } str4 = " ORDER BY xID ASC"; cmdText = str2 + str3 + str4; } SqlCommand command = new SqlCommand(cmdText, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { PtInfo item = new PtInfo { xID = reader["xID"].ToString(), reg_number = reader["reg_number"].ToString(), xtype = reader["xtype"].ToString(), title_of_invention = reader["title_of_invention"].ToString(), pt_desc = reader["pt_desc"].ToString(), spec_doc = reader["spec_doc"].ToString(), loa_no = reader["loa_no"].ToString(), loa_doc = reader["loa_doc"].ToString(), claim_no = reader["claim_no"].ToString(), claim_doc = reader["claim_doc"].ToString(), pct_no = reader["pct_no"].ToString(), pct_doc = reader["pct_doc"].ToString(), doa_no = reader["doa_no"].ToString(), doa_doc = reader["doa_doc"].ToString(), log_staff = reader["log_staff"].ToString(), reg_date = reader["reg_date"].ToString(), xvisible = reader["xvisible"].ToString() }; list.Add(item); } reader.Close(); return list; }
public List<PtInfo> getPtInfoByUserID(string ID) { List<PtInfo> list = new List<PtInfo>(); new PtInfo(); SqlConnection connection = new SqlConnection(this.Connect()); SqlCommand command = new SqlCommand("SELECT * FROM pt_info WHERE xID='" + ID + "' ", connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); while (reader.Read()) { PtInfo item = new PtInfo { xID = reader["xID"].ToString(), reg_number = reader["reg_number"].ToString(), xtype = reader["xtype"].ToString(), title_of_invention = ConvertTab2Apos(reader["title_of_invention"].ToString()), pt_desc = ConvertTab2Apos(reader["pt_desc"].ToString()), spec_doc = reader["spec_doc"].ToString(), loa_no = reader["loa_no"].ToString(), loa_doc = reader["loa_doc"].ToString(), claim_no = reader["claim_no"].ToString(), claim_doc = reader["claim_doc"].ToString(), pct_no = reader["pct_no"].ToString(), pct_doc = reader["pct_doc"].ToString(), doa_no = reader["doa_no"].ToString(), doa_doc = reader["doa_doc"].ToString(), log_staff = reader["log_staff"].ToString(), reg_date = reader["reg_date"].ToString(), xvisible = reader["xvisible"].ToString() }; list.Add(item); } reader.Close(); return list; }