Skip to content

Commit 9438a81

Browse files
author
Benjamin Chrétien
committed
Update for new API + use Eigen vectors
1 parent 0920609 commit 9438a81

3 files changed

Lines changed: 67 additions & 48 deletions

File tree

include/roboptim/core/plugin/cminpack.hh

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ namespace roboptim {
3232
/// This solver tries to minimize the euclidean norm of a vector valued
3333
/// function.
3434
class SolverWithJacobian :
35-
public Solver<SumOfC1Squares, boost::mpl::vector<> >
35+
public Solver<EigenMatrixDense>
3636
{
3737
public:
3838
/// \brief Parent type
39-
typedef Solver<SumOfC1Squares, boost::mpl::vector<> > parent_t;
39+
typedef Solver<EigenMatrixDense> parent_t;
4040

4141
/// \brief Cost function type
42-
typedef problem_t::function_t function_t;
42+
typedef SumOfC1Squares function_t;
4343

4444
/// \brief Type of result
4545
typedef function_t::argument_t argument_t;
@@ -88,34 +88,39 @@ namespace roboptim {
8888
/// Get value
8989
const argument_t& value () const
9090
{
91-
(*cost_)(value_, parameter_);
91+
(*baseCost_)(value_, parameter_);
9292
return value_;
9393
}
9494

9595
/// Get Jacobian
9696
const gradient_t& jacobianRow (size_type iRow) const
9797
{
98-
(*cost_).gradient (jacobianRow_, parameter_, iRow);
98+
(*baseCost_).gradient (jacobianRow_, parameter_, iRow);
9999
return jacobianRow_;
100100
}
101101

102+
const boost::shared_ptr<const DifferentiableFunction> baseCost () const
103+
{
104+
return baseCost_;
105+
}
106+
102107
private:
103108
/// Number of variables
104109
size_type n_;
105110
/// Dimension of the cost function
106111
size_type m_;
107112
/// Array of double to store variable of optimization problem
108-
double* x_;
113+
vector_t x_;
109114
/// Array of double to store value of optimization problem
110-
double* fvec_;
115+
vector_t fvec_;
111116
/// Array of double to store one line of Jacobian
112-
double* fjac_;
117+
vector_t fjac_;
113118
/// Array of int used by the optimizer
114-
int* ipvt_;
119+
Eigen::VectorXi ipvt_;
115120
/// Positive integer not less than 5*n_+m_
116121
int lwa_;
117122
/// array of double of size lwa_;
118-
double* wa_;
123+
vector_t wa_;
119124

120125
/// Parameter of the function
121126
argument_t parameter_;
@@ -124,7 +129,7 @@ namespace roboptim {
124129
/// Jacobian of the cost function
125130
mutable gradient_t jacobianRow_;
126131
/// Reference to cost function
127-
boost::shared_ptr <const DifferentiableFunction> cost_;
132+
boost::shared_ptr <const DifferentiableFunction> baseCost_;
128133
}; // class Solver
129134
} // namespace cminpack
130135
} // namespace roboptim

src/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# roboptim-core-plugin-cminpack If not, see
1818
# <http://www.gnu.org/licenses/>.
1919

20-
# Define the directory where plug-ins will be installed.
2120
SET(PLUGIN_NAME ${PROJECT_NAME})
2221

2322
# Define the directory where plug-ins will be installed.

src/cminpack.cc

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -56,61 +56,76 @@ namespace roboptim
5656
{
5757
namespace cminpack
5858
{
59-
SolverWithJacobian::SolverWithJacobian (const problem_t& problem) :
60-
Solver <SumOfC1Squares, boost::mpl::vector<> >
61-
(problem),
62-
n_ (problem.function ().baseFunction ()->inputSize ()),
63-
m_ (problem.function ().baseFunction ()->outputSize ()),
64-
x_ (new double [n_]),
65-
fvec_ (new double [m_]),
66-
fjac_ (new double [n_*n_]),
67-
ipvt_ (new int [n_]),
68-
lwa_ (static_cast<int> (5 * n_ + m_)),
69-
wa_ (new double [lwa_]),
70-
parameter_ (n_),
71-
value_ (m_),
72-
jacobianRow_ (n_),
73-
cost_ (problem.function ().baseFunction ())
59+
SolverWithJacobian::SolverWithJacobian (const problem_t& pb) :
60+
parent_t (pb),
61+
n_ (),
62+
m_ (),
63+
x_ (),
64+
fvec_ (),
65+
fjac_ (),
66+
ipvt_ (),
67+
lwa_ (),
68+
wa_ (),
69+
parameter_ (),
70+
value_ (),
71+
jacobianRow_ (),
72+
baseCost_ ()
7473
{
75-
std::size_t n = static_cast<std::size_t> (n_);
76-
std::size_t m = static_cast<std::size_t> (m_);
77-
std::size_t lwa = static_cast<std::size_t> (lwa_);
74+
const SumOfC1Squares* cost
75+
= dynamic_cast<const SumOfC1Squares*> (&pb.function ());
76+
77+
if (!cost)
78+
{
79+
throw std::runtime_error ("the cminpack plugin expects"
80+
" a SumOfC1Squares cost function");
81+
}
82+
83+
baseCost_ = cost->baseFunction ();
84+
85+
n_ = baseCost_->inputSize ();
86+
m_ = baseCost_->outputSize ();
87+
lwa_ = static_cast<int> (5 * n_ + m_);
7888

7989
// Initialize memory
80-
memset (x_, 0, n * sizeof (double));
81-
memset (fvec_, 0, m * sizeof (double));
82-
memset (fjac_, 0, n * n * sizeof (double));
83-
memset (ipvt_, 0, n * sizeof (int));
84-
memset (wa_, 0, lwa * sizeof (double));
90+
x_.resize (n_);
91+
x_.setZero ();
92+
fvec_.resize (m_);
93+
fvec_.setZero ();
94+
fjac_.resize (n_*n_);
95+
fjac_.setZero ();
96+
ipvt_.resize (n_);
97+
ipvt_.setZero ();
98+
wa_.resize (lwa_);
99+
wa_.setZero ();
85100

86101
// Initialize this class parameters
102+
parameter_.resize (n_);
87103
parameter_.setZero ();
104+
value_.resize (m_);
88105
value_.setZero ();
106+
jacobianRow_.resize (n_);
89107
jacobianRow_.setZero ();
90108
}
91109

92110
SolverWithJacobian::~SolverWithJacobian ()
93111
{
94-
delete[] x_;
95-
delete[] fvec_;
96-
delete[] fjac_;
97-
delete[] ipvt_;
98-
delete[] wa_;
99112
}
100113

101114
void SolverWithJacobian::solve ()
102115
{
103116
int ldfjac = static_cast<int> (n_);
104117
double tol = 1e-6;
118+
105119
// Set initial guess
106120
if (problem().startingPoint()) {
107-
vector_to_array (x_, *(problem().startingPoint()));
121+
x_ = *(problem().startingPoint());
108122
}
123+
109124
int info = lmstr1(roboptim_plugin_cminpack_fcn,
110125
(void*)this,
111126
static_cast<int> (m_), static_cast<int> (n_),
112-
x_, fvec_, fjac_, ldfjac,
113-
tol, ipvt_, wa_, lwa_);
127+
x_.data(), fvec_.data(), fjac_.data(), ldfjac,
128+
tol, ipvt_.data(), wa_.data(), lwa_);
114129
switch (info) {
115130
case 0:
116131
result_ = SolverError ("improper input parameters");
@@ -120,15 +135,15 @@ namespace roboptim
120135
case 3:
121136
{
122137
Result result (n_, 1);
123-
array_to_vector(result.x, x_);
138+
result.x = x_;
124139
result.value = problem().function()(result.x);
125140
result_ = result;
126141
}
127142
break;
128143
case 4:
129144
{
130145
ResultWithWarnings result (n_, 1);
131-
array_to_vector(result.x, x_);
146+
result.x = x_;
132147
result.value = problem().function()(result.x);
133148
result.warnings.push_back(SolverWarning
134149
("fvec is orthogonal to the columns of"
@@ -139,7 +154,7 @@ namespace roboptim
139154
case 5:
140155
{
141156
ResultWithWarnings result (n_, 1);
142-
array_to_vector(result.x, x_);
157+
result.x = x_;
143158
result.value = problem().function()(result.x);
144159
result.warnings.push_back(SolverWarning
145160
("number of calls to fcn with iflag = 1 "
@@ -150,7 +165,7 @@ namespace roboptim
150165
case 6:
151166
{
152167
ResultWithWarnings result (n_, 1);
153-
array_to_vector(result.x, x_);
168+
result.x = x_;
154169
result.value = problem().function()(result.x);
155170
result.warnings.push_back(SolverWarning
156171
("tol is too small. no further reduction"
@@ -161,7 +176,7 @@ namespace roboptim
161176
case 7:
162177
{
163178
ResultWithWarnings result (n_, 1);
164-
array_to_vector(result.x, x_);
179+
result.x = x_;
165180
result.value = problem().function()(result.x);
166181
result.warnings.push_back(SolverWarning
167182
("tol is too small. no further"

0 commit comments

Comments
 (0)