Skip to main content

Function: model2txt

model2txt(model, params?, log?): Promise<string | undefined>

Converts a model into a text format similar to IBM CP Optimizer file format. The result is human readable and can be stored in a file.


NameTypeDefault valueDescription
modelModelundefinedThe model to be exported.
params?ParametersundefinedThe parameters to pass to the solver (they are mostly unused).
logundefined | WritableStreamprocess.stdoutThe stream to which the solver output should be redirected.


Promise<string | undefined>

A string containing the model in text format.


Unlike JSON format, there is no way to convert the text format back into an instance of Model.

The result so similar to the file format used by IBM CP Optimizer that, under some circumstances, the result can be used as an input file for CP Optimizer. However there are some differences between OptalCP and CP Optimizer that makes it impossible to make sure the result is always valid for CP Optimizer. Known issues are:

  • OptalCP supports optional integer expressions, while CP Optimizer does not. If the model contains optional integer expressions, the result will not be valid for CP Optimizer or it will be badly interpreted. For example, in order to get valid CP Optimizer file, don't use function start, use startOr instead.
  • For the same reason, prefer precedence constraints such as endBeforeStart over constraint(x.end().le(y.start())).
  • Negative heights in cumulative expressions (e.g. in stepAtStart) are not supported by CP Optimizer.

The function is using OptalCP solver for the conversion, therefore it is asynchronous. To wait the result, use await keyword.

In case of an error this function returns a rejected promise.