Advanced maths

Markov decision process calculator with optimal policy

Solve a small discounted finite Markov decision process by Bellman iteration and return a stationary action policy.

Your numbers

Rows separated by semicolons; values by commas.
Rows separated by semicolons; values by commas.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Solve a small discounted finite Markov decision process by Bellman iteration and return a stationary action policy. Unlike a fixed Markov chain, the model lets each state offer several actions with different rewards and transition probabilities. A residual-based numerical value-error bound accompanies the solution rather than an unexplained convergence flag.

THE FORMULAV(s)=max_a[r(s,a)+gamma ΣP(s,a,s′)V(s′)].

A worked example

One state has two actions with immediate rewards 1 and 2, each returning to that same state. At discount 1/2, always choosing reward 2 gives V = 2 + V/2, hence optimal value V = 4.

Good to know

≤12states/5actions, constant transition probabilities, discount<0.99. Bellman residual yields numerical value-error bound; no uncertain or average-reward model.

Separate actions from stochastic outcomes

At a state, the decision maker selects an action. That action determines an immediate expected reward and a probability distribution over next states. The next state is still random even when the policy is deterministic. A fixed Markov chain is the special situation where no action choice remains to optimize.

The reward matrix has one row per state and one column per available action. All states use the same action count in this form. Transition rows are state-major: every action for state zero, then every action for state one, and so on. Misordering those rows changes the process rather than merely relabeling a table.

A discounted one-state MDP chooses the larger recurring reward0reward 2reward 1Discount: 1/2V=2+(1/2)VOptimal value: 4
One state has two actions with immediate rewards 1 and 2, each returning to that same state. At discount 1/2, always choosing reward 2 gives V = 2 + V/2, hence optimal value V = 4. Diagram is schematic; labels specify the numerical values.

The Markov chain calculator from a transition matrix holds a transition law fixed. A decision process compares action-dependent rewards and transition rows, choosing a policy under an explicit discount factor.

Choose the discounted objective

The objective maximizes expected reward summed over future steps with discount gamma to the step power. Gamma must be nonnegative and below the supported upper bound of 0.99. A smaller gamma emphasizes near-term rewards, while a larger one gives later consequences more weight.

This is not average reward per step or a finite-horizon terminal-payoff problem. There is no hidden stopping time or automatic terminal state. To represent an absorbing state, enter its transition and reward structure explicitly, checking whether continued reward after absorption is actually intended.

Derive the Bellman update

For each state and action, compute immediate reward plus gamma times expected current value of the next state. Taking the largest action value gives the next state-value estimate. Repeating this map is value iteration, and selecting an action attaining the maximum produces the displayed policy.

The maximum is over each state’s available actions, not over future random outcomes. Replacing the expectation by the best possible next state would solve an optimistic deterministic problem instead. The normalized transition rows are therefore fundamental inputs, not optional weights that may sum to any total.

Verify the one-state example

The default model has one state and two actions, rewarding one or two. Both actions return to the same state with probability one. At discount one half, always choosing the second action gives value two plus one plus one half and so on, equal to four.

The Bellman equation is V=max(1+V/2,2+V/2), whose solution is V=4 with the second action selected. Its zero-based action index is one. This distinction between action label and reward matters when reading a policy table; index one is not a value estimate of one.

Understand the error bound

For discounted finite models, the Bellman map is a contraction in the maximum norm. If a computed value vector has Bellman residual r, its distance from the optimal value vector is bounded by r divided by one minus gamma. The page reports this bound after its final update.

The bound concerns numerical solution of the entered model, not uncertainty in estimated rewards or probabilities. A very small Bellman residual can coexist with a poor real-world policy if the model is wrong. Larger gamma also amplifies a given residual bound, which explains why a tighter iteration tolerance can be needed near one.

Read policy ties without inventing uniqueness

Several actions can achieve the same optimal state value. The calculator selects a reproducible maximizing index, not a proof that every other action is strictly worse. Small perturbations of nearly tied rewards can change the chosen index while leaving optimal values almost unchanged.

The output is a stationary policy under the fixed model. It does not adapt transition estimates from new observations or explore uncertain actions as a learning algorithm would. No reinforcement-learning training, partial observability or belief-state update is implied by the existence of a policy table.

Keep model validity separate from optimization

The form supports up to twelve states and five actions, with a finite iteration cap. Invalid transition probabilities or failure to reach the numerical bound are rejected. Negative rewards are allowed and can represent costs only if their signs align with the maximization convention.

State aggregation, missing consequences and changing environments can matter more than numerical precision. Validate the action model and reward scale before interpreting a policy as a practical recommendation. The calculator certifies an approximate value solution for the supplied discounted MDP, not safe behavior in an unmodeled real system.

A few questions, answered

Are policy entries rewards?

No. They are zero-based action indices for each state.

Does the error bound include uncertainty in probabilities?

No. It bounds numerical value error for the entered model only.

Is this reinforcement learning?

No. Transitions and rewards are supplied, and the page solves the resulting known discounted model.

Can several actions be optimal?

Yes. One maximizing action is returned under a reproducible tie convention.

Find your calculator

Start typing to explore the library.