不报错,也没警告,但想要的东西出不来
这是Gillespie算法,代码没显示错误和警告,但是跑出来只有时间序列,没有状态的数据“GA_XYZW(example1).exe”(Win32): 已加载“C:\Users\Lenovo\source\repos\GA_XYZW(example1)\Debug\GA_XYZW(example1).exe”。已加载符号。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\vcruntime140d.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcp140d.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\ucrtbased.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\ucrtbased.dll”。
“GA_XYZW(example1).exe”(Win32): 已卸载“C:\Windows\SysWOW64\ucrtbased.dll”
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel.appcore.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcrt.dll”。
“GA_XYZW(example1).exe”(Win32): 已加载“C:\Windows\SysWOW64\rpcrt4.dll”。
线程 0x4380 已退出,返回值为 0 (0x0)。
线程 0x8274 已退出,返回值为 0 (0x0)。
线程 0x7ae8 已退出,返回值为 0 (0x0)。
程序“[38464] GA_XYZW(example1).exe”已退出,返回值为 0 (0x0)。
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
class System_States
{
public:
long Reaction, Reactions;
double States[4] = {0};
double A[6] = {0};
double C[6] = { 0.1,0.03,0.1,0.01,0.2,0.001 };
double A0=0 ;
double Tau=0;
System_States()
{
States[0] = 1.0;
States[1] = 1.0;
States[2] = 1.0;
States[3] = 1.0;
Reactions = 6;
};
void Propensity_function()
{
A[0] = C[0] * States[1];
A[1] = C[1] * States[2];
A[2] = C[2] * States[1] * (States[1] - 1.) / 2.;
A[3] = C[3] * States[3];
A[4] = C[4] * States[0] * States[1];
A[5] = C[5] * States[1] * (States[1] - 1.) / 2.;
A0 = A[0] + A[1] + A[2] + A[3] + A[4] + A[5];
}
void Update_Tau()
{
double R1;
R1 = 1.0 * rand() / RAND_MAX;
Tau = log(1.0 / R1) / A0;
}
void Update_Reaction()
{
double R2;
double R2A0;
R2 = 1.0 * rand() / RAND_MAX;
R2A0 = R2 * A0;
if (A[0] >= R2A0)
{
Reaction = 1;
}
else
{
double sum = A[0];
for (int i = 1;i < Reactions; i++)
{
sum = sum + A[i];
if (sum >=R2A0)
{
Reaction = i + 1;
break;
}
}
}
}
void Update_States()
{
switch (Reaction)
{
case 1:
States[1] = States[1] - 1;
States[2] = States[2] + 1;
break;
case 2:
States[1] = States[1] + 1;
States[2] = States[2] - 1;
break;
case 3:
States[1] = States[1] - 2;
States[3] = States[3] + 1;
break;
case 4:
States[1] = States[1] + 2;
States[3] = States[3] - 1;
break;
case 5:
States[0] = States[0] - 1;
States[1] = States[1] + 1;
break;
case 6:
States[0] = States[0] + 1;
States[1] = States[1] - 1;
default:
break;
}
}
};
void Gillespie(System_States States, long Maxsteps, long Maxtime, long SampleNumber)
{
double T= 0.0;
long step = 0;
ofstream fout("data." + to_string(SampleNumber));
while (T <= Maxtime && step <= Maxsteps)
{
fout << T << "" << States.States[0] << "" << States.States[1] << "" << States.States[2] << "" << States.States[3] << "\n";
States.Propensity_function();
States.Update_Tau();
States.Update_Reaction();
States.Update_States();
T= T+ States.Tau;
step = step + 1;
}
fout.close();
};
int main()
{
using namespace std;
srand((unsigned int)time(NULL));
System_States s1;
Gillespie(s1, 1000, 100000, 1);
return 0;
}
[此贴子已经被作者于2021-11-6 09:47编辑过]