| 263 | self.ixs = [0] + list(accumulate(len(e) for e in self.entities)) |
| 264 | |
| 265 | def _cost( |
| 266 | self, x0: Array[Float] |
| 267 | ) -> Tuple[ |
| 268 | Callable[[Array[Float]], float], |
| 269 | Callable[[Array[Float], Array[Float]], None], |
| 270 | Array[Float], |
| 271 | Array[Float], |
| 272 | ]: |
| 273 | |
| 274 | ixs = self.ixs |
| 275 | constraints = self.constraints |
| 276 | geoms = self.geoms |
| 277 | |
| 278 | # split initial values per entity |
| 279 | x0s = [x0[ixs[e] : ixs[e + 1]] for e in range(self.ne)] |
| 280 | |
| 281 | def f(x) -> float: |
| 282 | """ |
| 283 | Cost function to be minimized |
| 284 | """ |
| 285 | |
| 286 | rv = 0.0 |
| 287 | |
| 288 | for i, ((e1, e2), kind, val) in enumerate(constraints): |
| 289 | |
| 290 | cost = costs[kind] |
| 291 | |
| 292 | # build arguments for the specific constraint |
| 293 | args = [x[ixs[e1] : ixs[e1 + 1]], geoms[e1], x0s[e1]] |
| 294 | if e2 is not None: |
| 295 | args += [x[ixs[e2] : ixs[e2 + 1]], geoms[e2], x0s[e2]] |
| 296 | |
| 297 | # evaluate |
| 298 | rv += cost(*args, val) ** 2 |
| 299 | |
| 300 | return rv |
| 301 | |
| 302 | def grad(x, rv) -> None: |
| 303 | """ |
| 304 | Gradient of the cost function |
| 305 | """ |
| 306 | |
| 307 | rv[:] = 0 |
| 308 | |
| 309 | for i, ((e1, e2), kind, val) in enumerate(constraints): |
| 310 | |
| 311 | cost = costs[kind] |
| 312 | |
| 313 | # build arguments for the specific constraint |
| 314 | x1 = x[ixs[e1] : ixs[e1 + 1]] |
| 315 | args = [x1.copy(), geoms[e1], x0s[e1]] |
| 316 | if e2 is not None: |
| 317 | x2 = x[ixs[e2] : ixs[e2 + 1]] |
| 318 | args += [x2.copy(), geoms[e2], x0s[e2]] |
| 319 | |
| 320 | # evaluate |
| 321 | tmp = cost(*args, val) |
| 322 | |