-
Notifications
You must be signed in to change notification settings - Fork 25
/
objective_master.rs
229 lines (205 loc) · 8.39 KB
/
objective_master.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::groove::objective::*;
use crate::groove::vars::RelaxedIKVars;
pub struct ObjectiveMaster {
pub objectives: Vec<Box<dyn ObjectiveTrait + Send>>,
pub num_chains: usize,
pub weight_priors: Vec<f64>,
pub lite: bool,
pub finite_diff_grad: bool
}
impl ObjectiveMaster {
pub fn standard_ik(num_chains: usize) -> Self {
let mut objectives: Vec<Box<dyn ObjectiveTrait + Send>> = Vec::new();
let mut weight_priors: Vec<f64> = Vec::new();
for i in 0..num_chains {
objectives.push(Box::new(MatchEEPosGoals::new(i)));
weight_priors.push(1.0);
objectives.push(Box::new(MatchEEQuatGoals::new(i)));
weight_priors.push(1.0);
}
Self{objectives, num_chains, weight_priors, lite: true, finite_diff_grad: true}
}
pub fn tune_weight_priors(&mut self, vars: &RelaxedIKVars) {
let a = 0.05;
let cap = 0.001;
for i in 0..self.num_chains {
let mut score_max = 0.0;
for (option, score) in &vars.env_collision.active_obstacles[i] {
if *score > score_max {
score_max = *score;
}
}
// match ee quat goal objectives
let weight_cur = self.weight_priors[3*i+1];
let weight_delta = a / (a + score_max) - weight_cur;
if weight_delta.abs() < cap {
self.weight_priors[3*i+1] += weight_delta;
} else {
self.weight_priors[3*i+1] += cap * weight_delta / weight_delta.abs();
}
}
}
pub fn relaxed_ik(num_chains: usize, objective_mode: String) -> Self {
let mut objectives: Vec<Box<dyn ObjectiveTrait + Send>> = Vec::new();
let mut weight_priors: Vec<f64> = Vec::new();
for i in 0..num_chains {
objectives.push(Box::new(MatchEEPosGoals::new(i)));
weight_priors.push(1.0);
objectives.push(Box::new(MatchEEQuatGoals::new(i)));
if objective_mode == "ECA3" {
weight_priors.push(0.0);
} else if objective_mode == "ECAA" {
weight_priors.push(1.0);
} else {
weight_priors.push(1.0);
}
objectives.push(Box::new(EnvCollision::new(i)));
if objective_mode == "noECA" {
weight_priors.push(0.0);
} else {
weight_priors.push(1.0);
}
}
objectives.push(Box::new(MinimizeVelocity)); weight_priors.push(7.0);
objectives.push(Box::new(MinimizeAcceleration)); weight_priors.push(2.0);
objectives.push(Box::new(MinimizeJerk)); weight_priors.push(1.0);
objectives.push(Box::new(JointLimits)); weight_priors.push(1.0);
objectives.push(Box::new(NNSelfCollision)); weight_priors.push(1.0);
Self{objectives, num_chains, weight_priors, lite: false, finite_diff_grad: true} // fix this
}
pub fn call(&self, x: &[f64], vars: &RelaxedIKVars) -> f64 {
if self.lite {
self.__call_lite(x, vars)
} else {
self.__call(x, vars)
}
}
pub fn gradient(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
if self.lite {
if self.finite_diff_grad {
self.__gradient_finite_diff_lite(x, vars)
} else {
self.__gradient_lite(x, vars)
}
} else {
if self.finite_diff_grad {
self.__gradient_finite_diff(x, vars)
} else {
self.__gradient(x, vars)
}
}
}
pub fn gradient_finite_diff(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
if self.lite {
self.__gradient_finite_diff_lite(x, vars)
} else {
self.__gradient_finite_diff(x, vars)
}
}
fn __call(&self, x: &[f64], vars: &RelaxedIKVars) -> f64 {
let mut out = 0.0;
let frames = vars.robot.get_frames_immutable(x);
for i in 0..self.objectives.len() {
out += self.weight_priors[i] * self.objectives[i].call(x, vars, &frames);
}
out
}
fn __call_lite(&self, x: &[f64], vars: &RelaxedIKVars) -> f64 {
let mut out = 0.0;
let poses = vars.robot.get_ee_pos_and_quat_immutable(x);
for i in 0..self.objectives.len() {
out += self.weight_priors[i] * self.objectives[i].call_lite(x, vars, &poses);
}
out
}
fn __gradient(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
let mut grad: Vec<f64> = vec![0. ; x.len()];
let mut obj = 0.0;
let mut finite_diff_list: Vec<usize> = Vec::new();
let mut f_0s: Vec<f64> = Vec::new();
let frames_0 = vars.robot.get_frames_immutable(x);
for i in 0..self.objectives.len() {
if self.objectives[i].gradient_type() == 1 {
let (local_obj, local_grad) = self.objectives[i].gradient(x, vars, &frames_0);
f_0s.push(local_obj);
obj += self.weight_priors[i] * local_obj;
for j in 0..local_grad.len() {
grad[j] += self.weight_priors[i] * local_grad[j];
}
} else if self.objectives[i].gradient_type() == 0 {
finite_diff_list.push(i);
let local_obj = self.objectives[i].call(x, vars, &frames_0);
obj += self.weight_priors[i] * local_obj;
f_0s.push(local_obj);
}
}
if finite_diff_list.len() > 0 {
for i in 0..x.len() {
let mut x_h = x.to_vec();
x_h[i] += 0.0000001;
let frames_h = vars.robot.get_frames_immutable(x_h.as_slice());
for j in &finite_diff_list {
let f_h = self.objectives[*j].call(x, vars, &frames_h);
grad[i] += self.weight_priors[*j] * ((-f_0s[*j] + f_h) / 0.0000001);
}
}
}
(obj, grad)
}
fn __gradient_lite(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
let mut grad: Vec<f64> = vec![0. ; x.len()];
let mut obj = 0.0;
let mut finite_diff_list: Vec<usize> = Vec::new();
let mut f_0s: Vec<f64> = Vec::new();
let poses_0 = vars.robot.get_ee_pos_and_quat_immutable(x);
for i in 0..self.objectives.len() {
if self.objectives[i].gradient_type() == 1 {
let (local_obj, local_grad) = self.objectives[i].gradient_lite(x, vars, &poses_0);
f_0s.push(local_obj);
obj += self.weight_priors[i] * local_obj;
for j in 0..local_grad.len() {
grad[j] += self.weight_priors[i] * local_grad[j];
}
} else if self.objectives[i].gradient_type() == 0 {
finite_diff_list.push(i);
let local_obj = self.objectives[i].call_lite(x, vars, &poses_0);
obj += self.weight_priors[i] * local_obj;
f_0s.push(local_obj);
}
}
if finite_diff_list.len() > 0 {
for i in 0..x.len() {
let mut x_h = x.to_vec();
x_h[i] += 0.0000001;
let poses_h = vars.robot.get_ee_pos_and_quat_immutable(x_h.as_slice());
for j in &finite_diff_list {
let f_h = self.objectives[*j].call_lite(x, vars, &poses_h);
grad[i] += self.weight_priors[*j] * ((-f_0s[*j] + f_h) / 0.0000001);
}
}
}
(obj, grad)
}
fn __gradient_finite_diff(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
let mut grad: Vec<f64> = vec![0. ; x.len()];
let mut f_0 = self.call(x, vars);
for i in 0..x.len() {
let mut x_h = x.to_vec();
x_h[i] += 0.000001;
let f_h = self.call(x_h.as_slice(), vars);
grad[i] = (-f_0 + f_h) / 0.000001;
}
(f_0, grad)
}
fn __gradient_finite_diff_lite(&self, x: &[f64], vars: &RelaxedIKVars) -> (f64, Vec<f64>) {
let mut grad: Vec<f64> = vec![0. ; x.len()];
let mut f_0 = self.call(x, vars);
for i in 0..x.len() {
let mut x_h = x.to_vec();
x_h[i] += 0.000001;
let f_h = self.__call_lite(x_h.as_slice(), vars);
grad[i] = (-f_0 + f_h) / 0.000001;
}
(f_0, grad)
}
}