Re: [netlogo-users] manipulating and creating lists

Expand Messages
• Doh! So simple when you know how. Well I learnt a lot of other ways to construct NL in the process of /not /working this out :-) Many thanks again James and
Message 1 of 6 , Jul 1, 2008
Doh!

So simple when you know how.

Well I learnt a lot of other ways to construct NL in the process of /not
/working this out :-)

Many thanks again James and Esther.

Esther Verreau wrote:
> Hi Howard -
>
> I think what you want is N-OF.
>
> so your code might look like this:
>
> set away-times n-of ((random 4) + 1) [ 10 11 12 13 15 17 ]
>
> so away-times will be a list of 1 - 4 numbers all of which are in the
> list, numbers will no be repeated.
>
> good luck
>
> esther
>
>
> On Fri, 27 Jun 2008, Howard Noble wrote:
>
>
>> dear all,
>>
>> i want to create a list of numbers based on a small pre-defined list.
>> i've been round and round the houses trying to do this, finally giving
>> up with this monster:
>>
>> set away-times ( list sublist shuffle [ 10 11 12 13 15 17 ] 0 ((random
>> 4) + 1 ) ) ;; the + 1 is to prevent the second value being 0
>>
>> i want to use 'away times' in an if statement, something like this
>>
>> if offsite? = false and hour-in-day = one-of away-times [
>> set user-state "onsite-not-using"
>> set using? false
>> let away-duration one-of [ 0.5 1 2]
>> show away-duration
>> set return-time hour-in-day + away-duration
>>
>> the trouble is i keep getting the error that away times = 0 and from the
>> code above it seems that return-time is never being set
>>
>> can anyone tell me where my code is wrong please? i want away-times to
>> always have at least one value taken from the list in the first line of
>> code above. i want return-time to have at least one value from away-duration
>>
>> many thanks, howard
>>
>> ps thanks for all the help so far!
>>
>>
>>
>
>
• actually i wonder if there is a bug somewhere other than my code (not likely i know) the away-times is still causing an error: error while user 0 running
Message 2 of 6 , Jul 1, 2008
actually i wonder if there is a bug somewhere other than my code (not
likely i know)

the away-times is still causing an error:

error while user 0 running MEMBER? in procedure SET-STATE-OF-USER
called by procedure GO
MEMBER? expected input to be a string or list or agentset but got the
(halted running of go)

appears. it just seems that the list is set to zero occasionally at
random (e.g. not every run but most). theories i've tried: (1) hatched
computers are not being initialised properly (2) its possible to press
the go button before the setup button has finished initialising...

i've decided to hack the code for now with an ifelse fake of exception
handling e.g. ;; ifelse away-times != 0 [

but ideally i'd like to do things properly. so if anyone has time to run
the code and come up with some ideas that'd be really good:

http://modelling4all.wikidot.com/local--files/temp-howard/LowCarbonICTversion27.nlogo

regards, howard

James Steiner wrote:
> If I'm reading your code right, you want to randomly select a random
> quantity of items (1 to 4) from a list.
>
> That would be:
>
> set away-times n-of (1 + random 4) [ 10 11 12 13 15 17 ]
>
> So.
>
> Now, in the second part of your example, it looks like you write this:
>
> ... hour-in-day = one-of away-times ...
>
> I don't think this is really what you mean.
>
> This means: "is the current hour-in-day equal to a randomly selected
> one of the away-times"
>
> Note that each time this expression runs the randomly selected
> away-time may be different.
>
> You probably meant "is the current hour-in-day one of the members of
> away-times?"
> In other words: "can the current hour-in-day be found in the list away-times?"
> In other words: "is the hour-in-day a member of the list away-times?"
>
> The code, then, is:
>
> if offsite? = false and member? hour-in-day away-times
>
> The member? reporter works to find values in a list, substrings in a
> string, or agents in an agentset. In general, the syntax is "member?
> needle haystack" Note that this only tells you that the needle exists
> in the haystack, not the location of the needle in the haystack. For
> that (and only for strings and lists) you need to use "position needle
> haystack". That reports either the index of the first occurance of
>
> ~~James
>
> On Fri, Jun 27, 2008 at 10:50 AM, Howard Noble
> <howard.noble@...> wrote:
>
>> dear all,
>>
>> i want to create a list of numbers based on a small pre-defined list.
>> i've been round and round the houses trying to do this, finally giving
>> up with this monster:
>>
>> set away-times ( list sublist shuffle [ 10 11 12 13 15 17 ] 0 ((random
>> 4) + 1 ) ) ;; the + 1 is to prevent the second value being 0
>>
>> i want to use 'away times' in an if statement, something like this
>>
>> if offsite? = false and hour-in-day = one-of away-times [
>> set user-state "onsite-not-using"
>> set using? false
>> let away-duration one-of [ 0.5 1 2]
>> show away-duration
>> set return-time hour-in-day + away-duration
>> 1
>> the trouble is i keep getting the error that away times = 0 and from the
>> code above it seems that return-time is never being set
>>
>> can anyone tell me where my code is wrong please? i want away-times to
>> always have at least one value taken from the list in the first line of
>> code above. i want return-time to have at least one value from away-duration
>>
>> many thanks, howard
>>
>> ps thanks for all the help so far!
>>
>
>
• Hi Howard - I didn t trace through all your code but it looks to me like clear-all is getting called at some point (which resets all global variables to 0) and
Message 3 of 6 , Jul 2, 2008
Hi Howard -

I didn't trace through all your code but it looks to me like clear-all is
getting called at some point (which resets all global variables to 0) and
the list is not getting reinitialized. For some reason it's not getting to
this line:

set away-times n-of ((random 4) + 1) [ 10 11 12 13 15 17 ]

it could be because there are no users (I think it's inside an ask users)
or maybe day-in-year doesn't equal 364 or offsite? is true, you see where
I'm going here? The best way to debug is by adding print statements that
tell you something about the condition that is about to be tested. So
before:

if day-in-year = 364

print (word "day in year = " day-in-year)

I hope this helps. good luck.

esther

On Tue, 1 Jul 2008, Howard Noble wrote:

> actually i wonder if there is a bug somewhere other than my code (not
> likely i know)
>
> the away-times is still causing an error:
>
> error while user 0 running MEMBER? in procedure SET-STATE-OF-USER
> called by procedure GO
> MEMBER? expected input to be a string or list or agentset but got the
> (halted running of go)
>
> appears. it just seems that the list is set to zero occasionally at
> random (e.g. not every run but most). theories i've tried: (1) hatched
> computers are not being initialised properly (2) its possible to press
> the go button before the setup button has finished initialising...
>
> i've decided to hack the code for now with an ifelse fake of exception
> handling e.g. ;; ifelse away-times != 0 [
>
> but ideally i'd like to do things properly. so if anyone has time to run
> the code and come up with some ideas that'd be really good:
>
> http://modelling4all.wikidot.com/local--files/temp-howard/LowCarbonICTversion27.nlogo
>
> regards, howard
>
> James Steiner wrote:
>> If I'm reading your code right, you want to randomly select a random
>> quantity of items (1 to 4) from a list.
>>
>> That would be:
>>
>> set away-times n-of (1 + random 4) [ 10 11 12 13 15 17 ]
>>
>> So.
>>
>> Now, in the second part of your example, it looks like you write this:
>>
>> ... hour-in-day = one-of away-times ...
>>
>> I don't think this is really what you mean.
>>
>> This means: "is the current hour-in-day equal to a randomly selected
>> one of the away-times"
>>
>> Note that each time this expression runs the randomly selected
>> away-time may be different.
>>
>> You probably meant "is the current hour-in-day one of the members of
>> away-times?"
>> In other words: "can the current hour-in-day be found in the list away-times?"
>> In other words: "is the hour-in-day a member of the list away-times?"
>>
>> The code, then, is:
>>
>> if offsite? = false and member? hour-in-day away-times
>>
>> The member? reporter works to find values in a list, substrings in a
>> string, or agents in an agentset. In general, the syntax is "member?
>> needle haystack" Note that this only tells you that the needle exists
>> in the haystack, not the location of the needle in the haystack. For
>> that (and only for strings and lists) you need to use "position needle
>> haystack". That reports either the index of the first occurance of
>>
>> ~~James
>>
>> On Fri, Jun 27, 2008 at 10:50 AM, Howard Noble
>> <howard.noble@...> wrote:
>>
>>> dear all,
>>>
>>> i want to create a list of numbers based on a small pre-defined list.
>>> i've been round and round the houses trying to do this, finally giving
>>> up with this monster:
>>>
>>> set away-times ( list sublist shuffle [ 10 11 12 13 15 17 ] 0 ((random
>>> 4) + 1 ) ) ;; the + 1 is to prevent the second value being 0
>>>
>>> i want to use 'away times' in an if statement, something like this
>>>
>>> if offsite? = false and hour-in-day = one-of away-times [
>>> set user-state "onsite-not-using"
>>> set using? false
>>> let away-duration one-of [ 0.5 1 2]
>>> show away-duration
>>> set return-time hour-in-day + away-duration
>>> 1
>>> the trouble is i keep getting the error that away times = 0 and from the
>>> code above it seems that return-time is never being set
>>>
>>> can anyone tell me where my code is wrong please? i want away-times to
>>> always have at least one value taken from the list in the first line of
>>> code above. i want return-time to have at least one value from away-duration
>>>
>>> many thanks, howard
>>>
>>> ps thanks for all the help so far!
>>>
>>
>>
>
Your message has been successfully submitted and would be delivered to recipients shortly.