How to make the agent pick the highest value between two values? - max

guys. I created this procedure in NetLogo for my agents (farmers):
to calculate-deforestation
ask farmers [
set net-family-labor ( family-labor - ( ag-size * cell-labor-ag-keep ) )
set net-family-money ( family-money - ( ag-size * cell-cost-ag-keep ) )
ifelse net-family-labor < 0 or net-family-money < 0
[ set n-aband-cell-labor ( family-labor / cell-labor-ag-keep )
set n-aband-cell-money ( family-money / cell-cost-ag-keep )
set n-aband with-max [ n-aband-cell-labor n-aband-cell-money ]
]
[ set n-def-cell-labor ( net-family-labor / cell-labor-deforest )
set n-def-cell-money ( net-family-money / cell-cost-deforest )
set n-def with-min [ n-def-cell-labor n-def-cell-money ]
]
]
end
For the "n-aband", I would like to get the max value between "n-aband-cell-labor" and "n-aband-cell-money" (either one or the other; the same goes for "n-def"). I know a limited number of NetLogo primitives but the ones I was able to find do not work for my case, for instance, "with-max", "max-n-of", "max-one-of". I am sure there must be one that would work but I am having trouble finding it in the NetLogo dictionary. I wonder if anyone could suggest me one that could work for my case. Thank you in advance.

If you want to get the max value of a list, simply use max. So,
set n-aband max (list n-aband-cell-labor n-aband-cell-money )
will set n-aband to the highest of the two values.

Related

Performance enhancement in DAX query

I have Power BI DAX query used in a measure. It takes longer time to execute. Can anyone please help me with this?
MEASURE FACT_CONSOL_BALANCE_OL[Measure 4] =
SWITCH (
TRUE (),
CONTAINS (
DIM_ANALYTIC_STRUCT_ACCOUNT,
DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "1 - CURRENT ASSETS"
), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
CONTAINS (
DIM_ANALYTIC_STRUCT_ACCOUNT,
DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "2 - NON - CURRENT ASSETS"
), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1
)
Performance Result on DAX Studio:
Can you please try this code, see if It solves your problem. I tried to write it without contains() function.
MEASURE FACT_CONSOL_BALANCE_OL[Measure 4] =
SUMX (
FACT_CONSOL_BALANCE_OL,
VAR Balance =
SWITCH (
RELATED ( DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)] ),
"1 - CURRENT ASSETS", FACT_CONSOL_BALANCE_OL[BALANCE],
"2 - NON - CURRENT ASSETS", FACT_CONSOL_BALANCE_OL[BALANCE],
FACT_CONSOL_BALANCE_OL[BALANCE] * -1
)
RETURN
Balance
)

Netlogo How to avoid NOBODY Runtime error?

How can I avoid NOBODY Runtime error? The following is sample code. This is a code that can be used for zero division error avoidance. Therefore, I know that it can not be used to avoid NOBODY error. But I can not find any other way. The following is a Runtime error message-> "IFELSE-VALUE expected input to be a TRUE / FALSE but got NOBODY instead." I appreciate your advice.
set top ifelse-value (nobody)
[ 0 ][ top ]
set ts turtles with [speed = 0 and not right-end]
set top max-one-of turtles [who]
set topx [xcor] of top ; A NOBODY error appears in "of" of this code
set L count ts with [xcor > topx]
The input of ifelse-value needs to be a reporter the returns either true or false (full details here. So, if you use nobody as the input, Netlogo does not evaluate whether or not the input is nobody or not, it just reads nobody- in other words your input is not returning either true or false.
For input, then, you need to instead use a boolean variable (one that is either true or false), a to-report that returns true or false, an expression that Netlogo can evaluate, etc. Consider the following examples:
to go
let x true
set top ifelse-value ( x )
["x is true"]
["x is NOT true"]
print ( word "Example 1: " top )
set top ifelse-value ( this-is-true )
["Reporter returned true"]
["Reporter did not return true"]
print ( word "Example 2: " top )
set x nobody
set top ifelse-value ( x = nobody )
["x IS nobody"]
["Is NOT nobody"]
print ( word "Example 3: " top )
set x 0
set top ifelse-value ( x = nobody )
["x IS nobody"]
["x Is NOT nobody"]
print ( word "Example 4: " top )
set top ifelse-value ( nobody = nobody )
["nobody = nobody"]
["nobody != nobody"]
print ( word "Example 5: " top )
end
to-report this-is-true
report true
end

Can i do this code python with snowball?

The word length is 5. I want to delete the letter in position 0 and the letter in position 3
with python seems like this :
word = word[1:3] + word[4] #this is with python
The question is, How i can do it with snowball ?
Maybe the solution seems like this:
do(
[substring] among (
$p1 ($word_len == 5 delete)
)
markto $p2
[substring] among (
'cha' (atmark==$p2 delete)
)
)

Stretching words and quotation scoping

To play at Stretch the word, I've defined the following words, to try to work at the problem via the same method as this answer:
USING: kernel math sequences sequences.repeating ;
IN: stretch-words
! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter nip ;
! expected: "bonobo" -> "bonoobbooo"
! actual: "bonobo" -> "bbbooonnnooobbbooo"
: stretch-word ( string -- stretched )
dup ascend-string swap zip
[
dup first swap last
[ = ] curry [ dup ] dip count
repeat
] map last ;
stretch-word is supposed to repeat a character in a string by the number of times it's appeared up to that position in the string. However, my implementation is repeating all instances of the 1string it gets.
I have the feeling this is easily implementable in Factor, but I can't quite figure it out. How do I make this do what I want?
Hm... not a great golf, but it works...
First, I made a minor change to ascend-string so it leaves the string on the stack:
: ascend-string ( string -- string ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter ;
So stretch-word can work like this:
: stretch-word ( string -- stretched )
ascend-string zip ! just zip them in the same order
[
first2 over ! first2 is the only golf I could make :/
[ = ] curry count ! same thing
swap <array> >string ! make an array of char size count and make it a string
] map concat ; ! so you have to join the pieces
Edit:
I think the problem was using repeat to do the job.
: ascend-string ( string -- seqs )
"" [ suffix ] { } accumulate*-as ;
: counts ( string -- counts )
dup ascend-string [ indices length ] { } 2map-as ;
: stretch-word ( string -- stretched )
[ counts ] keep [ <string> ] { } 2map-as concat ;
"bonobo" stretch-word print
bonoobbooo
indices length could also be [ = ] with count

NetLogo - nelson-winter entrepreneur model works but very slow - can it be sped up?

I've been working on a NetLogo model simulating entrepreneurs based on Nelson-Winter model. In the model, every tick there is a Demand and Supply for some product,then the price=Demand/Supply, so every tick the entrepreneurs have a chance to set up a company and make money, the probability of setting uo a companies is effected by the revenue of other companies set up by the entrepreneurs in the same group, entrepreneurs in the same group share the same gamma and lambda,gamma decide the proportion of profit invested in R&D(innovation and imitation),the lambda decided the proportion of R & D funding invested in the innovation. The success of RD will enhance the production.Finally it will returned the result of companies averaged by group, the companies in the same group will have the same companyNO and same gamma and lambda.
This process seems to work fine but it runs exceedingly slow, to the point that I'm worried I won't be able to feasibly run many simulations. Any ideas on how to speed up this process? In the earlier version model without group the model works really fast, so I think the problem lies in the grouping. The model code is getting pretty long, so I just added the part related to the grouping. I can provide more code to someone if the problem seems to lie elsewhere. Any help would be much appreciated!
globals[
S;;aggregate supply
SafterDie
D;;aggregate demand
P;;price of demand,P=D/S
g b eta;;used for get demand
file1
file2
totalGamma
totalLambda
NO
]
breed [entrepreneurs a-entrepreneur]
breed [companies a-company]
entrepreneurs-own[
humanCapital
timesOfChuangYe
chuangYe?;;chuang ye mei de
gaiLv
renGamma
renLambda
renNO
]
companies-own[
humanCapital
tempHumanCapital;;20140516
oldHumanCapital
initialCap
initialTech
companiesNO
capitalK;;capital K,nedd money c*K
gamma;;money uesd for R&D
lambda;;money used for innovation in terms of imitation
lifeLength
RecentRevenue;;profit every tick
production;;production of the tick
profit
accumulatedInnovation
accumulatedImitation
]
to setup
foreach n-values NoOfgroup [?] [
set bigGamma (random-in-range 0.4 1);;money uesd for innovation
set bigLambda (random-in-range 0.1 1);;money used for innovation VS imitation
set NO (? + 1)
create-entrepreneurs numberOfentreprenursInEveryGroup [ entrepreneur-setup ]
]
to entrepreneur-setup ;; turtle procedure
set color red
set shape "line";;line
move-to one-of patches
set humanCapital random-in-range 0 1
set chuangYe? 0;;if Entrepreneurship,0 no,1 yes
set timesOfChuangYe 0;;Entrepreneurship times
set renGamma bingGamma
set renLambda bingLambda
set renNO NO
end
to ifChuangYe
ask entrepreneurs with [chuangYe? = 0]
[
let node1 self
set gaiLv 0;;probability of entrepreneurship
getProbabilitiOfEntrepreueur node1
if (random-float 1 <= gaiLv)[
set chuangYe? 1
set timesOfChuangYe (timesOfChuangYe + 1)
hatch-companies 1 [
set color yellow
set shape "house"
create-link-with node1 [set color yellow]
set humanCapital ([humanCapital] of node1);;!!!
set companyNO ([renNO] of node1)
set gamma ([renGamma] of node1);;money uesd for RD
set lambda ([renLambda] of node1);;money used for innovation VS imitation
set initialTech humanCapital
set tempHumanCapital humanCapital
set oldHumanCapital humanCapital
company-setup
]
]
]
end
to getProbabilitiOfEntrepreueur [node1]
ask node1[
let number renNO
let co1 no-turtles
let en1 entrepreneurs with [renNO = number];;get entrepreneur
ask en1 [
set co1 link-neighbors;;get company
]
let total 0
ask co1 [;;to link
if lifeLength != 0 [
let k lifeLength
let singleRevenue 0
if lifeLength >= 6 [set k 6]
foreach n-values k [?] [
set singleRevenue (singleRevenue + (item ? RecentRevenue)/ (? + 1))
];;RecentRevenue is a list to collect a company's profit of every tick
set total total + singleRevenue
]
]
set gaiLv (1 / 50 * total + 5 / 100)
;;show gaiLv
]
end
to imitation
let newH 0
ask companies[
set accumulatedImitation (accumulatedImitation + (1 - lambda)* gamma * profit)
if accumulatedImitation >= 0.3 * (capitalK ^ 3) [
let co2 companies with [companyNO = companyNo]
set newH max ([humanCapital] of co2)
if newH > humanCapital [set humanCapital newH set imNO imNO + 1]
set accumulatedImitation (accumulatedImitation - 0.3 * (capitalK ^ 3))
]
]
end

Resources