Town experiment · Builder guide
Write a household policy.
Give each household a decision rule, run four weeks of shift work, and compare two ways of assigning the same jobs. The model and example run locally with Node.js.
Run the example
Save these two files in the same directory. Open a terminal there and run the command below with Node.js 22 or later. Both files are plain JavaScript and use only built-in language features.
node policy.mjs
The example uses seed 42 and a two-day cash reserve. The interactive town uses a four-day reserve. For this seed and example policy, you should see 111 credits of unmet food cost with the lottery and 96 with lowest-cash priority.
The policy and rollout
A household sees its own cash and stamina and the posted market terms. Collect all twelve decisions from the same morning, then settle the market once.
import { DAYS, MODEL_VERSION, observe, reset, step } from './model.mjs';
function decide(observation) {
const canWork = observation.actions.includes('request');
const needsCash = observation.cash < 2 * observation.cost;
return canWork && needsCash ? 'request' : 'rest';
}
function run(seed, rule) {
let state = reset(seed);
let totalReward = 0;
for (let day = 1; day <= DAYS; day += 1) {
const actions = Array.from({ length: 12 }, (_, index) => {
const morning = observe(state, index + 1, rule);
return decide(morning);
});
state = step(state, actions, rule);
const dailyReward = -state.frame.unmet;
totalReward += dailyReward;
}
return {
version: MODEL_VERSION,
seed,
rule,
unmetFoodCredits: state.frame.cumulativeUnmet,
totalReward,
};
}
for (const rule of ['lottery', 'lowest-cash']) {
console.log(JSON.stringify(run(42, rule)));
}
Here the optional reward is the negative of the town’s unmet food cost each day. It treats every unpaid credit equally and omits household inequality and the cost of working. You can define another objective from the ledger.
Environment interface
reset(seed)- Start on day 0 with opening balances. Use an integer from 0 to 4294967295. The same seed fixes the opening town and external market draws.
observe(state, householdId, rule)- Get the household’s next morning. IDs run from 1 to 12. The observation includes its cash, stamina, food cost, legal actions, day, active hiring rule, shift capacity, wage and surcharge. After day 28 it returns
null. step(state, actions, rule)- Supply one action per household in ID order. A household can rest, or request a shift if it has stamina. The function returns a new state with daily and cumulative ledger entries. Invalid actions and steps after day 28 throw an error.
runPair(seed, rule)- Replay the fixed four-day reserve policy used in the browser, returning both complete day-by-day histories. Set
ruleto'lottery'for an identical-run control or'lowest-cash'to change priority on day 8.
Use the same seed and policy in both arms to isolate the hiring-rule change. Repeat on independently chosen seeds before drawing conclusions about policy performance. The downloadable model exposes full state to its caller and is intended for trusted local experiments.