how to read a .mod file in ampl and return the results so that I can do some modify and logic control??

608 views
Skip to first unread message

Yudi, Judy WANG

unread,
Nov 11, 2016, 8:50:14 PM11/11/16
to AMPL Modeling Language
I was trying to read .mod files, and convert the model into other format so that I can use it into R or MATLAB.

for example, can I do this?

<algorithm>
string input;
string var, func,equality, unequality;
input= read "input.mod";
n=length(input);
for i={1,......,n}
#find var and value;
   if input[i,i+1,i+2]=="var"
           var=input[i+3];
           varvalue=default value;

#find function
    if input [....]=="minmized"
  ...........
#find equalities....
#find unequalities.....
#find LB;
#find UB;
#find eqLB;
#find eqUB;
#find uneqLB;
#find uneqUB;
........................
end if;
return var, func, equality, unequality;

Robert Fourer

unread,
Nov 12, 2016, 2:53:29 PM11/12/16
to am...@googlegroups.com
Writing your own processor to convert AMPL models to some other form would be very difficult. Instead you can leave the model in AMPL and invoke the AMPL processor from within MATLAB or R -- using functions in these languages that start an external process. An invocation like "ampl app.run" will cause the script file app.run to be executed, and inside the script file can be commands for reading data, running solvers, etc.

In the case of MATLAB there is also an AMPL MATLAB API (http://ampl.com/products/api/) that lets you control an AMPL process directly from MATLAB (rather than indirectly through the script) and that provides methods for data exchange. We do not yet have such an API for R, however.

Bob Fourer
am...@googlegroups.com

=======

Yudi, Judy WANG

unread,
Nov 14, 2016, 1:26:21 PM11/14/16
to am...@googlegroups.com
Super interesting and helpful! Thank you again :)

--
You received this message because you are subscribed to a topic in the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ampl/kPzpbnBKooI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ampl+unsubscribe@googlegroups.com.
To post to this group, send email to am...@googlegroups.com.
Visit this group at https://groups.google.com/group/ampl.
For more options, visit https://groups.google.com/d/optout.



--

Yudi, Judy WANG

unread,
Nov 14, 2016, 3:00:16 PM11/14/16
to AMPL Modeling Language, 4...@ampl.com
Hi, I would also like to ask that:

I wrote my own solver in Matlab, I want to test them using AMPL in order to test on .mod files, how could I connect my own .m Matlab script with AMPL? Is there any efficient way except for develop my algorithm using C++/Fortran?

I really don't want to spent more time using C++/Fortran.

Thanks again!

Robert Fourer

unread,
Nov 18, 2016, 2:31:03 PM11/18/16
to am...@googlegroups.com
You can use the utilities described in the subsection "Use with MATLAB" on page 22 of Hooking Your Solver with AMPL. The source is available from ampl.com/netlib/ampl/solvers/examples/ so you can modify it to meet your needs. This approach does use mex files written in C, however. Some C is needed because the AMPL-solver library (ASL) is in C; there does not exist a MATLAB application to process the .nl files.
Reply all
Reply to author
Forward
0 new messages