ampl / mp

An open-source library for mathematical programming
https://mp.ampl.com
Other
227 stars 42 forks source link

Add an NL writer #30

Open vitaut opened 9 years ago

vitaut commented 9 years ago

Add an NL writer. Requirements:

This can potentially be used in https://github.com/casadi/casadi/issues/755.

tkelman commented 9 years ago

Having an extern "C" API that's accessible from high-level languages would be a nice plus. We could write our own if necessary though. edit: done in https://github.com/JackDunnNZ/NL.jl, so probably not necessary

vitaut commented 9 years ago

It is already possible to write an NL file using existing APIs:

#include "asl/aslbuilder.h"

using namespace mp;

int main() {
  // Build problem in ASL form.
  ASL *asl = ASL_alloc(ASL_read_fg);
  asl::internal::ASLBuilder b(asl);
  auto info = ProblemInfo();
  info.num_vars = info.num_nl_vars_in_objs = 1;
  info.num_nl_objs = info.num_objs = 1;
  b.SetInfo(info);
  b.AddVar(-100, 100, var::CONTINUOUS);
  // x - 42
  auto expr = b.MakeBinary(expr::SUB, b.MakeVariable(0), b.MakeNumericConstant(42));
  // minimize o: (x - 42) ^ 2
  b.AddObj(obj::MIN, b.MakeUnary(expr::POW2, expr), 1).AddTerm(0, 0);
  // Write .nl file.
  fg_write("test.nl", 0, ASL_write_ASCII);
}

However, this is not very user friendly as ProblemInfo object must be populated manually and Jacobian/gradient nonzero pattern too. Should be pretty easy to improve the API to reduce the above code to something like:

  asl::ASLBuilder b;
  auto x = b.AddVar(-100, 100);
  b.AddObj(obj::MIN, pow2(x - 2));
  WriteNLFile(b, "test.nl");

which is like a poor man's modeling language without model/data separation, presolve and other goodies =).

The next step would be to provide a more advanced reverse-SAX-like API for writing NL files without building any problem representation.

glebbelov commented 10 months ago

Reopening.

C++ API ready, C API being worked on.