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.