add_constraint, add_constraintex, str_add_constraint

Add a constraint to the lp.

unsigned char add_constraint(lprec *lp, REAL *row, int constr_type, REAL rh);

unsigned char add_constraintex(lprec *lp, int count, REAL *row, int *colno, int constr_type, REAL rh);

unsigned char str_add_constraint(lprec *lp, char *row_string, int constr_type, REAL rh);

Return Value

add_constraint, add_constraintex and str_add_constraint return TRUE (1) if the operation was successful. A return value of FALSE (0) indicates an error.



Pointer to previously created lp model. See return value of make_lp, copy_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI


Number of elements in row and colno.


An array with 1+get_Ncolumns (count for add_constraintex, if colno is different from NULL) elements that contains the values of the row.


A zero-based array with count elements that contains the column numbers of the row. However this variable can also be NULL. In that case element i in the variable row is column i and values start at element 1.


A string with column elements that contains the values of the row. Each element must be separated by space(s).


The type of the constraint. Can by any of the following values:

LE (1) Less than or equal (<=)
EQ (3) Equal (=)
GE (2) Greater than or equal (>=)


The value of the right hand side (RHS).


The add_constraint, add_constraintex, str_add_constraint functions add a row to the model (at the end) and sets all values of the row at once.

Note that for add_constraint (and add_constraintex when colno is NULL) element 0 of the array is not considered (i.e. ignored). Column 1 is element 1, column 2 is element 2, ...

str_add_constraint should only be used in small or demo code since it is not performant and uses more memory.

add_constraintex has the possibility to specify only the non-zero elements. In that case colno specifies the column numbers of the non-zero elements. Both row and colno are then zero-based arrays. This will speed up building the model considerably if there are a lot of zero values. In most cases the matrix is sparse and has many zero value.
Note that add_constraintex behaves the same as add_constraint when colno is NULL.

Thus it is almost always better to use add_constraintex instead of add_constraint. add_constraintex is always at least as performant as add_constraint.

Note that it is advised to set the objective function (via set_obj_fn, set_obj_fnex, str_set_obj_fn, set_obj) before adding rows. This especially for larger models. This will be much more performant than adding the objective function afterwards.

Note that these routines will perform much better when set_add_rowmode is called before adding constraints.

Note that if you have to add many constraints, performance can be improved by a call to resize_lp.


#include <stdio.h>
#include <stdlib.h>
#include "lp_lib.h"

int main(void)
  lprec *lp;
  REAL row[1+3];     /* must be 1 more than number of columns ! */
  REAL sparserow[2]; /* must be the number of non-zero values */
  int colno[2];

  /* Create a new LP model */
  lp = make_lp(0, 3);
  if(lp == NULL) {
    fprintf(stderr, "Unable to create new LP model\n");

  set_add_rowmode(lp, TRUE);

  row[1] = 1.0;
  row[2] = 0.0; /* also zero elements must be provided */
  row[3] = 2.0;
  add_constraint(lp, row, GE, 3.0); /* constructs the row: +v_1 +2 v_3 >= 3 */

  colno[0] = 1; sparserow[0] = 1.0; /* column 1 */
  colno[1] = 3; sparserow[1] = 2.0; /* column 3 */
  add_constraintex(lp, 2, sparserow, colno, GE, 3.0); /* constructs the row: +v_1 +2 v_3 >= 3 */

  set_add_rowmode(lp, FALSE);


lp_solve API reference

See Also make_lp, copy_lp, copy_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI, set_row, set_rowex, set_obj_fn, set_obj_fnex, str_set_obj_fn, set_obj, set_add_rowmode, is_add_rowmode, resize_lp, get_constr_type, is_constr_type, del_constraint, add_column, add_columnex, str_add_column, set_column, set_columnex, get_column, get_columnex, get_row, get_rowex, get_mat