Simple question about sequential commands vs. nesting them using ifelse

18 views
Skip to first unread message

JP Faguet

unread,
May 22, 2024, 11:59:59 AMMay 22
to netlogo-users
Hello!  I'm on sabbatical and trying to learn NetLogo and ABMs - apologies for the clueless question.

I made a few small modifications to the Simple Economy model that comes with NetLogo, to incorporate individual ability, public goods, and a tax/transfer scheme.  Wasn't sure how best to do it so I coded the Transact routine 2 different ways.  I thought they'd differ maybe in speed, but otherwise do the same thing.  But they seem to be giving me very different outputs.

So my question is: Do the following blocks of code do the same thing, or have I made a mistake with the ifelse commands?

Here is one of the 2 models attached in its entirety.  They only differ in the transact routine.

__________________________________________________________________________________
to transact
;; give money to another turtle; richest also pay transfer, poorest receive transfer
(ifelse
wealth >= mean [ wealth ] of max-n-of (count turtles * 0.333) turtles [ wealth ]
[ set wealth wealth - 1 - transfer ]
wealth <= mean [ wealth ] of min-n-of (count turtles * 0.333) turtles [ wealth ]
[ set wealth wealth - 1 + transfer ]
[ set wealth wealth - 1 ] )
ask one-of other turtles [ set wealth wealth + price + pub-good ]
end

_______________________________________________________________________________________

to transact
;; give money to another turtle
set wealth wealth - 1
ask one-of other turtles [ set wealth wealth + price + pub-good ]
if wealth >= mean [ wealth ] of max-n-of (count turtles * 0.333) turtles [ wealth ]
[ set wealth wealth - transfer ]
if wealth <= mean [ wealth ] of min-n-of (count turtles * 0.333) turtles [ wealth ]
[ set wealth wealth + transfer ]
end

_________________________________________________________________

Thanks so much!

JP
JPF_Simple Exchange_Ability_G_T_faster.nlogo

Alan

unread,
May 22, 2024, 4:52:37 PMMay 22
to netlogo-users
They are not the same, because both condition statements might execute in the second one.

But a better question to ask might be: how can you make it easier to determine sameness? One answer might be: practice command-query separation, because pure queries are much easier to test than mutating commands.  Here the query is, how much should the wealth of this turtle change? (For this you can use the ifelse-value reporter.) The command is, change the wealth of this turtle by a certain amount.

hth, Alan Isaac


--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/821f50f2-fcf5-4ea9-9a6b-093e3d6c627fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages