|
Posted on
2007-06-18 04:45
ROCk_IE
阅读( 1150)
评论()
编辑
收藏
举报
  1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace CircleTrans 10  { 11 public partial class Simulation_Round_About : Form 12 { 13 14 /**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 15 /// <summary> 16 /// 定义全局变量 17 /// </summary> 18 /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 19 20 int timeRecord = 0;//全局变量,纪录仿真时间 21 int totalCarNo = 0;//记录进入系统中的车的数目 22 Random r = new Random();//公共的随机数生成器 23 24 //统计变量 25 double[] statisticFlow = { 0, 0, 0, 0, 0 }; 26 int statisticCarOut = 0; 27 double statisticStayTime = 0; 28 29 //记录下个时刻各个节点的状态,在下个时刻初始时向各节点赋值。 30 //初始化环岛节点和进入环岛路段的节点,环岛划为24个节点。进入路段各划为三个节点。 31 Node[] circleNodes = new Node[25]; 32 Node[] lineNodes = new Node[13]; 33 Node[] tempCircleNodes = new Node[25]; 34 Node[] tempLineNodes = new Node[13]; 35 36 //创建excel文档,存储仿真数据 37 Excel.Application xlApp; 38 Excel.Workbook xlWorkbook = null; 39 Excel.Worksheet[] xlWorksheets=new Excel.Worksheet[3]; 40 int[] count = { 2, 2, 2 }; 41 string[] writeToExcelString=new string [38]; 42 43 /**////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 44 45 46 47 48 49 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 50 /// <summary> 51 /// 主体函数编写 52 /// </summary> 53 /// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// 54 public Simulation_Round_About() 55 { 56 InitializeComponent(); 57 58 for (int i = 1; i <= 12; i++)//初始化节点 59 { 60 circleNodes[2 * i - 1] = new Node(); 61 circleNodes[2 * i] = new Node(); 62 lineNodes[i] = new Node(); 63 tempCircleNodes[2 * i - 1] = new Node(); 64 tempCircleNodes[2 * i] = new Node(); 65 tempLineNodes[i] = new Node(); 66 } 67 68 CreateOrOpenExcelFile();//创建或者打开excel文件,记录数据 69 } 70 71 72 73 74 private void Simulation_Round_About_FormClosed(object sender, FormClosedEventArgs e) 75 { 76 CloseExcelApp();//窗体关闭事件 77 } 78 79 80 81 private void Form1_Load(object sender, EventArgs e) 82 { 83 84 } 85 86 87 88 private void timerInterval_Tick(object sender, EventArgs e) 89 { 90 timeRecord += 1; 91 this.labeltime.Text = timeRecord.ToString(); 92 93 //Statistics(60);//每隔60秒进行一次数据统计,该统计只统计最近间隔时间内。 94 StatisticsLong (60);//每隔60秒进行一次数据统计,该统计从起始时刻到现在。 95 96 97 GenerateCar();//在四个入口处产生到达的车辆 98 99 ChangeCircle();//更新环岛上节点下一个时刻的状态,存到tempCircleNode里面。 100 101 ChangeLine();//更新入口节点下一个时刻的状态,存到tempLineCircle里面。 102 103 for (int i = 1; i <= 12; i++)//更新下个时刻的所有节点 104 { 105 NodeFuZhi(circleNodes[2 * i - 1], tempCircleNodes[2 * i - 1]); 106 NodeFuZhi(circleNodes[2 * i], tempCircleNodes[2 * i]); 107 NodeFuZhi(lineNodes[i], tempLineNodes[i]); 108 } 109 110 ChangeTotalNodes();//更新下个时刻的所有节点显示 111 112 GenerateTransLog();//将该时刻的交通状态记录到excel文件 113 } 114 115 116 117 public void GenerateTransLog() 118 {//将该时刻的交通状态记录到excel文件 119 writeToExcelString[0] = timeRecord.ToString(); 120 for (int i = 1; i <= 24; i++) 121 { 122 if (circleNodes[i].getHasCar().Equals(true)) 123 { 124 writeToExcelString[i] = circleNodes[i].getCar().getCarNo().ToString(); 125 } 126 else 127 { 128 writeToExcelString[i] = ""; 129 } 130 } 131 for (int i = 1; i <= 12; i++) 132 { 133 if (lineNodes[i].getHasCar().Equals(true)) 134 { 135 writeToExcelString[i + 24] = lineNodes[i].getCar().getCarNo().ToString(); 136 } 137 else 138 { 139 writeToExcelString[i + 24] = ""; 140 } 141 } 142 xlWorksheets[0].get_Range(xlWorksheets[0].Cells[count[0], 1], xlWorksheets[0].Cells[count[0], 37]).Value2 = writeToExcelString; 143 count[0] += 1; 144 } 145 146 147 148 149 public void Statistics(int intervalTime) 150 { 151 for (int i = 1; i <= 4; i++) 152 { 153 if (circleNodes[(i - 1) * 6 + 2].getHasCar().Equals(true) && circleNodes[(i - 1) * 6 + 3].getHasCar().Equals(false)) 154 { //如果该时刻统计地点上一个路口有车,而统计地点没车,则将统计数据加1(因为下个时刻将会有车经过,故流量加1) 155 statisticFlow[i] += 1; 156 } 157 } 158 159 if (timeRecord % intervalTime == 0)//如果统计时刻到达 160 { 161 //更新统计数据 162 this.labelFloSta1.Text = Math.Round(3600 * statisticFlow[1] / (double)intervalTime, 0)+"/hr"; 163 this.labelFloSta2.Text = Math.Round(3600 * statisticFlow[2] / (double)intervalTime, 0)+"/hr"; 164 this.labelFloSta3.Text = Math.Round(3600 * statisticFlow[3] / (double)intervalTime, 0)+"/hr"; 165 this.labelFloSta4.Text = Math.Round(3600 * statisticFlow[4] / (double)intervalTime, 0)+"/hr"; 166 if (statisticCarOut == 0)//如果这段时间内没有车离开系统 167 { 168 this.labelStaAverageStayRate.Text = "统计时间内无车离开"; 169 } 170 else 171 { 172 this.labelStaAverageStayRate.Text = "平均等待:" + Math.Round(statisticStayTime / (double)statisticCarOut, 3); 173 } 174 //统计数据清空,进入下次统计 175 statisticFlow[1] = 0; 176 statisticFlow[2] = 0; 177 statisticFlow[3] = 0; 178 statisticFlow[4] = 0; 179 statisticCarOut = 0; 180 statisticStayTime = 0; 181 //记录框清空 182 this.textBoxInput.Text = ""; 183 this.textBoxOutput.Text = ""; 184 } 185 } 186 187 188 189 190 public void StatisticsLong(int intervalTime) 191 { 192 for (int i = 1; i <= 4; i++) 193 { 194 if (circleNodes[(i - 1) * 6 + 2].getHasCar().Equals(true) && circleNodes[(i - 1) * 6 + 3].getHasCar().Equals(false)) 195 { //如果该时刻统计地点上一个路口有车,而统计地点没车,则将统计数据加1(因为下个时刻将会有车经过,故流量加1) 196 statisticFlow[i] += 1; 197 } 198 } 199 200 if (timeRecord % intervalTime == 0)//如果统计时刻到达 201 { 202 //更新统计数据 203 this.labelFloSta1.Text = Math.Round(3600 * statisticFlow[1] / (double)timeRecord , 0) + "/hr"; 204 this.labelFloSta2.Text = Math.Round(3600 * statisticFlow[2] / (double)timeRecord , 0) + "/hr"; 205 this.labelFloSta3.Text = Math.Round(3600 * statisticFlow[3] / (double)timeRecord , 0) + "/hr"; 206 this.labelFloSta4.Text = Math.Round(3600 * statisticFlow[4] / (double)timeRecord , 0) + "/hr"; 207 if (statisticCarOut == 0)//如果这段时间内没有车离开系统 208 { 209 this.labelStaAverageStayRate.Text = "统计时间内无车离开"; 210 } 211 else 212 { 213 this.labelStaAverageStayRate.Text = "平均等待:" + Math.Round(statisticStayTime / (double)statisticCarOut, 3); 214 } 215 216 //记录框清空 217 this.textBoxInput.Text = ""; 218 this.textBoxOutput.Text = ""; 219 } 220 } 221 222 223 224 225 public void GenerateCar() 226 { 227 double[] rates = new double[5]; 228 rates[1] = (double)trackBarRate1.Value / 20; 229 rates[2] = (double)trackBarRate2.Value / 20; 230 rates[3] = (double)trackBarRate3.Value / 20; 231 rates[4] = (double)trackBarRate4.Value / 20; 232 233 234 //在四个入口处生成车辆的函数 235 for (int i = 1; i <= 4; i++) 236 { 237 238 if (lineNodes[(3 * i - 2)].getHasCar().Equals(false)) //该入口路径最前端没车时,将会按照possion到达产生车辆,进入队列。 239 { 240 //double rr = exp(rates[i]); 241 if (r.NextDouble() <= rates[i])//产生随机数,判断车辆是否到达,如果到达车辆,则设置端口节点有车。 242 { 243 Car newCar = new Car(); 244 245 totalCarNo += 1; 246 247 newCar.setCarNo(totalCarNo); 248 newCar.setInNode(i); 249 newCar.setOutNode(r.Next(1, 5)); 250 newCar.setNodeNo((((newCar.getOutNode() - newCar.getInNode() + 4) % 4 == 0) ? 4 : (newCar.getOutNode() - newCar.getInNode() + 4) % 4) * 6+3); 251 newCar.setRemainNodeNo(newCar.getNodeNo()-3); 252 newCar.setInTime(timeRecord); 253 newCar.setCarImg(newCar.getOutNode()); 254 255 this.textBoxInput.Text += timeRecord .ToString() + "时刻," + newCar.getCarNo ().ToString() + "号车从" +newCar .getInNode ().ToString() +"进,将从"+newCar .getOutNode ().ToString ()+"出"+ System.Environment.NewLine; 256 257 writeToExcelString[0] = timeRecord.ToString(); 258 writeToExcelString[1] = newCar.getCarNo().ToString(); 259 writeToExcelString[2] = newCar.getInNode().ToString(); 260 writeToExcelString[3] = newCar.getOutNode().ToString(); 261 writeToExcelString[4] = newCar.getNodeNo().ToString(); 262 xlWorksheets[1].get_Range(xlWorksheets[1].Cells[count[1], 1], xlWorksheets[1].Cells[count[1], 5]).Value2 = writeToExcelString; 263 count[1] += 1; 264 265 lineNodes[3 * i - 2].setHasCar(true); 266 lineNodes[3 * i - 2].setCar(newCar); 267 } 268 } 269 } 270 } 271 272 273 274 275 public void ChangeCircle() 276 { 277 //为下个时刻环线上的节点改变状态 278 for (int i = 1; i <= 23; i++)//初始化节点 279 { 280 if (circleNodes[i].getHasCar().Equals(true) && circleNodes[i].getCar().getRemainNodeNo() == 0)//如果有车而且车将要出环岛 281 { 282 tempCircleNodes[i].setHasCar(false); 283 NodeFuZhi(tempCircleNodes[i + 1], circleNodes[i + 1]); 284 circleNodes[i].getCar().setOutTime(timeRecord);//离开环岛的时间 285 286 //记录离开的车辆 287 this.textBoxOutput.Text += timeRecord.ToString() + "时刻," + circleNodes[i].getCar().getCarNo().ToString() + "号车经过" + circleNodes[i].getCar().getNodeNo ().ToString() + "站从" + circleNodes[i].getCar().getOutNode().ToString() + "离开,花了" + (circleNodes[i].getCar().getOutTime() - circleNodes[i].getCar().getInTime()).ToString() + "秒" + System.Environment.NewLine; 288 //统计离开车辆的等待时间 289 statisticCarOut += 1; 290 statisticStayTime += (double)(circleNodes[i].getCar().getOutTime() - circleNodes[i].getCar().getInTime()) / (double )circleNodes[i].getCar().getNodeNo(); 291 292 //将离开的车记录到excel文件 293 writeToExcelString[0] = timeRecord.ToString(); 294 writeToExcelString[1] = circleNodes[i].getCar().getCarNo().ToString(); 295 writeToExcelString[2] = circleNodes[i].getCar().getInNode().ToString(); 296 writeToExcelString[3] = circleNodes[i].getCar().getOutNode().ToString(); 297 writeToExcelString[4] = circleNodes[i].getCar().getNodeNo().ToString(); 298 writeToExcelString[5] = (circleNodes[i].getCar().getOutTime() - circleNodes[i].getCar().getInTime()).ToString(); 299 writeToExcelString[6] = ((double)(circleNodes[i].getCar().getOutTime() - circleNodes[i].getCar().getInTime()) / (double)circleNodes[i].getCar().getNodeNo()).ToString(); 300 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[count[2], 1], xlWorksheets[2].Cells[count[2], 7]).Value2 = writeToExcelString; 301 xlWorksheets[2].Cells[count[2], 7] = (double)(circleNodes[i].getCar().getOutTime() - circleNodes[i].getCar().getInTime()) / (double)circleNodes[i].getCar().getNodeNo(); 302 count[2] += 1; 303 } 304 else 305 { 306 if (circleNodes[i].getHasCar().Equals(true) && circleNodes[i + 1].getHasCar().Equals(false)) 307 { 308 tempCircleNodes[i].setHasCar(false); 309 NodeFuZhi(tempCircleNodes[i + 1], circleNodes[i]); 310 tempCircleNodes[i + 1].getCar().setRemainNodeNo(tempCircleNodes[i + 1].getCar().getRemainNodeNo() - 1); 311 312 } 313 else 314 { 315 NodeFuZhi(tempCircleNodes[i + 1], circleNodes[i + 1]); 316 } 317 } 318 } 319 //对第24个节点单独处理 320 if (circleNodes[24].getHasCar().Equals(true) && circleNodes[24].getCar().getRemainNodeNo() == 0)//车将要出环岛 321 { 322 tempCircleNodes[24].setHasCar(false); 323 NodeFuZhi(tempCircleNodes[1], circleNodes[1]); 324 circleNodes[24].getCar().setOutTime(timeRecord);//离开环岛的时间 325 326 //记录离开的车辆 327 this.textBoxOutput.Text += timeRecord.ToString() + "时刻," + circleNodes[24].getCar().getCarNo().ToString() + "号车经过" + circleNodes[24].getCar().getNodeNo ().ToString() + "站从" + circleNodes[24].getCar().getOutNode().ToString() + "离开,花了" + (circleNodes[24].getCar().getOutTime() - circleNodes[24].getCar().getInTime()).ToString() + "秒" + System.Environment.NewLine; 328 //统计离开车辆的等待时间 329 statisticCarOut += 1; 330 statisticStayTime += (double)(circleNodes[24].getCar().getOutTime() - circleNodes[24].getCar().getInTime()) / (double)circleNodes[24].getCar().getNodeNo(); 331 332 //将离开的车记录到excel文件 333 writeToExcelString[0] = timeRecord.ToString(); 334 writeToExcelString[1] = circleNodes[24].getCar().getCarNo().ToString(); 335 writeToExcelString[2] = circleNodes[24].getCar().getInNode().ToString(); 336 writeToExcelString[3] = circleNodes[24].getCar().getOutNode().ToString(); 337 writeToExcelString[4] = circleNodes[24].getCar().getNodeNo().ToString(); 338 writeToExcelString[5] = (circleNodes[24].getCar().getOutTime() - circleNodes[24].getCar().getInTime()).ToString(); 339 writeToExcelString[6] = ((double)(circleNodes[24].getCar().getOutTime() - circleNodes[24].getCar().getInTime()) / (double)circleNodes[24].getCar().getNodeNo()).ToString(); 340 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[count[2], 1], xlWorksheets[2].Cells[count[2], 7]).Value2 = writeToExcelString; 341 xlWorksheets[2].Cells[count[2], 7] = (double)(circleNodes[24].getCar().getOutTime() - circleNodes[24].getCar().getInTime()) / (double)circleNodes[24].getCar().getNodeNo(); 342 count[2] += 1; 343 } 344 else 345 { 346 if (circleNodes[24].getHasCar().Equals(true) && circleNodes[1].getHasCar().Equals(false)) 347 { 348 tempCircleNodes[24].setHasCar(false); 349 NodeFuZhi(tempCircleNodes[1], circleNodes[24]); 350 tempCircleNodes[1].getCar().setRemainNodeNo(tempCircleNodes[1].getCar().getRemainNodeNo() - 1); 351 } 352 else 353 { 354 if (circleNodes[1].getHasCar().Equals(true) && circleNodes[2].getHasCar().Equals(false))//如果1节点可以前进 355 { 356 tempCircleNodes[1].setHasCar(false); 357 } 358 else 359 { 360 NodeFuZhi(tempCircleNodes[1], circleNodes[1]); 361 } 362 } 363 } 364 } 365 366 367 368 369 public void ChangeLine() 370 { 371 //为下个时刻四个入口线上的节点改变状态 372 for (int i = 1; i <= 4; i++) 373 { 374 if (lineNodes[3 * i].getHasCar().Equals(true) && circleNodes[((i - 1) == 0 ? 4 : (i - 1)) * 6 - 1].getHasCar().Equals(false) && circleNodes[((i - 1) == 0 ? 4 : (i - 1)) * 6].getHasCar().Equals(false)) 375 {//对最前面进入环岛的节点讨论,进入环岛的条件 376 tempLineNodes[3 * i].setHasCar(false); 377 NodeFuZhi(tempCircleNodes[((i - 1) == 0 ? 4 : (i - 1)) * 6], lineNodes[3 * i]); 378 } 379 else 380 { 381 NodeFuZhi(tempLineNodes[3 * i], lineNodes[3 * i]); 382 } 383 384 if (lineNodes[3 * i - 1].getHasCar().Equals(true) && lineNodes[3 * i].getHasCar().Equals(false)) 385 {//对第二个节点讨论 386 tempLineNodes[3 * i - 1].setHasCar(false); 387 NodeFuZhi(tempLineNodes[3 * i], lineNodes[3 * i - 1]); 388 } 389 else 390 { 391 NodeFuZhi(tempLineNodes[3 * i - 1], lineNodes[3 * i - 1]); 392 } 393 394 if (lineNodes[3 * i - 2].getHasCar().Equals(true) && lineNodes[3 * i - 1].getHasCar().Equals(false)) 395 {//对第三个节点讨论 396 tempLineNodes[3 * i - 2].setHasCar(false); 397 NodeFuZhi(tempLineNodes[3 * i - 1], lineNodes[3 * i - 2]); 398 } 399 else 400 { 401 NodeFuZhi(tempLineNodes[3 * i - 2], lineNodes[3 * i - 2]); 402 } 403 } 404 } 405 406 407 408 409 public void ChangeTotalNodes() 410 { 411 //为下个时刻环线上的节点改变状态 412 changePictureBox(pictureBox1, circleNodes[1]); 413 changePictureBox(pictureBox2, circleNodes[2]); 414 changePictureBox(pictureBox3, circleNodes[3]); 415 changePictureBox(pictureBox4, circleNodes[4]); 416 changePictureBox(pictureBox5, circleNodes[5]); 417 changePictureBox(pictureBox6, circleNodes[6]); 418 changePictureBox(pictureBox7, circleNodes[7]); 419 changePictureBox(pictureBox8, circleNodes[8]); 420 changePictureBox(pictureBox9, circleNodes[9]); 421 changePictureBox(pictureBox10, circleNodes[10]); 422 changePictureBox(pictureBox11, circleNodes[11]); 423 changePictureBox(pictureBox12, circleNodes[12]); 424 changePictureBox(pictureBox13, circleNodes[13]); 425 changePictureBox(pictureBox14, circleNodes[14]); 426 changePictureBox(pictureBox15, circleNodes[15]); 427 changePictureBox(pictureBox16, circleNodes[16]); 428 changePictureBox(pictureBox17, circleNodes[17]); 429 changePictureBox(pictureBox18, circleNodes[18]); 430 changePictureBox(pictureBox19, circleNodes[19]); 431 changePictureBox(pictureBox20, circleNodes[20]); 432 changePictureBox(pictureBox21, circleNodes[21]); 433 changePictureBox(pictureBox22, circleNodes[22]); 434 changePictureBox(pictureBox23, circleNodes[23]); 435 changePictureBox(pictureBox24, circleNodes[24]); 436 437 //为下个时刻入口处的节点改变状态 438 changePictureBox(linePicture1, lineNodes[1]); 439 changePictureBox(linePicture2, lineNodes[2]); 440 changePictureBox(linePicture3, lineNodes[3]); 441 changePictureBox(linePicture4, lineNodes[4]); 442 changePictureBox(linePicture5, lineNodes[5]); 443 changePictureBox(linePicture6, lineNodes[6]); 444 changePictureBox(linePicture7, lineNodes[7]); 445 changePictureBox(linePicture8, lineNodes[8]); 446 changePictureBox(linePicture9, lineNodes[9]); 447 changePictureBox(linePicture10, lineNodes[10]); 448 changePictureBox(linePicture11, lineNodes[11]); 449 changePictureBox(linePicture12, lineNodes[12]); 450 } 451 452 453 454 455 public void changePictureBox(PictureBox p, Node node) 456 {//改变节点状态的函数 457 if (node.getHasCar().Equals(false)) 458 { 459 p.Image = CircleTrans.Properties.Resources.car_0; 460 } 461 else 462 { 463 if (node.getCar().getCarImg() == 1) 464 { 465 p.Image = CircleTrans.Properties.Resources.car_1; 466 } 467 else if (node.getCar().getCarImg() == 2) 468 { 469 p.Image = CircleTrans.Properties.Resources.car_2; 470 } 471 else if (node.getCar().getCarImg() == 3) 472 { 473 p.Image = CircleTrans.Properties.Resources.car_3; 474 } 475 else if (node.getCar().getCarImg() == 4) 476 { 477 p.Image = CircleTrans.Properties.Resources.car_4; 478 } 479 } 480 } 481 482 483 484 485 private void trackBar1_Scroll(object sender, EventArgs e) 486 {//调整仿真速度 487 this.timerInterval.Interval = this.trackBar1.Value * 150+50; 488 } 489 490 491 492 493 private void button1_Click(object sender, EventArgs e) 494 { 495 this.timerInterval.Interval = this.trackBar1.Value *150+50; 496 this.timerInterval.Enabled = true; 497 498 } 499 500 501 502 503 private void button2_Click(object sender, EventArgs e) 504 { 505 this.timerInterval.Enabled = false; 506 } 507 508 509 510 511 protected void CreateOrOpenExcelFile() 512 { 513 try 514 { 515 System.Reflection.Missing oMissing = System.Reflection.Missing.Value; 516 517 xlApp = new Excel.Application(); 518 xlWorkbook = xlApp.Workbooks.Add(oMissing); 519 520 for (int i = 1; i <= 3; i++) 521 { 522 xlWorksheets[i-1] = new Excel.Worksheet(); 523 xlWorksheets[i-1] = (Excel.Worksheet)xlWorkbook.Worksheets[i]; 524 } 525 526 xlWorksheets[0].Name = "交通状况记录"; 527 xlWorksheets[1].Name = "进入系统车辆记录"; 528 xlWorksheets[2].Name = "离开系统车辆记录"; 529 530 writeToExcelString[0]= "时刻"; 531 for(int i=1;i<=24;i++) 532 { 533 writeToExcelString[i] = "环线节点" + i.ToString() + "状态"; 534 } 535 for (int i = 1; i <= 12; i++) 536 { 537 writeToExcelString[i+24] = "入口节点" + i.ToString() + "状态"; 538 } 539 xlWorksheets[0].get_Range(xlWorksheets[0].Cells[1, 1], xlWorksheets[0].Cells[1, 37]).Value2 = writeToExcelString; 540 xlWorksheets[0].get_Range(xlWorksheets[0].Cells[1, 1], xlWorksheets[0].Cells[1, 37]).Font.Name = "黑體"; 541 xlWorksheets[0].get_Range(xlWorksheets[0].Cells[1, 1], xlWorksheets[0].Cells[1, 37]).Font.Bold = true; 542 xlWorksheets[0].get_Range(xlWorksheets[0].Cells[1, 1], xlWorksheets[0].Cells[1, 37]).Borders.LineStyle = 1; 543 xlWorksheets[0].Cells.EntireColumn.AutoFit(); 544 545 writeToExcelString[0]="时刻"; 546 writeToExcelString[1]="车辆号"; 547 writeToExcelString[2]="入口站号"; 548 writeToExcelString[3]="出口站号"; 549 writeToExcelString[4]="将要经过的总节点数"; 550 xlWorksheets[1].get_Range(xlWorksheets[1].Cells [1,1],xlWorksheets [1].Cells [1,5]).Value2 = writeToExcelString; 551 xlWorksheets[1].get_Range(xlWorksheets[1].Cells[1, 1], xlWorksheets[1].Cells[1, 5]).Font.Name = "黑體"; 552 xlWorksheets[1].get_Range(xlWorksheets[1].Cells[1, 1], xlWorksheets[1].Cells[1, 5]).Font.Bold = true; 553 xlWorksheets[1].get_Range(xlWorksheets[1].Cells[1, 1], xlWorksheets[1].Cells[1, 5]).Borders.LineStyle = 1; 554 xlWorksheets[1].Cells.EntireColumn.AutoFit(); 555 556 writeToExcelString[0] = "时刻"; 557 writeToExcelString[1] = "车辆号"; 558 writeToExcelString[2] = "入口站号"; 559 writeToExcelString[3] = "出口站号"; 560 writeToExcelString[4] = "经历的总节点数"; 561 writeToExcelString[5] = "总时间"; 562 writeToExcelString[6] = "等待率"; 563 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[1, 1], xlWorksheets[2].Cells[1, 7]).Value2 = writeToExcelString; 564 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[1, 1], xlWorksheets[2].Cells[1, 7]).Font.Name = "黑體"; 565 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[1, 1], xlWorksheets[2].Cells[1, 7]).Font.Bold = true; 566 xlWorksheets[2].get_Range(xlWorksheets[2].Cells[1, 1], xlWorksheets[2].Cells[1, 7]).Borders.LineStyle = 1; 567 xlWorksheets[2].Cells.EntireColumn.AutoFit(); 568 569 System.DateTime currentTime=new System.DateTime(); 570 currentTime =System .DateTime.Now ; 571 572 string nowTime = currentTime.ToString("D")+"-"+currentTime .Hour .ToString ()+"-"+currentTime.Minute.ToString ()+"-"+currentTime.Second .ToString (); 573 string saveAsPath = System.Windows.Forms.Application.StartupPath + "\\仿真日志" +nowTime+ ".xls"; 574 xlWorkbook.SaveAs(saveAsPath, oMissing, oMissing, oMissing, oMissing,oMissing, Excel.XlSaveAsAccessMode.xlShared, oMissing, oMissing, oMissing,oMissing, oMissing); 575 } 576 577 catch (Exception ex) 578 { 579 MessageBox.Show(ex.Message); 580 581 } 582 } 583 584 585 586 587 protected void CloseExcelApp() 588 { 589 try 590 { 591 xlWorkbook.Save(); 592 xlApp.Quit(); 593 594 } 595 catch (Exception ex) 596 { 597 MessageBox.Show(ex.Message); 598 } 599 finally 600 { 601 System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp); 602 xlApp = null; 603 GC.Collect(); 604 } 605 } 606 607 608 609 610 protected double exp(double beta) 611 { 612 //return r.NextDouble(); 613 return -beta * System.Math.Log(r.NextDouble()); 614 } 615 616 617 618 619 public void NodeFuZhi(Node a, Node b) 620 { 621 a.setHasCar(b.getHasCar()); 622 a.getCar().setCarImg(b.getCar().getCarImg()); 623 a.getCar().setCarNo(b.getCar().getCarNo()); 624 a.getCar().setInNode(b.getCar().getInNode()); 625 a.getCar().setInTime(b.getCar().getInTime()); 626 a.getCar().setNodeNo(b.getCar().getNodeNo()); 627 a.getCar().setOutNode(b.getCar().getOutNode()); 628 a.getCar().setOutTime(b.getCar().getOutTime()); 629 a.getCar().setRemainNodeNo(b.getCar().getRemainNodeNo()); 630 } 631 632 633 634 635 /**//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 636 } 637 638 639 640 641 642 643 644 645 646 647 648 public class Node 649 //定义节点类,节点的属性包括车,包括 650 { 651 private Car car = new Car();//节点上的车 652 private bool hasCar = false;//记录该时刻节点上有无车 653 654 public Node() 655 { 656 hasCar = false; 657 } 658 659 public Car getCar() 660 { 661 return car; 662 } 663 664 public bool getHasCar() 665 { 666 return hasCar; 667 } 668 669 public void setCar(Car car) 670 { 671 this.car = car; 672 } 673 674 public void setHasCar(Boolean hasCar) 675 { 676 this.hasCar = hasCar; 677 } 678 } 679 680 681 682 683 684 685 686 687 public class Car//定义车类 688 { 689 private int carNo;//车的编号 690 private int inNode;//车进入环岛的节点位置 691 private int outNode;//车离开环岛的节点位置 692 private int nodeNo;//车在环岛中应该经过的节点数目 693 private int remainNodeNo;//车离开环岛前还需要经过的节点数目 694 private int inTime;//车进入环岛的时刻 695 private int outTime;//车离开环岛的时刻 696 private int carImg;//车的图片 697 698 public Car() 699 { 700 carNo = 0; 701 inNode = 0; 702 outNode = 0; 703 nodeNo = 0; 704 remainNodeNo = 0; 705 inTime = 0; 706 outTime = 0; 707 carImg = 0; 708 } 709 710 public int getCarNo() 711 { 712 return this.carNo; 713 } 714 715 public int getNodeNo() 716 { 717 return this.nodeNo; 718 } 719 720 public int getInTime() 721 { 722 return this.inTime; 723 } 724 725 public int getOutTime() 726 { 727 return this.outTime; 728 } 729 730 public int getInNode() 731 { 732 return this.inNode; 733 } 734 735 public int getOutNode() 736 { 737 return this.outNode; 738 } 739 740 public int getRemainNodeNo() 741 { 742 return this.remainNodeNo; 743 } 744 745 public int getCarImg() 746 { 747 return this.carImg; 748 } 749 750 public void setCarNo(int carNo) 751 { 752 this.carNo = carNo; 753 } 754 755 public void setNodeNo(int nodeNo) 756 { 757 this.nodeNo = nodeNo; 758 } 759 760 public void setInTime(int inTime) 761 { 762 this.inTime = inTime; 763 } 764 765 public void setOutTime(int outTime) 766 { 767 this.outTime = outTime; 768 } 769 770 public void setInNode(int inNode) 771 { 772 this.inNode=inNode; 773 } 774 775 public void setOutNode(int outNode) 776 { 777 this.outNode = outNode; 778 } 779 780 public void setRemainNodeNo(int remainNodeNo) 781 { 782 this.remainNodeNo = remainNodeNo; 783 } 784 785 public void setCarImg(int carImg) 786 { 787 this.carImg = carImg; 788 } 789 } 790 791 792 793 794 }
|