[docs]classConstraint(OptFunction):""" Abstract base class for optimization constraints. Attributes ---------- tol: float The tolerance for constraint violations :group: core """
[docs]def__init__(self,*args,tol=1e-5,**kwargs):""" Constructor Parameters ---------- tol: float The tolerance for constraint violations """super().__init__(*args,**kwargs)self.tol=tol
[docs]defget_bounds(self):""" Returns the bounds for all components. Non-existing bounds are expressed by np.inf. Returns ------- min: np.array The lower bounds, shape: (n_components,) max: np.array The upper bounds, shape: (n_components,) """return(np.full(self.n_components(),-np.inf,dtype=np.float64),np.zeros(self.n_components(),dtype=np.float64),)
[docs]defcheck_individual(self,constraint_values,verbosity=0):""" Check if the constraints are fullfilled for the given individual. Parameters ---------- constraint_values: np.array The constraint values, shape: (n_components,) verbosity: int The verbosity level, 0 = silent Returns values: np.array ------- The boolean result, shape: (n_components,) """vals=constraint_valuesmi,ma=self.get_bounds()out=(vals+self.tol>=mi)&(vals-self.tol<=ma)ifverbosity:print(f"Constraint '{self.name}': tol = {self.tol}")cnames=self.component_namesforciinrange(self.n_components()):val=f"{cnames[ci]} = {vals[ci]:.3e}"suc="OK"ifout[ci]else"FAILED"print(f" Constraint {val:<30}{suc}")returnout
[docs]defcheck_population(self,constraint_values,verbosity=0):""" Check if the constraints are fullfilled for the given population. Parameters ---------- constraint_values: np.array The constraint values, shape: (n_pop, n_components,) verbosity: int The verbosity level, 0 = silent Returns ------- values: np.array The boolean result, shape: (n_pop, n_components) """vals=constraint_valuesmi,ma=self.get_bounds()mi=np.array(mi,dtype=np.float64)ma=np.array(ma,dtype=np.float64)out=(vals+self.tol>=mi[None,:])&(vals-self.tol<=ma[None,:])ifverbosity:print(f"Constraint '{self.name}': tol = {self.tol}")cnames=self.component_namesforciinrange(self.n_components()):suc="OK"ifnp.all(out[ci])else"FAILED"print(f" Constraint {cnames[ci]:<20}{suc}")returnout
[docs]@classmethoddefnew(cls,constraint_type,*args,**kwargs):""" Run-time constraint factory. Parameters ---------- constraint_type: str The selected derived class name args: tuple, optional Additional parameters for constructor kwargs: dict, optional Additional parameters for constructor """returnnew_instance(cls,constraint_type,*args,**kwargs)