Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args">Array of four mortality parameters (n, l, m, h)</param>
        /// <param name="logToFile">If false, program only outputs to Console. If true, program outputs to file rather than Console</param>
        private static void batchMode(string[] args)
        {
            // Get mortality parameters
            MortalityParam mortMaternal, mortSiblingComp, mortSiblingHelp, mortChildInfluence, mortMotherInfluence;

            StreamWriter sw = null;

            // run backwards and forwards using passed mortality params (no user input required)
            // population name
            string population = args[0];

            //
            mortMaternal    = parseArgs(args[1]);
            mortSiblingComp = parseArgs(args[2]);
            mortSiblingHelp = parseArgs(args[3]);
            // Siler params
            double silerA1 = double.Parse(args[4]);
            double silerB1 = double.Parse(args[5]);
            double silerA2 = double.Parse(args[6]);
            double silerA3 = double.Parse(args[7]);
            double silerB3 = double.Parse(args[8]);

            string births    = "";
            string popMatrix = "";
            string rFile     = "";

            if (args.Length == 10)
            {
                births = args[9];
            }
            else if (args.Length >= 11)
            {
                births    = args[9];
                popMatrix = args[10];
                rFile     = args[11];
            }

            // Return a string of mortality parameter values (e.g. medium, low, medium, high returns "MLMH")
            string sp1 = Enum.GetName(typeof(MortalityParam), mortMaternal).Substring(0, 1);
            string sp2 = Enum.GetName(typeof(MortalityParam), mortSiblingComp).Substring(0, 1);
            string sp3 = Enum.GetName(typeof(MortalityParam), mortSiblingHelp).Substring(0, 1);
            string p   = population + "-" + sp1 + sp2 + sp3; // +sp4 + sp5;

            // set up log file
            string logFilename = "Log-" + p + ".log";

            Console.WriteLine("(Running in batch mode)");
            Console.WriteLine();

            // create and initialise model
            Console.WriteLine("-- initialising model");
            BirthIntervals sdp = new BirthIntervals();

            Console.WriteLine("-- model initialised");

            // set parameters of model
            sdp.population          = population;
            sdp.childbirthMortality = mortMaternal;
            sdp.siblingCompetition  = mortSiblingComp;
            sdp.siblingHelp         = mortSiblingHelp;

            // set Siler params
            sdp.silerA1 = silerA1;
            sdp.silerB1 = silerB1;
            sdp.silerA2 = silerA2;
            sdp.silerA3 = silerA3;
            sdp.silerB3 = silerB3;

            Console.WriteLine(PrintParams(mortMaternal, mortSiblingComp, mortSiblingHelp));

            if (args.Length < 10)
            {
                // calculate optimal birth intervals
                Console.WriteLine("-- beginning backward iteration");
                sdp.runBackward();
                Console.WriteLine("-- backward iteration finished");

                // output birth decisions and fitness here
                Console.WriteLine("-- writing birth decisions to disk");
                sdp.outputBirthDecisions();
                sdp.outputGrowthRate();
                Console.WriteLine("-- birth decisions written to disk");

                Console.WriteLine("-- beginning forward iteration");
                sdp.runForward();
                Console.WriteLine("-- forward iteration finished");

                Console.WriteLine("-- writing population data to disk");
                sdp.outputGrowthRate();
                Console.WriteLine("-- population data written to disk");
            }
            if (args.Length == 10)
            {
                Console.WriteLine("-- importing births from {0}", births);
                sdp.importBirthDecisionsFromFile(births);
                Console.WriteLine("-- birth decisions imported");

                Console.WriteLine("-- beginning forward iteration");
                sdp.runForward();
                Console.WriteLine("-- forward iteration finished");

                Console.WriteLine("-- writing population data to disk");
                sdp.outputGrowthRate();
                Console.WriteLine("-- population data written to disk");
            }
            else if (args.Length >= 11)
            {
                Console.WriteLine("-- importing births from {0}", births);
                sdp.importBirthDecisionsFromFile(births);
                Console.WriteLine("-- birth decisions imported");

                Console.WriteLine("-- importing population matrix from {0}", popMatrix);
                sdp.importPopulationFromFile(popMatrix);
                Console.WriteLine("-- population matrix imported");

                Console.WriteLine("-- importing population growth rate from {0}", rFile);
                sdp.importPopulationGrowth(rFile);
                Console.WriteLine("-- population growth rate imported");

                Console.WriteLine("-- importing population matrix from {0}", popMatrix);
                sdp.importPopulationFromFile(popMatrix);
                Console.WriteLine("-- population matrix imported");
            }

            Console.WriteLine("-- calculating stats");
            sdp.calcStats();
            Console.WriteLine("-- calculated stats");

            Console.WriteLine("-- writing stats data to disk");
            sdp.outputStats();
            Console.WriteLine("-- stats data written to disk");
        }
Example #2
0
        private static void interactiveMode()
        {
            Console.WriteLine("(Running in interactive mode)");
            Console.WriteLine();

            Console.WriteLine("Please enter mortality parameters (h=high, m=medium, l=low, n=none):");
            Console.Write("Risk of mortality during childbirth: ");
            MortalityParam p1 = parseArgs(Console.ReadLine());

            Console.Write("Intensity of sibling competition: ");
            MortalityParam p2 = parseArgs(Console.ReadLine());

            Console.Write("Level of juvenile help: ");
            MortalityParam p3 = parseArgs(Console.ReadLine());

            Console.WriteLine(PrintParams(p1, p2, p3));

            Console.Write("Enter Siler parameter a1: ");
            double silerA1 = double.Parse(Console.ReadLine());

            Console.Write("Enter Siler parameter b1: ");
            double silerB1 = double.Parse(Console.ReadLine());

            Console.Write("Enter Siler parameter a2: ");
            double silerA2 = double.Parse(Console.ReadLine());

            Console.Write("Enter Siler parameter a3: ");
            double silerA3 = double.Parse(Console.ReadLine());

            Console.Write("Enter Siler parameter b3: ");
            double silerB3 = double.Parse(Console.ReadLine());

            bool done = false;

            // create and initialise model
            sdpInteractive = new BirthIntervals();

            // set parameters of model
            sdpInteractive.childbirthMortality = p1;
            sdpInteractive.siblingCompetition  = p2;
            sdpInteractive.siblingHelp         = p3;

            // set Siler params
            sdpInteractive.silerA1 = silerA1;
            sdpInteractive.silerB1 = silerB1;
            sdpInteractive.silerA2 = silerA2;
            sdpInteractive.silerA3 = silerA3;
            sdpInteractive.silerB3 = silerB3;

            do
            {
                // Main menu
                Console.WriteLine();
                Console.WriteLine("1 = calculate optimal interbirth intervals (backward iteration)");
                Console.WriteLine("2 = calculate population distributions (forward iteration)");
                Console.WriteLine("3 = calculate stats");
                Console.WriteLine("4 = full model run");
                Console.Write("Please select an option (0 = quit): ");

                string input = Console.ReadLine();

                int option;

                try
                {
                    option = int.Parse(input);
                }
                catch (FormatException e)
                {
                    Console.WriteLine("Invalid option. Try again");
                    continue;
                }

                if (option == 0)
                {
                    done = true;
                    continue;
                }
                else if (option == 1)
                {
                    back();
                }
                else if (option == 2)
                {
                    forward();
                }
                else if (option == 3)
                {
                    stats();
                }
                else if (option == 4)
                {
                    back();
                    forward();
                    stats();
                }
                else
                {
                    Console.WriteLine("{0} is an invalid option.", option);
                }
            } while (!done);
        }