mirror of
https://github.com/blackboxprogramming/lucidia.git
synced 2026-03-17 04:57:15 -05:00
Synced from BlackRoad-OS-Inc/blackroad-operator/orgs/personal/lucidia BlackRoad OS — Pave Tomorrow. RoadChain-SHA2048: fe729062952871e7 RoadChain-Identity: alexa@sovereign RoadChain-Full: fe729062952871e77147cf6d938b799096e87d9024d7005a14c9e209e12e8ad0c825b624c7bc649fc7eeb4c284fdcab8231af77980065cc04d9f36fca479ffc2346ed3c1b73de6f240d8f9485f47c995ad5b81142f7179b84932c67914dff1c08db039349ba28fca36cb57688093bf0199268dd1c2f3448c9383000bc77cc9663066ff57b834370afc8838b18466ea9029908018b961555cccaabf2ce21649cf3cabc7f64bdcc4abdf2da259b210c342835a2cecf92bdd3b4e109b4d6e622f6934e13b2b123607bd61ce3d0f20454c9ab594f9284cffe18716619c52db57ce5f4ee2856cb96e1fa3748fe1fe65435bec297c5ab3ab58d570ec1064aea29931dd
82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Callable, Any, List
|
|
|
|
@dataclass
|
|
class AdaptationRule:
|
|
"""Represents a single adaptation rule for human-machine interaction.
|
|
|
|
Attributes
|
|
----------
|
|
condition : Callable[[Any], bool]
|
|
A predicate that determines whether the rule should fire for a given state.
|
|
action : Callable[[Any], Any]
|
|
A transformation to apply when the condition is met.
|
|
description : str
|
|
Human-friendly summary of the rule's purpose.
|
|
"""
|
|
condition: Callable[[Any], bool]
|
|
action: Callable[[Any], Any]
|
|
description: str = ""
|
|
|
|
|
|
class AdaptiveSystem:
|
|
"""
|
|
Framework for applying adaptation rules based on conditions.
|
|
|
|
This simple system iterates through registered rules and applies the
|
|
action for the first rule whose condition is true. If no rule
|
|
matches, it returns the state unchanged.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self.rules: List[AdaptationRule] = []
|
|
|
|
def add_rule(self, rule: AdaptationRule) -> None:
|
|
"""Register a new adaptation rule."""
|
|
self.rules.append(rule)
|
|
|
|
def adapt(self, state: Any) -> Any:
|
|
"""
|
|
Apply the first matching adaptation rule to the given state.
|
|
|
|
Parameters
|
|
----------
|
|
state : Any
|
|
The current state or input value to adapt.
|
|
|
|
Returns
|
|
-------
|
|
Any
|
|
The adapted state if a rule matched, otherwise the original state.
|
|
"""
|
|
for rule in self.rules:
|
|
if rule.condition(state):
|
|
return rule.action(state)
|
|
return state
|
|
|
|
|
|
if __name__ == "__main__":
|
|
# Example: adapt temperature values to a comfortable range
|
|
adapt_sys = AdaptiveSystem()
|
|
|
|
def too_cold(x: float) -> bool:
|
|
return x < 20
|
|
|
|
def warm_action(x: float) -> float:
|
|
return x + 5
|
|
|
|
def too_hot(x: float) -> bool:
|
|
return x > 25
|
|
|
|
def cool_action(x: float) -> float:
|
|
return x - 5
|
|
|
|
adapt_sys.add_rule(AdaptationRule(too_cold, warm_action, "Warm up if too cold"))
|
|
adapt_sys.add_rule(AdaptationRule(too_hot, cool_action, "Cool down if too hot"))
|
|
|
|
temps = [18.0, 22.0, 28.0]
|
|
for t in temps:
|
|
print(f"{t} -> {adapt_sys.adapt(t)}")
|