Attract Agents to Mouse Cursor, via the ’’attract and repel’’
1. Create a number of turtles positioned randomly on the screen
2. State the ’’REPEL FUNCTION’’ for the turtles to form a regular spacing
3. Once the regular spacing between the turtles is formed, the turtles are connected through the additional ’’BRACINGFUNCTION’‘
The Connected (Braced) Turtles are attracted to the Mouse Cursor.
turtles-own [closest-turtle] ;; to define that the closest turtle it depends to a family of turtles. and this part is defined by the distance and myself.
to setup
ca
crt howmany
ask turtles [
setxy random-xcor random-ycor
set shape “line”
set color white
set size 10
set heading random 360
;;———————-
set shape “line”
set color random 77
set size 10
set heading random 360
;;—————————
]
end
;; now tell the turtles o go through the family.
to repel
ask turtles[
set closest-turtle min-one-of other turtles [distance myself] ;; functions callled by net logo. to go through every turtle. and every turtla has to find the closest turtle via the minimum value/number. ie. turtle 0 will specify which other turtle is the closest.
;;once closest turtle defined it will draw a line. the other it is not confuse and calculate ‘myself’
;; so once found closest turtle, move backwards. to do so we have to set:
face closest-turtle ;;turn your head to the closest turtle
bk 0.1 ;;asnother way to write the heading. more like turtle-like
]
end
to bracethem
ask turtles [
set closest-turtle min-one-of other turtles [distance myself]
create-link-with closest-turtle
]
end
to attract
ask turtles
;;2 thing will happen: a. move forwards, b. move backwards. then make a slider. these are the conditional statements. if the fistance of xy cords sre biger than the radius then turtles will have to walk to become closer. else…. walk backwards.
<em lang=”_
_set heading towardsxy mouse-xcor mouse-ycor_
_ifelse distancexy mouse-xcor mouse-ycor ;; if more is than
_
]
_end
to clearlinks
clear-links
end
to paint
export-view try
end
Patches
to setup
ca
create-turtles howmany
ask turtles [
setxy 0 0
set heading random 360
set shape “line”
set color random 155
set size 1
set heading random 360
]
end
to go
let dir 0 ;dir starts from zero every time. and then it get a new value deoending on the flipcoin
let flipcoin random 1 ;create a variable for direction. to decide which direction
;let is a new local variable that can be accesed from any procedure of the programm. local definition of a variable.
ifelse flipcoin > 10 ;;true or false value. boolean operation.
_
[set dir -90] ;;here dir takes new value_
ask turtles [
set heading heading dir ;; new variable to switch between a positive and a negative number for the direction
fd 1
pd
set dir dir 1
]
end
;;2 important entities are turtles and patches. patches can be asked to execute a command. one can ask patches in the same command.
to paint
export-view try
end
FlipCoin Squares
to setup
ca
create-turtles howmany
ask turtles [
setxy 0 0
set heading random 360
set shape “line”
set color random 155
set size 1
set heading random 360
]
end
to go
let dir 0 ;dir starts from zero every time. and then it get a new value deoending on the flipcoin
let flipcoin random 20 ;create a variable for direction. to decide which direction
;let is a new local variable that can be accesed from any procedure of the programm. local definition of a variable.
ifelse flipcoin > 5 ;;true or false value. boolean operation.
_
[set dir -90] ;;here dir takes new value_
ask turtles [
set heading heading + dir ; new variable to switch between a positive and a negative number for the direction
fd 2
pd
]
end
;;2 important entitied sre turtles and patchres. patches can be asked to executer a command. one can ask patches in the same command.
to paint
export-view try
end
Box Trail In Space
;;……………………………..STEP1
to setup
ca ;; CLEAR SCREEN
create-turtles 10;;NUMBER
ask turtles [
setxy 0 0 ;;starting point
set heading random 360
set shape “box”
set size 2
set color white
]
end;;
;;……………………………..STEP2
;;FUNCTION
to go
ask turtles [
pd
jump 1
fd 1 ;;The turtle moves forward by number steps, one step at a time. (If number is negative, the turtle moves backward.)
set heading heading + 10
]
end
to paint export-view try
end
Turtle Trails
to setup
ca ;; the function that clears the screen
create-turtles howmany ;; howmany is a variable
ask turtles [
setxy 0 0 ;;starting point
set heading random 360 ;; Random requires a number. It is between 0 and 360 (Degrees).
set shape “turtle”
set size 1
set color 8
]
end
;;——————————————————————————
to go
ask turtles [
fd 1 ;;The turtle moves forward by number steps, one step at a time. (If number is negative, the turtle moves backward.)
set heading heading + 10
pd ;; The turtle leaves the trail
]
end
to paint
export-view try
end
http://ccl.northwestern.edu/netlogo/
NetLogo is a multi-agent programmable modeling environment. It is used by tens of thousands of students, teachers and researchers worldwide. It also powers HubNet participatory simulations. It is authored by Uri Wilensky and developed at the CCL. You can download it free of charge.
NetLogo was designed in the spirit of the Logo programming language to be “low threshold and no ceiling,” that is to enable easy entry by novices and yet meet the needs of high powered users. The NetLogo environment enables exploration of emergent phenomena. It comes with an extensive models library including models in a variety of domains such as economics, biology, physics, chemistry, psychology, system dynamics and many other natural and social sciences. Beyond exploration, NetLogo enables the quick and easy authoring of models.
It is particularly well suited for modeling complex systems developing over time. Modelers can give instructions to hundreds or thousands of independent “agents” all operating concurrently. This makes it possible to explore the connection between the micro-level behavior of individuals and the macro-level patterns that emerge from the interaction of many individuals.
NetLogo has many thousands of active users. It is freely available from the NetLogo website. NetLogo is in use in a wide variety of educational contexts from elementary school to graduate school. Many teachers make use of NetLogo in their curricula. NetLogo comes bundled with a large library of sample models covering many domains in natural and social science.
NetLogo was designed and authored by Uri Wilensky, director of Northwestern University’s Center for Connected Learning and Computer-Based Modeling. Development has been funded by the National Science Foundation and other foundations.
Flow Chart and Diagram
breed [cats cat] ; creating the breed of cats
breed [mice mouse] ; creating the breed of mice
The opening statement is to ‘breed.’ ‘Mice’ and ‘mouses’ sets out the program language, of having both a family and an individual. These can be of any names, if the breeds are other than turtles.
Individuals that are close to one another are defined and named as the following. When starting off the agents, the agents will then need to be defined further, by a direction for example.
mice-own [closest-mouse danger-cat cat-alert killdis] ; variable assigned to the mice
cats-own [chase-mouse runawaya] ; variable assigned to the cats
globals© ; variable
to setup ; setup the agents
create-cats howcats ; creating the agents and assigning a variable to the number
create-mice howmice ; creating the agents and assigning a variable to the number
sk cats[
setxy random-xcor random-ycor ; sets a random starting point for each agent
set shape “circle” ; sets the shape of each of the agents
set size .5 ; sets the size of the agents
set color red ; sets the colour of the agents
]
ask mice[
setxy random-xcor random-ycor ; sets a random starting point for each agent
set shape “circle” ; sets the shape of each of the agents
set size 1 ; sets the size of the agents
set color grey ; sets the colour of the agentsv
]
_set© ; sets veriable
end
to wiggle
let flipcoin random 10 ; local variable with a random starting number from 1 to 10
let choice 0 ; local variable with a starting number of 0
ifelse flipcoin > 5 ; this command allows for an action to be met if the criteria is met otherwise the second action is taken.
; action to be taken if the criteria is met
; action to be taken if the criteria is not met
ask mice [
set heading heading + choice ; sets the heading for the agents based on the current heading and the addition of the dir variables value
fd 0.1 ; ask the agents to move forward one step at a time
]
end
to repel
ask mice[
set closest-mouse min-one-of other mice [distance myself] ; sets the veriable closest-mouse to the distance of the closest mouse _around its self
if closest-mouse != nobody [ ; if statment asking mice to stop repeling eachother if there are no mice
face closest-mouse ; turns the agent to face its closest-mouse
bk 0.1 ; asks the agent to move back 1 step away from the closest agent
]]
end
to chase
ask cats[
set chase-mouse min-one-of other mice [distance myself] ; sets the veriable chase-mouse to the distance of the closest mouse around its self
if chase-mouse != nobody [ ; if statment asking cats to stop chasing mice if there are no mice
face chase-mouse ; turns the agent to face its chase-mouse
fd 0.1 ; asks the agent to move forward 1 step away from the closest agent
]]
end
to flee
ask mice[
set danger-cat min-one-of other cats [distance myself] ; sets the danger-cat myself to the distance of the closest cat around its self
if danger-cat != Nobody [ ; if statement asking mice to stop repeling eachother if there are no mice
’nobody’ is a special value used in Netlogo used to indicate the absence of a turtle or patch. The brackets tells the turtle the command to run. ‘set heading towards closest-mouses’ is another command that uses a reporter. There is no space in the words ‘closest-mouses’ as the hyphen combines the two words into a single name.
face danger-cat ; turns the agent to face its danger-cat
bk 0.2 ; asks the agent to move back 0.2 step away from the closest agent
]]
end
Manipulating the Code
breed [cats cat] ;; [family one-member]
breed [mice mouse]
mice-own [closest-mouse danger-cat] ;; variable for repet _f_unction
cats-own [cat-chases-mouse]
;;—————————————————————————————————————-
to setup
ca
create-cats howmanycats
create-mice howmanymice
ask cats [ ;; setting up the cats
setxy random-xcor random-ycor
set shape “line”
set size 1
set color white
]
ask mice [ ;; setting up the mice
setxy random-xcor random-ycor
set shape “circle”
set size .5
set color random 45
]
;;ask patches [
;;set pcolor white
;;]
end
;;—————————————————————————————————————-
to wiggle
let flipcoin random 10 ;; local variable
let choice 1
ask mice [
ifelse flipcoin < 360
_
[set choice 1 ]
_set heading heading + choice
fd 1
pd
]
end
;;—————————————————————————————————————-
to repel
ask mice [
_set closest-mouse min-one-of other mice [distance myself]
face closest-mouse;; or set heading towards closest-mouse
bk 1
pd
]
end_
;;—————————————————————————————————————-
to chase
ask cats [
set cat-chases-mouse min-one-of other mice [distance myself];;
face cat-chases-mouse
fd 1
pd
]
end
;;—————————————————————————————————————
to flee
ask mice [
set danger-cat min-one-of other cats [distance myself]
face danger-cat
bk 1
pd
]
end
;;—————————————————————————————————————
to capture
export-view try
end