I am posting my query again for the same issue I am facing. I have tried NetLogo model library, table command and all other options in my domain. I am still unable to solve the problem.
Here is my problem; I want turtles to be rational and evaluate strategies with best output and adapt the one with highest output with upcoming time period. It is as farmers evaluate their water use behavior and learn from past and adapt accordingly. In the codes I have tried writing it as farmers have 3 strategies which give them different yield. In go farmer will evaluate and follow the strategy with maximum yield. I am unable to call and follow the strategy with maximum yield.
Any help in this regard will be highly appreciated.
Thanks
Here are the codes:
Globals [ surface-water groundwater maximum-yield water-demand water-used ]
Turtles-own [strategies strategy memory land groundwater-use surfacewater-use yield ]
to setup
clear-all
let memory-length 5
;let strategies ["use-gw" "use-sw" "50G-50s" "70G-25S" "25G-70S"]
crt 10 [ ;set yield 0
set memory n-values memory-length [-1]
set surface-water 10
set maximum-yield 60
set groundwater 20
set water-demand 17
set land random 5 + 3
strategy-1
strategy-2
strategy-3
;best-strategy
let no-strategies [ "strategy-1" "strategy-2" "strategy-3"]
set strategy n-values 2 [one-of no-strategies]
]
reset-ticks
end
to strategy-1
set surfacewater-use water-demand - surface-water
if surface-water < 0 [ set surface-water 0]
let excess-demand water-demand - surface-water
set groundwater-use excess-demand - groundwater
set yield 100 * surfacewater-use / water-demand + 70 * groundwater-use / water-demand
end
to
strategy-2
set surfacewater-use surface-water - 0.5 * water-demand
if surface-water < 0 [ set surface-water 0]
set groundwater-use groundwater - 0.5 * water-demand
set yield 100 * surfacewater-use / water-demand + 70 * groundwater-use / water-demand
end
to
strategy-3
set yield 70
end
to go
tick
ask turtles
[set yield run-result strategy
;foreach strategy run
set memory fput output but-last memory]
ask turtles [
show memory
show item 0 memory
show item 1 memory
show item 2 memory
show item 3 memory
show item 4 memory]
end
to-report output
report yield
end
Related
In NetLogo, I would like to have each round a likelihood of between 0 to 10 % turtles of the whole population to have a change of a variable. Within the chosen turtles, their variable can change between +1 and +4 by a certain likelihood.
breed [ humans human ]
humans-own [ var ]
to setup
create-humans(population) [ set var 0 ]
end
to go
ask humans [ var_change ]
end
to var_change [
let %draw (random-float 100)
let %strength 0 ;no eco loss
if (%draw < 50) [ set %strength (%strength + 1) ] ;1 little eco loss
if (%draw < 10) [ set %strength (%strength + 2) ] ;2 middle eco loss
if (%draw < 5) [ set %strength (%strength + 3) ] ;3 strong eco loss
if (%draw < 1) [ set %strength (%strength + 4) ] ;4 complete eco loss
[ ask one-of %strength patches [ set economic economic + 3 ]]; here I do not know how to continue(*)
]
end
*I do not know how to code that between 0 to 10 % of turtles can have with a certain probability have their variable var changed. How can I achieve that?
The way you have it set up, all turtles are sent to the var_change procedure. It would be easier to have the var_change procedure both select the turtles to change and assign the amount of change. Something like:
to go
var_change
end
to var_change
; choose proportion to change
let %draw-prop random-float 0.1
let n-changers round (%draw-prop * count turtles) + 1 ; +1 so at least one changes
; change by some amount
ask n-of n-changers turtles
[ let %draw-change random 100
set economic economic + 1 ;all get some eco loss
if (%draw-change < 10) [ set economic economic + 1 ] ;2 middle eco loss
if (%draw-change < 5) [ set economic economic + 1] ;3 strong eco loss
if (%draw-change < 1) [ set economic economic + 1 ] ;4 complete eco loss
]
end
Note that I changed all your + amounts to + 1. The way you had it written, drawing a number like 3 would have added 1 (as <50) then another 2 (as <10) then another 3 (as <5) for a total increase of 6. An if clause runs the code if the if condition is satisfied and skips over it if not satisfied. Either way, the next code always runs.
I would create a periodic function in Netlogo based on ticks and a price variable.
I would like a function where price is maintained between 2 rang (0 and 1) and I will be great if I can choose the periodicity.
my small reproducible example :
patches-own[
price
]
to setup
clear-all
ask patches [
set price 0.1
]
reset-ticks
end
to go
ask patches [
set price (sin ticks) + price
]
tick
end
Thank you in advance
It sounds like you just want a mod based cycle.
to test
ca
reset-ticks
print map price n-values 100 [?]
print map [price02 0 1 ?] n-values 100 [?]
end
to-report price [#ticks]
let _cycle 10 ;cycle length
let _first 3 ;length of first cycle component
report ifelse-value (#ticks mod _cycle < _first) [
0.1
][
0.2
]
end
Edit:
Alternatively, you may simply be trying to scale a sine wave.
to-report price02 [#min #max #ticks]
let _cycle 10
let _sin sin (#ticks * 360 / _cycle)
let _scaledsin (1 + _sin) / 2
report #min + (#max - #min) * _scaledsin
end
In my model I have some agents which act as food items with a set energy. These are fed upon by a number of turtle breeds who each have their own food-energy which is less than the energy of the food item.
The code for the feeding agents is as follows:
to eat
ifelse [food-energy] of myfood > 1.5 [
set food-energy 1.5]
end
and the associated code for the food item to decay is:
to decay
if any? turtles-here [set food-energy
(1.5 * count feeders-here with [myfood = myself]
end
The problem occurs if the energy of the food is not an exact multiple of the amount of energy the feeders can consume. So for example it can go down to 1 and this results in the feeders taking 1.5 units which should be impossible. This is exacerbated when I have different breeds with different food energies (i.e. < or > 1.5).
So my question is how can I get this things to balance?
You need to study the Wolf-Sheep Predation model. This is the first NetLogo tutorial: http://ccl.northwestern.edu/netlogo/docs/tutorial1.html There are five versions of increasing complexity in the NetLogo Models Library, which are covered in chapter 4 of Wilensky and Rand (2015), which you should read.
See some related material here:
http://jasss.soc.surrey.ac.uk/14/2/5.html
Some hints follow, but many details need filling in.
breed [feeders feeder]
patches-own [ food-energy ]
feeders-own [ myfood ]
to setup
ca
ask patches [set food-energy random 50]
create-feeders 500 [
move-to one-of patches
set myfood one-of patches
]
end
to go
ask feeders [move]
ask feeders [feed]
ask patches [growback]
end
to move ;how shd they move?
rt random 20
left random 40
fd 1
;shd movement cost energy?
end
to feed
if (patch-here = myfood) [
let _extracted min (list food-energy 1.5)
set food-energy (food-energy - _extracted)
]
end
to growback
;do you want growback?
end
thanks for your responses. I'll try to implement them. This is one inelegant workaround that worked for me:
to eat
ifelse (food-energy / capacity) < 1 and [meat] of myfood > capacity [
set food-energy 1.5] [set food-energy [meat] of myfood
ask myfood [set shape "square"]]
if (food-energy / capacity) = 1 [
set color white]
if (food-energy > 0 and food-energy / capacity < 1)
[ set color white ]
end
This was initially causing a problem such that when the food energy went down to 0 and I asked it to die any animal looking at the [meat] of myfood lost it's target and I got an error. So I made the animals break this connection once their colour was white.
to ignore
set myfood nobody
set food-energy food-energy * 1
end
In my model I have some agents collecting from another agent who they bump into at random after which they move back to their base. As they move back they drop off some material as defined by the random function. Here's some sample code
to go
ask searchers
[ set energy energy - 1
fd 0.0125
if random-float 1 < (1 / 50)
[ ifelse random 2 = 0
[ rt 45 ]
[ lt 45 ]
]
search
]
end
to search
if any? depots in-radius vision with [color = yellow]
[spread
set energy 0] ;; makes them to back to base
end
to spread
if random 10000 = 1 [hatch-rubbish 1 [ set color white
set shape "circle"
set size 0.5]]
end
If I set the visual radius to something enormous so they can always see the depot the number of bits of rubbish works out.
However if I allow them to move around with a radius of 1, the count of rubbish is much higher than 1 in 10,000.
Why would that make a difference?
Thanks
Using the below code I get agents are like this:
to setup
ask breadth-patches [sprout-walls wall-agents[set color 2]]
ask length-patches [sprout-walls wall-agents[set color 2]]
ask gap-patches [sprout-walls wall-agents[set color 2]]
ask length-patches[align-inside-at-top]
ask breadth-patches [align-inside-at-right-left]
ask gap-patches[align-inside-at-top]
end
to align-inside-at-top ;; patch procedure
let counter count walls-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop 0
if-else(pycor < 0)[set ytop pycor - .5 - half-gap]
[set ytop pycor + .5 - half-gap]
let xleft pxcor - .5 - half-gap
ask walls-here
[ set size gap1
set ycor ytop
set xcor xleft + gap1 * counter
set counter counter - 1 ;; so we're placing them from right to left
; set ycor ycor + 0.125
]
]
end
to align-inside-at-right-left ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop pycor + .5 + half-gap
let xleft 0
if-else (pxcor < 0)[
set xleft pxcor + .5 - half-gap]
[ set xleft pxcor - .5 + half-gap
]
ask turtles-here
[ set size gap1
set ycor ytop - gap1 * counter
set xcor xleft ;+ gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
Note: The gap in the rectangle is due to the following code
ask patches with [pxcor > (gap * (-1)) and pxcor < gap and pycor =(breadthrec - 1)][ask walls-here[die]]
Here, gap = 1 ,i.e, a width of 1 patch.
So the input parameter is the wall-agents which specifies the number of agents to created per patch along the length and breadth patches.
I wish to change to create overlapping agents as in the figure below(Sorry the figure is not so perfect, but I hope it explains it). Please help on how to achieve this.
This is a lot of code to ask anybody to debug for you.
I would suggest solving a simpler version of the problem first. Do you have code that can create wall-agents turtles in a single patch, evenly spaced along a line? Once you had a working code that did that, then you could attempt to generalize it to your more complex problem.
If you run into trouble writing that simpler version, you'll have a smaller question to ask here on Stack Overflow that will be much easier for someone to answer than your current, very large question.
If you are able to write the simpler version, don't throw it away — keep it, so you can go back to it if you need to. Then tackle the bigger problem.
You might even be able to take the simpler version, put it into a procedure, and then call that procedure from your larger solution. Making small procedures that work, and then calling those smaller procedures from other ones, is often a good way to break a problem down into manageable parts.