question about sum and arrays

Use this forum to post Vensim related questions.
Post Reply
ebr567
Member
Posts: 36
Joined: Sat Mar 14, 2015 11:26 am
Vensim version: DSS

question about sum and arrays

Post by ebr567 »

I have a few questions related to number arrays.

Question 1. What is the syntax for taking a partial sum of an array? I would expect something like SUM(array[i!],min_index,max_index)

I am including my mdl file for the next few questions.

Question 2. I want my array to update only when a certain value is non zero. Am I using the IF THEN ELSE syntax correctly in the variable "FailureTable"?

Question 3. The variable "FailureTable" is an array of 8 values. When "breed" is non-zero, these values should shift up one index and be multiplied by the FailureRate. The first index is filled with a different function. Again, I'm not sure my syntax is correct in the variable "FailureTable".

Question 4. I would like to do a weighted sum to compute the variable "Pregnant Feed Requirements". I'm not sure how to 'index' the queue fifo variable "pregnant ewes" so I can perform the weighted sum.



I have tried to use comments on these variables to describe further what is happening. Please feel free to ask any questions you might have.

Thank you!
Attachments
simpleewesAug5.mdl
(8.34 KiB) Downloaded 181 times
Administrator
Super Administrator
Posts: 4593
Joined: Wed Mar 05, 2003 3:10 am

Re: question about sum and arrays

Post by Administrator »

What is the syntax for taking a partial sum of an array? I would expect something like SUM(array[i!],min_index,max_index)
You will need to create an array index parameter,
i ID = i

Then
sum ( if them else ( i ID[i!] >- min_index :and: i ID[i!] <= max_index, array[i!] , 0 ) )

Question 2. I want my array to update only when a certain value is non zero. Am I using the IF THEN ELSE syntax correctly in the variable "FailureTable"?

I cannot decipher what you are attempting to do here so this is a guess. I think you need SAMPLE IF TRUE
failuretable[Failure] = SAMPLE IF TRUE ( breed >= 0.0001 , failuretable[failure]*failurerate )

Question 3. The variable "FailureTable" is an array of 8 values. When "breed" is non-zero, these values should shift up one index and be multiplied by the FailureRate. The first index is filled with a different function. Again, I'm not sure my syntax is correct in the variable "FailureTable".

No it's not. Can you explain why you need to do this and I'll see if I can think of a way?

Question 4. I would like to do a weighted sum to compute the variable "Pregnant Feed Requirements". I'm not sure how to 'index' the queue fifo variable "pregnant ewes" so I can perform the weighted sum.

Do you mean to use the following?
SUM( Pregnant Ewes[pregage!] * FEED BY AGE IN QUEUE[pregage!])
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: question about sum and arrays

Post by tomfid »

What is the syntax for taking a partial sum of an array? I would expect something like SUM(array[i!],min_index,max_index)
I avoid this kind of for-loop logic as much as possible.

If the partial sum is over some predictable subset, use a subrange, e.g.

Code: Select all

range : a,b,c,d,e,f
subrange : a, d, f
y = SUM( x[subrange!] )
If you do need y = SUM( x[ elements up to i ] ), you can use admin's solution. The index parameter can be wrapped in an INITIAL for efficiency, i.e.

Code: Select all

i ID[i] = INITIAL( i )
You can also use VECTOR SELECT for this kind of thing.

A faster (but slightly harder) solution is to use mapping, like:

Code: Select all

range : a,b,c,d,e,f
lowElements : a,b,c,d,e -> highElements
highElements : b,c,d,e,f -> lowElements
y[a] = x[a]
y[high elements] = y[lowElements] + x[highElements]
ebr567
Member
Posts: 36
Joined: Sat Mar 14, 2015 11:26 am
Vensim version: DSS

Re: question about sum and arrays

Post by ebr567 »

Hello thank you for the fast reply.

Question 1: Thank you. That is what I was looking for.

Questions 2 & 3:
Administrator wrote:I cannot decipher what you are attempting to do here so this is a guess. I think you need SAMPLE IF TRUE
failuretable[Failure] = SAMPLE IF TRUE ( breed >= 0.0001 , failuretable[failure]*failurerate )
No it's not. Can you explain why you need to do this and I'll see if I can think of a way?
I have a group of animals that enter a breeding pool. 30 days later X of them have bred and Y have not. The ones that breed move through the rest of the model and 250 days later the successful X, the unsuccessful Y, and new group Z animals enter the breeding pool. Some percent will breed, and some percent will fail. I need to track the number that fail for the 2nd-8th time because we'll be removing animals that fail to breed N times from the population. (where N is an input number between 2-8). I'm doing it statistically by creating an array variable that retains the number of animals that failed to breed in the previous cycle. Each time a new breed pool is populated I update my array variables to tell me how many animals are expected to fail for their Nth time in the present breeding cycle.

Administrator wrote:
Question 4. I would like to do a weighted sum to compute the variable "Pregnant Feed Requirements". I'm not sure how to 'index' the queue fifo variable "pregnant ewes" so I can perform the weighted sum.
Do you mean to use the following?
SUM( Pregnant Ewes[pregage!] * FEED BY AGE IN QUEUE[pregage!])
That is what I would like, can I really access the content of a queue fifo with a simple index? If so, this makes life much easier. Also, if so, does that index start at 1 or 0, and does the queue fifo interpolate for a non-whole number index?
ebr567
Member
Posts: 36
Joined: Sat Mar 14, 2015 11:26 am
Vensim version: DSS

Re: question about sum and arrays

Post by ebr567 »

tomfid wrote:
What is the syntax for taking a partial sum of an array? I would expect something like SUM(array[i!],min_index,max_index)
I avoid this kind of for-loop logic as much as possible.

If the partial sum is over some predictable subset, use a subrange, e.g.

Code: Select all

range : a,b,c,d,e,f
subrange : a, d, f
y = SUM( x[subrange!] )
If you do need y = SUM( x[ elements up to i ] ), you can use admin's solution. The index parameter can be wrapped in an INITIAL for efficiency, i.e.

Code: Select all

i ID[i] = INITIAL( i )
Hi Tom,

It is a predictable subset, but the size of the subset is set by an excel lookup.

So for example, if the variable in excel says, max_index= 4, then I will want the subrange to be subrange: a,b,c,d. Since I don't know how to set a subrange based on an input variable, I assume that I should probably use

Code: Select all

 sum ( if then else ( i ID[i!] >=1 :and: i ID[i!] <= max_index, array[i!] , 0 ) ) 
Administrator
Super Administrator
Posts: 4593
Joined: Wed Mar 05, 2003 3:10 am

Re: question about sum and arrays

Post by Administrator »

ebr567 wrote:I have a group of animals that enter a breeding pool. 30 days later X of them have bred and Y have not. The ones that breed move through the rest of the model and 250 days later the successful X, the unsuccessful Y, and new group Z animals enter the breeding pool. Some percent will breed, and some percent will fail. I need to track the number that fail for the 2nd-8th time because we'll be removing animals that fail to breed N times from the population. (where N is an input number between 2-8). I'm doing it statistically by creating an array variable that retains the number of animals that failed to breed in the previous cycle. Each time a new breed pool is populated I update my array variables to tell me how many animals are expected to fail for their Nth time in the present breeding cycle.
I'm not sure this is how I would approach this in Vensim.

Have a look at the tubs example in Vensim, this involves water flowing from one tub to another (similar to an ageing chain). There is also an ageing chain model in the documentation.

And the following might also be useful if you are a DSS user,
http://www.systemdynamics.org/conferenc ... /P1064.pdf
Advice to posters seeking help (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391

Units are important!
http://www.bbc.co.uk/news/magazine-27509559
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: question about sum and arrays

Post by tomfid »

I might do this the old-fashioned way, with an explicit chain of stocks rather than arrays. It might be a little more work to set up, but it's more visible.
tomfid
Administrator
Posts: 3811
Joined: Wed May 24, 2006 4:54 am

Re: question about sum and arrays

Post by tomfid »

Also, this might be a good Ventity model. You'd make each breeding stock with N failures an entity, and create the N needed entities automatically via an action.
Post Reply