1
0

feat: finish lut resolver

This commit is contained in:
2026-06-15 15:58:04 +08:00
parent 7721672b8e
commit ed8f5e1943
6 changed files with 312 additions and 238 deletions

View File

@@ -19,7 +19,7 @@ class ResultPriority(enum.Enum):
@dataclass
class ResolverRequest:
"""
The request object for the resolver.
All request infomation for the resolver.
"""
device_kind: DeviceKind
@@ -27,69 +27,11 @@ class ResolverRequest:
target_value: float
"""The target value of the device."""
tolerance: float
"""The tolerance of the device."""
"""The tolerance of the device in absolute value."""
result_priority: ResultPriority
"""The priority of the result."""
count_limit: int
"""The limit of the count of results."""
class ResolverResult:
"""
The result of the resolver.
"""
circuit: Circuit
"""The circuit of the result."""
__value_cache: float | None
"""The cache of the circuit value."""
__difference_cache: float | None
"""The cache of the difference between the target value and the circuit value."""
__relative_difference_cache: float | None
"""The cache of the relative difference between the target value and the circuit value."""
def __init__(self, circuit: Circuit):
self.circuit = circuit
self.__value_cache = None
self.__difference_cache = None
self.__relative_difference_cache = None
def compute(self, device_kind: DeviceKind) -> float:
"""
Compute the circuit value.
"""
if self.__value_cache is None:
self.__value_cache = self.circuit.compute(device_kind)
return self.__value_cache
def difference(self, target_value: float, device_kind: DeviceKind) -> float:
"""
Get the difference between the target value and the circuit value.
"""
if self.__difference_cache is None:
self.__difference_cache = abs(
target_value - self.circuit.compute(device_kind)
)
return self.__difference_cache
def relative_difference(
self, target_value: float, device_kind: DeviceKind
) -> float:
"""
Get the relative difference between the target value and the circuit value.
"""
if self.__relative_difference_cache is None:
self.__relative_difference_cache = (
abs(target_value - self.circuit.compute(device_kind)) / target_value
)
return self.__relative_difference_cache
def len_devices(self) -> int:
"""
Get the number of devices in the circuit.
"""
return self.circuit.len_devices()
"""The limited count of results."""
class Resolver(ABC):
@@ -98,5 +40,5 @@ class Resolver(ABC):
"""
@abstractmethod
def resolve(self, request: ResolverRequest) -> Iterator[ResolverResult]:
def resolve(self, request: ResolverRequest) -> Iterator[Circuit]:
pass