I have a problem with
[histogram]
In particular, I have a global variable (prova) that records the age of the turtles when they get sick.
In the beginning of the code I define prova as follows
set prova (list)
Then in the go procedure turtles can get sick, if they do then I want to record their age
ask turtles [
if ... [
set prova lput y prova ]]
When I try to plot this variable
[histogram] prova
I get the following error "expected a constant"
I don't understand why...since "prova" is a list I thought it worked!
#JenB It should be a list...
Related
I'm new to netlogo and I'm trying to build a network where nodes have a random number that changes with each tick, and connect randomly to other nodes. Once the connection is established I need that: if the number between the two nodes is the same then the link crystallizes and changes color, otherwise it is deleted for that tick, the procedure is repeated until all nodes have the same random value.
Thanks in advance
Following the clarification in the comments...
The thing you mentioned can be done in a fairly simple way, so hopefully you can use the explanation and keywords below to browse the NetLogo Manual (and in particular the NetLogo Dictionary) and become fully familiar with what is going on.
First, let's setup a few things
globals [
; You'll probably want to declare these two variables directly
; in the Interface with a slider or something, but here I'll
; set them up from within the Code section.
number-of-turtles
level-of-randomness
]
turtles-own [
my-number
connected?
]
to setup
clear-all
reset-ticks
set-globals
create-turtles number-of-turtles [
setxy random-xcor random-ycor
set connected? false
]
end
to set-globals
set number-of-turtles 500
set level-of-randomness 1000
end
So far you have a number of turtles scattered in their environment, a couple of global variables that everyone can access, and a couple of variables belonging to each turtle to keep track of their state.
Based word for word on what you described, you could go on as:
to go.v1
; This first block of commands below is to get rid of the links that
; emerged in the previous iteration of 'go.v1', but that
; you don't want to keep because they link turtles with
; different 'my-number'. You will also need to include the
; stop condition that best suits you.
ask links with [color != green] [
die
]
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
; The 'let' command creates a local variable.
ask turtles [
let target one-of other turtles
create-link-with target
; The statement below containing "[who] ..." is how you need to
; call a link: 'who' is a built-in turtle-own variable reporting
; the ID number of the turtle, and to report a link you will need
; to give it the two IDs of the two turtles being connected.
if (my-number = [my-number] of target) [
ask link ([who] of self) ([who] of target) [
set color green
]
if (connected? = false) [
set connected? true
]
if ([connected?] of target = false) [
ask target [
set connected? true
]
]
]
]
tick
end
The code above does what you said: each turtle always creates the link with another turtle, then the condition is tested, and based on that condition (the result of which is stored as the link's color: only the good links become green) the link is kept or eliminated at the beginning of the following iteration of go.v1.
However, although you may have reasons to do it in the way above, you might just be happy with an alternative which requires less computation:
to go.v2
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
ask turtles [
let target one-of other turtles
if (my-number = [my-number] of target) [
create-link-with target [
set color green
ask both-ends [
if (connected? = false) [
set connected? true
]
]
]
]
]
tick
end
This way, turtles evaluate the potential companion before creating any link, and they only proceed to create it if my-number is the same.
That way, there is no need to create and then eliminate all the unwanted links (and the my-number condition had to be tested anyway even in go.v1).
I need some help. I have some turtles linked by links. Turtles have both a variable "x" and a variable "y". The latter can be true or false.
I would like to do the following: each turtle has to "inspect" the "y" variable of all the neighbors linked to it, one at a time. Then if "y" is true the (inspecting) turtle updates its "x" = x + 1, if "y" is false the (inspecting) turtle updates its "x" = x - 1.
Summaring, each turtles updates it "x" during each meeting. Thererore if a turtle has 3 links with "y" true, it "x" has to be x + 3.
Thanks
So the value of X is given by the number of linked turtles with true y - number of linked turtles with false y? If so, you want something like:
ask turtles
[ set X count link-neighbors with [Y?] - count link-neighbors with [not Y?] ]
Note that standard practice in NetLogo is to have a question mark at the end of the variable name for boolean variables and I have named the y variable accordingly.
I have an SPSS variable containing lines like:
|2|3|4|5|6|7|8|10|11|12|13|14|15|16|18|20|21|22|23|24|25|26|27|28|29|
Every line starts with pipe, and ends with one. I need to refactor it into boolean variables as the following:
var var1 var2 var3 var4 var5
|2|4|5| 0 1 0 1 1
I have tried to do it with a loop like:
loop # = 1 to 72.
compute var# = SUBSTR(var,2#,1).
end loop.
exe.
My code won't work with 2 or more digits long numbers and also it won't place the values into their respective variables, so I've tried nest the char.substr(var,char.rindex(var,'|') + 1) into another loop with no luck because it still won't allow me to recognize the variable number.
How can I do it?
This looks like a nice job for the DO REPEAT command. However the type conversion is somewhat tricky:
DO REPEAT var#i=var1 TO var72
/i=1 TO 72.
COMPUTE var#i = CHAR.INDEX(var,CONCAT("|",LTRIM(STRING(i,F2.0)),"|"))>0).
END REPEAT.
Explanation: Let's go from the inside to the outside:
STRING(value,F2.0) converts the numeric values into a string of two digits (with a leading white space where the number consist of just one digit), e.g. 2 -> " 2".
LTRIM() removes the leading whitespaces, e.g. " 2" -> "2".
CONCAT() concatenates strings. In the above code it adds the "|" before and after the number, e.g. "2" -> "|2|"
CHAR.INDEX(stringvar,searchstring) returns the position at which the searchstring was found. It returns 0 if the searchstring wasn't found.
CHAR.INDEX(stringvar,searchstring)>0 returns a boolean value indicating if the searchstring was found or not.
It's easier to do the manipulations in Python than native SPSS syntax.
You can use SPSSINC TRANS extension for this purpose.
/* Example data*/.
data list free / TextStr (a99).
begin data.
"|2|3|4|5|6|7|8|10|11|12|13|14|15|16|18|20|21|22|23|24|25|26|27|28|29|"
end data.
/* defining function to achieve task */.
begin program.
def runTask(x):
numbers=map(int,filter(None,[i.strip() for i in x.lstrip('|').split("|")]))
answer=[1 if i in numbers else 0 for i in xrange(1,max(numbers)+1)]
return answer
end program.
/* Run job*/.
spssinc trans result = V1 to V30 type=0 /formula "runTask(TextStr)".
exe.
I'm trying to iterate over 2 parameters to get two splines for each pair. The code:
y_arr:[0.2487,0.40323333333333,0.55776666666667,0.7123]$
str_h_arr:[-0.8,-1.0,-1.2,-1.4]$
z_points:[0,0.1225,0.245,0.3675,0.49,0.6125,0.735,0.8575,0.98,1.1025,1.225,1.3475,1.47,
1.5925,1.715,1.8375,1.96,2.0825,2.205,2.26625,2.3275,2.3765,2.401,2.4255,2.43775,
2.4451,2.448775,2.45]$
length(a)$
length(b)$
load(interpol)$
for y_k:1 thru length(a) do (
for h_k:1 thru length(b) do (
y:y_arr[y_k],
str_h:str_h_arr[h_k],
bot_startpoints: [[-2.45,0],[0,y],[2.45,0]],
top_startpoints: [[-2.45,str_h_min],[0,y+str_h],[2.45,str_h_min]],
spline: cspline(bot_startpoints),
bot(x):=''spline,
print(bot(0))
)
);
//Part with top spline is skipped.
For all iterations output is now the same: 0.7123
What I want to get is two splines like in picture
Members of y_arr are y values in x=0, str_h_arr: height between splines in x=0.
So bot(0) should give me all values from y_arr.
If i don't use loop and just give this block values of y_k and h_k, it's working properly.
Can anybody point me to where I'm (or Maxima is) wrong with using loop with cspline?
The problem is that quote-quote (two single quotes, '') is applied only once, when it is read in input; it is not applied every time the expression in evaluated in the loop.
Looks like you need only to evaluate the spline at x = 0 and nothing else. So I'll suggest ev(spline, x=0) to evaluate it. You can also construct a lambda expression and evaluate that.
Here is the program after I've revised it as described above. Also, it is simpler and clearer to write for y in y_arr do (...) rather than making use of an explicit index for y_arr.
y_arr:[0.2487,0.40323333333333,0.55776666666667,0.7123]$
str_h_arr:[-0.8,-1.0,-1.2,-1.4]$
z_points:[0,0.1225,0.245,0.3675,0.49,0.6125,0.735,0.8575,0.98,1.1025,1.225,1.3475,1.47,
1.5925,1.715,1.8375,1.96,2.0825,2.205,2.26625,2.3275,2.3765,2.401,2.4255,2.43775,
2.4451,2.448775,2.45]$
load(interpol)$
for y in y_arr do (
for str_h in str_h_arr do (
bot_startpoints: [[-2.45,0],[0,y],[2.45,0]],
top_startpoints: [[-2.45,str_h_min],[0,y+str_h],[2.45,str_h_min]],
spline: cspline(bot_startpoints),
print (ev (spline, x=0))));
This is the output I get:
0.2487
0.2487
0.2487
0.2487
0.40323333333333
0.40323333333333
0.40323333333333
0.40323333333333
0.55776666666667
0.55776666666667
0.55776666666667
0.55776666666667
0.7123
0.7123
0.7123
0.7123
I'm coding a Google sketchup plugin with Ruby, and I faced a little problem. I have an array containing descriptions of every point like:
desc_array = ["anna ", "anna 45", "anna689", "anna36", "anna 888", "anna ",...]
The array containing every point's coordinates is:
todraw_su = [
[-16.23317, -16.530533, 99.276929],
[-25.142825, -12.476601, 99.237414],
[-32.716122, -5.92341, 99.187951],
[-38.964589, 4.181119, 99.182358],
[-41.351064, 18.350418, 99.453714],
[-40.797511, 33.987519, 99.697253],
...
]
I want to add a text in Google sketchup for each point. According to the Sketchup API this can be done by:
Sketchup.active_model.entities.add_text "This is the text", [x, y, z]
I tried:
todraw_su.each {|todraw| desc_array.each {|desc| Sketchup.active_model.entities.add_text desc,todraw }}
But, it gave me something unexpected, as it returns all the elements in desc_array for every element in to_draw.
What I want is every element in desc_array for every element in to_draw.
[desc_array, todraw_su].transpose.each do |desc, coord|
# ...
end
You can also do this with #zip like...
desc_array.zip(todraw_su).each do |desc, coord|
# ...
end
With the #zip technique, the result will always have the first array's dimension, with the second truncated or nil-padded as needed. This may or may not be TRT. Transpose will raise IndexError in that case.