@@ -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