Page 3 of 4

Posted: Fri Sep 03, 2010 4:21 pm
by khalediwm
Could Bob or anyone please help me to in resolving this issue? Any help is highly appreciated as I am stuck in linking Matlab and Vensim for more than a week.

Posted: Fri Sep 03, 2010 6:36 pm
by tomfid
Is there a typo in your matlab code at str=('SPECIAL>loadModel|C:\Vensimfloder\models\sample\'testlink.vpm') (extra ' before testlink)? Is "vensimfloder" a typo?

It seems likely that there's some error in the matlab code, but no one here is a matlab expert, so you may do better on a matlab forum.

Tom

Posted: Fri Sep 03, 2010 7:18 pm
by khalediwm
Thanks Bob for your quick response. Actually I created 'vensimfolder' to install both Vensim DSS and Model Reader. Is it OK?

Posted: Fri Sep 03, 2010 8:07 pm
by tomfid
Out of curiousity, what are you trying to do here, i.e. what is the modeling purpose of calling Vensim from Matlab?

Posted: Fri Sep 03, 2010 8:10 pm
by tomfid
Your path C:\Vensimfloder\models\sample\ would be OK in XP - I don't know about Win7. But is it spelled Vensimfloder or Vensimfolder? You may have a typo.

Also, it appears that you have an extra ' in your model path, just before testlink: C:\Vensimfloder\models\sample\'testlink.vpm

Posted: Fri Sep 03, 2010 9:03 pm
by khalediwm
Hi Bob
Actually its a typing error (Vensimfloder ), while I was composing mail to you, but the model is fine. Thanks for this correction.

I need to do simulation and optimization at the same time for my research work. Initiall I tried to do the optimization with the external facilities of Vensim but I was not be able to continue for more that 5 timestep. I think, my library function ( I bought from Visual Numerics) is not strong enough to solve nonlinear equations. At that time I found that 'F-solve' function of MATLAB can easily handle my non- linear sets of equation. Therefore, I planned to call Vensim from MATLAB, so that I can easily use f-solve function.

Let me know if you have any better idea/suggestion? If you have called Vensim from Matlab, that would be a great help for me, as you may have that tested code with you.


I couldn't et your point:
[Quote]
Also, it appears that you have an extra ' in your model path, just before testlink: C:\Vensimfloder\models\sample\'testlink.vpm

[unquote]
I put *.vpm file under 'sample' folder, therefore I was mentioning that path. However, I just changed the path and location of that file, (which is C:\Vensimfolder\models\'testlink.vpm) but the problem is same as before.


Now I am totally lost and don't know what to do next!!

Posted: Fri Sep 03, 2010 9:25 pm
by khalediwm
Sorry tomfid, I should say thanks to you , as you responded many of my quries. Unfortunately, I mentioned you as Bob in several times today. Sorry about that

Posted: Sat Sep 04, 2010 1:11 am
by tomfid
No problem, I'll answer to Bob. Unfortunately I don't have a copy of Matlab these days, and I've never connected it to Vensim.

When you say you were not able to continue for more than 5 time steps with Vensim, what was the problem? Did the model crash, or was it simply too slow, or ... ?

Vensim's FIND ZERO is a lot like fsolve, I think, so it may be possible to find a Vensim-only solution. What are the simultaneous equations you're solving, or at least what are they about?

Tom

Posted: Sat Sep 04, 2010 7:47 am
by Administrator
I put *.vpm file under 'sample' folder, therefore I was mentioning that path. However, I just changed the path and location of that file, (which is C:\Vensimfolder\models\'testlink.vpm) but the problem is same as before.
What Tom is saying is use

str=('SPECIAL>loadModel|C:\Vensimfloder\models\sample\testlink.vpm')
instead of
str=('SPECIAL>loadModel|C:\Vensimfloder\models\sample\'testlink.vpm')

There is an extra ' before testlink.vpm in your code.

And as Tom says, you would be better off asking for help on a Matlab forum. Nobody here has experience of this so we are just guessing.

[Edited on 4-9-2010 by Administrator]

Posted: Sat Sep 04, 2010 10:00 am
by bob@vensim.com
I am not really sure where you are on this but if you want to use the Vensim DLL then you must install Vensim DSS after installing the Vensim Model Reader. if you do it the other way around the DLL (which has the same name for both) will not be able to open any models for development. So you may need to reinstall Vensim DSS.

Posted: Sat Sep 04, 2010 8:28 pm
by khalediwm
Hi Tomfid
So far I know, matlab's ‘fsolve’ (non-linear equation solver) is something called a 'trust-region dogleg'. The algorithm approximates the function with a piecewise linear polygon.
I should thank Bob, as he helped me helped me to use FIND ZERO function.
Unfortunately, I couldn't get much information regarding FIND ZERO function from 'Vensim reference manual'. I tried to use that function to solve my problem. It works fine for few time steps then I am having floating point error. To avoid that, I tried to use the value from previous time step, as an initial guess for the current timestep. This modification makes model more venerable to floating point error.

Tomfid, I can't say that the equations are wrong, as f-solve (MATLAB) can handle the problem without floating point error?

Any suggestion??

I will be very glad if you please check my model.

Posted: Sat Sep 04, 2010 8:32 pm
by khalediwm
This one is not using previous value as an initial guess for the next time step.

Posted: Sun Sep 05, 2010 10:02 am
by khalediwm
Hi, I just remember that in case of 'L', I used a multiplying factor 6, so that the model can be simulated at least for few time steps. If I remove this factor then the model is not able to run even a single timestep.

Surprisingly, I was able to run the model for entire simulation period in Vensim 10.a, even though the model is not producing correct result every time (with a multiplying factor of 6 for L). But with factor 1, I can ‘t sart my model simulation due to the same problem. I don't know what are the changed you made from 5.3a to 10.a!

The error message is also confusing like below;
Floating point error computing - equations[f26] - at time = 1962.625000.
STOP VENSIM: Floating point overflow


I am not sure but it seems to me that there may not be much limitation in "FIND ZERO" function, the problem is, how Vensime is handling floating point, or it could be both. I am attaching that error message, so that you can have a look.

What is your opinion and how it could be solved?

Posted: Sun Sep 05, 2010 11:01 am
by bob@vensim.com
I tried this in the double precision version of Vensim and it behaves much better. You can switch to that by reinstalling and selecting the checkbox for double precision on the component choice dialog. Hopefully you can then make progress with the underlying work.

Posted: Sun Sep 05, 2010 6:46 pm
by khalediwm
Hi Bob
Thanks for your advice. Actually I tested that model with double precision in both 5.3a and 5.10a and I am getting better result in Vensim 5.10a. My question was what are the changes/up gradation you made in version 5.10a, so that it can perform better?

However, even with version 5.10a, I can't run that model (after removing a multiplying factor 6 for L(see the attachment)). Moreover, the error messages that is appearing, seems it is more related to Vensim calculation procedure (I don't know actually!) rather than FIND ZERO algorithm.

I must work with Vensim as my main model is developed in Vensim and I don’t have any other alternatives. Still now, I am bit hesitant to in using FIND ZERO function because, most of the parameters (around 25) will not be constant like the example I attached, they will be changing over time. That’s why I was trying to call Vensim from MATLAB to use ‘fsolve’ function and I failed.

My last important point is, can you please specifically say what's causing Vensim to STOP (Floating point overflow) and is it fixable?

Posted: Mon Sep 06, 2010 10:38 am
by bob@vensim.com
I can't remember any changes to FIND ZERO, but 5.3 was a long time ago so something might have happened. Recently we switched to a new compiler which may be the main difference (the way the optimizer renders code into machine language will be different).

It is very tough to trace floating point errors with the FIND ZERO function (in fact in a previous thread JJ was saying he would like the entire set of iterations through the solution vectors stored like simulation results so he could watch them. Usually, the errors trigger because the scaling factors are not sufficient to keep all the parameters changing the amount that they should. I am not sure where your model is triggering errors, and I will take a look take a closer look at it later this week. It may be possible to mask the errors during the loop iterations instead of stopping the simulation with an error message.

For now you best bet is to be sure the scaling vectors that bring both the t vector and the equations vector to be adjusted to have all elements with the same order of magnitude (1 for t).

Posted: Mon Sep 06, 2010 1:10 pm
by LAUJJL
Hi

To spot the error I would first correct the unit errors. Secondly I would simplify the model by reducing the size of the arrays to its maximum possible, eventually suppressing the arrays. Lastly I would increase the time step and change the looping action within a bigger time step into a looping action through successive time steps, adding a level to make it possible. It could give approximately the same results while avoiding the find zero function that is rather tricky to control. If you want absolutely keep the looping action within a single time step, you can simulate the find zero function, changing the values that change during the looping action into subscripted variables and controlling the convergence process with adequate equations, one does not need anymore the find zero function. I explained this in a preceding thread. You will then be able to trace how the looping action works. But I would do that if nothing else works, because it needs a good knowledge of the Vensim language.
It is too important to justify the necessity to do all this work practically unless is is only for research or academical reasons.
Regards.
JJ

[Edited on 6-9-2010 by LAUJJL]

Posted: Mon Sep 06, 2010 11:20 pm
by khalediwm
HI
Thanks for your advice. I think, I don't have any unit error. In response to you second point (reducing size), I would say that I am unable to reduce the number of variables. Regadring the bigger time step, I am in bit problem, as rest of the model needs small calculation time step to avoid instability. Unfortunately, I couldn't follow your instruction to tackle the timestep problem in other way (looping action into subscripted variables). Could you please explain more or give an example to overcome timestep problem?

I would be helpful if you give the link of your preceding thread, where you gave more explanation.

KHALED

Posted: Tue Sep 07, 2010 2:15 am
by tomfid
Is this some kind of general equilibrium model? Some insight into the problem statement might help intuition about the solution.

What is the origin of the time-varying behavior? Is it only the changing population?

One possible reason that you don't have any units errors reported is that you haven't defined any units for any of the variables! Defining units really helps with avoidance of errors, even if you're confident that your underlying equations are correct.

Tom

Posted: Tue Sep 07, 2010 2:16 am
by tomfid
Also, when you look a the solution at the initial time step, is it reasonable?

Posted: Tue Sep 07, 2010 4:47 am
by khalediwm
Hi tomfid. Thanks for your response. All the parameters that are entering in to the optimization are mostly unit less. We are using some kind of conversion factor for each of them. Some variables are related to energy price of different sources, energy reserve, emission and etc.

Yes, the solution I am having at the initial time is reasonable. I think if I can avoid floating point error, I could get a very reliable solution. I was surprised when I was able to run my model for much longer time span in version 5.10a, compared to Vernsim5.3a. Bob already mentioned that it could be due to new compiler.

Perhaps you can recall that I used IMSL library function to do that optimization in C++ and it works fine, but surprisingly it is not working properly when I am implement the same code in Vensim external function facilities.

So, what can be done and is it doable?

Posted: Tue Sep 07, 2010 2:58 pm
by LAUJJL
Hi

I do not share your opinion about unit errors. If a value has no unit, use the dmnl unit. There are never any reasons not to use units. Using unist is a bit cumbersome especially if one is not used to it, but it denotes somebody with bad practical SD experience. Accepting to write a model with unit errors is not serious, even if you find models with such errors, particularly with published models in the SD conference papers. These models are made by people who may have expertise in their domain but certainly not in SD.
About reducing the complexity of the model, I was not thinking of reducing the number of variables but the size of the subcripts, epecially as most of the problem is concentrated on the equation[f26] variable f26 being the last value of the fun subscript.
One remark: If I compile the model with the 2008 microsoft compiler, there is no more a floating point error, even if the find zero still cannot make the equation|f26] converge.
Another remark: if one makes the method of the find zero equal to 0, the Broydn serach as explained in the find zero equation explanation, there is no more a floating point error, even in interpreted mode, but the results are completely diffierent than with the method set to 1. Probably the broynd method does not work correctly.
I use the 5.10a dss version.
I will try if I have the time to show an example of replacing the find zero function with an array.
To find all the threads about the find zero function, use the search function on the red bar above the Vensim screen.
Regards.
JJ


[Edited on 7-9-2010 by LAUJJL]

[Edited on 7-9-2010 by LAUJJL]

[Edited on 7-9-2010 by LAUJJL]

call Vensim

Posted: Tue Sep 07, 2010 5:48 pm
by LAUJJL
Hi

Joined three models that try to explain how to emulate the find zero function using arrays.
I first tried to build a model using the first example of the Vensim find zero function description the z = (x-target)^2 but it does not work well, whatever the parameter used. The model is called find_zero_clone.mdl.
I then used the second example with the commod subscript.
I build a first model called find_zero_clone_2.mdl and a second model with arrays called find_zero_clone_2_array.mdl that emulate the find zero function.
joined the three models zipped.
Regards.
JJ

Posted: Tue Sep 07, 2010 6:02 pm
by tomfid
Why not do this in GAMS? With undefined units and no dynamics (except maybe the "delayed" variable, which isn't used in the sample), it seems like you're not taking advantage of Vensim's strengths.

Tom

Posted: Tue Sep 07, 2010 6:08 pm
by tomfid
Why not do this in GAMS? With undefined units and no dynamics (except maybe the "delayed" variable, which isn't used in the sample), it seems like you're not taking advantage of Vensim's strengths.

Tom