private ControlRecordList GenCrl(FileInfo crFile, PrimalArrivalList pal) { ControlRecordList crl = new ControlRecordList(); //c#文件流读文件 using (FileStream fsRead = crFile.OpenRead()) { StreamReader sr = new StreamReader(fsRead, System.Text.Encoding.Default); //读文件头 string s = sr.ReadLine(); s = s.Substring(1, s.Length - 2); string[] ss = s.Split(new char[] { ',', '=' }); if (ss[0] == "ID") { crl.PAL = pal; } while (!sr.EndOfStream) { s = sr.ReadLine(); ss = s.Split(new char[] { ';' }); ControlRecord controlRecord = new ControlRecord(); controlRecord.Time = Convert.ToInt32(ss[0]); controlRecord.Pro = this.ProSpace.FirstOrDefault(i => i.ProID == Convert.ToInt32(ss[1])); controlRecord.Sta = ss[2] == "1" ? ProductState.Open : ProductState.Closed; crl.Add(controlRecord); } } return(crl); }
//public PrimalArrivalList Gen(int id) //{ // PrimalArrivalList arr = new PrimalArrivalList() { PAListID = id }; // for (int t = 0; t < TimeHorizon;) // { // double u1 = rng.NextDouble(); // double u2 = rng.NextDouble(); // t = (int)Math.Ceiling(t - (1 / MaxLamada) * Math.Log(u1));//这里向上取整,可能会影响结果。 // if ((u2 <= (MarketInfo.Ro(t) / MaxLamada)) && t < TimeHorizon) // { // double u3 = rng.NextDouble(); // PrimalArrival temp = new PrimalArrival() // { // ArriveTime = t, // IndexOfMS = rollMS(t, u3).MSID // }; // arr.Add(temp); // } // } // return arr; //} public PrimalArrivalList Gen(int id) { PrimalArrivalList arr = new PrimalArrivalList() { PAListID = id, TimeHorizon = TimeHorizon }; for (int t = 0; t < TimeHorizon; t++) { double u1 = rng.NextDouble(); double u2 = rng.NextDouble(); double u3 = rng.NextDouble(); //t = (int)Math.Ceiling(t - (1 / MaxLamada) * Math.Log(u1));//这里向上取整,可能会影响结果。 //if ((u2 <= (MarketInfo.Ro(t) / MaxLamada)) && t < TimeHorizon) if (u1 < MarketInfo.Ro(t)) { //double u3 = rng.NextDouble(); PrimalArrival temp = new PrimalArrival() { ArriveTime = t, IndexOfMS = rollMS(t, u2).MSID, ChoosingParam = u3 }; arr.Add(temp); } } return(arr); }
private SellingRecordList GenSrl(FileInfo srFile, PrimalArrivalList pal) { SellingRecordList srl = new SellingRecordList(); //c#文件流读文件 using (FileStream fsRead = srFile.OpenRead()) { StreamReader sr = new StreamReader(fsRead, System.Text.Encoding.Default); //读文件头 string s = sr.ReadLine(); s = s.Substring(1, s.Length - 2); string[] ss = s.Split(new char[] { ',', '=' }); if (ss[0] == "ID") { srl.PAL = pal; } while (!sr.EndOfStream) { s = sr.ReadLine(); ss = s.Split(new char[] { ';' }); string[] sss = ss[1].Split(new char[] { ',' }); SellingRecord sellrecord = new SellingRecord(); sellrecord.Product = new List <IProduct>(); sellrecord.ArriveTime = Convert.ToInt32(ss[0]); foreach (string ssss in sss) { sellrecord.Product.Add(this.ProSpace.FirstOrDefault(i => i.ProID == Convert.ToInt32(ssss))); } srl.Add(sellrecord); } } return(srl); }
public void ReadXml(XmlReader reader) { XmlSerializer serializer = new XmlSerializer(typeof(PrimalArrivalList)); reader.ReadStartElement("PrimalArrivalData"); while (reader.NodeType != XmlNodeType.EndElement) { PrimalArrivalList sdi = serializer.Deserialize(reader) as PrimalArrivalList; this.Data.Add(sdi); } reader.ReadEndElement(); }
}//输出 #endregion public void BatchProcess(PrimalArrivalData Pad, string path) { //判断文件路径是否存在,不存在则创建文件夹 if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path);//不存在就创建目录 } XmlTextWriter writer1 = new XmlTextWriter(path + "SellingRecord.xml", null); XmlTextWriter writer2 = new XmlTextWriter(path + "ControlRecord.xml", null); //写入根元素 writer1.WriteStartElement("SellingRecordData"); writer2.WriteStartElement("ControlRecordData"); //TODO 限制任务数量 while (Pad.ReadNextPAL()) { PrimalArrivalList pal = Pad.GetCurPAL(); Task ta = factory.StartNew(() => { SellingRecordList Srlist; ControlRecordList Crlist; Process(pal, out Srlist, out Crlist); lock (writer1) { Srlist.WritetoXml(writer1); } lock (writer2) { Crlist.WritetoXml(writer2); } //SimData.CRD.Add(Crlist); //SimData.SRD.Add(Srlist); }, cts.Token); tasks.Add(ta); if (tasks.Count >= 128) { Task.WaitAll(tasks.ToArray()); tasks.Clear(); } } Task.WaitAll(tasks.ToArray()); tasks.Clear(); writer1.WriteEndElement(); writer2.WriteEndElement(); //将XML写入文件并且关闭XmlTextWriter writer1.Close(); writer2.Close(); }
public bool Process(PrimalArrivalList arr, out SellingRecordList Srlist, out ControlRecordList Crlist) { //初始化记录 Srlist = new SellingRecordList() { PAL = arr }; Crlist = new ControlRecordList() { PAL = arr }; //初始化资源 MetaResouceState rs = new MetaResouceState(InitState); //生成动态策略 IConOL conol = Controller.GenConOL(); if (conol != null) { conol.Update(rs); } List <IProduct> openProductList = Controller.OpenProductList(0, rs, conol);//初始开放产品 Crlist.UpdateOpenProducts(0, openProductList); for (int i = 0; i < arr.Count; i++)//顺序读取到达列表 { //生成开放产品集 openProductList = Controller.OpenProductList(arr[i].ArriveTime, rs, conol); //模拟旅客购票 List <IProduct> pro = (MarketInfo[arr[i].IndexOfMS] as IChoiceAgent).Select(openProductList, arr[i].ChoosingParam); if (pro == null) { continue; } //出票 List <Ticket> tickets = Controller.PrintTickets(rs, pro, conol); //更新资源状态 rs.UpdateAfterSelling(tickets); if (conol != null) { conol.Update(rs); } //记录产品情况 Crlist.UpdateOpenProducts(arr[i].ArriveTime, openProductList); //加入SellingRecordList Srlist.AddRecord(arr[i].ArriveTime, arr[i], pro, tickets); } return(true); }
public void BatchProcess(string inputDir, string outputDir) { DirectoryInfo folder = new DirectoryInfo(inputDir); int n = 0; int total = folder.GetFiles("*.arr").Length; int step = (int)Math.Ceiling((double)folder.GetFiles("*.arr").Length / 10000); Console.Write("进度 000.00%"); foreach (FileInfo f in folder.GetFiles("*.arr")) { FileInfo file = f;//防止在VS2010下报错 PrimalArrivalList pal = new PrimalArrivalList(); pal.ReadFromArrFile(file.FullName); Task ta = factory.StartNew(() => { SellingRecordList Srlist; ControlRecordList Crlist; Process(pal, out Srlist, out Crlist); //判断文件路径是否存在,不存在则创建文件夹 if (!System.IO.Directory.Exists(outputDir + @"\Sell")) { System.IO.Directory.CreateDirectory(outputDir + @"\Sell");//不存在就创建目录 } Srlist.WriteToFile(outputDir + @"\Sell\" + pal.PAListID + ".sr"); if (!System.IO.Directory.Exists(outputDir + @"\Control")) { System.IO.Directory.CreateDirectory(outputDir + @"\Control");//不存在就创建目录 } Crlist.WriteToFile(outputDir + @"\Control\" + pal.PAListID + ".cr"); if (n++ % step == 0) { lock (folder) { Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.Write("{0}%", String.Format("{0:000.00}", Math.Round(((double)n / (double)total), 4) * 100)); } } }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.WriteLine("100.00 % 完成!"); }
public PrimalArrivalList GetCurPAL() { PrimalArrivalList pal = new PrimalArrivalList(); pal.PAListID = Convert.ToInt32(_reader.GetAttribute("ID")); while (_reader.Read()) { if (_reader.NodeType == XmlNodeType.Element && _reader.Name == "PrimalArrival") { pal.Add(new PrimalArrival() { ArriveTime = Convert.ToInt32(_reader.GetAttribute("Time")), IndexOfMS = Convert.ToInt32(_reader.GetAttribute("MS")) }); } else if (_reader.NodeType == XmlNodeType.Element && _reader.Name == "PrimalArrivalList") { break; } } return(pal); }
public void Dowork(string arrpath, string srpath, string ctlpath, string outpupath) { DirectoryInfo arrFolder = new DirectoryInfo(arrpath); DirectoryInfo srFolder = new DirectoryInfo(srpath); DirectoryInfo ctlFolder = new DirectoryInfo(ctlpath); FileInfo[] sr = srFolder.GetFiles(); FileInfo[] ctl = ctlFolder.GetFiles(); Dictionary <SimStatic, double> dic = new Dictionary <SimStatic, double>(); Dictionary <SimStatic, List <double> > listDic = new Dictionary <SimStatic, List <double> >(); foreach (SimStatic s in IndexList) { if (s.Cal != null) { dic.Add(s, 0); } else if (s.TCal != null) { listDic.Add(s, new List <double>()); } } int n = 0; int total = arrFolder.GetFiles("*.arr").Length; int step = (int)Math.Ceiling((double)arrFolder.GetFiles("*.arr").Length / 10000); Console.Write("进度 000.00%"); foreach (FileInfo file in arrFolder.GetFiles("*.arr")) { Task ta = factory.StartNew(() => { PrimalArrivalList pal = new PrimalArrivalList(); pal.ReadFromArrFile(file.FullName); FileInfo srFile = sr.FirstOrDefault(i => i.Name == file.Name.Substring(0, file.Name.IndexOf('.')) + ".sr"); FileInfo ctlFile = ctl.FirstOrDefault(i => i.Name == file.Name.Substring(0, file.Name.IndexOf('.')) + ".cr"); ControlRecordList crl = GenCrl(ctlFile, pal); SellingRecordList srl = GenSrl(srFile, pal); lock (dic) { foreach (SimStatic s in IndexList) { if (s.Cal != null) { dic[s] += s.Cal(pal, srl, crl); } else if (s.TCal != null) { List <double> temp = s.TCal(pal, srl, crl); for (int i = 0; i < temp.Count; i++) { if (listDic[s].Count <= i) { listDic[s].Add(0); } listDic[s][i] += temp[i]; } } } } if (n++ % step == 0) { lock (arrFolder) { Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.Write("{0}%", String.Format("{0:000.00}", Math.Round(((double)n / (double)total), 4) * 100)); } } }, cts.Token); tasks.Add(ta); } Task.WaitAll(tasks.ToArray()); tasks.Clear(); Console.SetCursorPosition(Console.CursorLeft - 7, Console.CursorTop); Console.WriteLine("100.00 % 完成!"); //输出 StreamWriter sw = new StreamWriter(outpupath, true); sw.Write("\r\n" + arrFolder.FullName + ","); foreach (SimStatic s in IndexList) { if (s.Cal != null) { if (s.IsAvg) { dic[s] = dic[s] / (double)total; } if (IndexList.IndexOf(s) < IndexList.Count - 1) { sw.Write("{0},", dic[s]); } else { sw.Write("{0}", dic[s]); } } else if (s.TCal != null) { if (s.IsAvg) { for (int i = 0; i < listDic[s].Count; i++) { listDic[s][i] = listDic[s][i] / (double)total; } } sw.WriteLine(); sw.WriteLine(s.Name); for (int i = 0; i < listDic[s].Count; i++) { sw.WriteLine("{0},", listDic[s][i]); } } } sw.Close(); }