' ---------------------------------------------- ' NAME: SimulationData ' DESCRIPTION: Holds information about each ' run cycle of a given simulation ' ---------------------------------------------- TYPE SimulationData CycleNumber AS INTEGER TimeMark AS INTEGER PredatorCount AS DOUBLE TotalPopulation AS DOUBLE PopulationDifference AS DOUBLE DifferencePredators AS DOUBLE PercentagePredators AS DOUBLE PreyCount AS DOUBLE DifferencePreys AS DOUBLE PercentagePreys AS DOUBLE END TYPE ' --------------------------------------- ' Globally Shared Variable Declarations ' --------------------------------------- DIM SHARED CanExitSimulation AS INTEGER DIM SHARED LongestRun AS INTEGER DIM SHARED AggressiveFactor AS DOUBLE DIM SHARED TotalPopulation AS DOUBLE DIM SHARED NumberOfPredators AS DOUBLE DIM SHARED NumberOfPreys AS DOUBLE DIM SHARED RunCount AS INTEGER DIM SHARED Counter AS INTEGER DIM SHARED Answer AS STRING DIM SHARED SimulationRuns() AS SimulationData ' -------------------------------------------- ' Set up all initial data for the simulation ' -------------------------------------------- RANDOMIZE TIMER AggressiveFactor = RND(1) ' --------------------------------------------------------------------------- ' Loop to keep us simulation until we answer n or N to "Another Simulaion?" ' --------------------------------------------------------------------------- DO WHILE CanExitSimulation <> -1 RunCount = 1 ' ------------------------------------ ' Inquire for the initial population ' ------------------------------------ CLS PRINT "Predator / Prey simulation" PRINT "By MystikShadows" PRINT PRINT "Current Aggressive Factor is: ";: PRINT AggressiveFactor PRINT INPUT "Enter the number of Predators: "; NumberOfPredators INPUT "Enter the number of Preys: "; NumberOfPreys ' ------------------------------------------------------- ' Prepare the array for it's initial data and assign it ' ------------------------------------------------------- REDIM SimulationRuns(1 TO RunCount) AS SimulationData TotalPopulation = NumberOfPredators + NumberOfPreys SimulationRuns(RunCount).CycleNumber = RunCount SimulationRuns(RunCount).TimeMark = 0 SimulationRuns(RunCount).TotalPopulation = NumberOfPredators + NumberOfPreys SimulationRuns(RunCount).PopulationDifference = 0 SimulationRuns(RunCount).PredatorCount = NumberOfPredators SimulationRuns(RunCount).DifferencePredators = 0 SimulationRuns(RunCount).PercentagePredators = NumberOfPredators * 100 / TotalPopulation SimulationRuns(RunCount).PreyCount = NumberOfPreys SimulationRuns(RunCount).DifferencePreys = 0 SimulationRuns(RunCount).PercentagePreys = NumberOfPreys * 100 / TotalPopulation ' ---------------------- ' Main Simulation Loop ' ---------------------- DO WHILE NumberOfPredators > 1 AND NumberOfPreys > 1 ' --------------------------------- ' We first Increment the RunCount ' --------------------------------- RunCount = RunCount + 1 ' ----------------------------------------------------------- ' Next we calculate the new populations ' Note you can play with these formulas to see what happens ' ----------------------------------------------------------- NumberOfPredators = NumberOfPredators + _ (( 8 * NumberOfPredators - _ NumberOfPredators * NumberOfPreys / 3 ) * _ AggressiveFactor ) NumberOfPreys = NumberOfPreys + _ (( 4 * NumberOfPreys - _ NumberOfPreys * NumberOfPredators ) * .01 ) TotalPopulation = NumberOfPredators + NumberOfPreys ' -------------------------------------- ' Next we add the results to our array ' -------------------------------------- REDIM PRESERVE SimulationRuns(1 TO RunCount) AS SimulationData SimulationRuns(RunCount).CycleNumber = RunCount - 1 SimulationRuns(RunCount).TimeMark = RunCount - 1 SimulationRuns(RunCount).TotalPopulation = NumberOfPredators + NumberOfPreys SimulationRuns(RunCount).PopulationDifference = SimulationRuns(RunCount - 1).TotalPopulation + (NumberOfPredators + NumberOfPreys) SimulationRuns(RunCount).PredatorCount = NumberOfPredators SimulationRuns(RunCount).DifferencePredators = SimulationRuns(RunCount - 1).PredatorCount - NumberOfPredators SimulationRuns(RunCount).PercentagePredators = NumberOfPredators * 100 / TotalPopulation SimulationRuns(RunCount).PreyCount = NumberOfPreys SimulationRuns(RunCount).DifferencePreys = SimulationRuns(RunCount - 1).PreyCount - NumberOfPreys SimulationRuns(RunCount).PercentagePreys = NumberOfPreys * 100 / TotalPopulation ' ----------------------------------------- ' Display Current Information To The User ' ----------------------------------------- PRINT "Current Cycle: "; : PRINT USING "#,###"; RunCount - 1; PRINT " Predators: "; : PRINT USING "###"; NumberOfPredators; PRINT " Preys: "; : PRINT USING "###"; NumberOfPreys SLEEP 2000 LOOP ' --------------------------------- ' Adjust The Longest Run Variable ' --------------------------------- IF RunCount > LongestRun THEN LongestRun = RunCount - 1 END IF ' ------------------------------- ' Display The Simulation Report ' ------------------------------- CLS ' --------------------------- ' Display The Report Header ' --------------------------- PRINT " =========================================================================" PRINT " S I M U L A T I O N R E P O R T" PRINT " =========================================================================" PRINT PRINT " Aggressiveness Factor.......: ";:PRINT USING "###.#####"; AggressiveFactor PRINT " Initial Predator Population.: ";:PRINT USING " #,###"; INT(SimulationRuns(1).PredatorCount) PRINT " Initial Prey Population.....: ";:PRINT USING " #,###"; INT(SimulationRuns(1).PreyCount) PRINT ' ---------------------------- ' Display The Column Headers ' ---------------------------- PRINT " =========================================================================" PRINT " CYCLE TOTAL PREDATORS PREYS PERCENTAGES PRINT " COUNT POPULATION TOTAL DIFFERENCE TOTAL DIFFERENCE PREDATORS PREYS PRINT " -------------------------------------------------------------------------" ' ----------------------------------------------- ' Loop through the array to display the results ' ----------------------------------------------- FOR Counter = 1 TO UBOUND(SimulationRuns) PRINT USING " ##### ";Counter - 1; PRINT USING " ##### "; SimulationRuns(Counter).TotalPopulation; PRINT USING " ##### "; SimulationRuns(Counter).PredatorCount; PRINT USING " ##### "; SimulationRuns(Counter).DifferencePredators; PRINT USING " ##### "; SimulationRuns(Counter).PreyCount; PRINT USING " ##### "; SimulationRuns(Counter).DifferencePreys; PRINT USING " ###.##%"; SimulationRuns(Counter).PercentagePredators; PRINT USING " ###.##% "; SimulationRuns(Counter).PercentagePreys NEXT Counter PRINT " =========================================================================" PRINT ' -------------------------------------------- ' Display The Longest Simulation Run To Date ' -------------------------------------------- PRINT " The Best Simulation So Far Ran for " + STR$(LongestRun) + " Cycles." PRINT ' -------------------------------------------------- ' Enter Loop To Inquire for another Simulation Run ' -------------------------------------------------- DO WHILE Answer = "" OR (UCASE$(Answer) <> "Y" OR UCASE$(Answer) <> "N") INPUT " Run another simulation? (Y/N) "; Answer IF UCASE$(Answer) = "N" THEN CanExitSimulation = -1 END IF LOOP LOOP