pyduino.optimization
1from typing import Callable, List, Tuple 2from abc import ABC, abstractmethod 3import warnings 4import numpy as np 5 6def linmap(domain: List[Tuple[float, float]], codomain: List[Tuple[float, float]]) -> Callable[[np.ndarray], np.ndarray]: 7 """ 8 Linear mapping from domain to codomain. 9 domain list of pairs: 10 Maxima and minima that each parameter can cover. 11 Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10. 12 codomain list of pairs: 13 Maxima and minima that each parameter can cover. 14 Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10. 15 """ 16 domain = np.array(domain) 17 codomain = np.array(codomain) 18 19 M = (codomain[:, 1] - codomain[:, 0])/ (domain[:, 1] - domain[:, 0]) 20 21 def f(x): 22 assert x.shape[1] == domain.shape[0], f"x must have the same number of features as the domain. Expected {domain.shape[0]}, got {x.shape[1]}." 23 return codomain[:, 0] + M * (x - domain[:, 0]) 24 25 return f 26 27class Optimizer(ABC): 28 """ 29 Abstract class for optimization algorithms 30 """ 31 @abstractmethod 32 def __init__(self, growth_rate: float, population_size, ranges, iterations=None, rng_seed=0): 33 pass 34 35 @abstractmethod 36 def view(self, x, linmap): 37 pass 38 39 @abstractmethod 40 def view_g(self): 41 pass 42 43 @abstractmethod 44 def inverse_view(self, x, linmap=None): 45 pass 46 47 @abstractmethod 48 def ask_oracle(self) -> np.ndarray: 49 raise NotImplementedError("The oracle must be implemented.") 50 51 @abstractmethod 52 def init_oracle(self): 53 warnings.warn("The oracle initialization is not implemented.") 54 pass 55 56 @abstractmethod 57 def step(self): 58 pass
def
linmap( domain: List[Tuple[float, float]], codomain: List[Tuple[float, float]]) -> Callable[[numpy.ndarray], numpy.ndarray]:
7def linmap(domain: List[Tuple[float, float]], codomain: List[Tuple[float, float]]) -> Callable[[np.ndarray], np.ndarray]: 8 """ 9 Linear mapping from domain to codomain. 10 domain list of pairs: 11 Maxima and minima that each parameter can cover. 12 Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10. 13 codomain list of pairs: 14 Maxima and minima that each parameter can cover. 15 Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10. 16 """ 17 domain = np.array(domain) 18 codomain = np.array(codomain) 19 20 M = (codomain[:, 1] - codomain[:, 0])/ (domain[:, 1] - domain[:, 0]) 21 22 def f(x): 23 assert x.shape[1] == domain.shape[0], f"x must have the same number of features as the domain. Expected {domain.shape[0]}, got {x.shape[1]}." 24 return codomain[:, 0] + M * (x - domain[:, 0]) 25 26 return f
Linear mapping from domain to codomain. domain list of pairs: Maxima and minima that each parameter can cover. Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10. codomain list of pairs: Maxima and minima that each parameter can cover. Example: [(0, 10), (0, 10)] for two parameters ranging from 0 to 10.
class
Optimizer(abc.ABC):
28class Optimizer(ABC): 29 """ 30 Abstract class for optimization algorithms 31 """ 32 @abstractmethod 33 def __init__(self, growth_rate: float, population_size, ranges, iterations=None, rng_seed=0): 34 pass 35 36 @abstractmethod 37 def view(self, x, linmap): 38 pass 39 40 @abstractmethod 41 def view_g(self): 42 pass 43 44 @abstractmethod 45 def inverse_view(self, x, linmap=None): 46 pass 47 48 @abstractmethod 49 def ask_oracle(self) -> np.ndarray: 50 raise NotImplementedError("The oracle must be implemented.") 51 52 @abstractmethod 53 def init_oracle(self): 54 warnings.warn("The oracle initialization is not implemented.") 55 pass 56 57 @abstractmethod 58 def step(self): 59 pass
Abstract class for optimization algorithms