AMPL API issue

69 views
Skip to first unread message

Maxwell Harris

unread,
Apr 5, 2016, 1:04:46 PM4/5/16
to am...@googlegroups.com
Sir or Ma'am,

I am having issues calling some of the functions within matlab and Simulink using the AMPL API. 
The code I am writing is done in simulink, So I had to wrap a API code in an S function. When this is done there are a few issues that arise. 
The first is some of the code is erroring out, more specifically the DataFrame and ampl.expand  give me errors when run.

The Error is: No method 'expand' with matching signature found for class 'com.ampl.AMPL'.
While same type of error happens for Data frame.

Another error is that the API seems to randomly drop connection to ampl when solving some simple problems with relaxation of constraints resulting in no call to cplex.

Dose this make since? Is there something that I should explain more on? 

Code that is giving the errors.
S-function output block. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Output(block)

% ==============================================
% Java Part 

%Import Java
    import java.lang.Object;
    import com.ampl.*;
%     import com.ampl.Entity;
%     import com.ampl.Parameter;
%     import com.ampl.Set;
%     import com.ampl.Variable;
%     import com.ampl.Constraint;
%     import com.ampl.Objective;

% Look at msfcn_times_two_JAVA example from mathworks

% End Java part
% ===============================================

    global MOC T_sp_pre Y_pre Const DiscParam debug_var ub lb NumZone
    global k k_start Des_Temp P P_dT DBT HoursForIdent stepsPerHour


    %% AMPL Setup

    %var preload var
    AMPL_folder = '\AMPL\';

    % Create an AMPL instance
    ampl = AMPL();

    % Load from file the ampl model
    ampl.read([pwd  AMPL_folder 'AMPL_' num2str(NumZone) 'Zone_model.mod']);
    ampl.readData([pwd  AMPL_folder 'AMPL_data.dat']);
    
    ampl.setOption('solver', 'CPLEX');
      ampl.setOption('cplex_options',...
       'presolve 1 resolve 1 timelimit=900'); 
    

    ObjFunc = ampl.getObjective('ObjFunc');
    ampl.expand(ObjFunc);    
    fprintf('New objective value: %f\n', ObjFunc.value());

    Y_pre_ampl = ampl.getParameter('Y_pre');
    df = DataFrame(1,1:NumZone);
    for i=1:NumZone
        df.setColumn(i,Y_pre(:,i));
    end
    Y_pre_ampl.setData(df);

    ampl.close();
  
%endfunction

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Respectfully,

Maxwell T Harris
Colorado School of Mines - (BS) Engineering with a Specialty in Mechanics - December 2012
Colorado School of Mines - (MS) Mechanical Engineering - Expected 2016

Victor Zverovich

unread,
Apr 5, 2016, 8:04:35 PM4/5/16
to am...@googlegroups.com
Which version of AMPL API do you use and could you post the full error message including stack trace if available?

Thanks,
Victor

--
You received this message because you are subscribed to the Google Groups "AMPL Modeling Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ampl+uns...@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.

Maxwell Harris

unread,
Apr 12, 2016, 1:20:24 PM4/12/16
to AMPL Modeling Language

I am using the API version 1.2.2 

Respectfully,
Maxwell

Victor Zverovich

unread,
Apr 12, 2016, 3:28:04 PM4/12/16
to am...@googlegroups.com
Thanks for providing the additional info. The API is the latest one, but the stack trace is not very detailed unfortunately.

What do you get if you try to evaluate ObjFunc in MATLAB, e.g.

  >> ObjFunc
  ans =
  minimize o: x;

?

- Victor

Maxwell Harris

unread,
Apr 12, 2016, 3:51:06 PM4/12/16
to AMPL Modeling Language
When I have it print out ObjFunc I get the correct ans. 

ObjFunc =
minimize ObjFunc: sum{i in 1 .. TimeSteps} R[i]*Y[i];

I have also tried ampl.expand(ObjFunc);  and that works to expand just the expand from com.ampl.AMPL does not seem to work. I have also had the same issues with DataFrame. It generates the same with "No method 'DataFrame' with matching signature found for class 'com.ampl.AMPL'"
 
Thanks

Victor Zverovich

unread,
Apr 18, 2016, 2:07:58 PM4/18/16
to am...@googlegroups.com
Could you clarify what you mean by

  ampl.expand(ObjFunc);

working and "expand from com.ampl.AMPL" not working?

IIUC ampl is an object of class com.ampl.AMPL. Could you give a small self-contained example of MATLAB code for the case that is not working? For instance, here's a working example:

>> setUp
>> ampl = AMPL
 >> ampl.eval('minimize o: 0;')
>> ObjFunc = ampl.getObjective('o')
>> ampl.expand(ObjFunc)
minimize o:
0;

Also note that the AMPL MATLAB API wraps Java objects and mixing Java objects and their MATLAB wrappers may not work.

Thanks,
Victor  

Maxwell Harris

unread,
Apr 18, 2016, 3:35:04 PM4/18/16
to AMPL Modeling Language
The following is a brief example and description of what is going on.

model.mod:
     'minimize o: 0;'
Code:
>> setUp
>> ampl = AMPL()
>> ampl.read('model.mod');
>> ampl.setOption('solver', 'CPLEX');
>> ObjFunc = ampl.getObjective('o')
>> ampl.expand(ObjFunc)

however when this is run in wrapped in an s function for simulink ampl.expand(ObjFunc) gives the following error: No method 'expand' with matching signature found for class 'com.ampl.AMPL'.

but when the ampl.expand is replaced with 

model.mod:
     'minimize o: 0;'
Code:
>> setUp
>> ampl = AMPL()
>> ampl.read('model.mod');
>> ampl.setOption('solver', 'CPLEX');
>> ampl.eval('expand o;');

I get the correct response:

>> minimize o:
0; 


Hopefully this better explains my issue.
I agree that with the s-function that there may be an issue with the java wrapper causing this problem.


Respectfully,

Maxwell Harris

Victor Zverovich

unread,
Apr 27, 2016, 5:16:00 PM4/27/16
to am...@googlegroups.com
Looks like it has nothing to do with Simulink. The problem is that by importing AMPL Java API with

  import com.ampl.*;

you override the MATLAB API version of the AMPL class. You shouldn't mix Java and MATLAB APIs in this way. To fix it remove the import statement and use the AMPL MATLAB API as described in the documentation (http://ampl.com/api/latest/matlab/).

HTH,
Victor

--
Reply all
Reply to author
Forward
0 new messages