4.7. Call MindOpt with MATLAB

This section uses a simple example to show how to use the MATLAB to call MindOpt to read and solve the optimization model.

4.7.1. Edit .m File

Below we will show how to call MindOpt MATLAB API in the .m file to read the optimization problem model file and solve it.

First call mindopt_read() to read optimization problems in MPS/LP format:

4model = mindopt_read(file);

Finally, use mindopt() to solve the problem, and view the objective function value of the variable and optimal solution through the model attribute X and ObjVal.

 5result = mindopt(model);
 6
 7for v=1:length(result.X)
 8    fprintf('%s %d\n', model.varnames{v}, result.X(v));
 9end
10fprintf('Obj: %e\n', result.ObjVal);

Below is the complete source code file readmps.m

 1function[objval] = readmps(file);
 2%H1 read a model and solve it
 3
 4model = mindopt_read(file);
 5result = mindopt(model);
 6
 7for v=1:length(result.X)
 8    fprintf('%s %d\n', model.varnames{v}, result.X(v));
 9end
10fprintf('Obj: %e\n', result.ObjVal);
11
12objval = result.ObjVal;

You can find more MATLAB language-related example files in the installation path <MDOHOME>/<VERSION>/examples/matlab.

4.7.2. Execution on Linux

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/matlab. First start MATLAB IDE from the terminal (otherwise the MINDOPT_HOME environment variable will not be inherited):

open /usr/local/bin/matlab

Then execute the startup command in the MATLAB IDE Command Window to load the environment variables, and finally run the sample program to complete the optimization solution:

startup
readmps ../data/afiro.mps

Another way is to run the sample program directly through the command line. This method is more direct and does not require loading environment variables.

matlab -batch "readmps ../data/afiro.mps"

4.7.3. Execution on macOS

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/matlab. First start MATLAB IDE from the terminal (otherwise the MINDOPT_HOME environment variable will not be inherited):

open /Applications/MATLAB_R2024b.app

Then execute the startup command in the MATLAB IDE Command Window to load the environment variables, and finally run the sample program to complete the optimization solution:

startup
readmps ../data/afiro.mps

Another way is to run the sample program directly through the command line. This method is more direct and does not require loading environment variables.

/Applications/MATLAB_R2024b.app/bin/matlab -batch "readmps ../data/afiro.mps"

4.7.4. Execution on Windows

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/matlab. First start MATLAB IDE, then execute the startup command in the MATLAB IDE command line window to load environment variables, and finally run the sample program to complete the optimization solution:

startup
readmps ../data/afiro.mps

Another way is to run the sample program directly through the command line. This method is more direct and does not require loading environment variables.

"C:\Program Files\MATLAB\R2024b\bin\matlab.exe" -batch "readmps ../data/afiro.mps"

Note

  • In order for the application to correctly locate the dynamic library, the user needs to specify the path of the dynamic library in the environment variable. If the environment variable is not specified, the user needs to place the dynamic library file in an appropriate location according to the logic of the operating system to find the dynamic library. For environment variable settings, see Installation Instructions.

  • If user install mindopt for installer, the environment variable is automatically set. Call MATLAB IDE from terminal the environment variable will inherited, otherwise, you need to set the environment variable manually through replacing the statement mdo_env = getenv(‘MINDOPT_HOME’) with mdo_env = ‘<MDOHOME>/<VERSION>’ in <MDOHOME>/<VERSION>/examples/matlab/startup.m.