I've set up firms (turtles) in an industry (world) which either produce at home (firms-at-home located > ycor) or have offshored their production (offshore-firms located < ycor). I have given them a firms-own called offshored? which is answered with either true or false. At setup, they also get a firms-own firm-level-of-automation which is random between 0 and 1.
Now, I would like to have 30 % of them with the lowest firm-level-of-automation to move production to < 0 ycor and report: offshored? = true
breed [ firms firm ]
firms-own [
firm-level-of-automation ;; initially random between 0 and 1
offshored? ;; true or false
]
to go
tick
ask firms [
set firm-level-of-automation 0 + random-float 1
if min [ firm-level-of-automation ] of firms [
count firms * 0.3 firms [ setxy random-xcor random-between ( -10 ) -1 ;; distribute randomly abroad in an area < 0 ycor
set offshored? true ] ] ]
end
I don't know how to mix the if and min command best here. Any suggestions?
Not sure why you are being down voted for this, it seems like a sensible question. Anyway, there is no need to mix min and if, you can simply ask the 30% of turtles with the lower values to do what you want. Look up the min-n-of primitive in the dictionary, you will want something like:
ask min-n-of (0.3 * count firms) firms [firm-level-of-automation]
[ setxy random-xcor random-between ( -10 ) -1
set offshored? true
]
I have a rather specific error in netlogo that I've been staring at for a while now. Hope you guys have some insight.
The error is in a code which looks back in a list called 'strategy'. If the list is longer than investment-time variables 'REfocus' and 'PRICE' are set to a certain value. If the list is not longer than investment-time, the variables are not set (and thus remain 0).
The code consists out of a function strategy_actions and a reporter investment_time. Investment-time is approximately 3 years, but as ticks are in months, investment-time is rescaled to months. In strategy_actions, investment-time is scaled back to years, as each entry in the strategy list is also annual. (The scaling and rescaling seems arbitrary, but as investment-time is used a lot by other parts of the code, it made more sense to do it like this). The goal is to take the strategy from x time back (equal to investment-time).
The code (error follows underneath):
to strategy_actions
set_ROI
start_supply?
if current_strategy != 0
[
let it (investment_time / 12)
ifelse it >= length strategy
[
set REfocus 0
]
[
if item (it - 1) strategy = 1
[
if supply? = true [set_PRICE (set_discrete_distribution 0.29 0.19 0.29 0.15 0.07 0 0) (set_discrete_distribution 0.14 0.12 0.25 0.25 0.25 0 0)]
ifelse any? ids [set REfocus mean [mot_RE] of ids][set REfocus set_discrete_distribution 0.07 0.03 0.07 0.17 0.66 0 0]
]
if item (it - 1) strategy = 2
[
if supply? = true [set_PRICE (set_discrete_distribution 0.27 0.21 0.32 0.11 0.09 0 0) (set_discrete_distribution 0.15 0.11 0.22 0.30 0.23 0 0)]
ifelse any? prods [set REfocus mean [mot_RE] of prods][set REfocus set_discrete_distribution 0.12 0.03 0.10 0.18 0.57 0 0]
]
if item (it - 1) strategy = 3
[
if supply? = true [set_PRICE (set_discrete_distribution 0.26 0.22 0.26 0.18 0.09 0 0) (set_discrete_distribution 0.07 0.08 0.19 0.30 0.35 0 0)]
ifelse any? cons[set REfocus mean [mot_RE] of cons][set REfocus set_discrete_distribution 0.08 0.06 0.15 0.27 0.45 0 0]
]
]
set RE_history fput REfocus RE_history
]
end
to-report investment_time
report ((random-normal 3 1) * 12) ;approximately 3 years investment time
end
somehow, i sometimes get this runtime error during my behaviorspace experiment:
-1 isn't greater than or equal to zero.
error while observer running ITEM
called by procedure STRATEGY_ACTIONS
called by procedure SET_MEETING_ACTIONS
called by procedure GO
Does anyone know what causes this error?
You would help me out a lot!
Cheers,
Maria
It appears that investment_time is occasionally coming in as zero, so you are asking for item (0 - 1) of the strategy list. I did a bit of playing around with item and learned (to my surprise) that item (0.0001 - 1) strategy works just fine, yielding the 0th item in the list in spite of the argument being negative. But item (0 - 1) strategy does give the error you cite. Apparently an item number greater than -1 is interpreted as zero. Indeed item seems to truncate any fractional argument rather than rounding it. E.g., item 0.9 is interpreted as item 0, as is item -0.9
That might be worth putting in the documentation.
HTH,
Charles
I'm setting up patches from values read from a file. The values are in a list of lists:
[[10001 53 1 2 160 4 4 4 1] [10004 69 1 2 143 4 4 4 2] [10005 70 2 2 135 3 3 4 2] [10006 51 2 2 132 4 4 3 3] ... ]
Each "internal" list has the values for the properties of a patch.
The value in position 2 (I'm counting from 0 for the first value) represents gender:
1=male, 2=female.
My procedure:
to setup-patches-as-agents [ patches-data ]
set male-count 0
set female-count 0
set total-rows 0
foreach patches-data [
[ one-row ] ->
ask patches with [ pxcor = random-pxcor and pycor = random-pycor]
[ set total-rows total-rows + 1
;; add values to patch properties
;; **** id ****
set p-midus_id item 0 one-row
;; **** age *****
set p-age item 1 one-row
;; gender 1= MALE; 2= FEMALE; 7= DON'T KNOW; 8= REFUSE
set p-gender item 2 one-row
if item 2 one-row = 1 ;; male
[ set pcolor blue
set male-count male-count + 1 ] ;; male
if item 2 one-row = 2 ;; female
[ set pcolor orange
set female-count female-count + 1 ] ;; female
;; **** lose10lb ****
set p-lose10lb item 3 one-row
;; **** weightYrAgo ****
set p-weightYrAgo item 4 one-row
;; **** limitLiftCarryGroceries ****
set p-limitLiftCarryGroceries item 5 one-row
;; **** limitClimbingStairs ****
set p-limitClimbingStairs item 6 one-row
;; **** limitWalkingSeveralBlocks ****
set p-limitWalkingSeveralBlocks item 7 one-row
;; **** limitWalkingSeveralBlocks ****
set p-limitWalkingSeveralBlocks item 8 one-row
]
]
type "male-count: "
print male-count
type "female-count: "
print female-count
type "total-count: "
print total-rows
end
The total number of rows read is: 6325 (output after reading the file, which matches the number of rows in the file
Total number of Males should be: 3004, and for females: 3321. The count I get with those variables changes, including the total number of "rows"
Any idea why??
Glad you figured out a workaround! Just to give my two cents, I think your solution points to the likely issue with your first code- your
ask patches with [ pxcor = random-pxcor and pycor = random-pycor]
did not remove patches with assigned values from the pool of patches- that is to say you could have a patch be chosen multiple times, and so its initial values would be overwritten every time it was chosen. Since in your turtles solution you create a new turtle for each row, there is no overwriting happening.
If you need it to be patches instead of turtles, you can change your with statement to something that only selects patches that have not had values assigned yet. For example (assuming your setup does not previously set p-gender), you could take advantage of the fact that all declared variables are by default set to 0 until they are changed in code and change your original code to something like
...
foreach patches-data [
[ one-row ] ->
ask one-of patches with [ p-gender = 0 ] [
...
In the same pass you can just use one-of rather than manually choosing a random pxcor and pycor- it's simpler I think.
Additionally, and this is obviously up to you, instead of manually incrementing your counter for male and female, you might want to just set up a to-report procedure that monitors those for you. For example, if you have this in your code:
to-report male-turtle-count
report count turtles with [ gender = 1 ]
end
and then you print male-count (or use a monitor on the interface), it will show you the number of turtles that currently have a gender of 1. This dynamically updates so you don't have to keep track of a counter.
I switched to turtles and setup all properties, including initial coordinates and now I'm getting the right counts. I suspect that the condition for the coordinates to select a patch might be an issue, but I'm not sure. Anyway, here's my solution:
to setup-turtles [ turtles-data ]
foreach turtles-data [
[ one-row ] ->
crt 1 [
set shape "square"
setxy random-xcor random-ycor
set size 2
;;(item 0 one-row) and ycor = (item 1 one-row) ] [
;; **** id ****
set midus_id item 0 one-row
;; **** age *****
set age item 1 one-row
;; gender 1= MALE; 2= FEMALE; 7= DON'T KNOW; 8= REFUSE
set gender item 2 one-row
if item 2 one-row = 1 ;; male
[ set color blue ] ;; male
if item 2 one-row = 2 ;; female
[ set color orange ] ;; female
;; **** lose10lb ****
set lose10lb item 3 one-row
;; **** weightYrAgo ****
set weightYrAgo item 4 one-row
;; **** limitLiftCarryGroceries ****
set limitLiftCarryGroceries item 5 one-row
;; **** limitClimbingStairs ****
set limitClimbingStairs item 6 one-row
;; **** limitWalkingSeveralBlocks ****
set limitWalkingSeveralBlocks item 7 one-row
;; **** physicalFitnessCompared5YrAgo ****
set physicalFitnessCompared5YrAgo item 8 one-row
]
]
end
For my graduation project I am investigating the electricity consumption of occupants in officebuildings. Occupants when arriving at office they can leave their work desk doing other activities (e.g. lunch, meetings, restroom). Each occupant has his own time when going to office and when going home.
What I want is that occupants determine when they leave workdesk randomly distributed between going to office and going home.
to go
if time = 0 [start-day]
ask occupants [
let $currenttime hour-of-day
ifelse (go-to-office-today AND (workDays = "M-F" AND (Day >= 0) AND (Day
<= 4) AND ($currenttime * 100 >= workStartHour ) AND ($currenttime * 100
<= workEndHour - 100 )))
[if (not atWork?) [GoWork] ]
[if (atWork?) [GoHome] ]
if go-to-office-today and workschedule-next < length workschedule [
let workschedule-item item workschedule-next workschedule
]]
tick
to start-day
ask occupants [ set schedule-next 0
set go-to-office-today false
]
let $Occupancy-Percentage (50 + random 0)
ask n-of( int ($Occupancy-Percentage / 100 * count occupants)) occupants [
set go-to-office-today true
set workschedule [ ]
]
DetermineWorkSchedule
setMeetingSchedule
end
now i have only one fixed value for the list. But i want several normally distributed between workstarthour and workendhour (e.g between 10 and 90 ticks)
to go to restroom frequencie will be 4 times then i want the list to be workschedule [ 15 28 51 73 ]
to DetermineWorkSchedule
ask occupants [
let lunchtime [ ]
set lunchtime lput precision (random-normal 12 1 ) 0 lunchtime
set workschedule lput one-of lunchtime workschedule
let toilettime []
set toilettime lput precision (random-normal 15 2) 0 toilettime
set workschedule lput one-of toilettime workschedule
end
Since you now say you just want a n stochastic values between two endpoints without any distributional constraint, you could do this:
to-report transition-times [#n #start #end]
let _possible n-values (#end - #start + 1) [#start + ?]
report sort n-of #n _possible
end
This will give you values in [#start,#end] -- that is, including #start and #end. (Adjust to taste.) Values are constrained to be unique.
I have following data frame:
dataFrame <- data.frame(sent = c(1,1,2,2,3,3,3,4,5), word = c("good printer", "wireless easy", "just right size",
"size perfect weight", "worth price", "website great tablet",
"pan nice tablet", "great price", "product easy install"), val = c(1,2,3,4,5,6,7,8,9))
Data frame "dataFrame" looks like below:
sent word val
1 good printer 1
1 wireless easy 2
2 just right size 3
2 size perfect weight 4
3 worth price 5
3 website great tablet 6
3 pan nice tablet 7
4 great price 8
5 product easy install 9
And then I have words:
nouns <- c("printer", "wireless", "weight", "price", "tablet")
I need to extract only these words (nouns) from dataFrame and only these extracted add to new column (eg.extract) in dataFrame.
I really very appreciate any of your help od advice. Thanks a lot in forward.
Desired output:
sent word val extract
1 good printer 1 printer
1 wireless easy 2 wireless
2 just right size 3 size
2 size perfect weight 4 weight
3 worth price 5 price
3 website great tablet 6 table
3 pan nice tablet 7 tablet
4 great price 8 price
5 product easy install 9 remove this row (no match)
Here's a simple solution using the stringi package (size isn't in your nouns list btw).
library(stringi)
transform(dataFrame,
extract = stri_extract_all(word,
regex = paste(nouns, collapse = "|"),
simplify = TRUE))
# sent word val extract
# 1 1 good printer 1 printer
# 2 1 wireless easy 2 wireless
# 3 2 just right size 3 <NA>
# 4 2 size perfect weight 4 weight
# 5 3 worth price 5 price
# 6 3 website great tablet 6 tablet
# 7 3 pan nice tablet 7 tablet
# 8 4 great price 8 price
# 9 5 product easy install 9 <NA>
this is another solution. a bit more complicated but it also deletes the rows which have no matching between nouns and dataFrame$word
require(stringr)
dataFrame <- data.frame("sent" = c(1,1,2,2,3,3,3,4,5),
"word" = c("good printer", "wireless easy", "just right size",
"size perfect weight", "worth price", "website great tablet",
"pan nice tablet", "great price", "product easy install"),
val = c(1,2,3,4,5,6,7,8,9))
nouns <- c("printer", "wireless", "weight", "price", "tablet")
test <- character()
df.del <- list()
for (i in 1:nrow(dataFrame)) {
if(length(intersect(nouns, unlist(strsplit(as.character(dataFrame$word[i]), " ")))) == 0) {
df.del <- rbind(df.del, i)
} else {
test <- rbind(test,
intersect(nouns, unlist(strsplit(as.character(dataFrame$word[i]), " "))))
}
}
dataFrame <- dataFrame[-c(unlist(df.del)), ]
dataFrame <- cbind(dataFrame, test)
names(dataFrame)[4] <- "extract"
output:
sent word val extract
1 1 good printer 1 printer
2 1 wireless easy 2 wireless
4 2 size perfect weight 4 weight
5 3 worth price 5 price
6 3 website great tablet 6 tablet
7 3 pan nice tablet 7 tablet
8 4 great price 8 price
Here is another solution using loop function and if statement.
word<-dataFrame$word
dat<-NULL
extract<-c(rep(c("remove"), each=length(word)))
n<-length(word)
m<-length(nouns)
for (i in 1:n) {
g<-as.character(word[i])
for (j in 1:m) {
dat<-grepl(nouns[j], g)
if(dat == TRUE) {extract[i] <- nouns[j]}
}
}
dataFrame$extract <- extract
# sent word val extract
#1 1 good printer 1 printer
#2 1 wireless easy 2 wireless
#3 2 just right size 3 remove
#4 2 size perfect weight 4 weight
#5 3 worth price 5 price
#6 3 website great tablet 6 tablet
#7 3 pan nice tablet 7 tablet
#8 4 great price 8 price
#9 5 product easy install 9 remove