Skip to content

Commit

Permalink
Merge pull request #4 from pik-gane/viz_compat
Browse files Browse the repository at this point in the history
Compatibility with web book and viz
  • Loading branch information
mensch72 authored Feb 21, 2024
2 parents 93797bc + d851f12 commit 3f9c133
Show file tree
Hide file tree
Showing 19 changed files with 1,752 additions and 18,652 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: "Test"
on:
pull_request:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v25
with:
nix_path: nixpkgs=channel:nixos-unstable
- run: nix-shell --run "npm i && npm run test"
36 changes: 3 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,21 @@ Extension of webppl-agents with [additional non-maximizing agent types](/src/age

To test:
```
webppl --require webppl-dp --require . examples/runVerySimpleGW.wppl -- --gw GW3 --verbose
./run.sh examples/runVerySimpleGW.wppl -- --gw GW3 --verbose
```

(original README.md:)

# webppl-agents

This package provides constructors for MDP and POMDP agents, for gridworld and bandit environments, and a function for visualizing gridworlds:

- Environments:
- [`makeGridWorldMDP`](https://github.com/agentmodels/webppl-agents/blob/master/src/environments/makeGridWorldMDP.wppl)
- [`makeGridWorldPOMDP`](https://github.com/agentmodels/webppl-agents/blob/master/src/environments/makeGridWorldPOMDP.wppl)
- [`makeBanditPOMDP`](https://github.com/agentmodels/webppl-agents/blob/master/src/environments/makeBanditPOMDP.wppl)
- Agents:
- [`makeMDPAgent`](https://github.com/agentmodels/webppl-agents/blob/master/src/agents/makeMDPAgent.wppl) + [`simulateMDP`](https://github.com/agentmodels/webppl-agents/blob/master/src/simulation/simulateMDP.wppl)
- [`makePOMDPAgent`](https://github.com/agentmodels/webppl-agents/blob/master/src/agents/makePOMDPAgent.wppl) + [`simulatePOMDP`](https://github.com/agentmodels/webppl-agents/blob/master/src/simulation/simulatePOMDP.wppl)
- Visualization:
- [`GridWorld.draw`](https://github.com/agentmodels/webppl-agents/blob/master/src/visualization/gridworld.js) (also available as `viz.gridworld` if [webppl-viz](https://github.com/probmods/webppl-viz) is used)

## Installation

To globally install `webppl-agents`, run:

mkdir -p ~/.webppl
npm install --prefix ~/.webppl webppl-agents

This may print warnings (`npm WARN ENOENT`...) which can be ignored.

To upgrade to the latest version, run:

npm install --prefix ~/.webppl webppl-agents --force

For the agent functions, you will also need to install [webppl-dp](https://github.com/stuhlmueller/webppl-dp).

## Usage

Once installed, you can make the environment and agent functions available to `program.wppl` by running:

webppl --require webppl-dp --require webppl-agents program.wppl
./run.sh program.wppl

## Testing

Run the included test using:

webppl --require webppl-dp --require . tests/tests.wppl
./test.sh

## License

Expand Down
18 changes: 18 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{ pkgs ? import <nixpkgs> { } }:
with pkgs;
let
node = nodejs-18_x;
in
mkShell {
name = "env";
buildInputs = [
nodePackages.typescript-language-server
python3
# nodePackages.prettier
# nodePackages.eslint
node
];
LD_LIBRARY_PATH = lib.makeLibraryPath (with pkgs; [
libuuid
]);
}
36 changes: 25 additions & 11 deletions examples/runVerySimpleGW.wppl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// run via webppl --require webppl-dp --require . examples/runVerySimpleGW.wppl


var _W = webpplAgents;
var _SU = webpplAgents.satisfia;
var _PP = webpplAgents.pretty;

var env = getEnv(),
argv = env.argv,
params = extend({
Expand Down Expand Up @@ -45,6 +49,7 @@ var simulate = function(state, aleph, _t) {
if (options.verbose || options.debug) console.log(pad(state),"SIMULATE, t",t,"state",prettyState(state),"aleph4state",aleph4state,": localPolicy",JSON.stringify(localPolicy.params),"\n"+pad(state),"| action",action,"aleph4action",aleph4action,"Edel",Edel,"(terminal)");
return {
trajectory: [stepData], // sequence of [state, action] pairs
tr4viz: [state],
conditionalExpectedIndicator: Edel // expected indicator conditional on this trajectory
};
} else {
Expand All @@ -54,17 +59,18 @@ var simulate = function(state, aleph, _t) {
var nextOut = simulate(nextState, nextAleph4state, t+1);
return {
trajectory: [stepData].concat(nextOut.trajectory),
tr4viz: [state].concat(nextOut.tr4viz),
conditionalExpectedIndicator: Edel + nextOut.conditionalExpectedIndicator
};
}
};

console.log("aleph0", asInterval(aleph0));

var t0 = webpplAgents.time();
var t0 = _SU.time();
// verify meeting of expectations:
console.log("V", V(startState, aleph0));
console.log("TIME:", webpplAgents.time() - t0, "ms");
console.log("TIME:", _SU.time() - t0, "ms");
console.log("cupLoss", cupLoss(mdp.startState, aleph0));
console.log("entropy", entropy(mdp.startState, aleph0));
console.log("KLdiv", KLdiv(mdp.startState, aleph0));
Expand All @@ -74,20 +80,22 @@ var gd = agent.getData, agentData = gd();

// estimate distribution of trajectories:

var sym = simulate(mdp.startState, aleph0);

var trajDist = Infer({ model() {
return simulate(mdp.startState, aleph0).trajectory;
return sym.trajectory;
}}).getDist();

console.log("\nDATA FOR REGRESSION TESTS: \ntrajDist");
var regressionTestData = webpplAgents.trajDist2simpleJSON(trajDist);
var regressionTestData = _PP.trajDist2simpleJSON(trajDist);
console.log(JSON.stringify(regressionTestData));
console.log("END OF DATA FOR REGRESSION TESTS\n");

var trajData = trajDist2TrajData(trajDist, agent);

//console.log("trajData", trajData);

var locActionData = webpplAgents.trajDist2LocActionData(trajDist, trajData);
var locActionData = _SU.trajDist2LocActionData(trajDist, trajData);
console.log("locActionData", locActionData);

console.log("\nminAdmissibleQ:");
Expand All @@ -96,16 +104,22 @@ console.log("\nmaxAdmissibleQ:");
console.log(stateActionFct2ASCII(agent.maxAdmissibleQ, agentData.stateActionPairs));

console.log("\nQ:");
console.log(webpplAgents.locActionData2ASCII(locActionData.Q));
console.log(_PP.locActionData2ASCII(locActionData.Q));
console.log("\ncupLoss:");
console.log(webpplAgents.locActionData2ASCII(locActionData.cupLoss));
console.log(_PP.locActionData2ASCII(locActionData.cupLoss));
console.log("\nmessingPotential:");
console.log(webpplAgents.locActionData2ASCII(locActionData.messingPotential));
console.log(_PP.locActionData2ASCII(locActionData.messingPotential));
console.log("\ncombinedLoss:");
console.log(webpplAgents.locActionData2ASCII(locActionData.combinedLoss));
console.log(_PP.locActionData2ASCII(locActionData.combinedLoss));

console.log("\naction frequencies:");
console.log(webpplAgents.locActionData2ASCII(locActionData.actionFrequency));
console.log(_PP.locActionData2ASCII(locActionData.actionFrequency));


var tr=sym.tr4viz;
console.log(tr);
console.log(tr[0].loc);
console.log(tr[1].loc);

_W.draw(world, {trajectory:tr});

Loading

0 comments on commit 3f9c133

Please sign in to comment.