Skip to content

Commit e65b912

Browse files
committed
Merge pull request #31 from fdarricau/non-templated-problem
Remove function-based template arguments for Problem cf. roboptim/roboptim-core#98
2 parents 24b0a48 + cfc89ec commit e65b912

11 files changed

Lines changed: 146 additions & 165 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ env:
66
global:
77
- APT_DEPENDENCIES="doxygen doxygen-latex libltdl-dev libboost-all-dev liblog4cxx10-dev coinor-libipopt-dev libblas-dev liblapack-dev libmumps-seq-dev gfortran"
88
- HOMEBREW_DEPENDENCIES="doxygen libtool boost log4cxx ipopt openblas mumps"
9-
- GIT_DEPENDENCIES="roboptim/roboptim-core"
9+
- GIT_DEPENDENCIES="roboptim/roboptim-core:dev"
1010
- DEBSIGN_KEYID=5AE5CD75
1111
- PPA_URI="roboptim/ppa"
1212
- GH_USERNAME=thomas-moulard
@@ -24,6 +24,7 @@ notifications:
2424
branches:
2525
only:
2626
- master
27+
- dev
2728
- debian
2829
- travis
2930
- coverity_scan

include/roboptim/core/plugin/ipopt/ipopt-sparse.hh

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,11 @@ namespace roboptim
6060
/// to provide hessians in your function's problems.
6161
class ROBOPTIM_DLLEXPORT IpoptSolverSparse
6262
: public IpoptSolverCommon<
63-
Solver<DifferentiableSparseFunction,
64-
boost::mpl::vector<LinearSparseFunction,
65-
DifferentiableSparseFunction> > >
63+
Solver<EigenMatrixSparse> >
6664
{
6765
public:
6866
/// \brief RobOptim solver type.
69-
typedef Solver<
70-
DifferentiableSparseFunction,
71-
boost::mpl::vector<LinearSparseFunction,
72-
DifferentiableSparseFunction> > solver_t;
67+
typedef Solver<EigenMatrixSparse> solver_t;
7368

7469
/// \brief Parent type.
7570
typedef IpoptSolverCommon<solver_t> parent_t;

include/roboptim/core/plugin/ipopt/ipopt-td.hh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,11 @@ namespace roboptim
5555
/// to provide hessians in your function's problems.
5656
class ROBOPTIM_DLLEXPORT IpoptSolverTd
5757
: public IpoptSolverCommon<
58-
Solver <TwiceDifferentiableFunction,
59-
boost::mpl::vector<LinearFunction, TwiceDifferentiableFunction> > >
58+
Solver <EigenMatrixDense> >
6059
{
6160
public:
6261
/// \brief RobOptim solver type.
63-
typedef Solver<TwiceDifferentiableFunction,
64-
boost::mpl::vector<LinearFunction,
65-
TwiceDifferentiableFunction> > solver_t;
62+
typedef Solver<EigenMatrixDense> solver_t;
6663

6764
/// \brief Parent type.
6865
typedef IpoptSolverCommon<solver_t> parent_t;

include/roboptim/core/plugin/ipopt/ipopt.hh

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,11 @@ namespace roboptim
5858
/// to provide hessians in your function's problems.
5959
class ROBOPTIM_DLLEXPORT IpoptSolver
6060
: public IpoptSolverCommon<
61-
Solver<DifferentiableFunction,
62-
boost::mpl::vector<LinearFunction, DifferentiableFunction> > >
61+
Solver<EigenMatrixDense> >
6362
{
6463
public:
6564
/// \brief RobOptim solver type.
66-
typedef Solver<
67-
DifferentiableFunction,
68-
boost::mpl::vector<LinearFunction, DifferentiableFunction> > solver_t;
65+
typedef Solver<EigenMatrixDense> solver_t;
6966

7067
/// \brief Parent type.
7168
typedef IpoptSolverCommon<solver_t> parent_t;

src/ipopt-sparse.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434

3535
namespace roboptim
3636
{
37-
typedef Solver<DifferentiableSparseFunction,
38-
boost::mpl::vector<LinearSparseFunction,
39-
DifferentiableSparseFunction> >
40-
ipopt_solver_t;
37+
typedef Solver<EigenMatrixSparse> ipopt_solver_t;
4138

4239
template class IpoptSolverCommon<ipopt_solver_t>;
4340

src/ipopt-td.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434

3535
namespace roboptim
3636
{
37-
typedef Solver<TwiceDifferentiableFunction,
38-
boost::mpl::vector<LinearFunction,
39-
TwiceDifferentiableFunction> >
40-
solver_ipopt_td_t;
37+
typedef Solver<EigenMatrixDense> solver_ipopt_td_t;
4138

4239
template class IpoptSolverCommon<solver_ipopt_td_t>;
4340

src/ipopt.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434

3535
namespace roboptim
3636
{
37-
typedef Solver<DifferentiableFunction,
38-
boost::mpl::vector<LinearFunction, DifferentiableFunction> >
39-
ipopt_solver_t;
37+
typedef Solver<EigenMatrixDense> ipopt_solver_t;
4038

4139
template class IpoptSolverCommon<ipopt_solver_t>;
4240

src/tnlp.cc

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace roboptim
4242
{
4343
using namespace boost;
4444

45-
n = static_cast<Index> (solver_.problem ().function ().inputSize ());
45+
n = static_cast<Index> (costFunction_->inputSize ());
4646
m = static_cast<Index> (constraintsOutputSize ());
4747

4848
function_t::vector_t x (n);
@@ -56,18 +56,12 @@ namespace roboptim
5656

5757
// compute number of non zeros elements in jacobian constraint.
5858
nnz_jac_g = 0;
59-
typedef solver_t::problem_t::constraints_t::const_iterator
59+
typedef std::vector<differentiableFunctionPtr_t>::const_iterator
6060
citer_t;
61-
for (citer_t it = solver_.problem ().constraints ().begin ();
62-
it != solver_.problem ().constraints ().end (); ++it)
61+
for (citer_t it = differentiableConstraintFunctions_.begin ();
62+
it != differentiableConstraintFunctions_.end (); ++it)
6363
{
64-
shared_ptr<solver_t::commonConstraintFunction_t> g;
65-
if (it->which () == LINEAR)
66-
g = get<shared_ptr<linearFunction_t> > (*it);
67-
else
68-
g = get<shared_ptr<nonLinearFunction_t> > (*it);
69-
70-
nnz_jac_g += g->jacobian (x).nonZeros ();
64+
nnz_jac_g += (*it)->jacobian (x).nonZeros ();
7165
}
7266

7367

@@ -87,14 +81,14 @@ namespace roboptim
8781
using namespace boost;
8882
ROBOPTIM_DEBUG_ONLY
8983
(function_t::size_type n_ = static_cast<function_t::size_type> (n));
90-
assert (solver_.problem ().function ().inputSize () == n_);
84+
assert (costFunction_->inputSize () == n_);
9185
assert (constraintsOutputSize () == m);
9286

9387
if (!jacobian_)
9488
{
95-
jacobian_ = function_t::jacobian_t
96-
(static_cast<function_t::matrix_t::Index> (constraintsOutputSize ()),
97-
solver_.problem ().function ().inputSize ());
89+
jacobian_ = differentiableFunction_t::jacobian_t
90+
(static_cast<differentiableFunction_t::matrix_t::Index> (constraintsOutputSize ()),
91+
costFunction_->inputSize ());
9892
jacobian_->reserve (nele_jac);
9993
}
10094

@@ -111,15 +105,15 @@ namespace roboptim
111105
// First evaluate the constraints in zero to build the
112106
// constraints jacobian.
113107
int idx = 0;
114-
typedef solver_t::problem_t::constraints_t::const_iterator
108+
typedef std::vector<differentiableFunctionPtr_t>::const_iterator
115109
citer_t;
116110
unsigned constraintId = 0;
117111

118112
typedef Eigen::Triplet<double> triplet_t;
119113
std::vector<triplet_t> coefficients;
120114

121-
for (citer_t it = solver_.problem ().constraints ().begin ();
122-
it != solver_.problem ().constraints ().end ();
115+
for (citer_t it = differentiableConstraintFunctions_.begin ();
116+
it != differentiableConstraintFunctions_.end ();
123117
++it, ++constraintId)
124118
{
125119
LOG4CXX_TRACE
@@ -160,23 +154,17 @@ namespace roboptim
160154
else // other use initial guess.
161155
x = *(solver_.problem ().startingPoint ());
162156

163-
shared_ptr<solver_t::commonConstraintFunction_t> g;
164-
if (it->which () == LINEAR)
165-
g = get<shared_ptr<linearFunction_t> > (*it);
166-
else
167-
g = get<shared_ptr<nonLinearFunction_t> > (*it);
168-
169-
function_t::jacobian_t jacobian = g->jacobian (x);
157+
differentiableFunction_t::jacobian_t jacobian = (*it)->jacobian (x);
170158
for (int k = 0; k < jacobian.outerSize (); ++k)
171-
for (function_t::jacobian_t::InnerIterator
159+
for (differentiableFunction_t::jacobian_t::InnerIterator
172160
it (jacobian, k); it; ++it)
173161
{
174162
const int row = static_cast<int> (idx + it.row ());
175163
const int col = static_cast<int> (it.col ());
176164
coefficients.push_back
177165
(triplet_t (row, col, it.value ()));
178166
}
179-
idx += g->outputSize ();
167+
idx += (*it)->outputSize ();
180168
}
181169

182170
jacobian_->setFromTriplets
@@ -190,7 +178,7 @@ namespace roboptim
190178
idx = 0;
191179

192180
for (int k = 0; k < jacobian_->outerSize (); ++k)
193-
for (function_t::jacobian_t::InnerIterator it (*jacobian_, k);
181+
for (differentiableFunction_t::jacobian_t::InnerIterator it (*jacobian_, k);
194182
it; ++it)
195183
{
196184
iRow[idx] = it.row (), jCol[idx] = it.col ();
@@ -208,35 +196,29 @@ namespace roboptim
208196

209197
Eigen::Map<const function_t::vector_t> x_ (x, n);
210198

211-
typedef solver_t::problem_t::constraints_t::const_iterator
199+
typedef std::vector<differentiableFunctionPtr_t>::const_iterator
212200
citer_t;
213201

214202
int idx = 0;
215203
int constraintId = 0;
216-
for (citer_t it = solver_.problem ().constraints ().begin ();
217-
it != solver_.problem ().constraints ().end (); ++it)
204+
for (citer_t it = differentiableConstraintFunctions_.begin ();
205+
it != differentiableConstraintFunctions_.end (); ++it)
218206
{
219-
shared_ptr<solver_t::commonConstraintFunction_t> g;
220-
if (it->which () == LINEAR)
221-
g = get<shared_ptr<linearFunction_t> > (*it);
222-
else
223-
g = get<shared_ptr<nonLinearFunction_t> > (*it);
224-
225207
// TODO: use middleRows once Eigen is fixed
226208
// TODO: avoid allocation here (may be solved with
227209
// http://eigen.tuxfamily.org/bz/show_bug.cgi?id=910)
228-
copySparseBlock (*jacobian_, g->jacobian (x_), idx, 0);
229-
idx += g->outputSize ();
210+
copySparseBlock (*jacobian_, (*it)->jacobian (x_), idx, 0);
211+
idx += (*it)->outputSize ();
230212

231213
IpoptCheckGradient
232-
(*g, 0, x_,
214+
(*(*it), 0, x_,
233215
constraintId++, solver_);
234216
}
235217

236218
// Copy jacobian values from internal sparse matrix.
237219
idx = 0;
238220
for (int k = 0; k < jacobian_->outerSize (); ++k)
239-
for (function_t::jacobian_t::InnerIterator it (*jacobian_, k);
221+
for (differentiableFunction_t::jacobian_t::InnerIterator it (*jacobian_, k);
240222
it; ++it)
241223
{
242224
assert (idx < nele_jac);

src/tnlp.hh

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,56 @@ namespace roboptim
163163
/// \brief Current state of the solver (used by the callback function).
164164
solverState_t solverState_;
165165

166-
/// \brief Cost function type for this problem.
167-
typedef typename solver_t::problem_t::function_t function_t;
166+
/// \brief Traits of the cost function of this problem
167+
typedef typename solver_t::problem_t::function_t::traits_t traits_t;
168+
169+
/// \brief Function type of this problem
170+
typedef GenericFunction<traits_t> function_t;
171+
172+
/// \brief Differentiable function type of this problem
173+
typedef GenericDifferentiableFunction<traits_t> differentiableFunction_t;
174+
175+
/// \brief Twice Differentiable function type of this problem
176+
typedef GenericTwiceDifferentiableFunction<traits_t> twiceDifferentiableFunction_t;
177+
178+
/// \brief Function pointer type of this problem
179+
typedef boost::shared_ptr<const function_t> functionPtr_t;
180+
181+
/// \brief Differentiable function pointer type of this problem
182+
typedef boost::shared_ptr<const differentiableFunction_t> differentiableFunctionPtr_t;
183+
184+
/// \brief Twice Differentiable function pointer type of this problem
185+
typedef boost::shared_ptr<const twiceDifferentiableFunction_t> twiceDifferentiableFunctionPtr_t;
186+
187+
/// \brief Cost function
188+
const function_t* costFunction_;
189+
190+
/// \brief Differentiable cost function
191+
const differentiableFunction_t* differentiableCostFunction_;
192+
193+
/// \brief Twice Differentiable cost function
194+
const twiceDifferentiableFunction_t* twiceDifferentiableCostFunction_;
195+
196+
/// \brief Constraints
197+
std::vector<functionPtr_t> constraintFunctions_;
198+
199+
/// \brief Differentiable Constraints
200+
std::vector<differentiableFunctionPtr_t> differentiableConstraintFunctions_;
201+
202+
/// \brief Twice Differentiable Constraints
203+
std::vector<twiceDifferentiableFunctionPtr_t> twiceDifferentiableConstraintFunctions_;
168204

169205
/// \brief Cost function buffer.
170206
boost::optional<typename function_t::result_t> cost_;
171207

172208
/// \brief Cost gradient buffer.
173-
boost::optional<typename function_t::gradient_t> costGradient_;
209+
boost::optional<typename differentiableFunction_t::gradient_t> costGradient_;
174210

175211
/// \brief Constraints buffer.
176212
boost::optional<typename function_t::result_t> constraints_;
177213

178214
/// \brief Constraints jacobian buffer.
179-
boost::optional<typename function_t::matrix_t> jacobian_;
215+
boost::optional<typename differentiableFunction_t::matrix_t> jacobian_;
180216
};
181217
} // end of namespace detail.
182218
} // end of namespace roboptim.

0 commit comments

Comments
 (0)