4.2. Using C language¶
4.2.1. Brief description¶
// Method 1: A new method for creating model is introduced from version 0.19.0.
MdoEnvPtr env;
MdoMdlPtr model;
Mdo_createEnv(&env);
Mdo_createMdlWithEnv(&model, env);
Mdo_readProb(model, filename);
Mdo_solveProb(model);
Mdo_displayResults(model);
Mdo_freeMdl(&model);
// free env is need in C SDk
Mdo_freeEnv(&env);
// Method 2: The old method for creating model is still supported. It will be removed in future version.
/*
MdoMdlPtr model;
Mdo_createMdl(&model);
Mdo_readProb(model, filename);
Mdo_solveProb(model);
Mdo_displayResults(model);
Mdo_freeMdl(&model);
*/
The following describes how to use C language to compile and connect to the DLL of MindOpt and provides a compiling example. The example describes how to read an optimization model from the MPS file and solve an optimization problem.
Note
The DLL is connected only when the application runs. Therefore, you must specify the path of the DLL in an environment variable so that the application can correctly locate the DLL. If the corresponding environment variable is not specified, you must place the DLL beside the application. For more information about how to set environment variables, see Installation instructions. For more information about the support for C compiler versions, see Supported platforms.
4.2.2. Windows¶
MindOpt has been tested in Visual Studio 2017/2019. For more information about the compilation examples of the C programming language, see <MDOHOME>/<VERSION>/examples/C
.
To use Visual Studio to compile a DLL, perform the following steps:
Create an empty Visual Studio project.
Select
x64
as the solution platform.Set the include directory (
<MDOHOME>/<VERSION>/win64-x86/include
).Set the library directory (
<MDOHOME>/<VERSION>/win64-x86/lib
).Add the library file (
mindopt_x_x_x.lib
).
4.2.3. Linux¶
For more information about the compilation examples of the C programming language, see <MDOHOME>/<VERSION>/examples/C
. The compilation method and test command are as follows:
cd <MDOHOME>/<VERSION>/examples/C
make -f Makefile all
make -f Makefile test
The makefile
file used during compilation is as follows:
INCPATHS=-I../../linux64-x86/include -I.
LIBPATHS=-L../../linux64-x86/lib
MINDOPTLIB=-lmindopt
CCOPT=
LDOPT=-Wl,-rpath-link,./../../linux64-x86/lib -Wl,-rpath,'$$ORIGIN/../../linux64-x86/include' -pthread -lc -lm -Wl,--no-as-needed -ldl
CC=cc -m64
LD=cc -m64
MdoMps: MdoMps.c
$(CC) -c $(INCPATHS) $(CCOPT) -o MdoMps.o MdoMps.c
$(LD) $(LIBPATHS) MdoMps.o $(MINDOPTLIB) $(LDOPT) -o MdoMps
4.2.4. OSX¶
For more information about the compilation examples of the C programming language, see <MDOHOME>/<VERSION>/examples/C
. The compilation method and test command are as follows:
cd <MDOHOME>/<VERSION>/examples/C
make -f Makefile all
make -f Makefile test
The makefile
file used during compilation is as follows:
INCPATHS=-I../../osx64-x86/include
LIBPATHS=-L../../osx64-x86/lib
MINDOPTLIB=-lmindopt.x.x.x -rpath @executable_path/../../osx64-x86/lib/ -lpthread
CCOPT=
LDOPT=-Wl,-headerpad,128
CC=clang
LD=clang
%.o: %.cpp
$(CC) -c -g $(INCPATHS) $(CCOPT) -o $@ $<
MdoMps: MdoMps.o
$(CC) -g $(LIBPATHS) $(LDOPT) -o $@ $< $(MINDOPTLIB)
install_name_tool -change libmindopt.x.x.x.dylib `pwd`/../../osx64-x86/lib/libmindopt.x.x.x.dylib $@
4.2.5. C compilation example: MdoMps¶
The following part describes how to call the C API of MindOpt to read an optimization problem file in the MPS/LP format and solve the problem.
Load the C header file:
8#include "Mindopt.h"
Create an optimization model:
41 /*------------------------------------------------------------------*/
42 /* Step 1. Create a model and change the parameters. */
43 /*------------------------------------------------------------------*/
44 /* Create an empty model. */
45 MDO_CHECK_CALL(Mdo_createMdl(&model));
Call Mdo_readProb()
to read an optimization problem file in the MPS/LP format:
47 /*------------------------------------------------------------------*/
48 /* Step 2. Input model. */
49 /*------------------------------------------------------------------*/
50 /* Read model from file. */
51 MDO_CHECK_CALL(Mdo_readProb(model, filename));
Call Mdo_solveProb()
to solve the problem, and call Mdo_displayResults()
to view the optimization result.
53 /*------------------------------------------------------------------*/
54 /* Step 3. Solve the problem and populate the result. */
55 /*------------------------------------------------------------------*/
56 /* Solve the problem. */
57 MDO_CHECK_CALL(Mdo_solveProb(model));
58 Mdo_displayResults(model);
Call Mdo_freeMdl()
to release the memory.
60 /*------------------------------------------------------------------*/
61 /* Step 4. Free the model. */
62 /*------------------------------------------------------------------*/
63 /* Free the model. */
64 Mdo_freeMdl(&model);
MdoMps.c is the source code file.
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 "Mindopt.h"
9
10/* Macro to check the return code */
11#define MDO_CHECK_CALL(MDO_CALL) \
12 code = MDO_CALL; \
13 if (code != MDO_OKAY) \
14 { \
15 Mdo_explainResult(model, code, str); \
16 Mdo_freeMdl(&model); \
17 fprintf(stderr, "===================================\n"); \
18 fprintf(stderr, "Error : code <%d>\n", code); \
19 fprintf(stderr, "Reason : %s\n", str); \
20 fprintf(stderr, "===================================\n"); \
21 return (int)code; \
22 }
23
24int main(
25 int argc,
26 char * argv[])
27{
28 if (argc != 2)
29 {
30 return 0;
31 }
32
33 /* Variables. */
34 char str[1024] = { "\0" };
35 char * filename = argv[1];
36 double val = 0.0;
37 MdoMdl * model = NULL;
38 MdoResult code = MDO_OKAY;
39 MdoStatus status = MDO_UNKNOWN;
40
41 /*------------------------------------------------------------------*/
42 /* Step 1. Create a model and change the parameters. */
43 /*------------------------------------------------------------------*/
44 /* Create an empty model. */
45 MDO_CHECK_CALL(Mdo_createMdl(&model));
46
47 /*------------------------------------------------------------------*/
48 /* Step 2. Input model. */
49 /*------------------------------------------------------------------*/
50 /* Read model from file. */
51 MDO_CHECK_CALL(Mdo_readProb(model, filename));
52
53 /*------------------------------------------------------------------*/
54 /* Step 3. Solve the problem and populate the result. */
55 /*------------------------------------------------------------------*/
56 /* Solve the problem. */
57 MDO_CHECK_CALL(Mdo_solveProb(model));
58 Mdo_displayResults(model);
59
60 /*------------------------------------------------------------------*/
61 /* Step 4. Free the model. */
62 /*------------------------------------------------------------------*/
63 /* Free the model. */
64 Mdo_freeMdl(&model);
65
66 return (int)code;
67}
You can find more C example files under <MDOHOME>/<VERSION>/examples/C
of the installation package.