How to Specify a Model as a String for Google OR Tools?

Viewed 714

I'm working on a feature for a software package. This feature allows users to enter optimization models which the system would run with data it injects. The model format can be anything -- AMPL, FlatZinc, SMT-Lib, etc...

Microsoft's Z3 looked solid (it supported SMT-LIB), but unfortunately it was not free for commercial use. After some more searching, I settled on Google OR Tools, which is free for commercial use.

Unfortunately, I cannot find a way to specify a model as a string. For instance, here's how to do a simple linear programming problem using the OR Tools .NET API:

    Solver solver = Solver.CreateSolver(solverType);
    Variable x1 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x1");
    Variable x2 = solver.MakeIntVar(0.0, double.PositiveInfinity, "x2");
    Objective objective = solver.Objective();
    objective.SetMinimization();
    objective.SetCoefficient(x1, 1);
    objective.SetCoefficient(x2, 2);
    Constraint ct = solver.MakeConstraint(17, double.PositiveInfinity);
    ct.SetCoefficient(x1, 3);
    ct.SetCoefficient(x2, 2);
    Solver.ResultStatus resultStatus = solver.Solve();

However, as we'd need the user to specify a model, that the system can run dynamically later (with values it injects), we'd need to be able to specify models like this:

Solver solver = Solver.CreateSolver(solverType);
solver.AddParam("x1", 0);
model = @"
    int: x1;
    var int: x2;
    constraint x1 >= 0;
    constraint x2 >= 0;
    constraint 2*x2 + 3*x1 >= 17;
    solve minimize x1 + 2*x2;
";
results = solver.Solve(model);

The exact syntax doesn't matter. What matters is that the user is specifying the model in a high level modeling language and can specify parameters that we can inject at the back end (in this case, x1).

I searched the documentation, but I couldn't find anything. Admittedly, this documentation appeared incomplete and inaccurate/out of date, but it was all I could find. I also searched the examples, but found nothing.

Although I think OR Tools has an executable that supports external files, I can't use it as we need to avoid any external executables for this solution.

We can parse a JSON string into a Google ProtoBuf. This could be our user model language or even be sugared to form one. However, that still leaves open the question of the exact mapping as again, documentation is lacking.

How can I get Google OR Tools .NET API to implement the example above using a string to specify the model? A code example (parsing the JSON string into the Google ProtoBuf is fine) or definitive (accurate) documentation are all acceptable as an answer.

3 Answers

It sounds like you're wanting to use a dynamic C# scripting library, something like my Data.Eval library, https://github.com/bruce-dunwiddie/data-eval (https://www.nuget.org/packages/Data.Eval/) ? There are others, but the concept seems to match what you're asking for.

You should be able to take the C# code from the "The complete programs" section at https://developers.google.com/optimization/lp/glop#c_7 and convert it to a string and then "Eval" it using whichever library.

I can type up the full example if you think this is what you're looking for, I'm just not familiar with the Google OR Tools, although I am familiar with linear regression solvers.

Console.WriteLine(Evaluator.Eval("return 1+1"));

https://dotnetfiddle.net/DTLu6Z

AMPL and GAMS have APIs that allow this.

Unfortunately, these are commercial systems so they are not free. If I read your post carefully you are looking for a tool like this that has no cost associated with it.

One possible approach is to use the NEOS API to submit optimization tasks expressed in AMPL or GAMS to the NEOS server. See: https://neos-server.org/neos/.

Related