/// <summary> /// 计算 LSI 方法 /// </summary> /// <param name="outputFolderPath"></param> /// <param name="bugName"></param> /// <param name="queryText"></param> public static void ComputeLsi(string outputFolderPath, string bugName, List <string> queryText) { Utility.Status("Creating LSI: " + bugName); // 所有源码中的总独特词数 源文件 - 索引, 源文件单词 - 索引 int totalDistinctTermsInAllSourceFiles = IdfDictionary.Count; Dictionary <string, int> allSourceFilesWithIndex = TfDictionary.Keys.Select((x, index) => new { Name = x, Index = index }).ToDictionary(x => x.Name, x => x.Index); Dictionary <string, int> allSourceWordsWithIndex = IdfDictionary.Keys.Select((x, index) => new { Name = x, Index = index }).ToDictionary(x => x.Name, x => x.Index); // 为查询创建一个 1xm 的向量 double[,] queryMatrixTranspose = new double[1, totalDistinctTermsInAllSourceFiles]; queryText.ForEach(queryWord => { if (allSourceWordsWithIndex.ContainsKey(queryWord)) { queryMatrixTranspose[0, allSourceWordsWithIndex[queryWord]] = queryMatrixTranspose[0, allSourceWordsWithIndex[queryWord]] + 1; } }); // k列的 U 矩阵 var ks = _uk.Keys.Where(x => !File.Exists(outputFolderPath + LsiOutputFolderName + x + ".txt")).ToList(); // 为每个维度的矩阵计算相似度 foreach (var k in ks) { Utility.Status("Creating LSI for " + bugName + " where k=" + k); var uk = _uk[k]; var sk = _sk[k]; var vkTranspose = _vkTranspose[k]; DoubleMatrix q = new DoubleMatrix(queryMatrixTranspose); //qv = q * uk * sk.I [1xm,mxn,nxn = 1xn] DoubleMatrix qv = NMathFunctions.Product(q, uk); qv = NMathFunctions.Product(qv, NMathFunctions.Inverse(sk)); List <double> qDoubles = qv.Row(0).ToArray().ToList(); var similarityList = allSourceFilesWithIndex.Select(doc => new KeyValuePair <string, double>(doc.Key, Utility.GetSimilarity(qDoubles, vkTranspose.Col(doc.Value).ToArray().ToList()))); File.WriteAllLines(outputFolderPath + LsiOutputFolderName + k + ".txt", similarityList.OrderByDescending(x => x.Value).Select(x => x.Key + " " + x.Value.ToString("##.00000"))); } Utility.Status("Completed LSI: " + bugName); }
private Coordinate Positioning_Proximity() { List <Gateway> gateways = IndoorPositioningClient.GetGateways(); /* get the Rssi values of the beacon in question from the server */ RssiValue[] rssiValues = IndoorPositioningClient.GetRssi(gateways.Count); CoordinateAndDistance[] cooDist = new CoordinateAndDistance[gateways.Count]; for (int i = 0; i < gateways.Count; i++) { cooDist[i] = new CoordinateAndDistance() { coordinate = new Coordinate() { Xaxis = (int)GetGatewayXaxis(gateways[i]), Yaxis = (int)GetGatewayYaxis(gateways[i]), }, dist = rssiValues[i].Rssi }; } Coordinate coordinateFirstAndSecondLine = new Coordinate(); Coordinate coordinateFirstAndThirdLine = new Coordinate(); Coordinate coordinateSecondAndThirdLine = new Coordinate(); /* Create a vector from the coordinates */ /* Measure the first point of crossing the first and second line*/ DoubleMatrix matrixFirstAndSecondLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis }, { cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixFirstAndSecondLine) == 0) { return(cooDist[0].coordinate); } //matrixFirstAndSecondLine = matrixFirstAndSecondLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixFirstAndSecondLine = NMathFunctions.Inverse(matrixFirstAndSecondLine); DoubleVector vectorFirstAndSecondLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[1].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[1].GetDistPow(), cooDist[0].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[2].GetDistPow() }); vectorFirstAndSecondLine = vectorFirstAndSecondLine.Transform((p) => p / 2); DoubleVector coordinateVectorFirstAndSecondLine = NMathFunctions.Product(inversedMatrixFirstAndSecondLine, vectorFirstAndSecondLine); coordinateFirstAndSecondLine.Xaxis = (int)coordinateVectorFirstAndSecondLine[0]; coordinateFirstAndSecondLine.Yaxis = (int)coordinateVectorFirstAndSecondLine[1]; /* Measure the first point of crossing the first and third line*/ DoubleMatrix matrixFirstAndThirdLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis }, { cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixFirstAndThirdLine) == 0) { return(cooDist[0].coordinate); } //matrixFirstAndThirdLine = matrixFirstAndThirdLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixFirstAndThirdLine = NMathFunctions.Inverse(matrixFirstAndThirdLine); DoubleVector vectorFirstAndThirdLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[1].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[1].GetDistPow(), cooDist[1].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[1].GetDistPow() + cooDist[2].GetDistPow() }); vectorFirstAndThirdLine = vectorFirstAndThirdLine.Transform((p) => p / 2); DoubleVector coordinateVectorFirstAndThirdLine = NMathFunctions.Product(inversedMatrixFirstAndThirdLine, vectorFirstAndThirdLine); coordinateFirstAndThirdLine.Xaxis = (int)coordinateVectorFirstAndThirdLine[0]; coordinateFirstAndThirdLine.Yaxis = (int)coordinateVectorFirstAndThirdLine[1]; /* Measure the first point of crossing the first and third line*/ DoubleMatrix matrixSecondAndThirdLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis }, { cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixSecondAndThirdLine) == 0) { return(cooDist[0].coordinate); } //matrixSecondAndThirdLine = matrixSecondAndThirdLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixSecondAndThirdLine = NMathFunctions.Inverse(matrixSecondAndThirdLine); DoubleVector vectorSecondAndThirdLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[2].GetDistPow(), cooDist[1].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[1].GetDistPow() + cooDist[2].GetDistPow() }); vectorSecondAndThirdLine = vectorSecondAndThirdLine.Transform((p) => p / 2); DoubleVector coordinateVectorSecondAndThirdLine = NMathFunctions.Product(inversedMatrixSecondAndThirdLine, vectorSecondAndThirdLine); coordinateSecondAndThirdLine.Xaxis = (int)coordinateVectorSecondAndThirdLine[0]; coordinateSecondAndThirdLine.Yaxis = (int)coordinateVectorSecondAndThirdLine[1]; int xaxis = (coordinateFirstAndSecondLine.Xaxis + coordinateFirstAndThirdLine.Xaxis + coordinateSecondAndThirdLine.Xaxis) / 3; int yaxis = (coordinateFirstAndSecondLine.Yaxis + coordinateFirstAndThirdLine.Yaxis + coordinateSecondAndThirdLine.Yaxis) / 3; Debug.WriteLine("Xaxis:" + xaxis); Debug.WriteLine("Yaxis:" + yaxis); return(new Coordinate() { Xaxis = xaxis, Yaxis = yaxis }); }
private Coordinate Positioning_KnnProximity(List <AdjustedFingerprinting> fingerprintings) { int k = 3; /* run the knn classifier on the data */ KnnClassifier classifier = new KnnClassifier(); /* After fetching and processing the fingerprinting data, I am able to get the class count. * Basically, each of the reference point is a class to be classified to. */ int numClasses = IndoorPositioningClient.GetPoints(environment.EnvironmentId).Count; int gatewayCount = fingerprintings[0].RssiValueAndGateway.Count; /* get the Rssi values of the beacon in question from the server */ RssiValue[] rssiValues = IndoorPositioningClient.GetRssi(gatewayCount); /* we will use also gateway count on the area as K constant */ CoordinateAndDistance[] cooDist = classifier.GetNearestNeighbors(rssiValues, fingerprintings, numClasses, k); Coordinate coordinateFirstAndSecondLine = new Coordinate(); Coordinate coordinateFirstAndThirdLine = new Coordinate(); Coordinate coordinateSecondAndThirdLine = new Coordinate(); /* Create a vector from the coordinates */ /* Measure the first point of crossing the first and second line*/ DoubleMatrix matrixFirstAndSecondLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis }, { cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixFirstAndSecondLine) == 0) { return(classifier.Vote(cooDist, fingerprintings, numClasses, k)); } //matrixFirstAndSecondLine = matrixFirstAndSecondLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixFirstAndSecondLine = NMathFunctions.Inverse(matrixFirstAndSecondLine); DoubleVector vectorFirstAndSecondLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[1].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[1].GetDistPow(), cooDist[0].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[2].GetDistPow() }); vectorFirstAndSecondLine = vectorFirstAndSecondLine.Transform((p) => p / 2); DoubleVector coordinateVectorFirstAndSecondLine = NMathFunctions.Product(inversedMatrixFirstAndSecondLine, vectorFirstAndSecondLine); coordinateFirstAndSecondLine.Xaxis = (int)coordinateVectorFirstAndSecondLine[0]; coordinateFirstAndSecondLine.Yaxis = (int)coordinateVectorFirstAndSecondLine[1]; /* Measure the first point of crossing the first and third line*/ DoubleMatrix matrixFirstAndThirdLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis }, { cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixFirstAndThirdLine) == 0) { return(classifier.Vote(cooDist, fingerprintings, numClasses, k)); } //matrixFirstAndThirdLine = matrixFirstAndThirdLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixFirstAndThirdLine = NMathFunctions.Inverse(matrixFirstAndThirdLine); DoubleVector vectorFirstAndThirdLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[1].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[1].GetDistPow(), cooDist[1].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[1].GetDistPow() + cooDist[2].GetDistPow() }); vectorFirstAndThirdLine = vectorFirstAndThirdLine.Transform((p) => p / 2); DoubleVector coordinateVectorFirstAndThirdLine = NMathFunctions.Product(inversedMatrixFirstAndThirdLine, vectorFirstAndThirdLine); coordinateFirstAndThirdLine.Xaxis = (int)coordinateVectorFirstAndThirdLine[0]; coordinateFirstAndThirdLine.Yaxis = (int)coordinateVectorFirstAndThirdLine[1]; /* Measure the first point of crossing the first and third line*/ DoubleMatrix matrixSecondAndThirdLine = new DoubleMatrix(new double[, ] { { cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis }, { cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis } }); /* If the determinant value of the matrix is 0, it means this matrix cannot be inverted. * We can have the exact point values with one of the reference points. */ if (NMathFunctions.Determinant(matrixSecondAndThirdLine) == 0) { return(classifier.Vote(cooDist, fingerprintings, numClasses, k)); } //matrixSecondAndThirdLine = matrixSecondAndThirdLine.Transform((p) => p * 2); DoubleMatrix inversedMatrixSecondAndThirdLine = NMathFunctions.Inverse(matrixSecondAndThirdLine); DoubleVector vectorSecondAndThirdLine = new DoubleVector(new double[] { cooDist[0].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[2].GetDistPow(), cooDist[1].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[1].GetDistPow() + cooDist[2].GetDistPow() }); vectorSecondAndThirdLine = vectorSecondAndThirdLine.Transform((p) => p / 2); DoubleVector coordinateVectorSecondAndThirdLine = NMathFunctions.Product(inversedMatrixSecondAndThirdLine, vectorSecondAndThirdLine); coordinateSecondAndThirdLine.Xaxis = (int)coordinateVectorSecondAndThirdLine[0]; coordinateSecondAndThirdLine.Yaxis = (int)coordinateVectorSecondAndThirdLine[1]; //DoubleMatrix matrixFirstAndThirdLine = new DoubleMatrix(new double[,] //{ // { // cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, // cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis // }, // { // cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, // cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis // }, // { // cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, // cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis // } //}); //DoubleMatrix inversedMatrix = NMathFunctions.Inverse(matrix); //DoubleVector vector = new DoubleVector(new double[] //{ // cooDist[0].coordinate.GetNormPow() - cooDist[1].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[1].GetDistPow(), // cooDist[0].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[0].GetDistPow() + cooDist[2].GetDistPow(), // cooDist[1].coordinate.GetNormPow() - cooDist[2].coordinate.GetNormPow() - cooDist[1].GetDistPow() + cooDist[2].GetDistPow() //}); //DoubleVector coordinateVector = NMathFunctions.Product(inversedMatrix, vector); //coordinate.Xaxis = (int)coordinateVector[0]; //coordinate.Xaxis = (int)coordinateVector[1]; //Matrix linesMatrix = new Matrix( // cooDist[0].coordinate.Xaxis - cooDist[1].coordinate.Xaxis, // cooDist[0].coordinate.Yaxis - cooDist[1].coordinate.Yaxis, // cooDist[0].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, // cooDist[0].coordinate.Yaxis - cooDist[2].coordinate.Yaxis, // cooDist[1].coordinate.Xaxis - cooDist[2].coordinate.Xaxis, // cooDist[1].coordinate.Yaxis - cooDist[2].coordinate.Yaxis); ////Vector distances = new Vector( //// coordinateAndDistances[0].coordinate.Xaxis - coordinateAndDistances[1].coordinate.Xaxis,); int xaxis = (coordinateFirstAndSecondLine.Xaxis + coordinateFirstAndThirdLine.Xaxis + coordinateSecondAndThirdLine.Xaxis) / 3; int yaxis = (coordinateFirstAndSecondLine.Yaxis + coordinateFirstAndThirdLine.Yaxis + coordinateSecondAndThirdLine.Yaxis) / 3; Debug.WriteLine("Xaxis:" + xaxis); Debug.WriteLine("Yaxis:" + yaxis); return(new Coordinate() { Xaxis = xaxis, Yaxis = yaxis }); }