Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
COMPUTER PROBLEM-SOLVING IN ENGINEERING AND COMPUTER SCIENCE EGR 141 FALL 2006 PROBLEM-SOLVING EXERCISE #4 SIMULATION - SOLUTION As we’ve previously seen, equations describing situations often contain uncertain parameters, that is, parameters that aren’t necessarily a single value but instead are associated with a probability distribution function. When more than one of the variables is unknown, the outcome is difficult to visualize. A common way to overcome this difficulty is to simulate the scenario many times and count the number of times different ranges of outcomes occur. One such popular simulation is called a Monte Carlo Simulation. In this problem-solving exercise you will develop a program that will perform a Monte Carlo simulation on a simple profit function. Consider the following total profit function: PT = nPv Where PT is the total profit, n is the number of vehicles sold and Pv is the profit per vehicle. PART A Compute 5 iterations of a Monte Carlo simulation given the following information: n follows a uniform distribution with minimum of 1 and maximum 10 Pv follows a normal distribution with a mean of $4000 and a standard deviation of $1000 Number of bins: 10 Recall that for all practical purposes we will use 3 std. deviations from the mean as the maximum value for parameters following a normal distribution. Obviously, 5 iterations are not very many. In fact, typically you would simulate 10,000 iterations or so to view meaningful results but I figured that I’d give you a break . If you’d like to compute 10,000 iterations by hand for extra credit, go ahead… i.) What are the ranges for the 10 bins? n : nmin = 1 , nmax = 10 Pv: Pvmin = $1000, Pvmax = $7,000 Ptmin = nmin * Pvmin = $1000 Ptmax = nmax * Pvmax = $70,000 Range/Bin = (70000 – 1000)/10 = 6,900 Bin 1: 1000 – 7900 Bin 2: 7901 – 14800 Bin 3: 14801 – 21700 Bin 4: 21701 – 28600 Bin 5: 28601 – 35500 Bin 6: 35501 – 42400 Bin 7: 42401 – 49300 Bin 8: 49301 – 56200 Bin 9: 56201 – 63100 Bin 10: 63101 – 70000 ii.) Fill in the table below: Parameter n Pv PT Bin # $ Range Iteration 1 2 $3100 $6200 1 1000 – 7.9k iii.) Iteration 2 7 $4500 $31500 5 28601 – 35.5k Iteration 3 9 $3400 $30600 5 28601 - 35.5k Iteration 4 4 $6500 $26000 4 21701 – 28.6k Iteration 5 5 $2200 $11000 2 7901 – 14.8k Fill in the frequency of occurrences of each bin: 1: 1 6: 0 2: 1 7: 0 3: 0 8: 0 4: 1 9: 0 5: 2 10: 0 PART B Write the following three functions in a module called Simulate: Public Function GetRandomUniform(ByVal min As Integer, ByVal max as Integer) as Integer ‘ This function returns a random number from a uniform distribution between min and max. Public Function GetRandomNormal(ByVal mean As Single, ByVal stddev as Single) as Single ‘ This function returns a random number from a normal distribution with a mean of mean and standard ‘ deviation of stddev Public Function GetBinIndex(ByVal mini As Single, ByVal maxi As Single, ByVal numbins as _ Integer, ByVal valuetobin as Single) As Integer ‘ This function returns the Bin Index given an output minimum of mini, output maximum of maxi, ‘ numbins number of bins, and a value to bin of valuetobin See Part D for code. PART C Include the module created in part B to develop a Visual Basic .NET program that will simulate the basic profit calculation, PT = nPv, where n follows a uniform distribution, Pv follows a normal distribution, and the user can input the number of bins and number of iterations. The user must also input the min and max for n and the mean and standard deviation for Pv. Finally, the user can click a button and the results will be graphed on a bar chart using the Microsoft Chart Control. Turn in a listing of the code and a screen shot of the resulting chart using: 1. Iterations: 10000 Bins: 5 n-min: 1 n-max: 10 2. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 3. Iterations: 10000 Bins: 10 n-min: 1 n-max: 10 That’s three screen shots and a listing. See Part D for code. Pv-mean: 9000 Pv-mean: 9000 Pv-mean: 6000 Pv-stddev: 2000 Pv-stddev: 2000 Pv-stddev: 1000 PART D Extend the Visual Basic .NET program developed in part C to simulate the basic profit calculation, PT = nPv, where the user can select either a uniform or normal distribution for n using radio buttons and then must input the appropriate parameters (min and max if they select uniform, mean and standard deviation if they select normal) and they can similarly select either a uniform or normal distribution for Pv with appropriate parameters depending on the selection. Of course, the user will input the number of bins and number of iterations. Finally, the user can click a button and the results will be graphed on a bar chart using the Microsoft Chart Control. Turn in a listing of the code and a screen shot of the resulting chart using: 1. Iterations: 10000 Bins: 5 n-min: 1 n-max: 10 Pv-mean: 8000 2. Iterations: 10000 Bins: 10 n-mean: 9 n-stddev: 2 Pv-min: 2000 3. Iterations: 10000 Bins: 10 n- mean: 14 n- stddev: 3 Pv-min: 2000 Pv-stddev: 2000 Pv-max: 10000 Pv-max: 10000 That’s three screen shots and a listing. Code Listing for Parts C, D and E 'Form File: 'Date: 'Description: ' ' ' ' ' ' ' frmSIM.vb March 31, 2005 This application runs a Monte Carlo Simulation on a simple profit function. This code is used for Parts C, D and E. For Part C, the radio buttons for Pv uniform and N normal have their visible property set to False so as not to be accessible to the user. For Part D and E, these buttons are reset to visible = =True. Private Sub cmdSIM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSIM.Click Dim NumBins, n, x, i As Integer Dim Pt, Pv As Single Dim Ptmin, Ptmax, Pvmin, Pvmax As Single Dim nmax, nmin, nbins, cycles As Integer Dim Bins() As Integer 'Array holds the frequency of profit total nbins = Val(txtBins.Text) cycles = Val(TextBox1.Text) txtTotalProfit.Text = 0 'reset the total profit If rdoNuni.Checked = True Then 'uniform distribution 'txtN1 is maximum 'txtN2 is minimum nmin = Val(txtN2.Text) nmax = Val(txtN1.Text) Else 'rdoNnor is checked and normal distribution 'txtN1 is mean 'txtN2 is stddev nmin = Val(txtN1.Text) - 3 * Val(txtN2.Text) nmax = Val(txtN1.Text) + 3 * Val(txtN2.Text) End If If rdoPvuni.Checked = True Then ' uniform distribution 'Pv1 is maximum 'Pv2 is minimum Pvmin = Val(txtPv2.Text) Pvmax = Val(txtPv1.Text) Else 'rdoPvnor is checked and normal distribution 'Pv1 is mean 'Pv2 is stddev Pvmin = Val(txtPv1.Text) - 3 * Val(txtPv2.Text) Pvmax = Val(txtPv1.Text) + 3 * Val(txtPv2.Text) End If Ptmin = nmin * Pvmin Ptmax = nmax * Pvmax ReDim Bins(nbins + 1) For x = 1 To cycles If rdoNuni.Checked = True Then 'uniform n = GetRandomUniform(nmin, nmax) Else 'rdoNnor is checked then normal n = GetRandomNormal(Val(txtN1.Text), Val(txtN2.Text)) End If If n > nmax Then 'Value must be placed in last bin n = nmax End If If rdoPvuni.Checked = True Then 'uniform 'Pvmax is maximum 'Pvmin is minimum Pv = GetRandomUniform(Pvmin, Pvmax) Else 'rdoPvnor is checked then normal 'txtPv1 is mean 'txtPv2 is stddev Pv = GetRandomNormal(Val(txtPv1.Text), Val(txtPv2.Text)) End If If Pv > Pvmax Then Pv = Pvmax End If 'Total profit calculation Pt = n * Pv 'Keep track of Pt for aveage calculation txtTotalProfit.Text += Pt i = GetBinIndex(Ptmin, Ptmax, nbins, Pt) If i > nbins Then MessageBox.Show("2Index is out of Bounds") Exit For End If Bins(i) += 1 Next 'Now chart the results of the simulation MSChart1.RowCount = nbins MSChart1.ColumnCount = 1 For i = 1 To nbins MSChart1.Row = i MSChart1.Data = Bins(i) 'Row label will show the maximum of each bin MSChart1.RowLabel = Ptmin + (((Ptmax - Ptmin) / nbins) * (i)) Next 'Average Total Profit calculation txtTotalProfit.Text /= cycles txtTotalProfit.Text = FormatNumber(txtTotalProfit.Text, 2) End Sub Private Sub rdoPvuni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoPvuni.CheckedChanged If rdoPvuni.Checked = True Then lblMax1.Visible = True lblMin1.Visible = True lblMean1.Visible = False lblStddev1.Visible = False End If End Sub Private Sub rdoPvnor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoPvnor.CheckedChanged If rdoPvnor.Checked = True Then lblMax1.Visible = False lblMin1.Visible = False lblMean1.Visible = True lblStddev1.Visible = True End If End Sub Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click End End Sub Private Sub rdoNuni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoNuni.CheckedChanged If rdoNuni.Checked = True Then lblMax2.Visible = True lblMin2.Visible = True lblMean2.Visible = False lblStdDev2.Visible = False End If End Sub Private Sub rdoNnor_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoNnor.CheckedChanged If rdoNnor.Checked = True Then lblMax2.Visible = False lblMin2.Visible = False lblMean2.Visible = True lblStdDev2.Visible = True End If End Sub Private Sub frmSIM_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'This clears the form of the chart for startup MSChart1.RowCount = 0 End Sub Module Simulate Public Function GetRandomUniform(ByVal min As Integer, ByVal max As Integer) As Integer 'Function generates a uniformly distributed random number between min ‘and max. Dim u As Integer Randomize() u = Int(min + Rnd() * (max - min + 1)) 'u is a uniformly distributed random number Return u End Function Public Function GetRandomNormal(ByVal Mean As Single, ByVal StdDev As Single) As Single 'Function generates a random number on a normal distribution with ‘mean as Mean and standard deviation as StdDev. Dim r, phi, x, z As Single Randomize() r = Rnd() phi = Rnd() z = Math.Cos(2 * 3.14159 * r) * Math.Sqrt(-2 * Math.Log(phi)) 'z is a random number on the standard normal distribution x = (z * StdDev) + Mean 'x is a random number on the normal disrtibution of interest Return x End Function Public Function GetBinIndex(ByVal mini As Single, ByVal maxi As Single, ByVal numbins As Integer, ByVal ValueToBin As Single) As Integer 'Function generates a bin index for the bin array to track frequency 'of occurance of Pt in the simulation. Dim q As Double Dim qc As Double 'ValueToBin is Pt 'mini is Ptmin 'maxi is Ptmax 'numbins is nbins q = CDbl(ValueToBin - mini) * (numbins / (maxi - mini)) qc = Math.Ceiling(q) If qc > numbins Then MessageBox.Show("1Index is out of Bounds " & qc) End If 'qc is the index for the Bins array Return qc End Function End Module PART E You’re going to go to a job interview for OU Car Co. Knowing that the field is highly competitive, you have run sales scenarios ahead of time experimenting with different numbers of customers and vehicle profits given that in one month OU Car Co. sells between 4 and 10 cars uniformly distributed with profits of $4,000 on the average with standard deviation of $900. Which has a higher payoff, focusing on selling to a couple more customers or by increasing the average sale (with the same std. dev. of $900) by retraining your sales force or do they have basically the same effect on total sales? Support your answer. Solution: Note: Results for Ptave will vary due to being generated by random numbers. Therefore, results can be in a reasonable range from what is shown here. Baseline First, a baseline is established, based upon the above given information. Pv = Profit per vehicle Fits Normal distribution with mean of $4000 and a standard deviation of $900 N = Number of vehicles sold Fits a Uniform distribution with a maximum of 10 and a minimum of 4 Yields Ptave = Average Profit Total = $28,901.00 Case 1: 10.0 % Increase in either Pv or N Pvmean = $4400 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $30.881.86 Nmax = 11 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $30,112.84 The increase in Pv yields a resulting Ptave $769.02 higher than the increase in N. Case 2: 20.0 % Increase in either Pvmean or Nmax Pvmean = $4800 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $33,686.49 Nmax = 12 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $32,095.11 The increase in Pv yields a resulting Ptave $1591.38 higher than the increase in N.. Continued on next page Case 3: 30.0 % Increase in either Pv or N Pvmean = $5200 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $36,473.37 Nmax = 15 All other inputs remain at baseline input level Yields Ptave = Average Profit Total = $34,115.03 The increase in Pv yields a resulting Ptave $2358.34 higher than the increase in N. In conclusion: From these scenarios, an increase in either Pv or N will increase Ptave. However, increasing Pv has a greater impact on the average total profit (Ptave) from the baseline than a similar increase in the number of vehicles sold (N). So from these three cases efforts to increase the average sale (Pv) would be recommended.