public float calc_Fox_Runge_Kutta(float f, float r, float ts) { foxws.foxws fproxy = new foxws.foxws(); float k1 = 0; float k2 = 0; float k3 = 0; float k4 = 0; k1 = fproxy.foxpop_inc(f, r); //f(y) float tmp = f + (ts / 2) * k1; k2 = fproxy.foxpop_inc((tmp), r); //f(y+(h/2)k1) tmp = f + (ts / 2) * k2; k3 = fproxy.foxpop_inc((tmp), r); //f(y+(h/2)k2) tmp = f + ts * k3; k4 = fproxy.foxpop_inc((tmp), r); //f(y+ hk3) f = f + ts*(k1 + 2 * k2 + 2 * k3 + k4) / 6; //y + h*(k1 + 2 * k2 + 2 * k3 + k4)/6 return f; }
protected double[,] hybrid_horizontal_same_ts(float r, float f, int yr, float ts, string r_integ_method, string f_integ_method) { //objective: the two models run with ts=0.1 but they exchange data at multiple of 1 foxws.foxws fproxy = new foxws.foxws(); rabws.rabws rproxy = new rabws.rabws(); //the system will loop according to the defined time step //int lp = Convert.ToInt16(Math.Truncate(yr / ts)); //total number of loop due to the defined time step int lp = Convert.ToInt32(Math.Truncate(yr / 0.1)); //10 timestep in 1 year lp = lp + 2; double[,] result = new double[lp, 2]; float rx = r; float ry = f; float fx = r; float fy = f; for (int i = 1; i < lp; i++) { if (i % (Convert.ToInt16(ts * 10)) == 0) { //run rabbit model if (r_integ_method == "Runge-Kutta") // for Runge-Kutta integration of rabbit { r = calc_Rabbit_Runge_Kutta(r, f, ts); } else rx = rx + rproxy.pop_inc(rx, ry) * ts; //for euler integration //r = r + rabbit_model(r, f) * ts; //run fox model if (f_integ_method == "Runge-Kutta") // for Runge-Kutta integration of fox model { f = calc_Fox_Runge_Kutta(f, r, ts); } else fy = fy + fproxy.foxpop_inc(fy, fx) * ts; //for euler integration //f = f + fox_model(f, r) * ts; if ((i % 10) == 0) //when ts is 1,2,3, ... then the models exchange data { ry = f; fx = r; } r = rx; f = fy; if (r < 0) result[i, 0] = 0; else result[i, 0] = r; //Convert.ToDouble(r.ToString("#.##")); if (f < 0) result[i, 1] = 0; else result[i, 1] = f; // Convert.ToDouble(f.ToString("#.##")); if (result[i, 0] < 0) //(result[i, 0] < 1) { result[i, 0] = 0; //if pop<0 then set the pop value to 0 result[i, 1] = 0; break; } } else { if (r < 0) //(result[i, 0] < 1) { result[i, 0] = 0; //if pop<0 then set the pop value to 0 result[i, 1] = 0; break; } else { result[i, 0] = r; result[i, 1] = f; } } } return result; }
protected double[,] horizontal_same_ts(float r, float f, int yr, float ts, string r_integ_method, string f_integ_method) { //ts = Convert.ToSingle(0.1); foxws.foxws fproxy = new foxws.foxws(); //rabws.Service1 rproxy = new Service1(); rabws.rabws rproxy = new rabws.rabws(); //the system will loop according to the defined time step //int lp = Convert.ToInt16(Math.Truncate(yr / ts)); //total number of loop due to the defined time step int lp = Convert.ToInt32(Math.Truncate(yr / 0.1)); //10 timestep in 1 year lp = lp+2; double[,] result = new double[lp, 2]; float fr = r; //rabbit population for fox model for (int i = 1; i < lp; i++) { //if (i % (Convert.ToInt16(Math.Truncate(ts * 10))) == 0) if (i % (Convert.ToInt16(ts*10)) == 0) { //run rabbit model if (r_integ_method == "Runge-Kutta") // for Runge-Kutta integration of rabbit { //r = calc_Rabbit_Runge_Kutta(r, f, ts); r = diff_Rabbit_Runge_Kutta(r, f, ts); } else //r = r + rproxy.pop_inc(r, f) * ts; //for euler integration r = r + rabbit_model(r, f) * ts; // r = r + rabbit_with_diff_func(r, f) * ts; //rabbit with px/(q+x) function than bx //run fox model if (f_integ_method == "Runge-Kutta") // for Runge-Kutta integration of fox model { //f = calc_Fox_Runge_Kutta(f, fr, ts); f = diff_Fox_Runge_Kutta(f, fr, ts); } else //f = f + fproxy.foxpop_inc(f, fr) * ts; //for euler integration f = f + fox_model(f, fr) * ts; //f = f + fox_with_diff_func(f, fr) * ts; //fox with px/(q+x) function than bx fr = r; //update current rabbit population for fox model if (r < 0) result[i, 0] = 0; else result[i, 0] = r; //Convert.ToDouble(r.ToString("#.##")); if (f < 0) result[i, 1] = 0; else result[i, 1] = f; // Convert.ToDouble(f.ToString("#.##")); if (result[i, 0] < 0) //(result[i, 0] < 1) { result[i, 0] = 0; //if pop<0 then set the pop value to 0 result[i, 1] = 0; break; } } else { if (r < 0) //(result[i, 0] < 1) { result[i, 0] = 0; //if pop<0 then set the pop value to 0 result[i, 1] = 0; break; } else { result[i, 0] = r; result[i, 1] = f; } } } return result; }
protected void horizontal_diff_ts(float r, float r_ts, float f, float f_ts, int yr, out double[] r_result, ref double[] f_result, string r_integ_method, string f_integ_method) { foxws.foxws fproxy = new foxws.foxws(); rabws.rabws rproxy = new rabws.rabws(); //the system will loop for each time step int lp = Convert.ToInt32(Math.Truncate(yr / 0.1)); //total number of loop due to the defined year lp = lp + 1; r_result = new double[lp]; //array for rab f_result = new double[lp]; //array for fox double[] t = new double[3]; t[1] = Convert.ToDouble(r_ts.ToString("#.##")); //time step of rabbit t[2] = Convert.ToDouble(f_ts.ToString("#.##")); //time step of fox int temp_r_ts = Convert.ToInt16(r_ts * 10); int temp_f_ts = Convert.ToInt16(f_ts * 10); for (int i = 1; i < lp; i++) { double tmp = Convert.ToDouble(i * 0.1); if (i == 0) //t[0] is current time step for the loop since each loop is time step t[0] = 0; else t[0] = Convert.ToDouble(tmp.ToString("#.##")); int m = i % temp_r_ts; //m for maintaining result of % operation if (m == 0 && i>0) //check if the current time step is valid to run rabit model { if (r_integ_method == "Runge-Kutta") // Runge-Kutta integration of rabbit model { r = calc_Rabbit_Runge_Kutta(r, f, r_ts); } else //r = r + rproxy.pop_inc(r, f) * r_ts; r = r + rabbit_model(r, f) * r_ts; } r_result[i] = r; // Convert.ToDouble(r.ToString("#.##")); m = i % temp_f_ts; if (m == 0 && i>0) //check if the current time step is valid to run fox model { if (f_integ_method == "Runge-Kutta") // for Runge-Kutta integration of fox model { f = calc_Fox_Runge_Kutta(f, r, f_ts); } else //f = f + fproxy.foxpop_inc(f, r) * f_ts; f = f + fox_model(f,r) * f_ts; } if (f >= 0) f_result[i] = f; else //if fox pop <0 set it to 0 { f = 0; f_result[i] = 0; } if (r_result[i] < 0) { r_result[i] = 0;//if rab pop<0 then set the pop value to 0 and quit f_result[i] = 0; break; } } }
protected void horizontal_diff_2_ts(float r, float r_ts, float f, float f_ts, int yr, out double[] r_result, ref double[] f_result, string r_integ_method, string f_integ_method) { foxws.foxws fproxy = new foxws.foxws(); rabws.rabws rproxy = new rabws.rabws(); //the system will loop for each time step int lp = Convert.ToInt32(Math.Truncate(yr / 0.1)); //total number of loop due to the defined year lp = lp + 1; r_result = new double[lp]; //array for rab f_result = new double[lp]; //array for fox double[] t = new double[3]; t[1] = Convert.ToDouble(r_ts.ToString("#.##")); //time step of rabbit t[2] = Convert.ToDouble(f_ts.ToString("#.##")); //time step of fox int temp_r_ts = Convert.ToInt16(r_ts * 10); int temp_f_ts = Convert.ToInt16(f_ts * 10); float rx = r; float ry = f; float fx = r; float fy = f; for (int i = 1; i < lp; i++) { int m = i % temp_r_ts; //m for maintaining result of % operation if (m == 0 && i > 0) //check if the current time step is valid to run rabit model { if (r_integ_method == "Runge-Kutta") // Runge-Kutta integration of rabbit model { rx = calc_Rabbit_Runge_Kutta(rx, ry, r_ts); } else //rx = rx + rproxy.pop_inc(rx, ry) * r_ts; rx = rx + rabbit_model(rx, ry) * r_ts; //rx = rx + rabbit_with_diff_func(rx, ry) * r_ts; r = rx; } int n = i % temp_f_ts; if (n == 0 && i > 0) //check if the current time step is valid to run fox model { if (m == 0) fx = r; if (f_integ_method == "Runge-Kutta") // for Runge-Kutta integration of fox model { fy = calc_Fox_Runge_Kutta(fy, fx, f_ts); } else //fy = fy + fproxy.foxpop_inc(fy, fx) * f_ts; fy = fy + fox_model(fy, fx) * f_ts; //fy = fy + fox_with_diff_func(fy, fx) * f_ts; f = fy; fx = r; } if(m==0) //if the time step meets ts of rabbit then copy value of f ry = f; r_result[i] = r; // Convert.ToDouble(r.ToString("#.##")); if (f >= 0) f_result[i] = f; else //if fox pop <0 set it to 0 { f = 0; f_result[i] = 0; } if (r_result[i] < 0) { r_result[i] = 0;//if rab pop<0 then set the pop value to 0 and quit f_result[i] = 0; break; } } }
protected void horizontal_different_ts(float r, float r_ts, float f, float f_ts, int yr, out double[] r_result, ref double[] f_result, string r_integ_method, string f_integ_method) { foxws.foxws fproxy = new foxws.foxws(); rabws.rabws rproxy = new rabws.rabws(); //the system will loop for each time step int lp = Convert.ToInt32(Math.Truncate(yr / 0.1)); //total number of loop due to the defined year lp = lp + 1; r_result = new double[lp]; //array for rab f_result = new double[lp]; //array for fox //rx_result = new double[lp]; //ry_result = new double[lp]; //fx_result = new double[lp]; //fy_result = new double[lp]; double[] t = new double[3]; t[1] = Convert.ToDouble(r_ts.ToString("#.##")); //time step of rabbit t[2] = Convert.ToDouble(f_ts.ToString("#.##")); //time step of fox int temp_r_ts = Convert.ToInt16(r_ts * 10); int temp_f_ts = Convert.ToInt16(f_ts * 10); float rx = r; //recent value of rabbit population for rabbit model float ry = f; //recent value of fox population for rabbit model float fx = r; //recent value of rabbit population for fox model float fy = f; //recent value of fox population for fox model for (int i = 1; i < lp; i++) { double tmp = Convert.ToDouble(i * 0.1); if (i == 0) //t[0] is current time step for the loop since each loop is time step t[0] = 0; else t[0] = Convert.ToDouble(tmp.ToString("#.##")); //now for rabbit model int m = i % temp_r_ts; //m for maintaining result of % operation if (m == 0 && i > 0) //check if the current time step is valid to run rabit model { if (r_integ_method == "Runge-Kutta") // Runge-Kutta integration of rabbit model { rx = calc_Rabbit_Runge_Kutta(rx, ry, r_ts); } else //rx = rx + rproxy.pop_inc(rx, ry) * r_ts; rx = rx + rabbit_model(rx, ry) * r_ts; r = rx; //update recent value of r using recent output of rx ry = f; //copy recent pop of fox for next run } r_result[i] = r; // Convert.ToDouble(r.ToString("#.##")); //now for fox model int n = i % temp_f_ts; //n for maintaining result of % operation if (n == 0 && i > 0) //check if the current time step is valid to run fox model { if (f_integ_method == "Runge-Kutta") // for Runge-Kutta integration of fox model { fy = calc_Fox_Runge_Kutta(fy, fx, f_ts); } else //fy = fy + fproxy.foxpop_inc(fy, fx) * f_ts; fy = fy + fox_model(fy,fx) * f_ts; //update recent fox pop value if (fy >= 0) { f = fy; } else //if fox pop <0 set it to 0 { fy = 0; f = 0; } //copy recent rabbit pop for next run fx = r; } f_result[i] = f; if (n == 0 && m == 0) //if both models run then rabbit should copy recent value of fox since it is already run before fox { ry = f; } //rx_result[i] = rx; //ry_result[i] = ry; //fx_result[i] = fx; //fy_result[i] = fy; //if rab pop<0 then set the pop value to 0 and quit if (r_result[i] < 0) { r_result[i] = 0; f_result[i] = 0; break; } } }
protected double[,] vertical(int r, int f, int yr) { //rabws.Service1 rproxy = new Service1(); rabws.rabws rproxy = new rabws.rabws(); foxws.foxws fproxy = new foxws.foxws(); yr = yr + 1; int[] rab = rproxy.pop_list(r, yr); double[,] result = new double[yr, yr]; result[0, 0] = Convert.ToDouble(r); result[0, 1] = Convert.ToDouble(f); for (int i = 0; i < yr; i++) { //r = r + rproxy.pop_increase(r, f); f = f + fproxy.pop_inc(f, r); if (r <= 0 || f <= 0) break; result[i, 0] = Convert.ToDouble(rab[i]); result[i, 1] = Convert.ToDouble(f); } return result; }
protected double[,] horizontal(int r, int f, int yr) { foxws.foxws fproxy = new foxws.foxws(); //rabws.Service1 rproxy = new Service1(); rabws.rabws rproxy = new rabws.rabws(); double[,] result = new double[yr,yr]; result[0,0] = Convert.ToDouble(r); result[0,1] = Convert.ToDouble(f); for (int i = 0; i < yr; i++) { r = r + rproxy.pop_increase(r, f); f = f + fproxy.pop_inc(f, r); if (r <= 0 || f <= 0) break; result[i,0] = Convert.ToDouble(r); result[i,1] = Convert.ToDouble(f); } return result; }