11 #ifndef __MEAL_VectorRule_H
12 #define __MEAL_VectorRule_H
14 #include "MEAL/ProjectGradient.h"
15 #include "MEAL/Composite.h"
16 #include "stringtok.h"
22 class VectorRule :
public T
27 typedef typename T::Result Result;
48 void erase (
unsigned index);
57 unsigned size ()
const {
return model.size(); }
63 unsigned get_index ()
const {
return model_index; }
67 {
return "VectorRule<" + std::string(T::Name)+
">"; }
72 void calculate (Result& result, std::vector<Result>* grad);
77 std::vector< Project<T> > model;
115 unsigned nmodel = copy.model.size();
116 for (
unsigned imodel=0; imodel < nmodel; imodel++)
117 push_back (copy.model[imodel]);
119 model_index = copy.model_index;
128 std::cerr << get_name() +
"push_back" << std::endl;
130 model_index = model.size();
132 composite->map (model.back());
139 std::cerr << get_name() +
"assign" << std::endl;
147 if (model[model_index])
150 std::cerr << get_name() +
"assign unmap old" << std::endl;
152 composite->unmap (model[model_index]);
155 model[model_index] = x;
161 std::cerr << get_name() +
"assign map new" << std::endl;
163 composite->map (model[model_index]);
170 std::cerr << get_name() +
"erase" << std::endl;
176 std::cerr << get_name() +
"erase unmap" << std::endl;
178 composite->unmap (model[index]);
181 std::cerr << get_name() +
"erase vector::erase" << std::endl;
183 model.erase( model.begin() + index );
189 if (model_index >= model.size())
190 throw Error (InvalidRange,
"MEAL::"+get_name()+
"::get_current",
191 "index=%d >= nmodel=%d", model_index, model.size());
193 if (!model[model_index])
194 throw Error (InvalidState,
"MEAL::"+get_name()+
"::get_current",
195 "index=%d not set; nmodel=%d", model_index);
197 return model[model_index];
203 if (model_index >= model.size())
204 throw Error (InvalidRange,
"MEAL::"+get_name()+
"::get_projection",
205 "index=%d >= nmodel=%d", model_index, model.size());
207 if (!model[model_index])
208 throw Error (InvalidState,
"MEAL::"+get_name()+
"::get_projection",
209 "index=%d not set; nmodel=%d", model_index);
211 return model[model_index];
217 if (index == model_index)
220 if (index >= model.size())
221 throw Error (InvalidRange,
"MEAL::"+get_name()+
"::set_index",
222 "index=%d >= nmodel=%d", index, model.size());
225 composite->attribute_changed( Function::Evaluation );
230 std::vector<Result>* grad)
232 unsigned nmodel = model.size();
234 std::cerr << get_name() +
"calculate nmodel=" << nmodel << std::endl;
237 throw Error (InvalidState,
"MEAL::"+get_name()+
"::calculate",
"nmodel = 0");
240 std::vector<Result> comp_gradient;
243 std::vector<Result>* comp_gradient_ptr = 0;
246 comp_gradient_ptr = &comp_gradient;
248 if (this->get_verbose())
249 std::cerr << get_name() +
"calculate evaluate "
250 << model[model_index]->get_name() << std::endl;
255 result = model[model_index]->evaluate (comp_gradient_ptr);
257 if (this->get_verbose())
258 std::cerr << get_name() +
"calculate "
259 << model[model_index]->get_name()
260 <<
" result=" << result << std::endl;
263 catch (
Error& error) {
264 error += get_name() +
"calculate";
265 throw error <<
" model=" << model[model_index]->get_name();
270 if (model[model_index]->get_nparam() != comp_gradient.size())
271 throw Error (InvalidState, (get_name() +
"calculate").c_str(),
272 "model[%d]=%s.get_nparam=%d != gradient.size=%d",
273 model_index, model[model_index]->get_name().c_str(),
274 model[model_index]->get_nparam(), comp_gradient.size());
276 unsigned nparam = this->get_nparam();
278 grad->resize (nparam);
279 for (
unsigned iparam=0; iparam<nparam; iparam++)
280 (*grad)[iparam] = 0.0;
283 ProjectGradient (model[model_index], comp_gradient, *grad);
288 std::cerr << get_name() +
"calculate model index=" << model_index
289 <<
" result\n " << result << std::endl;
292 std::cerr << get_name() +
"calculate gradient" << std::endl;
293 for (
unsigned i=0; i<grad->size(); i++)
294 std::cerr <<
" " << i <<
":" << this->get_infit(i)
295 <<
"=" << (*grad)[i] << std::endl;