public CaminhoModel ExecutarDijkstra(DijkstraModel input) { DijkstraUtil util = new DijkstraUtil(); GrafoModel grafo = util.MontaGrafo(null); Dijkstra dijkstra = new Dijkstra(grafo); // executa o algoritmo de Dijkstra dijkstra.Executa(grafo.Vertices[input.Origem]); // extrai o menor caminho List <VerticeModel> menorCaminho = dijkstra.BuscaCaminho(grafo.Vertices[input.Destino]); if (menorCaminho == null || menorCaminho.Count <= 0) { return(null); } //monta string para buscar a distancia do percurso string caminho = ""; foreach (VerticeModel vertice in menorCaminho) { caminho += "_" + vertice.ToString().Replace("Nodo_", ""); } grafo = util.MontaGrafo(caminho.Substring(1)); dijkstra = new Dijkstra(grafo); int distanciaPercurso = dijkstra.RetornaDistanciaTrajeto(); return(new CaminhoModel(menorCaminho, distanciaPercurso)); }
public int PrimeiraQuestao(string caminho) { DijkstraUtil util = new DijkstraUtil(); GrafoModel grafo = util.MontaGrafo(caminho); Dijkstra dijkstra = new Dijkstra(grafo); return(dijkstra.RetornaDistanciaTrajeto()); }
/// <summary> /// 检查已找到的逻辑导通条件是否成立 /// </summary> public void CheckCondition() { AppProject pro = AppProject.GetInstance(); /*将所有条件注入点相关的所有回路都添加到relativeBrs集合中*/ List <TestBranch> relativeBrs = new List <TestBranch>(); foreach (var lp in loops) { BranchNet net = pro.LoopNets.Nets.FirstOrDefault(p => p.Branches.Contains(lp)); net.Branches.ForEach(p => { if (!relativeBrs.Contains(p)) { relativeBrs.Add(p); } }); } /*初始化所有节点的HasChanged属性为false * 触点没有发生动作*/ pro.Nodes.ForEach(p => p.HasChanged = false); /*将条件支路中得电的线圈对应的触点标记为动作*/ var query = pro.Nodes.Where(p => includedCoils.Contains(p.Part) && (p.TNType == TerminalType.ContactNormalOpen || p.TNType == TerminalType.ContactNormalClose)); foreach (var nd in query) { TNode another = nd.GetAnother(); if (another != null) { nd.HasChanged = true; another.HasChanged = true; } } /*将所有通电的线圈的触点标记为动作*/ List <TestBranch> rstBrs = new List <TestBranch>(); CheckCondition(relativeBrs, rstBrs); /*最后检查一下loops里的支路是否依然满足导通的条件*/ bool success = true;//没有问题 rstBrs.ForEach(p => loops.Add(p)); loops.Add(branches[0]);//将被测支路一起加入导通支路里检查 int[,] weight = new int[pro.Nodes.Count, pro.Nodes.Count]; Array.Copy(pro.weight, weight, weight.Length);//拷贝权重矩阵 foreach (var br in loops) { var noQuery = br.Branch.Where(p => p.TNType == TerminalType.ContactNormalOpen); var ncQuery = br.Branch.Where(p => p.TNType == TerminalType.ContactNormalClose); foreach (var no in noQuery) { TNode another = no.GetAnother(); if (br.Branch.Contains(another)) { if (!no.HasChanged) { success = false; weight[no.index, another.index] = 0; weight[another.index, no.index] = 0; } } } if (success) { foreach (var nc in ncQuery) { TNode another = nc.GetAnother(); if (br.Branch.Contains(another)) { if (nc.HasChanged) { success = false; weight[nc.index, another.index] = Dijkstra.infinite; weight[another.index, nc.index] = Dijkstra.infinite; } } } } //判断回路中是否可能存在短路情况 if (success) { Dijkstra dist = new Dijkstra(weight, br.Branch[0].index); List <TNode> negative = pro.GetSetTerminal(p => p.Type == NamesManage.Negative); success = !DijkstraUtil.hasShortCircuit(negative, dist); } if (!success) { break; } } loops.Remove(branches[0]);//检查完毕后从条件支路中移除 CanTest = success; if (!success) { loops.Clear(); includedCoils.Clear(); Debug.WriteLine("失败解析:{0}", Num); } else { Debug.WriteLine("成功解析:{0}", Num); kk += branches.Count; Debug.WriteLine(kk); Debug.WriteLine("条件支路个数:{0}", loops.Count); } }