next up previous contents
Next: Method eval_g Up: Coding the Problem Representation Previous: Method eval_f   Contents

Method eval_grad_f

with prototype
virtual bool eval_grad_f(Index n, const Number* x, bool new_x, 
                         Number* grad_f)
Return the gradient of the objective function at the point $ x$ .

The gradient array is in the same order as the $ x$ variables (i.e., the gradient of the objective with respect to x[2] should be put in grad_f[2]).

The boolean variable new_x will be false if the last call to any of the evaluation methods (eval_*) used the same $ x$ values. This can be helpful when users have efficient implementations that calculate multiple outputs at once. IPOPT internally caches results from the TNLP and generally, this flag can be ignored.

The variable n is passed in for your convenience. This variable will have the same value you specified in get_nlp_info.

In our example, we ignore the new_x flag and calculate the values for the gradient of the objective.

bool HS071_NLP::eval_grad_f(Index n, const Number* x, bool new_x, Number* grad_f)
{
  assert(n == 4);

  grad_f[0] = x[0] * x[3] + x[3] * (x[0] + x[1] + x[2]);
  grad_f[1] = x[0] * x[3];
  grad_f[2] = x[0] * x[3] + 1;
  grad_f[3] = x[0] * (x[0] + x[1] + x[2]);

  return true;
}


next up previous contents
Next: Method eval_g Up: Coding the Problem Representation Previous: Method eval_f   Contents
Andreas Waechter 2008-08-26