Cannot run the procedure - netlogo

Can someone help me get around the error coming in the procedure below when the code if run. At runtime, it gives an
error messsage: "Expected a literal value"
Thanks
to slow-down-car [ car-ahead ] ;; turtle procedure
[ speed ] of car-ahead - deceleration
]
if speed != 0 [ set speed deceleration ]
set patience patience - 1
end

You have a few syntax errors:
[ speed ] of car-ahead - deceleration
]
Notice you have one too many ]'s at the bottom. Also, the statement [ speed ] of car-ahead - deceleration doesn't make any sense. Are you trying to assign this to a variable?

Related

Getting turtles to spend energy while moving on a least cost path in a network environment

I’m creating a model in which turtles choose and traverse a least cost path in a network environment, one node at a time.
I’m using the nw:turtles-on-weighted-path-to primitive from the NW extension to let my turtles choose a path. Relevant snippets of my code (which so far works fine) are below:
undirected-link-breed [ routes route ]
undirected-link-breed [ tracts tract ]
breed [ stalkers stalker ]
stalkers-own [ energy ]
links-own [ difficulty ]
to setup-network
ask routes [
set thickness 2
set color white
set difficulty 1
]
ask tracts [
set thickness 2
set color black
set difficulty 2
]
end
to enter-the-zone
ask stalkers [
let path nobody
let target one-of nodes-on patch -10 42
ask nodes-on patch-here [ set path but-first nw:turtles-on-weighted-path-to one-of nodes-on patch -10 42 difficulty ]
ifelse patch-here != ( patch -10 42 )
[
face first path
move-to first path
]
[ return_to_base
]
]
tick
end
to return_to_base
move-to patch -258 -231
end
However, I would like to add an “energy” attribute to my turtles that depletes a little every time they reach a new node. The catch is: I would like this loss of energy to be equal to the difficulty of the link between the node they are now and the node in which they just were.
What would be the best way to implement that?
Thanks in advance for the help.

Turtles not reseting under a conditional

I'm trying to have turtles match based on attraction parameters, however I only get either one match or one mismatch and won't count anymore turtles matching, as well as not moving the counter forward.
to new-couple
set countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
The code that you have provided does not have an error. Try this version of it and you will see that the new-couple code correctly reduces the countdown 5 times and then kills the rest of the turtles. So it's not producing the problem that you describe of things only happening once.
to testme
clear-all
create-turtles 10
type "Start turtles: " print count turtles
new-couple
type "End turtles: " print count turtles
end
to new-couple
let countdown2 5
ask turtles [
;; CREATE NEW COUPLE
ifelse countdown2 <= 0
[ die ]
[ set countdown2 countdown2 - 1 ] ]
end
Could you please provide more of your code. Also, an explanation of what the countdown is supposed to achieve could be useful. At the moment it basically selects the number of turtles that don't die and there are easier ways to achieve that.

Netlogo: How to compare the ID and then flag in this case?

I would like to put a flag called "min-id" for the turtle with the smallest ID. And I want to flag other turtles as "not-min-id". However the following sample syntax has errors. The error message is as follows.
" error while turtle 0 running >
called by procedure GO
called by button 'go' "
I probably to need your advice. Thank you.
globals [ min-id not-min-id count-up ID ]
to go
reset-ticks
ask patch 0 0
[
sprout 1 ;;This model needs to use sprout.
]
ask (turtles-on patch 0 0)
[
set ID who
setxy min-pxcor 0
set heading 90
]
if (count turtles > 0)
[
ask min-one-of turtles [who]
[
set min-id TRUE
]
]
if (count turtles > 0)
[
ask (turtles-on patch 0 0)
[
if ID > min-one-of turtles [who] ;;This syntax has errors.
[
set not-min-id TRUE
]
]
]
ask (turtles-on patch 0 0) with [not-min-id]
[
set count-up count-up + 1
]
if (count turtles > 0) [
ask (turtles-on patch 0 0) with [min-id]
[
die
]
tick
end
You have some confusion in your code. From your description, I believe you want each turtle to have a flag for whether or not it has the minimum who number. This means you need a flag for each turtle. However, you have set up min-id as a global variable instead of a turtle variable. Furthermore, you only need the flag variable once (that is, you need min-id but not not-min-id) and you set it to TRUE or FALSE.
Replace
globals [ min-id not-min-id count-up ID ]
with
globals [ count-up ID ]
turtles-own [ min-id ]
and see if that fixes it. Also initialise min-id to FALSE as part of the sprout.
Having said all that, I strongly agree with Alan, if you ever use the who variable for anything except print statements in debug, you probably need to rethink your code. In your case, what is special about the turtle with the lowest who number that makes you want to keep track of it? Do you simply want a random turtle that happens to be at a particular location? Then select a random turtle at that location to do the TRUE/FALSE without going through who.

How to link actions of agents?

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.)

How to add to score after a turtle dies

So my go procedure right now is
to Go
ask turtles with [is-moving?]
[
if any? other turtles-here [
ask other turtles-here [
die
]
]
]
end
(is-moving? is just a turtles-own variable to specify the only turtle that i want to move)
I wanted to add 5 points to the score everytime it went through the go procedure and a turtle died. I already set a global variable to score. Thanks!
If score is global variable just add set score Score + 5
to Go
ask turtles with [is-moving?]
[
if any? other turtles-here [
ask other turtles-here [
set score Score + 5
die
]
]
]
end
Update:
I think you dont need to multiply it since all turtles will update the score value ! (my mistake! )To monitor Score :