44 using BASE::modelInfo;
47 using BASE::computeFitness;
50 std::vector<Frame> tipObjectives;
52 Eigen::VectorXd tip_diffs;
53 Eigen::VectorXd joint_diffs;
54 Eigen::MatrixXd jacobian;
55 std::vector<Frame> tip_frames_temp;
62 void initialize(
const Problem& problem)
64 BASE::initialize(problem);
65 tipObjectives.resize(problem.tip_link_indices.size());
66 for(
auto& goal : problem.goals)
67 tipObjectives[goal.tip_index] = goal.frame;
70 void optimizeJacobian(std::vector<double>& solution)
74 int tip_count = problem.tip_link_indices.size();
75 tip_diffs.resize(tip_count * 6);
76 joint_diffs.resize(problem.active_variables.size());
79 model.applyConfiguration(solution);
81 double translational_scale = 1;
82 double rotational_scale = 1;
85 tip_frames_temp = model.getTipFrames();
86 for(
int itip = 0; itip < tip_count; itip++)
88 auto twist = frameTwist(tip_frames_temp[itip], tipObjectives[itip]);
89 tip_diffs(itip * 6 + 0) = twist.vel.x() * translational_scale;
90 tip_diffs(itip * 6 + 1) = twist.vel.y() * translational_scale;
91 tip_diffs(itip * 6 + 2) = twist.vel.z() * translational_scale;
92 tip_diffs(itip * 6 + 3) = twist.rot.x() * rotational_scale;
93 tip_diffs(itip * 6 + 4) = twist.rot.y() * rotational_scale;
94 tip_diffs(itip * 6 + 5) = twist.rot.z() * rotational_scale;
99 model.computeJacobian(problem.active_variables, jacobian);
101 for(
auto ivar : problem.active_variables)
103 for(
size_t itip = 0; itip < tip_count; itip++)
105 jacobian(itip * 6 + 0, icol) *= translational_scale;
106 jacobian(itip * 6 + 1, icol) *= translational_scale;
107 jacobian(itip * 6 + 2, icol) *= translational_scale;
108 jacobian(itip * 6 + 3, icol) *= rotational_scale;
109 jacobian(itip * 6 + 4, icol) *= rotational_scale;
110 jacobian(itip * 6 + 5, icol) *= rotational_scale;
117 joint_diffs = jacobian.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(tip_diffs);
123 for(
auto ivar : problem.active_variables)
125 auto v = solution[ivar] + joint_diffs(icol);
126 if(!std::isfinite(v))
continue;
127 v = modelInfo.clip(v, ivar);
138 std::vector<double> solution, best_solution, gradient, temp;
146 void initialize(
const Problem& problem)
148 IKBase::initialize(problem);
149 solution = problem.initial_guess;
151 for(
auto& vi : problem.active_variables)
152 solution[vi] = random(modelInfo.getMin(vi), modelInfo.getMax(vi));
153 best_solution = solution;
157 const std::vector<double>& getSolution()
const {
return best_solution; }
165 for(
auto& vi : problem.active_variables)
166 solution[vi] = random(modelInfo.getMin(vi), modelInfo.getMax(vi));
172 gradient.resize(solution.size(), 0);
173 for(
auto ivar : problem.active_variables)
175 temp[ivar] = solution[ivar] - jd;
176 double p1 = computeFitness(temp);
178 temp[ivar] = solution[ivar] + jd;
179 double p3 = computeFitness(temp);
181 temp[ivar] = solution[ivar];
183 gradient[ivar] = p3 - p1;
188 for(
auto ivar : problem.active_variables)
189 sum += fabs(gradient[ivar]);
190 double f = 1.0 / sum * jd;
191 for(
auto ivar : problem.active_variables)
197 for(
auto ivar : problem.active_variables)
198 temp[ivar] = solution[ivar] - gradient[ivar];
199 double p1 = computeFitness(temp);
204 for(
auto ivar : problem.active_variables)
205 temp[ivar] = solution[ivar] + gradient[ivar];
206 double p3 = computeFitness(temp);
208 double p2 = (p1 + p3) * 0.5;
211 double cost_diff = (p3 - p1) * 0.5;
212 double joint_diff = p2 / cost_diff;
216 for(
auto ivar : problem.active_variables)
217 temp[ivar] = modelInfo.clip(solution[ivar] - gradient[ivar] * joint_diff, ivar);
227 if(computeFitness(temp) < computeFitness(solution))
243 if(computeFitness(solution) < computeFitness(best_solution)) best_solution = solution;
246 size_t concurrency()
const {
return threads; }
267 using IKBase::initialize;
268 std::vector<double> solution;
273 void initialize(
const Problem& problem)
276 solution = problem.initial_guess;
278 for(
auto& vi : problem.active_variables)
279 solution[vi] = random(modelInfo.getMin(vi), modelInfo.getMax(vi));
281 const std::vector<double>& getSolution()
const {
return solution; }
282 void step() { optimizeJacobian(solution); }
283 size_t concurrency()
const {
return threads; }