private void btnGo_Click(object sender, EventArgs e) { decimal t = 0; int ID = 0; Bitmap BM = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); pictureBox1.Image = BM; string trash = sr.ReadLine(); // Trash column titles from R code. trash = sr1.ReadLine(); // Read data arrays generated by R programme for (int i = 0; i < 10000; i++) { vDesired[i] = Convert.ToDecimal(sr.ReadLine()); string line = sr1.ReadLine(); if (line.IndexOf('E') > 0 || line.IndexOf('e') > 0) { BAC[i] = 0; // Small quantities in e-xx format } else { BAC[i] = Convert.ToDecimal(line); } if (BAC[i] < 0) { BAC[i] = 0; // BAC cannot be negative. } } while (1 == 1) { // Create a car at x = 0 at 3 second intervals. if (Math.Abs(t / 3 - Math.Floor(t / 3)) < 0.001m) { cars[ID] = new car(); initCar(cars[ID], ID, 0); ID++; } // Let's move the cars for (int i = 0; i < ID; i++) { cars[i].x += TIMESTEP * cars[i].v; cars[i].v += TIMESTEP * cars[i].a; // Cars may not go backwards. if (cars[i].v < 0) { cars[i].v = 0; cars[i].a = 0; } if (i != 0) { cars[i].headway = cars[cars[i].precedingCar].x - cars[i].x - 5; // Accident if (cars[i].headway < 0) { accident(i, cars[i].precedingCar, cars[i].x); } else { if (cars[i].headway <2 / cars[i].aggFac * cars[i].v || cars[i].v> cars[i].vDesired) { if (cars[i].brake && t >= cars[i].deTime + cars[i].reactTime) { if (cars[i].headway < 2 / cars[i].aggFac * cars[i].v) { cars[i].a = 300 * (cars[i].headway - 2 / cars[i].aggFac * cars[i].v) / cars[i].headway; } else if (cars[i].v > cars[i].vDesired) { cars[i].a = 300 * (cars[i].vDesired - cars[i].v) / cars[i].vDesired; } if (cars[i].a < MAXDECEL) { cars[i].a = MAXDECEL; } } else if (!cars[i].brake) { cars[i].brake = true; cars[i].gas = false; cars[i].deTime = t; } } else if (cars[i].headway > 2 / cars[i].aggFac * cars[i].v + 10 && cars[i].v < cars[i].vDesired) { if (cars[i].gas && t >= cars[i].acTime + cars[i].reactTime) { cars[i].a = aAve(cars[i].v); if (cars[i].a > aMax(cars[i].v)) { cars[i].a = aMax(cars[i].v); } } else if (!cars[i].gas) { cars[i].brake = false; cars[i].gas = true; cars[i].acTime = t; } } else { cars[i].brake = false; cars[i].gas = false; cars[i].a = 0; } } // Emergency stop situation if ((cars[i].headway < 600 && cars[cars[i].precedingCar].v < 0.75m * cars[i].v) || cars[cars[i].precedingCar].emergStop) { if (!cars[i].emergStop) { cars[i].esTime = t; } cars[i].emergStop = true; } if (cars[i].emergStop && t > cars[i].esTime + cars[i].reactTime) { cars[i].a = MAXDECEL; } if (cars[i].v <= cars[cars[i].precedingCar].v) { cars[i].emergStop = false; } } // Draw the car int offset = 0; switch (cmbDisplay.Text) { case " 0 to 1 km": offset = 0; lblStart.Text = "km 0 "; lblEnd.Text = "km 1 "; break; case " 1 to 2 km": offset = 1000; lblStart.Text = "km 1 "; lblEnd.Text = "km 2 "; break; case " 2 to 3 km": offset = 2000; lblStart.Text = "km 2 "; lblEnd.Text = "km 3 "; break; case " 3 to 4 km": offset = 3000; lblStart.Text = "km 3 "; lblEnd.Text = "km 4 "; break; case " 4 to 5 km": offset = 4000; lblStart.Text = "km 4 "; lblEnd.Text = "km 5 "; break; case " 5 to 6 km": offset = 5000; lblStart.Text = "km 5 "; lblEnd.Text = "km 6 "; break; case " 6 to 7 km": offset = 6000; lblStart.Text = "km 6 "; lblEnd.Text = "km 7 "; break; case " 7 to 8 km": offset = 7000; lblStart.Text = "km 7 "; lblEnd.Text = "km 8 "; break; case " 8 to 9 km": offset = 8000; lblStart.Text = "km 8 "; lblEnd.Text = "km 9 "; break; case " 9 to 10 km": offset = 9000; lblStart.Text = "km 9 "; lblEnd.Text = "km 10 "; break; case "10 to 11 km": offset = 10000; lblStart.Text = "km 10 "; lblEnd.Text = "km 11 "; break; case "11 to 12 km": offset = 1000; lblStart.Text = "km 11 "; lblEnd.Text = "km 12 "; break; default: offset = 0; lblStart.Text = "km 0 "; lblEnd.Text = "km 1 "; break; } lblStart.Refresh(); lblEnd.Refresh(); int xPlot = Convert.ToInt32(cars[i].x); xPlot -= offset; for (int x = xPlot; x >= xPlot - 5; x--) { if (x > 0 && x < 1000) { for (int y = 16; y < 19; y++) { BM.SetPixel(x, y, Color.LightGreen); } } } for (int x = xPlot - 6; x >= xPlot - 11; x--) { if (x > 0 && x < 1000) { for (int y = 16; y < 19; y++) { BM.SetPixel(x, y, SystemColors.ControlDarkDark); } } } } pictureBox1.Refresh(); string timer = t.ToString(); if (t < 0.001m) { timer += ".00"; } lblClock.Text = "SIMULATED TIME = " + timer; lblClock.Refresh(); t += TIMESTEP; } }
private void btnGo_Click(object sender, EventArgs e) { decimal t = 0; int ID = 0; Bitmap BM = new Bitmap(pictureBox1.ClientSize.Width, pictureBox1.ClientSize.Height); pictureBox1.Image = BM; string trash = sr.ReadLine(); // Trash column titles from R code. trash = sr1.ReadLine(); // Read data arrays generated by R programme for (int i = 0; i < 10000; i++) { vDesired[i] = Convert.ToDecimal(sr.ReadLine()); string line = sr1.ReadLine(); if (line.IndexOf('E') > 0 || line.IndexOf('e') > 0) BAC[i] = 0; // Small quantities in e-xx format else BAC[i] = Convert.ToDecimal(line); if (BAC[i] < 0) BAC[i] = 0; // BAC cannot be negative. } while (1 == 1) { // Create a car at x = 0 at 3 second intervals. if (Math.Abs(t / 3 - Math.Floor(t / 3)) < 0.001m) { cars[ID] = new car(); initCar(cars[ID], ID, 0); ID++; } // Let's move the cars for (int i = 0; i < ID; i++) { cars[i].x += TIMESTEP * cars[i].v; cars[i].v += TIMESTEP * cars[i].a; // Cars may not go backwards. if (cars[i].v < 0) { cars[i].v = 0; cars[i].a = 0; } if (i != 0) { cars[i].headway = cars[cars[i].precedingCar].x - cars[i].x - 5; // Accident if (cars[i].headway < 0) { accident(i, cars[i].precedingCar, cars[i].x); } else { if (cars[i].headway < 2 / cars[i].aggFac * cars[i].v || cars[i].v > cars[i].vDesired) { if (cars[i].brake && t >= cars[i].deTime + cars[i].reactTime) { if (cars[i].headway < 2 / cars[i].aggFac * cars[i].v) { cars[i].a = 300 * (cars[i].headway - 2 / cars[i].aggFac * cars[i].v) / cars[i].headway; } else if (cars[i].v > cars[i].vDesired) { cars[i].a = 300 * (cars[i].vDesired - cars[i].v) / cars[i].vDesired; } if (cars[i].a < MAXDECEL) cars[i].a = MAXDECEL; } else if (!cars[i].brake) { cars[i].brake = true; cars[i].gas = false; cars[i].deTime = t; } } else if (cars[i].headway > 2 / cars[i].aggFac * cars[i].v + 10 && cars[i].v < cars[i].vDesired) { if (cars[i].gas && t >= cars[i].acTime + cars[i].reactTime) { cars[i].a = aAve(cars[i].v); if (cars[i].a > aMax(cars[i].v)) cars[i].a = aMax(cars[i].v); } else if (!cars[i].gas) { cars[i].brake = false; cars[i].gas = true; cars[i].acTime = t; } } else { cars[i].brake = false; cars[i].gas = false; cars[i].a = 0; } } // Emergency stop situation if ((cars[i].headway < 600 && cars[cars[i].precedingCar].v < 0.75m * cars[i].v) || cars[cars[i].precedingCar].emergStop) { if (!cars[i].emergStop) cars[i].esTime = t; cars[i].emergStop = true; } if (cars[i].emergStop && t > cars[i].esTime + cars[i].reactTime) { cars[i].a = MAXDECEL; } if (cars[i].v <= cars[cars[i].precedingCar].v) cars[i].emergStop = false; } // Draw the car int offset = 0; switch (cmbDisplay.Text) { case " 0 to 1 km": offset = 0; lblStart.Text = "km 0 "; lblEnd.Text = "km 1 "; break; case " 1 to 2 km": offset = 1000; lblStart.Text = "km 1 "; lblEnd.Text = "km 2 "; break; case " 2 to 3 km": offset = 2000; lblStart.Text = "km 2 "; lblEnd.Text = "km 3 "; break; case " 3 to 4 km": offset = 3000; lblStart.Text = "km 3 "; lblEnd.Text = "km 4 "; break; case " 4 to 5 km": offset = 4000; lblStart.Text = "km 4 "; lblEnd.Text = "km 5 "; break; case " 5 to 6 km": offset = 5000; lblStart.Text = "km 5 "; lblEnd.Text = "km 6 "; break; case " 6 to 7 km": offset = 6000; lblStart.Text = "km 6 "; lblEnd.Text = "km 7 "; break; case " 7 to 8 km": offset = 7000; lblStart.Text = "km 7 "; lblEnd.Text = "km 8 "; break; case " 8 to 9 km": offset = 8000; lblStart.Text = "km 8 "; lblEnd.Text = "km 9 "; break; case " 9 to 10 km": offset = 9000; lblStart.Text = "km 9 "; lblEnd.Text = "km 10 "; break; case "10 to 11 km": offset = 10000; lblStart.Text = "km 10 "; lblEnd.Text = "km 11 "; break; case "11 to 12 km": offset = 1000; lblStart.Text = "km 11 "; lblEnd.Text = "km 12 "; break; default: offset = 0; lblStart.Text = "km 0 "; lblEnd.Text = "km 1 "; break; } lblStart.Refresh(); lblEnd.Refresh(); int xPlot = Convert.ToInt32(cars[i].x); xPlot -= offset; for (int x = xPlot; x >= xPlot - 5; x--) { if (x > 0 && x < 1000) { for (int y = 16; y < 19; y++) { BM.SetPixel(x, y, Color.LightGreen); } } } for (int x = xPlot - 6; x >= xPlot - 11; x--) { if (x > 0 && x < 1000) { for (int y = 16; y < 19; y++) { BM.SetPixel(x, y, SystemColors.ControlDarkDark); } } } } pictureBox1.Refresh(); string timer = t.ToString(); if (t < 0.001m) timer += ".00"; lblClock.Text = "SIMULATED TIME = " + timer; lblClock.Refresh(); t += TIMESTEP; } }
public void initCar(car auto, int ID, decimal x) { auto.ID = ID; auto.x = x; //auto.vDesired = 30; auto.BAC = 0; int j = ID; while (j >= 10000) { j -= 10000; } auto.vDesired = vDesired[j]; if (cbAlcohol.Checked) { auto.BAC = BAC[j]; } auto.a = 0; if (ID == 0) { auto.precedingCar = -1; // The first car has no preceding car; auto.headway = 1000; // arbitrarily set its headway to 1 km. } else { // Find the preceding car decimal xMin = x + 1000; for (int i = 0; i < ID; i++) { if (cars[i].x > x) { if (cars[i].x < xMin) { auto.precedingCar = i; auto.headway = cars[i].x - x - 5; // 5 meters for the car length cars[i].followingCar = ID; // We're following the preceding car xMin = cars[i].x; } // End if } // End if } // End for } // End else auto.v = auto.vDesired; auto.exists = true; auto.aggFac = 1; // 1 = no aggression if (cbAggressive.Checked) { Random random = new Random(); int randomNumber = random.Next(0, 100); auto.aggFac += Convert.ToDecimal(randomNumber) / 200; // auto.aggFac *= (1 + 10 * auto.BAC); } auto.reactTime = 1.5m; // From the New Jersey Driver Manual, Chapter 5 //auto.reactTime = 0.5375m; // Average Baseline from Austin (2009) //auto.reactTime = 0.25m; // An arbitrary value I chose. double martini = 1 + Convert.ToDouble(auto.BAC); auto.reactTime *= Convert.ToDecimal(Math.Pow(martini, 2)); auto.brake = false; auto.gas = false; auto.acTime = 0; auto.deTime = 0; auto.crashed = false; auto.emergStop = false; } // End Method
public void initCar(car auto, int ID, decimal x) { auto.ID = ID; auto.x = x; //auto.vDesired = 30; auto.BAC = 0; int j = ID; while (j >= 10000) j -= 10000; auto.vDesired = vDesired[j]; if (cbAlcohol.Checked) auto.BAC = BAC[j]; auto.a = 0; if (ID == 0) { auto.precedingCar = -1; // The first car has no preceding car; auto.headway = 1000; // arbitrarily set its headway to 1 km. } else { // Find the preceding car decimal xMin = x + 1000; for (int i = 0; i < ID; i++) { if (cars[i].x > x) { if (cars[i].x < xMin) { auto.precedingCar = i; auto.headway = cars[i].x - x - 5; // 5 meters for the car length cars[i].followingCar = ID; // We're following the preceding car xMin = cars[i].x; } // End if } // End if } // End for } // End else auto.v = auto.vDesired; auto.exists = true; auto.aggFac = 1; // 1 = no aggression if (cbAggressive.Checked) { Random random = new Random(); int randomNumber = random.Next(0, 100); auto.aggFac += Convert.ToDecimal(randomNumber) / 200; // auto.aggFac *= (1 + 10 * auto.BAC); } auto.reactTime = 1.5m; // From the New Jersey Driver Manual, Chapter 5 //auto.reactTime = 0.5375m; // Average Baseline from Austin (2009) //auto.reactTime = 0.25m; // An arbitrary value I chose. double martini = 1 + Convert.ToDouble(auto.BAC); auto.reactTime *= Convert.ToDecimal(Math.Pow(martini, 2)); auto.brake = false; auto.gas = false; auto.acTime = 0; auto.deTime = 0; auto.crashed = false; auto.emergStop = false; }