how to rewrite this code in netlogo 6.1.1? - netlogo

i'm transfering my code from net logo 3.1.5 to netlogo 6.1.1. i couldn't know how to make this line works in the new version. this is the code line in netlogo 3.1.5 and it works:
set d0 sort-by [length ?1 <= length ?2] d0
this line means sort the list d0 from the little to the big value and when finishing put the result in d0 again.
the [length ?1 <= length ?2] meanse compare the value of the temporary variable named ?1 with the value of temporary variable named ?2
in netlogo 6.1.1 it tells me that nothing named ?1 has been defined
can you please help me !

The NetLogo dictionary is available at https://ccl.northwestern.edu/netlogo/docs/dictionary.html It is a good starting point for finding the new syntax of any of the primitives you will have in your v3 code. In this case, one of the examples for sort-by is to sort on the length of a string. I'm guessing that the contents of d0 are strings - it' not clear from your question because you talk about sorting from small to large values but you have length in your code.
Assuming that you are sorting by length, here is the exact example as given in the dictionary:
show sort-by [ [string1 string2] -> length string1 < length string2 ] ["Grumpy" "Doc" "Happy"]
gives ["Doc" "Happy" "Grumpy"]
Translating to your case, you probably want:
set d0 sort-by [ [s1 s2] length s1 <= length s2] d0

Related

Printing part of an array in MiniZinc

I have a MiniZinc model for wolf-goat-cabbage in which I store the locations of each entity in its own array, e.g., array[1..max] of Loc: wolf where Loc is defined as an enum: enum Loc = {left, rght}; and max is the maximum possible number of steps needed, e.g., 20..
To find a shortest plan I define a variable var 1..max: len; and constrain the end state to occur at step len.
constraint farmer[len] == left /\ wolf[len] == left /\ goat[len] == left /\ cabbage[len] == left
Then I ask for
solve minimize len
I get all the right answers.
I'd like to display the arrays from 1..len, but I can't find a way to do it. When I try, for example, to include in the output:
[ "\(wolf[n]), " | n in 1..max where n <= len ]
I get an error message saying that I can't display an array of opt string.
Is there a way to display only an initial portion of an array, where the length of the initial portion is determined by the model?
Thanks.
Did you try to fix the len variable in the output statement like n <= fix(len)?. See also What is the use of minizinc fix function?

How to match a value of a list of numbers to item from a list of names in netlogo?

I am trying something (in netlogo), but it is not working. I want a value of a position from a list of numbers. And I want to use the number that comes out of it to retrieve a name from a list of names.
So if I have a list like [1 2 3 4] en a list with ["chicken" "duck" "monkey" "dog"]
I want my number 2 to correspond with "duck".
So far, my zq is a list of numbers and my usedstrategies is a list of names.
let m precision (max zq) 1
let l position m zq
let p (position l zq) usedstrategies
But when I try this the result will be false, because l is not part of usedstrategies.
Ideas?
You need the item primitive to select from the list after matching on the other list. I am not sure what the precision line is for. However, here is a self contained piece of code that I think demonstrates what you want to do. Note that NetLogo counts positions from 0, not 1. I also used arbitrary numbers in the list so you don't get confused between the number in the list and its position.
to testme
let usedstrategies (list "chicken" "duck" "monkey" "dog")
let zq (list 5 6 7 8)
let strategynum position 7 zq
let thisstrategy item strategynum usedstrategies
type "Selected strategy number " type strategynum
type " which is " print thisstrategy
end
Jen's solution is perfectly fine, but I think this could also be a good use case for the table extension. Here is an example:
extensions [table]
to demo
let usedstrategies ["chicken" "duck" "monkey" "dog"]
let zq [5 6 7 8]
let strategies table:from-list (map list zq usedstrategies)
; get item corresponding with number 7:
print table:get strategies 7
end
A "table", here, is a data structure where a set of keys are associated with values. Here, your numbers are the keys and the strategies are the values.
If you try to get an item for which there is no key in the table (e.g., table:get strategies 9), you'll get the following error:
Extension exception: No value for 9 in table.
Here is a bit more detail about how the code works.
To construct the table, we use the table:from-list reporter, which takes a list of lists as input and gives you back a table where the first item of each sublist is used as a key and the second item is used as a value.
To construct our list of lists, we use the map primitive. This part is a bit more tricky to understand. The map primitive needs two kind of inputs: one or more lists, and a reporter to be applied to elements of these lists. The reporter comes first, and the whole expression needs to be inside parentheses:
(map list zq usedstrategies)
This expression "zips" our two lists together: it takes the first element of zq and the first element of usedstrategies, passes them to the list reporter, which constructs a list with these two elements, and adds that result to a new list. It then takes the second element of zq and the second element of usedstrategies and does the same thing with them, until we have a list that looks like:
[[5 "chicken"] [6 "duck"] [7 "monkey"] [8 "dog"]]
Note that the zipping expression could also have be written:
(map [ [a b] -> list a b ] zq usedstrategies)
...but it's a more roundabout way to do it. The list reporter by itself is already what we want; there is no need to construct a separate anonymous reporter that does the same thing.

Global variable changes to 0

I have a to-report that divide a range into 11 steps, like this:
to-report random11steps
let step random 11 + 1
let step-value (minLM / 11)
let rnd 0
repeat step [
set rnd (rnd + step-value)
]
report rnd
end
where minLM is a global variable set to 0.005. When I run the procedure minLM changes to be '0.
If I substitute minLM by 0.005 the function works but I do not know why minLM changes to 0.
Besides, is there any primitive to make the code of this procedure a bit shorter?
Regards
This function is not changing minLM; look elsewhere.
Don't write functions that depend on global variables unless absolutely necessary; pass minLM as an argument.
It is unclear why you want to use repeated addition instead of multiplication. That is, you could just return (step * step-value).

using <> in omron PLC

I have a <>305 in my ladder logic like so
<> D19720 D3890
then after it i have a less then and a greater then like so
cf002 cf005
What it does is checks the encoders current value (d19720) with a stored value(d3890) To make sure they are the same value. Due to it being an encoder it would not take much for the value to be + or - 2 .
Is there any way i can keep the above code but say if its greater or less then current number +/- 4 ?
thus if the stored value is 500 , then the greater then would not come on untill 504 and the less then would not come on till 496
Sure. What I usually do when I need an output on over an encoder range is something like
| 12.34
|--[ >= ]---[ <= ]--------------------------O
| [ D100 ] [ D100 ]
| [ D201 ] [ D202 ]
This energizes 12.34 when D100 is between the range of values in D201 and D202. In this case you would have D100 as your encoder value, D201 would be 496 and D202 would be 504.
Above this rung you would need, of course, a rung (always on, presumably) which would take your setpoint value (500) in this case, and subtract 4, then store in D201; and add 4 and store in D202.

Why does ifelse convert a data.frame to a list: ifelse(TRUE, data.frame(1), 0)) != data.frame(1)?

I want to return a data.frame from a function if TRUE, else return NA using return(ifelse(condition, mydf, NA))
However, ifelse strips the column names from the data.frame.
Why are these results different?
> data.frame(1)
X1
1 1
> ifelse(TRUE, data.frame(1), NA)
[[1]]
[1] 1
Some additional insight from dput():
> dput(ifelse(TRUE, data.frame(1), 0))
list(1)
> dput(data.frame(1))
structure(list(X1 = 1), .Names = "X1", row.names = c(NA, -1L),
class = "data.frame")
ifelse is generally intended for vectorized comparisons, and has side-effects such as these: as it says in ?ifelse,
‘ifelse’ returns a value with the same shape as ‘test’ ...
so in this case (test is a vector of length 1) it tries to convert the data frame to a 'vector' (list in this case) of length 1 ...
return(if (condition) mydf else NA)
As a general design point I try to return objects of the same structure no matter what, so I might prefer
if (!condition) mydf[] <- NA
return(mydf)
As a general rule, I find that R users (especially coming from other programming languages) start by using if exclusively, take a while to discover ifelse, then overuse it for a while, discovering later that you really want to use if in logical contexts. A similar thing happens with & and &&.
See also:
section 3.2 of Patrick Burns's R Inferno ...
Why can't R's ifelse statements return vectors?