with

 

with is a reporter primitive that allows us to extract a sub-agentset from an agentset based on a provided set of conditional statements. A condition within a with statement is similar to an if statement. For example, if we wanted to model a predator-prey ecosystem where a hawk could only see brighter colored mice, but not the darker colored ones, we would write the following code:

ask hawks [
    if any? (mice-here with [color = gray and distance myself < 2]) [
        hunt
    ]
]

Notice that we first wrote the name of the agentset (mice-here), then used the with primitive, and then provided our conditional statements within brackets ([ ]).

Things to keep in mind when using with:

In the model example below, we have three parallel roads and each road has a car on it. In the go procedure, we use with to differentiate between the speeds of blue cars and red cars. Blue cars go twice as fast as red cars when they have enough gas. When a car does not have enough gas, it goes much much slower. Lastly, we use with to stop the model when all the cars run out of gas.

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the with primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the with primitive:
 
 
Similar primitives:
if

Carries out a provided set of rules (code) if a given condition is true. Does nothing if a given condition is false.

Read more
of

Reports the value of an agent-owned variable.

Read more
turtles-own

Declare a variable that belongs to turtles.

Read more
patches-own

Defines custom characteristics (variables) for patches. Each custom characteristic can have a different value for each patch.

Read more
 
Learn another primitive: