Reputation: 99
How can I write a code that permit to an agent to change a value of a variables of a patches,where the agent is not above? It is the case of a farmer, in a previous question I asked how to create the farm. Is it possible that an agent could change a variable of different patches during the same tick? thank you very much for your help.
After 1 tick
After 30 tick
Upvotes: 2
Views: 1082
Reputation: 1894
This is another way that you can do it:
turtles-own [
profit-from-fuel
profit-from-food
expected-fuel-sell-price
expected-food-sell-price
profit
farm
farm-size
;risk-attitude-food
;risk-attitude-fuel
]
patches-own [
fuel-yeld
food-yeld
land-sustainability
water-level
belongs-to
food
fuel
]
globals [
fuel-sell-price
food-sell-price
governs
]
to setup
clear-all
clear-all-plots
create-farmers
setup-land
reset-ticks
ask turtles
[ set-farm-in-radius farm-size ]
set fuel-sell-price 30 ;+ random 2 + random -2
set food-sell-price 30 ;+ random 2 + random -2
end
to create-farmers
create-turtles 30
[
set shape "person"
setxy random-pxcor random-pycor
set profit-from-fuel 0 ; indicizzazione del profitto iniziale a 0
set profit-from-food 0 ; indicizzazione del profitto iniziale a 0
set farm-size random 5 + 1
set label farm-size
]
end
to setup-land
ask patches [set belongs-to nobody]
ask patches
[
set pcolor 3
set food-yeld 10000
set fuel-yeld 10000
set land-sustainability random 5
set water-level random 3
]
end
to set-farm-in-radius [d]
move-to one-of patches with [not any? other patches in-radius d with [belongs-to != nobody]]
set farm patches in-radius farm-size
ask farm [set belongs-to myself]
let c random 6 + 61
ask farm [set pcolor c]
end
to set-farm-distance [d]
move-to one-of patches with [not any? other patches with [belongs-to != nobody and distance myself < d]]
set farm patches with [distance myself < d]
ask farm [set belongs-to myself]
let c random 6 + 61
ask farm [set pcolor c]
end
to go
tick
ask turtles [
set expected-fuel-sell-price fuel-sell-price + random 5 + random -5
if expected-fuel-sell-price < 0 [set expected-fuel-sell-price 1]
set expected-food-sell-price food-sell-price + random 5 + random -5
if expected-food-sell-price < 0 [set expected-food-sell-price 1]
set profit profit-from-fuel + profit-from-food
if profit = 0 [ set profit 1 ]
]
set fuel-sell-price fuel-sell-price + random 5 + random -5
if fuel-sell-price < 0 or fuel-sell-price = 0 [set fuel-sell-price 1 ]
set food-sell-price food-sell-price + random 5 + random -5
if food-sell-price < 0 or food-sell-price = 0 [set food-sell-price 1]
ask turtles [
cultivate
ask farm [recolor-farm]
set profit profit-from-food + profit-from-fuel
;if water-level > 0.95 [ set profit profit - (profit * ( 2 / profit )) ] valutare se inserire anche una failing probability
]
if ticks = Duration [ stop ]
if ticks > Duration [stop]
end
to cultivate
let r risk-attitude
let e-f expected-fuel-sell-price
let e-food expected-food-sell-price
ask farm [
ifelse land-sustainability < 2.1 or water-level = 1
[ ask myself [set profit-from-food food-sell-price * (((sum [food-yeld] of farm ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa))
]set food 1
]
[
if land-sustainability >= 2.1 or water-level = 0
[
let utility-from-food (( food-yeld * e-food * land-sustainability) ^ r ) / r
let utility-from-fuel (( fuel-yeld * e-f * land-sustainability) ^ (1 - r) ) / ( 1 - r)
ifelse utility-from-food < utility-from-fuel
[
ask myself [set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) ]
set fuel 1
]
[ ask myself [set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa)) ]
set food 1
]
]
]
]
end
to recolor-farm
if food = 1 [set pcolor green ]
if fuel = 1 [set pcolor red]
end
and this is after first tick:
This is final view:
Upvotes: 0
Reputation: 1894
If this is related to your Farmer question you can use following:
When you want to make changes on a group of patches with a common property you should filter them with their common property , for example if the belong so same Farmer just ask Farm
if you want to make changes on a patches from a farm which has food = 1 you can ask
ask farm with [food = 1]
In summary, I think you should include these kind of condition in your cultivate procedure to filter which patch is being changed.
ask farm with [land-sustainability < 2.1 or water-level = 1]
[
set food 1
set pcolor green
]
ask farm with [The condition which they should set fuel to 1]
[
set fuel 1
set pcolor red
]
UPDATE
turtles-own [
profit-from-fuel
profit-from-food
expected-fuel-sell-price
expected-food-sell-price
profit
farm
farm-size
;risk-attitude-food
;risk-attitude-fuel
]
patches-own [
fuel-yeld
food-yeld
land-sustainability
water-level
belongs-to
food
fuel
]
globals [
fuel-sell-price
food-sell-price
governs
]
to setup
clear-all
clear-all-plots
create-farmers
setup-land
reset-ticks
ask turtles
[ set-farm-in-radius farm-size ]
set fuel-sell-price 30 ;+ random 2 + random -2
set food-sell-price 30 ;+ random 2 + random -2
end
to create-farmers
create-turtles 30
[
set shape "person"
setxy random-pxcor random-pycor
set profit-from-fuel 0 ; indicizzazione del profitto iniziale a 0
set profit-from-food 0 ; indicizzazione del profitto iniziale a 0
set farm-size random 5 + 1
set label farm-size
]
end
to setup-land
ask patches [set belongs-to nobody]
ask patches
[
set pcolor 3
set food-yeld 10000
set fuel-yeld 10000
set land-sustainability random 5
set water-level random 3
]
end
to set-farm-in-radius [d]
move-to one-of patches with [not any? other patches in-radius d with [belongs-to != nobody]]
set farm patches in-radius farm-size
ask farm [set belongs-to myself]
let c random 6 + 61
ask farm [set pcolor c]
end
to set-farm-distance [d]
move-to one-of patches with [not any? other patches with [belongs-to != nobody and distance myself < d]]
set farm patches with [distance myself < d]
ask farm [set belongs-to myself]
let c random 6 + 61
ask farm [set pcolor c]
end
to go
tick
ask turtles [
set expected-fuel-sell-price fuel-sell-price + random 5 + random -5
if expected-fuel-sell-price < 0 [set expected-fuel-sell-price 1]
set expected-food-sell-price food-sell-price + random 5 + random -5
if expected-food-sell-price < 0 [set expected-food-sell-price 1]
set profit profit-from-fuel + profit-from-food
if profit = 0 [ set profit 1 ]
]
set fuel-sell-price fuel-sell-price + random 5 + random -5
if fuel-sell-price < 0 or fuel-sell-price = 0 [set fuel-sell-price 1 ]
set food-sell-price food-sell-price + random 5 + random -5
if food-sell-price < 0 or food-sell-price = 0 [set food-sell-price 1]
ask turtles [
cultivate
ask farm [recolor-farm]
set profit profit-from-food + profit-from-fuel
;if water-level > 0.95 [ set profit profit - (profit * ( 2 / profit )) ] valutare se inserire anche una failing probability
]
if ticks = Duration [ stop ]
if ticks > Duration [stop]
end
to cultivate
ifelse land-sustainability < 2.1 or water-level = 1
[
set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa))
ask farm with [land-sustainability < 2.1 or water-level = 1]
[
set food 1
]
]
[
let utility-from-food ((food-yeld * expected-food-sell-price * land-sustainability) ^ risk-attitude ) / risk-attitude
let utility-from-fuel ((food-yeld * expected-fuel-sell-price * land-sustainability) ^ (1 - risk-attitude) ) / ( 1 - risk-attitude)
ifelse utility-from-food < utility-from-fuel
[
set profit-from-fuel fuel-sell-price * (((fuel-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa))
ask farm with [land-sustainability >= 2.1 or water-level != 1][
set fuel 1
]
]
[
set profit-from-food food-sell-price * (((food-yeld ) ^ (1 - alfa)) * (((water-level) ^ (1 - gamma)) * ((land-sustainability) ^ (gamma)) ^ alfa))
ask farm with [land-sustainability >= 2.1 or water-level != 1] [
set food 1
]
]
]
end
to recolor-farm
if food = 1 [set pcolor green ]
if fuel = 1 [set pcolor red]
end
Upvotes: 1
Reputation: 14972
It is a special feature of NetLogo that an agent can directly change the value of a variable of the patch it is directly over (i.e., ask turtle 0 [ set pcolor blue ]
will turn the patch under turtle 0
to blue
), but any turtle can ask
any patch to change one of its variables.
For example:
ask turtles [
ask patch-ahead 1 [
set pcolor green
]
]
will tell all turtles to change to color of the patch ahead of them (not directly under) to green
.
They can also ask multiple patches to do stuff, like in the in-radius
example found in the user manual:
ask turtles [
ask patches in-radius 3 [
set pcolor red
]
]
Upvotes: 2