4.6. Call MindOpt with C#¶
This section uses a simple example to show how to use the C# to call MindOpt to read and solve the optimization model.
4.6.1. Edit .cs File¶
Below we will show how to call MindOpt C# API in the .cs
file to read the optimization problem model file and solve it.
First import the C# mindopt package:
1using Mindopt;
Then create the environment and optimize the model:
16 MDOEnv env = new MDOEnv();
17 MDOModel? model = null;
Then call the constructor of MDOModel.MDOModel
to read the optimization problem in MPS/LP format:
25 model = new MDOModel(env, args[0]);
Finally, use MDOModel.Optimize
to solve the problem, and call MDOModel.Get
to get the objective function value of the optimal solution.
31 model.Optimize();
32 Console.WriteLine($"Obj value: {model.Get(MDO.DoubleAttr.ObjVal)}");
Below is the complete source code file ReadMps.cs.
1using Mindopt;
2
3namespace Example
4{
5 public class ReadMps
6 {
7 public static void Main(string[] args)
8 {
9 if (args.Length != 1)
10 return;
11
12 /*------------------------------------------------------------------*/
13 /* Step 1. Create a model and change the parameters. */
14 /*------------------------------------------------------------------*/
15 /* Create an empty model. */
16 MDOEnv env = new MDOEnv();
17 MDOModel? model = null;
18
19 try
20 {
21 /*--------------------------------------------------------------*/
22 /* Step 2. Input model. */
23 /*--------------------------------------------------------------*/
24 /* Read model from file. */
25 model = new MDOModel(env, args[0]);
26
27 /*--------------------------------------------------------------*/
28 /* Step 3. Solve the problem and print the result. */
29 /*--------------------------------------------------------------*/
30 /* Solve the problem. */
31 model.Optimize();
32 /* Print the result. */
33 Console.WriteLine($"Obj value: {model.Get(MDO.DoubleAttr.ObjVal)}");
34 }
35 catch (MDOException e)
36 {
37 Console.WriteLine(e.Message);
38 }
39 finally
40 {
41 /* Dispose of model and environment */
42 if (model != null) model.Dispose();
43 env.Dispose();
44 }
45 }
46 }
47}
You can find more C# language related example files under the installation path <MDOHOME>/<VERSION>/examples/csharp
.
4.6.2. Compile on Linux and macOS Platforms¶
We provide example files under the installation path <MDOHOME>/<VERSION>/examples/csharp
. Taking linux x86 / .NET SDK 7.x as an example, execute the following instructions to copy any example file to csproject directory, compile and perform optimization solution:
cd <MDOHOME>/<VERSION>/examples/csharp
cp ReadMps.cs csproject
cd csproject
dotnet build
dotnet run ../../data/afiro.mps --framework netcoreapp7.0
4.6.3. Compile on Windows Platform¶
We provide example files under the installation path <MDOHOME>\<VERSION>\examples\csharp
. Taking .NET SDK 7.x as an example, execute the following instructions to copy any example file to csproject directory, compile and perform an optimized solution:
cd <MDOHOME>\<VERSION>\examples\csharp
copy ReadMps.cs csproject
cd csproject
dotnet build
dotnet run ..\..\data\afiro.mps --framework netcoreapp7.0
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.
See Supported Platforms for supported C# language compiler versions.