| 235 | |
| 236 | |
| 237 | class SketchConstraintSolver(object): |
| 238 | |
| 239 | entities: List[DOF] |
| 240 | constraints: List[Constraint] |
| 241 | geoms: List[Geoms] |
| 242 | ne: int |
| 243 | nc: int |
| 244 | ixs: List[int] |
| 245 | |
| 246 | def __init__( |
| 247 | self, |
| 248 | entities: Iterable[DOF], |
| 249 | constraints: Iterable[Constraint], |
| 250 | geoms: Iterable[Geoms], |
| 251 | ): |
| 252 | |
| 253 | self.entities = list(entities) |
| 254 | self.constraints = list(constraints) |
| 255 | self.geoms = list(geoms) |
| 256 | |
| 257 | self.ne = len(self.entities) |
| 258 | self.nc = len(self.constraints) |
| 259 | |
| 260 | # validate and transform constraints |
| 261 | |
| 262 | # indices of x corresponding to the entities |
| 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: |