12.3 The OPF Format

The Optimization Problem Format (OPF) is an alternative to LP and MPS files for specifying optimization problems. It is row-oriented, inspired by the CPLEX LP format.

Apart from containing objective, constraints, bounds etc. it may contain complete or partial solutions, comments and extra information relevant for solving the problem. It is designed to be easily read and modified by hand and to be forward compatible with possible future extensions.

Intended use

The OPF file format is meant to replace several other files:

  • The LP file format: Any problem that can be written as an LP file can be written as an OPF file too; furthermore it naturally accommodates ranged constraints and variables as well as arbitrary characters in names, fixed expressions in the objective, empty constraints, and conic constraints.
  • Parameter files: It is possible to specify integer, double and string parameters along with the problem (or in a separate OPF file).
  • Solution files: It is possible to store a full or a partial solution in an OPF file and later reload it.

12.3.1 The File Format

The format uses tags to structure data. A simple example with the basic sections may look like this:

This is a comment. You may write almost anything here...

# This is a single-line comment.

[objective min 'myobj']
x + 3 y + x^2 + 3 y^2 + z + 1

[con 'con01'] 4 <= x + y  [/con]

[b] -10 <= x,y <= 10  [/b]

[cone quad] x,y,z [/cone]

A scope is opened by a tag of the form [tag] and closed by a tag of the form [/tag]. An opening tag may accept a list of unnamed and named arguments, for examples:

[tag value] tag with one unnamed argument [/tag]
[tag arg=value] tag with one named argument in quotes [/tag]

Unnamed arguments are identified by their order, while named arguments may appear in any order, but never before an unnamed argument. The value can be a quoted, single-quoted or double-quoted text string, i.e.

[tag 'value']     single-quoted value [/tag]
[tag arg='value'] single-quoted value [/tag]
[tag "value"]     double-quoted value [/tag]
[tag arg="value"] double-quoted value [/tag] Sections

The recognized tags are


A comment section. This can contain almost any text: Between single quotes () or double quotes (") any text may appear. Outside quotes the markup characters ([ and ]) must be prefixed by backslashes. Both single and double quotes may appear alone or inside a pair of quotes if it is prefixed by a backslash.


The objective function: This accepts one or two parameters, where the first one (in the above example min) is either min or max (regardless of case) and defines the objective sense, and the second one (above myobj), if present, is the objective name. The section may contain linear and quadratic expressions. If several objectives are specified, all but the last are ignored.


This does not directly contain any data, but may contain the subsection con defining a linear constraint.

[con] defines a single constraint; if an argument is present ([con NAME]) this is used as the name of the constraint, otherwise it is given a null-name. The section contains a constraint definition written as linear and quadratic expressions with a lower bound, an upper bound, with both or with an equality. Examples:

[con 'con1'] 0 <= x + y       [/con]
[con 'con2'] 0 >= x + y       [/con]
[con 'con3'] 0 <= x + y <= 10 [/con]
[con 'con4']      x + y  = 10 [/con]

Constraint names are unique. If a constraint is specified which has the same name as a previously defined constraint, the new constraint replaces the existing one.


This does not directly contain any data, but may contain the subsections b (linear bounds on variables) and cone (quadratic cone).

[b]. Bound definition on one or several variables separated by comma (,). An upper or lower bound on a variable replaces any earlier defined bound on that variable. If only one bound (upper or lower) is given only this bound is replaced. This means that upper and lower bounds can be specified separately. So the OPF bound definition:

[b]  x,y >= -10  [/b]
[b]  x,y <= 10   [/b]

results in the bound \(-10 \leq x,y \leq 10\).

[cone]. currently supports the quadratic cone and the rotated quadratic cone.

A conic constraint is defined as a set of variables which belong to a single unique cone.

  • A quadratic cone of \(n\) variables \(x_1,\ldots ,x_n\) defines a constraint of the form

    \[x_1^2 \geq \sum_{i=2}^n x_i^2,\quad x_1\geq 0.\]
  • A rotated quadratic cone of \(n\) variables \(x_1,\ldots ,x_n\) defines a constraint of the form

    \[2x_1 x_2 \geq \sum_{i=3}^n x_i^2,\quad x_1,x_2\geq 0.\]

A [bounds]-section example:

[b]  0 <= x,y <= 10  [/b] # ranged bound
[b] 10 >= x,y >=  0  [/b] # ranged bound
[b]  0 <= x,y <= inf [/b] # using inf
[b]       x,y free   [/b] # free variables
# Let (x,y,z,w) belong to the cone K
[cone quad]  x,y,z,w  [/cone] # quadratic cone
[cone rquad] x,y,z,w  [/cone] # rotated quadratic cone

By default all variables are free.


This defines an ordering of variables as they should appear in the problem. This is simply a space-separated list of variable names. Optionally, an attribute can be added [variables disallow_new_variables] indicating that if any variable not listed here occurs later in the file it is an error.


This contains a space-separated list of variables and defines the constraint that the listed variables must be integer values.


This may contain only non-essential data; for example estimates of the number of variables, constraints and non-zeros. Placed before all other sections containing data this may reduce the time spent reading the file.

In the hints section, any subsection which is not recognized by MOSEK is simply ignored. In this section a hint in a subsection is defined as follows:

[hint ITEM] value [/hint]

where ITEM may be replaced by numvar (number of variables), numcon (number of linear/quadratic constraints), numanz (number of linear non-zeros in constraints) and numqnz (number of quadratic non-zeros in constraints).


This section can contain a set of full or partial solutions to a problem. Each solution must be specified using a [solution]-section, i.e.

[solution]...[/solution] #solution 1
[solution]...[/solution] #solution 2
#other solutions....
[solution]...[/solution] #solution n

Note that a [solution]-section must be always specified inside a [solutions]-section. The syntax of a [solution]-section is the following:

[solution SOLTYPE status=STATUS]...[/solution]

where SOLTYPE is one of the strings

  • interior, a non-basic solution,
  • basic, a basic solution,
  • integer, an integer solution,

and STATUS is one of the strings


Most of these values are irrelevant for input solutions; when constructing a solution for simplex hot-start or an initial solution for a mixed integer problem the safe setting is UNKNOWN.

A [solution]-section contains [con] and [var] sections. Each [con] and [var] section defines solution information for a single variable or constraint, specified as list of KEYWORD/value pairs, in any order, written as


Allowed keywords are as follows:

  • sk. The status of the item, where the value is one of the following strings:
    • LOW, the item is on its lower bound.
    • UPR, the item is on its upper bound.
    • FIX, it is a fixed item.
    • BAS, the item is in the basis.
    • SUPBAS, the item is super basic.
    • UNK, the status is unknown.
    • INF, the item is outside its bounds (infeasible).
  • lvl Defines the level of the item.
  • sl Defines the level of the dual variable associated with its lower bound.
  • su Defines the level of the dual variable associated with its upper bound.
  • sn Defines the level of the variable associated with its cone.
  • y Defines the level of the corresponding dual variable (for constraints only).

A [var] section should always contain the items sk, lvl, sl and su. Items sl and su are not required for integer solutions.

A [con] section should always contain sk, lvl, sl, su and y.

An example of a solution section

[solution basic status=UNKNOWN]
[var x0] sk=LOW    lvl=5.0      [/var]
[var x1] sk=UPR    lvl=10.0     [/var]
[var x2] sk=SUPBAS lvl=2.0  sl=1.5 su=0.0 [/var]

[con c0] sk=LOW    lvl=3.0 y=0.0 [/con]
[con c0] sk=UPR    lvl=0.0 y=5.0 [/con]
  • [vendor] This contains solver/vendor specific data. It accepts one argument, which is a vendor ID – for MOSEK the ID is simply mosek – and the section contains the subsection parameters defining solver parameters. When reading a vendor section, any unknown vendor can be safely ignored. This is described later.

Comments using the # may appear anywhere in the file. Between the # and the following line-break any text may be written, including markup characters. Numbers

Numbers, when used for parameter values or coefficients, are written in the usual way by the printf function. That is, they may be prefixed by a sign (+ or -) and may contain an integer part, decimal part and an exponent. The decimal point is always . (a dot). Some examples are


Some invalid examples are

e10   # invalid, must contain either integer or decimal part
.     # invalid
.e10  # invalid

More formally, the following standard regular expression describes numbers as used:

[+|-]?([0-9]+[.][0-9]*|[.][0-9]+)([eE][+|-]?[0-9]+)? Names

Variable names, constraint names and objective name may contain arbitrary characters, which in some cases must be enclosed by quotes (single or double) that in turn must be preceded by a backslash. Unquoted names must begin with a letter (a-z or A-Z) and contain only the following characters: the letters a-z and A-Z, the digits 0-9, braces ({ and }) and underscore (_).

Some examples of legal names:

'single quoted name'
"double quoted name"
"name with \\"quote\\" in it"
"name with []s in it"

12.3.2 Parameters Section

In the vendor section solver parameters are defined inside the parameters subsection. Each parameter is written as

[p PARAMETER_NAME] value [/p]

where PARAMETER_NAME is replaced by a MOSEK parameter name, usually of the form MSK_IPAR_..., MSK_DPAR_... or MSK_SPAR_..., and the value is replaced by the value of that parameter; both integer values and named values may be used. Some simple examples are

[vendor mosek]
[p MSK_DPAR_DATA_TOL_BOUND_INF]     1.0e18 [/p]

12.3.3 Writing OPF Files from MOSEK

To write an OPF file set the parameter MSK_IPAR_WRITE_DATA_FORMAT to MSK_DATA_FORMAT_OP as this ensures that OPF format is used.

Then modify the following parameters to define what the file should contain:

MSK_IPAR_OPF_WRITE_SOL_BAS Include basic solution, if defined.
MSK_IPAR_OPF_WRITE_SOL_ITG Include integer solution, if defined.
MSK_IPAR_OPF_WRITE_SOL_ITR Include interior solution, if defined.
MSK_IPAR_OPF_WRITE_SOLUTIONS Include solutions if they are defined. If this is off, no solutions are included.
MSK_IPAR_OPF_WRITE_HEADER Include a small header with comments.
MSK_IPAR_OPF_WRITE_PROBLEM Include the problem itself — objective, constraints and bounds.
MSK_IPAR_OPF_WRITE_PARAMETERS Include all parameter settings.
MSK_IPAR_OPF_WRITE_HINTS Include hints about the size of the problem.

12.3.4 Examples

This section contains a set of small examples written in OPF and describing how to formulate linear, quadratic and conic problems. Linear Example lo1.opf

Consider the example:

\[\begin{split}\begin{array}{lccccccccl} \mbox{maximize} & 3 x_0 & + & 1 x_1 & + & 5 x_2 & + & 1 x_3 & &\\ \mbox{subject to} & 3 x_0 & + & 1 x_1 & + & 2 x_2 & & & = & 30,\\ & 2 x_0 & + & 1 x_1 & + & 3 x_2 & + & 1 x_3 & \geq & 15,\\ & & & 2 x_1 & & & + & 3 x_3 & \leq & 25, \end{array}\end{split}\]

having the bounds

\[\begin{split}\begin{array}{ccccc} 0 & \leq & x_0 & \leq & \infty ,\\ 0 & \leq & x_1 & \leq & 10,\\ 0 & \leq & x_2 & \leq & \infty ,\\ 0 & \leq & x_3 & \leq & \infty . \end{array}\end{split}\]

In the OPF format the example is displayed as shown in Listing 12.1.

Listing 12.1 Example of an OPF file for a linear problem. Click here to download.
  The lo1 example in OPF format

  [hint NUMVAR] 4 [/hint]
  [hint NUMCON] 3 [/hint]
  [hint NUMANZ] 9 [/hint]

[variables disallow_new_variables]
  x1 x2 x3 x4 

[objective maximize 'obj']
   3 x1 + x2 + 5 x3 + x4

  [con 'c1'] 3 x1 +   x2 + 2 x3         = 30 [/con]
  [con 'c2'] 2 x1 +   x2 + 3 x3 +   x4 >= 15 [/con]
  [con 'c3']        2 x2        + 3 x4 <= 25 [/con]

  [b] 0 <= * [/b]
  [b] 0 <= x2 <= 10 [/b]
[/bounds] Quadratic Example qo1.opf

An example of a quadratic optimization problem is

\[\begin{split}\begin{array}{lcccl} \mbox{minimize} & & & x_1^2 + 0.1 x_2^2 + x_3^2 - x_1 x_3 - x_2 & \\ \mbox{subject to} & 1 & \leq & x_1 + x_2 + x_3, & \\ & & & x \geq 0. & \end{array}\end{split}\]

This can be formulated in opf as shown below.

Listing 12.2 Example of an OPF file for a quadratic problem. Click here to download.
  The qo1 example in OPF format

  [hint NUMVAR] 3 [/hint]
  [hint NUMCON] 1 [/hint]
  [hint NUMANZ] 3 [/hint]
  [hint NUMQNZ] 4 [/hint]

[variables disallow_new_variables]
  x1 x2 x3 

[objective minimize 'obj']
  # The quadratic terms are often written with a factor of 1/2 as here, 
  # but this is not required.

   - x2 + 0.5 ( 2.0 x1 ^ 2 - 2.0 x3 * x1 + 0.2 x2 ^ 2 + 2.0 x3 ^ 2 )

  [con 'c1'] 1.0 <= x1 + x2 + x3 [/con]

  [b] 0 <= * [/b]
[/bounds] Conic Quadratic Example cqo1.opf

Consider the example:

\[\begin{split}\begin{array}{lccccc} \mbox{minimize} & x_3 + x_4 + x_5 & & & &\\ \mbox{subject to} & x_0 + x_1 + 2 x_2 & = & 1, & &\\ & x_0,x_1,x_2 & \geq & 0, & &\\ & x_3 \geq \sqrt{x_0^2 + x_1^2}, & & & &\\ & 2 x_4 x_5 \geq x_2^2. & & & & \end{array}\end{split}\]

Please note that the type of the cones is defined by the parameter to [cone ...]; the content of the cone-section is the names of variables that belong to the cone. The resulting OPF file is in Listing 12.3.

Listing 12.3 Example of an OPF file for a conic quadratic problem. Click here to download.
  The cqo1 example in OPF format.

  [hint NUMVAR] 6 [/hint]
  [hint NUMCON] 1 [/hint]
  [hint NUMANZ] 3 [/hint]

[variables disallow_new_variables]
  x1 x2 x3 x4 x5 x6 

[objective minimize 'obj']
   x4 + x5 + x6

  [con 'c1']  x1 + x2 + 2e+00 x3 = 1e+00 [/con]

  # We let all variables default to the positive orthant
  [b] 0 <= * [/b]

  # ...and change those that differ from the default
  [b] x4,x5,x6 free [/b]

  # Define quadratic cone: x4 >= sqrt( x1^2 + x2^2 )
  [cone quad 'k1'] x4, x1, x2 [/cone]

  # Define rotated quadratic cone: 2 x5 x6 >= x3^2
  [cone rquad 'k2'] x5, x6, x3 [/cone]
[/bounds] Mixed Integer Example milo1.opf

Consider the mixed integer problem:

\[\begin{split}\begin{array}{lccl} \mbox{maximize} & x_0 + 0.64 x_1 & & \\ \mbox{subject to} & 50 x_0 + 31 x_1 & \leq & 250, \\ & 3 x_0 - 2 x_1 & \geq & -4, \\ & x_0, x_1 \geq 0 & & \mbox{and integer} \end{array}\end{split}\]

This can be implemented in OPF with the file in Listing 12.4.

Listing 12.4 Example of an OPF file for a mixed-integer linear problem. Click here to download.
  The milo1 example in OPF format

  [hint NUMVAR] 2 [/hint]
  [hint NUMCON] 2 [/hint]
  [hint NUMANZ] 4 [/hint]

[variables disallow_new_variables]
  x1 x2 

[objective maximize 'obj']
   x1 + 6.4e-1 x2

  [con 'c1'] 5e+1 x1 + 3.1e+1 x2 <= 2.5e+2 [/con]
  [con 'c2'] -4 <= 3 x1 - 2 x2 [/con]

  [b] 0 <= * [/b]

  x1 x2