Last week's puzzler didn't get a lot of traffic, but hopefully this one will be a bit more fun. I've always been interested in simple population/ecological simulations. I thought it might be interesting to build such a tool in ColdFusion. First, let's contrive a situation.
You work for MicroBucks, a multi-billion dollar corporation that produces top quality cow bells. Unfortunately, producing cow bells results in a nasty byproduct: Element X. Element X isn't exactly safe for humans (it causes dandruff and leaky bowels), so obviously the solution is to dump it in the local lake.
However, Element X tends to kill off fish in the pond. As we all know, fish really aren't that important, so we don't mind killing fish, but we don't want to kill all the fish or some local tree hugger may go crazy.
So here is your problem. You need to write a program that lets MicroBucks determine how much of Element X they can dump every period and get away with it. Your program will take 4 inputs:
- The first input is the population of fish.
- The second input is their reproductive rate per period. This is a percentage from 1 to 100.
- The third input is the number of units of Element X to dump in the lake per period.
- The fourth input is the lethality of Element X. For ever y N units of Element X, one fish wlll be killed per period. So if you entered 10, it would mean that for every 10 units of Element X you would kill one fish.
The program will accept the four inputs and then graph the results. In every period you first kill off the fish based on the lethality. You then let the fish "have fun" and reproduce. Since you are using a percentage you should round down since you can't give birth to half a fish. You can graph the results over 30, 60, or however many periods you think make sense.
Archived Comments
This reminds me of a turtles game in the back of a 3-2-1 contact magazine when I was in 4th grade. It grew turtles along a beach and you entered inputs and took care of them. It was all in Basic (good old basic) and was actually pretty neat. Sorry about OT =)
Hi,
here is my solution:
<cfparam name="url.population">
<cfparam name="url.repro">
<cfparam name="url.units">
<cfparam name="url.lethality">
<cffunction name="getPopulation">
<cfargument name="pop">
<cfargument name="let">
<cfargument name="units">
<cfargument name="rep">
<cfset var kill = int(arguments.units / arguments.let) />
<cfset var fish = arguments.pop - kill />
<cfset fish = int(fish*(1+(arguments.rep / 100)))>
<cfreturn fish />
</cffunction>
<cfif structKeyExists(url,"submit")>
<cfset popQuery = queryNew("period,population")>
<cfset pop = url.population />
<cfloop from="1" to="30" index="i">
<cfif pop lt 0>
<cfbreak>
</cfif>
<cfset newrow = queryAddRow(popQuery,1)>
<cfset temp = querySetCell(popQuery,"period",i,i) />
<cfset temp = querySetCell(popQuery,"population",pop,i) />
<cfset pop = getPopulation(pop,url.lethality,url.units,url.repro) />
</cfloop>
<cfchart chartheight="400" chartwidth="700" xaxistitle="Periods" yaxistitle="Population">
<cfchartseries type="curve" query="popQuery" itemcolumn="period" valuecolumn="population" />
</cfchart>
</cfif>
<cfform action="#cgi.SCRIPT_NAME#" method="get">
<cfoutput>
<label >Population:
<input type="text" value="#url.population#" name="population" /></label><br />
<label >Reprodutive Rate:
<input type="text" value="#url.repro#" name="repro" /></label><br />
<label >Unit per period:
<input type="text" value="#url.units#" name="units" /></label><br />
<label >Lethality
<input type="text" value="#url.lethality#" name="lethality" /></label><br />
<input type="submit" name="submit" value="submit" />
</cfoutput>
</cfform>
Cool little exercise =) Sorry if the formatting gets all screwy...
Simple form:
<form action="fish_death.cfm" method="post">
Current fish population <input type="text" name="currentPopulation"><br>
Reproductive rate <input type="text" name="reproductiveRate"><br>
Units of Element X put into the lake per period <input type="text" name="unitsOfDeath"><br>
Lethality (number of fish killed per unit of Element X) <input type="text" name="lethality"><br>
<input type="submit" value="Kill some fish!">
</form>
A function to kill some fish:
<cffunction name="killFish" access="public" returntype="numeric" hint="Returns the new fish population of the lake based on the given data. Assume the fish have their fun and reproduce before being killed off (lucky little beggers)">
<cfargument name="currentPopulation" type="numeric" required="yes" hint="The current population of the lake"/>
<cfargument name="reproductiveRate" type="numeric" required="yes" hint="The percentage the fish population will grow by in a period"/>
<cfargument name="unitsOfDeath" type="numeric" required="yes" hint="The number of units of Element X dumped into the lake in a period"/>
<cfargument name="lethality" type="numeric" required="yes" hint="The number of fish that 1 unit of Element X will kill in a period"/>
<cfset var newPopulation = 0 />
<cfset var fishKilled = int(arguments.unitsOfDeath / arguments.lethality) />
<cfif arguments.reproductiveRate gt 0 and arguments.reproductiveRate lte 100>
<cfset newPopulation = (int(arguments.currentPopulation * (arguments.reproductiveRate/100)) + currentPopulation) - fishKilled />
</cfif>
<cfreturn newPopulation />
</cffunction>
And the action page with a chart:
<cfset newPopulation = form.currentPopulation />
<cfset fishBreeder = CreateObject("component","Fish.Fish") />
<cfchart format="flash" xaxistitle="Number of periods passed" yaxistitle="Fish population" chartwidth="800" chartheight="600"
title="Fish population, putting #form.unitsOfDeath# units of Element X into the lake every period, with #form.lethality# units killing 1 fish">
<cfchartseries type="line" markerstyle="circle">
<cfloop index="i" from="1" to="60">
<cfset newPopulation = fishBreeder.killFish(currentPopulation=newPopulation,reproductiveRate=form.reproductiveRate,unitsOfDeath=form.unitsOfDeath,lethality=form.lethality) />
<cfchartdata item="#i#" value="#newPopulation#"/>
</cfloop>
</cfchartseries>
</cfchart>
I forgot to mention. If you want to post the source code and an example on your own site, that is fine.