torchmodal.systems¶
torchmodal.systems ¶
torchmodal.systems ~~~~~~~~~~~~~~~~~~
Higher-level modal logic systems built on top of the core operators.
Provides ready-to-use modules for specific modal logics:
- Epistemic Logic (K_a): Agent a knows ϕ iff ϕ is true in all worlds accessible to a.
- Doxastic Logic (B_a): Agent a believes ϕ, where beliefs may differ from reality.
- Temporal Logic (G, F): Globally ϕ (necessity over future states) and Finally ϕ (possibility of eventual truth).
- Composite Operators (K∘G, K∘F): Nested modal operators for complex multi-agent temporal reasoning.
EpistemicOperator ¶
Bases: Module
Epistemic knowledge operator K_a.
K_a(ϕ) asserts "agent a knows ϕ" — ϕ is true in all worlds
accessible to agent a.
This is equivalent to □ restricted to agent a's accessibility row in the relation matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tau
|
float
|
Temperature. Default 0.1. |
0.1
|
top_k
|
Optional[int]
|
Top-k aggregation for the underlying □ (see
:class: |
None
|
Example::
>>> K = EpistemicOperator()
>>> # agent_accessibility: (|W|,) row for agent a
>>> knowledge = K(prop_bounds, agent_accessibility)
Source code in torchmodal/systems.py
forward ¶
Evaluate K_a(ϕ) for a single agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
agent_accessibility
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Scalar or |
Source code in torchmodal/systems.py
DoxasticOperator ¶
Bases: Module
Doxastic belief operator B_a.
B_a(ϕ) asserts "agent a believes ϕ" — ϕ is true in all
worlds compatible with a's beliefs, which may differ from reality.
Structurally identical to :class:EpistemicOperator, but
semantically distinct: epistemic accessibility requires veridical
knowledge (ϕ must actually hold), while doxastic accessibility
permits false beliefs.
This distinction is captured by the accessibility relation: epistemic relations are typically reflexive (T axiom: K_a(ϕ) → ϕ), while doxastic relations may not be.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tau
|
float
|
Temperature. Default 0.1. |
0.1
|
top_k
|
Optional[int]
|
Top-k aggregation for the underlying □ (see
:class: |
None
|
Source code in torchmodal/systems.py
forward ¶
Evaluate B_a(ϕ) for a single agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
agent_accessibility
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Bounds for B_a(ϕ). |
Source code in torchmodal/systems.py
TemporalOperator ¶
Bases: Module
Temporal logic operators G (Globally), F (Finally), and U (Until).
- G(ϕ) ≡ □ϕ over temporal accessibility: ϕ holds at all future time steps. Uses necessity over forward-reachable states.
- F(ϕ) ≡ ♢ϕ over temporal accessibility: ϕ holds at some future time step. Uses possibility over forward-reachable states.
- U(ϕ, ψ): ϕ holds continuously until ψ becomes true. Implemented via a backward dynamic-programming sweep using Łukasiewicz connectives for differentiability.
The Until operator closes the expressiveness gap with STLCG (Leung et al., IJRR 2023), which supports the full fragment of signal temporal logic including Until.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_steps
|
int
|
Number of discrete time steps. |
required |
tau
|
float
|
Temperature. Default 0.1. |
0.1
|
top_k
|
Optional[int]
|
Top-k aggregation for G / F (see
:class: |
None
|
Example::
>>> temporal = TemporalOperator(num_steps=5)
>>> A_temporal = temporal.build_forward_accessibility()
>>> globally_phi = temporal.globally(prop_bounds, A_temporal)
>>> finally_phi = temporal.finally_(prop_bounds, A_temporal)
>>> until_result = temporal.until(phi_bounds, psi_bounds, A_temporal)
Source code in torchmodal/systems.py
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 238 239 240 241 | |
build_forward_accessibility ¶
Build a forward-time accessibility matrix.
Creates a lower-triangular-inverted matrix where each time step can access all current and future steps.
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
globally ¶
G(ϕ) — globally, ϕ holds at all accessible future states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
temporal_accessibility
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Bounds for G(ϕ). |
Source code in torchmodal/systems.py
finally_ ¶
F(ϕ) — finally, ϕ holds at some accessible future state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
temporal_accessibility
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Bounds for F(ϕ). |
Source code in torchmodal/systems.py
until ¶
U(ϕ, ψ) — ϕ holds continuously until ψ becomes true.
Computes the Until operator using a backward dynamic-programming
sweep: U_t = ψ_t ∨ (ϕ_t ∧ U_{t+1}).
This operator is essential for expressing liveness and safety-with-guarantee properties that cannot be captured by G (globally) and F (finally) alone.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phi_bounds
|
Tensor
|
|
required |
psi_bounds
|
Tensor
|
|
required |
temporal_accessibility
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Bounds for ϕ U ψ. |
Source code in torchmodal/systems.py
MultiAgentKripke ¶
Bases: Module
Multi-agent Kripke structure with temporal and epistemic dimensions.
Creates a spacetime state space S = W × T where: - W is a set of agent worlds - T is a set of time steps
Supports composite modal operators like K∘G (epistemic-temporal knowledge) and K∘F (epistemic-temporal possibility).
This mirrors the architecture used in the Diplomacy and CaSiNo experiments from the paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_agents
|
int
|
Number of agents |W|. |
required |
num_steps
|
int
|
Number of time steps |T|. |
1
|
tau
|
float
|
Temperature. Default 0.1. |
0.1
|
learnable_epistemic
|
bool
|
If |
True
|
init_bias
|
float
|
Initial bias for learnable epistemic logits. Default -2.0 ("prior of distrust"). |
-2.0
|
top_k
|
Optional[int]
|
Top-k aggregation for every □ / ♢ in this structure (K, G,
F and the composites), see :class: |
None
|
Source code in torchmodal/systems.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
get_epistemic_accessibility ¶
Get the epistemic (agent-to-agent) accessibility matrix.
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
get_full_accessibility ¶
Get the combined spacetime accessibility matrix.
Combines temporal accessibility (within-agent time flow) with epistemic accessibility (between-agent trust).
The result is kron(A_epi, triu(1_T)): agent trust scaled
by forward-time flow.
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
K ¶
Epistemic knowledge operator over agents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
features
|
Optional[Tensor]
|
Optional features for metric accessibility. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
G ¶
Temporal globally operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
F ¶
Temporal finally operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
K_G ¶
Composite K∘G: agent knows ϕ holds globally.
First applies G (temporal necessity), then K (epistemic).
.. warning::
This is two □ levels, so it carries twice the slack. Both G
and K are :func:~torchmodal.functional.necessity neurons, so
the returned interval is widened by
:math:\tau H_G(w) + \tau H_K(w) — the sum of the two levels'
box widths, each bounded by :math:\tau \log n — rather than
by one. Measured with 3 agents, 4 steps, tau=0.1,
phi=[1,1]: G alone gives L = 0.861 while K_G
gives L = 0.770, the two
:func:~torchmodal.functional.box_width_entropy levels
contributing 0.1387 each. Budget accordingly: the faithful
nesting depth :math:k^* = 1/(\tau\bar{H}) is consumed twice
as fast by this composite as by a bare K. See
:func:torchmodal.functional.necessity for the per-level
table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
features
|
Optional[Tensor]
|
Optional features for metric accessibility. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
K_F ¶
Composite K∘F: agent knows ϕ holds eventually.
First applies F (temporal possibility), then K (epistemic).
.. warning::
This is two modal levels, so it carries twice the slack —
a ♢ (F) followed by a □ (K). Each widens the interval by its
own :func:~torchmodal.functional.box_width_entropy,
:math:\tau H(w) \le \tau \log n, and the two accumulate.
See :meth:K_G for the measured figures and
:func:torchmodal.functional.necessity for the per-level
table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
|
required |
features
|
Optional[Tensor]
|
Optional features for metric accessibility. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in torchmodal/systems.py
forward ¶
Apply a named modal operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prop_bounds
|
Tensor
|
Truth bounds tensor. |
required |
operator
|
str
|
One of |
'K'
|
features
|
Optional[Tensor]
|
Optional features for metric accessibility. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Transformed truth bounds. |