2020年12月22日星期二

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

I'm working on a feature for a software package. This feature allows users to enter their own models for optimization, which the system would later 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 an example (modified from their example) of how to do a simple linear programming problem:

    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, in 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 of course can vary, but this should give you an idea of what we're looking for. What matters is that the system can run a model provided as a string (or strings), inject parameters into it (in this case, x1), and produces results our code can use.

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 their standalone version supports external files, I can't use it as we need to avoid any external executables for this solution.

How can I get Google OR Tools .NET API to do something like this?

https://stackoverflow.com/questions/65417492/how-to-specify-a-model-as-a-string-for-google-or-tools December 23, 2020 at 08:16AM

没有评论:

发表评论