This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

Solving your first model in C#

LocalSolver is implemented in C++ language. Nevertheless, object-oriented application programming interfaces (APIs) are provided for .NET 2.0 (or superior), allowing a full integration of LocalSolver in your .NET business applications. LocalSolver’s APIs are lightweight, with only a few classes to manipulate. Remind that LocalSolver is a black-box math programming solver: having instantiated the model, no additional code has to be written in order to run the solver.

Below is given the Java code for solving the knapsack toy instance introduced during the Quick tour of LocalSolver’s modeler. The corresponding source file can be retrieved in examples/toy.

A small example

/********** Toy.cs **********/

using localsolver;

public class Toy
{
    public static void Main()
    {
        int[] weights = { 10, 60, 30, 40, 30, 20, 20, 2 };
        int[] values = { 1, 10, 15, 40, 60, 90, 100, 15 };

        using (LocalSolver localsolver = new LocalSolver())
        {
            // Declares the optimization model.
            LSModel model = localsolver.GetModel();

            // 0-1 decisions
            LSExpression[] x = new LSExpression[8];
            for (int i = 0; i < 8; i++)
                x[i] = model.Bool();

            // knapsackWeight <- 10*x0 + 60*x1 + 30*x2 + 40*x3 + 30*x4 + 20*x5 + 20*x6 + 2*x7;
            LSExpression knapsackWeight = model.Sum();
            for (int i = 0; i < 8; i++)
                knapsackWeight.AddOperand(weights[i] * x[i]);

            // knapsackWeight <= 102;    
            model.Constraint(knapsackWeight <= 102);

            // knapsackValue <- 1*x0 + 10*x1 + 15*x2 + 40*x3 + 60*x4 + 90*x5 + 100*x6 + 15*x7;
            LSExpression knapsackValue = model.Sum();
            for (int i = 0; i < 8; i++)
                knapsackValue.AddOperand(values[i] * x[i]);

            // maximize knapsackValue;
            model.Maximize(knapsackValue);

            // close the model before solving it
            model.Close();

            // Parameterizes the solver.
            LSPhase phase = localsolver.CreatePhase();
            phase.SetTimeLimit(1);
            localsolver.Solve();
        }
    }
}

You can observe that the structure of the C# program follows the structure of corresponding LSP. First, we read input data. Then, we declare the knapsack model by creating some expressions. Once the model is closed, we can arameterize the search before lauching the solver.

On Windows, the above program is compiled and launched using the following lines in Visual Studio Command Prompt (x86 or x64, depending on your version of LocalSolver). Note that if you use directly Visual Studio IDE for building your program, you must specify the Platform target (to x86 or x64, depending on our version of LocalSolver) in the Properties of your Visual Studio project.

copy %LS_HOME%\bin\*net.dll .
csc Toy.cs /reference:localsolvernet.dll
Toy

If you have some troubles in compiling or launching the program, please have a look to the Installation & licensing. We invite users willing to go further with APIs to consult the C# API Reference.