Mathematical Modeling: 10 Monte Carlo Simulation

Table of contents

Overview

Example

Needle problem

three door problem

Simulate queuing problem

Constrained nonlinear programming problem

Book buying problems in bookstores (0-1 planning)

missile tracking problem

Traveling Salesman Problem (TSP)

Change light bulb

Weapon upgrades

Estimate the base e of the natural logarithm


Overview

Principle: According to the theorem of large numbers, when the sample size is large enough, the frequency of an event is its probability

Example

Needle problem

 ​​​​​​

What needs to be randomly simulated is x (the distance from the midpoint to the nearest parallel line) and the angle φ 

%% 由于一次模拟的结果具有偶然性,可以重复100次后再来求一个平均的pi
result = zeros(100,1);  % 初始化保存100次结果的矩阵

l =  0.520;     
a = 1.314;

n = 1000000;
    
for num = 1:100
    m = 0;  
    x = rand(1, n) * a / 2 ; % 随机模拟针的终点到平行线的距离
    phi = rand(1, n) * pi; % 随机模拟角度
    for i=1:n
        if x(i) <= l / 2 * sin(phi (i)) % 判断相交的条件
            m = m + 1;
        end
    end
    p = m / n;
    mypi = (2 * l) / (a * p);
    result(num) = mypi;  % 把求出来的myphi保存到结果矩阵中
end
mymeanpi = mean(result);  % 计算result矩阵中保存的100次结果的均值

three door problem

What is the "three-door problem" in the decisive blackjack game? How to increase the probability of winning the lottery? Teacher Li Yongle explains the Monty Hall problem (2018 latest)_bilibili_bilibili

Find: ① If you have successfully won the prize, the probability of winning the prize is because of changes/no changes.
       ② Find the probability of not winning, winning after changes, or winning without changes.

What needs to be simulated is: which door x was chosen for the first time, the prize is behind door y, and whether the choice changes

%%%%%%%%%%%%%%%%%%%%%%%%%% 在成功获奖的条件下,改变还是不改变 赢的概率大
n = 100000;  % n代表蒙特卡罗模拟重复次数
a = 0;  % a表示不改变主意时能赢得汽车的次数
b = 0;  % b表示改变主意时能赢得汽车的次数
for i= 1 : n 
    % 第一次选的是哪个门
    x = randi([1,3]);  % 随机生成一个1-3之间的整数x
    % 哪个门后有车
    y = randi([1,3]); 

    if x == y   % 如果x和y相同,只有不改变主意时才能赢
        a = a + 1;     b = b + 0;
    else  % x ~= y % 如果x和y不同,只有改变主意时才能赢
        a = a + 0;     b = b +1;
    end
end
disp(['蒙特卡罗方法得到的不改变主意时的获奖概率为:', num2str(a/n)]);
disp(['蒙特卡罗方法得到的改变主意时的获奖概率为:', num2str(b/n)]);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 不获奖、改变获奖、不改变获奖的概率
n = 100000;
a = 0;
b = 0;  
c = 0;  % c表示没有获奖的次数

for i= 1 : n 
    x = randi([1,3]); 
    y = randi([1,3]); 

    % 改不改变选择
    change = randi([0, 1]); % change =0  不改变主意,change = 1 改变主意
    % 下面分为两种情况讨论:x=y和x~=y
    if x == y   % 如果x和y相同,只有不改变主意时才能赢
        if change == 0  % 不改变主意
        	a = a + 1; 
        else  % 改变了主意
            c= c + 1;
        end
    else  % x ~= y % 如果x和y不同,只有改变主意时才能赢
         if change == 0  % 不改变主意
        	c = c + 1; 
        else  % 改变了主意
            b = b + 1;
         end
    end
end
disp(['蒙特卡罗方法得到的不改变主意时的获奖概率为:', num2str(a/n)]);
disp(['蒙特卡罗方法得到的改变主意时的获奖概率为:', num2str(b/n)]);
disp(['蒙特卡罗方法得到的没有获奖的概率为:', num2str(c/n)]);

Simulate queuing problem

What needs to be simulated is:

  • The time interval xi when the i-th customer arrives 
  • The service time yi of the i-th customer (note that if it is less than 1, change it to 1)

calculate:

  • Arrival time of the i-th customer ci = c(i-1) + xi (previous arrival time + interval)
  • The service start time of the i-th customer bi = max( e(i-1), ci ) (self arrival time/when the previous person completes)
  • The end service time of the i-th customer ei = bi + yi (start time + service time)
  • The waiting time of the i-th customer wi = bi - ci (starting service time - arrival time)

Loop condition:

  • Start service time < 480 (8 hours a day, unit is minutes)

initialization:

  • e0 = 0, c0 = 0
  • n(k) records the number of people serving on day k

Constrained nonlinear programming problem

Finding the local optimal solution of nonlinear programming requires a given initial value, and the Monte Carlo algorithm can obtain the initial value.

Book buying problems in bookstores (0-1 planning)

variable:

  • xij (01 variable) If the jth book in the i-th mall is bought, it is 1, otherwise it is 0
  • mij records the selling price of the j-th book in the i-th mall
  • qi records the shipping fee of the i-th mall
  • ti records whether the book was purchased at the i-th store. If purchased, the shipping fee will be calculated. 

condition:

  • The sum of each column is 1 (each book is only bought in one store)

Objective function: total cost + freight

missile tracking problem

Idea: Take a small time interval and calculate the distance between the missile and the ship after each time interval. If it is less than a set value, it is considered a collision.

Traveling Salesman Problem (TSP)

Randomly generate routes and compare them to get the smallest one

Change light bulb

Set a time T by yourself and calculate the cost of changing the light bulb within T time

Weapon upgrades

Estimate the base e of the natural logarithm

 

Guess you like

Origin blog.csdn.net/m0_54625820/article/details/128682161