External Function
External Function
HI
I am trying to work out an optimization problem, by solving systems of non-linear equations. I know that I have to use external function facilities. But I have 27 nonlinear equations to solve. So, I may have more than 27 optimized variables & parameters to return in Vensim ( in every time step to use for the next time step), instead of a single objective function values.
1) Do you have any suggestion on this issue?
2) Is it OK, if I use Java instead of C/C++ in developing external function coding?
I am trying to work out an optimization problem, by solving systems of non-linear equations. I know that I have to use external function facilities. But I have 27 nonlinear equations to solve. So, I may have more than 27 optimized variables & parameters to return in Vensim ( in every time step to use for the next time step), instead of a single objective function values.
1) Do you have any suggestion on this issue?
2) Is it OK, if I use Java instead of C/C++ in developing external function coding?
1) Check to be sure that you can't solve your problem with the FIND ZERO function. If you need to go the external function route, consider hooking up some external library to do most of the work. I once connected Vensim to LINDO (an LP solver) for a similar purpose.
2) External functions have to be compiled in .dlls. It's probably possible to wrap Java to work this way, but there would be a lot of overhead. I'd stick to C.
Tom
2) External functions have to be compiled in .dlls. It's probably possible to wrap Java to work this way, but there would be a lot of overhead. I'd stick to C.
Tom
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Hi Tom
Thanks for your suggestion. However, I am still not clear, how can I get more than one variable from the external function, as the usual way of calling an external function is Y= Externalfunction (x1,x2,...xn). In case of having all the route, how should I modify my equation. In the conventional way I may able to get only the value of Y.
Thanking you in advance
Thanks for your suggestion. However, I am still not clear, how can I get more than one variable from the external function, as the usual way of calling an external function is Y= Externalfunction (x1,x2,...xn). In case of having all the route, how should I modify my equation. In the conventional way I may able to get only the value of Y.
Thanking you in advance
You can write an external function that returns an array. I don't have the documentation in front of me, but I think if you look at the example external function library you'll find an applicable example.
The call from Vensim would probably look like:
val : (v1-v27)
myResult[val] = MYFUNC( ... )
The call from Vensim would probably look like:
val : (v1-v27)
myResult[val] = MYFUNC( ... )
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
OK, good luck. If you share more details on what you're trying to accomplish, you might get additional comments.
Tom
Tom
/*
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Advice to posters (it really helps us to help you)
http://www.ventanasystems.co.uk/forum/v ... f=2&t=4391
Blog: http://blog.metasd.com
Model library: http://models.metasd.com
Bookmarks: http://delicious.com/tomfid/SystemDynamics
*/
Hi
I am trying to use, external functions of VENSIM to solve nonlinear series of equation, which consist of 26 variables and 33 constants. I used Visual Studio 2008 and IMSL library function to calculate the roots. The program works fine in Visual Studio environment. However, I am having problem while implementing the same algorithm in VENEXT.C file. I am attaching that file, so that you can have a look. I actually created the *.dll file to call from Vensim, as user defined function. I am able to call that function (SLVSYSNONLINREQN) but couldn’t simulate because of floating point error. I am bit unclear about the following points:
1) Under “3-Grouping of functions in a structure- see venext.h “ , it is given as “COSINE (x), 1,0,cos_Func,0,0”. I couldn’t able to find Venext.h file, therefore confused about the different values under cosine function and may did some mistake
2) For non linear equation solution I am using,” Solve_Sys_of_Nonlinear_Equations’ external function and this function also calling “fcn” function. The other possibility could be such that the first function is not able to pass the value to the second one.
I am not bit lost, as I tried to resolve the issue in different ways but none of those succeeded. Could anyone please help me to solve my problem or at least anybody could give me an example where such nonlinear equations are dealt with?
I am trying to use, external functions of VENSIM to solve nonlinear series of equation, which consist of 26 variables and 33 constants. I used Visual Studio 2008 and IMSL library function to calculate the roots. The program works fine in Visual Studio environment. However, I am having problem while implementing the same algorithm in VENEXT.C file. I am attaching that file, so that you can have a look. I actually created the *.dll file to call from Vensim, as user defined function. I am able to call that function (SLVSYSNONLINREQN) but couldn’t simulate because of floating point error. I am bit unclear about the following points:
1) Under “3-Grouping of functions in a structure- see venext.h “ , it is given as “COSINE (x), 1,0,cos_Func,0,0”. I couldn’t able to find Venext.h file, therefore confused about the different values under cosine function and may did some mistake
2) For non linear equation solution I am using,” Solve_Sys_of_Nonlinear_Equations’ external function and this function also calling “fcn” function. The other possibility could be such that the first function is not able to pass the value to the second one.
I am not bit lost, as I tried to resolve the issue in different ways but none of those succeeded. Could anyone please help me to solve my problem or at least anybody could give me an example where such nonlinear equations are dealt with?
- Attachments
-
[The extension cpp has been deactivated and can no longer be displayed.]
-
- Senior Member
- Posts: 1107
- Joined: Wed Mar 12, 2003 2:46 pm
The FUNC_DESC structure is in vensim.h (it used to be venext.h but that is no longer used).
To do what you want I would actually recommend that you map the 33 parameters to a small number of arrays (possibly just 1) in Vensim and then just pass the arrays.
The function closest to what you want is probably MATRIX_INPLACE_INVERT which takes one array and makes changes to that (the function is marked as changing its arguments which is important for equation ordering).
Do be careful about the form in which information is passed. Regular arguments are just passed as float (or double in the double precision version) but vectors are passed as structure pointers. To change a value you will need to pass the argument as an array - even if it is a scalar.
To do what you want I would actually recommend that you map the 33 parameters to a small number of arrays (possibly just 1) in Vensim and then just pass the arrays.
The function closest to what you want is probably MATRIX_INPLACE_INVERT which takes one array and makes changes to that (the function is marked as changing its arguments which is important for equation ordering).
Do be careful about the form in which information is passed. Regular arguments are just passed as float (or double in the double precision version) but vectors are passed as structure pointers. To change a value you will need to pass the argument as an array - even if it is a scalar.
Thanks Bob, for your advice and I will try my best.
Could you please let me know, how to write an external function, with such arrays, as I mentioned to you before that I am not clear about the values under "COSINE" function? I don't know how you choose those 0 and 1 ?
If you have any example which works with arrays and used as external function, please let me have a chance to look in to that.
Could you please let me know, how to write an external function, with such arrays, as I mentioned to you before that I am not clear about the values under "COSINE" function? I don't know how you choose those 0 and 1 ?
If you have any example which works with arrays and used as external function, please let me have a chance to look in to that.
-
- Senior Member
- Posts: 1107
- Joined: Wed Mar 12, 2003 2:46 pm
This is the function to use as a prototype:
{"MATRIX_INPLACE_INVERT", // function name
" {matrix} ", // argument list used in equation editor
1, // the number of arguments the function takes in Vensim
1, // the number of vector arguments (these will be first)
INPLACE_INVERT_FUNC, // the function index passed to vensim_external
0, // number of user loops
1, // modification flag - this function will change its arguments
0, // the number of literal arguments (these precede lookups and vectors
0 // the number of Lookups passed (these precede vectors)
},
This passes a vector that it modifies, which is what you want from your description.
{"MATRIX_INPLACE_INVERT", // function name
" {matrix} ", // argument list used in equation editor
1, // the number of arguments the function takes in Vensim
1, // the number of vector arguments (these will be first)
INPLACE_INVERT_FUNC, // the function index passed to vensim_external
0, // number of user loops
1, // modification flag - this function will change its arguments
0, // the number of literal arguments (these precede lookups and vectors
0 // the number of Lookups passed (these precede vectors)
},
This passes a vector that it modifies, which is what you want from your description.
Hi, I just faced another problem regarding external function. Previously my external function was working fine. However, I just transfered all my files to my new PC (Windows7, 64 bit). I coppied the actual VENSIM.DLL file and then mentioned the path in 'startup', but I am not able to see my external functions! Can anybody tell me what is going on?
-
- Super Administrator
- Posts: 4592
- Joined: Wed Mar 05, 2003 3:10 am
-
- Senior Member
- Posts: 1107
- Joined: Wed Mar 12, 2003 2:46 pm
If VENSIM.DLL is a file that you created and that was working on your previous computer it should still work under Windows 7. There might, however, be a permissions issue. Make sure that the current user has permissions to work with this file. It is also possible that it is being blocked as a potential security threat by a firewall.
If you still have the old computer the best test would be top copy your VENSIM.DLL file back and see is it still works on that computer.
If you still have the old computer the best test would be top copy your VENSIM.DLL file back and see is it still works on that computer.
-
- Senior Member
- Posts: 1107
- Joined: Wed Mar 12, 2003 2:46 pm
right click on Vensim and select run as administrator. If that allows you to use your external function dll it is an access control issue. You will need to get a manifest setitng the DLL to asInvoker - this is pretty straightforward to do using Visual Studio 2010, otherwise you will need to the manifest tool from the Microsoft website and figure out how to use it.
If this is not the problem my guess is that the dll got corrupted somehow.
If this is not the problem my guess is that the dll got corrupted somehow.
Thanks bob for your kind reply.
However, I followed your advice to run Vensim as administrator but I am still having the same trouble. Then I created a new VENSIM.DLL file and checked in my older PC which works fine, but it's not working in my new machine. So, there could be 2 issues as per my view:
1) I have installed Vensim 5.10d in my new computer but my old computer has Vensim 5.10C. could it be an issue?
2) I am using Visual studio 2008 to create dll file, is it a problem for Windows 7?
However, I followed your advice to run Vensim as administrator but I am still having the same trouble. Then I created a new VENSIM.DLL file and checked in my older PC which works fine, but it's not working in my new machine. So, there could be 2 issues as per my view:
1) I have installed Vensim 5.10d in my new computer but my old computer has Vensim 5.10C. could it be an issue?
2) I am using Visual studio 2008 to create dll file, is it a problem for Windows 7?
-
- Super Administrator
- Posts: 4592
- Joined: Wed Mar 05, 2003 3:10 am
-
- Senior Member
- Posts: 1107
- Joined: Wed Mar 12, 2003 2:46 pm