4.2. Call MindOpt with C

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

4.2.1. Edit .c File

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

First import the C header file:

9#include "Mindopt.h"

Then create the environment and optimize the model:

25    MDOenv* env;
26    MDOemptyenv(&env);
27    MDOstartenv(env);
28    MDOmodel* model;

Then use MDOreadmodel() to read the optimization problem in MPS/LP format:

34    CHECK_RESULT(MDOreadmodel(env, argv[1], &model));

Then use MDOoptimize() to solve the problem and MDOgetdblattr() to see the objective function for the optimal solution.

40    CHECK_RESULT(MDOoptimize(model));
41    double obj;
42    CHECK_RESULT(MDOgetdblattr(model, MDO_DBL_ATTR_OBJ_VAL, &obj));

Finally, use MDOfreemodel() to free memory space.

50    MDOfreemodel(&model);
51    MDOfreeenv(&env);

The following is the complete source code file read_mps.c.

 1/**
 2 *  Description
 3 *  -----------
 4 *
 5 *  Read model from an MPS/LP file, and call optimizer to solve the problem.
 6 */
 7#include <stdio.h>
 8#include <stdlib.h>
 9#include "Mindopt.h"
10
11
12/* Macro to check the return code */
13#define CHECK_RESULT(code) { int res = code; if (res != 0) { fprintf(stderr, "Bad code: %d\n", res); exit(res); } }
14
15int main(int argc, char* argv[])
16{
17    if (argc != 2)
18    {
19        return 0;
20    }
21    /*------------------------------------------------------------------*/
22    /* Step 1. Create a environment and model.                          */
23    /*------------------------------------------------------------------*/
24    /* Create an empty model. */
25    MDOenv* env;
26    MDOemptyenv(&env);
27    MDOstartenv(env);
28    MDOmodel* model;
29
30    /*------------------------------------------------------------------*/
31    /* Step 2. Input model.                                             */
32    /*------------------------------------------------------------------*/
33    /* Read model from file. */
34    CHECK_RESULT(MDOreadmodel(env, argv[1], &model));
35
36    /*------------------------------------------------------------------*/
37    /* Step 3. Solve the problem and print the result.                  */
38    /*------------------------------------------------------------------*/
39    /* Solve the problem. */
40    CHECK_RESULT(MDOoptimize(model));
41    double obj;
42    /* Print the result. */
43    CHECK_RESULT(MDOgetdblattr(model, MDO_DBL_ATTR_OBJ_VAL, &obj));
44    printf("Obj value: %f\n", obj);
45
46    /*------------------------------------------------------------------*/
47    /* Step 4. Free the model ane the environment.                      */
48    /*------------------------------------------------------------------*/
49    /* Free the model. */
50    MDOfreemodel(&model);
51    /* Free the environment. */
52    MDOfreeenv(&env);
53    return 0;
54}

Users can find more C language related example files under the installation path <MDOHOME>/<VERSION>/examples/c.

4.2.2. Compile on Linux Platforms

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/c. Taking linux x86 as an example, execute the following commands in this path to compile the sample code, link MindOpt dynamic library and perform optimization solution:

cc -std=c99 -O2 -DNDEBUG -Wl,-rpath,../../linux64-x86/lib -I../../linux64-x86/include -o read_mps read_mps.c -L../../linux64-x86/lib -lmindopt
./read_mps ../data/afiro.mps

At the same time, we also provide a Makefile script, which can compile and execute the sample code through the make command:

# Compile and execute
make read_mps
./read_mps ../data/afiro.mps

# Automatically compile and execute all examples using the 'make test' command
make test

4.2.3. Compile on macOS Platforms

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/c. Taking macOS x86 as an example, execute the following commands in this path to compile the sample code, link MindOpt dynamic library and perform optimization solution:

cc -std=c99 -O2 -DNDEBUG -Wl,-rpath,../../osx64-x86/lib -I../../osx64-x86/include -o read_mps read_mps.c -L../../osx64-x86/lib -lmindopt
./read_mps ../data/afiro.mps

At the same time, we also provide a Makefile script, which can compile and execute the sample code through the make command:

# Compile and execute
make read_mps
./read_mps ../data/afiro.mps

# Automatically compile and execute all examples using the 'make test' command
make test

4.2.4. Compile on Windows Platform

For Windows platform users, first install and use Visual Studio 2019 or above, then open x64 Native Tools Command Prompt (You can see the program by typing cmd in the Start menu) and switch the path to <MDOHOME>\<VERSION>\examples\c, under this path Execute the following commands to compile the sample code, link the MindOpt dynamic library and perform an optimization solution:

cl /I ../../win64-x86/include read_mps.c /link /LIBPATH:../../win64-x86/lib mindopt_1_0_0.lib
read_mps.exe ..\data\afiro.mps

At the same time, we also provide a Makefile script, which can compile and execute the sample code through the nmake command:

# Compile and execute
nmake read_mps
read_mps.exe ..\data\afiro.mps

# Automatically compile and execute all examples with the 'nmake test' command
nmake test

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.

  • For C language compiler version support, please refer to the description in Supported Platform.