Skip to content

Commit

Permalink
Add support for perIteration callback (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-moulard committed Oct 2, 2013
1 parent 078b7af commit e02af9d
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ IF(HAS_VISIBILITY)
ENDIF()

# Search for dependencies.
SET(BOOST_COMPONENTS
date_time filesystem system thread program_options unit_test_framework)
SEARCH_FOR_BOOST()

# Search for Ipopt.
Expand Down
14 changes: 14 additions & 0 deletions include/roboptim/core/plugin/nag/nag-differentiable.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ namespace roboptim
/// \brief Solve the problem.
void solve () throw ();

void
setIterationCallback (callback_t callback) throw (std::runtime_error)
{
callback_ = callback;
}

const callback_t& callback () const throw ()
{
return callback_;
}

private:
/// \brief Relative accuracy.
double e1_;
Expand All @@ -65,6 +76,9 @@ namespace roboptim
Function::vector_t f_;
/// \brief Current gradient.
Function::vector_t g_;

/// \brief Per-iteration callback function.
callback_t callback_;
};

/// @}
Expand Down
12 changes: 12 additions & 0 deletions include/roboptim/core/plugin/nag/nag-nlp-sparse.hh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ namespace roboptim
/// \brief Solve the problem.
void solve () throw ();

void
setIterationCallback (callback_t callback) throw (std::runtime_error)
{
callback_ = callback;
}

const callback_t& callback () const throw ()
{
return callback_;
}
private:
void compute_nf ();
void fill_xlow_xupp ();
Expand Down Expand Up @@ -121,6 +131,8 @@ private:

Integer ninf_;
double sinf_;

callback_t callback_;
};

/// @}
Expand Down
12 changes: 12 additions & 0 deletions include/roboptim/core/plugin/nag/nag-nlp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ namespace roboptim
/// \brief Solve the problem.
void solve () throw ();

void
setIterationCallback (callback_t callback) throw (std::runtime_error)
{
callback_ = callback;
}

const callback_t& callback () const throw ()
{
return callback_;
}
private:
Integer n_;
Integer nclin_;
Expand All @@ -78,6 +88,8 @@ namespace roboptim
Function::vector_t grad_;
TwiceDifferentiableFunction::hessian_t h_;
Function::vector_t x_;

callback_t callback_;
};

/// @}
Expand Down
14 changes: 14 additions & 0 deletions include/roboptim/core/plugin/nag/nag-simplex.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ namespace roboptim
/// \brief Solve the problem.
void solve () throw ();


void
setIterationCallback (callback_t callback) throw (std::runtime_error)
{
callback_ = callback;
}

const callback_t& callback () const throw ()
{
return callback_;
}
private:
/// \brief Lower bound.
std::vector<double> a_;
Expand All @@ -79,6 +90,9 @@ namespace roboptim
Function::vector_t f_;
/// \brief Current gradient.
Function::vector_t g_;

/// \brief Per-iteration callback function.
callback_t callback_;
};

/// @}
Expand Down
5 changes: 5 additions & 0 deletions src/nag-differentiable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ namespace roboptim

gc_.setZero ();
gc_ = solver->problem ().function ().gradient (x_, 0);

if (!solver->callback ())
return;
DifferentiableFunction::vector_t xCb = x_;
solver->callback () (xCb, solver->problem ());
}
} // end of namespace detail

Expand Down
5 changes: 5 additions & 0 deletions src/nag-nlp-sparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ namespace roboptim
}
assert (offset == leng);
}

if (!solver->callback ())
return;
DifferentiableFunction::vector_t xCb = x_;
solver->callback () (xCb, solver->problem ());
}
} // end of namespace detail

Expand Down
8 changes: 7 additions & 1 deletion src/nag-nlp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ namespace roboptim

if (*mode == 1 || *mode == 2) // evaluate objective gradient
grad_ = solver->problem ().function ().gradient (x_, 0);

if (!solver->callback ())
return;
DifferentiableFunction::vector_t xCb = x_;
solver->callback () (xCb, solver->problem ());
}
} // end of namespace detail

Expand All @@ -136,7 +141,8 @@ namespace roboptim
clamda_ (),
grad_ (),
h_ (),
x_ (pb.function ().inputSize ())
x_ (pb.function ().inputSize ()),
callback_ ()
{
objf_[0] = 0.;
}
Expand Down
5 changes: 5 additions & 0 deletions src/nag-simplex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ namespace roboptim

fc_.setZero ();
fc_ = solver->problem ().function () (x_);

if (!solver->callback ())
return;
Function::vector_t xCb = x_;
solver->callback () (xCb, solver->problem ());
}

static void
Expand Down

0 comments on commit e02af9d

Please sign in to comment.