On this page

# AgentSet

A model's Patches, Turtles, Links, are all subclasses of AgentSet.

AgentSets are AgentArrays that are factories for their own Agents. That means you do not call `new Turtle()`, rather Turtles will create the Turtle instances, adding them to itself.

Finally, a Breed is simply a subarray of Patches, Turtles, Links. Patches could have a Streets breed, Turtles could have Cops and Robbers breeds, and Links Spokes and Rim breeds

AgentSets are not created directly by modelers. Instead, class Model creates them along with their Breeds. You can easily skip this initially, instead simply understand AgentSets are the basis for Patches, Turtles, Links & Breeds

## Constructor

### new AgentSet(model, AgentClass, name, baseSetopt)

Parameters:

Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`model` | Model |  |  | Instance of Class Model to which I belong
`AgentClass` | Patch | Turtle | Link |  |  | Class of items stored in this AgentSet
`name` | String |  |  | Name of this AgentSet. Ex: Patches
`baseSet` | Patches | Turtles | Links | <optional> | null | If a Breed, it's parent AgentSet

Source
  - AgentSet.js, line 27

## Methods

### protoMixin(agentProto, AgentClass)

Add common variables to an Agent being added to this AgentSet.

Each Agent has it's AgentSet and the Model instance. It also has an id, set by the AgentSet's global ID.

The Agent also has three methods added: setBreed, getBreed, isBreed.

Parameters:

Name | Type | Description
--- | --- | ---
`agentProto` | Object | A new instance of the Agent being added
`AgentClass` | Patch | Turtle | Link | It's Class

Source
  - AgentSet.js, line 76

### addAgent(o) → {Object}

Add an Agent to this AgentSet. Only used by factory methods. Adds the `id` property to Agent. Increment AgentSet `ID`.

Parameters:

Name | Type | Description
--- | --- | ---
`o` | Object | An Agent to be added to this AgentSet

Source
  - AgentSet.js, line 125

Returns:

The input Agent, bound to this AgentSet.

Type:
  Object

### removeAgent(o) → {AgentSet}

Remove an Agent from this AgentSet

Parameters:

Name | Type | Description
--- | --- | ---
`o` | Object | The Agent to be removed

Source
  - AgentSet.js, line 141

Returns:

This AgentSet with the Agent removed

Type:
  AgentSet

### setBreed(a) → {Agent}

Move an agent from its AgentSet/breed to be in this AgentSet/breed

Parameters:

Name | Type | Description
--- | --- | ---
`a` | Agent | An agent, a member of another AgentSet

Source
  - AgentSet.js, line 161

Returns:

The updated agent

Type:
  Agent

### setDefault(name, value) → {AgentSet}

Set a default value shared by all Agents in this AgentSet

Parameters:

Name | Type | Description
--- | --- | ---
`name` | String | The name of the shared value
`value` | any |

Source
  - AgentSet.js, line 189

Returns:

This AgentSet

Type:
  AgentSet

### getDefault(name) → {any}

Return a default, shared value

Parameters:

Name | Type | Description
--- | --- | ---
`name` | String | The name of the default

Source
  - AgentSet.js, line 200

Returns:

The default value

Type:
  any

### newBreed(name) → {AgentSet}

Create a subarray of this AgentSet. Example: create a people breed of Turtles:

`people = turtles.newBreed('people')`

Parameters:

Name | Type | Description
--- | --- | ---
`name` | String | The name of the new breed AgentSet

Source
  - AgentSet.js, line 214

Returns:

A subarray of me

Type:
  AgentSet

### isBreedSet() → {boolean}

Source
  - AgentSet.js, line 221

Returns:

true if I am a baseSet subarray

Type:
  boolean

### isBaseSet() → {boolean}

Source
  - AgentSet.js, line 227

Returns:

true if I am a Patches, Turtles or Links AgentSet

Type:
  boolean

### withBreed(breed) → {AgentArray}

Return breeds in a subset of an AgentSet

Ex: patches.inRect(5).withBreed(houses)

Parameters:

Name | Type | Description
--- | --- | ---
`breed` | AgentSet | A breed AgentSet

Source
  - AgentSet.js, line 239

Returns:

Type:
  AgentArray

### clear()

Remove all Agents from this AgentSet using agent.die() for each agent.

Source
  - AgentSet.js, line 252

### ask(fcn)

Call fcn(agent, index, array) for each item in AgentArray. Index & array optional. Overrides AgentArray's ask with additional guards for modifications in AgentSet's array.

Parameters:

Name | Type | Description
--- | --- | ---
`fcn` | function | fcn(agent, index?, array?)

Source
  - AgentSet.js, line 264

Type anything to view search result