JohnsonPaper06 (1)

Embed Size (px)

DESCRIPTION

,mn,m

Citation preview

Ant Optimization in NetLogo

Stephen JohnsonComputer Systems Lab 05-06TJHSST, 6560 Braddock Rd.Alexandria, Virginia 22312

AbstractThe purpose of this project is to investigate the applications of swarm intelligence to finding optimal solutions. I'm using an agent based environment of artificial ants to find the optimal path between a food source and the anthill. I hope to test the effects of varying chemical evaporation rates on the effectiveness of the ants. This project was created with and runs in NetLogo.

1. IntroductionMy project addresses the subject of optimization and finding optimal solutions. More specifically, I am using ant optimization, which is a method for optimization that involves a large number of simple agents who, when working together, can perform a complex task. The advantage to using ant optimization is that the amount of coding that is necessary is relatively small because the agents are simplistic. The difficult part is anticipating how the agents will behave as a large group. A small modification in a single variable or method can have a profound effect on the behavior of the group as a whole. My project investigates the effects of chemical evaporation on the effectiveness of the ants.In this document I hope to clearly show my approach to this problem, outline the various stages of production as well as the problems I encountered along the way, describe my research and discuss my results.

2. BackgroundOptimization has been an interest of computer scientists since computers were developed. Certainly humans can look at certain situations and, by simple visual inspection or with the aid of mathematical techniques, find the optimal solution. A paper boy, for example, can decide how best to deliver to all the houses in his neighborhood and make it home before dinner. As these situations grow in complexity, however, it becomes more and more difficult for a human to solve them. Imagine that same paper boy trying to find the shortest path connecting every house in the United States. A computer, on the other hand, cam compute far faster than any human, allowing us to find the solutions to these problems.As computers and computer science evolved, several optimization techniques developed. Simulated annealing is a technique in which, a "neighbor solution" to the current solution is examined and probabilistically chosen to replace the current solution. This process continues until the solution is refined to the desired degree. Genetic algorithms keep track of a number of solutions and then tries to "evolve" the optimal solution by mixing and matching pieces of the various solutions and keeping more optimal ones. Taboo searches attempt to catalog suboptimal elements and then compiles solutions that do not contain these elements.The algorithm for Ant Colony Optimization (ACO) was developed by Marco Dorigo in 1992. This was an optimization method that simulated real ants by having agents traverse the data set and lay "pheromones" on optimal paths. Other agents would pick up on these paths and further optimize them, producing stronger paths. The largest advantage of ACO over the other methods described above is that ACO can be used in a dynamic environment. The methods listed above look at the entire data set and attempt to formulate the optimal solution. If you change the data set you have to start over from the beginning. ACO creates agents that are never looking at more than a small piece of the data set anyway, so dynamic changes can be made without having to start over.Ant Colony Optimization has been a subject of research since its development. My initial interest in this subject came from a NetLogo ant foraging model by Uri Wilensky. In my research I came across experiments Deneubourg and Co. performed on a number of real ants (Dorigo 2). I compared their data from real ants to data I collected from the same experiment on my artificial ants. Liviu Panait and Sean Luke performed an ant foraging and optimization project documented in "Ant Foraging Revisited." Their project is largely the basis for mine. I extended their model by investigating depleting and multiple food sources.

3. Development

3.1 TheoryThe kind of optimization that I am trying to produce, that is, optimization using swarm intelligence, is known as ant colony optimization. This type of optimization implements large numbers of agents. Each agent may perform only relatively simple tasks and may only gather information from its immediate surroundings, but in large numbers they can accomplish more complicated things, like finding optimal solutions. The more agents you have running a simulation the faster you get results.In my simulation, the ants are only aware of whether or not they are carrying food and only gather information from adjacent spaces. The information they are gathering is a "pheromone" that is left behind by other ants. As the ants walk away from either the anthill or a food source they leave a pheromone of diminishing strength. This behavior establishes a gradient of pheromone that is strongest at the food source and gets weaker the farther you are from the food source. One ant can not cover very much area, but many ants can quickly cover the entire environment. Ants then follow this pheromone to find their destination.

3.2 Design CriteriaWhile designing my project I had to keep it within the parameters of ant optimization. I had to figure what the ants would need to know, what information they would need to get from the environment, and how they should imprint the environment so as to communicate with the other ants. I also aimed to present my project in an organized and accessible way. The interface of my program is intuitive, information is provided, and my code is annotated.

3.3 MaterialsI programmed my project using NetLogo. NetLogo is a very useful programming language for modeling. This language allows programmers to easily and quickly create agent-based environments with graphical interfaces. The program includes an interface with which to write the actual code, as well as to layout the buttons, sliders, switches, displays, etc. that the user will use to interact with the program. All the components of the interface are automatically integrated with the actual code, removing that hurdle for the programmer. More information on NetLogo can be found at (http://ccl.northwestern.edu/netlogo/). Additional materials include all the written and electronic sources I used.

3.4 Procedures

3.4.1 Initial Code DevelopmentMy initial goal was to create an environment of ants, food, and predators that modeled life. I began by investigating the behavior of ants. When an ant has food it lays a chemical pheromone that other ants can sense. This pheromone forms a trail that attracts ants and leads them to the food source. As for returning to the hill, many ants have an innate ability to find the hill based on landmarks and the position of the sun.In my simulation, the environment is composed of little squares called patches. Each patch maintains its own variable for the amount of ant pheromone on that space. The environment is traversed by agents known as turtles. These agents are representative of the ants. The ants have their own variable for whether or not they are carrying food and are able to read the levels of pheromone on the patches around them. The food sources and ant hill in my simulation were also represented by turtles and had their own variables for the amount of food at those locations.My first generation of ants was rather ineffective. When carrying food the ant would lay a pheromone of strength x, provided that the current pheromone on that patch was less than x. Each time step the pheromone would evaporate and spread to nearby patches by a percent k. The result of this was that the pheromone directly under the ant had the value x and a pheromone nearer the food source had the value x*(k)^t where t is the number of time steps since the chemical was laid. Since x*(k)^t was always less than x and ants seek out the strongest nearby pheromone, ants ended up following each other back the hill with no food rather than going the other way and finding the food source.

3.4.2 Pheromone RevisionsI remedied this problem in my second generation of ants. When these ants found a food source they laid a pheromone of strength x. They stored this value x in an internal variable c and then, as they walked away from the food source, they diminished this internal variable c by a percent l each time step. Now, a newly laid pheromone had the value x*(l)^s, where s was the number of steps since finding a food source, and an old pheromone had the value x*(l)^s*(k)^t. This model worked well, for the most part. There were some complications when the evaporation rate (k) was too great compared to the diminishing rate (l) of the ants. I didn't recognize the cause of this problem until much later.

3.4.3 Walls and ObstaclesAround this time I decided that trying to model the entire life cycle of an ant was too broad a scope. Instead I was going to focus exclusively on ant foraging behavior. I wanted to introduce walls into my environment and then design my ants to always find the shortest path connecting the ant hill and the food source.After implementing walls I immediately ran into obstacles (pun not intended). Ant's used their innate ant hill finding ability to always be walking towards the ant hill when carrying food. When they encountered a wall they would chose a direction to go (left or right, for example) that involved the least amount of turning. After turning and taking a step they would face back towards the ant hill and repeat. This caused ants to become stuck, banging their heads into the wall just opposite the ant hill (see below).

I tried numerous fixes, including have ants remember whether they were previously following a wall and to not turn back towards the ant hill until they had cleared the wall. The code was complicated and convoluted. I almost got it working for walls that were at 90 degree angles, but I had no idea how I could implement it for walls at oblique angles.I found my fix reading an article called "Ant Foraging Revisited" by Liviu Panait and Sean Luke. The source of my problem was the ant's innate ability to point towards the nest. Panait and Luke designed a model similar to mine but instead of utilizing this innate ability, their ants used a second chemical to find their way back to the nest. When ants left from the nest they would leave a diminishing chemical exactly as they did when they left a food source, creating a totally symmetrical process.I implemented Panait and Luke's solution and it worked beautifully. I am able to insert walls at whatever angle I want. The walls are stored in a boolean variable of the patches that signals to approaching ant's whether or not the ground is passable. In addition, the pheromone values of wall patches are automatically set to zero.

3.4.4 Putting my Ants to the TestIn my reading a came across a series of experiments performed by Deneubourg and colleagues (Deneubourg, Aron, Gross, and Pasteel) called the double bridge experiments. These were a series of experiments performed on live ants by connecting their nest to a food source via bridges of varying length and recording the tendencies of the ants to form a pheromone trail across one bridge versus the other. I performed the same experiments on my virtual ants to see how they compared to Deneubourg and Co.'s real ants.In the first experiment, both bridges are the same length. The ants of the colony are initially spread evenly across both bridges. After finding the food, more ants will return via a particular bridge, or return faster, just as a consequence of random chance. This begins a positive feedback loop where more ants are choosing a particular bridge and are laying more chemicals which causes more ants to chose that bridge and so on. After 500 time steps I looked to see if more ants were on either bridge of if the distribution was relatively even (25 trials). My results from this experiment matched those of Deneubourg and Co. quite nicely. In most cases a majority of the ants wound up on one bridge and the chance of them ending up on either bridge was essentially equal. Very occasionally they were distributed evenly across both bridges.

BridgeLeft BridgeRight BridgeEqual DistributionIncidence11122

In the second experiment one bridge is longer than the other. The ants should disperse rather evenly across both bridges, just as before, but instead of settling on one bridge or the other the ants should have a tendency to establish a trail on the shorter of the two bridges. My ants produced these results very clearly, which matched the results from Deneubourg and Co.

BridgeShort BridgeLong BridgeEqual DistributionIncidence2221

The third test is very similar to the second test except that when the test begins the ants have only the long bridge via which to reach the food source. Then, after a time, the second, shorter bridge is introduced. The purpose of this test is to see if the ants change their path after it had been established on the long bridge. Deneubourg and Co. found that the real ants only occasionally abandoned the longer trail in favor of the shorter one because the pheromone on that trail was that much stronger. On this test my ants diverged from the real ants of Deneubourg and Co.'s experiments. The real ants seemed to exhibit an accumulation of chemical that prevented the new shorter path from gaining momentum. In my model, all trails begin with the same strength and then diminish with length. After the second, shorter path was introduced to my ants, once one ant found it and returned to the nest it immediately began diverting the rest of the colony and within a few seconds the long path was forgotten.It was not possible for me to collect meaningful data for this test because I could not find how long Deneubourg and Co. waited before deciding which branch had been settled upon. They added the second bridge after 30 minutes, but without knowing when the end time was it was very difficult for me to try to frame this test correctly. I believe the most important conclusion to be drawn from the experiment is the one described in the previous paragraph.

3.4.5 The Evaporation Dilemma

It was at this time that I decided to start working with multiple food sources. The only problem was that in order for multiple food sources to be implemented I had to fix the evaporation mechanism in my model. After some careful examination I found the problem within my code.As you will recall, the strength of newly laid and previously laid chemicals in my model were x*(l)^s and x*(l)^s*(k)^t respectively. If the decrease in strength over time (evaporation) (k)^t was greater than the decrease in strength due to distance from the food source (l)^s, the pheromone closer to the food source was actually weaker than the pheromone being laid closer to the hill. This was creating highs in the pheromone gradient where there was no food.The logic behind this system was that the pheromone high would be placed on the food source and at the same time the value of that high was stored inside the ant. As the ant walked away he diminished that internal variable and then laid pheromones as strong as the diminished internal variable and in this way created a gradient.When evaporation was introduced to the model, it decreased the value of the chemical high laid at the food source, but it did not decrease the value of the internal variable stored within the ant. This was the source of the problem. Instead of reading the chemical value once and then diminishing it over time, I solved the problem by reading the chemical every time step after it had been evaporated and then diminishing it by the distance from the food source.

3.4.6 The Final Generation

The final generation of my ants works as follows: Whenever an ant steps on a food source or hive it lays the corresponding chemical of the maximum strength. When on any other patch, an ant will get the highest pheromone value from the surrounding patches, diminish it by the percent l and then apply a pheromone of that strength on the current patch if the current patch is not already stronger. This way it is impossible for an ant to create a high in the pheromone gradient unless they are standing on a food source of ant hill. With the implementation of this final generation of ants I was ready to begin experimenting with multiple food sources. See the results section for more information.

3.5 Graphs and Figures

Functionality - The figures above display the general functionality of the program. The ants leave from the ant hill, find the food source, return to the ant hill, and optimize their path.

Laying the Food Chemical - In these figures the ants have found a food source and are laying a chemical of diminishing strength as they return to the ant hill. A shorter path will correspond with a stronger chemical because it has been diminished over fewer steps.

Laying the Home Chemical - These figures show the ants laying the home chemical as they spread out from the ant hill. As time goes by the chemical diffuses and fades if it is not reapplied. Notice how the home chemical extends all the way to the food source in the right figure.

The Result - This figure shows the results of the food and home chemicals. Ants that are carrying food will follow the home chemical to get to the hill. Ants that do not have food will follow the food chemical to find the food source. The result is a neat line of ants that are using the optimal path between the two end points.

4. Results

Once I had all the various aspects of my program fully functional I had to find something to test. I had just finished fixing the pheromone evaporation problem, so I thought that would be a good place to start.I figured that if an ant pheromone evaporated too quickly, it would be very difficult for the colony to establish a trail. On the other hand, if the pheromone did not evaporate at all then when a food source was depleted the ants would be following a trail to nowhere.I then created a static test environment with 5 food sources (see graphs and figures) and proposed to test various evaporation rates. For each evaporation rate I performed multiple trials to see how many time steps, on average, it took for the ants to collect all of the food in the environment.

My first battery of tests used the following conditions:# of Food Sources: 5Food Value: 400Number of Ants: 150Diffusion rate: 0%Gradient Degree: 1Infinite Food: off

Trial #0%.1%.2%.3%.4%.5%.6%.7%.8%.9%1%1318214681587XXXXXXXX2302114071680XXXXXXXX331631890XXXXXXXXXAvg:31221588XXXXXXXXX

After gathering the data seen above, I decided to abandon this test. The evaporation rate was causing the ants to gather food faster, as seen in the two averages, but it was also causing the home chemical to evaporate. The condition of the test was that all of the food was gathered and, on occasion, the last few ants could not return the last of the food because the home chemical had evaporated. This created artificially high values that were not representative of the results.In my model it is necessary for pheromones to evaporate if the ants are to gather from multiple food sources, but there is only one nest. Therefor, there is no reason that the home chemical in my model should evaporate at all. In real life this could cause confusion with the pheromone of other hives, or perhaps attract predators. As a compromise, I rewrote my program so that the home chemical evaporated at half the rate of the food chemical and repeated my tests.

Trial #0%.1%.2%.3%.4%.5%.6%.7%.8%.9%1%131821093120812441420121812521491123612201143230211079109911211296113111891179128210851154331631320131912121103129811981313131510711165Avg:31221164120911921273121612131328127711251154

The change to the evaporation code produced much more consistent values than the previous tests. Unfortunately it did not reveal any kind of pattern or obvious optimal evaporation rate.I thought perhaps what I was seeing was only a small piece of a larger picture. To test this theory I conducted a second battery of tests. In this test the range of evaporation values I tested were 0%-5%. I also increased the number of trials for each value from 3 to 5 for more accurate results and increased the number of ants from 150 to 300 in order to reduce the impact of the random movement of the ants.

# of Food Sources: 5Food Value: 400Number of Ants: 300Diffusion rate: 0%Gradient Degree: 1Infinite Food: off

Trial #0%.5%1%1.5%2%2.5%3%3.5%4%4.5%5%1111764069877775280875078278377186221151636772670800810796861821767879312036967737676877178177336898857964145769582788270084570679777581281251390770710736671812769711789795893Avg:1264687756766722798768777771806848

Even after the modifications I made to my test I still did not discover an obvious trend or optimal evaporation rate. This might still be attainable if I rework my model, if perhaps I am not testing the evaporation rate the right way. However, I am out of time. I was unable to find the optimal evaporation rate but in my testing I was able to make a number of important conclusions about evaporation rates.

(see next page for Conclusions)

5. ConclusionWhile I was unable to find the optimal evaporation rate for ants, I did make a number of other important observations regarding evaporation rate of the ant pheromones.When the pheromone laid by an ant evaporates slowly, it will allow that ant to form a trail faster and farther from the hive. Since each trail will be around for longer the chances of another ant discovering and reinforcing it are greater.A slow evaporation rate leads to an effect called "pocketing". This is when the ants are swarming over a depleted food source. The pheromone is evaporating slowly, so the pheromone high will remain even after the food source is depleted. If there are other food sources near by this is immensely beneficial to the ants. Since they are already gathered in one spot, a single ant can lay a trail to a new food source and if he hits that pocket all the ants in the pocket will begin following his trail. On the other hand, if there are no food sources nearby and especially if the remaining food sources are on the opposite side of the hive, the ants will be stuck in the pocket for a long while until the pheromone evaporates.A pheromone that evaporates quickly will eliminate pocketing. The faster the pheromone evaporates the sooner the ants will redistribute themselves over the rest of the environment. A slow evaporation rate also makes it harder to form a trail. Since the trail is not there for very long there must be ants nearby ready to pick it up and reinforce it. Slow evaporation rates, therefor, depend on higher ant populations and ant density in order to function well. If the pheromone evaporates before any ants can find it then the ants may not as well lay any pheromones at all.If any conclusion can be drawn from all this, its that the optimal evaporation rate of ant pheromones is highly situational. When an ant hill is young and its population is still relatively small a slowly evaporating chemical would be more beneficial. If food sources are often isolated and not near other food sources than a slowly evaporation chemical would be more beneficial in order to eliminate pocketing. Perhaps if ants received some kind of signal from the hive regarding the current size of the population, or associates certain food types with certain pheromones it could lay a dynamic pheromone with an evaporation rate optimal to the ants specific situation.

6. References

NetLogo user manual, Uri Wilensky

"Growing Artificial Societies," Joshua Epstein & Robert Axtell

"Turtles, Termites, and Traffic Jams," Mitchel Resnick

7. Appendices

7.1 Code

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Ant Optimization;;;;;;;;;;;;;;By Steve Johnson;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;Varables;;;;;;;;;;;;

globals [clock de-wall evap? Gheading Increment]

breeds [hive worker builder food] ;;These are the diferent turtle "breeds" used in this modelhive-own [hfood] ;;This variable keeps track of a hive's food levelworker-own [wfood whome havefood?] ;;The wfood and whome (workerfood and workerhome) variables keep track of the ant's chemical strength level. These diminish with each step. The last variable tells the ant if it is carrying foodbuilder-own [btime] ;;This is used by the model to create the wallsfood-own [ffood] ;;This variable keep track of the size of a food source

patches-own [foodc homec wall?] ;;These variables store the food and home chemicals on that patch and tell the patch if it is a wall

;;;;;;;;Setup Methods;;;;;;;;;

to setup ;;Orchestrates the program setupca ;;Clear all: resets the worldset clock 0 ;;A clock variable used time thingsset de-wall false ;;A variable used in the third custom setupset evap? falsesetup-shapes ;;Assigns shapes for the various turtle breedssetup-patches ;;Sets up the patchessetup-hive ;;Sets up the hive and ant turtle breedssetup-food ;;Sets up the food turtle breedend

to setup-shapes ;;Assigns shapes for the various turtle breedsset-default-shape hive "hive"set-default-shape worker "ant"set-default-shape food "food"if(fun-shapes) ;;A joke option that will assign random shapes to each breed[ask turtles[set size 1]set-default-shape hive random-one-of shapesset-default-shape worker random-one-of shapesset-default-shape food random-one-of shapes]end

to setup-patches ;;Sets up the patchesask patches[set wall? false] ;;All patches start out as non-wallslet temp 0while [temp < num-walls] [ create-custom-builder 1 [ setxy random-x random-y ;;placement of walls is random set heading random 360 ;;direction of walls is random set btime random 15 + 20 ;;length of walls is random let temp2 0 while [temp2 < btime] [ ask patch-here [ set wall? true ;;Changes this patch ask neighbors [set wall? true] ;;and nearby patches into walls ] if(abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1) ;;Ensures that the turtle does not walk off the screen edge [rt 180] fd 1 set temp2 temp2 + 1 ] die ;;Disposes of the builder afterwards ] set temp temp + 1 ]ask patches[if (wall?) [set pcolor 9]] ;;Walls are displayed as white. Non-walls remain blackend

to setup-hive ;;Sets up the hive and ant turtle breedscreate-custom-hive 1 [ set color new-random-color ;;hives assume a random color setxy 0 -30 ;;hives have a set position while[wall?-of patch-here] ;;choses a new position if the set position was walled [setxy random-x random-y ] set hfood 1 ;;hives start with no food set size 0 ;;the hive's size is proportional to its food hatch-worker num-ants ;;the hive spawns some ants [ set size 1 set heading random 360 ;;ants begin facing a random direction set havefood? false ;;ants are not carrying food when created set whome 100 ;;ants begin on the same spot as the hive and must have this variable adjusted accordingly ] ]end

to setup-food ;;Sets up the food turtle breedset Gheading 0set increment 160 / (food-start + 1)let temp 1while [temp = 2001)[set clock clock + 1] ;;increments the clock

if(de-wall and clock = de-wall-val) ;;Used to remove the walls in the third custom setup[create-custom-builder 1 [ setxy -4 -12 set heading 0 set btime 5 ]create-custom-builder 1 [ setxy -4 16 set heading 0 set btime 5 ]ask builder [ while [btime > 0] [ ask patch-here [ set wall? false ask neighbors [set wall? false] ] if(abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1) [rt 180] fd 1 set btime btime - 1 ] die ]]end

to go-food ;;Tells the food what to do on each time stepask food[if(ffood < 5) ;;If the food source drops below a certain level it gets removed [die]set size sqrt (ffood / 25) ;;The food source gets smaller as it is depleted]end

to go-hive ;;Tells the hive what to do on each time stepask hive[set size 1 + sqrt (hfood / 100) ;;The hive gets bigger as ants bring in more food]end

to go-workers ;;Tells the ants what to do on each time stepask worker[if any? hive-on patch-here ;;adjust your pheromone value if there is a hive here [set whome 100 set homec-of patch-here whome]if any? food-on patch-here ;;adjust your pheromone value if there is food here [set wfood 100 set foodc-of patch-here wfood]ifelse(havefood?) ;;does this ant have food? [return-to-nest] ;;if so, return to the nest [search-for-food] ;;if not, go look for food]end

to return-to-nest ;;ants will return to the hive if they are carrying foodifelse any? hive-on patch-here ;;is there a hive here [ ask one-of hive-on patch-here ;;add food to the hive [set hfood hfood + 5] set havefood? false ;;the ant is no longer carrying food set wfood 0 ;;the ant has just left the hive, so its food pheromone value is set to 0 rt 180 ;;turn around and go look for more food ] [ ifelse any? hive-on neighbors ;;if there is no hive here, is there one next door? [ set heading towards one-of hive-on neighbors ;;if so, go there ] [ let pos1 patch-left-and-ahead 45 1 ;;if not, check the three patches in front of you let pos2 patch-right-and-ahead 45 1 if homec-of pos2 > homec-of pos1 [set pos1 pos2] ;;find which has the strongest pheromone value ifelse homec-of pos1 > homec-of patch-ahead 1 ;;check if the values in front of you arent all the same [ ;;if not, go towarsd the strongest value set heading towards pos1 wiggle ] [ ;;if so, go in a random direction rt random 45 lt random 45 ] check-wrapping ;;Ensures that ants do not walk off the screen edge ] ]fd .75 ;;The ant ends by moving forward one stepset wfood foodc-of max-one-of neighbors [foodc] * (.05 * gradiant-degree + .94);;Reduces the ant's chemical strengthif foodc-of patch-here < wfood ;;Updates the chemical on the ant's patch [set foodc-of patch-here wfood]end

to search-for-food ;;Ants will go and search for food if they have noneifelse any? food-on patch-here ;;Is there food on this patch [ if not (infinite-food) ;;As long as infinite-food is not on the food source will get smaller [ ask one-of food-on patch-here [set ffood ffood - 5] ] set havefood? true ;;The ant has food, resets its chemical and turns for home set whome 0 rt 180 ] [ ifelse any? food-on neighbors ;;Is there food on a neighboring patch? [ set heading towards one-of food-on neighbors ;;If so move towards it wiggle ;;Keeps the ants from moving strictly at 45 degree angles ] [ let pos1 patch-left-and-ahead 45 1 ;;Examines the three front patches and finds the strongest chemical let pos2 patch-right-and-ahead 45 1 if foodc-of pos2 > foodc-of pos1 [set pos1 pos2] if foodc-of pos1 > foodc-of patch-ahead 1 [set heading towards pos1] ifelse foodc-of pos1 = foodc-of patch-ahead 1 ;;If all the patches are the same it will not turn [ rt random 45 lt random 45 ] [ wiggle ] check-wrapping ;;Makes sure the ant does not wrap around the screen ] ]fd .75 ;;The ant ends by moving forward one stepset whome homec-of max-one-of neighbors [homec] * (.05 * gradiant-degree + .94);;Reduces the ant's chemical strengthif homec-of patch-here < whome ;;Updates the chemical on the ant's patch [set homec-of patch-here whome]end

to go-patches ;;Tells the patches what to do on each time stepdiffuse foodc diffusion * .1 ;;Causes the chemicals to difuse and bleed to adjacent patchesdiffuse homec diffusion * .1ask patches[set foodc foodc * (1 - evaporation * .01) ;;Accounts for any evaporation in the modelset homec homec * (1 - evaporation * .001)if foodc > 100 ;;sets upper and lower limits on the value of the chemical strength [set foodc 100]if homec > 100 [set homec 100]if foodc < .5 [set foodc 0]if homec < .5 [set homec 0]if view = "Food-chem" or (view = "Both" and foodc > homec) ;;Colors the patches acording to the current view setting [if not wall? [set pcolor 60 + ((foodc / 100) * 9)]]if view = "Home-chem" or (view = "Both" and foodc < homec) [if not wall? [set pcolor 90 + ((homec / 100) * 9)]]if view = "None" [if not wall? [set pcolor 0]]if (wall? or abs pxcor > screen-edge-x - 1 or abs pycor > screen-edge-y - 1) [ set foodc 0 set homec 0 ]]end

to-report new-random-color ;;Used to generate new colors for the hives and ants in each modellet temp (((1 + random 13) * 10) + (3 + random 6))while [any? hive with [color = temp]][set temp (((1 + random 13) * 10) + (3 + random 6))]report tempend

to-report random-x ;;Generates a random x value acording to the screen sizereport random (screen-size-x - 2) - (screen-edge-x - 1)end

to-report random-y ;;Generates a random y value acording to the screen sizereport random (screen-size-y - 2) - (screen-edge-y - 1)end

to-report now-food ;;Used to report the food level of the hive to the graph in the interfacereport (hfood-of one-of hive)end

to wiggle ;;Keeps ants from moving strictly at 45 degree angleslt random 15rt random 15end

to check-wrapping ;;Keeps ants from wrapping around the screenwhile[wall?-of patch-ahead 1 or abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1][ifelse (random 2 < 1)[while [wall?-of patch-ahead 1 or abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1][rt 30]][while [wall?-of patch-ahead 1 or abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1][lt 30]]]end

to-report report-clock ;;Reports the clock variable to the display on the interfacereport clockend

to test-1ca ;;Clear all: resets the worldset clock 0 ;;A clock variable used time thingsset de-wall false ;;A variable used in the third custom setupset evap? truesetup-shapes ;;Assigns shapes for the various turtle breedssetup-patches ;;Sets up the patchessetup-hive ;;Sets up the hive and ant turtle breedscreate-custom-food 1[setxy 15 -25]create-custom-food 1[setxy -15 -5]create-custom-food 1[setxy -30 25]create-custom-food 1[setxy 35 10]create-custom-food 1[setxy 0 35]ask food[set color green ;;Leaves are green...duhset ffood food-val ;;each piece of food has an amount of food set by food-valset size sqrt (ffood / 25) ;;food sources get smaller as they are depleted]end

to setup-1 ;;A custom setup for the first Deneuborg and Co. Double-Bridge Experimentcaset clock 0set de-wall falseset fixed-position? trueset evap? falseset food-start 1setup-shapes

ask patches[set wall? false]let temp 0create-custom-builder 1 [ setxy -5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy 5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy -5 -35 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy -5 -15 set heading 300 set btime 30 ]create-custom-builder 1 [ setxy 5 -15 set heading 60 set btime 30 ]create-custom-builder 1 [ setxy -30 0 set heading 45 set btime 33 ]create-custom-builder 1 [ setxy 30 0 set heading 315 set btime 33 ]create-custom-builder 1 [ setxy -5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy 5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy -5 37 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy 0 -7 set heading 60 set btime 18 ]create-custom-builder 1 [ setxy 0 -7 set heading 300 set btime 18 ]create-custom-builder 1 [ setxy -15 2 set heading 45 set btime 20 ]create-custom-builder 1 [ setxy 15 2 set heading 315 set btime 20 ]create-custom-builder 1 [ setxy 0 15 set heading 0 set btime 1 ]ask builder [ while [btime > 0] [ ask patch-here [ set wall? true ask neighbors [set wall? true] ] if(abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1) [rt 180] fd 1 set btime btime - 1 ] die ]set temp temp + 1ask patches[if (wall?) [set pcolor 9]]

setup-hivesetup-foodend

to setup-2 ;;A custom setup for the second Deneuborg and Co. Double-Bridge Experimentcaset clock 0set de-wall falseset fixed-position? trueset evap? falseset food-start 1setup-shapes

ask patches[set wall? false]let temp 0create-custom-builder 1 [ setxy -5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy 5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy -5 -35 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy -5 -15 set heading 315 set btime 22 ]create-custom-builder 1 [ setxy 5 -15 set heading 75 set btime 35 ]create-custom-builder 1 [ setxy 38 -5 set heading 0 set btime 10 ]create-custom-builder 1 [ setxy -20 2 set heading 30 set btime 30 ]create-custom-builder 1 [ setxy 38 4 set heading 300 set btime 37 ]create-custom-builder 1 [ setxy -5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy 5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy -5 37 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy 0 -7 set heading 75 set btime 28 ]create-custom-builder 1 [ setxy 0 -7 set heading 315 set btime 13 ]create-custom-builder 1 [ setxy -10 2 set heading 30 set btime 16 ]create-custom-builder 1 [ setxy 26 0 set heading 300 set btime 30 ]create-custom-builder 1 [ setxy 0 15 set heading 0 set btime 1 ]ask builder [ while [btime > 0] [ ask patch-here [ set wall? true ask neighbors [set wall? true] ] if(abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1) [rt 180] fd 1 set btime btime - 1 ] die ]set temp temp + 1ask patches[if (wall?) [set pcolor 9]]

setup-hivesetup-foodend

to setup-3 ;;A custom setup for the third Deneuborg and Co. Double-Bridge Experimentcaset clock 0set de-wall trueset fixed-position? trueset evap? falseset food-start 1setup-shapes

ask patches[set wall? false]let temp 0create-custom-builder 1 [ setxy -5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy 5 -35 set heading 0 set btime 20 ]create-custom-builder 1 [ setxy -5 -35 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy -5 -15 set heading 315 set btime 22 ]create-custom-builder 1 [ setxy 5 -15 set heading 75 set btime 35 ]create-custom-builder 1 [ setxy 38 -5 set heading 0 set btime 10 ]create-custom-builder 1 [ setxy -20 2 set heading 30 set btime 30 ]create-custom-builder 1 [ setxy 38 4 set heading 300 set btime 37 ]create-custom-builder 1 [ setxy -5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy 5 23 set heading 0 set btime 15 ]create-custom-builder 1 [ setxy -5 37 set heading 90 set btime 10 ]create-custom-builder 1 [ setxy 0 -7 set heading 75 set btime 28 ]create-custom-builder 1 [ setxy 0 -7 set heading 315 set btime 13 ]create-custom-builder 1 [ setxy -10 2 set heading 30 set btime 16 ]create-custom-builder 1 [ setxy 26 0 set heading 300 set btime 30 ]create-custom-builder 1 [ setxy 0 15 set heading 0 set btime 1 ] create-custom-builder 1 [ setxy -4 -12 set heading 0 set btime 5 ]create-custom-builder 1 [ setxy -4 16 set heading 0 set btime 5 ]ask builder [ while [btime > 0] [ ask patch-here [ set wall? true ask neighbors [set wall? true] ] if(abs pxcor-of patch-ahead 1 > screen-edge-x - 1 or abs pycor-of patch-ahead 1 > screen-edge-y - 1) [rt 180] fd 1 set btime btime - 1 ] die ]set temp temp + 1ask patches[if (wall?) [set pcolor 9]]

setup-hivesetup-foodend