// ²âÊÔ 3 ¸ö×èµ²µã public List <Vertex> test3Stop(MGraph pMGraph) { StopPoint pStopPoint = null; pStopPoint = new StopPoint(); pMGraph.addStopPoint(1, 0, pStopPoint); pStopPoint = new StopPoint(); pMGraph.addStopPoint(1, 1, pStopPoint); pStopPoint = new StopPoint(); pMGraph.addStopPoint(1, 2, pStopPoint); List <Vertex> vertList; if (pMGraph.isPathCacheValid(0, 8)) { vertList = pMGraph.getShortestPathFromPathCache(0, 8); } else { vertList = pMGraph.getOrCreateShortestPath(0, 8); } return(vertList); }
public void run() { MGraph pMGraph = new MGraph(); pMGraph.init(3, 3, 1, 1); List <Vertex> vertList; FileStream pFile = new FileStream("E:\\aaa.txt", FileMode.Create); string strStream = ""; vertList = test5Stop(pMGraph); serializePath(vertList, ref strStream); pMGraph.clearAllStopPoint(); pMGraph.clearPath(); vertList = test4Stop(pMGraph); serializePath(vertList, ref strStream); pMGraph.clearAllStopPoint(); pMGraph.clearPath(); vertList = test2Stop(pMGraph); serializePath(vertList, ref strStream); pMGraph.clearAllStopPoint(); pMGraph.clearPath(); vertList = test2Stop(pMGraph); serializePath(vertList, ref strStream); pMGraph.clearAllStopPoint(); pMGraph.clearPath(); byte[] inBytes = System.Text.Encoding.UTF8.GetBytes(strStream); pFile.Write(inBytes, 0, inBytes.Length); pFile.Close(); pFile.Dispose(); }
static void Floyd(MGraph g) { int[,] dist = new int[MaxV, MaxV]; //顶点之间的最短长度矩阵 int[,] path = new int[MaxV, MaxV]; //顶点之间的最短路径矩阵 int i, j, k; //初始化节点间距及前驱节点 for (i = 0; i < g.n; i++) { for (j = 0; j < g.n; j++) { dist[i, j] = g.edges[i, j]; if (dist[i, j] == INF) { path[i, j] = -1; } else { path[i, j] = i; } } } for (k = 0; k < g.n; k++) //中间节点在最外层 { for (i = 0; i < g.n; i++) //起点 { if (i == k) //十字交叉法优化 { continue; } for (j = 0; j < g.n; j++) //终点 { if (j == i || j == k) //十字交叉法优化 { continue; } if (dist[i, k] != INF && dist[k, j] != INF && dist[i, k] + dist[k, j] < dist[i, j]) //从i经过k到j的路径比从i到j的路径短 { dist[i, j] = dist[i, k] + dist[k, j]; //更新路径长度 path[i, j] = path[k, j]; //更新前驱节点 } } } } //for (i = 0; i < g.n; i++)//起点 //{ // for (j = 0; j < g.n; j++)//终点 // { // Console.Write("{0,6}", path[i, j]); // } // Console.WriteLine("\n"); //} Display(dist, path, g.n); //输出最短路径 }
//public string[] dkmileage; //public double[] m;// = { 11,180,340,474.862,751.254}; //public double[] h;// = { 201.53, 202.63, 206.36, 210.92, 216.9}; //public double[] r; // = { 0, 3000, 3000,3500}; //public double[] i1;// = {0, 0, 0, 0 }; 前坡度 //public double[] i2;// = {0, 0, 0, 0 }; 后坡度 //public double[] sm;// = {0, 0, 0, 0 }; 起始变高 //public double[] em;// = {0, 0, 0, 0 }; 终止变高 //public double[] heightM; //public void initHeightCurve(string pdPath = @"C:\GISData\Common\pd.xlsx") //{ // DataTable dt1; // string code; // double mileage; // //double dis; // int count; // int i = 0; // dt1 = ExcelWrapper.LoadDataTableFromExcel(pdPath, @"select mileage, altitude, radius from [sheet1$] order by id"); // count = dt1.Rows.Count; // dkmileage = new string[count]; // m = new double[count]; // h = new double[count]; // r = new double[count]; // i1 = new double[count]; // i2 = new double[count]; // sm = new double[count]; // em = new double[count]; // //CSubPath path = new CSubPath(CRailwayLineList.gMileage, "DK", 299000, "右改DK", 17600, 10); // //path.outputPathInfo(); // foreach (DataRow dr in dt1.Rows) // { // dkmileage[i] = dr["mileage"].ToString(); // CRailwayLineList.parseDKCode(dkmileage[i], out code, out mileage); // m[i] = getPathMileageByDKCode(code, mileage); // if (m[i] >= 0) // { // h[i] = Convert.ToDouble(dr["altitude"]); // r[i] = Convert.ToDouble(dr["radius"]); // } // else // { // Console.WriteLine(dkmileage[i] + "不存在"); // } // i++; // } // for (i = 1; i < m.Length - 1; i++) // { // heightCurveParams(i); // } // sm[0] = 0; // em[0] = 0; // sm[m.Length - 1] = em[m.Length - 1] = m[m.Length - 1]; //} //private void createHeightSegment(int typeSeg, int id, double length, double fromM, out double[] mSeg, out double[] hSeg, bool includeLastP = true) //{ // if (typeSeg == 0) // 前直线 // mSeg = CSubLine.generateSamplePoints(length, 50, fromM, includeLastP); // else // 圆曲线 // mSeg = CSubLine.generateSamplePoints(length, 10, fromM, includeLastP); // if (mSeg == null) // { // hSeg = null; // return; // } // hSeg = new double[mSeg.Length]; // for (int i = 0; i < mSeg.Length; i++) // { // if (typeSeg == 0) // hSeg[i] = h[id] + (mSeg[i] - m[id]) * i1[id]; // else // hSeg[i] = h[id] + (sm[id] - m[id]) * i1[id] + (mSeg[i] - sm[id]) * i1[id] // + Math.Sign(i2[id] - i1[id]) * (mSeg[i] - sm[id]) * (mSeg[i] - sm[id]) / r[id] / 2; // } //} ///// <summary> ///// 利用竖曲线创建高程,写到txt文件中 ///// </summary> ///// <param name="pdPath">竖曲线坡度表</param> //public void createHeightSample(string pdPath) //{ // initHeightCurve(pdPath); // List<PathNode> ls = mPathNodeList; // double[][] mlist = new double[ls.Count][]; // double[][] hlist = new double[ls.Count][]; // List<double> mls = new List<double>(); // List<double> hls = new List<double>(); // int j = 0; // double fromM = 0; // double[] mtmp, htmp; // for (int id = 1; id < m.Length; id++) // { // while (j < ls.Count && mNodeLength[j] < sm[id]) // { // createHeightSegment(0, id, mNodeLength[j] - fromM, fromM, out mtmp, out htmp, true); // fromM = mNodeLength[j]; // mls.AddRange(mtmp); // hls.AddRange(htmp); // mlist[j] = mls.ToArray(); // hlist[j] = hls.ToArray(); // mls.Clear(); // hls.Clear(); // j++; // } // if (id < m.Length - 1) // createHeightSegment(0, id, sm[id] - fromM, fromM, out mtmp, out htmp, false); // else // createHeightSegment(0, id, sm[id] - fromM, fromM, out mtmp, out htmp, true); // fromM = sm[id]; // mls.AddRange(mtmp); // hls.AddRange(htmp); // //double[] mm1 = CSubLine.generateSamplePoints(sm[id] - em[id - 1], 50, em[id - 1]); // //double[] hh1 = new double[mm1.Length]; // //for (int k = 0; k < hh1.Length; k++) // //{ // // hh1[k] = h[id] + (mm1[k] - m[id]) * i1[id]; // // Console.WriteLine(Math.Round(mm1[k], 2) + "\t" + Math.Round(hh1[k], 2)); // //} // //Console.WriteLine(); // if (em[id] - sm[id] < 0.1) continue; // 一般是最后一个点 // while (j < ls.Count && mNodeLength[j] < em[id]) // { // createHeightSegment(1, id, mNodeLength[j] - fromM, fromM, out mtmp, out htmp, true); // fromM = mNodeLength[j]; // mls.AddRange(mtmp); // hls.AddRange(htmp); // mlist[j] = mls.ToArray(); // hlist[j] = hls.ToArray(); // mls.Clear(); // hls.Clear(); // j++; // } // createHeightSegment(1, id, em[id] - fromM, fromM, out mtmp, out htmp, false); // fromM = em[id]; // mls.AddRange(mtmp); // hls.AddRange(htmp); // //double[] mm2 = CSubLine.generateSamplePoints(em[id] - sm[id], 10, sm[id]); // //double[] hh2 = new double[mm2.Length]; // //for (int k = 0; k < hh2.Length; k++) // //{ // // hh2[k] = h[id] + (sm[id] - m[id]) * i1[id] + (mm2[k] - sm[id]) * i1[id] // //+ Math.Sign(i2[id] - i1[id]) * (mm2[k] - sm[id]) * (mm2[k] - sm[id]) / r[id] / 2; // // Console.WriteLine(Math.Round(mm2[k], 2) + "\t" + Math.Round(hh2[k], 2)); // //} // //Console.WriteLine(); // } // mlist[mlist.Length - 1] = mls.ToArray(); // hlist[mlist.Length - 1] = hls.ToArray(); // int i = 0; // double offsetm; // foreach (PathNode pn in ls) // { // if (i == 0) // offsetm = 0; // else // offsetm = mNodeLength[i - 1]; // if (!pn.isReverse) // offsetm += -Math.Abs(pn.fromMileage - pn.mRL.mStart); // //else // // offsetm += Math.Abs(pn.fromMileage - pn.mRL.mEnd); // for (j = 0; j < mlist[i].Length; j++) // { // mlist[i][j] = mlist[i][j] - offsetm; // //Console.WriteLine(Math.Round(mlist[i][j], 2) + "\t" + Math.Round(hlist[i][j], 2)); // } // pn.mRL.createExcelData(mlist[i], hlist[i], new System.IO.StreamWriter(pn.mRL.mIndex + ".txt")); // Console.WriteLine(); // i++; // } //} //public void heightCurveParams(int id) //{ // i1[id] = (h[id] - h[id - 1]) / (m[id] - m[id - 1]); // i2[id] = (h[id + 1] - h[id]) / (m[id + 1] - m[id]); // double t = r[id] * Math.Abs(i2[id] - i1[id]) / 2; // double e = t * t / r[id] / 2; // sm[id] = m[id] - t; // em[id] = m[id] + t; //} //public double getHeight(double mileage, int id) //{ // double res = 0; // if (mileage < sm[id]) // { // res = h[id] + (mileage - m[id]) * i1[id]; // } // else if (mileage > em[id]) // { // res = h[id] + (mileage - m[id]) * i2[id]; // } // else // { // res = h[id] + (sm[id] - m[id]) * i1[id] + (mileage - sm[id]) * i1[id] // + Math.Sign(i2[id] - i1[id]) * (mileage - sm[id]) * (mileage - sm[id]) / r[id] / 2; // } // return res; //} #endregion public CSubPath(MGraph graph, string fromDK, double fromMileage, string toDK, double toMileage, double stepm) { List <string> strls = new List <string>(); List <double> dls = new List <double>(); strls.Add(fromDK); strls.Add(toDK); dls.Add(fromMileage); dls.Add(toMileage); CreateSubPathFromNodeList(graph, strls, dls, stepm); }
static void initialVexInfo(MGraph g) { //打开poi的shp文件,获取节点X\Y\Z IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass(); IFeatureWorkspace pWS = (IFeatureWorkspace)pWSF.OpenFromFile(@"res\ppp_jw", 0);//依据经纬度数据画线 IFeatureClass pFeatureclass = pWS.OpenFeatureClass("poi.shp"); IFeatureCursor pCursor = pFeatureclass.Search(null, false); IPolyline pPolyline = new PolylineClass(); IPointCollection pPolycollect = pPolyline as IPointCollection; int i = 0;//节点ID while (pCursor != null) { IFeature pFeature = pCursor.NextFeature(); if (pFeature != null) { IGeometry pGeometry = pFeature.Shape; object objmiss = Type.Missing; IPoint pPoint = new PointClass(); pPoint.X = Convert.ToDouble(pFeature.get_Value(2)); pPoint.Y = Convert.ToDouble(pFeature.get_Value(3)); pPoint.Z = Convert.ToDouble(pFeature.get_Value(9)); g.vexs[i].SpatialPoi = pPoint; i++; } else { pCursor = null; } } g.vexs[0].VexNo = "0"; g.vexs[0].VexName = "七星鲁王宫"; g.vexs[1].VexNo = "1"; g.vexs[1].VexName = "西沙海底墓"; g.vexs[2].VexNo = "2"; g.vexs[2].VexName = "秦岭神树"; g.vexs[3].VexNo = "3"; g.vexs[3].VexName = "云顶天宫"; g.vexs[4].VexNo = "4"; g.vexs[4].VexName = "西王母宫"; g.vexs[5].VexNo = "5"; g.vexs[5].VexName = "张家古楼"; }
// ²âÊÔ 0 ¸ö×èµ²µã public List <Vertex> test0Stop(MGraph pMGraph) { List <Vertex> vertList; if (pMGraph.isPathCacheValid(0, 4)) { vertList = pMGraph.getShortestPathFromPathCache(0, 4); } else { vertList = pMGraph.getOrCreateShortestPath(0, 4); } return(vertList); }
static void Dijkstra(MGraph g, int v) { int[] dist = new int[MaxV]; //从源点v到其他各节点的最短路径长度 int[] prev = new int[MaxV]; //path[i]表示从源点v到节点i之间最短路径的前驱节点 bool[] s = new bool[MaxV]; //选定的节点的集合 for (int i = 0; i < g.n; i++) { dist[i] = g.edges[v, i]; //距离初始化 s[i] = false; //s[]初始化false if (g.edges[v, i] < INF) //初始化前驱节点 { prev[i] = v; } else { prev[i] = -1; } } s[v] = true;//源点编号v放入s中 for (int i = 0; i < g.n; i++) { int mindis = INF; int u = -1; //-1 表示没有可到达的最近节点 for (int j = 0; j < g.n; j++) //选取不在s中且具有最小距离的节点u { if (!s[j] && dist[j] < mindis) { u = j; mindis = dist[j]; } } if (u == -1) //如没有找到新的最近点,即剩余都是无穷远点/孤点 { break; //孤点/图情景,剪去不必要的计算 } else { s[u] = true;//节点u加入s中 } for (int j = 0; j < g.n; j++) { if (!s[j] && g.edges[u, j] != INF && dist[u] + g.edges[u, j] < dist[j]) //如果通过u能到达j,并且比之前的路径更短 { dist[j] = dist[u] + g.edges[u, j]; //更新路径长度 prev[j] = u; //更新j的前驱节点 } } } Console.Write("\t前驱节点表:", v); foreach (int p in prev) { Console.Write("{0,4}", p); } Console.WriteLine(); Display(dist, prev, s, g.n, v); //输出最短路径 }
// public Dictionary<string, SiteEpochSatData> DataOfAllSites = new Dictionary<string, SiteEpochSatData>(); #endregion public void IndepentBaselineProcess() { //存储基线 Dictionary <string, Edge> graphRound = new Dictionary <string, Edge>(); int n = DataOfAllSites.Length; mgraph = new MGraph(); mgraph.n = n; mgraph.e = n * (n - 1) / 2; mgraph.vex = new VertexType[n]; mgraph.edges = new int[n][]; road = new Road[n * (n - 1) / 2]; v = new int[n * (n - 1) / 2]; int ii = 0; for (int i = 0; i < n; i++) { // SiteEpochSatData dataA = DataOfAllSites.ElementAt(i).Value; SiteEpochSatData dataA = DataOfAllSites[i]; mgraph.vex[i] = new VertexType(); mgraph.vex[i].no = i; mgraph.vex[i].name = dataA.SiteName; v[i] = i; //顶点ver[i]初始时表示各在不同的连通分支v[i]中,父结点依次为v[i] for (int j = i + 1; j < n; j++) { mgraph.edges[i] = new int[n]; SiteEpochSatData dataB = DataOfAllSites[j]; //.ElementAt (j).Value; double toleranceSeccond = 3.5; //限差 单位:秒 int count = 0; //for (int k = 0, s = 0; k < dataA.TimeInfo.Length.Data.Count && s < dataB.Data.Count; k++, s++) for (int k = 0, s = 0; k < dataA.TimeInfo.Length && s < dataB.TimeInfo.Length; k++, s++) { //double diff = Math.Abs(dataA.Data.ElementAt(k).Key - dataB.Data.ElementAt(s).Key); double diff = Math.Abs(dataA.TimeInfo[k] - dataB.TimeInfo[s]); if (diff <= toleranceSeccond) { //IEnumerable<int> strB = dataB.EpochSatData.ElementAt(s).Value; string strA = dataA.PrnInfo[k]; string strB = dataB.PrnInfo[s]; if (strA == null || strB == null) { break; } for (int ss = 0; ss < strA.Length / 3; ss++) { string prn = strA.Substring(ss * 3, 3); if (strB.Contains(prn)) // if (strB.IndexOf(prn)!=-1) //即含有 { count += 1; } } } else { // if (dataB.Data.ElementAt(s).Key < dataA.Data.ElementAt(k).Key) if (dataB.TimeInfo[s] < dataA.TimeInfo[k]) { while (diff > toleranceSeccond) { s++; if (s >= dataB.TimeInfo.Length) { break; } //diff = Math.Abs(dataA.Data.ElementAt(k).Key - dataB.Data.ElementAt(s).Key); diff = Math.Abs(dataA.TimeInfo[k] - dataB.TimeInfo[s]); if (diff <= toleranceSeccond) { string strA = dataA.PrnInfo[k]; string strB = dataB.PrnInfo[s]; if (strA == null || strB == null) { break; } for (int ss = 0; ss < strA.Length / 3; ss++) { string prn = strA.Substring(ss * 3, 3); if (strB.Contains(prn)) { count += 1; } } break; } } } else if (dataB.TimeInfo[s] > dataA.TimeInfo[k]) { while (diff > toleranceSeccond) { k++; if (k >= dataA.TimeInfo.Length) { break; } diff = Math.Abs(dataA.TimeInfo[k] - dataB.TimeInfo[s]); if (diff <= toleranceSeccond) { string strA = dataA.PrnInfo[k]; string strB = dataB.PrnInfo[s]; if (strA == null || strB == null) { break; } for (int ss = 0; ss < strA.Length / 3; ss++) { string prn = strA.Substring(ss * 3, 3); if (strB.Contains(prn)) { count += 1; } } break; } } } } } //End 完成一条基线的统计 mgraph.edges[i][j] = count; road[ii] = new Road(); road[ii].a = i; road[ii].b = j; road[ii].w = count; ii++; ////写入 //string sb = dataA.SiteName + "-" + dataB.SiteName + " " + count.ToString(); //writer.WriteLine(sb); Node NodeA = new Node(); NodeA.Name = dataA.SiteNumber; NodeA.strName = dataA.SiteName; NodeA.Visited = false; NodeA.Tag = dataA.Path; Node NodeB = new Node(); NodeB.Name = dataB.SiteNumber; NodeB.strName = dataB.SiteName; NodeB.Visited = false; NodeB.Tag = dataB.Path; Edge edge = new Edge(); edge.NodeA = NodeA; edge.NodeB = NodeB; edge.Weight = count; string baselineName = dataA.SiteName + dataB.SiteName; graphRound.Add(baselineName, edge); } } //sort 排序 由大到小 List <KeyValuePair <string, Edge> > graphPair = new List <KeyValuePair <string, Edge> >(graphRound); graphPair.Sort(delegate(KeyValuePair <string, Edge> s1, KeyValuePair <string, Edge> s2) { return(s1.Value.Weight.CompareTo(s2.Value.Weight)); }); //存储排序基线 Dictionary <string, Edge> graph = new Dictionary <string, Edge>(); for (int index = graphPair.Count - 1; index >= 0; index--) { var item = graphPair.ElementAt(index); Edge edge = new Edge(); edge.NodeA = item.Value.NodeA; edge.NodeB = item.Value.NodeB; edge.Weight = item.Value.Weight; graph.Add(item.Key, edge); //重新排序 road[mgraph.e - 1 - index].a = DataOfAllSites[item.Value.NodeA.Name].SiteNumber; road[mgraph.e - 1 - index].b = DataOfAllSites[item.Value.NodeB.Name].SiteNumber; road[mgraph.e - 1 - index].w = item.Value.Weight; } //根据Kruskal算法生成最小生成树 GetMinCostSpanTree List <Edge> findedMinCostSpanTree = new List <Edge>(); List <List <Node> > findedNodes = new List <List <Node> >(); findedNodes.Add(new List <Node>() { graph.ElementAt(0).Value.NodeA, graph.ElementAt(0).Value.NodeB }); findedMinCostSpanTree.Add(graph.ElementAt(0).Value); for (int index = 1; index < graph.Count; index++) { var item = graph.ElementAt(index); int i = 0, indexA = -1, indexB = -1; Node nodeA = item.Value.NodeA; Node nodeB = item.Value.NodeB; foreach (var nodes in findedNodes) { foreach (var node in nodes) { if (!item.Value.NodeB.Visited && node.Name == nodeB.Name && node.Visited == nodeB.Visited) { item.Value.NodeB.Visited = true; indexB = i; } if (!item.Value.NodeA.Visited && node.Name == nodeA.Name && node.Visited == nodeA.Visited) { item.Value.NodeA.Visited = true; indexA = i; } } i++; } // if (item.Value.NodeA.Visited && item.Value.NodeB.Visited && (indexA != indexB)) { //连接不同的联通分量,则这两个连通分量可以合并成一个了。 int minId = Math.Min(indexA, indexB); int maxId = Math.Max(indexA, indexB); findedNodes[minId].AddRange(findedNodes[maxId]); findedNodes.RemoveAt(maxId); findedMinCostSpanTree.Add(item.Value); } else if (!item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //都不包含,直接添加新列表 findedNodes.Add(new List <Node>() { nodeA, nodeB }); findedMinCostSpanTree.Add(item.Value); } else if (item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //包含A,则将B添加到A的集合中去 findedNodes[indexA].Add(nodeB); findedMinCostSpanTree.Add(item.Value); } else if (!item.Value.NodeA.Visited && item.Value.NodeB.Visited) { //包含B,则将A添加到B的集合中去 findedNodes[indexB].Add(nodeA); findedMinCostSpanTree.Add(item.Value); } item.Value.NodeA.Visited = false; item.Value.NodeB.Visited = false; } //writer.WriteLine("\n"); //writer.WriteLine("根据最大观测量选择的独立基线: "); int jj = 0; IndepentBaselinesInfo = new List <GnssBaseLineName>(); foreach (var item in findedMinCostSpanTree) { var tmp = new GnssBaseLineName(item.NodeA.strName, item.NodeB.strName) { RefFilePath = item.NodeA.Tag + "", RovFilePath = item.NodeB.Tag + "", } ; // + BaseLineSplitter + item.NodeB.strName;// + " " + key.Weight; IndepentBaselinesInfo.Add(tmp); //写入 //writer.WriteLine(tmp); jj++; } //writer.Close(); //关闭 }
/// <summary> /// 生成子路径 /// </summary> /// <param name="g">寻径的floyd图</param> /// <param name="DKlist">路径上的dkcode列表,至少2个</param> /// <param name="milelist">路径上的里程列表,至少2个</param> /// <param name="stepm">细分里程间隔</param> private void CreateSubPathFromNodeList(MGraph g, List <string> DKlist, List <double> milelist, double stepm) { if (DKlist == null || DKlist.Count < 2) { return; } mFromDK = DKlist.First(); mToDK = DKlist.Last(); mFromMileage = milelist.First(); mToMileage = milelist.Last(); mStepM = stepm; mLength = 0; List <PathNode> ls = new List <PathNode>(); // 路径节点,0,2,4,6下标为链路编号,1,3,5,7等标注正序还是逆序,0-正序,1-逆序 List <int> idls = null; CRailwayLine tmpLine; //存放所有路径上的链路编号 int[] rlls = new int[DKlist.Count]; for (int i = 0; i < DKlist.Count; i++) { tmpLine = CRailwayLineList.getRailwayLineByDKCode(DKlist[i], milelist[i]); if (tmpLine == null) { return; } rlls[i] = tmpLine.mIndex; } // 路径节点,0,2,4,6下标为路径编号,1,3,5,7等标注正序还是逆序,0-正序,1-逆序 idls = ShortestPathFloyd.getNavPath(rlls, g); if (idls == null) { Console.WriteLine(DKlist[0] + "等路径不存在"); return; } if (DKlist.Count == 2 && rlls[0] == rlls[1]) // 起点和终点在同一个链路中 { CRailwayLine rl = CRailwayLineList.getRailwayLineByIndex(idls[0]); mLength = Math.Abs(milelist[0] - milelist.Last()); //if (idls[1] == 0) ls.Add(new PathNode(rl, milelist[0], milelist.Last(), false)); //else // ls.Add(new PathNode(rl, milelist.Last(), milelist[0], true)); } else if (idls.Count > 2) { mLength = 0; mNodeLength = new double[idls.Count / 2]; // path中的第一条链 CRailwayLine rl = CRailwayLineList.getRailwayLineByIndex(idls[0]); if (idls[1] == 0) { ls.Add(new PathNode(rl, milelist[0], rl.mEnd, false)); mLength += Math.Abs(milelist[0] - rl.mEnd); } else { ls.Add(new PathNode(rl, milelist[0], rl.mStart, true)); mLength += Math.Abs(milelist[0] - rl.mStart); } mNodeLength[0] = mLength; if (idls.Count > 2) { // path的中间链 for (int i = 1; i < idls.Count / 2 - 1; i++) { rl = CRailwayLineList.getRailwayLineByIndex(idls[2 * i]); if (idls[2 * i + 1] == 0) { ls.Add(new PathNode(rl, rl.mStart, rl.mEnd, false)); } else { ls.Add(new PathNode(rl, rl.mEnd, rl.mStart, true)); } mLength += Math.Abs(rl.mLength); mNodeLength[i] = mLength; } // path的最后一条链 rl = CRailwayLineList.getRailwayLineByIndex(idls[idls.Count - 2]); if (idls.Last() == 0) { ls.Add(new PathNode(rl, rl.mStart, milelist.Last(), false)); mLength += Math.Abs(rl.mStart - milelist.Last()); } else { ls.Add(new PathNode(rl, rl.mEnd, milelist.Last(), true)); mLength += Math.Abs(rl.mEnd - milelist.Last()); } mNodeLength[mNodeLength.Length - 1] = mLength; } } mPathNodeList = ls; hasPath = mPathNodeList.Count > 0 && mLength > 1; if (hasPath) { createSubLine(); } }
public CSubPath(MGraph g, List <string> DKlist, List <double> milelist, double stepm) { CreateSubPathFromNodeList(g, DKlist, milelist, stepm); }
public static MGraph gMileageConnection; // 用于计算里程 /// <summary> /// 由数据库或远程服务器读入三维中线的数据,这儿无效,在工具中使用, /// </summary> /// <param name="fileName"></param> //public static void CreateLinelistFromExcel(string dbPath) //{ // double[] m, x, y, z; // double fromMeter, toMeter; // //double fromX, toX, fromY, toY; // int chainIndex; // int chainType; // string dkcode, dkcode2; // //bool isRight = false; // //bool isDouble = true; // int count; // DataTable dt1, dt2, dt3; // //本地sqlite数据库读取里程数据 // //#if DEBUG // // Helper.LogHelper.WriteLog("SQlite Database" + dbPath); // //#endif // dt1 = ExcelWrapper.LoadDataTableFromExcel(dbPath, @"select chainIndex, fromMeter, toMeter,DKCode,DKCode2,chainType from [ChainInfo$] order by chainIndex"); // dt2 = ExcelWrapper.LoadDataTableFromExcel(dbPath, @"select fromChain, toChain from [ExtraConnection$] "); // List<OneConnection> connectionList = new List<OneConnection>(); // foreach (DataRow dr in dt2.Rows) // { // OneConnection aCon = new OneConnection(); // aCon.fromIndex = Convert.ToInt32(dr["fromChain"]); // aCon.toIndex = Convert.ToInt32(dr["toChain"]); // //aCon.isRealConnect = Convert.ToInt32(dr["isConnect"])==0 ? true:false; // //aCon.mileageOffset = Convert.ToDouble(dr["mileageOffset"]); // connectionList.Add(aCon); // } // //int index = 0; // foreach (DataRow dr in dt1.Rows) // { // chainIndex = Convert.ToInt32(dr["chainIndex"]); // fromMeter = Convert.ToDouble(dr["fromMeter"]); // toMeter = Convert.ToDouble(dr["toMeter"]); // dkcode = (string)dr["DKCode"]; // dkcode2 = dr["DKCode2"].ToString(); // chainType = Convert.ToInt32(dr["chainType"]); // // isReverse = Convert.ToBoolean(dr["IsReverse"]); // string strOrder = (fromMeter < toMeter) ? "" : " desc "; // string tableName = "sheet" + chainIndex; // dt3 = ExcelWrapper.LoadDataTableFromExcel(dbPath, @"select Mileage, Longitude,Latitude,Altitude from [" + tableName + "$] order by mileage " + strOrder); // count = dt3.Rows.Count; // if (count < 2) continue; // m = new double[count]; // x = new double[count]; // y = new double[count]; // z = new double[count]; // //xMars = new double[count]; // //yMars = new double[count]; // int j = 0; // foreach (DataRow dr2 in dt3.Rows) // { // m[j] = Math.Abs(Convert.ToDouble(dr2["Mileage"]) - fromMeter); // x[j] = Convert.ToDouble(dr2["Longitude"]); // y[j] = Convert.ToDouble(dr2["Latitude"]); // z[j] = Convert.ToDouble(dr2["Altitude"]); // j++; // //xMars[j] = Convert.ToDouble(dr2["LongitudeMars"]); // //yMars[j] = Convert.ToDouble(dr2["LatitudeMars"]); // } // mLineList.Add(new CRailwayLine(chainIndex, dkcode, dkcode2, fromMeter, toMeter, chainType, count, m, x, y, z)); // } // gRealConnection = new MGraph(); // gMileageConnection = new MGraph(); // ShortestPathFloyd.initPathGraph(mLineList, gRealConnection, null); // ShortestPathFloyd.initPathGraph(mLineList, gMileageConnection, connectionList); //} /// <summary> /// 由数据库或远程服务器读入三维中线的数据 /// </summary> /// <param name="fileName"></param> public static void CreateLinelistFromSqlite(string dbPath) { double[] m, x, y, z; double fromMeter, toMeter; //double fromX, toX, fromY, toY; int chainIndex; int chainType; string dkcode, dkcode2; //bool isRight = false; //bool isDouble = true; int count; DataTable dt1, dt2, dt3; //本地sqlite数据库读取里程数据 //#if DEBUG // Helper.LogHelper.WriteLog("SQlite Database" + dbPath); //#endif dt1 = DatabaseWrapper.ExecuteDataTable(dbPath, @"select chainIndex, fromMeter, toMeter,DKCode,DKCode2,chainType from ChainInfo order by chainIndex; "); dt2 = DatabaseWrapper.ExecuteDataTable(dbPath, @"select fromChain, toChain from ExtraConnection; "); //dt1 = ExcelWrapper.LoadDataTableFromExcel(dbPath, @"select chainIndex, fromMeter, toMeter,DKCode,DKCode2,chainType from [ChainInfo$] order by chainIndex"); //dt2 = ExcelWrapper.LoadDataTableFromExcel(dbPath, @"select fromChain, toChain from [ExtraConnection$] "); List <OneConnection> connectionList = new List <OneConnection>(); foreach (DataRow dr in dt2.Rows) { OneConnection aCon = new OneConnection(); aCon.fromIndex = Convert.ToInt32(dr["fromChain"]); aCon.toIndex = Convert.ToInt32(dr["toChain"]); //aCon.isRealConnect = Convert.ToInt32(dr["isConnect"])==0 ? true:false; //aCon.mileageOffset = Convert.ToDouble(dr["mileageOffset"]); connectionList.Add(aCon); } //int index = 0; foreach (DataRow dr in dt1.Rows) { chainIndex = Convert.ToInt32(dr["chainIndex"]); fromMeter = Convert.ToDouble(dr["fromMeter"]); toMeter = Convert.ToDouble(dr["toMeter"]); dkcode = (string)dr["DKCode"]; if (dr["DKCode2"] == null) { dkcode2 = ""; } else { dkcode2 = dr["DKCode2"].ToString(); } chainType = Convert.ToInt32(dr["chainType"]); // isReverse = Convert.ToBoolean(dr["IsReverse"]); string strOrder = (fromMeter < toMeter) ? "" : " desc "; //string tableName = "sheet" + chainIndex; dt3 = DatabaseWrapper.ExecuteDataTable(dbPath, @"select Mileage, Longitude,Latitude,Altitude from MileageInfo where MileagePrefix=" + chainIndex + " order by mileage " + strOrder); count = dt3.Rows.Count; if (count < 2) { continue; } m = new double[count]; x = new double[count]; y = new double[count]; z = new double[count]; //xMars = new double[count]; //yMars = new double[count]; int j = 0; foreach (DataRow dr2 in dt3.Rows) { m[j] = Math.Abs(Convert.ToDouble(dr2["Mileage"]) - fromMeter); x[j] = Convert.ToDouble(dr2["Longitude"]); y[j] = Convert.ToDouble(dr2["Latitude"]); z[j] = Convert.ToDouble(dr2["Altitude"]); j++; //xMars[j] = Convert.ToDouble(dr2["LongitudeMars"]); //yMars[j] = Convert.ToDouble(dr2["LatitudeMars"]); } mLineList.Add(new CRailwayLine(chainIndex, dkcode, dkcode2, fromMeter, toMeter, chainType, count, m, x, y, z)); } gRealConnection = new MGraph(); gMileageConnection = new MGraph(); ShortestPathFloyd.initPathGraph(mLineList, gRealConnection, null); ShortestPathFloyd.initPathGraph(mLineList, gMileageConnection, connectionList); }
public void IndepentBaselineProcess() { //比较计算 // var name = DateTime.Now.ToString("yyyy-MM-dd_HH"); string outPath = Path.GetDirectoryName(files[0]) + "\\" + "baseline.param"; StreamWriter writer = new StreamWriter(outPath); //存储基线 Dictionary <string, Edge> graphRound = new Dictionary <string, Edge>(); int n = DataOfAllSites.Count; mgraph = new MGraph(); mgraph.n = n; mgraph.e = n * (n - 1) / 2; mgraph.vex = new VertexType[n]; mgraph.edges = new int[n][]; road = new Road[n * (n - 1) / 2]; v = new int[n * (n - 1) / 2]; int ii = 0; for (int i = 0; i < n; i++) { SiteEpochSatData dataA = DataOfAllSites.ElementAt(i).Value; mgraph.vex[i] = new VertexType(); mgraph.vex[i].no = i; mgraph.vex[i].name = dataA.SiteName; v[i] = i; //顶点ver[i]初始时表示各在不同的连通分支v[i]中,父结点依次为v[i] for (int j = i + 1; j < n; j++) { mgraph.edges[i] = new int[n]; SiteEpochSatData dataB = DataOfAllSites.ElementAt(j).Value; double toleranceSeccond = 0.5; //限差 单位:秒 int count = 0; for (int k = 0, s = 0; k < dataA.Data.Count && s < dataB.Data.Count; k++, s++) { double diff = Math.Abs(dataA.Data.ElementAt(k).Key - dataB.Data.ElementAt(s).Key); if (diff <= toleranceSeccond) { IEnumerable <int> strA = dataA.EpochSatData.ElementAt(k).Value; IEnumerable <int> strB = dataB.EpochSatData.ElementAt(s).Value; IEnumerable <int> comPrn = strA.Intersect(strB); //bool isIntersected = strA.Intersect(strB).Count() > 0; count += comPrn.Count(); int[] iA = dataA.EpochSatData.ElementAt(k).Value; int[] iB = dataB.EpochSatData.ElementAt(s).Value; int lengthA = iA.Length; int lengthB = iB.Length; int maxLength = Math.Max(lengthA, lengthB); int kk = 0, nn = 0, mm = 0; for (int ss = 0; ss < maxLength; ss++, kk++, nn++) { if (kk >= lengthA) { break; } if (nn >= lengthB) { break; } while (iA[kk] != iB[nn]) { if (iA[kk] > iB[nn]) { nn++; if (nn >= lengthB) { break; } } if (iA[kk] < iB[nn]) { kk++; if (kk >= lengthA) { break; } } } if (kk >= lengthA) { break; } if (nn >= lengthB) { break; } if (iA[kk] == iB[nn]) { mm++; } } if (mm != comPrn.Count()) { // } //foreach (var key in dataA.Data.ElementAt(k).Value) //{ // if (dataB.Data.ElementAt(s).Value.Contains(key)) // { count += 1; } //} } else { if (dataB.Data.ElementAt(s).Key < dataA.Data.ElementAt(k).Key) { while (diff > toleranceSeccond) { s++; if (s >= dataB.Data.Count) { break; } diff = Math.Abs(dataA.Data.ElementAt(k).Key - dataB.Data.ElementAt(s).Key); if (diff <= toleranceSeccond) { IEnumerable <int> strA = dataA.EpochSatData.ElementAt(k).Value; IEnumerable <int> strB = dataB.EpochSatData.ElementAt(s).Value; IEnumerable <int> comPrn = strA.Intersect(strB); count += comPrn.Count(); //foreach (var key in dataA.Data.ElementAt(k).Value) //{ // if (dataB.Data.ElementAt(s).Value.Contains(key)) // { count += 1; } //} break; } } } else if (dataB.Data.ElementAt(s).Key > dataA.Data.ElementAt(k).Key) { while (diff > toleranceSeccond) { k++; if (k >= dataA.Data.Count) { break; } diff = Math.Abs(dataA.Data.ElementAt(k).Key - dataB.Data.ElementAt(s).Key); if (diff <= toleranceSeccond) { IEnumerable <int> strA = dataA.EpochSatData.ElementAt(k).Value; IEnumerable <int> strB = dataB.EpochSatData.ElementAt(s).Value; IEnumerable <int> comPrn = strA.Intersect(strB); count += comPrn.Count(); //foreach (var key in dataA.Data.ElementAt(k).Value) //{ // if (dataB.Data.ElementAt(s).Value.Contains(key)) // { count += 1; } //} break; } } } } } //完成一条基线的统计 mgraph.edges[i][j] = count; road[ii] = new Road(); road[ii].a = i; road[ii].b = j; road[ii].w = count; ii++; //写入 string sb = dataA.SiteName + "-" + dataB.SiteName + " " + count.ToString(); writer.WriteLine(sb); Node NodeA = new Node(); NodeA.Name = dataA.SiteName; NodeA.Visited = false; Node NodeB = new Node(); NodeB.Name = dataB.SiteName; NodeB.Visited = false; Edge edge = new Edge(); edge.NodeA = NodeA; edge.NodeB = NodeB; edge.Weight = count; string baselineName = NodeA.Name + NodeB.Name; graphRound.Add(baselineName, edge); } } //sort List <KeyValuePair <string, Edge> > graphPair = new List <KeyValuePair <string, Edge> >(graphRound); graphPair.Sort(delegate(KeyValuePair <string, Edge> s1, KeyValuePair <string, Edge> s2) { return(s1.Value.Weight.CompareTo(s2.Value.Weight)); }); //存储排序基线 Dictionary <string, Edge> graph = new Dictionary <string, Edge>(); for (int index = graphPair.Count - 1; index >= 0; index--) { var item = graphPair.ElementAt(index); Edge edge = new Edge(); edge.NodeA = item.Value.NodeA; edge.NodeB = item.Value.NodeB; edge.Weight = item.Value.Weight; graph.Add(item.Key, edge); //重新排序 road[mgraph.e - 1 - index].a = DataOfAllSites[item.Value.NodeA.Name].SiteNumber; road[mgraph.e - 1 - index].b = DataOfAllSites[item.Value.NodeB.Name].SiteNumber; road[mgraph.e - 1 - index].w = item.Value.Weight; } //Kruskal GetMinCostSpanTree List <Edge> finded = new List <Edge>(); List <List <Node> > findedNodes = new List <List <Node> >(); findedNodes.Add(new List <Node>() { graph.ElementAt(0).Value.NodeA, graph.ElementAt(0).Value.NodeB }); finded.Add(graph.ElementAt(0).Value); for (int index = 1; index < graph.Count; index++) { var item = graph.ElementAt(index); int i = 0, indexA = -1, indexB = -1; Node nodeA = item.Value.NodeA; Node nodeB = item.Value.NodeB; foreach (var nodes in findedNodes) { foreach (var node in nodes) { if (!item.Value.NodeB.Visited && node.Name == nodeB.Name && node.Visited == nodeB.Visited) { item.Value.NodeB.Visited = true; indexB = i; } if (!item.Value.NodeA.Visited && node.Name == nodeA.Name && node.Visited == nodeA.Visited) { item.Value.NodeA.Visited = true; indexA = i; } } i++; } // if (item.Value.NodeA.Visited && item.Value.NodeB.Visited && (indexA != indexB)) { //连接不同的联通分量,则这两个连通分量可以合并成一个了。 int minId = Math.Min(indexA, indexB); int maxId = Math.Max(indexA, indexB); findedNodes[minId].AddRange(findedNodes[maxId]); findedNodes.RemoveAt(maxId); finded.Add(item.Value); } else if (!item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //都不包含,直接添加新列表 findedNodes.Add(new List <Node>() { nodeA, nodeB }); finded.Add(item.Value); } else if (item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //包含A,则将B添加到A的集合中去 findedNodes[indexA].Add(nodeB); finded.Add(item.Value); } else if (!item.Value.NodeA.Visited && item.Value.NodeB.Visited) { //包含B,则将A添加到B的集合中去 findedNodes[indexB].Add(nodeA); finded.Add(item.Value); } item.Value.NodeA.Visited = false; item.Value.NodeB.Visited = false; } writer.WriteLine("\n"); writer.WriteLine("根据最大观测量选择的独立基线: "); foreach (var item in finded) { string tmp = item.NodeA.Name + "-" + item.NodeB.Name + " " + item.Weight; //写入 writer.WriteLine(tmp); } writer.Close(); //关闭 }
/// <summary> /// 根据路径选择独立基线 /// </summary> /// <param name="coords"></param> /// <returns></returns> private string[] IndepentBaselineProcess(List <NamedXyz> coords) { string[] IndepentBaselinesInfo = new string[coords.Count - 1]; //存储基线 Dictionary <string, Edge> graphRound = new Dictionary <string, Edge>(); int n = coords.Count; mgraph = new MGraph(); mgraph.n = n; mgraph.e = n * (n - 1) / 2; mgraph.vex = new VertexType[n]; mgraph.edges = new double[n][]; road = new Road[n * (n - 1) / 2]; v = new int[n * (n - 1) / 2]; int ii = 0; for (int i = 0; i < n; i++) { // SiteEpochSatData dataA = DataOfAllSites.ElementAt(i).Value; //SiteEpochSatData dataA = DataOfAllSites[i]; NamedXyz dataA = coords[i]; mgraph.vex[i] = new VertexType(); mgraph.vex[i].no = i; mgraph.vex[i].name = dataA.Name; //.SiteName; v[i] = i; //顶点ver[i]初始时表示各在不同的连通分支v[i]中,父结点依次为v[i] for (int j = i + 1; j < n; j++) { mgraph.edges[i] = new double[n]; // SiteEpochSatData dataB = DataOfAllSites[j];//.ElementAt (j).Value; NamedXyz dataB = coords[j]; //double toleranceSeccond = 3.5; //限差 单位:秒 double count = (dataA.Value - dataB.Value).Length; mgraph.edges[i][j] = count; road[ii] = new Road(); road[ii].a = i; road[ii].b = j; road[ii].w = count; ii++; ////写入 //string sb = dataA.SiteName + "-" + dataB.SiteName + " " + count.ToString(); //writer.WriteLine(sb); Node NodeA = new Node(); NodeA.Name = i; NodeA.strName = dataA.Name; NodeA.Visited = false; Node NodeB = new Node(); NodeB.Name = j; NodeB.strName = dataB.Name; NodeB.Visited = false; Edge edge = new Edge(); edge.NodeA = NodeA; edge.NodeB = NodeB; edge.Weight = count; string baselineName = dataA.Name + dataB.Name; graphRound.Add(baselineName, edge); } } //sort 排序 由大到小 List <KeyValuePair <string, Edge> > graphPair = new List <KeyValuePair <string, Edge> >(graphRound); graphPair.Sort(delegate(KeyValuePair <string, Edge> s1, KeyValuePair <string, Edge> s2) { return(s1.Value.Weight.CompareTo(s2.Value.Weight)); }); //存储排序基线 Dictionary <string, Edge> graph = new Dictionary <string, Edge>(); for (int index = 0; index < graphPair.Count; index++) { var item = graphPair.ElementAt(index); Edge edge = new Edge(); edge.NodeA = item.Value.NodeA; edge.NodeB = item.Value.NodeB; edge.Weight = item.Value.Weight; graph.Add(item.Key, edge); //重新排序 road[mgraph.e - 1 - index].a = item.Value.NodeA.Name; road[mgraph.e - 1 - index].b = item.Value.NodeB.Name; road[mgraph.e - 1 - index].w = item.Value.Weight; } //根据Kruskal算法生成最小生成树 GetMinCostSpanTree List <Edge> findedMinCostSpanTree = new List <Edge>(); List <List <Node> > findedNodes = new List <List <Node> >(); findedNodes.Add(new List <Node>() { graph.ElementAt(0).Value.NodeA, graph.ElementAt(0).Value.NodeB }); findedMinCostSpanTree.Add(graph.ElementAt(0).Value); for (int index = 1; index < graph.Count; index++) { var item = graph.ElementAt(index); int i = 0, indexA = -1, indexB = -1; Node nodeA = item.Value.NodeA; Node nodeB = item.Value.NodeB; foreach (var nodes in findedNodes) { foreach (var node in nodes) { if (!item.Value.NodeB.Visited && node.Name == nodeB.Name && node.Visited == nodeB.Visited) { item.Value.NodeB.Visited = true; indexB = i; } if (!item.Value.NodeA.Visited && node.Name == nodeA.Name && node.Visited == nodeA.Visited) { item.Value.NodeA.Visited = true; indexA = i; } } i++; } // if (item.Value.NodeA.Visited && item.Value.NodeB.Visited && (indexA != indexB)) { //连接不同的联通分量,则这两个连通分量可以合并成一个了。 int minId = Math.Min(indexA, indexB); int maxId = Math.Max(indexA, indexB); findedNodes[minId].AddRange(findedNodes[maxId]); findedNodes.RemoveAt(maxId); findedMinCostSpanTree.Add(item.Value); } else if (!item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //都不包含,直接添加新列表 findedNodes.Add(new List <Node>() { nodeA, nodeB }); findedMinCostSpanTree.Add(item.Value); } else if (item.Value.NodeA.Visited && !item.Value.NodeB.Visited) { //包含A,则将B添加到A的集合中去 findedNodes[indexA].Add(nodeB); findedMinCostSpanTree.Add(item.Value); } else if (!item.Value.NodeA.Visited && item.Value.NodeB.Visited) { //包含B,则将A添加到B的集合中去 findedNodes[indexB].Add(nodeA); findedMinCostSpanTree.Add(item.Value); } item.Value.NodeA.Visited = false; item.Value.NodeB.Visited = false; } //writer.WriteLine("\n"); //writer.WriteLine("根据最大观测量选择的独立基线: "); int jj = 0; foreach (var item in findedMinCostSpanTree) { string tmp = item.NodeA.strName + "-" + item.NodeB.strName;// + " " + key.Weight; IndepentBaselinesInfo[jj] = tmp; //写入 //writer.WriteLine(tmp); jj++; } //writer.Close(); //关闭 return(IndepentBaselinesInfo); }