Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Use this forum to post Vensim related questions.
Post Reply
jrissman
Senior Member
Posts: 95
Joined: Wed Oct 16, 2013 6:04 pm
Vensim version: DSS

Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by jrissman »

I wish to use the ALLOCATE AVAILABLE function to allocate a single quantity of items into buckets differentiated by two subscripts. The function sorts the items only into the buckets characterized by the first subscript and fills the other buckets to their capacities, which introduces items into the system from nowhere.

To clearly explain the issue, I have developed a small model that demonstrates the problem. In this model, I have a quantity of marbles that I want to sell. They are being sorted into four bins. The four bins are differentiated by two subscripts. The first is "Color" (red or blue) and the second is Shape (round or square). Each of the four bins has a capacity for marbles. There is also a price per marble, which varies by bin. (Marbles added to some bins are sold at higher prices than marbles added to other bins.) I also set up a Priority Profile subscript with the typical four values (ptype, priority, pwidth, pextra). For all four bins, I'm using a normal distribution shape with integer-only allocation (ptype=13) and a standard deviation of 1 (pwidth=1). I use ALLOCATE AVAILABLE in the usual way, but showing the "Color" and "Shape" subscripts. Here are some screenshots showing the setup and the key results. (The actual model is attached, if you want to run it yourself or to view any part of it that I didn't include in a screenshot.)
Model structure, subscripts, and results
Model structure, subscripts, and results
StructureAndResults.PNG (35.69 KiB) Viewed 4788 times
Equation inside the "Marbles by Bin" variable
Equation inside the "Marbles by Bin" variable
MarblesByBinEquation.PNG (34.92 KiB) Viewed 4788 times
The numerical results of running this model are shown as part of the first screenshot. You can see that we have a total of 100 marbles to sell. The red, round bin has a capacity of 80 marbles. The red, square bin has a capacity of 120 marbles. The blue round bin has a capacity of 65 mables. The blue, square bin has a capacity of 30 marbles. The prices for which you sell marbles that end up in these bins are 3, 4, 3.5, and 4.5 respectively.

Vensim sorts all 100 marbles into the red, round and red, square bins (giving the red, round bin 22 marbles and the red, square bin 78 marbles). Then the two blue bins are given a number of marbles equal to the capacity of those bins. In total, we end up with 195 marbles, even though we only had 100 marbles to sell.

The correct behavior would be for ALLOCATE AVAILABLE to sort the 100 marbles into all four bins, given the four bins' different costs per marble and different capacities for marbles.

In my actual model, the Energy Policy Simulator, I work around this limitation by laboriously copying values from things that are in two subscripts into one giant subscript. Then I run the values through ALLOCATE AVAILABLE. Then I laboriously copy the values from the giant subscript back into a variable with two subscripts. From there, the values go on to be used in other parts of the model. The need to manually move items into a single, long subscript and back out again means that I have to add or remove equations in the model whenever I add or remove an item from one of the subscripts, instead of the model handling it gracefully in a data-driven way (as it does for most equations when you add elements to or remove elements from a subscript).

Do you think it would be possible to enable ALLOCATE AVAILABLE to allocate items into bins across multiple subscripts, rather than allocating items across only the first subscript and producing wrong answers for later subscripts?

Jeff
Attachments
MarbleModel.mdl
The model file (.mdl format)
(2.62 KiB) Downloaded 199 times
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by Administrator »

The only way to do it at present is to create a dummy subscript range, copy all elements to that, allocate and then copy back into the 2d parameter.

The function works on the last subscript range (in this example, shape). As "marbles to sell" is not subscripted by shape, all 100 gets allocated on each call of the function (RED and BLUE).

The way you need it in this example is to allocate from a constant to 2D. We could implement some more functions, but where would we stop?

ALLOCATE AVAILABLE CONSTANT TO 1D
ALLOCATE AVAILABLE CONSTANT TO 2D
ALLOCATE AVAILABLE 1D TO 2D
ALLOCATE AVAILABLE 2D TO 2D
etc.

I don't have any suggestions for you at the moment other than to convert the 2D to a 1D, allocate, then copy 1D back to 2D. This should be straightforward using a combination of SUM and IF THEN ELSE.

Another alternative is to look to linear programming, http://vensim.com/workbench/#lp-solve-link
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: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by tomfid »

This is actually not a bug, because it's the specified behavior of the function, but I agree that it would be useful. I think what we actually need is a more general linear programing approach to such things. That can be done with the lp_alloc link, http://vensim.com/workbench/, though with significant work.
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by tomfid »

But, back to your real question, it's definitely possible. It just requires a lot of planning, because every time we introduce something like this, it has side effects on a lot of infrastructure, like the external function system.
jrissman
Senior Member
Posts: 95
Joined: Wed Oct 16, 2013 6:04 pm
Vensim version: DSS

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by jrissman »

Thanks for the discussion. I just wanted to bring this limitation to your attention, because it requires a troublesome work-around in the Energy Policy Simulator. I do copy everything from 2D to 1D, allocate, and then copy back from 1D to 2D. But I need to keep track of all the elements separately- I can't SUM() any of them - so it needs a new, huge subscript range that contains a number of elements equal to the number of elements in each of the two original subscripts multiplied by each other, and it needs an equation for each of the elements in the huge subscript range in each place that the conversion needs to be done (three input variables and one output variable for each allocation). And I must do more than one such allocation in the model. In total, this amounts to hundreds of equations whose only purpose is to map things from 2D to 1D and back again. You are right- the mapping process is straightforward- but it is also laborious and makes the model harder to update.

Linking to a linear programming solver is an interesting idea. However, it isn't suitable for my use case, because (1) my model runs in a web interface using the Linux multi-context shared object version of Vensim, which would need additional development work to interface with lp_solve, and (2) my model needs to be able to be downloaded and run by novice users in Vensim Model Reader. These two issues are harder to surmount than just laboriously doing the 2D to 1D mapping, allocating, and mapping 1D back to 2D.

A more generalized way to specify which subscripts things should be allocated into (perhaps by marking them with exclamation points in the arguments to ALLOCATE AVAILABLE, a convention used in other vector functions) would be one way to make the existing ALLOCATE AVAILABLE function more helpful and powerful. One nice thing about this approach is that it doesn't change the way Vensim would handle any existing models, because no existing models use exclamation points inside the arguments to ALLOCATE AVAILABLE function calls. Another nice thing is that it could leverage the way ALLOCATE AVAILABLE already works with minimal infrastructure changes, because Vensim itself can simply perform the XD to 1D mapping, run the allocation, and then map everything back from 1D to XD. A more general linear programming approach to such questions, as Tom suggests, might be even more powerful, though that might be more development work.

Anyway, just something to consider. Thanks for taking the time to think about it! :)
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by Administrator »

I like the exclamation marks logic, it makes complete sense. I'm not sure what the implications for the software are though so I'll raise it as an enhancement in our internal bug tracking system.

I've attached a model that does the mapping as I'm not sure how you've done it. Updating this model should be easy, as long as the "dummy" subscript range is larger than the number of colors * number of shapes. And a warning message is displayed if the dummy range is not the same size (but it could easily be changed to show the warning if the number of dummy elements was less than the colors/shapes.
Attachments
MarbleModel(Tony).mdl
(6.14 KiB) Downloaded 194 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
tomfid
Administrator
Posts: 3804
Joined: Wed May 24, 2006 4:54 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by tomfid »

Speaking of dummy ranges, one way to handle the proliferation of dimensions would be to create a 2D x 2D function. Then users could insert a dummy dimension ( one : one1 ) on one side when they wanted 2D x 1D, etc. Making the number of loops a parameter might also be possible, but tricky in the compiler.

I can't think of a case where I've wanted 3 dimensions of allocation with a commodity. That usually arises in noncommodity situations, like M suppliers serving N demanders through K channels, with different markups for each channel. Similarly, you might have M suppliers shipping to N demanders, with an MxN shipping cost matrix. You can handle those with FIND ZERO, but it's usually overkill and can be numerically fragile. A built-in LP-based function would be a lot better.
jrissman
Senior Member
Posts: 95
Joined: Wed Oct 16, 2013 6:04 pm
Vensim version: DSS

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by jrissman »

Thanks for putting together the modified version of the Marble Model, Tony. It is using techniques that I have not used in the Energy Policy Simulator and would like to investigate, to see if I can make that part of the model automatically adaptive to changes in numbers of elements in the subscripts (like number of colors or shapes in the Marble Model). I definitely prefer your more automated approach to my approach of manually mapping things between 2D and 1D.

Unfortunately, when I try to run your modified version of the Marble Model in Vensim DSS 6.3G, I get an error:
Error on trying to run Tony's modified Marble Model
Error on trying to run Tony's modified Marble Model
MarbleModelRunError.PNG (5.54 KiB) Viewed 4754 times
Despite this error message, it appears that the model actually has completed its run and produced numerical results for all timesteps. After I dismiss the error, I can view the data in each variable using the "Table" tool. Do you get this error when you run the model on your computer? I would want to avoid generating an error at runtime, even if the model actually has executed successfully, to avoid frightening and confusing new users who download the model and try to run it.
Administrator
Super Administrator
Posts: 4573
Joined: Wed Mar 05, 2003 3:10 am

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by Administrator »

I put that message in for a reason
And a warning message is displayed if the dummy range is not the same size (but it could easily be changed to show the warning if the number of dummy elements was less than the colors/shapes.
Have a look at "check var msg". and either change it so that it displays when "NUMBER OF DUMMY < NUMBER OF COLORS * NUMBER OF SHAPES", or change the number of elements in the dummy range.
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
jrissman
Senior Member
Posts: 95
Joined: Wed Oct 16, 2013 6:04 pm
Vensim version: DSS

Re: Bug Report: Allocate Available Produces Incorrect Results with 2+ Subscripts

Post by jrissman »

Oh, right! Sorry, by the time I was done playing with your version of the Marble Model, I had forgotten that you had mentioned in your post that you included a warning message!

Thanks for making this example model- I will look into using this mapping approach in the Energy Policy Simulator. Much appreciated. :-)
Post Reply