Gabriel Fair
Gabriel Fair

Reputation: 4274

Is there a way to create an enum in netlogo?

I have some netlogo code that I would like to make more descriptive. So instead of:

MOVE-TO ONE-OF PATCHES WITH [ PCOLOR = BLUE ]

It would say:

MOVE-TO ONE-OF PATCHES WITH [ WATER ]

In java I would create an enum to accomplish this. How can I do it in Netlogo?

Upvotes: 1

Views: 370

Answers (3)

JenB
JenB

Reputation: 17678

Alan's answer is perfectly fine, but this question suggests to me a different conceptualisation. What you really mean is that the patch is coloured blue because it is water, but you are coding it the other way around so that colour is indicating its status as water. If other aspects of your model (eg travel speed, type of crops) depend on whether it is water or not, then you could consider a different construction.

patches-own
[ water?
]

to setup
  ask patches
  [ set water? FALSE
    if random-float 1 < 0.2
    [ set water? TRUE
      set pcolor blue
    ]
  ]
end

In this construction, you have a true/false variable for each patch that indicates it is water (if true). Then you can directly have statements such as ask patches with [water?] []. You can also set up a global variable that holds the patch-set of water patches and then make statements like ask water-patches []

If you have multiple types of land style (eg water, sand, soil, rock...) then you colour is more likely to be the way to go since you don't want separate variables for all of these. Even then, though, you could have one attribute for land style and have constructions that are ask patches with [ type = "water"]

Upvotes: 2

Nicolas Payette
Nicolas Payette

Reputation: 14972

Alan's answer is fine, but I would also consider the possibility of creating a patch variable instead of relying on the patch color. For example:

patches-own [ water? ]

If you set this to true for each water patch, you can then say things like:

move-to one-of patches with [ water? ]

The main reason for doing this is that you might want to change the color of the water patches at some point: make them a slightly darker or lighter blue, for example, or use color to temporarily highlight patches with some other characteristic.

Separating presentation and program semantics is generally good practice.

Another, different way to achieve this would be to create an agentset with your water patches during setup. For example, supposing you declare water-patches as a global variable, you would do:

set water-patches patches with [ pcolor = blue ]

And then you can do:

move-to one-of water-patches

The water-patches agentset is not affected if you change the color of a patch. It might also be a bit faster since you only construct it once instead of filtering over all patches over and over again.

Upvotes: 3

Alan
Alan

Reputation: 9620

to-report water ;patch proc
  report pcolor = blue
end

Upvotes: 2

Related Questions