cplex_solution_read, a MATLAB code which extracts solution data from a CPLEX result file.

CPLEX can be "fed" by an LP file created by polyomino_monohedral_matrix() or polyomino_multihedral_matrix(), and the results can be displayed by polyomino_monohedral_tiling_print() or polyomino_multihedral_tiling_print().

CPLEX is a general integer programming package. We use CPLEX to solve a particular set of undetermined integer equations of the form A*x=b, for which the solution x is required to be binary, that is, to contain only 0 and 1 values.

CPLEX can compute all possible solutions, and returns them packaged in a single XML file, which also contains other information, and substantial formatting text. It is desired to extract the NUM_VAR by NUM_SOL array of solution vectors X, for treatment by MATLAB.

A program called XML2STRUCT is employed to convert XML to a MATLAB structure. Then the necessary solution data is carefully extracted and the array X is reconstructed and output.

Although it is not necesary to know this, the particular integer programming problem being solved is related to a polyomino tiling puzzle. CPLEX is a powerful and accurate tool for solving the related system. To learn more about the problem being solved, you can refer to the simple MATLAB codes for attempting this solution: POLYOMINO_MONOHEDRAL and POLYOMINO_MULTIHEDRAL.

The information stored in the CPLEX file has a number of peculiarities. For one thing, if there are N solutions, and N is more than 1, then, CPLEX displays N+1 solutions, since it wishes to specifically display an "optimal" solution. Thus, when gathering solutions from a CPLEX file, if the number of solutions is greater than 1, then the first solution should be dropped.

In listing a particular solution, depending on the user options, CPLEX might only display the nonzero entries. Since most entries are zero, this keeps the output short; however, this also means that the CPLEX file never actually lists the total number of variables. Thus, we prefer that the user specifically ask CPLEX to story ALL variable values, including zeros. Otherwise, the returned solution information may be confusingly shortened because trailing zeros may be omitted.

Apparently, during the solution process, CPLEX reindexes the variables. The output listing of the variables include the user's original index (or label, actually, such as "x20"), and the CPLEX index. Thus, we have to ignore the CPLEX index, read the string "x20", drop the "x" and convert the remainder from string to number.


The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Related Programs and Data:


gurobi_solution_read, a MATLAB code which reads a file created by the optimization package GUROBI, representing the solution of a polyomino tiling problem, and writes out a simple ASCII file that can be read by load().

polyominoes, a MATLAB code which defines, solves, and plots a variety of polyomino tiling problems, which are solved by a direct algebraic approach involving the reduced row echelon form (RREF) of a specific matrix, instead of the more typical brute-force or backtracking methods.

scip_solution_read, a MATLAB code which reads a file created by the integer programming package SCIP, representing the solution of a polyomino tiling problem, and writes out a simple ASCII file that can be read by load().

trinity_solution, a MATLAB code which prints or plots a solution to the trinity tiling puzzle, after it has been defined by trinity_lp_automatic() and solved by cplex().


  1. IBM ILOG CPLEX Optimization Studio,
    Getting Started with CPLEX.
  2. IBM ILOG CPLEX Optimization Studio,
    CPLEX User's Manual.

Source Code:

Last revised on 27 May 2021.