private void Begin_Click(object sender, EventArgs e) { Stopwatch sw = new Stopwatch(); sw.Start(); var fullUri = string.Empty; if (string.IsNullOrWhiteSpace(PathBox.Text)) { fullUri = string.Format($"{Environment.CurrentDirectory}\\Data\\OD.xlsx"); } else { fullUri = PathBox.Text; } messageBox.Text += string.Format($"开始计算,文件路径为{fullUri}\r\n"); var result = ReadExcel.LuDuan(fullUri); result = result.OrderBy(l => l.No).ToList(); var luduans = ReadExcel.LuduanAndPoint(result, fullUri); messageBox.Text += string.Format($"导入路段完成,路段数{luduans.Count}\r\n"); var nodes = ReadExcel.Nodes(fullUri); messageBox.Text += string.Format($"导入节点完成,节点数{nodes.Count}\r\n"); var ods = ReadExcel.OD(fullUri); messageBox.Text += string.Format($"导入OD完成,OD对数{ods.Count}\r\n"); ReadExcel.Varia(fullUri); messageBox.Text += string.Format($"导入参数完成\t\n"); messageBox.Text += string.Format($"{nameof(Varias.LuJingCount)}:{Varias.LuJingCount}\r\n"); foreach (var od in ods) { od.LuJings = GenarateLuJing.GetAllPath(od, luduans, nodes); foreach (var lujing in od.LuJings) //添加路段所在路径信息 { foreach (var luduan in lujing.LuDuans) { if (luduan.No != 0) { if (luduans.NumOf(luduan.No).At == null) { luduans.NumOf(luduan.No).At = new List <LuJing>(); } if (!luduans.NumOf(luduan.No).At.Any(l => l.start.No == lujing.Nodes.First().No&& l.end.No == lujing.Nodes.Last().No)) { luduans.NumOf(luduan.No).At.Add(lujing); } } } } } var uri = string.Format($"{Environment.CurrentDirectory}"); Iteration.Run(ods, luduans, nodes, uri); sw.Stop(); messageBox.Text += string.Format($"任务完成!耗时{sw.ElapsedMilliseconds / 1000}秒"); }
public void Running() { var fullUri = string.Format($"{Environment.CurrentDirectory}\\OD.xlsx"); var result = ReadExcel.LuDuan(fullUri); result = result.OrderBy(l => l.No).ToList(); var luduans = ReadExcel.LuduanAndPoint(result, fullUri); var nodes = ReadExcel.Nodes(fullUri); var ods = ReadExcel.OD(fullUri); ReadExcel.Varia(fullUri); foreach (var od in ods) { od.LuJings = GenarateLuJing.GetAllPath(od, luduans, nodes); foreach (var lujing in od.LuJings) //添加路段所在路径信息 { foreach (var luduan in lujing.LuDuans) { if (luduan.No != 0) { if (luduans.NumOf(luduan.No).At == null) { luduans.NumOf(luduan.No).At = new List <LuJing>(); } if (!luduans.NumOf(luduan.No).At.Any(l => l.start.No == lujing.Nodes.First().No&& l.end.No == lujing.Nodes.Last().No)) { luduans.NumOf(luduan.No).At.Add(lujing); } } } } } var uri = string.Format($"{Environment.CurrentDirectory}"); Iteration.Run(ods, luduans, nodes, uri); }
public void GroupRun() { var uri = string.Format($"{Environment.CurrentDirectory}"); var fullUri = string.Format($"{Environment.CurrentDirectory}\\OD.xlsx"); var groups = new List <Group>(); Varias.GroupNo = 0; ReadExcel.Varia(fullUri); var result = ReadExcel.LuDuan(fullUri); result = result.OrderBy(l => l.No).ToList(); var luduans = ReadExcel.LuduanAndPoint(result, fullUri); var nodes = ReadExcel.Nodes(fullUri); var ods = ReadExcel.OD(fullUri); #region 产生种群 for (int i = 0; i < Varias.M; i++) { foreach (var od in ods) { od.LuJings = GenarateLuJing.GetAllPath(od, luduans, nodes); foreach (var lujing in od.LuJings) //添加路段所在路径信息 { foreach (var luduan in lujing.LuDuans) { if (luduan.No != 0) { if (luduans.NumOf(luduan.No).At == null) { luduans.NumOf(luduan.No).At = new List <LuJing>(); } if (!luduans.NumOf(luduan.No).At.Any(l => l.start.No == lujing.Nodes.First().No&& l.end.No == lujing.Nodes.Last().No)) { luduans.NumOf(luduan.No).At.Add(lujing); } } } } } var group = new Group { No = Varias.GroupNo, Luduans = luduans, Ods = ods }; foreach (var ld in group.Luduans) { ld.F = Randam.F; } groups.Add(group); Varias.GroupNo += 1; } #endregion #region 循环 foreach (var group in groups) { Varias.IsGA = true; group.Result = Iteration.Run(group.Ods, group.Luduans, ReadExcel.Nodes(fullUri), uri); } var mins = new List <double>(); for (int i = 0; i < Varias.T; i++) { var chosenGroup = Randam.Roulette(groups); var ODs = ReadExcel.OD(fullUri); var children = GeneticAlgorithm.Children(groups, ODs, luduans, nodes); foreach (var child in children) { child.Result = Iteration.Run(child.Ods, child.Luduans, ReadExcel.Nodes(fullUri), uri); } var maxGroup = groups.OrderBy(g => g.Result).Take(Varias.M - (int)Math.Round(Varias.M * Varias.Pc)).ToList(); children.AddRange(maxGroup); children = GeneticAlgorithm.CalculateFitness(groups); mins.Add(children.Min(c => c.Result)); } #endregion }