Showing posts with label agent-based simulation. Show all posts
Showing posts with label agent-based simulation. Show all posts

25.11.14

Paper Published

I've finally had my main thesis chapter published! A lot of hard work and two rounds of edits following reviewer comments and it has finally been released online. See it here for non-paywall freely available copy:

A spatially explicit agent-based model of the interactions between jaguar populations and their habitats:

https://drive.google.com/file/d/0B2itFn5PP0TCenl5cWpKazFBUmM/view?usp=sharing

This should be freely available now. Let me know if there are any problems.

29.11.13

Model fail.. reasons unknown

Todays post is mainly just a summing up of model results following some recent analysis that has shook the foundations of my trust in my model.

As it turns out, when you have a complex model, analysing results can be complex and assigning blame to any particular parameter/process can be even more challenging.

I've been happily plodding along in the knowledge that I finally have my model churning out some decent results, with the right things happening at the right time in the right places. One final analysis has turned this on its head with the finding that territory sizes are not properly linked to food resources. I have suspected this has been the case for some time, but following the reduction of food resources outside of the protected area I find that male territories in the matrix are about 3/4 the size of territories inside the CBWS forest. Females surprisingly are the reverse, but I think this is mainly due to most females being driven out of CBWS due to avoidance of males. In the matrix it seems they are able to expand their territories and are not so constrained due to the presence of fewer males. The average size of their territories is also unfeasibly small inside CBWS and nowhere near the size that would be needed in the real world to maintain a single individual....

crap.

So it seems a pretty fundamental feature of my model is not working as it should and I am at a pretty big loss as to why this might be the case.

Looking at males, they are attracted to trails, and attracted to male pheromones on trails. This causes larger territory sizes as they move along trails, pop out further into the forest and create new areas to call their own. Individuals are seemingly not linked to any particular area and territories are completely transitory. I wonder whether this is the problem? Males, due to this feature, also exhibit larger territory sizes when there are more males around, presumably because they get drawn onto trails. However, having a process that allows males to move further along trails causes a population crash in pretty short time (i.e. around year 20 of the simulation). I'm not quite sure why, but I'm doing some tests to figure out if the number of moves makes any difference to the fate of the population.

Looks like I might have to go back to the drawing board.

This is not the news I wanted with 6 months left on my PhD!

What do I want:
Males move along trails where possible
Males follow other males along trails, for some suitable distance.
All individuals have larger territories outside of the protected area as they need to move more to consume sufficient food resources
Females avoid males but generally their territories are overlapped to a large extend by males
Males move much faster along trails
Males move much faster when following a female to mate
All individuals would prefer to be inside the protected area


21.10.13

Jaguar population model parameters

So I haven't updated this for a while... I've been busy re-doing most of what I've spent all of this year doing. VERY frustrating!

Basically, after finishing my PhD transfer report I showed my work (all the output from my jaguar population model) to my collaborators in Belize - researchers who have been working with jaguars for years, I found that some of the key outputs from the model were not close enough to realistic values for jaguar populations in the wild. Namely, territory sizes and the distances that individuals moved over time. Those things that seemed pretty critical to the emergence of a stable population size. In fact, upon initial attempts to increase territory sizes, my stable population became stable no longer. Hence, I've spent the last 3 months re-doing all the tedious trial and error work I did during the previous 3 months in trying to get a stable population size over time that exhibited more realistic individual behavioural characteristics.

I've just about finished doing all the work and getting all the output. Ive harnessed the power of 2 computers at the university to do the bulk of the simulation work which I have managed to get remote access to. This has made the whole thing a lot easier as I don't have daily access to the computers in person. This has also freed up my own computer to do some other work - the work I should have been doing over the past 3 months instead of re-doing the population model.

So, perhaps I am jumping the gun a bit here as I have yet to fully analyse the output or run it past my field collaborators, but these are the current model parameter settings. First is the least-cost model, and food availability:



 Next comes the agent parameters, dictating the agent behaviours and life history traits:


Then comes the interaction parameters:


In short, parameters I've changed are:
  • the *number of time steps per day* - from 4 to 10; 
  • the *number of cells males can move when trying to find a female to mate with* - from a maximum of 10 to a max of 25;
  • the *pheromone degrade rate* -  to 0.08;
  • the *cost of male pheromone to a female* - from p to 0.3*p;
  • the *cost of a male pheromone to another male, if the males are on a trail* - from p to -0.001*p;
  • *food availability* outside of Cockscomb protected reserve - reduced to 85% of the availability inside CBWS;
  • addition of a *post-model setup* period - the initial 10 years (including the 3-year startup period) describe a reduced mortality for all adults, allowing greater time for the population to find suitable territories.
Initial analyses give male territories from around 40 to 70km2 and females around 10-20km2. These seem much closer to realistic sizes. Population sizes also seem to settle around 70-120 in number which also seem pretty good for the location.

These settings also seem to generate larger numbers of individuals being captured by the static model camera traps. Original stats gave an average of around 5 per sampling period versus the 191 collected in the field which is pretty bad. The new settings don't give anything near 191 either, but closer to an average of around 20ish.

Some other points:
  • Females undergo typical reproductive rates, being available to mate only for the central third of their oestrus cycle;
  • sub-adults leave the mother aged 2 and undergo a single year of 'dispersal' during which time they can move freely within their mothers range, are reproductively inactive and experience a higher mortality rate;
  • dispersal is therefore an emergent feature of the model and not hard-coded.
We'll see what the final analysis looks like soon and take it from there.

5.6.13

The first spatially-explicit agent based model of jaguar population dynamics

The chapter is done! After over a year working on building and developing an agent-based model of jaguar population dynamics I have finally finished tinkering, got my results, analysed and interpreted and written up the chapter for my thesis. I officially handed it in over southampton uni's electronic tracker system yesterday and my relief was palpable!

The unofficial deadline I proposed to my supervisors back in January was 15th March(!). But of course delays and problems along the way have caused this deadline to overrun by a mere 3 months. This of course puts the rest of my thesis in a bit more jeaopardy but I feel like the last few months have been a massive learning curve and have been well worth the endless hair-pulling and 'throwing my computer out of the window' in frustration events. Sort of...

I now feel like I have a decent-ish model of the large scale population dynamics of jaguars. Territories are strongly correlated with population size which is a nice validation that things are working as expected. Territories are also pretty much the size that would be expected, although a little on the small side if truth be told. I have found it incredibly difficult to obtain a true territory-resource relationship so that territory sizes are not imbedded in the code of the model, but rather an emergent feature. I still think more work could be done here and I will continue to develop this further as time goes on and I get some more feedback from a larger variety of people.

Some results from my model are shown below. Population size is reduced within the first 10 years and then stabilises. 

Territory sizes of males (blue) and females (red) increases with smaller population sizes.

Territory sizes increase over the first 10-20 years and then stabilise. The decreasing trend correlates with the increasing trend in population size.

A snapshot of the simulation after the first 3 years showing clear territories of individuals (coloured areas - different colours equate to different individuals) - all shown on a habitat map of the area.


A full map of the landscape used can be seen below showing a range of different habitats, the outline of the protected area and the camera traps.



Validation of the model with field data was a bit iffy at first glance. Field data captured 191 jaguar sightings, the model captured an average of 5 per data collected period. But discrepancies are mainly due to the models inability to capture the fine-scale social-oriented movement of individuals. In the real world males are known for making 'information-gathering' expeditions along trails to find out who has been around and when, with sub-adult males known to follow older male for short periods. This has not been included in the model, but its generally thought to be a non-fitness function and not thought to affect territory dynamics or the contact/interactions between individuals.

I can't of course display all the results and discussion from the model outputs just yet as I'm currently trying to update the chapter for submission to a publication. Once its been submitted and reviewed I will then make it openly available! 

Open-source information for all.

27.2.13

The frustrating attempt at obtaining a stable population size

Todays post is summed up nicely by its title. Two weeks after 'finishing' model construction, I am still finding bugs in the code and still tearing my hair out at trying to get my simulation to generate a stable population size.

With my 'glass is half full' head on, I say to myself that this time has been good and constructive and I'm glad I've found these niggly gremlins now rather than in a month when I've done my analysis and handed in my transfer report. And yes, this is true, but my 'life sucks' head says, why have I been so stupid as to code in these bugs in the first place? Have I not had years of coding experience by now? Am I not a reasonable programmer? Perhaps not? So yes, these bugs are MY fault. I am THAT stupid and model verification is the bane of my existence!



Coding has to be the worlds best way to make you doubt your own abilities and really look hard at yourself. Inevitably I find that a few hours of brilliant genius coding effort leads to some very strong hair-pulling out tactics a few weeks later when the 'simple' changes have done strange and unpredictable things to my agents and the way they behave. So strange in fact that it frazzles my tiny little brain trying to understand why my model is behaving in the way it is.

Agent-based models, it seems, really DO reflect complex systems. Or is it just my bad coding? Only time will tell.

Yesterdays coding blunder, after frustratingly trying to figure out why my population get exploding after about 12 years, was that not all my agents were NOT under-going their 'lose some energy every time step' step. And that only the ones who were actually eating food were doing this, meaning those agents that chose not to eat were perfectly happy and never suffered any negative consequences from choosing to perform actions that denied them the potential to consume resources. This at least explains nicely why my male agents were choosing to move incredibly long distances along trails (that provided no food).

So yes, in essence I am happy I found this bug. But todays problem is finding a set of parameters values that now fixes the opposite problem - generate a population that doesn't fall to zero within about 10 years.

Oh, the joys of programming.




20.1.13

Spatially-explicit agent-based model of jaguar movement - Overview of model

So my first week back at work after maternity leave has been spent getting re-acquainted with my model and preparing to write the next chapter for my thesis. I've found several articles which use ABMs to look at the response of populations to some form of landscape change, and whilst these are different to mine in key aspects, they nonetheless provide me a good overview of how others have gone about doing similar work.

Some papers to mention:

  • Parry et al (2007) Aphid population response to agricultural ;landscape change: A spatially-explicit, individual-based model. Ecological Modelling 199 451-463
  • Wang & Grimm (2007) Home range dynamics and population regulation: An individual-based model of the common shrew Sorex araneus. Ecological Modelling 205 397-409
  • Pitt et al (2003) An individual-based model of canid populations: modelling territoriality and social structure. Ecological Modelling 166 109-121
Grimm has been quite prolific in producing individual-based models in ecology and a great paper by Grimm (and plenty of others) outlines a standard procedure for describing these type of models, so I'll be sticking to this and following the seven elements they outline:
  1. Purpose,
  2. State Variables and Scales,
  3. Process and Overview and Scheduling,
  4. Design Concepts,
  5. Initialisation,
  6. Input, and
  7. Submodels.
Parry et al (2007) start from the protocol but provide information flow diagrams that make it easy to follow the model steps and simplify model procedures, so I've incorporated three flow diagrams into the Grimm protocol, as you can see below:

 Fig 1. Overview of model process

 Fig 2. Overview of agent process


Fig 3. Overview of movement process of agents


Fig 3 is undoubtedly quite complicated and probably subject to some change, but the current version of my model follows these steps quite literally. And I have to say it was quite difficult getting a fairly simplified(!) version of the movement process. A flow diagram is definitely the best way of presenting this information in my thesis chapter and hopefully will ease attempts at future publications as I'm following Ecological Modelling formats.

6.6.12

Conference Poster

I have just finished preparing a poster for the University of Southampton Biology Postgraduate conference due to take place on June 14th 2012.
I haven't quite got far enough with my current work to do a poster on validating my agent-based model so I've had to do a poster based on my ECAL poster... not too bad considering it only took me a few hours


22.5.12

Other people using ABMS for spatially explicit ecological modelling

I recently went to a talk by Lael Parrot who was visiting Southampton University for the week. Not a name I had heard of before but I'm quite surprised at this now considering she is involved in very similar work to me!

The most relevant paper I found from her was the one she talked about at the seminar she gave:

Anwar, S.M., Jeanneret, C.A., Parrot, L. & Marceau, D.J. (2007) Conceptualization and implementation of a multi agent model to simulate whale-wathcing tours in the St. Lawrence Estuary in Quebec, Canada. Environmental Modelling and Software 22 1775-1787.

Not only was this paper spatially explicit and employed agent-based modelling, but it was done using the Repast Simphony toolkit!! Finally, an ecological example of someone doing a peer-reviewed and published piece of research using Repast.

I've not yet management to dive into the paper in depth but some of the simulations Lael showed during her talk have me very impressed - linking the repast output with GIS data and creating some pretty good looking 3D models of the system. They also re-integrated the model output with the original GIS data to do some spatial analysis and statistics, so I wonder whether this might also be my best option... definitely something to look into in more detail.

Its a much bigger model than mine will be so I have no doubt that I'll be able to pick up some interesting tips from Lael and the paper. I'll report back with any useful bits of information.

17.1.12

Model Decision-Making

Ok, so following on from the 'model dilemma' post, I have decided, rather than choosing between the two separate movement decision options, to combine them together.

So, instead of having my agents move around looking for food and ignoring the type of habitat, they now make a decision based on 4 factors:

1. amount of food
2. habitat cost
3. pheromone cost
4. road cost

Decisions are now based on a weighting of these 4 parameters, with the need to move still driven by food requirements, but the decision inherently based on weighing up pros of food availability vs the cons of habitat/pheromone/road cost (which implicitly include a range of other factors that may be important to movement decision making by jaguars).

A total 'cell quality' value now exists for each neighbour and the jaguar choose the cell with the best cell quality, calculated as follows:

cellQuality = food - (0.1*habitatCost) - (0.1*pheromoneCost) - roadCost

Food is explicitly linked to habitat, with the food reaching a maximum of 10 in good quality preferential forest habitats.
habitatCost and pheromoneCost reach a maximum level of 100 and so are multiplied by 0.1 to standardise the relative values to that of food. 
roadCost is a tricky one, I haven't yet decided whether to go to a maximum cost of 10 here or to 100, to provide a better relative difference between road types..... The main tricky aspect here is that I want to encourage males to use the trails (classed as a type of road) but discourage the females from doing the same. In this way, males view the smaller forest trails as either of no cost or negative cost (TBC) whereas females see them of some low cost value.

This method of combining the food and cost decision making loops takes out around 3/4 of the steps required in the food loop, making only one additional step to the cost loop - a biased choice towards maximising food if the jaguar has low food reserves. 

Adding in different costs for the trails also provides some inherent decision making towards using trails or not, versus the hard-coding probabilistic choice to avoid or prefer trails that was used in both the food and cost loops.


11.1.12

Model Dilemma

So now my model is nearing completion (sort of), I have one main dilemma.

Following a presentation of my work to fellow biologist/ecologists, it seemed that what people wanted to see what a motivation for why individuals move around, rather than simply hard-coding them to move around x% of the time. This meant a shift away from the least-cost model to something more intuitive - food.

At first glance, this seems a relatively straight-forward shift to make. Now, instead of individuals choosing a location based on some negative cost value (and opting for the least-cost move) now they would move around according to their food requirements. In principle, the landscape-effect is similar. Forest is still good whilst agricultural areas/urban areas etc are not so good. However, the shift to 'emergent' behaviour, i.e. individuals moving around to meet some food requirement involves quite a lot of behind the scenes work within the code itself. To put in brief, the two flowcharts outline the decisions made by individuals within each given scenario, 1. moving to find enough food and 2. moving according to least-cost principles. Circles in red are decisions, those in thick red are standard default decisions.




Pros and Cons

1. Food
Now it becomes clear, whilst moving for food may be more intuitive and more 'realistic', it is a lot more complicated, involving a large number of additional parameters, is more likely to cause some un-conceieved bias in the results, and hence is far more sensitive to initial conditions and far less robust that a simpler movement process. There is also no explicit recognition of mortality risk, protection, mating opportunities, physiological costs etc that are encompassed with the single cost value used in the least-cost model. In short, the only factor affecting where a jaguar moves is the availability of food.

In addition, mortality risk is modelled separate to movement choice. Individuals are not 'aware' of the mortality risk of any particular location and are unable to choose a location according to this risk. Of course, this could be added as another factor, but this would increase the complexness of the model, add further parameters and make it even more sensitive.

Some additional parameters include:
1. Amount of food required by an individual (per step or as an average per day still to be decided)
2. Reduction in food per time step - should this be different depending on if the individual physically moves compared to staying in the same cell? should there be a reduction per time step and another depending on how the individual moves? Should this be a set amount or a proportion of the total food resource/food required per step/day?
3. Increase in food availability in cells that have been visited by a jaguar - should this be proportional, an incremental increase or a logistic increase? - i.e. type I, type II or type III response to predation?
4. Should the food be homogenous or clumped/variable in distribution?
5. Should the food change in spatial representation over time?




2. Cost
But then again, cost doesn't necessarily make the best option either. Here, whilst the model decision making is simple and straight-forward there is now no motivation for individuals to move. Movement is not 'emergent' but hard-coded. Males move x% of the time, females y% of the time. However, there is much less risk of the model being sensitive to initial conditions as there are only two parameters to be concerned about: relative differences in cost between habitats and movement rates for males and females.

In addition to this, movement decisions are now based on a range of factors, implicitly outlined by the cost of any particular habitat. Not only does food get represented in the cost, but also mortality risk, cover, mating opportunities, physiological costs of moving through the terrain.

1.11.11

Habitat preference and territory development

Getting jaguar agents to mimic real-life individuals and create territories is one of the most important features to try and get right in this type of agent-based model.

Exploring the way jaguars move through landscapes involves understanding how they interact with each other and with the environment. Territory formation and avoidance (or not) of conspecifics are integral to getting these interactions to be as close to mimicking those found in real populations as possible.

So.... some kind of pheromone seems that it should elicit an easy territory response in the agents. 'Marking' each cell with some level of pheromone as the agents move into and out of the cell and then allowing this 'pheromone' to degrade over time should allow territories to organically emerge during the simulation and with a bit of tweaking as to how quickly the pheromones should degrade and how 'strong' they should be to other individuals and to themselves.

The main idea is that individuals should not want to re-trace their steps too often, and that generally individuals should try and avoid each other - either due to possible conflicts between adults and due to resource depletion.

We know a bit about how large and flexible territories are in wild jaguar populations and so we settled on a maximum pheromone level of 100, with a degradation rate of 0.98, so that the pheromone reduced by 0.02% each timestep. This gave realistic territory sizes.

The strength of the pheromone is equal to its level, except for an agents own pheromone which is reduced to 0.15% of its current level; a deterrent to re-entering the cell but not enough to mean that the individual would not want to re-treat to its territory if meeting another individual or unsuitable habitat was the only other option.

The addition of pheromones, and the basic least-cost model idea of the simulation led to the following output, where clearly define territories (individuals in different colours) and habitat preferences can be seen:



Some individuals have been 'pushed' out of the forest area due to the population size. This is intentional. A higher number of individuals creates stress for any single individual. Without some level of stress, individuals will be content on remaining in fairly isolated areas. Some stress is needed in order for individuals to move and seek new areas in which lead a lifestyle of least-cost, so prompting the migration of individuals from one side of the landscape to the other.

20.9.11

1st jaguar simulation model

With my case study involving complicated landscape structures and real-world scenarios, I needed to first get to grips with getting an agent-based model of jaguar movements up and running in a simplified landscape that would enable some interpretation of population dynamics without the need for an in-depth and overly detailed landscape.
After discussing several options, a very abstract and basic landscape structure was decided on to try to answer some fundamental questions important to my on-going research:

1) Can we get a model of jaguar movements working, with plausible daily movements and territory sizes close to those found in real world populations?

2) Does landscape structure seem to influence the way jaguars move around the landscape and does it affect the fitness of individuals and the population as a whole?

3) Can habitat corridors help to increase the connectivity of the landscape and facilitate movement of individuals between habitat patches?

9 landscape structures were defined to help explore these questions, including 3 control landscapes (top row), 3 connected corridors (middle row) and three non-connected corridors (bottom row) :




'Good' habitat here is forest (in green). This is where jaguars will prefer to be. Intermediate habitat is marked in blue, and represented edge habitat; the edge of the forest that meets the non-forest habitat. This is deemed to be less desirable than the forest itself but more desirable than the non-forest habitat. Non-forest (matrix) is coloured beige, and represents any habitat where the jaguar do not want to be; urban areas, agricultural land, roads etc etc.

Landscapes were designed to cover a range of potential options. All designs (bar the first two; the all forest and the 2 distinct habitat patches) have exactly the same amount of forest and matrix habitat. Edge habitat changes depending on the design of the landscape.

The model is designed as a grid, with individual jaguars occupying any one grid cell at a time. Least-cost methodologies are employed so that each habitat has a cost; forest of 1, edge of 5 and non-forest (or matrix) as 25. This represents the strength of the preference of jaguars to any habitat (lower cost equals a much stronger preference).


Next post - more information about jaguar movements and getting territories to develop.













7.6.11

Summer Project 2010

My first exploration with agent-based simulations was, thanks to my good friend Melissa, via Netlogo: a free and really nice piece of software with a whole host of built in models for you to explore.

I had limited programming ability before starting at Southampton and some introductory model building during my first year with Matlab, Python and C# told me that I did not have enough experience as of yet to attempt to create my first ABM (agent-based model) in one of these languages. Netlogo is a lot easier to use than stand-alone languages, coming in a friendly GUI environment and lacking the usual 'grammer' and 'punctuation' of other more advanced languages. The package is built specifically for agent-based modelling and I found it quick, easy and straightforward to pick up.

If programming is not your strong point then I definitely recommend using Netlogo, at least to start with.

It was easy to get agents moving around the screen: Netlogo has a built in feature that automatically displays agents on the screen. You can add buttons, graphs and sliding scales to change model parameters, display certain bits of information and keep a running output of any agent characteristics that you may be interested in.

Check out the simple sheep-wolf model that comes as part of the Netlogo package. The link will open up the model in a standard web browser from where you can manipulate and run the model:



1. Click on setup. This will display the landscape, sheep and wolves in the display box on the right.
2. Clock on go. This will run the model and you should see the sheep and wolves moving around the display box.
3. Clock on go again to stop the simulation.
4. Note the graph of population size of both sheep and wolves at the bottom, changing over time as the simulation runs.
5. Change any of the settings and run the simulation again (remembering to click setup before you click on go).
6. You should not see a change in the graph at the bottom of the page that reflects the changes you made in the parameters.
7. Change the show-energy? and grass? buttons to on instead of off and these will now be displayed in the display box.
8. Its as easy as that!

Example:



When you download netlogo you are presented with a blank display console incorporating a blank display box on the right hand side. Clicking on file - models library, will open the list of available models that you can choose to run.

A really great feature, from having so many models included in the package, is that it is really easy to figure  out how to create models: simply look at the code that comes with any of the included models to figure things out. This is done via the procedures tab at the top of the netlogo console.

The Netlogo website has a lot of information on how to get started, notably via the User manual and the tutorials, which can be found here.



next post: my first netlogo model