Entries:
Discussions:

Something went wrong getting user information from Channel 9

Latest Achievement:

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Something went wrong getting the Visual Studio Achievements

Simulations using Excel

• Oops, something didn't work.

Getting subscription
Subscribe to this conversation
Unsubscribing
Subscribing
• Hi members,

I am a high school teacher ( more details in my profile ).

I have one student who is modelling travel times between bus stops on a particualar bus route. The model assumes that the Total Travel time between two bus stops is the Passenger loading time plus the Bus travel time between the stops.

The Passenger loading time is dependent on the number of passengers ( No. of passengers ) who wish to board the bus multiplied by the time is takes to serve each of them. This Serving time per passenger is taken to be a constant ( 10 seconds per passenger, for illustrative purposes ). No considersation is made for the time it takes to off load passengers. ( We assume that the time it takes for off-loading passengers to leave the bus does not influence the model )

The Bus travel time between bus stops is taken to be a constant. So no consideration for varying amounts of traffic on route or speed of the bus is made.

So in brief,

where,

Passenger loading time = No. of Passengers * Serving time per passenger

No. of Passengers = RANDBETWEEN(0,15)
(RANDBETWEEN(0,15)  generates a random number between some limits )

FINALLY...., ( sorry, if it was too much of mouthful )

The point is I wish to use Excel to run the simulation a 1000 times. If you like refresh the calculations so that new random numbers of passengers will be generated and hence different Passenger loading times. And then collect a frequency distribution for different classes of observations ( Total Travel times ) in table from which a histogram could be generated using the Excel wizard chart functionality.

How can this be done in code ? Using a subroutine and perhaps a single button to start the simulation on the spreadsheet.. ?

I hope it makes sense. And I hope it is not too much to ask this forum.

Keli

• Have you ever tried to do something like this in .NET? You could write a simple console application that outputs the results as CSV files (comma separated files). you can then open those directly in Excel and create the charts etc.

• Sorry, if that isn't clear, we are working within Excel. So we are going to be trying to use the cells in the worksheet as our working environment.

I was thinking more in terms of creating macros, subroutines ... etc

Keli

• There's only 15 possible outcomes and all you'd be doing is measuring the randomness of Excel's random number generator, which should result in an even distribution. Or am I missing something? I managed to miss statistics in school.

In any case, how about putting your constants at the top of your sheet, make one row of formulas based on a single random value, and copying that down for 1000 rows? Then do your stats and charts on that table's data.

• You should be able to put your model, including all variables and relationships between them, in two rows; with the first row being the variable names, and the second row being the formulas.

 Passengers Time Per Passenger Passenger Loading Time Bus Travel Time Total Travel Time =Rand(…) 10 =A2*B2 500 =C2+D2

Then you can fill the second row down through row 1001 and get 1000 random samplings.

Then you can make a chart using the last column to show the distribution.

EDIT: meh... what DCMonkey said.

• Hi

Maurits is closest to what I was trying to describe. However, on the bus route they are many bustops - so they would 10 or more similar lines on the spreadsheet. Each bustops has an upper and lower limit of passengers who might be wanting to board the bus. So the simulation will generate different combinations of random numbers of boarding passengers at each bustop.  All these different combinations will affect  passenger loading times and hence the total travel time over the entire journey.

I tried to paste a copy ( screen print ) of the spreadsheet into this message but I can't do it. Any ideas how ?

Any other way to send you this information ? You then be able to see why I need some code to run the simulation many times so that a frequency distribution of total bus journey travel time can be plotted.
( Normal frequency distributions are the ones that are normally taught in schools - but it is likely that this bus simulations freq. dist. won't be normal - but that's what I want my student to find out)

Keli

PS
How did you make the table, Maurits ?
Is it possible to send file attachments ?
Can we paste images into the messages ?

• I still cant see why you cant use what Maurits suggested?

You could have say 10 samples per bus stop. So you have ten rows, each row calculating the total travel time for a random number of passengers. Then you could average these time to a mean total travel time.

Then do another 10 rows for the other bustop, and calculate the total times for another 10 random samples.

I dont think you can post images into messages. You could up load your Excel file and post a link here.

• I made an attempt at writing VBA code for this problem, available here:

http://paintedblue.panicnow.net/Code/BUS_TEST_SIMULATOR.xls

Since it uses VBA, you will need to set Excel's security to allow macros to run (in Tools | Options | Security | Macro Security -- setting this value to Medium will prompt you regarding the execution of macros, however you should reset it back to High security if you are concerned about the potential for computer virii executing within Excel's context).

Basically, I have provided a set of cells to configure the test. These cells are currently located in Range J1:N2. You can specify the number of tests to run in addition to the value of the constants.

To the left of the configuration cells is a command button. Clicking the button will run the test. The data will populate in Columns A through F, although this is configurable in the code.

To view the VBA code, select Tools | Macro | Visual Basic Editor. The majority of the code resides on the Sheet1 object.

Finally, it is worth mentioning that I am currently populating the random number of passengers value using a simple VBA random number generator algorithm. However, if you want to use the RANDBETWEEN function, you can uncomment line 55 and comment line 56. I chose not to use RANDBETWEEN because it appears to recalculate all cells each time that it is executed, which results in very slow performance. The VBA approach will generate 1000 rows of data in less than one second on my workstation.

Please let me know if you have any questions!

• Keli wrote:
How did you make the table, Maurits ?

I initially made it in Excel.  I typed in the formulas with a leading ' which made Excel display them literally.  I used Excel's "border" tool to make the border.

Then I opened up a new message in Outlook 2000.  I set the mail format to HTML, and copy/pasted from Excel to the mail message.

Then I right-clicked the message and "View Source"d.

Then I copied the HTML from <table> to </table>.

Then I went to Channel 9, replied to your post, and switched to the "HTML" tab in the FreeTextBox editor.

Finally I pasted the HTML I had copied.

(yuck!)

To post images... first I got a free Geocities account (www.geocities.com)
I save the image in gif or jpg format and upload it to my account
Then I open the image at the geocities URL in another browser window
Finally I drag and drop the image into the FreeTextBox editor.

To transfer files, you could .zip the file up and post it on your Geocities account.  Then you could post a link in your comment.

Finally... to add more stops, you could add more columns.  The formulae get a little more complicated, but ten stops shouldn't be impossibly difficult.

• Maurits wrote:
﻿To post images... first I got a free Geocities account (www.geocities.com)
I save the image in gif or jpg format and upload it to my account
Then I open the image at the geocities URL in another browser window
Finally I drag and drop the image into the FreeTextBox editor.

Does that work? I didn't think Geocities allowed external linking to images.

• Sven Groot wrote:
Does that work? I didn't think Geocities allowed external linking to images.

Sure, see this post for example

• Big thanks to y'all for the replies. ( anon, PaintedBlue, Maurits, Sven Groot )

PaintedBlue - I will look at your file over the weekend and give feedback in a couple of days. And then post more stuff via geocities.

Have a fab weekend

Keli

• Hi all,

I modified PaintedBlue's file - BUS_TEST_SIMULATOR.xls and included a frequency distribution ( Histogram ).

Modified - Paintedblue solution
http://www.geocities.com/keli_mutiso/bus_test_simulator_modified_v2_paintedblue.xls

I left my student to figure out how to modify his solution to achieve a similar output. However you will notice the major differences

Student's file - so far
http://www.geocities.com/keli_mutiso/simulations_modified_06june06_version1.xls

I have left him to try and look at paintedblue solution and see if he can incorporate some extra things. The major difference in between the two solutions is that paintedblue's Bus_test_simulator  has one bus stop in the model but my student has several bus stops on route each with different min and max passengers who might want to board the bus.

ok - thanks again. You have been very helpful - any additional comments from the pros will be taken seriously.  Please don't hesistate to post your thoughts on the  code and the solution.

Keli

• The student's approach looks good to me. It is a very clean approach to initializing the test conditions and running the simulation.

The largest difference between the student's version and my code is that I actually write the test cases out to the worksheet as records, but that is not necessary for generating the histogram. I definitely misinterpreted the requirements in this respect.

With respect to the student's code, adding a DoEvents call in TestRun's For loop will keep Excel from locking up while the tests are running (see my code as an example -- you only need to call it periodically, not for every iteration of the loop). This might be useful in case you wanted to watch the histogram Chart evolve over the course of the simulation in realtime (assuming that a Chart is predefined within the workbook), which is actually pretty neat to see.

For the AllocateFrequencyToTotalTravelTimeClassBins method, I wanted to point out that all of the If statements are currently being executed for each test iteration, but this does not have to be the case. For example, the following:

If Cells(29, 5) >= 1840 And Cells(29, 5) < 1890 Then
Cells(3, 7) = Cells(3, 7) + 1
End If

If Cells(29, 5) >= 1900 And Cells(29, 5) < 1950 Then
Cells(4, 7) = Cells(4, 7) + 1
End If

If Cells(29, 5) >= 1960 And Cells(29, 5) < 2010 Then
Cells(5, 7) = Cells(5, 7) + 1
End If

can be changed to:

If Cells(29, 5) >= 1840 And Cells(29, 5) < 1890 Then
Cells(3, 7) = Cells(3, 7) + 1
ElseIf Cells(29, 5) >= 1900 And Cells(29, 5) < 1950 Then
Cells(4, 7) = Cells(4, 7) + 1
ElseIf Cells(29, 5) >= 1960 And Cells(29, 5) < 2010 Then
Cells(5, 7) = Cells(5, 7) + 1
End If

This would result in less work being done on each iteration, and it might allow for the simulation to run slightly faster. In general, it is ideal to minimize the amount of work being performed within a loop when possible.

Finally, I see that the student is writing a constant value to a cell to induce the worksheet to recalculate, thus allowing RANDBETWEEN to generate new values:

Cells(2, 3) = 10

The more explicit way to recalculate the worksheet through code is to fire the worksheet's Calculate() event:

Call Worksheets("Sheet1").Calculate

Certainly, this causes the same result to occur in the end, but it is easier to interpret what the code is doing -- the method is named to allow the code to be self-documenting in nature.

Thanks, and let me know if you have any other questions.

• Maurits wrote:
﻿
 Keli wrote:How did you make the table, Maurits ?

I initially made it in Excel.  I typed in the formulas with a leading ' which made Excel display them literally.  I used Excel's "border" tool to make the border.

Then I opened up a new message in Outlook 2000.  I set the mail format to HTML, and copy/pasted from Excel to the mail message.

Then I right-clicked the message and "View Source"d.

Then I copied the HTML from <table> to </table>.

Then I went to Channel 9, replied to your post, and switched to the "HTML" tab in the FreeTextBox editor.

Finally I pasted the HTML I had copied.

(yuck!)

To post images... first I got a free Geocities account (www.geocities.com)
I save the image in gif or jpg format and upload it to my account
Then I open the image at the geocities URL in another browser window
Finally I drag and drop the image into the FreeTextBox editor.

To transfer files, you could .zip the file up and post it on your Geocities account.  Then you could post a link in your comment.

Finally... to add more stops, you could add more columns.  The formulae get a little more complicated, but ten stops shouldn't be impossibly difficult.

Somehow I doubt you did those steps Maurits, seems largely unncessary ("sending an email to yourself" says it all) and you know HTML already.

Here's the (as far as I'm concerned) easier/simpler way of doing it:

Flip to "HTML" view and type this:

<table border="1">

That marks the beginning of a table.

For each row, you type <tr>, followed by <td> for each column in the table. The column's data is contained like so: <td>HERE</td>

Once you've finished a row, type </tr> and move on to the next <tr>

Once you've finished writing the table, type </table>

An example is this:

<table border="1">
<tr><td>Top Left</td><td>Top Right</td></tr>
<tr><td>Bot Left</td><td>Bot Right</td></tr>
</table>

Produces:

 Top Left Top Right Bot Left Bot Right

As for the images, in HTML view you type:

<img src="%FULL URL TO IMAGE GOES HERE%" />

Quite simple, really

<img src="http://www.forumspile.com/O_RLY-Quite.jpg" />

but enough orthoganal posting... sorry I couldn't answer the actual problem at hand, I just felt like nit-picking Maurits' posting

• PaintedBlue are still receiving this thread ?