# What Environment Properties Select Agents For World-Modeling?

*Thanks to John Wentworth for helpful critique.*

0\. Introduction
----------------

Agency/goal-driven behavior is a complex thing. By definition, it implies [a system that is deliberately choosing to take actions that will achieve the goal it wants](https://www.lesswrong.com/posts/qhsELHzAHFebRJE59/a-greater-than-b-greater-than-a). That requires, at the least, an internalized "goal", a world-model, a mechanism that allows the world-model to be ran iteratively as the agent searches for optimal actions, and so on.

But taking some intuitive, idealized, pre-established notion of "agency", and translating these intuitions into formalisms, is somewhat backwards. If our goal is to understand the kinds of AI we'll get, a more natural question seems to be: "what properties of the environment make goal-driven behavior *necessary*, and what shape the internal mechanisms for it *need* to take?".

Going further, we can notice that the SGD and evolution are both incremental processes, and agency is too complex to develop from scratch. So we can break the question down further: "what properties of the environment make the *building blocks* of agency necessary?". How are these building blocks useful in the intermediary stages, before they're assimilated into the agency engine? What pressures these building blocks to incrementally improve, until they become precise/advanced enough to serve the needs of agency?

This post attempts to look at the incremental development of one of these building blocks — world models, and the environment conditions that make them necessary and incentivize improving their accuracy.

1\. Intuitions
--------------

When trying to identify the direction of incremental improvement, it helps to imagine two concrete examples at different ends of the spectrum, then try to figure out the intermediary stages between them.

At one end of the spectrum, we have glorified look-up tables. Their entire policy is loaded into them during development/training, and once they're deployed, it's not modified.

At the other end of the spectrum, we have idealized agents. Idealized agents model the environment and adapt their policy to it, on a moment-to-moment basis, trying to perform well in the specific situation they're in.

In-between, we have things like humans, who use intensive action-optimization reminiscent of idealized agents *in tandem* with heuristics and built-in instincts, which they often apply "automatically", if the situation seems to call for it on the surface.

Between look-up tables and humans, we have in-context learners, like advanced language models. They can improve their performance on certain problems post-deployment, after being shown one or a few examples. Much like humans, they tailor their approach.

This is the value that seems to be increasing, from look-up tables to language models to humans to idealized agents. The more "agenty" a system is, the more it tailors its policy to the precise details of the situation it's in, rather than relying on pre-computed heuristics.

Turning it around, this implies that there are some environment properties that would make this necessary. What are those?

GPT-3 is often called a "shallow pattern-matcher". One of the stories of how it works is that it's an advanced search engine. It has a vast library of contexts and optimal behaviors in them, and whenever prompted with something, it finds the best matches, and outputs some weighted sum of them. Alternatively, it may be described as a library of human personas/writing styles, which treats prompts as information regarding which of these patterns it is supposed to roleplay.

That is "shallow pattern-matching". It looks at the surface reading of the situation, then acts like it's been taught to act in situations that looked like this. The more advanced a pattern-matcher gets, the deeper its cognitive algorithms may become, but it approaches agency from the top-down.

This trick will not work in contexts where the optimal policy can't be realistically "interpolated" from a relatively small pool of shallow $\langle \text{situation}, \text{response} \rangle$ pairs it's been taught. Certainly, we can imagine an infinite look-up table of such entries for any possible situation which were pre-computed to infallibly impersonate an agent. But in practice, we're constrained by practicality: ML models' memory size is not infinite, and neither are our training sets.

So, if we ask a shallow pattern-matcher to offer up a sufficiently good response to a sufficiently specific situation, in the problem domain where it doesn't have a vast enough library of pre-computed optimal responses, it will fail. And indeed: language models are notoriously bad at retaining consistency across sufficiently long episodes, or coherently integrating a lot of details. They perform well enough as long as there are many ways to perform well enough, or if the problem is simple. But not if we demand a *specific* and *novel* solution to a complex problem.

How can we translate these intuitions into math?

2\. Formalisms
--------------

### 2.1. Foundations

I'll be using the baseline model architecture described in [Agents Over Cartesian World Models](https://www.lesswrong.com/posts/LBNjeGaJZw7QdybMw/agents-over-cartesian-world-models). To quote:

> There are four types: actions, observations, environmental states, and internal states. Actions and observations go from agent to environment and vice-versa. Environmental states are on the environment side, and internal states are on the agent side. Let $A, O, E, I$ refer to actions, observations, environmental states, and internal states.
> 
> We describe how the agent interfaces with the environment with four maps: $\text{observe}$, $\text{orient}$, $\text{decide}$, and $\text{execute}$.
> 
> *   $\text{observe}: E \to \Delta O$ describes how the agent observes the environment, e.g., if the agent sees with a video camera, $\text{observe}$ describes what the video camera would see given various environmental states. If the agent can see the entire environment, the image of $\text{observe}$ is distinct point distributions. In contrast, humans can see the same observation for different environmental states.
> *   $\text{orient}: O \times I \to \Delta I$ describes how the agent interprets the observation, e.g., the agent's internal state might be memories of high-level concepts derived from raw data. If there is no historical dependence, $\text{orient}$ depends only on the observation. In contrast, humans map multiple observations onto the same internal state.
> *   $\text{decide}: I \to \Delta A$ describes how the agent acts in a given state, e.g., the agent might maximize a utility function over a world model. In simple devices like thermostats, $\text{decide}$ maps each internal state to one of a small number of actions. In contrast, humans have larger action sets.
> *   $\text{execute}: E \times A \to \Delta E$ describes how actions affect the environment, e.g., code that turns button presses into game actions. If the agent has absolute control over the environment, for all $e \in E$, the image of $\text{execute}(e, \cdot)$ is all point distributions over $E$. In contrast, humans do not have full control over their environments.

The primary goal is to open up the black box that is $\text{decide}$. I'll be doing it by making some assumptions about the environment and the contents of $I$.

For clarity, let $a \in A$, $o \in O$, $e \in E$, and $i \in I$ be individual members of the sets of actions, observations, environmental states, and internal states respectively, and $\text{agent}:=\text{decide} \circ \text{orient} \circ \text{observe}$ be the function representing the agent.

### 2.2. Optimality

Let's expand the model by introducing the following function :$$

$$
\text{optimality}: E \times A \to \mathbb{R}
$$

$\text{optimality}$ takes in the current environment-state and the action the agent took in response to it, and returns a value representing "how well" the agent is performing. $\text{optimality}$ is not an utility function, but defined over one. It considers all possible trajectories through the environment's space-time available to the agent at a given point, calculates the total value the agent accrues in each trajectory, then returns the optimality of an action as the amount of value accrued in the best trajectory of which this action is part.

Formally: Let $\Pi_{e_{i} \to a_{k}}$ be the set of all possible policies that start with taking an action $a_{k}$ in the environmental state $e_{i}$, and $\text{utility}: E \to \mathbb{R}$ represent the utility function defined over the environment.[^q9qydamwyi] Given an initial environment-state $e_i$, the optimality of taking an action $a_{k}$ is:

$$
\text{optimality}(e_i, a_{k}) := \underset{\pi \in \Pi_{e_{i} \to a_{k}}}{\text{max}} \left( \sum_{t=0}^\infty \lambda^t \mathbb{E}_{e \sim \pi|t}[\text{utility}(e)] \right)
$$

It functions as an utility function in some contexts, such as myopic agents with high time discounting $\lambda$, one-shot regimes where the agent needs to solve the problem in one forward pass, and so on.

Moving on. For a given environment-state $e_{i}$, we can define the optimal action, as follows:

$$
a_{\text{opt}|e_{i}} \in A\text{ | }∀a_{k} \in A: \text{optimality}(e_{i}, a_{\text{opt}|e_{i}}) \geq \text{optimality}(e_{i}, a_{k})
$$

Building off of that, for any given environment-state $e_{i}$ and a minimal performance level $r \in [0;1]$, we define a set of *near-optimal actions*:

$$
A_{\text{n-o}|e_{i}}^r:= \{ a_{k} \in A\text{ | }∀a_{k}: \frac{\text{optimality}(e_{i}, a_{k})}{\text{optimality}(e_{i}, a_{\text{opt}|e_{i}})} \geq r \}
$$

Intuitively, an action is near-optimal if taking it leaves the agent with the ability to capture at least a fraction $r$ of all future value. As a rule-of-thumbs, we want that fraction fairly high, $r \approx 1$.

A specific action from the set of near-optimal actions shall be denoted $a_{\text{n-o}|e_{i}}^r \in A_{\text{n-o}|e_{i}}^r$.

Now, we can state the core assumption of this model. For two random environment-states $e_i$ and $e_k$ and some distance function $\text{DIST}_E(\cdot||\cdot)$, we have:

$$
\text{(1)} \hspace{0.5cm} P(a_{\text{n-o}|e_{k}}^r \in A_{\text{n-o}|e_{i}}^r) \propto \frac{1}{\text{DIST}_E(e_i||e_k)}
$$

That is: the more similar two situations are, the more likely it is that if an action does well in one of them, it would do well in the other.

Note that this property places some heavy constraints on the model of the environment and the distance metric we're using.

Some intuitions. Imagine the following three environment-states:

1.  Our universe at the moment of an AGI's creation; AGI is created by humanity.
2.  Our universe at the moment of an AGI's creation, except all the galaxies beyond the Solar System have been randomly shuffled around.
3.  Our universe at the moment of an AGI's creation, except the AGI (with the same values) is created by a non-human alien civilization that evolved in place of humans.

For a purely physical, low-level distance metric, (1) and (2) would be much more different than (1) and (3) (assuming no extra-terrestrial aliens). And yet, the optimal trajectories in (1) and (2) would be basically the same for a certain abstract format of "actions" (the only difference would be in the specific directions the AGI sends out Von Neumann ships after it takes over the world), whereas the optimal trajectories for (1) and (3) would start out wildly different, depending on the specifics of the AGI's situation (what social/cognitive systems it would need to model and subvert, which manipulation tactics to use).

I'm not sure what combinations of $\langle \text{environment model}, \text{distance function} \rangle$ can be used here.

Suitable environment-models in general seem to be a bottleneck on our ability to adequately model agents. At the very least, we'd want some native support for multi-level models and natural abstractions.

### 2.3. The Training Set

Next, we turn to the agent proper. We introduce the set of "optimal policies" $C$. For every environment-state $e_{i}$, it holds a 2-tuple $c_{i} \in C:= \langle o_{i}, a_{\text{n-o}|e_{i}}^r \rangle$, where $o_{i} := \text{observe}(e_{i})$ and $a_{\text{n-o}|e_{i}}^r \in A_{\text{n-o}|e_{i}}^r$.

$C$, thus, is the complete set of instructions for how a near-optimal agent with a given utility function over the environment should act in response to any given observation. Note that the behavior of an agent blindly following it would not be optimal unless its sensors can record all of the information about the environment. There would be collusion, in which different environment-states map to the same observation; a simple instruction-executor would not be able to tell them apart.

The training set $C_{T} \subset C$ is some set of initial optimal policies we'll "load" into the agent during training. $|C_{T}|=M$, where $M$ is upper-bounded by practical constraints, such as the agent's memory size, the training time, the compute costs, difficulties with acquiring enough training data, and so on.

Formally, it's assumed that $I$ is an $n$-tuple, and $C_{T} \in I$, so that every $c_{i} \in C_{T}$ is accessible to the agent's $\text{orient}$ and $\text{decide}$ functions.

To strengthen some conclusions, we'll assume that $C_{T}$ is well-chosen to cover as much of the environment as possible given the constraint posed by $M$:

$$
\text{(2)} \hspace{0.5cm} {∀c_{i}, c_{k} \in C_{T}: \text{DIST}_E(e_{i}||e_{k})=\text{const}}
$$

That is, all of the policy examples stored in memory are for situations maximally different from each other. Even if it's implausible for actual training sets, we may assume that this optimization is done by the SGD. Given a poorly-organized training set, where solutions for some sub-areas of the environment space are over-represented, the SGD would pick and choose, discarding the redundant ones.

Finally, for any given $e_{i}$ and a distance variable $d \in [0; +\infty)$, we define a set of *reference policies*:

$$
C_{d|e_{i}} \subset C := \left\{ c_{r} \in C \text{ | } ∀c_{r}: \text{DIST}_E\left(e_{i} || e_{r} \right)\leq d\right\}
$$

Computing the full set $C_{d|e_{i}}$ is, again, intractable for any sufficiently complex environment, so I'll be using $C_{d|e_{i}}^* \subset C_{d|e_{i}}$ to refer to any practically-sized sample of it.

Furthermore, to bring the model a little closer to how NNs seem to behave in reality, and to avoid some silliness near the end, we'll define the set of *reference actions*. Letting $\text{second}$ be a function that takes the second element of a tuple,

$$
A_{d|e_{i}}^* := \{\text{second}(c_r)\text{ | }∀c_r \in C_{d|e_{i}}^*\}
$$

That is, it's just some reference policy set with the observation data thrown out.

### 2.4. Agent Architecture

Now, let's combine the formalisms from 2.2 and 2.3.

From (1), we know that knowing the optimal policy for a particular situation gives us information about optimal policies for sufficiently similar situations. Our agent starts out loaded with a number of such optimal policies, but tagged with subjective descriptions of environment-states, not the environment-states themselves. Nonetheless, it seems plausible that there are ways to use them to figure out what to do in unfamiliar-looking situations.

Let's decompose $\text{decide}$ as follows:

*   $\text{decide} := \text{act}\circ f$
*   $f: I \to A$
*   $\text{act}: A \to  A$

$\text{act}$ is the function that does what we're looking for. It accepts a set of actions, presumed to be a bounded set of reference actions $A_{d|e_{i}}^*$. It returns the action the agent is to take.

The main constraint on $\text{act}$, which seems plausible from (1), is as follows:

$$
P(\text{act}(A_{d|e_{i}}^*) \in A_{\text{n-o}|e_{i}}^r) \propto \frac{1}{d}
$$

Informally, $\text{act}$ is some function which, given a set of actions that perform well in some situations similar to this one, tries to generate a good response to the current situation. And it's more likely to succeed the more similar the reference situations are to the current one.

By implication, for some $r$,

$$
\text{(3)} \hspace{0.5cm} P \left(\frac{\text{optimality}(e_{i}, \text{agent}(o_{i}))}{\text{optimality}(e_{i}, a_{\text{opt}|e_{i}})} \geq r \right) \propto \frac{1}{d}
$$

The shorter the distance $d$, the more likely it is that the agent will perform well.

Suppose we've picked some $d'$ that would allow our agent to arrive at some desired level of performance $r$ with high probability. That is, for a random environment state $e_{i}$,

$$
P(\text{act}(A_{d|e_{i}}^*) \in A_{\text{n-o}|e_i}^r\text{ | }d=d') \approx 1
$$

The question is: how does $f: I \to A$ compute $A_{d|e_{i}}^*$; and before it, $C_{d|e_{i}}^*$?

### 2.5. Approximating the Reference Policies Set

**i.** In the most primitive case, it may be a simple lookup table. $f$ can function by comparing the observation (which presumably can be extracted from the internal state) against a list of stored observation-action pairs, then outputting the closest match. Letting $\text{first}$ be a function which retrieves the first element of a tuple, we get:

$$
C_{d|e_{i}}^* \approx \{c_{r} \in C_{T} \text{ | } \underset{c_{r} \in C_{T}}{\text{min}}(\text{DIST}_O(o_{i}||\text{first}(c_r)))\}
$$

The follow-up computations are trivial: discard the observation data, then retrieve the only member of the set, and that's the agent's output.

Note that the distance function $\text{DIST}_O$ used to estimate the similarity of observations isn't necessarily the same as the one for environment-states, given that the data formats of $E$ and $O$ are likely different.

The underlying assumption here is that the more similar the observations, the more likely it is that the underlying environment-states are similar as well.

That is, for a given $d$,

$$
P(\text{DIST}_E(e_{i}, e_{k}) \leq d\text{ | }\text{DIST}_{O}(o_i, o_k)\leq d_o) \propto \frac{1}{d_o}
$$

Not unlike (1), this puts some constraints on what we mean by "observation". Having your eyes open or closed leads to a very large difference in observations, but a negligible difference in the states of the world. As such, "observation" has to, at the least, mean a certain *collection* of momentary sense-data relevant to the context in which we're taking an action — as opposed to every direct piece of sense-data we receive moment-to-moment.

For a language model, at least, the definition is straightforward: an "observation" is the entire set of tokens it's prompted with.

**ii.** A more sophisticated technique is to more fully approximate $C_{d|e_{i}} \cap C_{T}$ by computing the following:

$$
C_{d|e_{i}}^* \approx\left\{ c_{r} \in C_{T} \text{ | } ∀c_{r}: \text{DIST}_O(o_{i}||\text{first}(c_{r}))\leq d\right\}
$$

That is, it assembles the set of all situations that looked "sufficiently similar" to the agent's observation of the current situation.

$\text{act}$ would be doing something more complex in this case, somehow "interpolating" the optimal action from the set of reference actions. This, at least, seems to be [how GPT-3 works](https://www.lesswrong.com/posts/AcKRB8wDpdaN6v6ru/interpreting-gpt-the-logit-lens): it immediately computes some prospective outputs, dumps the input, and starts refining the outputs.

**iii.** A still more advanced version implements few-shot learning. The following changes to the agent's model would need to be made:

1.  Assume that the agent has post-training memory to which it can write additional $c_{i}$ entries based on its experience.
2.  Modify $\text{decide}$ so that it can affect the internal state $I$ as well, writing into it the action the model takes.
3.  Introduce some mechanism by which the model can evaluate its performance.
4.  Add a function that looks at the resulting 3-tuples $\langle o_{n}, a_{n}, \text{optimality}(e_{n}, a_{n})\rangle$, and appends $\langle o_{n}, a_{n} \rangle$ to $C_{T}$ if the behavior is optimal enough.

I won't write all of this out, since it's trivial yet peripheral to my point, but I'll mention it. It's a powerful technique: it's pretty likely that the $A_{d|e_{i}}^*$ computed this way would have a short $d$ to the next environment-state the agent would find itself in — inasmuch as the agent would already be getting policy data about the specific situation it's in.

(c) is the only challenging bit. A proper implementation of it would make the model a mesa-optimizer. It can be circumvented in various ways, however. For example, the way GPT-3 does it: it essentially assumes that whatever it does is the optimal action, foregoing a reward signal.

**iv)** But what if $C_{d|e_{i}} \cap C_{T}  = \{ ø \}$? That is, what if *none* of the reference policies loaded into the agent's memory are for situations "sufficiently similar" to the one the agent is facing now?

Furthermore, what if this state of affairs is endemic to a given environment?

(4) In detail:

*   We pick some environment $E$ and a minimal performance level $r$. (1) holds in this environment.
*   We take a ML model with a memory size $M$, and stuff it to the gills with reference policies $C_{T}$, picked to cover as many environment-states as possible, as per (2).
*   The above setup corresponds to some underlying distance $d'$, such that: In this environment, for a random environment-state$$ $e_{i}$, the reference policies need to be intended for situations not farther than $d'$ from it, in order for $\text{act}$ to be able to shallowly interpolate the optimal policy for it.
*   However, $P(C_{d'|e_{i}} \cap C_{T} \neq \{ ø \} \text{ | }e_{i}) \approx 0$.

How can the SGD/evolution solve this?

The following structure seems plausible. Decompose $f$ as follows:

*   $f:= \text{optimize} \circ \text{model}$
*   $\text{model}(I):= P(e_{i}|I)$. This function takes in the current internal state, which includes the lookup table of optimal policies $C_{T}$, the current observation, and plausibly a history of other recent observations. It returns the probability distribution over the current environment-state.
*   $\text{optimize}(P(e_{i}|I):= a_{\text{n-o}|e \sim P(e_{i}|I)}^r \in A_{\text{DIST}_E(P(e_{i}|I)||e_{i})|e_{i}}^*$. This function takes in the probability distribution over the current environment-state, and computes the optimal response to some environment state $e$ sampled from that distribution. Alternatively, it may return a *set* of such responses, sampling from the distribution several times. A bounded set of reference actions populated by such entries would have $d = \text{DIST}_E(P(e_{i}|I)||e_{i})$.

Plugging it into (3), we get:

$$
P \left( \frac{\text{optimality}(e_{i},\text{agent}(o_{i}))}{\text{optimality}(e_{i}, a_{\text{opt}|e_{i}})} \geq r \right) \propto \frac{1}{\text{DIST}_E(P(e_{i}|I)||e_{i})}
$$

Thus, for situations meeting the conditions of (4), an agent would face a pressure to improve its ability to develop accurate models of the environment.

3\. Closing Thoughts
--------------------

There's a few different ways to spread the described functionality around. For example, we may imagine that modelling and optimization happen in $\text{act}$: that $f$ just "primitively" assembles a reference set based on observations, and variants of $\text{model}$ and $\text{optimize}$ are downstream of it. In terms of telling an incremental story, this might actually be the *better* framing. Also, I probably under-used the $\text{orient}$ function; some of the described functionality might actually go into it.

None of this seems to change the conclusion, though. We can define some simple, intuitive environment conditions under which shallow pattern-matching won't work, and in them, *some* part of the agent has to do world-modelling.

From another angle, the purpose of this model is to provide prospective desiderata for *environment-models* suitable for modeling agents. As I'd mentioned in 2.2, I think the lack of good ways to model environments is one of the main barriers on our ability to model agency/goal-directedness.

A weaker result is suggested by $\text{optimize}$. The definition of an optimal action $a_{\text{opt}|e_{i}}$performs an explicit search for actions that best maximize some value function. It would be natural to expect that $\text{optimize}$ would replicate this mechanic, implying mesa-optimization.

I'm hesitant to make that leap, however. There may be other ways for $\text{optimize}$ to extract the correct action from a given environment-state, acting more like a calculator. While the need for a world-model in some conditions appears to be a strong consequence of this formalism, the same cannot be said for goal-driven search. Deriving the necessary environment conditions for mesa-optimization, thus, requires further work.

Appendix
--------

This is a bunch of formulae deriving which seemed like an obvious thing to do, but which turned out so messy I'm not sure they're useful.

**1\.** We can measure the "generalizability" of an environment. That is: Given an environment $E$ and a desired level of performance $r$, what is the "range" centered on a random state $e_i$ within which lie environment-states $e_{k}$ such that the optimal policies for these states are likely near-optimal for the initial state $e_{i}$?

The answer, as far as I can tell, is this monstrosity:

$$
g(r):= \frac{1}{|E|}\sum_{e_{i} \in E} \underset{l \in [0; +\infty)}{\text{max}} \left( l \text{ | } P \left( \frac{\text{optimality}(e_{i}, a_{\text{opt}|e \sim \text{unif}(\{e_{k}\text{ | }\text{DIST}_E(e_{k}||e_{i})\leq l\})})}{\text{optimality}(e_{i}, a_{\text{opt}|e_{i}})}\geq r\right) \approx1 \right)
$$

Where $e \sim \text{unif}(\{e_{k}\text{ | }\text{DIST}_E(e_{k}||e_{i})\leq l\})$ is an environment-state sampled from the uniform distribution over the set of states not farther than $l$ $$from $e_{i}$.

For a given $r \approx 1$, this function can be used to measure the intuition I'd referred to back in Part 1: how "tailor-made" do policies need to be to do well in a given environment?

**2.** Suppose we have an $M$-sized training set $C_T$, and the set of corresponding environment-states $E_{C_T}$, such that every policy in $C_T$ is optimal for some environment-state in $E_{C_T}$. We can define the average distance from a random environment-state to one of the states in $E_{C_T}$:

$$
\langle d \rangle := \frac{1}{|E|} \underset{e_i \in E}{\sum} \underset{e_{k} \in E_{C_{T}}}{\text{min}}(\text{DIST}_E(e_{i}||e_{k}))
$$

**3.** And now, for a given environment $E$, a training set $C_T$ of size $M$, and a target performance $r$, we can theoretically figure out whether the ML model would need to develop a world-model. The condition is simple:

$$
\langle d \rangle > g(r)
$$

This, in theory, is a powerful tool. But the underlying functions seem quite... unwieldy. Faster/simpler approximations would need to be found before it's anywhere close to practically applicable.

[^q9qydamwyi]: Expanding the model to support other kinds of utility functions from Agents Over Cartesian World Models seems possible, if a bit fiddly.