Keep a subscript element name attached to its value after a VECTOR REORDER?

Use this forum to post Vensim related questions.
Post Reply
Brunilde
Junior Member
Posts: 7
Joined: Tue Jul 14, 2020 11:20 am
Vensim version: PRO

Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Brunilde »

Hi everyone,

I am creating a sub-model that is prioritizing the elements of a subscript, and I'd like the element's name to remain securely tied to the values they will represent in different variables.

Here is an easy analogy: There is several types of candies at the store and you want to try as many types as possible with a limited amount of money. Since each candy has its own type (subscript "Candy type" with elements candy1 to candy4)) and its own cost (subscripted variable "candy cost" with four different prices), you want to start with the cheapest and add as many more types as possible, in an increasing order.

To do this I have sorted the variable "candy cost" by increasing price with a VECTOR SORT ORDER followed by a VECTOR REORDER. I then do a cumulative SUM (I found the trick for this on the forum, but am open to more solutions for that as well) on the reordered candies, to know how many different types I can buy. But, here is the problem, I have lost their initial type affiliation in the REORDER process, and I need to know the type of the candies I decided to buy.

I was wondering if there is a standard and appropriate way to do this (re-linking values of a subscripted variable to their initial affiliation after having been reordered). Sorry if this was already asked!

Thank you very much in advance!!

P.S: In real I try to represent social, technical and political dimensions of the energy transition. Cheers!
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Administrator »

It would really help if you uploaded a model demonstrating what you are trying to do (and where it is going wrong). We would need to take the time to generate it ourselves from your description, but there is a good chance we would get it wrong.
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
Brunilde
Junior Member
Posts: 7
Joined: Tue Jul 14, 2020 11:20 am
Vensim version: PRO

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Brunilde »

Hi, thank you for the quick reply. My question is really about how to avoid the "reshuffling" that the function VECTOR REORDER operates between the subscripts elements and the values it sorted. On the help page (https://www.vensim.com/documentation/fn ... _order.htm ) it is indeed written:
"Note that it is not really necessary to define scompany in this example. However, the order of values in sorted rev is no longer by company so it prevents possible confusion to use a different subscript range."
This seems to confirm what I observed. The order of the subscripts elements itself doesn't change, but the values are reorganized by the Vector reorder, so they do not match anymore. I was wondering if this was a known problem and how it was usually dealt with.

If still needed, or just for the pleasure to receive a bespoke help, I will also upload a simple model later :) Thank you very much!
tomfid
Administrator
Posts: 3806
Joined: Wed May 24, 2006 4:54 am

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by tomfid »

There's no way to sort the subscript element names themselves. For computational purposes, you can access everything you need numerically (I assume you've seen the sample models). Do you need something special for display purposes?
Brunilde
Junior Member
Posts: 7
Joined: Tue Jul 14, 2020 11:20 am
Vensim version: PRO

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Brunilde »

Hi both, and thank you for your answers. Here is a simple model on the candy example.
essai1507.mdl
(2.7 KiB) Downloaded 171 times
My goal is to know the name of the type of candies I was able to buy. The problem arises at the VECTOR REORDER stage. As the name of the candies is lost, the results of the variable "candy is bought" have no coherent meaning.

Step by step:
1) candy price for each element of the subscript "candy"
2) prices are given a "rank" (sort order)
3) prices are reordered by increasing value
4) each element is attributed a cumulative cost (the candy itself + all cheaper candies)
5) this is compared to the total money available : if the cumulative cost for an element is <= total money available then "candy is bought" is activated (which doesn't work right because of REORDER)

Thank you heaps in advance :mrgreen:

Brunilde
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Administrator »

Try the attached model.

I've ranked the candy in order or price and used that to work out the cumulative cost based on this rank. Then a simple IF THEN ELSE to work out if it was purchased or not.
Attachments
essai1507_modified.mdl
(2.31 KiB) Downloaded 161 times
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
Brunilde
Junior Member
Posts: 7
Joined: Tue Jul 14, 2020 11:20 am
Vensim version: PRO

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Brunilde »

Thank you very much, it works very well!!! I had tried to use the VECTOR RANK before, but as I didn't find the solution for the cumulative sum then, I thought I needed to reorder the vector with SORT ORDER and REORDER to do so.

Just to be really certain I understand well and can reuse it, would you mind explaining the equation below in plain english? Does "candy price ranked in order of price[candy!]" represents the sum of the elements in this context? I struggle to find similar examples in the help, and I know it might help others to understand well the possibilities to do cumulative SUMs.

SUM (
IF THEN ELSE ( candy price ranked in order of price[candy!] <= candy price ranked in order of price[candy]
, candy price[candy!]
, 0 )
)

Thank you and have a nice day!
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Administrator »

I'll try and explain.

"candy price ranked in order of price" is the order in terms of price that the candy costs. CHOCO is the cheapest, so it's value is 1, BANANA is 2.

The following is slightly trickier to explain.

SUM (
IF THEN ELSE ( candy price ranked in order of price[candy!] <= candy price ranked in order of price[candy]
, candy price[candy!]
, 0 )
)

It might be easier to explain with an example. Say we look at CHOCO, it's the cheapest (so the ranked candy price is 1). Expanding the sum and replacing "candy price ranked in order of price[candy]" with it's value (1) gives

if then else ( candy price ranked in order of price[CHOCO] <= 1 , candy price ranked in order of price[CHOCO] , 0 )
+ if then else ( candy price ranked in order of price[CARAMEL] <= 1 , candy price ranked in order of price[CARAMEL] , 0 )
+ if then else ( candy price ranked in order of price[LEMON] <= 1 , candy price ranked in order of price[LEMON] , 0 )
+ if then else ( candy price ranked in order of price[BANANA] <= 1 , candy price ranked in order of price[BANANA] , 0 )

Only the first statement matches as only "candy price ranked in order of price[CHOCO]" is <= 1.

Expanding for CARAMEL gives
if then else ( candy price ranked in order of price[CHOCO] <= 3 , candy price ranked in order of price[CHOCO] , 0 )
+ if then else ( candy price ranked in order of price[CARAMEL] <= 3 , candy price ranked in order of price[CARAMEL] , 0 )
+ if then else ( candy price ranked in order of price[LEMON] <= 3 , candy price ranked in order of price[LEMON] , 0 )
+ if then else ( candy price ranked in order of price[BANANA] <= 3 , candy price ranked in order of price[BANANA] , 0 )

Does this make sense? I'm summing only the values that have a rank less than or equal to the rank of the candy being calculated.
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
Brunilde
Junior Member
Posts: 7
Joined: Tue Jul 14, 2020 11:20 am
Vensim version: PRO

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Brunilde »

Yes, it does make sense, thanks a lot for the detailed explanation. I was not aware these kind of exceptions could be embeded relatively easily in functions dedicated to subscripts such as SUM.

The first sign "!" of the equation here simply means that all the values of the subscripted variable are sequentially compared to a single value of the same subscripted variable. It is really useful and I hope it will help many others like it helped me :)

Also many thanks for the answer to the initial question of the topic, which is, if you need to keep the subscripts elements linked to their values, find another way, don't use VECTOR REORDER. (^^).

All best,
Brunilde
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Keep a subscript element name attached to its value after a VECTOR REORDER?

Post by Administrator »

Vensim does not have a traditional loop arrangement (eg, for i = 0 to 10). But you can use SUM/VMIN/VMAX to loop over a subscripted parameter.
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
Post Reply