File size: 9,309 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | import random
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple
import numpy as np
from .base_agent import BaseAgent
from autoresttest.graph import OperationGraph
from autoresttest.models import ParameterKey
from autoresttest.utils import get_param_combinations, get_required_params
@dataclass
class ParameterAction:
req_params: Optional[Tuple[ParameterKey, ...]]
mime_type: Optional[str]
class ParameterAgent(BaseAgent):
def __init__(
self,
operation_graph: OperationGraph,
alpha: float = 0.1,
gamma: float = 0.9,
epsilon: float = 0.1,
):
# Q-table maps the operation IDs to a dictionary containing a location key (e.g., "params") mapped to the parameter combinations
self.q_table: dict[
str, dict[str, dict[tuple[ParameterKey, ...] | str, float]]
] = {}
self.operation_graph = operation_graph
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
def initialize_q_table(self) -> None:
for (
operation_id,
operation_node,
) in self.operation_graph.operation_nodes.items():
if operation_id not in self.q_table:
self.q_table[operation_id] = {"params": {}, "body": {}}
required = get_required_params(
operation_node.operation_properties.parameters
)
params = get_param_combinations(
operation_node.operation_properties.parameters,
required_params=required,
seed=operation_id,
)
self.q_table[operation_id]["params"] = {param: 0.0 for param in params}
mimes = (
list(operation_node.operation_properties.request_body.keys())
if operation_node.operation_properties.request_body
else []
)
self.q_table[operation_id]["body"] = {mime: 0 for mime in mimes}
self.q_table[operation_id]["params"]["None"] = 0
self.q_table[operation_id]["body"]["None"] = 0
def get_action(self, operation_id: str) -> ParameterAction:
if operation_id not in self.q_table:
raise ValueError(
f"Operation '{operation_id}' not found in the Q-table for ParameterAgent."
)
if operation_id not in self.operation_graph.operation_nodes:
raise ValueError(
f"Operation '{operation_id}' not found in the operation graph."
)
if random.random() < self.epsilon:
return self.get_random_action(operation_id)
return self.get_best_action(operation_id)
def get_random_action(self, operation_id: str) -> ParameterAction:
random_params: tuple[ParameterKey, ...] | None = None
if self.q_table[operation_id]["params"]:
key = random.choice(list(self.q_table[operation_id]["params"].keys()))
if key != "None" and isinstance(key, tuple):
random_params = key
random_mime: str | None = None
if self.q_table[operation_id]["body"]:
key = random.choice(list(self.q_table[operation_id]["body"].keys()))
if key != "None" and isinstance(key, str):
random_mime = key
return ParameterAction(req_params=random_params, mime_type=random_mime)
def get_best_action(self, operation_id: str) -> ParameterAction:
required_params: set[ParameterKey] = get_required_params(
self.operation_graph.operation_nodes[
operation_id
].operation_properties.parameters
)
best_params: tuple[ParameterKey, ...] | None = None
best_params_score: float = -np.inf
if self.q_table[operation_id]["params"] and required_params:
for params, score in self.q_table[operation_id]["params"].items():
if (
params != "None"
and isinstance(params, tuple)
and required_params.issubset(set(params))
and score > best_params_score
):
best_params = params
best_params_score = score
elif self.q_table[operation_id]["params"]:
best_key = max(
self.q_table[operation_id]["params"].items(), key=lambda x: x[1]
)[0]
if best_key != "None" and isinstance(best_key, tuple):
best_params = best_key
best_body: str | None = None
best_body_score: float = -np.inf
if self.q_table[operation_id]["body"]:
for mime, score in self.q_table[operation_id]["body"].items():
if mime != "None" and isinstance(mime, str) and score > best_body_score:
best_body = mime
best_body_score = score
return ParameterAction(req_params=best_params, mime_type=best_body)
def get_Q_next(self, operation_id: str) -> Tuple[float, float]:
if operation_id not in self.q_table:
return 0.0, 0.0
best_next_q_params = (
max(self.q_table[operation_id]["params"].values())
if self.q_table[operation_id]["params"]
else 0.0
)
best_next_q_body = (
max(self.q_table[operation_id]["body"].values())
if self.q_table[operation_id]["body"]
else 0.0
)
return best_next_q_params, best_next_q_body
def get_Q_curr(
self, operation_id: str, action: ParameterAction
) -> Tuple[float, float]:
if operation_id not in self.q_table:
return 0.0, 0.0
req_params_key = action.req_params if action.req_params is not None else "None"
mime_type_key = action.mime_type if action.mime_type is not None else "None"
current_q_params = (
self.q_table[operation_id]["params"].get(req_params_key, 0.0)
if self.q_table[operation_id]["params"]
else 0.0
)
current_q_body = (
self.q_table[operation_id]["body"].get(mime_type_key, 0.0)
if self.q_table[operation_id]["body"]
else 0.0
)
return current_q_params, current_q_body
def update_Q_item(
self, operation_id: str, action: ParameterAction, td_error: float
) -> None:
if operation_id not in self.q_table:
return
req_params_key = action.req_params if action.req_params is not None else "None"
mime_type_key = action.mime_type if action.mime_type is not None else "None"
if (
self.q_table[operation_id]["params"]
and req_params_key in self.q_table[operation_id]["params"]
):
self.q_table[operation_id]["params"][req_params_key] += (
self.alpha * td_error
)
if (
self.q_table[operation_id]["body"]
and mime_type_key in self.q_table[operation_id]["body"]
):
self.q_table[operation_id]["body"][mime_type_key] += self.alpha * td_error
def update_q_table(
self, operation_id: str, action: ParameterAction, reward: float
) -> None:
if operation_id not in self.q_table:
return
req_params_key = action.req_params if action.req_params is not None else "None"
mime_type_key = action.mime_type if action.mime_type is not None else "None"
current_q_params = (
self.q_table[operation_id]["params"].get(req_params_key, 0.0)
if self.q_table[operation_id]["params"]
else 0.0
)
current_q_body = (
self.q_table[operation_id]["body"].get(mime_type_key, 0.0)
if self.q_table[operation_id]["body"]
else 0.0
)
best_next_q_params = (
max(self.q_table[operation_id]["params"].values())
if self.q_table[operation_id]["params"]
else 0.0
)
best_next_q_body = (
max(self.q_table[operation_id]["body"].values())
if self.q_table[operation_id]["body"]
else 0.0
)
new_q_params = current_q_params + self.alpha * (
reward + self.gamma * best_next_q_params - current_q_params
)
new_q_body = current_q_body + self.alpha * (
reward + self.gamma * best_next_q_body - current_q_body
)
if (
self.q_table[operation_id]["params"]
and req_params_key in self.q_table[operation_id]["params"]
):
self.q_table[operation_id]["params"][req_params_key] = new_q_params
if (
self.q_table[operation_id]["body"]
and mime_type_key in self.q_table[operation_id]["body"]
):
self.q_table[operation_id]["body"][mime_type_key] = new_q_body
def number_of_zeros(self, operation_id: str) -> int:
if operation_id not in self.q_table:
return 0
zeros = 0
for value in self.q_table[operation_id]["params"].values():
if value == 0:
zeros += 1
for value in self.q_table[operation_id]["body"].values():
if value == 0:
zeros += 1
return zeros
|