介绍性C ++和蒙特卡罗方法

时间:2012-10-05 03:24:49

标签: c++ loops montecarlo

所以我刚刚开始编程,我不得不说这是我做过的最有价值但令人沮丧的事情。我正在接受越来越难的项目,最近的项目涉及使用蒙特卡罗方法和大量循环。以下是到目前为止完成的代码:

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <ctime>

using namespace std;




int main ()
{
    srand (time(0));
    string operation;
    cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
    cin >> operation;
    string reservoir_name; // Creating variables for reservoir
    double reservoir_capacity;
    double outflow;
    double inflow_min;
    double inflow_max;

    if (operation == "q")
    {
        cout << "Exiting program." << endl;
        system ("pause");
        return 0;
    }

    while (operation == "o") // Choose one or multiple simulations.
        {

                string reservoir_name; // Creating variables for reservoir function
                double reservoir_capacity;

                double inflow_min = 0;
                double inflow_max = 0;
                double inflow_range = inflow_min + inflow_max;
                double inflow_difference = inflow_max - inflow_min;
                double inflow_threshold = .9 * inflow_range/2; // Math for acceptable flow threshold.



                cout << "What is the name of the reservoir?" << endl;
                cin.ignore ();
                getline (cin,reservoir_name); // Grab whole string for reservoir name.
                cout << "What is the capacity of the reservoir in MAF (Millions of Acre Feet)?" << endl;
                cin >> reservoir_capacity;
                cout << "What is the minimum inflow?" << endl;
                cin >> inflow_min;
                cout << "What is the maximum inflow?" << endl;
                cin >> inflow_max;
                cout << "What is the required outflow?" << endl;
                cin >> outflow;
                inflow_range = inflow_min + inflow_max;
                inflow_threshold = .9 * inflow_range/2;
                cin.ignore ();

                if (outflow > inflow_threshold) // Check for unacceptable outflow levels.
                {
                    cout << "Warning! The outflow is over 90% of the average inflow. Simulation aborted. Returning to main menu." << endl;
                }
                else
                {
                    const int number_simulations = 10;
                    double fill_level = 0;
                    int years = 1;
                    cout << "Running simulation." << endl;
                    for (int i = 1; i < number_simulations; i++) // Each year
                    {

                        for (years; fill_level < reservoir_capacity; years++ ) 
                        {
                            double r = rand() * 1.0 / RAND_MAX;
                            double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
                            if (fill_level < 0)
                            {
                            fill_level = 0;
                            }
                        }   // Simulate the change of water level.
                    cout << years << endl;
                    }

                }
                    cout << "What would you like to do now?" << endl; // Saving for later. The menu re-prompt message and code.
                    cout << "Using the letters 'o', or 'q', please indicate if you would like to simulate once, or quit the program: " << endl;
                    cin >> operation;
    }


    system ("pause");
    return 0;
}

所以我想我的主要问题是我遇到了关于在“运行模拟”下设置for循环的问题,我需要设置第一个for循环来运行内部for循环10次,每个对于随机值的查询,内部for循环的那10次迭代得出随机数,用于可接受结果的范围。我被告知这个想法是使用蒙特卡罗方法,即

double r = rand() * 1.0 / RAND_MAX;
double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.

所以程序会为流入创建一个随机值。我们的想法是内部for循环将继续运行,直到油藏的fill_level(从0开始)达到reservoir_capacity。模拟多少年(每个迭代的内部for循环代表一年)的过程将由父循环重复10次循环for fill_level模拟。

当我尝试按照你在这里看到的那样运行程序时,它将一直运行到“运行模拟”,然后它将不再继续。有经验的人比我自己更了解我在说什么,知道发生了什么吗?

1 个答案:

答案 0 :(得分:1)

for (years; fill_level < reservoir_capacity; years++ ) 
{
    double r = rand() * 1.0 / RAND_MAX;
    double x = inflow_min + inflow_range * r;// SHOULD be between minimum inflow and maximum inflow.
    if (fill_level < 0)
    {
    fill_level = 0;
    }
}   // Simulate the change of water level.

你永远不会在这个循环中增加fill_level。这是一个无限循环。