My first netlogo program was working well, but now is failing since 'tick' in the 'go' method is not in a valid context.
Please see the attached code, line 99, which generates:
You can't use tick in a turtle/patch context, because it is observer only.
Code is here:
http://jpark.us/temp/CSSS.v1.nlogo
Problem solved...
I was trying to 'set number.sparrows...' down in other methods, but not within a proper patches context.
So this works:
if all? patches [ eggs.laid = true ] [
ask patches [ set number.sparrows count sparrows-here ]
whereas this does not:
if all? patches [ eggs.laid = true ] [
set number.sparrows count sparrows-here
Related
NetLogo does not have a native SWITCH, SWITCH-CASE, or SELECT-CASE type of multiple condition, how do I do the same thing without having a giant mess of nested IF-ELSE statements?
Since NetLogo 6.1, NetLogo has supported multi-case ifelse, which can be used similarly to other languages' switch statements. Here is an example from the docs:
ask patches [
let choice random 4
(ifelse
choice = 0 [
set pcolor red
set plabel "r"
]
choice = 1 [
set pcolor blue
set plabel "b"
]
choice = 2 [
set pcolor green
set plabel "g"
]
; elsecommands
[
set pcolor yellow
set plabel "y"
])
]
And here is the example from the other answer rewritten to use it:
;; example of SWITCH style conditional block
(ifelse
(criteria-1) [ action-1 ]
(criteria-2) [ action-2 ]
(criteria-3) [ action-3 ]
[ default-action ]
)
This does not need to have the "multi-close" of the ] on the last line, so it overall seems cleaner to me.
We can simulate a SWITCH statement with a chain of nested IF-ELSE statements, formatted so it's not a big mess, and actually easy to read and edit.
We will format the chain of IF-ELSE statement with the following goals:
avoid excessive indenting,
avoid overly long code lines
make it look more like switch in other languages
rearranging the cases does not break the syntax
there is a default case
A SWITCH Statement
Here's how it looks:
;; example of SWITCH style conditional block
if-else [ criteria 1 ] [ action-1 ][
if-else [ criteria 2 ] [ action-2 ][
if-else [ criteria 3 ] [ action-3 ][
if-else [ true ] [ default-action ][
]]]]
Other considerations
Some programmers may find it offensive to have a default action that tests for true, and leaves the "else block" if the final "IF" empty. You can do what you like. I find making the default case explicit, makes all the actions formatted exactly the same, and avoids an awkward cluster of punctuation at the end.
Avoid overly long lines--it makes it harder to read
Keep conditions short--wrap them in reporter if needed.
Keep actions short--wrap them in procedures if needed.
This helps the code read more like a story and less like code.
I am trying to make an agentset do something, if an agent(of a different agentset) has a particular shape.
Here, if the shape of a particular
ghost (say Ghost 1) is circle,
then all rabbits are supposed to move forward 1. (<-This is the
intended behavior)
where
ghosts are agentset A
rabbits are agentset B
I have tried along these lines:
ask rabbits
[
if (shape ghost 1 = "circle")
[
forward 1
]
]
For this code I get,
"Expected a closing paranthesis here."
with a highlighter on ghost.
I'm aware that this code is wrong but I can't think of how else this should be written to get the desired result.
This will (I think - can't test) get the syntax correct:
ask rabbits
[
if ([shape] of ghost 1 = "circle")
[
forward 1
]
]
but you also have an ordering error and will have every rabbit check the shape of chost 1. I think what you really want is:
if ([shape] of ghost 1 = "circle")
[ ask rabbits
[ forward 1
]
]
I would like to change the value of the variable my-inventory-slot of a set of agents but only in the case that value is not repeated (i.e. his value is unique in the set).
breed [ tags tag ]
tags-own [ my-inventory-slot inventoried? ]
breed [ readers reader ]
and
and I have tested
ask my-tags with [ my-inventory-slot != [my-inventory-slot] of self ] [
set inventoried? true
set color red
]
where my-tagsis a reader variable containing the tags around the reader.
The problem is in the selection with [ my-inventory-slot != [my-inventory-slot] of self ] because I have try my-inventory-slot = 5 and the code works fine.
UPDATED
let thistag [ my-inventory-slot ] of one-of my-tags
ask tags with [ my-inventory-slot != thistag ] [
set inventoried? true
set color red
]
UPDATED AGAIN - after downloading code
This is the code that calls the problem procedure:
to go
ask reader 0 [
setup-inventory-in-frame
one-frame-inventory
]
end
So a reader is being asked to run the code. However, readers do not have a variable my-inventory-slot, this is a variable belonging to the tags breed. This is your problem, you need to work out the connection between the reader running the code and the my-inventory-slot variable that you want the match to.
From the chat discussion, what you actually want is the tags belonging to the reader (that is, the variable my-tags) that have unique values of my-inventory-slot. I think this code will do that:
to one-frame-inventory
let frame-time 0
let unique-tags my-tags
let forward-list [ my-inventory-slot ] of my-tags
let reverse-list reverse forward-list
let num-tags count my-tags
ask my-tags
[ if position my-inventory-slot forward-list + position my-inventory-slot reverse-list != num-tags - 1
[ set unique-tags other unique-tags ]
]
ask unique-tags
[ set color red
set inventoried? true
]
end
It is very ugly, so someone else's answer would be good. What this does is strips out the my-inventory-values for the relevant tags into a list. It creates two copies of that list, one in normal order and the other reversed. It identifies non-unique values by finding those with a different position for the first appearance in those two lists. If it's not unique, the relevant tag is removed from the unique-tags agentset.
I have solved the question asking any other my-tags, like this:
let kk my-tags
ask my-tags [
if not any? other kk with [ my-inventory-slot = [my-inventory-slot] of myself ] [
set inventoried? true
set color red
] ]
which is short and clear.
I am trying to program the simulation with 3 sets of agents - A,B,C.
The point is that the agents from the set A can choose to DO the action or NOT.
If they decide to NOT do the action, the simulation stops.
When they decide to DO the action, simulation continues to the next step, where the agents from the set B can also decide to DO the action or not. The same is here.
And the agents from the set C, can also decide to DO the action or NOT, but here, the simulation in both cases stops.
Here is my code:
ask turttles [
if breed = set A [ ifeslse do?= false [ set lazy]
stop]
[ if breed = set B [ ifelse do1?= false [ set lazy]
stop]
[ask other turtles [ if breed = set C [ ifelse do 2? = false [ set lazy
stop] ]
[set done
stop] ]
]
]
]
The code does not work very good,I need somehing to link these three step, because when I export-world, I got data only from the first step
If you do stop inside of an ask, it won't cause the whole simulation to stop. It will only stop the current turtle from executing the rest of the ask.
I think you want something more like:
globals [done?]
to setup
...
set done? false
...
end
to go
if done? [ stop ]
ifelse ...
[ ask A [ do-action ] ]
[ set done? true ]
ifelse ...
[ ask B [ do-action ] ]
[ set done? true ]
ifelse ...
[ ask C [ do-action ] ]
[ set done? true ]
...
end
But I'm guessing somewhat, since it's difficult to tell from your description what your actual intentions are. (Especially since you haven't included your real code — the coede in your question wouldn't get past the compiler.)
I want to save the links of the shortest path between the source and destination , so thagt i can change their color to red ie the links' color. But theres is no primitive to save the links
the code is:
ask nodes with [label = "Source" ]
[
show nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
can somebody tell me how to save the links reported by the nw primitive used above, so as to change their color to red in the graph?
I'm not entirely sure what you mean by saving the links, but you can store the list of links in a variable. So, if you have a turtles-own variable path-to-destination, you can just do
ask nodes with [label = "Source" ] [
set path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
Alternatively, you can just store the list of links in a local variable if you don't need to do anything with them later:
ask nodes with [label = "Source" ] [
let path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
As for turning them red, nw:weighted-path-to returns a list of links, so we can just loop through that list ask each to become red. Extending the previous code, that looks like:
ask nodes with [label = "Source" ] [
let path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
foreach path-to-destination [ ask ? [ set color red ] ]
]