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.

Parameters

lp

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

count

Number of elements in row and colno.

row

An array with 1+column (count for set_obj_fnex) elements that contains the values of the objective function.

colno

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.

row_string

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

column

The column number for which the value must be set.

value

The value that must be set.

Remarks

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_fnex. Note however that set_obj_fnex, when colno is not NULL, only sets the specified values. The other columns are not set to zero and thus keep their values. This is no problem when the routine is called the first time, but if it is called again, you must consider this site effect.
The set_obj function sets the objective value for the specified column. It is as performant as using 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.

Example

#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");
    return(1);
  }

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

  delete_lp(lp);
  return(0);
}

lp_solve API reference

See Also make_lp, read_lp, read_LP, read_lpt, read_LPT, read_mps, read_freemps, read_MPS, read_freeMPS, read_XLI, add_constraint, add_constraintex, str_add_constraint, add_column, add_columnex, str_add_column, get_column, get_row, get_mat