I am simulating a classroom to find the total energy consumption from appliances of a classroom. Now I want to run the simulation in BehaviorSpace so that I get the energy consumption (energy-calculation) by varying the number of students in the classroom.
globals[
temp1 simulation-timer number-of-seats number-of-lights
number-of-fans number-of-acs gap row col x-cor y-cor half half2
student-no t-light t-fan t-ac t-energy
]
breed [seats seat]
breed [seat-teachers seat-teacher]
breed [lights light]
breed [fans fan]
breed [acs ac ]
breed [students student ]
seats-own [
seat-color
occupied?
]
seat-teachers-own [
seat-color
]
students-own [
entry-time
found-seat
]
lights-own [
l-energy
]
fans-own [
f-energy
]
acs-own [
a-energy
]
to setup
clear-all
ask patches [ set pcolor 9 ]
set gap floor ((max-pxcor) / (no-of-row-or-col) )
set half ceiling (gap / 2)
set half2 floor (gap / 2)
place-seat-teachers
place-seats-students
place-lights
place-fans
place-acs
ask patches with [ pxcor = 3 * gap + half2 ] [ set pcolor 4 ]
ask patches with [ pxcor = 6 * gap + half2 ] [ set pcolor 4 ]
create-students-classroom
reset-ticks
reset-timer
end
to place-seat-teachers
create-seat-teachers 1 [
setxy ((max-pxcor - min-pxcor) / 2) 1
set shape "square"
set size 3
set color red
]
end
to place-seats-students
set row gap
set col gap
set x-cor 0
set y-cor 0
while [ x-cor <= gap * no-of-row ]
[
ifelse (x-cor = row)[
set col gap
set y-cor 0
while [ y-cor <= gap * no-of-row-or-col ]
[
ifelse (y-cor = col)[
create-seats 1 [
set shape "square"
set size 1.5
set color blue
setxy col row
set label who
set number-of-seats number-of-seats + 1
show (word row ", " col )
]
set col col + gap
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + gap
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-lights
set row gap + half2
set col gap + half
set x-cor 0
set y-cor 0
while [ x-cor <= gap * no-of-row-or-col ]
[
ifelse (x-cor = row)[
set col gap + half
set y-cor 0
while [ y-cor <= gap * no-of-row ]
[
ifelse (y-cor = col)[
create-lights 1 [
set shape "pentagon"
set size 1
set color red
setxy row col
set number-of-lights number-of-lights + 1
show (word row "," col )
]
set col col + ( gap * 2)
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + ( gap * 2)
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-fans
set row ( gap * 2 ) + half2
set col gap + half
set x-cor 0
set y-cor 0
while [ x-cor <= ( gap * no-of-row-or-col ) ]
[
ifelse (x-cor = row)[
set col gap + half
set y-cor 0
while [ y-cor <= ( gap * no-of-row ) ]
[
ifelse (y-cor = col)[
create-fans 1 [
set shape "x" ;; x shape
set size 1
set color red
setxy row col
set number-of-fans number-of-fans + 1
show (word row "," col )
]
set col col + ( gap * 2)
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
set row row + ( gap * 2)
set x-cor x-cor + 1
]
[set x-cor x-cor + 1]
]
end
to place-acs
set row 3
set col 13
set x-cor 0
set y-cor 0
while [ y-cor <= 45 ]
[
ifelse (y-cor = col)[
create-acs 1 [
set shape "star" ;; star shape
set size 1
set color red
setxy row col
set number-of-acs number-of-acs + 1
show (word row "," col )
]
set col col + 10
set y-cor y-cor + 1
]
[set y-cor y-cor + 1]
]
end
to go
set simulation-timer 0
output-show (word "timer = "simulation-timer )
tick
move-students
while [simulation-timer < time ] [
set simulation-timer simulation-timer + 1
output-show (word "timer = "simulation-timer )
]
end
to create-students-classroom
create-students number-of-students [
set entry-time random threshold + 1
let stu-no sort-on [who] students
foreach stu-no [x -> ask x [ show (word x " -> " entry-time ) ]
]
set shape "person"
set color 3
]
end
to move-students
let s sort [who] of seats
let a first s
let l length s
while [ l > (number-of-seats - number-of-students )] [
set temp1 simulation-timer
tick
tick
ask students [ if ( entry-time = temp1 )
[
move-to seat a ; If it does the student moves to a seat
set color red
appliance-on
energy-calculation
show (word temp1 "," l "," a)
set s remove a s
set a a + 1
set l length s
]
]
set simulation-timer simulation-timer + 1
output-show (word "timer = "simulation-timer )
]
end
to appliance-on
ask students [ ask lights in-radius 4
[ set color green ]]
ask students [ ask fans in-radius 4
[ set color green ]]
ask students [ ask acs in-radius 9
[ set color green ]]
stop
end
to energy-calculation
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 ] ]
let light-e sum [l-energy] of lights
let fan-e sum [f-energy] of fans
let ac-e sum [a-energy] of acs
set t-light ( light-e / 60 )
set t-fan (fan-e / 60 )
set t-ac (ac-e / 60 )
show (word "total-ac-time = " t-ac )
set t-energy ( t-light + t-fan + t-ac )
end
In the BehaviorSpace: measure runs using these reporters I am putting energy-calculation but in the spreadsheet everything is showing zero. Why is this happening? When I am seeing the energy-calculation in a monitor it shows a value. What I want to do is run this code several times with different student numbers and get the varied energy-calculation each time. Or should I use file save in .csv for this situation?
You have written energy-calculation as a command that sets the global variable t-energy rather than as a reported that reports t-energy to the caller. I suspect that in your monitor you have used the t-energy variable which is reset each time energy-calculation is called. But in BehaviorSpace, the reporter by which runs are measured must actually be a reporter. It must return a value to BehaviorSpace. This is easily done by rewriting energy-calculation as
to-report energy-calculation
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 ] ]
let light-e sum [l-energy] of lights
let fan-e sum [f-energy] of fans
let ac-e sum [a-energy] of acs
set t-light ( light-e / 60 )
set t-fan (fan-e / 60 )
set t-ac (ac-e / 60 )
show (word "total-ac-time = " t-ac )
set t-energy ( t-light + t-fan + t-ac )
report t-energy
end
Whether or not you still need the global t-energy or can simply replace it with the reporter that reports its value, would depend on how else t-energy is used in the code.
But, I am actually surprised that the BehaviorSpace experiment tries to run at all. BehaviorSpace should catch that energy-calculation is not a reporter and throw an error.
Related
I will be very grateful if someone could give me any kind of advice about a problem that I have in my code. I am working on a model with four kind of agents. Each type of agents uses a different strategy. These strategies are Prisoner Dilemma variations. Two of these strategies (tit for tat and unforgiving) need to record (in a list) who was the partner of interaction, and depending if that partner defected or not, will be the behave (cooperative or defect) of the agent in the next encounter with the same partner. Agents move randomly through the world. The problem come when agents die or birth according of their resources level. At this point these strategies that require to record who was the previous partner and how was his behave (cooperative or defect) do not work. This is the message that show up.
I am really appreciate any support!!!!.
number of initial turtles is 42, but two ticks later the number is 52
Below is a code that reproduces the error. In order to run it, it is necessary to create the sliders: n-tit-for-tat-com; n-unforgiving-com; inc-ecological-resources; energy-consume-companies:
globals
[
;;Number of companies with each strategy¨
num-tit-for-tat-com
num-unforgiving-com
;;number of interactions by each strategy
num-tit-for-tat-com-games
num-unforgiving-com-games
;; Total score of all companies playing each strategy
tit-for-tat-com-score
unforgiving-com-score
; Total companies en each ticks
num-companies
]
breed [companies company]
;;create companies variables
companies-own [
score-com
score-com-round
strategy-com
defect-now?-com
partner-defected?-com ;;action of the partner
partnered?-com ;;am I partnered?
partner-com ;;WHO of my partner (nobody if not partnered)
partner-history-com ;;a list containing information about past interactions with other turtles (indexed by WHO values)
earnings-tit-for-tat-Com
earnings-unforgiving-Com
earnings-com
tit-for-tat-com-score-round
unforgiving-com-score-round
]
patches-own
[
resources-amount ;; resources amount in each patches
resources-to ;; aux variable to count resources taken
]
;;;Setup Procedures;;;
to setup
clear-all
setup-patches
setup-companies ;;setup the turtles and distribute them randomly
reset-ticks
end
; Setup patches
;===============
to setup-patches
ask patches [set resources-amount random 4] ;; patches represent ecological system. Resources are distributed randomly. [0,1,2 or 3] 0 -> non resources; 3 -> plenty of resources
ask patches [
set pcolor (ifelse-value
resources-amount = 0 [49]
resources-amount = 1 [58]
resources-amount = 2 [67]
resources-amount = 3 [65]
[63] ;; this case never happen. Because 4 means integer number in range [0 to 3]
)]
end
to setup-companies
make-companies ;;create the appropriate number of turtles playing each strategy
setup-common-variables-com ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-companies
create-companies n-tit-for-tat-com
[ set strategy-com "tit-for-tat-com"
set color 135
set earnings-tit-for-tat-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
create-companies n-unforgiving-com
[ set strategy-com "unforgiving-com"
set color 4
set earnings-unforgiving-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
end
; Setup common variables
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;set the variables that all turtles share
to setup-common-variables-com
ask companies [
set shape "pentagon"
set size 1.3
set tit-for-tat-com-score-round 0
set unforgiving-com-score-round 0
set score-com 0
set partnered?-com false
set partner-com nobody
setxy random-xcor random-ycor
]
end
;;;Runtime Procedures;;;
to go
ask companies [birth-die-com]
store-initial-companies-counts ;;record the number of turtles for each strategy
setup-history-lists-com
clear-last-round
ask companies [ partner-up-com ] ;;have turtles try to find a partner
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ select-strategy-com ] ;;all partnered turtles select action
ask partnered-com [ play-a-round-com ]
adjust-patches
tick
end
;Birth-die-com ; According to the level of resources companies can die or birth
;======================
to birth-die-com
(ifelse
strategy-com = "tit-for-tat-com"
[
set earnings-com earnings-tit-for-tat-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-tit-for-tat-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
strategy-com = "unforgiving-com"
[
set earnings-com earnings-unforgiving-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-unforgiving-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
)
end
;;STORE INITIAL COMPANIES¨
;===========================
;;record the number of turtles at the begining of each tick
;;The number of turtles of each strategy is used when calculating average payoffs.
to store-initial-companies-counts
set num-tit-for-tat-com (count companies with [strategy-com = "tit-for-tat-com"])
set num-unforgiving-com (count companies with [strategy-com = "unforgiving-com"])
end
;Setup-History-Lists-com;; initialize PARTNER-HISTORY list in all turtles
;========================
to setup-history-lists-com
set num-companies (count companies)
let default-history-com [] ;;initialize the DEFAULT-HISTORY variable to be a list
;create a list with NUM-TURTLE elements for storing partner histories
repeat (count companies) [ set default-history-com (fput false default-history-com) ]
;give each turtle a copy of this list for tracking partner histories
ask companies [ set partner-history-com default-history-com ]
end
;Clear-last-round
;================
to clear-last-round
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ release-partners-com ]
end
; release partner between companies and turn around
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to release-partners-com
set partnered?-com false
set partner-com nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
;;partner-up. Find partner
;===========================
to partner-up-com ;;turtle procedure
if (not partnered?-com) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner-com one-of other (companies in-radius 1 ) with [ not partnered?-com ]
if partner-com != nobody [ ;;if successful grabbing a partner, partner up
set partnered?-com true
set heading 270 ;;face partner
ask partner-com [
set partnered?-com true
set partner-com myself
set heading 90
]
]
]
end
;Select stratey company
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;choose an action based upon the strategy being played
to select-strategy-com ;;turtle procedure
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com ]
if strategy-com = "unforgiving-com" [ unforgiving-com ]
end
;Play a round-companies
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to play-a-round-com ;;turtle procedure
get-payoff-com ;;calculate the payoff for this round
update-history-com ;;store the results for next time
end
; get-payoff-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;calculate the payoff for this round and
;;display a label with that payoff.
;; Strategy-companies
;Turtles l l
;Action l C l D
;__________l________ l__________
; C l 3 l -3
;----------l---------l----------
; D l 5 l -2
to get-payoff-com
set partner-defected?-com [defect-now?-com] of partner-com
ifelse partner-defected?-com [
ifelse defect-now?-com
[ set score-com (score-com + 1 ) set label 1 set score-com-round 1 ]
[set score-com (score-com + 0) set label 0 set score-com-round 0]
]
[ifelse defect-now?-com
[set score-com (score-com + 5) set label 5 set score-com-round 5]
[set score-com (score-com + 3) set label 3 set score-com-round 3]
]
end
;Update-history
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;update PARTNER-HISTORY based upon the strategy being played
to update-history-com
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com-history-update ]
if strategy-com = "unforgiving-com" [ unforgiving-com-history-update ]
end
;;;Strategies;;;
; Tit-for-tat-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com
set num-tit-for-tat-com-games num-tit-for-tat-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set tit-for-tat-com-score-round ( tit-for-tat-com-score-round + score-com-round )
set earnings-tit-for-tat-com (earnings-tit-for-tat-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + tit-for-tat-com-score-round - energy-consume-companies )
ask patches in-radius 1 [set resources-to 0 ]
set tit-for-tat-com-score-round 0
]
end
;Tit-for-tat-com-history-update¨
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com-history-update
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
end
;unforgiving-com ; Works similar to tit-for-tat, but once the another defect, always defect
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com
set num-unforgiving-com-games num-unforgiving-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set unforgiving-com-score-round ( unforgiving-com-score-round + score-com-round )
set earnings-unforgiving-com (earnings-unforgiving-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + unforgiving-com-score-round - energy-consume-companies)
ask patches in-radius 1 [set resources-to 0 ]
set unforgiving-com-score-round 0
]
end
;unforgiving-com-history-update
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com-history-update
if partner-defected?-com [
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
]
end
;; Adjusting Patches
;; Put limits
to adjust-patches
ask patches [ set resources-amount resources-amount + random-float Inc-Ecological-resources ] ;; Resources are renewable. They grow up again between [0-3). Use Slider
ask patches [ ;; Resources range [0-3)
set resources-amount (ifelse-value
resources-amount < 0 [0] ;;
resources-amount > 3 [3] ;;
[resources-amount]
)
]
;; Patches are colored according their level
ask patches [
set pcolor (ifelse-value
resources-amount < 0 [ black ]
resources-amount = 0 [ 38 ]
resources-amount > 0 and resources-amount <= 0.5 [ 49 ]
resources-amount > 0.5 and resources-amount <= 2 [ 67 ]
resources-amount > 2 and resources-amount <= 3 [ 65 ]
resources-amount > 3 and resources-amount <= 4 [ 63 ]
[ white ])
]
end
I am writing a model and want to add a button of chooser to assess alternative management options. Model was working fine as some variables graphs disappeared as I have added chooser for around all of turtles. I have also tried changing parameters from Turtle-own to global and vice versa and not rectified the error. I couldn't figure out the issus. Problem arise when i add chooser in irrigate procedure. Codes are bit longer, please bear with me. I will be grateful for the solution. Please help me to get out of difficult situation.
I have also shared before and after image of interface.
Before
and after
Thanks
Globals [farmlocations farmers-list water eva IWR-perday ASW maxgwater usedflow wdemand wtdmean wtdsd av-wdischarge wdischarge wd totannual-wdischarge current-demand wtd canal-wdischarge water-right alpha beta]
Breed [Farmers farmer]
farmers-own [ irrigation-turn season1-profit swh season2-profit yield myflow DWT total-gw-cost watercosts total-sw-cost raqba sw benefits evapotrans totaldemand swcost gwateruse wneed logging gwcost salinity aw totalcosts dws resources irrigation-turn gw AGW mywaterloss] ;swh= surface water height aw= old surface water height
Patches-own [gw-level]
links-own [flow]
to setup
clear-all
setup-farmers
import-data
reset-ticks
end
to setup-farmers
create-farmers 5 [ set irrigation-turn [0]
ask farmer 0 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 1 [ setxy min-pxcor + 1 0 set irrigation-turn 2]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 2 set irrigation-turn 3]
ask farmer 4 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
]
set farmers-list sort turtles
set eva 0.3
ask farmers [set label who + 1
set size 1
set myflow 0
If land = "small farmers" [ set raqba random 5 + 3 ]
If land = "small and large farmers" [ ifelse random 3 = 1 [set raqba 10 + random 20 ] [ set raqba 5 + random 5]]
set total-sw-cost 500 * raqba
Set resources 1
set DWS 5 + 1 * (who)
set evapotrans 5 + 1 * (who)
set water-right 4.5 * raqba
Set logging 500 / DWS
Set salinity DWS / 5
set maxgwater 200
set wtdmean 10
set wtdSD 5
set sw 0
set swh 0
set aw 0
set gw-level 100 / dws
set gw 50000 + random 1000
Depth-to-Water-Table
ground-water-travel
water-losses
set alpha 1
set beta 0.7
]
set av-wdischarge 4.5
end
to Depth-to-Water-Table
set DWT Water-Table-Depth + DWS / 10
end
to ground-water-travel
set gw gw + 10 / (random-float 1 + DWT) * DWS
set AGW gw + (sum [gw-level] of patches in-radius 2)
set pcolor scale-color blue AGW 1 max [gw-level] of patches in-radius 2
end
to water-losses
Set mywaterloss waterloss + 5 * DWS ; 5 + 1* who
end
to go
tick
if ticks = 630 [ stop ]
calculate-sw
evaporate
irrigate
ask turtles [set color scale-color blue swh 30 3]
calculate-profits
update-output
end
to
calculate-sw
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set canal-wdischarge item list-element totannual-wdischarge
set wdischarge canal-wdischarge - (canal-wdischarge * discharge-reduction)]
end
to evaporate
let list-element floor ticks / 5
ask item (ticks mod 5) farmers-list [
set current-demand item list-element IWR-perday
set Aw swh
if aw >= current-demand [set wneed 0]
set wneed 0
set wneed wneed + current-demand - aw
ask farmers [ set totaldemand current-demand * raqba + evapotrans]
if wneed < 0 [set wneed 0]
set swh swh - swh * eva
if swh < 0 [set swh 0]
]
end
to irrigate
If water-use-mangement = "Business as usual" [
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red
set shape "person"
set pcolor brown + who
set ASW (Wdischarge - myWaterloss)* raqba
if ASW < 0 [set ASW 0]
set myflow ASW
ifelse wneed > 0 [
set swh myflow / raqba + aw
ifelse swh > current-demand [set swh current-demand ] [use-gw]
ifelse swh > aw [set usedflow (swh - aw) * raqba
if usedflow < 0 [ set usedflow 0]
set ASW ASW - usedflow
if ASW < 0 [ set ASW 0]
update-gw-use ]
[set usedflow 0]] [
set myflow 0
set swh aw + ((myflow ) / raqba)
if swh > current-demand [set swh current-demand ]]]]
end
to use-gw
Ask farmers [ let excess-demand (current-demand - swh) * raqba
if AGW > excess-demand [ ;set excess-demand AGW
set gwateruse agw - excess-demand
set dwt dwt + (0.2 + who / 20)
Ifelse DWT > 8 [ set gwcost excess-demand * 17.5] [set gwcost excess-demand * 5]
set agw agw - excess-demand
If AGW < 0 [ set AGW 0]
Ifelse excess-demand > 0.5 * current-demand [ifelse random 4 = 1 [ set salinity salinity + 0.1
set logging logging - 0.05]
[set salinity salinity + .05
set logging logging + 0.05 ]] [set salinity salinity - 0.05
set logging logging + 0.1]]]
set gw gw - 1
end
to update-gw-use
set salinity salinity + .04
set logging logging + 0.01
set gw gw - 1
set total-gw-cost total-gw-cost + gwcost
end
to calculate-profits
ask farmers [ if ticks mod 630 = 252 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
;update-yield
calculate-season1-profit
]]
ask farmers [ if ticks mod 630 = 0 [ set total-sw-cost raqba * 500
set watercosts total-gw-cost + total-sw-cost
calculate-season2-profit]]
end
to calculate-season1-profit
ask farmers [ set yield yield + ((usedflow / totaldemand ) * alpha + ( gwateruse / totaldemand)* beta) * 55 - 0.1 * (55 * salinity / 12) - 0.1 *(55 * logging / 12) ; 55mon/acre market price
let price 1350 ; price/ maund
set season1-profit yield * price - watercosts
]
end
to calculate-season2-profit
ask Farmers[ set yield yield + ((usedflow / totaldemand ) * alpha + (gwateruse / totaldemand)* beta ) * 100 - 0.1 * (100 * salinity / 18) - 0.1 * (100 * logging / 18) ; 100 mauns/acre market price
let price 2500 ; price/ maund
set season2-profit yield * price - watercosts]
end
to update-output
set-current-plot "Available-surface-water"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [swh] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [swh] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [swh] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [swh] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [swh] of turtle 4] [plot 0]
set-current-plot "Logging"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [logging] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [logging] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [logging] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [logging] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [logging] of turtle 4] [plot 0]
set-current-plot "Salinity"
set-current-plot-pen _clarify-duplicate-plot-pen-name "Farmer 1" ifelse ticks > 0 [plot [salinity] of turtle 0] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 2" ifelse ticks > 0 [plot [salinity] of turtle 1] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 3" ifelse ticks > 0 [plot [salinity] of turtle 2] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 4" ifelse ticks > 0 [plot [salinity] of turtle 3] [plot 0]
set-current-plot-pen _clarify-duplicate-plot-pen-name "farmer 5" ifelse ticks > 0 [plot [salinity] of turtle 4] [plot 0]
end
to import-data
ifelse ( file-exists? "totannual-wdischarge.txt" )
[
set totannual-wdischarge []
file-open "totannual-wdischarge.txt"
while [ not file-at-end? ]
[
set totannual-wdischarge sentence totannual-wdischarge (list file-read)
]
file-close
]
[ user-message "There is no totannual-wdischarge.txt file in current directory!" ]
;; Import water demand
ifelse ( file-exists? "IWR-perday.txt" )
[
set IWR-perday []
file-open "IWR-perday.txt"
while [ not file-at-end? ]
[
set IWR-perday sentence IWR-perday (list file-read)
]
file-close
]
[ user-message "There is no IWR-perday.txt file in current directory!" ]
end
to-report _clarify-duplicate-plot-pen-name [ name ]
let name-map [["Min" "Min"] ["MIn" "MIn_1"]]
let replacement filter [ rename -> first rename = name] name-map
let reported-name name
if not empty? replacement [
set reported-name item 1 replacement
]
report reported-name
end
The plots are working, it's just that they are showing 0. My best guess is that the text you have inside the chooser for the options is different than the text you are testing in your if statement, so the if statement is always false.
Try something like this:
ifelse water-use-mangement = "Business as usual"
[ ; all your irrigation code that you already have
]
[ print "ERROR: Management case not found"
]
This will print an error message if the text doesn't match.
I'm trying to create an own ecosystem and have already written about 100 lines of code. But I'm currently getting an annoying error message [Title] I cannot get rid of unless I delete the line with the «Tick» command.
That's how the Interface looks like at the moment
That's how the Interface looked like after the latest successful simulation
That's the defective Code:
turtles-own [saturation age gender]
breed [sheep a-sheep]
sheep-own [love-cooldown-sheep-f love-cooldown-sheep-m age%-sheep]
globals [tick-stopper]
patches-own [Next-to]
to setup
clear-all
setup-patches
setup-sheep
set tick-stopper 0
reset-ticks
end
to setup-patches
ask patches [ set pcolor green ]
end
to setup-sheep
create-sheep number_of_sheep [set saturation max-saturation-sheep set gender random 2 set shape "sheep" set age 0]
if setup-sheep-random-age? [
ask turtles with [shape = "sheep"] [set age random life-exp.-sheep]
]
ask turtles with [shape = "sheep"] [
setxy random-xcor random-ycor
if gender = 0 [
set color 118
]
if gender = 1 [
set color 98
]
]
end
to go
if tick-stopper >= stop-at-tick and stop-at-tick? [
set tick-stopper 0
stop
]
move-sheep
sheep-eat-grass
make-love-sheep
check-death-sheep
regrow-grass
set tick-stopper tick-stopper + 1
tick
end
to move-sheep
ask turtles with [shape = "sheep"] [
right random 60
left random 60
forward 1
set saturation saturation - 1
set age age + (((random life-exp.-sheep) + 1) / ((life-exp.-sheep + 1)/ 2))
if gender = 0 and love-cooldown-sheep-f > 0 [
set love-cooldown-sheep-f love-cooldown-sheep-f - 1
set saturation (saturation - (saturation-loss-til-birth-sheep / contribution-period-sheep))
]
]
if gender = 1 [
if love-cooldown-sheep-m > 0 [
set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
to sheep-eat-grass
ask turtles with [shape = "sheep"] [
if pcolor = green and saturation < max-saturation-sheep [
set pcolor brown
set saturation saturation + saturation-from-grass
]
ifelse show-saturation?
[ set label saturation ]
[ set label "" ]
]
end
to make-love-sheep
ask turtles with [shape = "sheep"] [
if gender = 0 and love-cooldown-sheep-f = 1 [hatch 1 [set saturation birth-saturation-sheep set gender random 2 set age 0
if gender = 0 [set color 118]
if gender = 1 [set color 98]
if gender = 1 and love-cooldown-sheep-m = 0 and saturation > saturation-for-love-sheep and 1 = count turtles with [shape = "sheep" and gender = 0 and love-cooldown-sheep-f = 0 and saturation > saturation-for-birth-sheep] in-radius 1 [
set saturation saturation - saturation-loss-at-love-sheep
set love-cooldown-sheep-m cooldown-for-love-sheep
ask turtles with [shape = "sheep"] in-radius 1 [
if gender = 0 and saturation > saturation-for-birth-sheep and love-cooldown-sheep-f = 0 [set love-cooldown-sheep-f contribution-period-sheep]
]
]
]
]
]
end
to check-death-sheep
ask turtles with [shape = "sheep"] [
set age%-sheep (100 * (age / life-exp.-sheep))
if saturation <= 0 [die]
if age%-sheep < 22.5039287 [
if (10 ^(0.00984 *(age%-sheep ^ 2) - 0.25918 * age%-sheep + 2.922))/ life-exp.-sheep > random 10000 [die]
]
if age%-sheep > 156.24733 [
if 10000 / (life-exp.-sheep / 100) > random 10000 [die]
]
if age%-sheep > 22.5039287 and age%-sheep < 156.24733 [
if (10 ^(1.899801588 -(((16 / 75 * (age%-sheep ^ 3)) - (50 * (age%-sheep ^ 2))) / 131250)))/ life-exp.-sheep > (random 10000) [die]
]
]
end
to regrow-grass
ask patches with [pcolor = brown] [
set next-to count neighbors4 with [pcolor = green]
if (random 1000000 / 10000) <= (grass-growth% * count neighbors4 with [pcolor = green]) [set pcolor green]
]
end
Thanks for your help <3
This is my first project with NetLogo 6.0.4 and it isn't planned to start any other projects after this one.
You have a bracketing problem in your move-sheep procedure. You have a structure like this:
to move-sheep
ask turtles with [shape = "sheep"]
[ right random 60
...
if gender = 0 and love-cooldown-sheep-f > 0
[
]
]
if gender = 1
[ if love-cooldown-sheep-m > 0
[ set love-cooldown-sheep-m love-cooldown-sheep-m - 1
]
]
end
So you accidentally closed the ask turtles before dealing with the if gender = 1. As soon as you have if gender = 1 then it switches to turtle context.
Hi I need help in NetLogo variable settings.
I have turtles that own attributes with range of values, that is the minimum and the maximum.
turtles-own [weight history state-turtles run-duration ek tt cu sp]
to setup-turtles
create- NMAs 2 [
set ek 8 ; range 8 to 9
set tt 5 ;range 5 to 7
set cu 3 ; range 3 to 5
set sp 4 ; range 4 to 7
]
create- NBSs 2 [
set ek 3 ; range 3 to 5
set tt 4 ; range 4 to 7
set cu 3 ; range 3 to 4
set sp 3 ; range 3 to 6
]
to setup-patches
ask patches [ let projects random 4
;setup colours
]
End
to go
tick
ask turtles [
......
]
search-patch
if .....
]
End
to search-patch
if ( [ pcolor ] of patch-here = brown ) [
ifelse (;statement) [
update-turtles
] [
action-turtles
]
]
end
to update-turtles
if pcolor = yellow [
set ek ek + 0.1
set tt tt + 0.5
set cu cu + 0.1
set sp sp + 1 ]
if pcolor = green [
set ek ......
The numbers are kept increasing and I want to set up limits (range) as shown in setup turtles for each attribute Thank you
turtles-own [... ek-max tt-max cu-max sp-max ...]
to setup
...
create-NMAs 2 [
set ek 8
set ek-max 9
...
]
...
end
to update-turtles
...
if ek + 0.1 <= ek-max [ set ek ek + 0.1 ]
if tt + 0.5 <= tt-max [ set tt tt + 0.5 ]
if cu + 0.1 <= cu-max [ set cu cu + 0.1 ]
if sp + 1 <= sp-max [ set sp sp + 1 ]
...
end
As a side note, tick should come at the end of your go procedure, not at the beginning (reference).
How can I accomplish the following in the code below:
patches change color reflective of their distance from the row "min-pycor"
For example, colors alternate from yellow to red and then to black (signifying death).
But this should take into account that the production of yellow patches > red > black.
turtles-own
[
stem? ;; true for stem cells, false for transitory cells
age ;; age of cell. changes color with age
metastatic? ;; false for progeny of stem cell 0, true for progeny of stem cell 1
]
globals
[
cell-count
]
to setup
clear-all
set-default-shape turtles "square"
ask patches[
if pycor = min-pycor [
ifelse random 10 <= 2
[set pcolor white]
[sprout 1 [set shape "square" set color blue] ]
]
]
evaluate-params
reset-ticks
end
to go
ask patches with [pcolor = yellow]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
ask patches with [pcolor = white]
[if count neighbors with [pcolor = black] > 0
[ask one-of neighbors with [pcolor = black][set pcolor yellow]
]
]
tick
end
;;transitional cells move and hatch more. Turtle proc.
to move-transitional-cells
if (not stem?)
[
set color ( red + 0.25 * age )
fd 1
if (age < 6)
[
hatch 1
[ ;amplification
rt random-float 360
fd 1
]
]
]
end
to mitosis ;; turtle proc. - stem cells only
if stem?
[
hatch 1
[
fd 1
set color red
set stem? false
ifelse (who = 1)
[ set age 16 ]
[ set age 0 ]
]
]
end
to death ;; turtle proc.
if (not stem?) and (not metastatic?) and (age > 20)
[ die ]
if (not stem?) and metastatic? and (age > 4)
[ die ]
end
to evaluate-params
set cell-count count turtles ;cell count
if (cell-count <= 0)
[ stop ]
end
to kill-original-stem-cell
ask turtle 0
[ die ]
end
to kill-moving-stem-cell
ask turtle 1
[ die ]
end
to kill-transitory-cells
ask turtles with [ age < 10 and not stem? ]
[ die ]
end
You seem to have two conflicting requirements, the color change based on proximity in your code, and the color change based on PYCOR that you ask about.
Ignoring the code for a moment, we can set color based on PYCOR in many ways. For example, we can create bands of color, we can create dithered interminglings of color, or we can create a "smooth" transition between colors.
The first is easy. We can use an IFELSE structure. This example creates even bands, but you can change the "divide" variables to create bands of any height.
let color1 red
let color2 yellow
let color3 black
let divide1 (min-pycor + world-height * 0.33)
let divide2 (min-pycor + world-height * 0.66)
ask patches
[ ifelse pycor < divide1 [ set pcolor color1 ][
ifelse pycor < divide2 [ set pcolor color2 ][
set pcolor color3
]]
]
We can also do it in a mathy way. This example creates even bands.
let colors [ red yellow black ]
let bands length colors
let band-height floor ( world-height / bands + .5 )
;; pycor - min-pycor shifts the range from 0 to world-height
ask patches [ set pcolor item ( floor ( ( pycor - min-pycor ) / band-height ) ) colors ]
We can create dithered bands by introducing a random element to the pycor. we also have to add some tests to keep the random numbers in range.
let colors [ red yellow black ]
let bands length colors
let band-height floor (world-height / bands + .5)
let dither band-height * .5 ;; adjust this to change dithery-ness
ask patches
[ let py pycor - min-pycor + dither - random-float ( dither * 2 )
;; you might want to really study the above line to fully grok what's happening there
if py < 0 [ set py 0 ]
if py > world-height [ set py world-height ]
set pcolor item ( floor ( py / band-height ) ) colors
]
Graduated (gradiant) bands are tougher, because the NetLogo color space doesn't do gradual shifts in hue, only tint and shade, so there's really no way to get from red to yellow that way. So we would have to use RGB (three value list)colors, instead of NetLogo (single value) colors. And that's beyond what I'm willing to type out at the moment, so there you go--left as an exersize.