所以我刚刚开始接触编程,我不得不说这是我做过的最有意义但最令人沮丧的事情。我正在从事难度越来越大的项目,最近的项目涉及使用蒙特卡洛方法和大量循环。以下是目前完成的代码:
#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。 fill_level模拟for循环的父for循环要重复10次模拟多少年的过程(内部for循环的每次迭代代表一年)。
当我尝试运行您在这里看到的程序时,它会一直运行到“运行模拟”,然后就不会再继续了。有比我更有经验的人理解我在说什么并知道发生了什么吗?
最佳答案
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
。这是一个无限循环。
关于c++ - 介绍 C++ 和蒙特卡洛方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739052/