Calculating energy in Netlogo - netlogo

I am calculating energy consumption of 4 classrooms together.In Netlogo I am doing a classroom simulation where students sit randomly in a seat and the corresponding electrical appliance (e,g fan light) turn on. Thus when all the students are seated the total energy consumption is calculated. How can I calculate the total energy calculation? Using the below code I am not getting the correct value. The is the time the appliance is turning on(becoming green). How should I approach? Please help.
ask lights [ ifelse ( color = green ) [ set l-energy ( light-wattage * ( time - temp1 )) ] [ set l-energy 0 ] ]
ask fans [ ifelse ( color = green ) [ set f-energy ( fan-wattage * (time - temp1 )) ] [ set f-energy 0 ] ]
ask acs [ ifelse ( color = green ) [ set a-energy ( ac-wattage * ( time - temp1 )) ] [ set a-energy 0 ] ]

Related

Netlogo- how to perform a procedure every 30 ticks

how can i make a precise procedure every 30 ticks?I have written this procedure in "to go".
i tried to write like this:
'to go
if count population with [underPovertyLine] = count population or
count firms = 0 [stop]
ask population [
set min-indiv-cost-of-living runresult [max-indiv-cost-of-living]
]
if ticks mod 30 = 0 [
ask population with [employed = false and color = grey] [
set mymoneyOnBankOfGrey runresult [moneyOnBankOfGrey - min-indiv-
cost-of-living]
if mymoneyOnBankOfGrey < 0 [die]
]
ask population with [employed = false and color = blue ] [
set mymoneyOnBankOfBlue runresult[ moneyOnBankOfBlue - min-indiv-cost-
of-living]
if mymoneyOnBankOfBlue < 0 [die]
]
ask population with [employed = false and color = red] [
set mymoneyOnBankOfRed runresult [ moneyOnBankOfRed - min-indiv-cost-
of-living]
if mymoneyOnBankOfRed < 0 [die]
]
ask population with [employed = true and color = grey] [
set mymoneyOnBankOfGrey runresult[ moneyOnBankOfGrey + wage - min-
indiv-cost-of-living]
if mymoneyOnBankOfGrey < 0 [die]
]
ask population with [employed = true and color = blue] [
set mymoneyOnBankOfBlue runresult [ moneyOnBankOfBlue + wage - min-
indiv-cost-of-living]
if mymoneyOnBankOfBlue < 0 [die]
]
ask population with [employed = true and color = red ] [
set mymoneyOnBankOfRed runresult [ moneyOnBankOfRed + wage - min-indiv-
cost-of-living]
if mymoneyOnBankOfRed < 0 [die]
]
]
tick
end'
I would like the procedure to take place every 30 ticks but it happens only 1 time at the 30th tick and it stops and it does not update. how can i fix it?? thanks!!

How can I make the model run?

I am essentially trying to combine elements of the 'Segregation' model and 'Rebellion' model to form a model that is representative of alliance forming.
Here is what I have so far- when I attempt to run it I receive the error: THREATS breed does not own variable ACTIVE?
error while threat 0 running ACTIVE?
called by procedure GO
called by Button 'go'
breed [ agents an-agent]
breed [ threats threat ]
globals [
k ; factor for determining attack probability
threshold ; by how much must D - BS > A to make a state burden share
percent-similar ; on the average, what percent of a turtle's neighbors
; are the same color as that turtle? Likely to ally
percent-unhappy ; what percent of the turtles are unhappy? Or percieve threats?
visualization
]
agents-own [
allied-states ; R, fixed for the agent's lifetime, ranging from 0-1 (inclusive)- for each turtle, indicates whether at least %-similar-wanted percent of
; that turtle's neighbors are the same color as the turtle
perceived-threat ; T, also ranging from 0-1 (inclusive)- how many have a turtle of another color?
active? ; if true, then the agent is actively allied
; if false, then the agent is free-riding
conflict ; how many turns in conflict remain? (if 0, the agent is not in conflict)- sum of previous two variables
total-nearby ; sum of previous two variables
]
patches-own [
neighborhood ; surrounding patches within the vision radius
]
to setup
clear-all
; set globals
set k 2.3
set threshold 0.1
ask patches [
; make background a slightly dark gray
set pcolor gray - 1
; cache patch neighborhoods
set neighborhood patches in-radius vision
]
if initial-threats-density + initial-agent-density > 206 [
user-message (word
"The sum of INITIAL-THREATS-DENSITY and INITIAL-AGENT-DENSITY "
"should not be greater than 206.")
stop
]
; create threats
create-threats round (initial-threats-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
display-threats
]
; create agents
create-agents round (initial-agent-density * .01 * count patches) [
move-to one-of patches with [ not any? turtles-here ]
set heading 0
set allied-states random-float 1.0
set perceived-threat random-float 1.0
set active? false
set conflict 0
display-agent
]
; start clock and plot initial state of system
reset-ticks
end
to go
if all? turtles [ active? ] [ stop ]
move-unhappy-turtles
update-turtles
update-globals
tick
end
; unhappy turtles try a new spot
to move-unhappy-turtles
ask turtles with [ not active? ]
[ find-new-spot ]
end
; move until we find an unoccupied spot
to find-new-spot
rt random-float 360
fd random-float 10
if any? other turtles-here [ find-new-spot ] ; keep going until we find an unoccupied patch
move-to patch-here ; move to center of patch
end
to update-turtles
ask turtles [
; in next two lines, we use "neighbors" to test the eight patches
; surrounding the current patch
set allied-states count (turtles-on neighbors) with [ color = [ color ] of myself ]
set perceived-threat count (turtles-on neighbors) with [ color != [ color ] of myself ]
set total-nearby allied-states + perceived-threat
set active? allied-states >= (percent-similar * total-nearby / 100)
; add visualization here
if visualization = "old" [ set shape "default" set size 1.3 ]
if visualization = "square-x" [
ifelse active? [ set shape "square" ] [ set shape "X" ]
]
]
end
to update-globals
let similar-neighbors sum [ allied-states ] of turtles
let total-neighbors sum [ total-nearby ] of turtles
set percent-similar (similar-neighbors / total-neighbors) * 100
set percent-unhappy (count turtles with [ not active? ]) / (count turtles) * 100
; Agents engaged in conflict have the duration reduced at the end of each clock tick
ask agents [ if conflict > 0 [ set conflict conflict - 1 ] ]
; update agent display
ask agents [ display-agent ]
ask threats [ display-threats ]
; advance clock and update plots
tick
end
; AGENT AND THREAT BEHAVIOR
; move to an empty patch
to move ; turtle procedure
if movement? or breed = threats [
; move to a patch in vision; candidate patches are
; empty or contain only jailed agents
let targets neighborhood with [
not any? threats-here and all? agents-here [ conflict > 0 ]
]
if any? targets [ move-to one-of targets ]
]
end
; AGENT BEHAVIOR
to determine-behavior
set active? (burden-sharing - allied-states * estimated-conflict-probability > threshold)
end
to-report burden-sharing
report perceived-threat * (1 - alliance-protection)
end
to-report estimated-conflict-probability
let t count (threats-on neighborhood)
let a 1 + count (agents-on neighborhood) with [ active? ]
; See Info tab for a discussion of the following formula
report 1 - exp (- k * floor (t / a))
end
to alliance
if any? threats [attack]
set active? true
end
; THREAT BEHAVIOR
to attack
if any? (agents-on neighborhood) with [ active? ] [
; arrest suspect
let suspect one-of (agents-on neighborhood) with [ active? ]
move-to suspect ; move to patch of the jailed agent
ask suspect [
set active? false
set conflict random conflict-term
set color pink
]
]
end
; VISUALIZATION OF AGENTS AND COPS
to display-agent ; agent procedure
set color cyan
set shape "triangle"
end
to display-active?
set color pink
set shape "triangle"
end
to display-threats
set color red
set shape "circle 2"
end
The problem is that you have two breeds of turtles, agents and threats, and only agents "own" the variable active?. turtles is a superset of all breeds, so when the all? primitive tries to query the active? variable of literally all turtles, it tries to query active? for threats too, and can't find it. The line should be
if all? agents [ active? ] [ stop ]

How to divide turtles in groups by percent?

In my world, turtles are firms. 30 % of all firms have a low output (Y = 1), 60 % a medium (Y = 2) and 10 % a high production output (Y = 3).
How can I assign an input for Y to 30 % of my turtles/firms? Best would be to have a slider in order to change the values if needed.
I have given the turtles a firm-own variable called Y which is their output. I have also created number-of-firms slider where I can decide how many firms will be in the world for each setup.
breed [ firm firms ]
firm-own [
Y ;; output
]
to setup
clear-all
setup-industry
reset-ticks
end
to setup-industry
create-firm number-of-firms [ ;; number of firms to be defined through slider
ask n-of ( count firms * 0.3 ) firms [
set Y 1 ]
ask n-of ( count firms * 0.6 ) firms [
set Y 2 ]
ask n-of ( count firms * 0.1 ) firms [
set Y 3 ]
]
end
The error message says that when I have "ask n-of ( count firms ... )" firms are expected to have 1 input, meaning a number.
The first input in breed defines the agentset whereas the second input goes for a single member. Consider following modifications:
breed [ firms firm ]
firms-own [
Y ;; output
]
...
create-firms number-of-firms [ ;; number of firms to be defined through slider
Now you won't have any error messages.

Netlogo: "this code can't be run by a patch" when trying to call pcolor?

I'm currently working through the telemarketer IBM in Grimm & Railsback's book. I'm sure it's something really obvious, but I can't figure out why I get the error:
this code can't be run by a patch
error while patch -38 75 running IF
called by procedure MAKE-CALLS
called by procedure GO
called by Button 'step'
This is the problematic code (specifically, "if pcolor = black").
to make-calls
ask turtles [
let territory ( 10 * sqrt size )
let max-calls floor ( 100 * size )
let potential-customers patches in-radius territory
set successful-sales 0
ifelse count potential-customers <= max-calls
[
ask potential-customers[ ;call all customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
[
ask n-of max-calls potential-customers[ ;call max-calls customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
set total-sales total-sales + successful-sales
]
end
I want to check whether the patches within the 'territory' of the turtles (the 'potential customers') are coloured black, but the turtles (telemarketers) can only make a certain number of calls. So if the number of patches in their territory exceeds max-calls, I check the colour of a number of patches within the territory equal to max-calls.
Any help would be appreciated :-)
FULL CODE:
globals[
sim-length
money-size-ratio
total-sales
]
patches-own[
;potential customers coloured black, unavailable customers coloured red
]
turtles-own[
;telemarketers
funds
successful-sales
]
to setup
ca
set sim-length 200
set money-size-ratio 0.001
set total-sales 0
crt initial-num-marketers [
set size 1.0
set funds 0.0
set successful-sales 0
setxy random-xcor random-ycor
set shape "circle"
]
ask patches [ set pcolor black ]
end
to go
reset-phones
make-calls
do-accounting
update-observer
tick
if ticks = sim-length [stop]
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
to reset-phones
ask patches [ set pcolor black ]
end
to make-calls
ask turtles [
let territory ( 10 * sqrt size )
let max-calls floor ( 100 * size )
let potential-customers patches in-radius territory
set successful-sales 0
ifelse count potential-customers <= max-calls
[
ask potential-customers[ ;call all customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
[
ask n-of max-calls potential-customers[ ;call max-calls customers
if pcolor = black[
set pcolor red
set successful-sales successful-sales + 1
]]
]
set total-sales total-sales + successful-sales
]
end
to do-accounting
ask turtles [
let costs ( size * 50 )
let income successful-sales * 2
set funds funds + income - costs
if funds > growth-param
[
let growth floor ( funds - growth-param )
set size size + ( size * growth * money-size-ratio )
]
if funds < 0 [ die ]
]
end
to update-observer
set-current-plot "number of businesses"
plot count turtles
set-current-plot "business size distribution"
histogram [size] of turtles
set-current-plot "total sales"
plot total-sales
end
The problem is successful-sales: it is a turtle attribute, but you are asking patches to set it. Change it everywhere to _sales, then change set _sales 0 to let _sales 0. This introduces a new local variable. Now your code should work. However, you are no longer using the successful-sales attribute of turtles. Get rid of it. If you cannot get rid of it for some reason, you can set it to _sales right before you update total-sales.

Update Birth and death rates in an SIR model

i would like to simulate the disease spread using agent base model by using NetLogo software.
how to Update Birth and death rates in an SIR model.
As a starting point you could try the http://ccl.northwestern.edu/netlogo/models/SimpleBirthRates
that has a method reproduce which adjusts the fertility of the different populations
to reproduce
ask turtles
[
ifelse color = red
[
set fertility floor red-fertility
set fertility-remainder red-fertility - (floor red-fertility)
]
[
set fertility floor blue-fertility
set fertility-remainder blue-fertility - (floor blue-fertility)
]
ifelse (random-float 100) < (100 * fertility-remainder)
[ hatch fertility + 1 [ wander ]]
[ hatch fertility [ wander ]]
]
end
For the SIR model see https://en.wikipedia.org/wiki/SIR_model#The_SIR_model
You probably want three different populations blue(S susceptible), red(I infectious), and green(R recovered). You want to change the code so that you change beta I S blues into reds and change nu I reds into greens. beta and nu are model parameters. It might be easier to start by killing and hatching equal numbers of blues and reds.
The following code implements this. I have three different sets of turtles, initially many more blue than the other colours. The main part happens in the reproduce which turns blues to reds:
ifelse color = blue
[
if (random-float 100) < (beta * red-count * blue-count ) / 1000000
[set color red]
]
and reds to greens
ifelse color = red
[
if (random-float 100) < (nu * red-count ) / 10000
[set color green]
]
The full code is. You need to add sliders for beta and nu, add a line in the graph for green-count and a monitor for green-count. The numbers have been chosen by guess work which seem to show a good effect.
globals
[
red-count ; population of red turtles
blue-count ; population of blue turtles
green-count ; population of green turtles
]
turtles-own
[
]
to setup
clear-output
setup-experiment
end
to setup-experiment
cp ct
clear-all-plots
reset-ticks
crt carrying-capacity
[
setxy random-xcor random-ycor ; randomize turtle locations
ifelse who < (carrying-capacity / 10) ; start out with equal numbers of reds and blues
[ set color red ]
[
ifelse who < (2 * carrying-capacity / 10) ; start out with equal numbers of reds and blues
[ set color green ]
[ set color blue ]
]
set size 2 ; easier to see
]
reset-ticks
end
to go
reproduce
tick
end
to reproduce
ask turtles
[
ifelse color = blue
[
if (random-float 100) < (beta * red-count * blue-count ) / 1000000
[set color red]
]
[
ifelse color = red
[
if (random-float 100) < (nu * red-count ) / 10000
[set color green]
]
[
]
]
]
end