File size: 2,193 Bytes
6c50d1f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import random
from .base_agent import BaseAgent
from autoresttest.graph import OperationGraph
class OperationAgent(BaseAgent):
def __init__(
self,
operation_graph: OperationGraph,
alpha: float = 0.1,
gamma: float = 0.9,
epsilon: float = 0.1,
):
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
self.operation_graph = operation_graph
self.q_table: dict[str, float] = {}
def initialize_q_table(self) -> None:
operation_ids = self.operation_graph.operation_nodes.keys()
self.q_table = {operation_id: 0 for operation_id in operation_ids}
def get_action(self) -> str:
if random.random() < self.epsilon:
return self.get_random_action()
return self.get_best_action()
def get_best_action(self) -> str:
if not self.q_table:
raise ValueError(
"No operations were parsed from the specification for OperationAgent."
)
return max(self.q_table.items(), key=lambda x: x[1])[0]
def get_random_action(self) -> str:
if not self.q_table:
raise ValueError(
"No operations were parsed from the specification for OperationAgent."
)
return random.choice(list(self.q_table.keys()))
def update_q_table(self, operation_id: str, reward: float) -> None:
if operation_id not in self.q_table:
return
current_q = self.q_table[operation_id]
best_next_q = self.get_Q_next(operation_id)
new_q = current_q + self.alpha * (reward + self.gamma * best_next_q - current_q)
self.q_table[operation_id] = new_q
def get_Q_next(self, operation_id: str) -> float:
return max(self.q_table.values()) if self.q_table else 0.0
def get_Q_curr(self, operation_id: str) -> float:
return self.q_table.get(operation_id, 0.0)
def update_Q_item(self, operation_id: str, td_error: float) -> None:
if operation_id not in self.q_table:
return
self.q_table[operation_id] = (
self.q_table.get(operation_id, 0.0) + self.alpha * td_error
)
|