set_obj_fn, set_obj_fnex, str_set_obj_fn, set_obj

Set the objective function (row 0) of the matrix.

unsigned char set_obj_fn(lprec *lp, REAL *row);

unsigned char set_obj_fnex(lprec *lp, int count, REAL *row, int *colno);

unsigned char str_set_obj_fn(lprec *lp, char *row_string);

unsigned char set_obj(lprec *lp, int column, REAL value);

Return Value

set_obj_fn, set_obj_fnex, str_set_obj_fn and set_obj 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 set_obj_fnex) elements that contains the values of the objective function.


An 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.


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


The column number for which the value must be set.


The value that must be set.


The set_obj_fn, set_obj_fnex, str_set_obj_fn functions set all values of the objective function at once.
Note that for set_obj_fn (and set_obj_fnex 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, ...
set_obj_fnex has the possibility to specify only the non-zero elements. In that case colno specifies the column numbers of the non-zero elements. 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. Thus it is almost always better to use set_obj_fnex instead of set_obj_fn. set_obj_fnex is always at least as performant as set_obj_fn. Note that unspecified values by set_obj_fnex are set to zero.
The set_obj function sets the objective value for the specified column. If multiple objective values must be set, it is more performant to use set_obj_fnex.
Note that it is advised to set the objective function before adding rows via add_constraint, add_constraintex, str_add_constraint. This especially for larger models. This will be much more performant than adding the objective function afterwards.


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

int main(void)
  lprec *lp;
  REAL row[1+2]; /* must be 1 more then number of columns ! */

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

  row[1] = 1.0;
  row[2] = 2.0;
  set_obj_fn(lp, row);  /* constructs the obj: +v_1 +2 v_2 */


lp_solve API reference

See Also make_lp, copy_lp, read_lp, read_LP, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI, add_constraint, add_constraintex, str_add_constraint, set_row, set_rowex, add_column, add_columnex, str_add_column, set_column, set_columnex, get_column, get_columnex, get_row, get_rowex, get_mat