Touge Computer Algorithm Design and Analysis: Randomization Algorithm

Level 1: Coin Experiment

任务描述
相关知识
    随机数
编程要求
测试说明

Task Description
The task of this level: pseudo-random numbers generated by the computer to simulate a coin toss experiment.

Relevant knowledge
In order to complete the task of this level, you need to master: 1. How to get the length of the array, 2. How to traverse the array.

Random Numbers
Random numbers play a very important role in the design of randomization algorithms.
Real random numbers cannot be generated on a real computer, so the random numbers used in the randomization algorithm are all random to a certain extent, that is, pseudo-random numbers.

A coin toss experiment is simulated using computer-generated pseudo-random numbers.
Assume that a coin is tossed 10 times, and each coin toss gets heads and tails at random. 10 coin tosses constitute an event.
Calling Random(2) returns a binary result.
In the main program, the function TossCoins is repeatedly called to simulate the event of tossing 10 coins 50,000 times. Recording these 50,000 simulations with
head i gets exactly i head stabs. The final output is the probability map of the heads event obtained by simulating the coin toss event.

Programming requirements
According to the prompts, supplement the code in the editor on the right.

The test shows that
the RandomNumber.h function has been written, and you can click to view it.

The platform will test the code you write:
for example:
input:
10
50000
output:

    0 *
    1 *
    2   *
    3        *
    4              *
    5                 *
    6              *
    7        *
    8   *
    9 *
    10 *`
#include "RandomNumber.h"
#include <iostream>
using namespace std;
int TossCoins(int numberCoins);
int main()
{
    
    
    //模拟随机抛硬币事件
    int NCOINS;
    long NTOSSES;
    cin >>NCOINS;
    cin >>NTOSSES;
    //heads[i]是得到i次正面的次数
    long i,heads[NCOINS+1];
    int j,position;
    //初始化数组heads
    for(int j=0; j<NCOINS+1;j++)
    {
    
    
        heads[j] = 0;
    }
    //重复50,000次模拟事件
    for(int i=0; i<NTOSSES; i++)
    {
    
    
        heads[TossCoins(NCOINS)]++;
    }
    //输出频率图
    for(int i=0; i<=NCOINS; i++)
    {
    
    
        position = int(float(heads[i])/NTOSSES*72);
        cout<<i<<" ";
        for(int j=0; j<position-1; j++)
        {
    
    
            cout<<" ";
        }
        cout<<"*"<<endl;
    }
    return 0;
}
int TossCoins(int numberCoins)
{
    
    
    //随机抛硬币
    static RandomNumber coinToss(1);
    int i,tosses = 0;
    for(int i=0; i<numberCoins; i++)
    {
    
    
        //Random(2) = 1表示正面
        tosses += coinToss.Random(2);
    }
    return tosses;
}

Level 2: Finding pi with the method of randomly throwing points

    任务描述
    相关知识
    编程要求
    测试说明

Task Description
The task of this level: write a program to calculate pi by random casting method

Relevant knowledge
insert image description here
programming requirements
According to the prompt, supplement the code in the editor on the right.
Test instructions
The platform will test the code you write:
input:

100

output:

n1=100
pi=3.12

Let's start your mission, I wish you success!

//随机化算法 用随机投点法计算π值
#include "RandomNumber.h"
#include <iostream>
using namespace std;
double Darts(int n);
int main()
{
    
    
    int n1;
    cin>> n1;
    cout<<"n1="<<n1<<"\npi="<<Darts(n1)<<endl;
    return 0;
}
//用随机投点法计算π值
double Darts(int n)
{
    
    
    static RandomNumber dart(1);
    int k = 0;
    for(int i=1; i<=n; i++)
    {
    
    
        double x = dart.fRandom();
        double y = dart.fRandom();
        if((x*x + y*y)<=1)
        {
    
    
            k++;
        }
    }
    return 4*k/double(n);
}

Level 3: Solving Nonlinear Equations

任务描述
相关知识
编程要求
测试说明

Task description
Task for this level: write a program to find linear equations.

Relevant knowledge
Solve the following nonlinear equations
insert image description here
insert image description here
Programming requirements
Find a simple two-dimensional linear equations:
x1-x2=fx1
x1+x2=fx2
According to the prompt, supplement the code in the editor on the right.
Test instructions
The platform will test the code you write:
For example:
Input:
1
3
Output:
The original equations are:
x1-x2=1
x1+x2=3
The roots are:
x1=2.02104 x2=1.01445

Let's start your mission, I wish you success!

//随机化算法 解线性方程组
#include "RandomNumber.h"
#include <iostream>
using namespace std;
bool NonLinear(double *x0,double *dx0,double *x,double *fx,double a0,
                    double epsilon,double k,int n,int Steps,int M);
double f(double *x,double *fx);
int main()
{
    
    
    double  *x0,                //根初值
            *x,                    //根
            *dx0,            //增量初值
            *fx,  // 函数值
            a0 = 0.0001,            //步长
            epsilon = 0.01,        //精度
            k = 1.1;            //步长变参
    int n = 2,                    //方程个数
        Steps = 10000,            //执行次数
        M = 1000;                //失败次数
    x0 = new double[n+1];
    dx0 = new double[n+1];
    x = new double[n+1];
    fx = new double[n+1];
    cin >> fx[1];
    cin >> fx[2];
    //根初值
    x0[1] = 0.0;
    x0[2] = 0.0;
    //增量初值
    dx0[1] = 0.01;
    dx0[2] = 0.01;
    cout<<"The original equations are:"<<endl;
    cout<<"x1-x2="<<fx[1]<<endl;
    cout<<"x1+x2="<<fx[2]<<endl;
    cout<<"The roots are:"<<endl;
    bool flag = NonLinear(x0,dx0,x,fx,a0,epsilon,k,n,Steps,M);
    while(!flag)
    {
    
            
        flag = NonLinear(x0,dx0,x,fx,a0,epsilon,k,n,Steps,M);
    }    
    for(int i=1; i<=n; i++)
    {
    
    
        cout<<"x"<<i<<"="<<x[i]<<" ";
    }
    cout<<endl;
    return 0;
}
//解非线性方程组的随机化算法
bool NonLinear(double *x0,double *dx0,double *x,double *fx,double a0,
                    double epsilon,double k,int n,int Steps,int M)
{
    
    
    static RandomNumber rnd(1);
    bool success;            //搜索成功标志
    double *dx,*r;
    dx = new double[n+1];    //步进增量向量
    r = new double[n+1];    //搜索方向向量
    int mm = 0;                //当前搜索失败次数
    int j = 0;                //迭代次数
    double a = a0;            //步长因子
    for(int i=1; i<=n; i++)
    {
    
    
        x[i] = x0[i];
        dx[i] = dx0[i];
    }
    double fy = f(x,fx);        //计算目标函数值
    double min = fy;        //当前最优值
    while(j<Steps)
    {
    
    
        //(1)计算随机搜索步长
        if(fy<min)//搜索成功
        {
    
    
            min = fy;
            a *= k;
            success = true;
        }
        else//搜索失败
        {
    
    
            mm++;
            if(mm>M)
            {
    
    
                a /= k;
            }
            success = false;
        }
        if(min<epsilon)
        {
    
    
            break;
        }
        //(2)计算随机搜索方向和增量
        for(int i=1; i<=n; i++)
        {
    
    
            r[i] = 2.0 * rnd.fRandom()-1;
        }
        if(success)
        {
    
    
            for(int i=1; i<=n; i++)
            {
    
    
                dx[i] = a * r[i];
            }
        }
        else
        {
    
    
            for(int i=1; i<=n; i++)
            {
    
    
                dx[i] = a * r[i] - dx[i];
            }
        }
        //(3)计算随机搜索点
        for(int i=1; i<=n; i++)
        {
    
    
            x[i] += dx[i];
        }
        //(4)计算目标函数值
        fy = f(x,fx);
        j++;
    }    
    if(fy<=epsilon)
    {
    
    
        return true;
    }
    else
    {
    
    
        return false;
    }
}
double f(double *x, double* fx)
{
    
    
    return (x[1]-x[2]-fx[1])*(x[1]-x[2]-fx[1])
            +(x[1]+x[2]-fx[2])*(x[1]+x[2]-fx[2]);
}

Guess you like

Origin blog.csdn.net/m0_63007797/article/details/130606376