Tutorial 10: Embedding your optimization model in a C# application (Diet problem)

Welcome to this tutorial on embedding elytica optimization models in a C# application. It is worthwhile to revisit the previous tutorial, Tutorial 9: Working with JSON file formats, since we will be using the Diet Problem again as an example, and since JSON is the primary mechanism for data exchange between the elytica service and the C# API.

For convenience, the C# code presented in this tutorial is available as a Visual Studio project and can be downloaded from here.

Diet application overview

Before we dive into coding details, we will give you an overview of what the final product will look like and how you can simply reuse the Diet Problem project that you have created as part of Tutorial 9: Working with JSON file formats.

On launching the Diet application, the first thing to do is to provide an elytica access token. Use the same token that you have created in Tutorial 9 for the elytica Excel add-in.

Next, since you already created a project for the Diet Problem as part of Tutorial 9, you can simply apply the following settings to access the model. Make sure that the input file “diet_problem.json” has been uploaded manually, or that you have completed an optimization run using the elytica Excel add-in, while working with the “diet_problem.xlsx” file.

Once you have applied these options, you should see the following main screen of the Diet application.

The information displayed above should look familiar since it is the contents of the input file “diet_problem.json”. By clicking on the “Solve” button, the contents of the two data grids will be written back to the input file “diet_problem.json” to persist any changes that were made. It will then be uploaded to your Diet Problem project and be used to solve the corresponding optimization model.

On successful completion of the optimization call, the screen will be updated to reflect the solution information.

C# package requirements

Once you have downloaded the source code for the c# Diet application from here, make sure that it references the elytica_service and NewtonSoft.Json packages.

Program execution

The following is a flow diagram of the Diet application program execution, with specific reference to the use of the elytica service package. This provides an outline of the implementation sequence of the elytica service (annotation on the right of each flow diagram block), which will be relevant to other custom applications making use of elytica to solve optimization problems.