How to ask for a specific share of turtles with a minimum share of a value? - if-statement

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
]

Related

Percent Retention

I am trying to create a "Percent Retention" for policies during a given time period ( By month, YTD and year over year) . So all of the policies at a given time period compared to those active at the end of the period.
Policies can be:
N=New
RN=ReNew
C=Cancel
RI=ReInstate
NR=NonRenew
Transaction data kinda looks like this, the StatusNum is something I can derive to show inforce status.
PolicyID PolicyNum StatusDate Status StatusNum Net
1 123 1/1/2018 N 1 1
2 123 3/31/2018 C 0 -1
3 123 4/1/2018 RI 1 +1
4 123 6/1/2018 RN 1 0
5 222 2/1/2018 N 1 1
6 222 7/1/2018 RN 1 0
7 333 1/1/2018 N 1 1
8 333 6/1/2018 NR 0 -1
9 444 1/1/2018 N 1 1
10 444 5/30/2018 C 0 -1
My best guess on how to do this is to take the sum of the last StatusNum values at a PIT (partitioned by Policy Number) divided by the first StatusNum value at the beginning PIT. So if I filter by dates 1/1/2018 to 8/1/2018
123 will be in force (+1,+1)
222 will not be in force yet(so not counted for anything) (+0,+0)
333 was in force at the beginning, but it non renewed (+1,-1)
444 was in force at the beginning, but it cancelled (+1,-1)
So 3 of the policies were active at 1/1/2018 and 2 cancelled, 1 doesn't matter so the retention would be 33.3%
Can anyone offer feedback if this is the best way to do this and how to accomplish this?
Thank you in advance for your assistance.
Update
This is kinda what I am looking for, but it is too slow:
'AsOfPolicies =
var A= SELECTCOLUMNS(SUMMARIZECOLUMNS(Transactions[PolicyNumber], filter( Transactions,Transactions[DateKey]=min(Transactions[DateKey])&&Transactions[IsInForce]=-1) ),"aPolicyNumber", [PolicyNumber])
var B=SELECTCOLUMNS(SUMMARIZECOLUMNS(Transactions[PolicyNumber], filter( Transactions,Transactions[DateKey]<=MAX(Transactions[DateKey]) ),"MaxDate",MAX(Transactions[DateKey]) ),"bPolicyNumber",[PolicyNumber],"MaxDate",[MaxDate]) var C = SELECTCOLUMNS(filter(CROSSJOIN(A,B),[aPolicyNumber]=[bPolicyNumber]),"cPolicyNumber",[aPolicyNumber],"MaxDateKey",[MaxDate])
Var D = SELECTCOLUMNS(filter(CROSSJOIN(C,Transactions),[cPolicyNumber]=[PolicyNumber] && [MaxDateKey]=[DateKey]),"PolicyNumber",[PolicyNumber],"PD_ID",[PD_ID],"IsInForce",[IsInForce])
Return D'
Update
Also the filter does not appear to be working
I think you can do something like this:
Retention =
VAR StartDates =
SUMMARIZE (
ALLSELECTED ( PolicyLog ),
PolicyLog[PolicyNum],
"Start", MIN ( PolicyLog[StatusDate] )
)
VAR Included =
SELECTCOLUMNS (
FILTER ( StartDates, [Start] <= MIN ( Dates[Date] ) ),
"Policies", PolicyLog[PolicyNum]
)
VAR Filtered = FILTER ( PolicyLog, PolicyLog[PolicyNum] IN Included )
RETURN
DIVIDE (
SUMX ( Filtered, PolicyLog[Net] ),
COUNTROWS ( SUMMARIZE ( Filtered, PolicyLog[PolicyNum] ) )
)
First, you create a table, StartDates, that gives the earliest dates for each policy limited to the time frame you have selected. It would look something like this:
StartData =
PolicyNum Start
123 1/1/2018
222 2/1/2018
333 1/1/2018
444 1/1/2018
From there, we just want a list of which policies we want to include in the calculation. So we pick the ones that have a Start on the minimum selected date in the date slicer. We just want a list of the resulting policy numbers, so we just select that column.
Included =
Policies
123
333
444
From there we filter the whole PolicyLog table to just include these ones (Filtered).
Finally, we can add up the Net column for each of these selected policies and divide by the distinct count of them to get our retention percentage.
Edit: In response to your comment, I think you want to be a bit more selective with the StartDate variable. Instead of MIN( PolicyLog[StatusDate] ), try something more like this:
CALCULATE( MIN(PolicyLog[StatusDate]), PolicyLog[Status] IN {"N", "RN", "RI"} )

Netlogo: runtime-error with list, item, and -1

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

Netlogo missing inconsistent results when iterating through a list of ~ 6K lists

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

Netlogo random values for list bounded by values

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.

Netlogo turtles need to move same time at two different target location

Sorry, I am yet beginner with Netlogo programming i am trying to
schedule the jobs on machine based on their priority.
At the moment only product1 turtle is process by one machine turtle.
While other products turtles are not been run on the machines which
are idle e.g machine2 and machine3. Currently they are waiting for
the other loop to finish to process the next.
What i want to achieve is Product1 needs only Machine1 for operation.
while P2 needs M2 and M3, P3 needs M1,M2 and M3. So when Product1
turtles are on M1, other turtles should move to their corresponding
targets and schedule themselves
breed [product1 productA]
breed [product3 productB]
breed [product2 productC]
breed [machine1 machineA]
breed [machine2 machineB]
breed [machine3 machineC]
breed [schedulers scheduler]
globals [Priority]
product1-own
[
productID
productLength
arriveTime
startTime
finishTime
]
product2-own
[
productID
productLength
arriveTime
startTime
finishTime
waitTime
]
product3-own
[
productID
productLength
arriveTime
startTime
finishTime
]
schedulers-own
[
numJobArrive
numJobStart
numJobFinish
numJobWait
currentmachineID
]
machine-own
[
machineID
numJobStart
numJobFinish
waitTimeM
idleTimeM
nextAvailTime
currentproductID
utilization
]
to setup
clear-all
ask patches [set pcolor 8]
ask patch -3 6 [set pcolor 125]
ask patch -3 2 [set pcolor 125]
ask patch 1 4 [set pcolor black]
setup-products1
setup-products2
setup-products3
setup-machines
reset-ticks
end
to setup-machines
create-machine1 1 [setxy -3 6
set shape "computer server"]
create-machine2 1[setxy -3 2
set shape "computer server"]
create-machine3 1[setxy 1 4
set shape "computer server"]
end
to setup-products1
create-product1 Job1-products [
set shape "hexagonal prism"
set color red
set size 1.25
set xcor -14
set ycor 6
set heading 90
separate-products
]
end
to setup-products2
create-product2 Job2-products [
set shape "die 4"
set color blue
set xcor -14
set ycor 4
set size 1.25
set heading 90
separate-products
]
end
to setup-products3
create-product3 Job3-products [
set shape "box"
set color brown
set size 1.25
set xcor -14
set ycor 2
set heading 90
separate-products
]
end
; this procedure is needed so when we click "Setup" we
; don't end up with any two Products on the same patch
to separate-products ;; turtle procedure
if any? other turtles-here
[ fd 1.5
separate-products ]
end
to go-product3
let totalprod (turtle-set product1 product2 product3)
let targetmachine1 patch -3 6
let targetmachine2 patch -3 2
let targetmachine3 patch 1 4
if count product3 > 0
[
ask one-of product3 [
if not any? product1-on machine1 or not any? product2-on machine1
[
move-to targetmachine1
]
if not any? product1-on machine2 or not any? product2-on machine2
[
face targetmachine2
move-to targetmachine2
]
if not any? product1-on machine3 or not any? product2-on machine3
[
face targetmachine3
move-to targetmachine3
die
]
]]
tick
end
to go-product2
let targetmachine1 patch -3 2
let targetmachine2 patch 1 4
if count product2 > 0
[
ask one-of product2 [
if not any? product1-on machine2 or not any? product3-on machine2
[
face targetmachine1
move-to targetmachine1
]
if not any? product1-on machine3 or not any? product3-on machine3
[
face targetmachine2
move-to targetmachine2
die
]
]]
tick
end
to go-product1
let targetmachine patch 1 4
if count product1 > 0
[
ask one-of product1
[
if not any? product2-on machine3 or not any? product3-on machine3
[
face targetmachine
move-to targetmachine
die
]
]]
tick
end
to go
let totalprod (turtle-set product1 product2 product3)
while [count totalprod > 0] [
if Priority = "0" [
go-product1
go-product2
go-product3
]
if Priority = "1" [
while [count product1 > 0][
go-product1 ]
go-product2
go-product3
]
if Priority = "2" [
while [count product2 > 0][
go-product2 ]
go-product1
go-product3
]
if Priority = "3" [
while [count product3 > 0][
go-product3 ]
go-product1
go-product2
]
]
tick
end
Take the calls to tick out of your individual go-product procedures. It only ever makes sense to call tick once in your entire model, and that's at the end of your main go procedure.
Remove the uses of while from your go procedure. Time will pass in your model because go is called over and over again in a forever button -- not because you have loops inside your go procedure. The forever button is looping enough.
The goal is to structure your model so that each time through go (which is to say one tick), only one unit of work or action or motion takes place. Then, as go runs over and over again, then longer units of work, that take multiple ticks to accomplish, will appear to take place concurrently.
Nearly every model in Sample Models, including the Tutorial 3 model, is structured this way. You should only have a different structure if you're absolutely sure you know what you're doing and why you're doing it.