mirror of
https://github.com/velocitatem/PHANTOM.git
synced 2026-07-16 01:53:37 +00:00
Compare commits
62 Commits
cleanup
...
paper-firs
| Author | SHA1 | Date | |
|---|---|---|---|
| f4f2a6d9cf | |||
|
|
1950976eae | ||
| 21349c9387 | |||
| 4b392fc0a5 | |||
| a53ecdedca | |||
| b5a067e2f9 | |||
| ea5e432672 | |||
| f2d29f578a | |||
| 7b3572fe1b | |||
|
|
f9de76b3ac | ||
|
|
7eaf199241 | ||
|
|
12cd0632ae | ||
|
|
8fd4655ab3 | ||
|
|
182e32ac7f | ||
|
|
516e8ecb1f | ||
|
|
61f7dc444e | ||
| 9d39bbde55 | |||
| f9ea473fbd | |||
| eec2b07b25 | |||
| 2a9edcec93 | |||
| 47a5c44da4 | |||
| 2ce917999d | |||
| 31c65a8c6a | |||
| b5b98d712a | |||
| 7ef5363e05 | |||
| 1faabf627c | |||
| 0a1149b460 | |||
| 53ccbc8289 | |||
| 8c77d8de17 | |||
| 9a02a04117 | |||
| 5fb5f5b858 | |||
| b5461b9277 | |||
| bfc8cc8648 | |||
| a9a53fd13f | |||
| 0067143fcd | |||
| 92d1849049 | |||
| b1a4a4d01a | |||
| 03f6f3f5ea | |||
| 81ac9e64a2 | |||
| f58804ebfc | |||
| 0d6e9d3a3d | |||
| 6f31bf032e | |||
| f5823d28cf | |||
| 1e003cd9fd | |||
| d0de488a83 | |||
| b0a637b294 | |||
| bf045059f8 | |||
| 1cc590abed | |||
| e81913443d | |||
| 1fb1cbe320 | |||
| 24aaaee91e | |||
| 163f5b59ad | |||
| bd5f8aab5a | |||
| 921f4f7f1f | |||
| b9fa35e820 | |||
| 10d472353f | |||
| 2ccf9bd444 | |||
| 59c8228657 | |||
| 0aec72f9b1 | |||
| 22c6a8dda9 | |||
| db4951dabf | |||
| 86089e0577 |
@@ -1,17 +0,0 @@
|
|||||||
.git
|
|
||||||
.venv
|
|
||||||
.venv-tpu
|
|
||||||
**/__pycache__
|
|
||||||
**/*.pyc
|
|
||||||
**/*.pyo
|
|
||||||
**/.pytest_cache
|
|
||||||
**/.mypy_cache
|
|
||||||
**/.ruff_cache
|
|
||||||
**/.ipynb_checkpoints
|
|
||||||
wandb
|
|
||||||
build
|
|
||||||
paper/build
|
|
||||||
paper/build-cais
|
|
||||||
node_modules
|
|
||||||
**/node_modules
|
|
||||||
*.egg-info
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# Copy this file to .env.sweep and fill in values.
|
|
||||||
|
|
||||||
# Required for wandb runs and sweep agent workers.
|
|
||||||
WANDB_API_KEY=
|
|
||||||
WANDB_ENTITY=
|
|
||||||
WANDB_PROJECT=phantom-pricing
|
|
||||||
|
|
||||||
# Required for private repo bootstrap workers.
|
|
||||||
GITHUB_TOKEN=
|
|
||||||
|
|
||||||
# Optional defaults for bootstrap mode.
|
|
||||||
# REPO_URL=https://github.com/org/repo.git
|
|
||||||
# BRANCH=main
|
|
||||||
# WORKDIR=$HOME/PHANTOM-agent
|
|
||||||
# SWEEP_ID=entity/project/id
|
|
||||||
# AGENT_COUNT=0
|
|
||||||
# AGENT_LOOP=1
|
|
||||||
# RETRY_SECONDS=20
|
|
||||||
82
.gitignore
vendored
82
.gitignore
vendored
@@ -1,86 +1,22 @@
|
|||||||
# environment and secrets
|
|
||||||
**/.env
|
**/.env
|
||||||
.env.*
|
|
||||||
!.env.*.example
|
|
||||||
**/.venv
|
**/.venv
|
||||||
|
|
||||||
# python build/cache artifacts
|
|
||||||
**/__pycache__
|
**/__pycache__
|
||||||
phantom.egg-info/
|
|
||||||
*.egg-info/
|
|
||||||
|
|
||||||
# notebook artifacts
|
|
||||||
**/.ipynb_checkpoints/
|
**/.ipynb_checkpoints/
|
||||||
**/.virtual_documents/
|
**/.virtual_documents/
|
||||||
|
|
||||||
# editor/tool state
|
|
||||||
**/.pdf-view-restore
|
|
||||||
.nextstep
|
|
||||||
.ignore-gitlogue
|
|
||||||
.cloudflare
|
|
||||||
|
|
||||||
# generated svg/graphics
|
|
||||||
**/session_*.svg
|
**/session_*.svg
|
||||||
**/*graph.svg
|
**/*graph.svg
|
||||||
**/auto/*.el
|
|
||||||
|
|
||||||
# misc generated
|
|
||||||
*.old
|
|
||||||
**/package-lock.json
|
|
||||||
**/*.parquet
|
|
||||||
**/_build/
|
|
||||||
|
|
||||||
# paper build artifacts
|
|
||||||
paper/src/bib/auto
|
paper/src/bib/auto
|
||||||
paper/src/auto/*
|
|
||||||
paper/src/bib/auto
|
|
||||||
paper/template/*
|
|
||||||
paper/build-cais/
|
|
||||||
paper/src/main.pdf
|
|
||||||
paper/src/main-blx.bib
|
|
||||||
paper/src/svg-inkscape/
|
|
||||||
paper/src/mirrors/
|
|
||||||
paper/variations/
|
|
||||||
paper/src/graphics/test_*.png
|
|
||||||
thesis-latest.pdf
|
|
||||||
|
|
||||||
# experiment run artifacts and logs
|
# Airflow logs - exclude DAG run logs
|
||||||
docs/goals/*.md
|
|
||||||
PHANTOM.wiki/
|
|
||||||
experiments/airflow/logs/*
|
experiments/airflow/logs/*
|
||||||
experiments/airflow/logs/scheduler/
|
experiments/airflow/logs/scheduler/
|
||||||
experiments/airflow/logs/dag_processor_manager/
|
experiments/airflow/logs/dag_processor_manager/
|
||||||
experiments/collected_data/
|
experiments/collected_data/*
|
||||||
experiments/agents/collected_data/
|
|
||||||
tests/e2e/test-results/
|
paper/src/auto/*
|
||||||
|
lib/
|
||||||
|
docs/goals/*.md
|
||||||
|
PHANTOM.wiki/
|
||||||
tests/e2e/node_modules/**
|
tests/e2e/node_modules/**
|
||||||
|
**/auto/*.el
|
||||||
# rl/sim run outputs
|
*.old
|
||||||
sim/rl/behavior_loader/*.dot
|
|
||||||
sim/rl/behavior_loader/*.png
|
|
||||||
sim/rl/behavior_loader/*.svg
|
|
||||||
sim/rl/behavior_loader/*.pdf
|
|
||||||
sim/rl/runs/
|
|
||||||
lab/case/thesis/runs*/
|
|
||||||
sim/case/thesis_simplified/runs*/
|
|
||||||
|
|
||||||
# model binaries
|
|
||||||
engine/models/*.zip
|
|
||||||
*.zip
|
|
||||||
|
|
||||||
# wandb local state
|
|
||||||
wandb/
|
|
||||||
|
|
||||||
# data directory (large datasets)
|
|
||||||
data/
|
|
||||||
|
|
||||||
# ktem local app data
|
|
||||||
ktem_app_data/
|
|
||||||
|
|
||||||
# generated visualization pdfs
|
|
||||||
*_mdp_viz.pdf
|
|
||||||
phantom_env_comparison.png
|
|
||||||
sim/phantom_env_comparison.png
|
|
||||||
|
|
||||||
# web clone
|
|
||||||
PHANTOM_web/*
|
|
||||||
|
|||||||
127
Makefile
127
Makefile
@@ -9,45 +9,11 @@ PYTHON := $(VENV)/bin/python
|
|||||||
PIP := $(VENV)/bin/pip
|
PIP := $(VENV)/bin/pip
|
||||||
PYTEST := $(VENV)/bin/pytest
|
PYTEST := $(VENV)/bin/pytest
|
||||||
|
|
||||||
SWEEP_ENV_FILE ?= .env.sweep
|
|
||||||
|
|
||||||
WANDB_ENTITY ?=
|
|
||||||
WANDB_PROJECT ?= phantom-pricing
|
|
||||||
SWEEP_ID ?=
|
|
||||||
LOCAL_TRAIN_ARGS ?= --algo ppo --total-timesteps 50000
|
|
||||||
AGENT_COUNT ?= 0
|
|
||||||
|
|
||||||
REPO_URL ?=
|
|
||||||
BRANCH ?= main
|
|
||||||
WORKDIR ?= $(HOME)/PHANTOM-agent
|
|
||||||
AGENT_LOOP ?= 1
|
|
||||||
RETRY_SECONDS ?= 20
|
|
||||||
|
|
||||||
TRAIN_IMAGE_REF := us-central1-docker.pkg.dev/phantom-trc/phantom/phantom-trainer
|
|
||||||
TPU_NAME ?=
|
|
||||||
TPU_ZONE ?= us-central2-b
|
|
||||||
TPU_PROJECT ?= phantom-trc
|
|
||||||
TPU_REPO_DIR ?= /tmp/PHANTOM
|
|
||||||
|
|
||||||
SWEEP_ENV_LOAD = set -a; [ -f "$(SWEEP_ENV_FILE)" ] && . "$(SWEEP_ENV_FILE)" || true; set +a
|
|
||||||
|
|
||||||
.DEFAULT_GOAL := help
|
.DEFAULT_GOAL := help
|
||||||
|
|
||||||
.PHONY: help
|
.PHONY: help
|
||||||
help:
|
help:
|
||||||
@echo "pdf.build pdf.watch pdf.clean | test.backend test.e2e test.all | web.dev | install | train | train.agent | train.bootstrap | train.tpu.pod | train.tpu.vm | train.tpu.vm.sweep | stats.lines"
|
@echo "pdf.build pdf.watch pdf.clean | test.backend test.e2e test.all | web.dev | install | stats.lines"
|
||||||
@echo "docker.train.publish"
|
|
||||||
@echo ""
|
|
||||||
@echo "Local wandb run:"
|
|
||||||
@echo " make train LOCAL_TRAIN_ARGS='--algo ppo --total-timesteps 50000'"
|
|
||||||
@echo ""
|
|
||||||
@echo "Local sweep agent from this repo:"
|
|
||||||
@echo " make train.agent SWEEP_ID=entity/project/id AGENT_COUNT=5"
|
|
||||||
@echo ""
|
|
||||||
@echo "Bootstrap private repo worker from anywhere:"
|
|
||||||
@echo " make train.bootstrap REPO_URL=https://github.com/org/repo.git BRANCH=main SWEEP_ID=entity/project/id"
|
|
||||||
@echo ""
|
|
||||||
@echo "Config source: $(SWEEP_ENV_FILE) (auto-loaded)"
|
|
||||||
|
|
||||||
$(BUILDDIR):
|
$(BUILDDIR):
|
||||||
mkdir -p paper/$(BUILDDIR)
|
mkdir -p paper/$(BUILDDIR)
|
||||||
@@ -83,10 +49,8 @@ test.backend: $(VENV)
|
|||||||
test.e2e:
|
test.e2e:
|
||||||
@cd tests/e2e && npm install
|
@cd tests/e2e && npm install
|
||||||
@cd tests/e2e && npx playwright install chromium
|
@cd tests/e2e && npx playwright install chromium
|
||||||
@test -f tests/e2e/.env || cp tests/e2e/.env.example tests/e2e/.env
|
|
||||||
@timeout 30 bash -c 'until curl -sf http://localhost:5000/health > /dev/null 2>&1; do sleep 1; done' || (echo "Backend not ready" && exit 1)
|
@timeout 30 bash -c 'until curl -sf http://localhost:5000/health > /dev/null 2>&1; do sleep 1; done' || (echo "Backend not ready" && exit 1)
|
||||||
@timeout 30 bash -c 'until curl -sf http://localhost:3000 > /dev/null 2>&1; do sleep 1; done' || (echo "Web app not ready" && exit 1)
|
@timeout 30 bash -c 'until curl -sf http://localhost:3000 > /dev/null 2>&1; do sleep 1; done' || (echo "Web app not ready" && exit 1)
|
||||||
@timeout 30 bash -c 'until curl -sf http://localhost:8085/health > /dev/null 2>&1; do sleep 1; done' || (echo "Airflow not ready" && exit 1)
|
|
||||||
@cd tests/e2e && npm test
|
@cd tests/e2e && npm test
|
||||||
|
|
||||||
.PHONY: test.all
|
.PHONY: test.all
|
||||||
@@ -104,46 +68,12 @@ $(VENV):
|
|||||||
install: $(VENV)
|
install: $(VENV)
|
||||||
$(PIP) install -r requirements.txt
|
$(PIP) install -r requirements.txt
|
||||||
|
|
||||||
.PHONY: train
|
|
||||||
train: install
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$WANDB_API_KEY" || (echo "WANDB_API_KEY required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); WANDB_API_KEY="$$WANDB_API_KEY" WANDB_ENTITY="$(WANDB_ENTITY)" WANDB_PROJECT="$(WANDB_PROJECT)" \
|
|
||||||
$(PYTHON) -m engine.train $(LOCAL_TRAIN_ARGS)
|
|
||||||
|
|
||||||
.PHONY: train.agent
|
|
||||||
train.agent: install
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$WANDB_API_KEY" || (echo "WANDB_API_KEY required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
@test -n "$(SWEEP_ID)" || (echo "SWEEP_ID required, e.g. SWEEP_ID=entity/project/id" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); WANDB_API_KEY="$$WANDB_API_KEY" WANDB_ENTITY="$(WANDB_ENTITY)" WANDB_PROJECT="$(WANDB_PROJECT)" \
|
|
||||||
$(PYTHON) -m engine.train --sweep-agent --sweep-id "$(SWEEP_ID)" \
|
|
||||||
$(if $(filter-out 0,$(AGENT_COUNT)),--count $(AGENT_COUNT),)
|
|
||||||
|
|
||||||
.PHONY: train.bootstrap
|
|
||||||
train.bootstrap:
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$WANDB_API_KEY" || (echo "WANDB_API_KEY required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$GITHUB_TOKEN" || (echo "GITHUB_TOKEN required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
@test -n "$(REPO_URL)" || (echo "REPO_URL required, e.g. REPO_URL=https://github.com/org/repo.git" && exit 1)
|
|
||||||
@test -n "$(SWEEP_ID)" || (echo "SWEEP_ID required, e.g. SWEEP_ID=entity/project/id" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); \
|
|
||||||
WANDB_API_KEY="$$WANDB_API_KEY" \
|
|
||||||
WANDB_ENTITY="$(WANDB_ENTITY)" \
|
|
||||||
WANDB_PROJECT="$(WANDB_PROJECT)" \
|
|
||||||
GITHUB_TOKEN="$$GITHUB_TOKEN" \
|
|
||||||
REPO_URL="$(REPO_URL)" \
|
|
||||||
BRANCH="$(BRANCH)" \
|
|
||||||
WORKDIR="$(WORKDIR)" \
|
|
||||||
SWEEP_ID="$(SWEEP_ID)" \
|
|
||||||
AGENT_COUNT="$(AGENT_COUNT)" \
|
|
||||||
AGENT_LOOP="$(AGENT_LOOP)" \
|
|
||||||
RETRY_SECONDS="$(RETRY_SECONDS)" \
|
|
||||||
bash scripts/wandb_agent_bootstrap.sh
|
|
||||||
|
|
||||||
.PHONY: stats.lines
|
.PHONY: stats.lines
|
||||||
stats.lines:
|
stats.lines:
|
||||||
@find . \( -path '*/node_modules' -o -path '*/.venv' -o -path '*/venv' \) -prune -o \
|
@find . \( -path '*/node_modules' -o -path '*/.venv' -o -path '*/venv' \) -prune -o \
|
||||||
\( -name "*.ts" -o -name "*.py" \) -type f -print0 | xargs -0 cat | wc -l
|
\( -name "*.ts" -o -name "*.py" \) -type f -print0 | xargs -0 cat | wc -l
|
||||||
|
|
||||||
.PHONY: wordcount
|
.PHONY wordcount
|
||||||
wordcount:
|
wordcount:
|
||||||
@echo "Counting words in main text (excluding appendix)..."
|
@echo "Counting words in main text (excluding appendix)..."
|
||||||
@texcount -nosub -total -sum -1 \
|
@texcount -nosub -total -sum -1 \
|
||||||
@@ -154,59 +84,6 @@ wordcount:
|
|||||||
$(SRCDIR)/chapters/05-discussion.tex \
|
$(SRCDIR)/chapters/05-discussion.tex \
|
||||||
$(SRCDIR)/chapters/06-conclusion.tex
|
$(SRCDIR)/chapters/06-conclusion.tex
|
||||||
|
|
||||||
.PHONY: docker.train.publish
|
|
||||||
docker.train.publish:
|
|
||||||
docker build -f docker/Trainer.dockerfile --target gpu -t $(TRAIN_IMAGE_REF):gpu-latest .
|
|
||||||
docker push $(TRAIN_IMAGE_REF):gpu-latest
|
|
||||||
docker build -f docker/Trainer.dockerfile --target tpu -t $(TRAIN_IMAGE_REF):tpu-latest .
|
|
||||||
docker push $(TRAIN_IMAGE_REF):tpu-latest
|
|
||||||
|
|
||||||
.PHONY: train.tpu.pod
|
|
||||||
train.tpu.pod:
|
|
||||||
@test -n "$(TPU_NAME)" || (echo "TPU_NAME required, e.g. TPU_NAME=TPUlong" && exit 1)
|
|
||||||
@test -n "$(SWEEP_ID)" || (echo "SWEEP_ID required, e.g. SWEEP_ID=entity/project/id" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$WANDB_API_KEY" || (echo "WANDB_API_KEY required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
gcloud compute tpus tpu-vm scp scripts/tpu_pod_run.sh $(TPU_NAME):/tmp/tpu_pod_run.sh \
|
|
||||||
--zone=$(TPU_ZONE) --project=$(TPU_PROJECT) --worker=all
|
|
||||||
@$(SWEEP_ENV_LOAD); \
|
|
||||||
gcloud compute tpus tpu-vm ssh $(TPU_NAME) \
|
|
||||||
--zone=$(TPU_ZONE) --project=$(TPU_PROJECT) --worker=all \
|
|
||||||
--command="WANDB_API_KEY='$$WANDB_API_KEY' SWEEP_ID='$(SWEEP_ID)' AGENT_COUNT='$(AGENT_COUNT)' sh /tmp/tpu_pod_run.sh"
|
|
||||||
|
|
||||||
.PHONY: train.tpu.vm.prepare
|
|
||||||
train.tpu.vm.prepare:
|
|
||||||
@test -n "$(TPU_NAME)" || (echo "TPU_NAME required, e.g. TPU_NAME=TPUlong" && exit 1)
|
|
||||||
TPU_NAME="$(TPU_NAME)" TPU_ZONE="$(TPU_ZONE)" TPU_PROJECT="$(TPU_PROJECT)" \
|
|
||||||
LOCAL_REPO_DIR="$(CURDIR)" REMOTE_REPO_DIR="$(TPU_REPO_DIR)" \
|
|
||||||
sh scripts/tpu_sync_repo.sh
|
|
||||||
gcloud compute tpus tpu-vm scp scripts/tpu_vm_train.sh $(TPU_NAME):/tmp/tpu_vm_train.sh \
|
|
||||||
--zone=$(TPU_ZONE) --project=$(TPU_PROJECT) --worker=all
|
|
||||||
|
|
||||||
.PHONY: train.tpu.vm.run
|
|
||||||
train.tpu.vm.run:
|
|
||||||
@test -n "$(TPU_NAME)" || (echo "TPU_NAME required, e.g. TPU_NAME=TPUlong" && exit 1)
|
|
||||||
@test -n "$(LOCAL_TRAIN_ARGS)" || (echo "LOCAL_TRAIN_ARGS required, e.g. --algo ppo --jax --total-timesteps 200000" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); \
|
|
||||||
gcloud compute tpus tpu-vm ssh $(TPU_NAME) \
|
|
||||||
--zone=$(TPU_ZONE) --project=$(TPU_PROJECT) --worker=all \
|
|
||||||
--command="REPO_DIR='$(TPU_REPO_DIR)' TRAIN_ARGS='$(LOCAL_TRAIN_ARGS)' WANDB_API_KEY='$$WANDB_API_KEY' sh /tmp/tpu_vm_train.sh"
|
|
||||||
|
|
||||||
.PHONY: train.tpu.vm
|
|
||||||
train.tpu.vm: train.tpu.vm.prepare train.tpu.vm.run
|
|
||||||
|
|
||||||
.PHONY: train.tpu.vm.sweep
|
|
||||||
train.tpu.vm.sweep:
|
|
||||||
@test -n "$(TPU_NAME)" || (echo "TPU_NAME required, e.g. TPU_NAME=TPUlong" && exit 1)
|
|
||||||
@test -n "$(SWEEP_ID)" || (echo "SWEEP_ID required, e.g. SWEEP_ID=lusiana/phantom-pricing/abc123" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); test -n "$$WANDB_API_KEY" || (echo "WANDB_API_KEY required — set it in $(SWEEP_ENV_FILE)" && exit 1)
|
|
||||||
@$(SWEEP_ENV_LOAD); WANDB_API_KEY="$$WANDB_API_KEY" \
|
|
||||||
python3 scripts/tpu_vm_sweep_agent.py \
|
|
||||||
--sweep-id "$(SWEEP_ID)" \
|
|
||||||
--tpu-name "$(TPU_NAME)" \
|
|
||||||
--tpu-zone "$(TPU_ZONE)" \
|
|
||||||
--tpu-project "$(TPU_PROJECT)" \
|
|
||||||
--tpu-repo-dir "$(TPU_REPO_DIR)" \
|
|
||||||
$(if $(filter-out 0,$(AGENT_COUNT)),--count $(AGENT_COUNT),)
|
|
||||||
|
|
||||||
.PHONY: pdf clean watch run.webapp test count-lines all
|
.PHONY: pdf clean watch run.webapp test count-lines all
|
||||||
pdf: pdf.build
|
pdf: pdf.build
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
64 spot Cloud TPU v6e chips in zone europe-west4-a
|
|
||||||
32 spot Cloud TPU v4 chips in zone us-central2-b
|
|
||||||
64 spot Cloud TPU v5e chips in zone us-central1-a
|
|
||||||
64 spot Cloud TPU v6e chips in zone us-east1-d
|
|
||||||
32 on-demand Cloud TPU v4 chips in zone us-central2-b
|
|
||||||
64 spot Cloud TPU v5e chips in zone europe-west4-b
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# 32 spot Cloud TPU v4 chips in zone us-central2-b
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUv4s32spotUC2B
|
|
||||||
export TPU_NAME=tpu-v4-32-uc2b-spot
|
|
||||||
export ZONE=us-central2-b
|
|
||||||
export ACCELERATOR_TYPE=v4-32
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv4
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm create ${TPU_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--version=${RUNTIME_VERSION} \
|
|
||||||
--spot \
|
|
||||||
|| \
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION} \
|
|
||||||
--spot
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
# 32 on-demand Cloud TPU v4 chips in zone us-central2-b
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUlong
|
|
||||||
export ZONE=us-central2-b
|
|
||||||
export ACCELERATOR_TYPE=v4-32
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv4
|
|
||||||
#gcloud compute tpus tpu-vm create ${TPU_NAME} --zone=${ZONE} --project=${PROJECT_ID} --accelerator-type=${ACCELERATOR_TYPE} --version=${RUNTIME_VERSION}
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# 64 spot Cloud TPU v5e chips in zone europe-west4-b
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUv5e64spotEW4B
|
|
||||||
export TPU_NAME=tpu-v5e-64-ew4b
|
|
||||||
export ZONE=europe-west4-b
|
|
||||||
export ACCELERATOR_TYPE=v5e-64
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv5-lite
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm create ${TPU_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--version=${RUNTIME_VERSION} \
|
|
||||||
--spot \
|
|
||||||
|| \
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION} \
|
|
||||||
--spot
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# 64 spot Cloud TPU v5e chips in zone us-central1-a
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUv5e64spotUC1A
|
|
||||||
export TPU_NAME=tpu-v5e-64-uc1a
|
|
||||||
export ZONE=us-central1-a
|
|
||||||
export ACCELERATOR_TYPE=v5e-64
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv5-lite
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm create ${TPU_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--version=${RUNTIME_VERSION} \
|
|
||||||
--spot \
|
|
||||||
|| \
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION} \
|
|
||||||
--spot
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# 64 spot Cloud TPU v6e chips in zone europe-west4-a
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUv6e64spotEW4A
|
|
||||||
export TPU_NAME=tpu-v6e-64-ew4a
|
|
||||||
export ZONE=europe-west4-a
|
|
||||||
export ACCELERATOR_TYPE=v6e-64
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv6e
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm create ${TPU_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--version=${RUNTIME_VERSION} \
|
|
||||||
--spot \
|
|
||||||
|| \
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION} \
|
|
||||||
--spot
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
# 64 spot Cloud TPU v6e chips in zone us-east1-d
|
|
||||||
export PROJECT_ID=phantom-trc
|
|
||||||
export QR_NAME=TPUv6e64spotUE1D
|
|
||||||
export TPU_NAME=tpu-v6e-64-ue1d
|
|
||||||
export ZONE=us-east1-d
|
|
||||||
export ACCELERATOR_TYPE=v6e-64
|
|
||||||
export RUNTIME_VERSION=v2-alpha-tpuv6e
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm create ${TPU_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--version=${RUNTIME_VERSION} \
|
|
||||||
--spot \
|
|
||||||
|| \
|
|
||||||
gcloud compute tpus queued-resources create ${QR_NAME} \
|
|
||||||
--project=${PROJECT_ID} \
|
|
||||||
--zone=${ZONE} \
|
|
||||||
--node-id=${TPU_NAME} \
|
|
||||||
--accelerator-type=${ACCELERATOR_TYPE} \
|
|
||||||
--runtime-version=${RUNTIME_VERSION} \
|
|
||||||
--spot
|
|
||||||
@@ -47,52 +47,53 @@ def health() -> dict:
|
|||||||
|
|
||||||
@app.get("/api/{mode}/price/{productId}", response_model=PriceResponse)
|
@app.get("/api/{mode}/price/{productId}", response_model=PriceResponse)
|
||||||
def get_price(mode: Literal['hotel', 'airline'], productId: str, sessionId: Optional[str] = Query(None), experimentId: Optional[str] = Query(None)):
|
def get_price(mode: Literal['hotel', 'airline'], productId: str, sessionId: Optional[str] = Query(None), experimentId: Optional[str] = Query(None)):
|
||||||
"""
|
|
||||||
THIS is the fast lookup service (mechanism).
|
|
||||||
Priority: session-keyed price > global optimal price > base price
|
|
||||||
"""
|
|
||||||
product = supabase.table(f'{mode}_products').select("metadata").eq('id', productId).execute().data[0]
|
product = supabase.table(f'{mode}_products').select("metadata").eq('id', productId).execute().data[0]
|
||||||
if not product: raise HTTPException(404, f"Product {productId} not found")
|
if not product: raise HTTPException(404, f"Product {productId} not found")
|
||||||
|
|
||||||
metadata = product['metadata']
|
metadata = product['metadata']
|
||||||
base_price = metadata.get('base_price', 100.0)
|
base_price = metadata.get('base_price', 100.0)
|
||||||
|
|
||||||
# PRIORITY 1: session-aware price (computed by Airflow worker)
|
# fetch pre-computed prices from registry
|
||||||
if sessionId:
|
|
||||||
session_price = registry.get_session_price(sessionId, productId)
|
|
||||||
if session_price is not None:
|
|
||||||
return PriceResponse(
|
|
||||||
productId=productId,
|
|
||||||
price=session_price,
|
|
||||||
base_price=base_price,
|
|
||||||
markup=session_price/base_price,
|
|
||||||
elasticity=None,
|
|
||||||
model_version='session-aware'
|
|
||||||
)
|
|
||||||
|
|
||||||
# PRIORITY 2: global pre-computed prices (surge pricing)
|
|
||||||
prices_df = registry.get_prices('latest')
|
prices_df = registry.get_prices('latest')
|
||||||
if prices_df is not None:
|
elasticity_df = registry.get_elasticity('latest')
|
||||||
product_price_row = prices_df[prices_df['productId'] == productId]
|
|
||||||
if not product_price_row.empty:
|
if prices_df is None:
|
||||||
optimal_price = float(product_price_row['optimal_price'].iloc[0])
|
# fallback: no pre-computed prices available
|
||||||
return PriceResponse(
|
return PriceResponse(
|
||||||
productId=productId,
|
productId=productId,
|
||||||
price=optimal_price,
|
price=base_price,
|
||||||
base_price=base_price,
|
base_price=base_price,
|
||||||
markup=optimal_price/base_price,
|
markup=1.0,
|
||||||
elasticity=None,
|
elasticity=None
|
||||||
model_version='surge'
|
)
|
||||||
)
|
|
||||||
|
# lookup pre-computed price for this product
|
||||||
|
product_price_row = prices_df[prices_df['productId'] == productId]
|
||||||
|
if product_price_row.empty:
|
||||||
|
# product not in pre-computed prices, fallback to base
|
||||||
|
return PriceResponse(
|
||||||
|
productId=productId,
|
||||||
|
price=base_price,
|
||||||
|
base_price=base_price,
|
||||||
|
markup=1.0,
|
||||||
|
elasticity=None
|
||||||
|
)
|
||||||
|
|
||||||
|
optimal_price = float(product_price_row['optimal_price'].iloc[0]) # TODO: use optimal_price everywhere as aresult
|
||||||
|
|
||||||
|
# get elasticity if available
|
||||||
|
product_elasticity = None
|
||||||
|
if elasticity_df is not None:
|
||||||
|
product_elasticity_row = elasticity_df[elasticity_df['productId'] == productId]
|
||||||
|
if not product_elasticity_row.empty:
|
||||||
|
product_elasticity = float(product_elasticity_row['elasticity'].iloc[0])
|
||||||
|
|
||||||
# PRIORITY 3: fallback to base price
|
|
||||||
return PriceResponse(
|
return PriceResponse(
|
||||||
productId=productId,
|
productId=productId,
|
||||||
price=base_price,
|
price=optimal_price,
|
||||||
base_price=base_price,
|
base_price=base_price,
|
||||||
markup=1.0,
|
markup=optimal_price/base_price,
|
||||||
elasticity=None,
|
elasticity=product_elasticity
|
||||||
model_version='base'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.get("/models")
|
@app.get("/models")
|
||||||
|
|||||||
@@ -198,16 +198,12 @@ def dump_logs(
|
|||||||
auto_offset_reset='earliest',
|
auto_offset_reset='earliest',
|
||||||
enable_auto_commit=False,
|
enable_auto_commit=False,
|
||||||
value_deserializer=lambda x: json.loads(x.decode('utf-8')),
|
value_deserializer=lambda x: json.loads(x.decode('utf-8')),
|
||||||
consumer_timeout_ms=30000,
|
consumer_timeout_ms=5000
|
||||||
fetch_max_wait_ms=10000,
|
|
||||||
max_poll_records=1000
|
|
||||||
)
|
)
|
||||||
|
|
||||||
events = []
|
events = []
|
||||||
for msg in consumer:
|
for msg in consumer:
|
||||||
events.append(msg.value)
|
events.append(msg.value)
|
||||||
if last_n and len(events) >= last_n * 2:
|
|
||||||
break
|
|
||||||
|
|
||||||
consumer.close()
|
consumer.close()
|
||||||
|
|
||||||
|
|||||||
@@ -112,14 +112,11 @@ services:
|
|||||||
depends_on:
|
depends_on:
|
||||||
- postgres
|
- postgres
|
||||||
environment:
|
environment:
|
||||||
- AIRFLOW__CORE__EXECUTOR=LocalExecutor
|
- AIRFLOW__CORE__EXECUTOR=SequentialExecutor
|
||||||
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
||||||
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
||||||
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
||||||
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
||||||
- AIRFLOW__CORE__PARALLELISM=16
|
|
||||||
- AIRFLOW__CORE__DAG_CONCURRENCY=8
|
|
||||||
- AIRFLOW__CORE__MAX_ACTIVE_RUNS_PER_DAG=4
|
|
||||||
- _AIRFLOW_DB_MIGRATE=true
|
- _AIRFLOW_DB_MIGRATE=true
|
||||||
- _AIRFLOW_WWW_USER_CREATE=true
|
- _AIRFLOW_WWW_USER_CREATE=true
|
||||||
- _AIRFLOW_WWW_USER_USERNAME=admin
|
- _AIRFLOW_WWW_USER_USERNAME=admin
|
||||||
@@ -139,20 +136,14 @@ services:
|
|||||||
- airflow-init
|
- airflow-init
|
||||||
- redis
|
- redis
|
||||||
environment:
|
environment:
|
||||||
- AIRFLOW__CORE__EXECUTOR=LocalExecutor
|
- AIRFLOW__CORE__EXECUTOR=SequentialExecutor
|
||||||
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
||||||
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
||||||
- AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION=true
|
- AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION=true
|
||||||
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
||||||
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
||||||
- AIRFLOW__CORE__PARALLELISM=16
|
|
||||||
- AIRFLOW__CORE__DAG_CONCURRENCY=8
|
|
||||||
- AIRFLOW__CORE__MAX_ACTIVE_RUNS_PER_DAG=4
|
|
||||||
- AIRFLOW__SCHEDULER__MIN_FILE_PROCESS_INTERVAL=30
|
|
||||||
- AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL=60
|
|
||||||
- AIRFLOW__WEBSERVER__EXPOSE_CONFIG=true
|
- AIRFLOW__WEBSERVER__EXPOSE_CONFIG=true
|
||||||
- AIRFLOW__WEBSERVER__SECRET_KEY=${AIRFLOW_SECRET_KEY}
|
- AIRFLOW__WEBSERVER__SECRET_KEY=${AIRFLOW_SECRET_KEY}
|
||||||
- AIRFLOW__API__AUTH_BACKENDS=airflow.api.auth.backend.basic_auth
|
|
||||||
- KAFKA_HOST=kafka
|
- KAFKA_HOST=kafka
|
||||||
- KAFKA_PORT=29092
|
- KAFKA_PORT=29092
|
||||||
- BACKEND_URL=http://backend:5000
|
- BACKEND_URL=http://backend:5000
|
||||||
@@ -182,20 +173,13 @@ services:
|
|||||||
redis:
|
redis:
|
||||||
condition: service_started
|
condition: service_started
|
||||||
environment:
|
environment:
|
||||||
- AIRFLOW__CORE__EXECUTOR=LocalExecutor
|
- AIRFLOW__CORE__EXECUTOR=SequentialExecutor
|
||||||
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
- AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=postgresql+psycopg2://airflow:airflow@postgres/airflow
|
||||||
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
- AIRFLOW__CORE__FERNET_KEY=${AIRFLOW_FERNET_KEY}
|
||||||
- AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION=true
|
- AIRFLOW__CORE__DAGS_ARE_PAUSED_AT_CREATION=true
|
||||||
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
- AIRFLOW__CORE__LOAD_EXAMPLES=false
|
||||||
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
- AIRFLOW__CORE__ENABLE_XCOM_PICKLING=true
|
||||||
- AIRFLOW__CORE__PARALLELISM=16
|
|
||||||
- AIRFLOW__CORE__DAG_CONCURRENCY=8
|
|
||||||
- AIRFLOW__CORE__MAX_ACTIVE_RUNS_PER_DAG=4
|
|
||||||
- AIRFLOW__SCHEDULER__MIN_FILE_PROCESS_INTERVAL=30
|
|
||||||
- AIRFLOW__SCHEDULER__DAG_DIR_LIST_INTERVAL=60
|
|
||||||
- AIRFLOW__SCHEDULER__PARSING_PROCESSES=2
|
|
||||||
- AIRFLOW__WEBSERVER__SECRET_KEY=${AIRFLOW_SECRET_KEY}
|
- AIRFLOW__WEBSERVER__SECRET_KEY=${AIRFLOW_SECRET_KEY}
|
||||||
- AIRFLOW__API__AUTH_BACKENDS=airflow.api.auth.backend.basic_auth
|
|
||||||
- KAFKA_HOST=kafka
|
- KAFKA_HOST=kafka
|
||||||
- KAFKA_PORT=29092
|
- KAFKA_PORT=29092
|
||||||
- BACKEND_URL=http://backend:5000
|
- BACKEND_URL=http://backend:5000
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
# syntax=docker/dockerfile:1.7
|
|
||||||
|
|
||||||
FROM pytorch/pytorch:2.5.1-cuda12.4-cudnn9-runtime AS gpu
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY docker/trainer.requirements.txt /tmp/requirements.txt
|
|
||||||
RUN pip install --no-cache-dir -r /tmp/requirements.txt
|
|
||||||
|
|
||||||
# Optional for JAX-on-GPU workflows.
|
|
||||||
ARG INSTALL_JAX_GPU=false
|
|
||||||
RUN if [ "${INSTALL_JAX_GPU}" = "true" ]; then \
|
|
||||||
pip install --no-cache-dir "jax[cuda12]==0.4.30" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html; \
|
|
||||||
fi
|
|
||||||
|
|
||||||
COPY --chmod=755 docker/trainer-agent-entrypoint.sh /usr/local/bin/trainer-agent-entrypoint
|
|
||||||
COPY engine /app/engine
|
|
||||||
|
|
||||||
ENV PYTHONPATH=/app \
|
|
||||||
XLA_PYTHON_CLIENT_PREALLOCATE=false
|
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/local/bin/trainer-agent-entrypoint"]
|
|
||||||
|
|
||||||
|
|
||||||
FROM python:3.11-slim AS tpu
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY docker/trainer.requirements.txt /tmp/requirements.txt
|
|
||||||
RUN pip install --no-cache-dir -r /tmp/requirements.txt
|
|
||||||
|
|
||||||
RUN pip install --no-cache-dir "jax[tpu]==0.4.30" -f https://storage.googleapis.com/jax-releases/libtpu_releases.html
|
|
||||||
|
|
||||||
COPY --chmod=755 docker/trainer-agent-entrypoint.sh /usr/local/bin/trainer-agent-entrypoint
|
|
||||||
COPY engine /app/engine
|
|
||||||
|
|
||||||
ENV PYTHONPATH=/app \
|
|
||||||
PHANTOM_USE_JAX=1 \
|
|
||||||
PHANTOM_DEFAULT_AGENT_ARGS="--jax" \
|
|
||||||
XLA_PYTHON_CLIENT_PREALLOCATE=false
|
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/local/bin/trainer-agent-entrypoint"]
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
if [ -z "${SWEEP_ID:-}" ]; then
|
|
||||||
echo "SWEEP_ID is required"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
set -- python -m engine.train --sweep-agent --sweep-id "${SWEEP_ID}"
|
|
||||||
|
|
||||||
if [ -n "${PHANTOM_DEFAULT_AGENT_ARGS:-}" ]; then
|
|
||||||
set -- "$@" ${PHANTOM_DEFAULT_AGENT_ARGS}
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "${TRAIN_ARGS:-}" ]; then
|
|
||||||
set -- "$@" ${TRAIN_ARGS}
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "${AGENT_COUNT:-0}" != "0" ]; then
|
|
||||||
set -- "$@" --count "${AGENT_COUNT}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
exec "$@"
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
numpy>=1.24.0
|
|
||||||
pandas>=2.0.0
|
|
||||||
scipy>=1.11.0
|
|
||||||
gymnasium>=0.29.0
|
|
||||||
stable-baselines3>=2.2.0
|
|
||||||
tensorboard>=2.15.0
|
|
||||||
wandb>=0.17.0
|
|
||||||
tensorflow-probability==0.24.0
|
|
||||||
flax==0.10.7
|
|
||||||
optax==0.2.7
|
|
||||||
distrax==0.1.5
|
|
||||||
orbax-checkpoint==0.11.32
|
|
||||||
chex==0.1.90
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
from sys import platform
|
|
||||||
import numpy as np
|
|
||||||
from .lib.demand import generate_demand_for_actor, estimate_demand
|
|
||||||
from .lib.behavior import sample_behavior
|
|
||||||
from logging import INFO, getLogger
|
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
|
||||||
logger.setLevel(INFO)
|
|
||||||
|
|
||||||
|
|
||||||
class MarketEngine:
|
|
||||||
"""implements separate demand distributions for humans and agents per Section 3.1.1"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
alpha: float,
|
|
||||||
N: int,
|
|
||||||
human_params: tuple,
|
|
||||||
agent_params: tuple,
|
|
||||||
demand_distribution=np.random.normal,
|
|
||||||
noise_std: float = 1.0,
|
|
||||||
action_weights: dict | None = None,
|
|
||||||
):
|
|
||||||
# no defaults for D_H, D_A - force explicit experiment design
|
|
||||||
self.alpha = alpha
|
|
||||||
self.N = int(N)
|
|
||||||
self.Nagents = int(N * alpha)
|
|
||||||
self.Nhumans = int(N * (1 - alpha))
|
|
||||||
self.human_params = human_params
|
|
||||||
self.agent_params = agent_params
|
|
||||||
self.noise_std = noise_std
|
|
||||||
self.demand_dist = demand_distribution
|
|
||||||
self.action_weights = action_weights
|
|
||||||
|
|
||||||
def act(self, prices):
|
|
||||||
# generate separate demands d() per actor type
|
|
||||||
demand_h = generate_demand_for_actor(
|
|
||||||
prices,
|
|
||||||
self.human_params,
|
|
||||||
self.noise_std,
|
|
||||||
distribution_method=self.demand_dist,
|
|
||||||
)
|
|
||||||
demand_a = generate_demand_for_actor(
|
|
||||||
prices,
|
|
||||||
self.agent_params,
|
|
||||||
self.noise_std,
|
|
||||||
distribution_method=self.demand_dist,
|
|
||||||
)
|
|
||||||
# sample behavior trajectories from each demand distribution
|
|
||||||
human_t = [sample_behavior(demand_h, human=True) for _ in range(self.Nhumans)]
|
|
||||||
agent_t = [sample_behavior(demand_a, human=False) for _ in range(self.Nagents)]
|
|
||||||
# store trajectories for agent probability calculation
|
|
||||||
self.last_trajectories = human_t + agent_t
|
|
||||||
return estimate_demand(self.last_trajectories, self.action_weights)
|
|
||||||
|
|
||||||
def measure(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class PricingEngine:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
) -> None:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def act(self, demand):
|
|
||||||
return np.random.uniform(low=25, high=100, size=10)
|
|
||||||
|
|
||||||
|
|
||||||
class Limbo:
|
|
||||||
def __init__(self, platform, market) -> None:
|
|
||||||
self.platform_turn = True
|
|
||||||
self.platform = platform
|
|
||||||
self.market = market
|
|
||||||
self.output = None
|
|
||||||
|
|
||||||
def step(self):
|
|
||||||
if self.platform_turn:
|
|
||||||
self.output = self.platform.act(self.output)
|
|
||||||
else:
|
|
||||||
self.output = self.market.act(self.output)
|
|
||||||
self.platform_turn = not self.platform_turn
|
|
||||||
return self.output
|
|
||||||
|
|
||||||
def reset(self):
|
|
||||||
self.platform_turn = True
|
|
||||||
self.output = None
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
platform = PricingEngine()
|
|
||||||
market = MarketEngine(
|
|
||||||
alpha=0.3, N=100, human_params=(50, 10), agent_params=(45, 15)
|
|
||||||
)
|
|
||||||
limbo = Limbo(platform, market)
|
|
||||||
for _ in range(10):
|
|
||||||
limbo.step()
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
"""JAX-compatible training and environment modules for PHANTOM."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
try:
|
|
||||||
import jax # noqa: F401
|
|
||||||
import jax.numpy as jnp # noqa: F401
|
|
||||||
|
|
||||||
JAX_AVAILABLE = True
|
|
||||||
except ImportError:
|
|
||||||
JAX_AVAILABLE = False
|
|
||||||
|
|
||||||
__all__ = ["JAX_AVAILABLE"]
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
"""Orbax checkpoint helpers for JAX training runs."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
try:
|
|
||||||
import orbax.checkpoint as ocp
|
|
||||||
|
|
||||||
HAS_ORBAX = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_ORBAX = False
|
|
||||||
|
|
||||||
|
|
||||||
def _require_orbax() -> None:
|
|
||||||
if not HAS_ORBAX:
|
|
||||||
raise ImportError(
|
|
||||||
"orbax-checkpoint is required for checkpoint support. "
|
|
||||||
"Install engine/jax/requirements.txt first."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def create_manager(directory: str | Path, max_to_keep: int = 5):
|
|
||||||
_require_orbax()
|
|
||||||
root = Path(directory)
|
|
||||||
root.mkdir(parents=True, exist_ok=True)
|
|
||||||
options = ocp.CheckpointManagerOptions(
|
|
||||||
max_to_keep=max(1, int(max_to_keep)), create=True
|
|
||||||
)
|
|
||||||
return ocp.CheckpointManager(root.as_posix(), ocp.PyTreeCheckpointer(), options)
|
|
||||||
|
|
||||||
|
|
||||||
def save(manager, *, step: int, payload: Any) -> bool:
|
|
||||||
_require_orbax()
|
|
||||||
return bool(manager.save(int(step), payload))
|
|
||||||
|
|
||||||
|
|
||||||
def latest_step(manager) -> int | None:
|
|
||||||
_require_orbax()
|
|
||||||
return manager.latest_step()
|
|
||||||
|
|
||||||
|
|
||||||
def restore(manager, *, target: Any, step: int | None = None) -> Any:
|
|
||||||
_require_orbax()
|
|
||||||
step_to_restore = manager.latest_step() if step is None else int(step)
|
|
||||||
if step_to_restore is None:
|
|
||||||
return target
|
|
||||||
return manager.restore(step_to_restore, items=target)
|
|
||||||
@@ -1,287 +0,0 @@
|
|||||||
"""JAX-native PHANTOM environment with robust contamination step."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from typing import NamedTuple
|
|
||||||
|
|
||||||
try:
|
|
||||||
import jax
|
|
||||||
import jax.numpy as jnp
|
|
||||||
except ImportError as exc: # pragma: no cover
|
|
||||||
raise ImportError("engine.jax.env requires JAX") from exc
|
|
||||||
|
|
||||||
from .primitives import (
|
|
||||||
_sample_sessions_jax,
|
|
||||||
agent_probability_from_kl,
|
|
||||||
batch_kl,
|
|
||||||
compute_session_transitions,
|
|
||||||
load_transition_data,
|
|
||||||
purchase_flags,
|
|
||||||
reward_with_coi_penalty,
|
|
||||||
revenue_from_demand,
|
|
||||||
weighted_demand,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class EnvParams(NamedTuple):
|
|
||||||
n_products: int
|
|
||||||
n_sessions: int
|
|
||||||
max_episode_steps: int
|
|
||||||
max_session_steps: int
|
|
||||||
price_low: float
|
|
||||||
price_high: float
|
|
||||||
lambda_coi: float
|
|
||||||
info_value: float
|
|
||||||
robust_radius: float
|
|
||||||
margin_floor: float
|
|
||||||
margin_floor_patience: int
|
|
||||||
action_scales: jax.Array
|
|
||||||
alpha_nominal: float
|
|
||||||
alpha_candidates: jax.Array
|
|
||||||
human_T: jax.Array
|
|
||||||
agent_T: jax.Array
|
|
||||||
terminal_mask: jax.Array
|
|
||||||
purchase_mask: jax.Array
|
|
||||||
event_weights: jax.Array
|
|
||||||
start_idx: int
|
|
||||||
term_idx: int
|
|
||||||
|
|
||||||
|
|
||||||
class EnvState(NamedTuple):
|
|
||||||
prices: jax.Array
|
|
||||||
demand: jax.Array
|
|
||||||
step_count: jax.Array
|
|
||||||
low_margin_streak: jax.Array
|
|
||||||
last_agent_prob: jax.Array
|
|
||||||
last_alpha_adv: jax.Array
|
|
||||||
|
|
||||||
|
|
||||||
class CandidateEval(NamedTuple):
|
|
||||||
reward: jax.Array
|
|
||||||
revenue: jax.Array
|
|
||||||
demand: jax.Array
|
|
||||||
agent_prob: jax.Array
|
|
||||||
leakage: jax.Array
|
|
||||||
discount: jax.Array
|
|
||||||
n_purchases: jax.Array
|
|
||||||
n_agents: jax.Array
|
|
||||||
|
|
||||||
|
|
||||||
def make_env_params(
|
|
||||||
*,
|
|
||||||
n_products: int,
|
|
||||||
alpha: float,
|
|
||||||
n_sessions: int,
|
|
||||||
lambda_coi: float,
|
|
||||||
robust_radius: float,
|
|
||||||
robust_points: int,
|
|
||||||
info_value: float,
|
|
||||||
action_levels: int,
|
|
||||||
action_scale_low: float,
|
|
||||||
action_scale_high: float,
|
|
||||||
price_low: float,
|
|
||||||
price_high: float,
|
|
||||||
max_episode_steps: int,
|
|
||||||
max_session_steps: int = 40,
|
|
||||||
margin_floor: float = 0.05,
|
|
||||||
margin_floor_patience: int = 5,
|
|
||||||
prefer_behavior_data: bool = True,
|
|
||||||
) -> EnvParams:
|
|
||||||
transition = load_transition_data(prefer_data=prefer_behavior_data).to_jax()
|
|
||||||
if robust_radius <= 0.0 or robust_points <= 1:
|
|
||||||
alpha_candidates = jnp.asarray([float(alpha)], dtype=jnp.float32)
|
|
||||||
else:
|
|
||||||
lo = max(0.0, float(alpha) - float(robust_radius))
|
|
||||||
hi = min(1.0, float(alpha) + float(robust_radius))
|
|
||||||
alpha_candidates = jnp.linspace(lo, hi, int(robust_points), dtype=jnp.float32)
|
|
||||||
|
|
||||||
action_scales = jnp.linspace(
|
|
||||||
float(action_scale_low),
|
|
||||||
float(action_scale_high),
|
|
||||||
int(action_levels),
|
|
||||||
dtype=jnp.float32,
|
|
||||||
)
|
|
||||||
return EnvParams(
|
|
||||||
n_products=int(n_products),
|
|
||||||
n_sessions=int(n_sessions),
|
|
||||||
max_episode_steps=int(max_episode_steps),
|
|
||||||
max_session_steps=int(max_session_steps),
|
|
||||||
price_low=float(price_low),
|
|
||||||
price_high=float(price_high),
|
|
||||||
lambda_coi=float(lambda_coi),
|
|
||||||
info_value=float(info_value),
|
|
||||||
robust_radius=float(robust_radius),
|
|
||||||
margin_floor=float(margin_floor),
|
|
||||||
margin_floor_patience=int(margin_floor_patience),
|
|
||||||
action_scales=action_scales,
|
|
||||||
alpha_nominal=float(alpha),
|
|
||||||
alpha_candidates=alpha_candidates,
|
|
||||||
human_T=jnp.asarray(transition.human_T),
|
|
||||||
agent_T=jnp.asarray(transition.agent_T),
|
|
||||||
terminal_mask=jnp.asarray(transition.terminal_mask),
|
|
||||||
purchase_mask=jnp.asarray(transition.purchase_mask),
|
|
||||||
event_weights=jnp.asarray(transition.event_weights),
|
|
||||||
start_idx=int(transition.start_idx),
|
|
||||||
term_idx=int(transition.term_idx),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _flatten_obs(demand: jax.Array, prices: jax.Array) -> jax.Array:
|
|
||||||
return jnp.concatenate([demand.astype(jnp.float32), prices.astype(jnp.float32)])
|
|
||||||
|
|
||||||
|
|
||||||
def _decode_action(
|
|
||||||
prices: jax.Array, action: jax.Array, params: EnvParams
|
|
||||||
) -> jax.Array:
|
|
||||||
idx = jnp.clip(action.astype(jnp.int32), 0, params.action_scales.shape[0] - 1)
|
|
||||||
scale = params.action_scales[idx]
|
|
||||||
next_prices = prices * scale
|
|
||||||
return jnp.clip(next_prices, params.price_low, params.price_high)
|
|
||||||
|
|
||||||
|
|
||||||
def _evaluate_candidate(
|
|
||||||
key: jax.Array,
|
|
||||||
alpha_candidate: jax.Array,
|
|
||||||
prices: jax.Array,
|
|
||||||
params: EnvParams,
|
|
||||||
) -> CandidateEval:
|
|
||||||
states, products, actors, lengths = _sample_sessions_jax(
|
|
||||||
key,
|
|
||||||
params.human_T,
|
|
||||||
params.agent_T,
|
|
||||||
params.terminal_mask,
|
|
||||||
params.start_idx,
|
|
||||||
params.term_idx,
|
|
||||||
alpha_candidate,
|
|
||||||
params.n_products,
|
|
||||||
params.n_sessions,
|
|
||||||
params.max_session_steps,
|
|
||||||
int(params.human_T.shape[0]),
|
|
||||||
)
|
|
||||||
session_trans = compute_session_transitions(
|
|
||||||
states, lengths, int(params.human_T.shape[0])
|
|
||||||
)
|
|
||||||
delta_h, delta_a = batch_kl(session_trans, params.human_T, params.agent_T)
|
|
||||||
agent_probs = agent_probability_from_kl(delta_h, delta_a)
|
|
||||||
agent_prob = jnp.mean(agent_probs)
|
|
||||||
|
|
||||||
demand = weighted_demand(states, products, params.n_products, params.event_weights)
|
|
||||||
revenue = revenue_from_demand(prices, demand)
|
|
||||||
reward, leakage, discount = reward_with_coi_penalty(
|
|
||||||
revenue,
|
|
||||||
agent_prob,
|
|
||||||
params.lambda_coi,
|
|
||||||
params.info_value,
|
|
||||||
)
|
|
||||||
purchases = purchase_flags(states, params.purchase_mask)
|
|
||||||
return CandidateEval(
|
|
||||||
reward=reward,
|
|
||||||
revenue=revenue,
|
|
||||||
demand=demand,
|
|
||||||
agent_prob=agent_prob,
|
|
||||||
leakage=leakage,
|
|
||||||
discount=discount,
|
|
||||||
n_purchases=jnp.sum(purchases.astype(jnp.float32)),
|
|
||||||
n_agents=jnp.sum(actors.astype(jnp.float32)),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def reset_env(key: jax.Array, params: EnvParams) -> tuple[jax.Array, EnvState]:
|
|
||||||
prices = jax.random.uniform(
|
|
||||||
key,
|
|
||||||
shape=(params.n_products,),
|
|
||||||
minval=params.price_low,
|
|
||||||
maxval=params.price_high,
|
|
||||||
)
|
|
||||||
demand = jnp.zeros((params.n_products,), dtype=jnp.float32)
|
|
||||||
state = EnvState(
|
|
||||||
prices=prices,
|
|
||||||
demand=demand,
|
|
||||||
step_count=jnp.asarray(0, dtype=jnp.int32),
|
|
||||||
low_margin_streak=jnp.asarray(0, dtype=jnp.int32),
|
|
||||||
last_agent_prob=jnp.asarray(params.alpha_nominal, dtype=jnp.float32),
|
|
||||||
last_alpha_adv=jnp.asarray(params.alpha_nominal, dtype=jnp.float32),
|
|
||||||
)
|
|
||||||
return _flatten_obs(demand, prices), state
|
|
||||||
|
|
||||||
|
|
||||||
def step_env(
|
|
||||||
key: jax.Array,
|
|
||||||
state: EnvState,
|
|
||||||
action: jax.Array,
|
|
||||||
params: EnvParams,
|
|
||||||
) -> tuple[jax.Array, EnvState, jax.Array, jax.Array, dict[str, jax.Array]]:
|
|
||||||
prices = _decode_action(state.prices, action, params)
|
|
||||||
n_candidates = params.alpha_candidates.shape[0]
|
|
||||||
cand_keys = jax.random.split(key, n_candidates)
|
|
||||||
evals = jax.vmap(
|
|
||||||
lambda k, a: _evaluate_candidate(k, a, prices, params),
|
|
||||||
in_axes=(0, 0),
|
|
||||||
)(cand_keys, params.alpha_candidates)
|
|
||||||
idx = jnp.argmin(evals.reward)
|
|
||||||
|
|
||||||
demand = evals.demand[idx]
|
|
||||||
reward = evals.reward[idx]
|
|
||||||
revenue = evals.revenue[idx]
|
|
||||||
agent_prob = evals.agent_prob[idx]
|
|
||||||
leakage = evals.leakage[idx]
|
|
||||||
discount = evals.discount[idx]
|
|
||||||
n_purchases = evals.n_purchases[idx]
|
|
||||||
n_agents = evals.n_agents[idx]
|
|
||||||
alpha_adv = params.alpha_candidates[idx]
|
|
||||||
|
|
||||||
step_count = state.step_count + 1
|
|
||||||
avg_price = jnp.maximum(jnp.mean(prices), 1e-6)
|
|
||||||
avg_margin = (avg_price - params.price_low) / avg_price
|
|
||||||
next_streak = jnp.where(
|
|
||||||
avg_margin < params.margin_floor, state.low_margin_streak + 1, 0
|
|
||||||
)
|
|
||||||
|
|
||||||
margin_collapsed = next_streak >= params.margin_floor_patience
|
|
||||||
done = (step_count >= params.max_episode_steps) | margin_collapsed
|
|
||||||
|
|
||||||
next_state = EnvState(
|
|
||||||
prices=prices,
|
|
||||||
demand=demand,
|
|
||||||
step_count=step_count,
|
|
||||||
low_margin_streak=next_streak,
|
|
||||||
last_agent_prob=agent_prob,
|
|
||||||
last_alpha_adv=alpha_adv,
|
|
||||||
)
|
|
||||||
obs = _flatten_obs(demand, prices)
|
|
||||||
info = {
|
|
||||||
"revenue": revenue,
|
|
||||||
"agent_prob": agent_prob,
|
|
||||||
"alpha_adv": alpha_adv,
|
|
||||||
"coi_leakage": leakage,
|
|
||||||
"coi_discount": discount,
|
|
||||||
"n_purchases": n_purchases,
|
|
||||||
"n_agents": n_agents,
|
|
||||||
"avg_margin": avg_margin,
|
|
||||||
}
|
|
||||||
return obs, next_state, reward, done, info
|
|
||||||
|
|
||||||
|
|
||||||
class PHANTOMJAXEnv:
|
|
||||||
def __init__(self, params: EnvParams):
|
|
||||||
self.params = params
|
|
||||||
|
|
||||||
def reset(self, key: jax.Array, params: EnvParams | None = None):
|
|
||||||
return reset_env(key, self.params if params is None else params)
|
|
||||||
|
|
||||||
def step(
|
|
||||||
self,
|
|
||||||
key: jax.Array,
|
|
||||||
state: EnvState,
|
|
||||||
action: jax.Array,
|
|
||||||
params: EnvParams | None = None,
|
|
||||||
):
|
|
||||||
return step_env(key, state, action, self.params if params is None else params)
|
|
||||||
|
|
||||||
def action_space_n(self, params: EnvParams | None = None) -> int:
|
|
||||||
p = self.params if params is None else params
|
|
||||||
return int(p.action_scales.shape[0])
|
|
||||||
|
|
||||||
def observation_dim(self, params: EnvParams | None = None) -> int:
|
|
||||||
p = self.params if params is None else params
|
|
||||||
return int(p.n_products * 2)
|
|
||||||
@@ -1,495 +0,0 @@
|
|||||||
"""JAX-compatible primitives for PHANTOM session simulation and separability."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from functools import partial
|
|
||||||
from typing import Mapping, Sequence
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
try:
|
|
||||||
import jax
|
|
||||||
import jax.numpy as jnp
|
|
||||||
|
|
||||||
JAX_AVAILABLE = True
|
|
||||||
except ImportError:
|
|
||||||
jax = None # type: ignore[assignment]
|
|
||||||
jnp = np # type: ignore[assignment]
|
|
||||||
JAX_AVAILABLE = False
|
|
||||||
|
|
||||||
|
|
||||||
STATE_START_KEYS = ("session_start", "start")
|
|
||||||
TERMINAL_EVENT_TOKENS = (
|
|
||||||
"session_end",
|
|
||||||
"end",
|
|
||||||
"purchase_complete",
|
|
||||||
"checkout_start",
|
|
||||||
"checkout",
|
|
||||||
)
|
|
||||||
PURCHASE_EVENT_TOKENS = (
|
|
||||||
"purchase_complete",
|
|
||||||
"purchase",
|
|
||||||
"checkout_start",
|
|
||||||
"checkout",
|
|
||||||
)
|
|
||||||
|
|
||||||
CATEGORY_WEIGHTS = {"cart": 4.0, "dwell": 2.0, "nav": 1.0, "filter": 0.5}
|
|
||||||
ACTION_CATEGORIES = {
|
|
||||||
"cart": {"add_item", "add_to_cart", "remove", "checkout", "purchase"},
|
|
||||||
"dwell": {
|
|
||||||
"hover_title",
|
|
||||||
"hover_paragraph",
|
|
||||||
"hover_link",
|
|
||||||
"hover_over_title",
|
|
||||||
"hover_over_paragraph",
|
|
||||||
"hover_over_link",
|
|
||||||
"hover_over_button",
|
|
||||||
},
|
|
||||||
"nav": {
|
|
||||||
"page_view",
|
|
||||||
"view_item",
|
|
||||||
"view",
|
|
||||||
"learn_more",
|
|
||||||
"learn_more_about_item",
|
|
||||||
"view_item_page",
|
|
||||||
"session_start",
|
|
||||||
},
|
|
||||||
"filter": {
|
|
||||||
"search",
|
|
||||||
"filter_date",
|
|
||||||
"filter_price",
|
|
||||||
"sort",
|
|
||||||
"filter_for_date",
|
|
||||||
"filter_for_price",
|
|
||||||
"filter_for_amenities",
|
|
||||||
"sort_change",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
DEFAULT_ACTION_WEIGHTS = {
|
|
||||||
action: CATEGORY_WEIGHTS[group]
|
|
||||||
for group, actions in ACTION_CATEGORIES.items()
|
|
||||||
for action in actions
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class TransitionData:
|
|
||||||
"""Dense transition kernels and per-state metadata."""
|
|
||||||
|
|
||||||
human_T: np.ndarray
|
|
||||||
agent_T: np.ndarray
|
|
||||||
terminal_mask: np.ndarray
|
|
||||||
purchase_mask: np.ndarray
|
|
||||||
event_weights: np.ndarray
|
|
||||||
event_names: tuple[str, ...]
|
|
||||||
start_idx: int
|
|
||||||
term_idx: int
|
|
||||||
|
|
||||||
def to_jax(self) -> "TransitionData":
|
|
||||||
if not JAX_AVAILABLE:
|
|
||||||
return self
|
|
||||||
return TransitionData(
|
|
||||||
human_T=jnp.asarray(self.human_T),
|
|
||||||
agent_T=jnp.asarray(self.agent_T),
|
|
||||||
terminal_mask=jnp.asarray(self.terminal_mask),
|
|
||||||
purchase_mask=jnp.asarray(self.purchase_mask),
|
|
||||||
event_weights=jnp.asarray(self.event_weights),
|
|
||||||
event_names=self.event_names,
|
|
||||||
start_idx=int(self.start_idx),
|
|
||||||
term_idx=int(self.term_idx),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
|
||||||
class SessionBatch:
|
|
||||||
states: np.ndarray
|
|
||||||
products: np.ndarray
|
|
||||||
actors: np.ndarray
|
|
||||||
lengths: np.ndarray
|
|
||||||
|
|
||||||
|
|
||||||
def _event_weight(name: str) -> float:
|
|
||||||
if name in DEFAULT_ACTION_WEIGHTS:
|
|
||||||
return float(DEFAULT_ACTION_WEIGHTS[name])
|
|
||||||
if name.startswith("hover"):
|
|
||||||
return float(CATEGORY_WEIGHTS["dwell"])
|
|
||||||
if name.startswith("filter") or name in {"search", "sort", "sort_change"}:
|
|
||||||
return float(CATEGORY_WEIGHTS["filter"])
|
|
||||||
if name.startswith("add") or name in {
|
|
||||||
"checkout",
|
|
||||||
"checkout_start",
|
|
||||||
"purchase",
|
|
||||||
"remove_item",
|
|
||||||
"purchase_complete",
|
|
||||||
}:
|
|
||||||
return float(CATEGORY_WEIGHTS["cart"])
|
|
||||||
if any(token in name for token in TERMINAL_EVENT_TOKENS):
|
|
||||||
return 0.0
|
|
||||||
return float(CATEGORY_WEIGHTS["nav"])
|
|
||||||
|
|
||||||
|
|
||||||
def _is_terminal(name: str) -> bool:
|
|
||||||
return any(token in name for token in TERMINAL_EVENT_TOKENS)
|
|
||||||
|
|
||||||
|
|
||||||
def _is_purchase(name: str) -> bool:
|
|
||||||
return any(token in name for token in PURCHASE_EVENT_TOKENS)
|
|
||||||
|
|
||||||
|
|
||||||
def _collect_events(*transitions: Mapping[str, Mapping[str, float]]) -> tuple[str, ...]:
|
|
||||||
names: set[str] = set()
|
|
||||||
for trans in transitions:
|
|
||||||
for src, dsts in trans.items():
|
|
||||||
names.add(src)
|
|
||||||
names.update(dsts.keys())
|
|
||||||
names.discard("__terminal__")
|
|
||||||
return tuple(sorted(names))
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_rows(matrix: np.ndarray, term_idx: int) -> np.ndarray:
|
|
||||||
row_sums = matrix.sum(axis=1, keepdims=True)
|
|
||||||
dead_rows = np.isclose(row_sums.squeeze(-1), 0.0)
|
|
||||||
if np.any(dead_rows):
|
|
||||||
matrix[dead_rows] = 0.0
|
|
||||||
matrix[dead_rows, term_idx] = 1.0
|
|
||||||
row_sums = matrix.sum(axis=1, keepdims=True)
|
|
||||||
return matrix / np.maximum(row_sums, 1e-8)
|
|
||||||
|
|
||||||
|
|
||||||
def _dense_from_dict(
|
|
||||||
transitions: Mapping[str, Mapping[str, float]],
|
|
||||||
event_to_idx: Mapping[str, int],
|
|
||||||
term_idx: int,
|
|
||||||
) -> np.ndarray:
|
|
||||||
n_states = len(event_to_idx)
|
|
||||||
matrix = np.zeros((n_states, n_states), dtype=np.float32)
|
|
||||||
for src, dsts in transitions.items():
|
|
||||||
i = event_to_idx.get(src)
|
|
||||||
if i is None:
|
|
||||||
continue
|
|
||||||
for dst, prob in dsts.items():
|
|
||||||
j = event_to_idx.get(dst)
|
|
||||||
if j is None:
|
|
||||||
continue
|
|
||||||
matrix[i, j] += float(prob)
|
|
||||||
return _normalize_rows(matrix, term_idx)
|
|
||||||
|
|
||||||
|
|
||||||
def compile_transition_data(
|
|
||||||
human_transitions: Mapping[str, Mapping[str, float]],
|
|
||||||
agent_transitions: Mapping[str, Mapping[str, float]],
|
|
||||||
) -> TransitionData:
|
|
||||||
event_names = _collect_events(human_transitions, agent_transitions)
|
|
||||||
if not event_names:
|
|
||||||
return fallback_transition_data()
|
|
||||||
|
|
||||||
event_names = tuple([*event_names, "__terminal__"])
|
|
||||||
term_idx = len(event_names) - 1
|
|
||||||
event_to_idx = {name: i for i, name in enumerate(event_names)}
|
|
||||||
|
|
||||||
human_T = _dense_from_dict(human_transitions, event_to_idx, term_idx)
|
|
||||||
agent_T = _dense_from_dict(agent_transitions, event_to_idx, term_idx)
|
|
||||||
|
|
||||||
terminal_mask = np.array([_is_terminal(name) for name in event_names], dtype=bool)
|
|
||||||
purchase_mask = np.array([_is_purchase(name) for name in event_names], dtype=bool)
|
|
||||||
event_weights = np.array(
|
|
||||||
[_event_weight(name) for name in event_names], dtype=np.float32
|
|
||||||
)
|
|
||||||
|
|
||||||
terminal_mask[term_idx] = True
|
|
||||||
|
|
||||||
for idx, is_term in enumerate(terminal_mask):
|
|
||||||
if not is_term:
|
|
||||||
continue
|
|
||||||
human_T[idx] = 0.0
|
|
||||||
agent_T[idx] = 0.0
|
|
||||||
human_T[idx, idx] = 1.0
|
|
||||||
agent_T[idx, idx] = 1.0
|
|
||||||
|
|
||||||
start_idx = 0
|
|
||||||
for key in STATE_START_KEYS:
|
|
||||||
if key in event_to_idx:
|
|
||||||
start_idx = int(event_to_idx[key])
|
|
||||||
break
|
|
||||||
|
|
||||||
return TransitionData(
|
|
||||||
human_T=human_T,
|
|
||||||
agent_T=agent_T,
|
|
||||||
terminal_mask=terminal_mask,
|
|
||||||
purchase_mask=purchase_mask,
|
|
||||||
event_weights=event_weights,
|
|
||||||
event_names=event_names,
|
|
||||||
start_idx=start_idx,
|
|
||||||
term_idx=term_idx,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def fallback_transition_data() -> TransitionData:
|
|
||||||
human = {
|
|
||||||
"session_start": {
|
|
||||||
"page_view": 0.80,
|
|
||||||
"view_item_page": 0.15,
|
|
||||||
"session_end": 0.05,
|
|
||||||
},
|
|
||||||
"page_view": {"view_item_page": 0.55, "search": 0.25, "session_end": 0.20},
|
|
||||||
"view_item_page": {
|
|
||||||
"learn_more_about_item": 0.40,
|
|
||||||
"add_item_to_cart": 0.28,
|
|
||||||
"session_end": 0.32,
|
|
||||||
},
|
|
||||||
"learn_more_about_item": {
|
|
||||||
"add_item_to_cart": 0.50,
|
|
||||||
"view_item_page": 0.30,
|
|
||||||
"session_end": 0.20,
|
|
||||||
},
|
|
||||||
"add_item_to_cart": {
|
|
||||||
"checkout_start": 0.58,
|
|
||||||
"view_item_page": 0.24,
|
|
||||||
"session_end": 0.18,
|
|
||||||
},
|
|
||||||
"checkout_start": {"purchase_complete": 0.70, "session_end": 0.30},
|
|
||||||
"purchase_complete": {"session_end": 1.0},
|
|
||||||
}
|
|
||||||
agent = {
|
|
||||||
"session_start": {
|
|
||||||
"page_view": 0.90,
|
|
||||||
"view_item_page": 0.08,
|
|
||||||
"session_end": 0.02,
|
|
||||||
},
|
|
||||||
"page_view": {"view_item_page": 0.40, "search": 0.35, "session_end": 0.25},
|
|
||||||
"view_item_page": {
|
|
||||||
"learn_more_about_item": 0.55,
|
|
||||||
"add_item_to_cart": 0.15,
|
|
||||||
"session_end": 0.30,
|
|
||||||
},
|
|
||||||
"learn_more_about_item": {
|
|
||||||
"view_item_page": 0.45,
|
|
||||||
"add_item_to_cart": 0.20,
|
|
||||||
"session_end": 0.35,
|
|
||||||
},
|
|
||||||
"add_item_to_cart": {
|
|
||||||
"checkout_start": 0.42,
|
|
||||||
"view_item_page": 0.28,
|
|
||||||
"session_end": 0.30,
|
|
||||||
},
|
|
||||||
"checkout_start": {"purchase_complete": 0.52, "session_end": 0.48},
|
|
||||||
"purchase_complete": {"session_end": 1.0},
|
|
||||||
}
|
|
||||||
return compile_transition_data(human, agent)
|
|
||||||
|
|
||||||
|
|
||||||
def load_transition_data(prefer_data: bool = True) -> TransitionData:
|
|
||||||
if not prefer_data:
|
|
||||||
return fallback_transition_data()
|
|
||||||
try:
|
|
||||||
from ..lib.behavior import get_transition_models
|
|
||||||
|
|
||||||
human_trans, agent_trans = get_transition_models()
|
|
||||||
return compile_transition_data(human_trans, agent_trans)
|
|
||||||
except Exception:
|
|
||||||
return fallback_transition_data()
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
|
|
||||||
@partial(jax.jit, static_argnums=(8, 9, 10))
|
|
||||||
def _sample_sessions_jax(
|
|
||||||
key: jax.Array,
|
|
||||||
human_T: jax.Array,
|
|
||||||
agent_T: jax.Array,
|
|
||||||
terminal_mask: jax.Array,
|
|
||||||
start_idx: int,
|
|
||||||
term_idx: int,
|
|
||||||
alpha: float,
|
|
||||||
n_products: int,
|
|
||||||
n_sessions: int,
|
|
||||||
max_steps: int,
|
|
||||||
n_states: int,
|
|
||||||
) -> tuple[jax.Array, jax.Array, jax.Array, jax.Array]:
|
|
||||||
k_actor, k_product, k_step = jax.random.split(key, 3)
|
|
||||||
start_idx_i32 = jnp.asarray(start_idx, dtype=jnp.int32)
|
|
||||||
term_idx_i32 = jnp.asarray(term_idx, dtype=jnp.int32)
|
|
||||||
actor_draw = jax.random.uniform(k_actor, (n_sessions,))
|
|
||||||
actors = (actor_draw < alpha).astype(jnp.int32)
|
|
||||||
products = jax.random.randint(
|
|
||||||
k_product, (n_sessions,), 0, n_products, dtype=jnp.int32
|
|
||||||
)
|
|
||||||
|
|
||||||
active_init = jnp.ones((n_sessions,), dtype=jnp.bool_)
|
|
||||||
state_init = jnp.full((n_sessions,), start_idx_i32, dtype=jnp.int32)
|
|
||||||
|
|
||||||
def _scan_step(carry, _):
|
|
||||||
states, active, rng = carry
|
|
||||||
rng, k = jax.random.split(rng)
|
|
||||||
probs_h = human_T[states]
|
|
||||||
probs_a = agent_T[states]
|
|
||||||
probs = jnp.where(actors[:, None] == 0, probs_h, probs_a)
|
|
||||||
next_state = jax.random.categorical(k, jnp.log(probs + 1e-10), axis=-1)
|
|
||||||
next_state = jnp.where(active, next_state, term_idx_i32)
|
|
||||||
emitted = jnp.where(active, next_state, -1)
|
|
||||||
is_terminal = terminal_mask[jnp.clip(next_state, 0, n_states - 1)]
|
|
||||||
next_active = active & (~is_terminal)
|
|
||||||
carry_states = jnp.where(next_active, next_state, term_idx_i32)
|
|
||||||
return (carry_states, next_active, rng), emitted
|
|
||||||
|
|
||||||
_, state_t = jax.lax.scan(
|
|
||||||
_scan_step, (state_init, active_init, k_step), None, length=max_steps
|
|
||||||
)
|
|
||||||
states = state_t.T
|
|
||||||
lengths = jnp.sum(states >= 0, axis=1, dtype=jnp.int32)
|
|
||||||
return states, products, actors, lengths
|
|
||||||
|
|
||||||
|
|
||||||
def sample_sessions(
|
|
||||||
key,
|
|
||||||
transition_data: TransitionData,
|
|
||||||
alpha: float,
|
|
||||||
n_products: int,
|
|
||||||
n_sessions: int,
|
|
||||||
max_steps: int,
|
|
||||||
) -> SessionBatch:
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
td = transition_data.to_jax()
|
|
||||||
states, products, actors, lengths = _sample_sessions_jax(
|
|
||||||
key,
|
|
||||||
td.human_T,
|
|
||||||
td.agent_T,
|
|
||||||
td.terminal_mask,
|
|
||||||
int(td.start_idx),
|
|
||||||
int(td.term_idx),
|
|
||||||
float(alpha),
|
|
||||||
int(n_products),
|
|
||||||
int(n_sessions),
|
|
||||||
int(max_steps),
|
|
||||||
int(td.human_T.shape[0]),
|
|
||||||
)
|
|
||||||
return SessionBatch(
|
|
||||||
states=states, products=products, actors=actors, lengths=lengths
|
|
||||||
)
|
|
||||||
|
|
||||||
rng = np.random.default_rng(int(np.asarray(key).reshape(-1)[0]))
|
|
||||||
n_states = transition_data.human_T.shape[0]
|
|
||||||
products = rng.integers(0, n_products, size=n_sessions, dtype=np.int32)
|
|
||||||
actors = (rng.random(size=n_sessions) < alpha).astype(np.int32)
|
|
||||||
states = np.full((n_sessions, max_steps), -1, dtype=np.int32)
|
|
||||||
lengths = np.zeros((n_sessions,), dtype=np.int32)
|
|
||||||
for i in range(n_sessions):
|
|
||||||
current = int(transition_data.start_idx)
|
|
||||||
mat = transition_data.agent_T if actors[i] == 1 else transition_data.human_T
|
|
||||||
for t in range(max_steps):
|
|
||||||
nxt = int(rng.choice(n_states, p=mat[current]))
|
|
||||||
states[i, t] = nxt
|
|
||||||
if transition_data.terminal_mask[nxt]:
|
|
||||||
lengths[i] = t + 1
|
|
||||||
break
|
|
||||||
current = nxt
|
|
||||||
if lengths[i] == 0:
|
|
||||||
lengths[i] = max_steps
|
|
||||||
return SessionBatch(
|
|
||||||
states=states, products=products, actors=actors, lengths=lengths
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
|
|
||||||
@partial(jax.jit, static_argnums=(2,))
|
|
||||||
def compute_session_transitions(states, lengths, n_states: int):
|
|
||||||
src = states[:, :-1]
|
|
||||||
dst = states[:, 1:]
|
|
||||||
time_idx = jnp.arange(src.shape[1])[None, :]
|
|
||||||
valid = (src >= 0) & (dst >= 0) & (time_idx < (lengths[:, None] - 1))
|
|
||||||
src_clip = jnp.clip(src, 0, n_states - 1)
|
|
||||||
dst_clip = jnp.clip(dst, 0, n_states - 1)
|
|
||||||
src_oh = jax.nn.one_hot(src_clip, n_states)
|
|
||||||
dst_oh = jax.nn.one_hot(dst_clip, n_states)
|
|
||||||
counts = jnp.einsum(
|
|
||||||
"nti,ntj,nt->nij", src_oh, dst_oh, valid.astype(jnp.float32)
|
|
||||||
)
|
|
||||||
row_sums = jnp.sum(counts, axis=-1, keepdims=True)
|
|
||||||
return counts / (row_sums + 1e-10)
|
|
||||||
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
def compute_session_transitions(states, lengths, n_states: int):
|
|
||||||
trans = np.zeros((states.shape[0], n_states, n_states), dtype=np.float32)
|
|
||||||
for i in range(states.shape[0]):
|
|
||||||
for t in range(max(int(lengths[i]) - 1, 0)):
|
|
||||||
s = int(states[i, t])
|
|
||||||
d = int(states[i, t + 1])
|
|
||||||
if s >= 0 and d >= 0:
|
|
||||||
trans[i, s, d] += 1.0
|
|
||||||
row_sums = trans.sum(axis=-1, keepdims=True)
|
|
||||||
return trans / (row_sums + 1e-10)
|
|
||||||
|
|
||||||
|
|
||||||
def batch_kl(P, Q_human, Q_agent, eps: float = 1e-10):
|
|
||||||
p = P + eps
|
|
||||||
p = p / jnp.sum(p, axis=-1, keepdims=True)
|
|
||||||
qh = Q_human[None, ...] + eps
|
|
||||||
qa = Q_agent[None, ...] + eps
|
|
||||||
delta_h = jnp.sum(p * jnp.log(p / qh), axis=(1, 2))
|
|
||||||
delta_a = jnp.sum(p * jnp.log(p / qa), axis=(1, 2))
|
|
||||||
return delta_h, delta_a
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
batch_kl = jax.jit(batch_kl)
|
|
||||||
|
|
||||||
|
|
||||||
def agent_probability_from_kl(delta_h, delta_a, temperature: float = 1.0):
|
|
||||||
t = jnp.maximum(float(temperature), 1e-6)
|
|
||||||
exp_h = jnp.exp(-delta_h / t)
|
|
||||||
exp_a = jnp.exp(-delta_a / t)
|
|
||||||
return exp_a / (exp_h + exp_a + 1e-10)
|
|
||||||
|
|
||||||
|
|
||||||
def estimate_alpha_from_kl(delta_h, delta_a, beta: float = 2.0):
|
|
||||||
logits = beta * (delta_h - delta_a)
|
|
||||||
return 1.0 / (1.0 + jnp.exp(-logits))
|
|
||||||
|
|
||||||
|
|
||||||
def weighted_demand(states, products, n_products: int, event_weights):
|
|
||||||
valid = states >= 0
|
|
||||||
state_clip = jnp.clip(states, 0, event_weights.shape[0] - 1)
|
|
||||||
weights = event_weights[state_clip] * valid
|
|
||||||
per_session = jnp.sum(weights, axis=1)
|
|
||||||
demand = jnp.zeros((n_products,), dtype=jnp.float32)
|
|
||||||
demand = demand.at[products].add(per_session)
|
|
||||||
total = jnp.sum(demand)
|
|
||||||
return jnp.where(total > 0.0, (demand / total) * 100.0, demand)
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
weighted_demand = jax.jit(weighted_demand, static_argnums=(2,))
|
|
||||||
|
|
||||||
|
|
||||||
def purchase_flags(states, purchase_mask):
|
|
||||||
state_clip = jnp.clip(states, 0, purchase_mask.shape[0] - 1)
|
|
||||||
hits = purchase_mask[state_clip] & (states >= 0)
|
|
||||||
return jnp.any(hits, axis=1)
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
purchase_flags = jax.jit(purchase_flags)
|
|
||||||
|
|
||||||
|
|
||||||
def revenue_from_demand(prices, demand):
|
|
||||||
return jnp.dot(prices, demand)
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
revenue_from_demand = jax.jit(revenue_from_demand)
|
|
||||||
|
|
||||||
|
|
||||||
def reward_with_coi_penalty(
|
|
||||||
revenue, agent_prob: float, lambda_coi: float, info_value: float
|
|
||||||
):
|
|
||||||
leakage = agent_prob * info_value
|
|
||||||
discount = jnp.clip(1.0 - lambda_coi * leakage, 0.0, 1.0)
|
|
||||||
return revenue * discount, leakage, discount
|
|
||||||
|
|
||||||
|
|
||||||
if JAX_AVAILABLE:
|
|
||||||
reward_with_coi_penalty = jax.jit(reward_with_coi_penalty)
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
flax==0.10.7
|
|
||||||
optax==0.2.7
|
|
||||||
distrax==0.1.5
|
|
||||||
orbax-checkpoint==0.11.32
|
|
||||||
chex==0.1.90
|
|
||||||
1319
engine/jax/train.py
1319
engine/jax/train.py
File diff suppressed because it is too large
Load Diff
@@ -1,14 +0,0 @@
|
|||||||
from .demand import estimate_demand, estimate_weighted_demand, generate_demand_for_actor
|
|
||||||
from .behavior import sample_behavior, get_transition_models, trajectory_to_events
|
|
||||||
from .render import DashboardRenderer, style_axis
|
|
||||||
from .wrappers import EconomicMetricsWrapper
|
|
||||||
from .callbacks import MetricsCallback, EvalMetricsCallback, CheckpointArtifactCallback
|
|
||||||
from .providers import (
|
|
||||||
ProviderBenchmark,
|
|
||||||
ProviderResult,
|
|
||||||
BenchmarkConfig,
|
|
||||||
RandomBaseline,
|
|
||||||
SurgeBaseline,
|
|
||||||
)
|
|
||||||
from .coi import compute_uplift_coi, extract_purchases, compute_agent_probability
|
|
||||||
from .discrete import EventQTable
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
sys.path.insert(0, str(Path(__file__).parents[2]))
|
|
||||||
|
|
||||||
try:
|
|
||||||
from sim.rl.behavior_loader.models import (
|
|
||||||
BehaviorModel,
|
|
||||||
AgentBehaviorModel,
|
|
||||||
aggregate_event_transitions,
|
|
||||||
)
|
|
||||||
except ImportError:
|
|
||||||
BehaviorModel = None
|
|
||||||
AgentBehaviorModel = None
|
|
||||||
aggregate_event_transitions = None
|
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
from .demand import generate_demand_for_actor
|
|
||||||
|
|
||||||
base_dir = Path(__file__).parents[2] / "experiments"
|
|
||||||
human_dir = str(base_dir / "collected_data")
|
|
||||||
agent_dir = str(base_dir / "agents" / "collected_data")
|
|
||||||
|
|
||||||
_cache = {} # lazy cache for models and base pivots
|
|
||||||
|
|
||||||
|
|
||||||
def _get_base_pivot(human: bool):
|
|
||||||
if (
|
|
||||||
BehaviorModel is None
|
|
||||||
or AgentBehaviorModel is None
|
|
||||||
or aggregate_event_transitions is None
|
|
||||||
):
|
|
||||||
raise ImportError("behavior loader dependencies are unavailable")
|
|
||||||
key = "human" if human else "agent"
|
|
||||||
if key not in _cache:
|
|
||||||
model = BehaviorModel(human_dir) if human else AgentBehaviorModel(agent_dir)
|
|
||||||
mdp = model.build_MDP()
|
|
||||||
_cache[key] = pd.DataFrame(aggregate_event_transitions(mdp)).fillna(0.0)
|
|
||||||
return _cache[key]
|
|
||||||
|
|
||||||
|
|
||||||
def get_transition_models():
|
|
||||||
"""load human and agent transition models for agent probability calculation
|
|
||||||
|
|
||||||
returns:
|
|
||||||
tuple: (human_transitions, agent_transitions) as dicts of event->event->prob
|
|
||||||
"""
|
|
||||||
if (
|
|
||||||
BehaviorModel is None
|
|
||||||
or AgentBehaviorModel is None
|
|
||||||
or aggregate_event_transitions is None
|
|
||||||
):
|
|
||||||
raise ImportError("behavior loader dependencies are unavailable")
|
|
||||||
|
|
||||||
human_model = BehaviorModel(human_dir)
|
|
||||||
agent_model = AgentBehaviorModel(agent_dir)
|
|
||||||
|
|
||||||
human_mdp = human_model.build_MDP()
|
|
||||||
agent_mdp = agent_model.build_MDP()
|
|
||||||
|
|
||||||
human_trans = aggregate_event_transitions(human_mdp)
|
|
||||||
agent_trans = aggregate_event_transitions(agent_mdp)
|
|
||||||
|
|
||||||
return human_trans, agent_trans
|
|
||||||
|
|
||||||
|
|
||||||
def trajectory_to_events(trajectory: list) -> list:
|
|
||||||
"""extract event names from trajectory for KL divergence calculation
|
|
||||||
|
|
||||||
trajectories are in format 'eventName_product0', extract just eventName
|
|
||||||
|
|
||||||
args:
|
|
||||||
trajectory: list like ['view_product0', 'add_to_cart_product1', 'checkout_product1']
|
|
||||||
|
|
||||||
returns:
|
|
||||||
list: event names like ['view', 'add_to_cart', 'checkout']
|
|
||||||
"""
|
|
||||||
events = []
|
|
||||||
for state in trajectory:
|
|
||||||
# state format from sample_behavior: 'eventName_productX'
|
|
||||||
if "_product" in state:
|
|
||||||
event = state.rsplit("_product", 1)[0]
|
|
||||||
else:
|
|
||||||
event = state
|
|
||||||
events.append(event)
|
|
||||||
return events
|
|
||||||
|
|
||||||
|
|
||||||
def adjust_behavior_to_condition(condition, transition_matrix):
|
|
||||||
# expand NxN transition matrix to (N*P)x(N*P) weighted by demand condition
|
|
||||||
condition = np.asarray(condition, dtype=float)
|
|
||||||
condition = np.nan_to_num(condition, nan=0.0, posinf=0.0, neginf=0.0)
|
|
||||||
condition = np.clip(condition, 0.0, None)
|
|
||||||
s = float(np.sum(condition))
|
|
||||||
if not np.isfinite(s) or s <= 0:
|
|
||||||
cond_norm = np.full(len(condition), 1.0 / max(len(condition), 1), dtype=float)
|
|
||||||
else:
|
|
||||||
cond_norm = condition / s
|
|
||||||
n_products = len(condition)
|
|
||||||
base_vals = transition_matrix.values
|
|
||||||
base_cols, base_rows = (
|
|
||||||
transition_matrix.columns.tolist(),
|
|
||||||
transition_matrix.index.tolist(),
|
|
||||||
)
|
|
||||||
|
|
||||||
# expand via kronecker-like tiling: each cell becomes a P*P block weighted by outer product of cond_norm
|
|
||||||
expanded = np.kron(base_vals, np.outer(cond_norm, cond_norm))
|
|
||||||
new_cols = [f"{c}_product{p}" for c in base_cols for p in range(n_products)]
|
|
||||||
new_rows = [f"{r}_product{p}" for r in base_rows for p in range(n_products)]
|
|
||||||
return pd.DataFrame(expanded, index=new_rows, columns=new_cols)
|
|
||||||
|
|
||||||
|
|
||||||
def sample_behavior(condition, human=True, max_len=40):
|
|
||||||
base_pivot = _get_base_pivot(human)
|
|
||||||
adjusted_transitions = adjust_behavior_to_condition(condition, base_pivot)
|
|
||||||
|
|
||||||
trajectory = [np.random.choice(adjusted_transitions.index)]
|
|
||||||
while len(trajectory) < max_len and "checkout" not in trajectory[-1]:
|
|
||||||
probs = np.asarray(adjusted_transitions.loc[trajectory[-1]].values, dtype=float)
|
|
||||||
probs = np.nan_to_num(probs, nan=0.0, posinf=0.0, neginf=0.0)
|
|
||||||
probs = np.clip(probs, 0.0, None)
|
|
||||||
s = float(np.sum(probs))
|
|
||||||
sample = np.random.choice(
|
|
||||||
adjusted_transitions.columns, p=(probs / s) if s > 0 else None
|
|
||||||
)
|
|
||||||
trajectory.append(sample)
|
|
||||||
return trajectory
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
t = sample_behavior(generate_demand_for_actor(np.array([10, 20, 30])), human=True)
|
|
||||||
print(t)
|
|
||||||
t = sample_behavior(generate_demand_for_actor(np.array([10, 20, 30])), human=False)
|
|
||||||
print(t)
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
"""Training callbacks for W&B/TensorBoard logging - reads from info dict."""
|
|
||||||
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from stable_baselines3.common.callbacks import BaseCallback, EvalCallback
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from ..wandb_checkpoint import checkpoint_artifact_name, log_checkpoint_file
|
|
||||||
|
|
||||||
try:
|
|
||||||
import wandb
|
|
||||||
|
|
||||||
HAS_WANDB = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_WANDB = False
|
|
||||||
|
|
||||||
|
|
||||||
class MetricsCallback(BaseCallback):
|
|
||||||
"""Training metrics logger - reads info['economics'], logs to W&B."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, log_histograms: bool = True, log_freq: int = 100, verbose: int = 0
|
|
||||||
):
|
|
||||||
super().__init__(verbose)
|
|
||||||
self.log_histograms = log_histograms
|
|
||||||
self.log_freq = log_freq
|
|
||||||
self._episode_revenues: list[float] = []
|
|
||||||
|
|
||||||
def _on_step(self) -> bool:
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return True
|
|
||||||
|
|
||||||
for info in self.locals.get("infos", []):
|
|
||||||
if "economics" not in info:
|
|
||||||
continue
|
|
||||||
|
|
||||||
econ = info["economics"]
|
|
||||||
t = self.num_timesteps
|
|
||||||
|
|
||||||
payload = {
|
|
||||||
"economics/revenue": econ["revenue"],
|
|
||||||
"economics/margin": econ["margin"],
|
|
||||||
"coi/level": econ["coi_level"],
|
|
||||||
"economics/regret": econ["regret"],
|
|
||||||
}
|
|
||||||
if "coi_mix" in econ:
|
|
||||||
payload["coi/mix"] = econ["coi_mix"]
|
|
||||||
if "coi_base" in econ:
|
|
||||||
payload["coi/base"] = econ["coi_base"]
|
|
||||||
if "coi_leakage" in econ:
|
|
||||||
payload["coi/leakage"] = econ["coi_leakage"]
|
|
||||||
if "coi_penalty" in econ:
|
|
||||||
payload["coi/penalty"] = econ["coi_penalty"]
|
|
||||||
wandb.log(payload, step=t)
|
|
||||||
|
|
||||||
self._episode_revenues.append(econ["revenue"])
|
|
||||||
|
|
||||||
# histograms at log_freq intervals
|
|
||||||
if self.log_histograms and self.num_timesteps % self.log_freq == 0:
|
|
||||||
for info in self.locals.get("infos", []):
|
|
||||||
if "prices" in info:
|
|
||||||
wandb.log(
|
|
||||||
{"distributions/prices": wandb.Histogram(info["prices"])},
|
|
||||||
step=self.num_timesteps,
|
|
||||||
)
|
|
||||||
if "demand" in info:
|
|
||||||
wandb.log(
|
|
||||||
{"distributions/demand": wandb.Histogram(info["demand"])},
|
|
||||||
step=self.num_timesteps,
|
|
||||||
)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def _on_rollout_end(self) -> None:
|
|
||||||
if not HAS_WANDB or wandb.run is None or not self._episode_revenues:
|
|
||||||
return
|
|
||||||
wandb.log(
|
|
||||||
{
|
|
||||||
"episode/mean_revenue": np.mean(self._episode_revenues),
|
|
||||||
"episode/total_revenue": np.sum(self._episode_revenues),
|
|
||||||
},
|
|
||||||
step=self.num_timesteps,
|
|
||||||
)
|
|
||||||
self._episode_revenues = []
|
|
||||||
|
|
||||||
|
|
||||||
class CheckpointArtifactCallback(BaseCallback):
|
|
||||||
"""Periodic SB3 checkpoint uploader backed by W&B artifacts."""
|
|
||||||
|
|
||||||
def __init__(self, cfg: dict, interval: int = 10_000, verbose: int = 0):
|
|
||||||
super().__init__(verbose)
|
|
||||||
self.cfg = dict(cfg)
|
|
||||||
self.interval = max(1, int(interval))
|
|
||||||
self.model_dir = Path(str(self.cfg.get("model_dir", "engine/models")))
|
|
||||||
self.model_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
self._next_checkpoint = self.interval
|
|
||||||
self._last_saved_step = -1
|
|
||||||
|
|
||||||
def _artifact_name(self) -> str:
|
|
||||||
sweep_id = (
|
|
||||||
getattr(wandb.run, "sweep_id", None)
|
|
||||||
if HAS_WANDB and wandb.run is not None
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
return checkpoint_artifact_name(self.cfg, backend="sb3", sweep_id=sweep_id)
|
|
||||||
|
|
||||||
def _checkpoint_file(self) -> Path:
|
|
||||||
algo = str(self.cfg.get("algo", "model"))
|
|
||||||
base = self.model_dir / f"phantom_{algo}_checkpoint"
|
|
||||||
self.model.save(str(base))
|
|
||||||
return base.with_suffix(".zip")
|
|
||||||
|
|
||||||
def _save_checkpoint(self) -> None:
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return
|
|
||||||
step = int(self.num_timesteps)
|
|
||||||
if step <= self._last_saved_step:
|
|
||||||
return
|
|
||||||
checkpoint_path = self._checkpoint_file()
|
|
||||||
metadata = {
|
|
||||||
"step": step,
|
|
||||||
"algo": str(self.cfg.get("algo", "unknown")),
|
|
||||||
"sweep_id": getattr(wandb.run, "sweep_id", None),
|
|
||||||
}
|
|
||||||
saved = log_checkpoint_file(
|
|
||||||
self._artifact_name(),
|
|
||||||
file_path=checkpoint_path,
|
|
||||||
artifact_file_name=checkpoint_path.name,
|
|
||||||
metadata=metadata,
|
|
||||||
)
|
|
||||||
if saved:
|
|
||||||
self._last_saved_step = step
|
|
||||||
|
|
||||||
def _on_step(self) -> bool:
|
|
||||||
if self.num_timesteps < self._next_checkpoint:
|
|
||||||
return True
|
|
||||||
self._save_checkpoint()
|
|
||||||
while self._next_checkpoint <= self.num_timesteps:
|
|
||||||
self._next_checkpoint += self.interval
|
|
||||||
return True
|
|
||||||
|
|
||||||
def _on_training_end(self) -> None:
|
|
||||||
self._save_checkpoint()
|
|
||||||
|
|
||||||
|
|
||||||
class EvalMetricsCallback(EvalCallback):
|
|
||||||
"""Deterministic evaluation - true performance without exploration noise."""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, eval_env, eval_freq: int = 1000, n_eval_episodes: int = 5, **kwargs
|
|
||||||
):
|
|
||||||
super().__init__(
|
|
||||||
eval_env, eval_freq=eval_freq, n_eval_episodes=n_eval_episodes, **kwargs
|
|
||||||
)
|
|
||||||
self._eval_revenues: list[float] = []
|
|
||||||
|
|
||||||
def _on_step(self) -> bool:
|
|
||||||
result = super()._on_step()
|
|
||||||
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return result
|
|
||||||
|
|
||||||
# log eval metrics after evaluation runs
|
|
||||||
if self.n_calls % self.eval_freq == 0 and hasattr(self, "last_mean_reward"):
|
|
||||||
wandb.log(
|
|
||||||
{
|
|
||||||
"eval/mean_reward": self.last_mean_reward,
|
|
||||||
"eval/mean_revenue": np.mean(self._eval_revenues)
|
|
||||||
if self._eval_revenues
|
|
||||||
else 0,
|
|
||||||
},
|
|
||||||
step=self.num_timesteps,
|
|
||||||
)
|
|
||||||
self._eval_revenues = []
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
def _log_success_callback(self, locals_: dict, globals_: dict) -> None:
|
|
||||||
# called after each eval episode
|
|
||||||
info = locals_.get("info", {})
|
|
||||||
if "economics" in info:
|
|
||||||
self._eval_revenues.append(info["economics"]["revenue"])
|
|
||||||
@@ -1,76 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
from typing import Dict
|
|
||||||
|
|
||||||
|
|
||||||
def compute_agent_probability(
|
|
||||||
trajectory: list, human_transitions: Dict, agent_transitions: Dict
|
|
||||||
) -> float:
|
|
||||||
"""estimate agent probability via KL divergence between trajectory transitions and reference models
|
|
||||||
|
|
||||||
compares empirical trajectory transition distribution to human/agent prototypes
|
|
||||||
|
|
||||||
args:
|
|
||||||
trajectory: list of state/event strings from session
|
|
||||||
human_transitions: reference transition dict from human MDP (event->event->prob)
|
|
||||||
agent_transitions: reference transition dict from agent MDP (event->event->prob)
|
|
||||||
|
|
||||||
returns:
|
|
||||||
agent probability in [0, 1] via softmax over KL divergences
|
|
||||||
"""
|
|
||||||
if len(trajectory) < 2:
|
|
||||||
return 0.0 # insufficient data, assume human
|
|
||||||
|
|
||||||
# build empirical transition distribution from trajectory
|
|
||||||
trans_counts = {}
|
|
||||||
for s, s_next in zip(trajectory[:-1], trajectory[1:]):
|
|
||||||
if s not in trans_counts:
|
|
||||||
trans_counts[s] = {}
|
|
||||||
trans_counts[s][s_next] = trans_counts[s].get(s_next, 0) + 1
|
|
||||||
|
|
||||||
# normalize to probabilities
|
|
||||||
empirical = {}
|
|
||||||
for s, nxt in trans_counts.items():
|
|
||||||
total = sum(nxt.values())
|
|
||||||
empirical[s] = {s_n: cnt / total for s_n, cnt in nxt.items()}
|
|
||||||
|
|
||||||
# compute KL divergence to each prototype
|
|
||||||
def kl_div(p_dist: Dict, q_dist: Dict) -> float:
|
|
||||||
eps = 1e-10
|
|
||||||
# aggregate over all source states in empirical dist
|
|
||||||
kl = 0.0
|
|
||||||
for s in p_dist:
|
|
||||||
if s not in q_dist:
|
|
||||||
continue # skip states not in reference
|
|
||||||
p_trans, q_trans = p_dist[s], q_dist[s]
|
|
||||||
for k in p_trans:
|
|
||||||
p_val = p_trans[k] + eps
|
|
||||||
q_val = q_trans.get(k, 0.0) + eps
|
|
||||||
kl += p_val * np.log(p_val / q_val)
|
|
||||||
return kl
|
|
||||||
|
|
||||||
kl_human = kl_div(empirical, human_transitions)
|
|
||||||
kl_agent = kl_div(empirical, agent_transitions)
|
|
||||||
|
|
||||||
# convert to probability via softmax (lower KL = higher prob)
|
|
||||||
# agent_prob = exp(-kl_agent) / (exp(-kl_human) + exp(-kl_agent))
|
|
||||||
exp_h = np.exp(-kl_human)
|
|
||||||
exp_a = np.exp(-kl_agent)
|
|
||||||
return float(exp_a / (exp_h + exp_a + 1e-10))
|
|
||||||
|
|
||||||
|
|
||||||
def extract_purchases(trajectories: list) -> Dict[int, int]:
|
|
||||||
purchases: Dict[int, int] = {}
|
|
||||||
for traj in trajectories:
|
|
||||||
if traj and "checkout" in traj[-1] and "_product" in traj[-1]:
|
|
||||||
prod_id = int(traj[-1].rsplit("_product", 1)[1])
|
|
||||||
purchases[prod_id] = purchases.get(prod_id, 0) + 1
|
|
||||||
return purchases
|
|
||||||
|
|
||||||
|
|
||||||
def compute_uplift_coi(
|
|
||||||
prices: np.ndarray, purchases: Dict[int, int], baseline_prices: np.ndarray
|
|
||||||
) -> float:
|
|
||||||
# TODO: consider view-weighted fractional purchase for denser signal
|
|
||||||
return float(
|
|
||||||
sum(max(0.0, prices[k] - baseline_prices[k]) * n for k, n in purchases.items())
|
|
||||||
)
|
|
||||||
@@ -1,92 +0,0 @@
|
|||||||
import numpy as np
|
|
||||||
|
|
||||||
CATEGORY_WEIGHTS = {"cart": 4.0, "dwell": 2.0, "nav": 1.0, "filter": 0.5}
|
|
||||||
ACTION_CATEGORIES = {
|
|
||||||
"cart": {"add_item", "add_to_cart", "remove", "checkout", "purchase"},
|
|
||||||
"dwell": {"hover_title", "hover_paragraph", "hover_link"},
|
|
||||||
"nav": {"page_view", "view_item", "view", "learn_more"},
|
|
||||||
"filter": {"search", "filter_date", "filter_price", "sort"},
|
|
||||||
}
|
|
||||||
DEFAULT_ACTION_WEIGHTS = {
|
|
||||||
a: CATEGORY_WEIGHTS[c] for c, actions in ACTION_CATEGORIES.items() for a in actions
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def generate_demand_for_actor(
|
|
||||||
prices: np.ndarray,
|
|
||||||
params: tuple,
|
|
||||||
noise_std: float = 1.0,
|
|
||||||
distribution_method=np.random.normal,
|
|
||||||
) -> np.ndarray:
|
|
||||||
"""d(p;0) = max(0, valuation - price) + epsi for single actor type
|
|
||||||
params: (mean, std) for valuation distribution D_H or D_A"""
|
|
||||||
val = distribution_method(*params, size=len(prices))
|
|
||||||
noise = distribution_method(0, noise_std, len(prices))
|
|
||||||
demand = np.maximum(0, val - prices + noise)
|
|
||||||
total = np.sum(demand)
|
|
||||||
return demand / total * 100 if total > 0 else demand
|
|
||||||
|
|
||||||
|
|
||||||
def estimate_demand(trajectories, action_weights=None):
|
|
||||||
return estimate_weighted_demand(trajectories, action_weights)
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_event_state(state: str):
|
|
||||||
if "_product" not in state:
|
|
||||||
return state, None
|
|
||||||
action, raw_pid = state.rsplit("_product", 1)
|
|
||||||
return action, int(raw_pid) if raw_pid.isdigit() else None
|
|
||||||
|
|
||||||
|
|
||||||
def _weight_for_action(action: str, action_weights: dict) -> float:
|
|
||||||
if action in action_weights:
|
|
||||||
return action_weights[action]
|
|
||||||
if action.startswith("hover"):
|
|
||||||
return CATEGORY_WEIGHTS["dwell"]
|
|
||||||
if action.startswith("filter") or action in {"search", "sort"}:
|
|
||||||
return CATEGORY_WEIGHTS["filter"]
|
|
||||||
if action.startswith("add") or action in {"checkout", "purchase", "remove"}:
|
|
||||||
return CATEGORY_WEIGHTS["cart"]
|
|
||||||
return CATEGORY_WEIGHTS["nav"]
|
|
||||||
|
|
||||||
|
|
||||||
def estimate_weighted_demand(trajectories, action_weights=None):
|
|
||||||
action_weights = (
|
|
||||||
DEFAULT_ACTION_WEIGHTS if action_weights is None else action_weights
|
|
||||||
)
|
|
||||||
scores = {}
|
|
||||||
for traj in trajectories:
|
|
||||||
for state in traj:
|
|
||||||
action, product_id = _parse_event_state(state)
|
|
||||||
if product_id is None:
|
|
||||||
continue
|
|
||||||
w = _weight_for_action(action, action_weights)
|
|
||||||
if w <= 0:
|
|
||||||
continue
|
|
||||||
scores[product_id] = scores.get(product_id, 0.0) + w
|
|
||||||
total = sum(scores.values())
|
|
||||||
return (
|
|
||||||
{pid: (score / total) * 100 for pid, score in scores.items()}
|
|
||||||
if total > 0
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# Example usage
|
|
||||||
if __name__ == "__main__":
|
|
||||||
np.random.seed(42)
|
|
||||||
prices = np.array([20.0, 35.0, 50.0, 65.0])
|
|
||||||
# demo actor-specific demands
|
|
||||||
human_params, agent_params = (50, 10), (45, 15)
|
|
||||||
demand_h = generate_demand_for_actor(prices, human_params)
|
|
||||||
demand_a = generate_demand_for_actor(prices, agent_params)
|
|
||||||
print("Human Demand:", demand_h)
|
|
||||||
print("Agent Demand:", demand_a)
|
|
||||||
from .behavior import sample_behavior
|
|
||||||
|
|
||||||
N, alpha = 200, 0.3
|
|
||||||
n_h, n_a = int(N * (1 - alpha)), int(N * alpha)
|
|
||||||
human_t = [sample_behavior(demand_h, human=True) for _ in range(n_h)]
|
|
||||||
agent_t = [sample_behavior(demand_a, human=False) for _ in range(n_a)]
|
|
||||||
demand_estimate = estimate_demand(human_t + agent_t)
|
|
||||||
print("Estimated Demand from Behavior:", demand_estimate)
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
from collections import defaultdict
|
|
||||||
import gymnasium as gym
|
|
||||||
from gymnasium import spaces
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
|
||||||
class DiscretePriceActionWrapper(gym.ActionWrapper):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
env: gym.Env,
|
|
||||||
n_levels: int = 9,
|
|
||||||
min_scale: float = 0.8,
|
|
||||||
max_scale: float = 1.2,
|
|
||||||
):
|
|
||||||
super().__init__(env)
|
|
||||||
self.scales = np.linspace(min_scale, max_scale, n_levels, dtype=np.float32)
|
|
||||||
self.action_space = spaces.Discrete(n_levels)
|
|
||||||
|
|
||||||
def action(self, action: int):
|
|
||||||
scale = float(self.scales[int(action)])
|
|
||||||
cur = np.asarray(self.env.unwrapped._prices, dtype=np.float32)
|
|
||||||
lo, hi = self.env.unwrapped.price_bounds
|
|
||||||
return np.clip(cur * scale, lo, hi).astype(np.float32)
|
|
||||||
|
|
||||||
|
|
||||||
class EventQTable:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
n_actions: int,
|
|
||||||
n_products: int,
|
|
||||||
price_bounds: tuple,
|
|
||||||
lr: float = 0.1,
|
|
||||||
gamma: float = 0.99,
|
|
||||||
n_bins: int = 6,
|
|
||||||
):
|
|
||||||
self.n_actions = int(n_actions)
|
|
||||||
self.n_products = int(n_products)
|
|
||||||
self.lr = float(lr)
|
|
||||||
self.gamma = float(gamma)
|
|
||||||
self.q = defaultdict(lambda: np.zeros(self.n_actions, dtype=np.float32))
|
|
||||||
lo, hi = price_bounds
|
|
||||||
self.demand_bins = np.linspace(0.0, 100.0, n_bins + 1)[1:-1]
|
|
||||||
self.price_bins = np.linspace(lo, hi, n_bins + 1)[1:-1]
|
|
||||||
|
|
||||||
def encode(self, obs: np.ndarray) -> tuple:
|
|
||||||
obs = np.asarray(obs, dtype=np.float32)
|
|
||||||
d = obs[: self.n_products]
|
|
||||||
p = obs[self.n_products : 2 * self.n_products]
|
|
||||||
d_mean = float(np.mean(d)) if d.size else 0.0
|
|
||||||
d_std = float(np.std(d)) if d.size else 0.0
|
|
||||||
p_mean = float(np.mean(p)) if p.size else 0.0
|
|
||||||
return (
|
|
||||||
int(np.digitize(d_mean, self.demand_bins)),
|
|
||||||
int(np.digitize(d_std, self.demand_bins)),
|
|
||||||
int(np.digitize(p_mean, self.price_bins)),
|
|
||||||
)
|
|
||||||
|
|
||||||
def act(self, obs: np.ndarray, eps: float = 0.0) -> tuple[int, tuple]:
|
|
||||||
s = self.encode(obs)
|
|
||||||
if np.random.random() < eps:
|
|
||||||
return int(np.random.randint(self.n_actions)), s
|
|
||||||
return int(np.argmax(self.q[s])), s
|
|
||||||
|
|
||||||
def update(self, s: tuple, a: int, r: float, s2: tuple, done: bool):
|
|
||||||
target = r + (0.0 if done else self.gamma * float(np.max(self.q[s2])))
|
|
||||||
self.q[s][a] += self.lr * (target - self.q[s][a])
|
|
||||||
|
|
||||||
def predict(self, obs: np.ndarray, deterministic: bool = True):
|
|
||||||
a, _ = self.act(obs, 0.0 if deterministic else 0.05)
|
|
||||||
return a, None
|
|
||||||
@@ -1,182 +0,0 @@
|
|||||||
"""Provider benchmarking - compare pricing strategies across contamination levels."""
|
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Callable, Any
|
|
||||||
import numpy as np
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
try:
|
|
||||||
import wandb
|
|
||||||
|
|
||||||
HAS_WANDB = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_WANDB = False
|
|
||||||
|
|
||||||
|
|
||||||
class RandomBaseline:
|
|
||||||
"""uniform random action selection as a lower-bound baseline"""
|
|
||||||
|
|
||||||
def __init__(self, n_actions: int):
|
|
||||||
self.n = n_actions
|
|
||||||
|
|
||||||
def __call__(self, obs):
|
|
||||||
return int(np.random.randint(self.n))
|
|
||||||
|
|
||||||
def predict(self, obs, **kw):
|
|
||||||
return self(obs), None
|
|
||||||
|
|
||||||
|
|
||||||
class SurgeBaseline:
|
|
||||||
"""heuristic surge pricing: boost price when demand is above threshold, discount when below.
|
|
||||||
matches the naive pricing rule from thesis Section 3.3.2"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, n_actions: int, high_threshold: float = 60.0, low_threshold: float = 30.0
|
|
||||||
):
|
|
||||||
self.n = n_actions
|
|
||||||
self.mid = n_actions // 2 # identity action (scale ~1.0)
|
|
||||||
self.high_t = high_threshold
|
|
||||||
self.low_t = low_threshold
|
|
||||||
|
|
||||||
def __call__(self, obs):
|
|
||||||
obs = np.asarray(obs, dtype=np.float32)
|
|
||||||
n_prod = len(obs) // 2
|
|
||||||
demand_mean = float(np.mean(obs[:n_prod])) if n_prod > 0 else 0.0
|
|
||||||
if demand_mean >= self.high_t:
|
|
||||||
return min(self.mid + 2, self.n - 1) # surge: two levels above identity
|
|
||||||
if demand_mean <= self.low_t:
|
|
||||||
return max(self.mid - 2, 0) # discount: two levels below identity
|
|
||||||
return self.mid # hold
|
|
||||||
|
|
||||||
def predict(self, obs, **kw):
|
|
||||||
return self(obs), None
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ProviderResult:
|
|
||||||
"""Single benchmark result for one provider at one alpha level."""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
alpha: float
|
|
||||||
total_revenue: float
|
|
||||||
mean_revenue: float
|
|
||||||
coi_level: float
|
|
||||||
coi_preserved_pct: float # vs alpha=0 baseline
|
|
||||||
margin_integrity: float
|
|
||||||
regret: float
|
|
||||||
episodes: int
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class BenchmarkConfig:
|
|
||||||
"""Configuration for provider benchmark runs."""
|
|
||||||
|
|
||||||
n_episodes: int = 100
|
|
||||||
alpha_range: list[float] = field(default_factory=lambda: [0.0, 0.1, 0.3, 0.5])
|
|
||||||
baseline_name: str = "fixed"
|
|
||||||
|
|
||||||
|
|
||||||
class ProviderBenchmark:
|
|
||||||
"""Compare pricing providers to prove margin preservation across contamination levels.
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
def env_factory(alpha):
|
|
||||||
return EconomicMetricsWrapper(PHANTOM(alpha=alpha))
|
|
||||||
|
|
||||||
providers = {
|
|
||||||
"fixed": lambda obs: np.ones(10) * 50,
|
|
||||||
"learned": model.predict,
|
|
||||||
}
|
|
||||||
|
|
||||||
benchmark = ProviderBenchmark(env_factory, providers)
|
|
||||||
results = benchmark.run()
|
|
||||||
print(benchmark.summary_table())
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
env_factory: Callable[[float], Any],
|
|
||||||
providers: dict[str, Callable],
|
|
||||||
config: BenchmarkConfig | None = None,
|
|
||||||
):
|
|
||||||
self.env_factory = env_factory # fn(alpha) -> wrapped env
|
|
||||||
self.providers = providers # {name: fn(obs) -> action}
|
|
||||||
self.config = config or BenchmarkConfig()
|
|
||||||
self.results: list[ProviderResult] = []
|
|
||||||
|
|
||||||
def run(self) -> list[ProviderResult]:
|
|
||||||
"""Run benchmark across all providers and alpha levels."""
|
|
||||||
baseline_coi: dict[str, float] = {} # {provider: coi at alpha=0}
|
|
||||||
|
|
||||||
for alpha in self.config.alpha_range:
|
|
||||||
env = self.env_factory(alpha)
|
|
||||||
|
|
||||||
for name, policy_fn in self.providers.items():
|
|
||||||
revenues, coi_levels, margins = [], [], []
|
|
||||||
|
|
||||||
for _ in range(self.config.n_episodes):
|
|
||||||
obs, _ = env.reset()
|
|
||||||
episode_revenue = 0.0
|
|
||||||
done = False
|
|
||||||
|
|
||||||
while not done:
|
|
||||||
action = policy_fn(obs)
|
|
||||||
# handle sb3 model.predict returning tuple
|
|
||||||
if isinstance(action, tuple):
|
|
||||||
action = action[0]
|
|
||||||
obs, reward, term, trunc, info = env.step(action)
|
|
||||||
done = term or trunc
|
|
||||||
|
|
||||||
econ = info.get("economics", {})
|
|
||||||
episode_revenue += econ.get("revenue", 0)
|
|
||||||
coi_levels.append(econ.get("coi_level", 0))
|
|
||||||
margins.append(econ.get("margin", 0))
|
|
||||||
|
|
||||||
revenues.append(episode_revenue)
|
|
||||||
|
|
||||||
mean_coi = np.mean(coi_levels) if coi_levels else 0.0
|
|
||||||
if alpha == 0.0:
|
|
||||||
baseline_coi[name] = mean_coi
|
|
||||||
|
|
||||||
base = baseline_coi.get(name, mean_coi)
|
|
||||||
coi_preserved = mean_coi / base if base > 0 else 1.0
|
|
||||||
|
|
||||||
result = ProviderResult(
|
|
||||||
name=name,
|
|
||||||
alpha=alpha,
|
|
||||||
total_revenue=float(np.sum(revenues)),
|
|
||||||
mean_revenue=float(np.mean(revenues)),
|
|
||||||
coi_level=mean_coi,
|
|
||||||
coi_preserved_pct=coi_preserved * 100,
|
|
||||||
margin_integrity=float(np.mean(margins)) if margins else 0.0,
|
|
||||||
regret=0.0, # compute vs optimal if known
|
|
||||||
episodes=self.config.n_episodes,
|
|
||||||
)
|
|
||||||
self.results.append(result)
|
|
||||||
|
|
||||||
# log to wandb if available
|
|
||||||
if HAS_WANDB and wandb.run is not None:
|
|
||||||
wandb.log(
|
|
||||||
{
|
|
||||||
f"benchmark/{name}/revenue": result.mean_revenue,
|
|
||||||
f"benchmark/{name}/coi_preserved": result.coi_preserved_pct,
|
|
||||||
f"benchmark/{name}/margin": result.margin_integrity,
|
|
||||||
"benchmark/alpha": alpha,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return self.results
|
|
||||||
|
|
||||||
def to_dataframe(self) -> pd.DataFrame:
|
|
||||||
"""Convert results to pandas DataFrame."""
|
|
||||||
return pd.DataFrame([r.__dict__ for r in self.results])
|
|
||||||
|
|
||||||
def summary_table(self) -> pd.DataFrame:
|
|
||||||
"""Pivot table: providers x alpha with revenue/COI metrics."""
|
|
||||||
df = self.to_dataframe()
|
|
||||||
return df.pivot_table(
|
|
||||||
index="name",
|
|
||||||
columns="alpha",
|
|
||||||
values=["mean_revenue", "coi_preserved_pct", "margin_integrity"],
|
|
||||||
aggfunc="mean",
|
|
||||||
)
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
"""rendering logic for PHANTOM environment dashboard"""
|
|
||||||
import numpy as np
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
from matplotlib.gridspec import GridSpec
|
|
||||||
|
|
||||||
|
|
||||||
def style_axis(ax, title: str = None, xlabel: str = None, ylabel: str = None):
|
|
||||||
ax.spines['top'].set_visible(False)
|
|
||||||
ax.spines['right'].set_visible(False)
|
|
||||||
if title: ax.set_title(title, fontsize=11, fontweight='bold', pad=8)
|
|
||||||
if xlabel: ax.set_xlabel(xlabel, fontsize=9)
|
|
||||||
if ylabel: ax.set_ylabel(ylabel, fontsize=9)
|
|
||||||
|
|
||||||
|
|
||||||
class DashboardRenderer:
|
|
||||||
"""stateful renderer for PHANTOM market dynamics visualization"""
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.fig = None
|
|
||||||
self.gs = None
|
|
||||||
|
|
||||||
def render(self, env) -> None:
|
|
||||||
if self.fig is None:
|
|
||||||
plt.ion()
|
|
||||||
self.fig = plt.figure(figsize=(14, 10))
|
|
||||||
self.gs = GridSpec(3, 3, figure=self.fig, hspace=0.35, wspace=0.3,
|
|
||||||
left=0.07, right=0.95, top=0.92, bottom=0.08)
|
|
||||||
plt.show(block=False)
|
|
||||||
|
|
||||||
self.fig.clear()
|
|
||||||
self.fig.suptitle(f'PHANTOM Market Dynamics [t={env._step_count}, a={env.alpha:.2f}]',
|
|
||||||
fontsize=14, fontweight='bold')
|
|
||||||
|
|
||||||
demand_mat = np.array(env._demand_history).T
|
|
||||||
price_mat = np.array(env._price_history).T
|
|
||||||
elasticity = env._compute_elasticity()
|
|
||||||
|
|
||||||
self._render_scatter(env)
|
|
||||||
self._render_elasticity_bar(env, elasticity)
|
|
||||||
self._render_session_pie(env)
|
|
||||||
self._render_price_heatmap(price_mat)
|
|
||||||
self._render_demand_heatmap(demand_mat)
|
|
||||||
self._render_correlation(env.n_products, price_mat, demand_mat)
|
|
||||||
self._render_revenue(env)
|
|
||||||
|
|
||||||
self.fig.canvas.draw_idle()
|
|
||||||
self.fig.canvas.flush_events()
|
|
||||||
|
|
||||||
def _render_scatter(self, env):
|
|
||||||
ax = self.fig.add_subplot(self.gs[0, 0])
|
|
||||||
prices_flat = np.array(env._price_history).flatten()
|
|
||||||
demands_flat = np.array(env._demand_history).flatten()
|
|
||||||
product_ids = np.tile(np.arange(env.n_products), len(env._price_history))
|
|
||||||
ax.scatter(prices_flat, demands_flat, c=product_ids, cmap='plasma', alpha=0.6, s=15, edgecolors='none')
|
|
||||||
if len(prices_flat) > 1:
|
|
||||||
z = np.polyfit(prices_flat, demands_flat, 1)
|
|
||||||
p_line = np.linspace(prices_flat.min(), prices_flat.max(), 50)
|
|
||||||
ax.plot(p_line, np.polyval(z, p_line), '--', lw=1.5, alpha=0.8)
|
|
||||||
style_axis(ax, "Price-Demand Relationship", "Price ($)", "Demand")
|
|
||||||
|
|
||||||
def _render_elasticity_bar(self, env, elasticity):
|
|
||||||
ax = self.fig.add_subplot(self.gs[0, 1])
|
|
||||||
ax.barh(range(env.n_products), elasticity, alpha=0.8)
|
|
||||||
ax.axvline(0, lw=0.8, alpha=0.5)
|
|
||||||
ax.axvline(-1, lw=1, ls='--', alpha=0.5)
|
|
||||||
ax.set_yticks(range(env.n_products))
|
|
||||||
ax.set_yticklabels([f'P{i}' for i in range(env.n_products)], fontsize=7)
|
|
||||||
style_axis(ax, "Price Elasticity", "(dQ/dP)(P/Q)", None)
|
|
||||||
|
|
||||||
def _render_session_pie(self, env):
|
|
||||||
ax = self.fig.add_subplot(self.gs[0, 2])
|
|
||||||
n_h, n_a = env.market.Nhumans, env.market.Nagents
|
|
||||||
wedges, _ = ax.pie([n_h, n_a], startangle=90, wedgeprops={'linewidth': 2, 'edgecolor': 'white'})
|
|
||||||
ax.legend(wedges, [f'H ({n_h})', f'A ({n_a})'], loc='lower center', fontsize=8,
|
|
||||||
frameon=False, bbox_to_anchor=(0.5, -0.05))
|
|
||||||
ax.set_title("Session Mix", fontsize=11, fontweight='bold')
|
|
||||||
|
|
||||||
def _render_price_heatmap(self, price_mat):
|
|
||||||
ax = self.fig.add_subplot(self.gs[1, :2])
|
|
||||||
im = ax.imshow(price_mat, aspect='auto', cmap='viridis', origin='lower')
|
|
||||||
style_axis(ax, "Price Heatmap P(product, t)", "Step", "Product")
|
|
||||||
cbar = self.fig.colorbar(im, ax=ax, fraction=0.03, pad=0.02)
|
|
||||||
cbar.set_label('$', fontsize=8)
|
|
||||||
|
|
||||||
def _render_demand_heatmap(self, demand_mat):
|
|
||||||
ax = self.fig.add_subplot(self.gs[1, 2])
|
|
||||||
im = ax.imshow(demand_mat, aspect='auto', cmap='Blues', origin='lower')
|
|
||||||
style_axis(ax, "Demand Q(product, t)", "Step", None)
|
|
||||||
self.fig.colorbar(im, ax=ax, fraction=0.046, pad=0.02)
|
|
||||||
|
|
||||||
def _render_correlation(self, n_products, price_mat, demand_mat):
|
|
||||||
ax = self.fig.add_subplot(self.gs[2, 0])
|
|
||||||
if price_mat.shape[1] > 2:
|
|
||||||
corr = np.corrcoef(price_mat, demand_mat)[:n_products, n_products:]
|
|
||||||
im = ax.imshow(corr, cmap='RdBu', vmin=-1, vmax=1, aspect='auto')
|
|
||||||
ax.set_xticks(range(n_products))
|
|
||||||
ax.set_yticks(range(n_products))
|
|
||||||
ax.set_xticklabels([f'Q{i}' for i in range(n_products)], fontsize=6)
|
|
||||||
ax.set_yticklabels([f'P{i}' for i in range(n_products)], fontsize=6)
|
|
||||||
self.fig.colorbar(im, ax=ax, fraction=0.046, pad=0.02)
|
|
||||||
style_axis(ax, "Price-Demand Correlation", None, None)
|
|
||||||
|
|
||||||
def _render_revenue(self, env):
|
|
||||||
ax = self.fig.add_subplot(self.gs[2, 1:])
|
|
||||||
n_steps = len(env._revenue_history)
|
|
||||||
demand_std = [np.std(d) for d in env._demand_history]
|
|
||||||
ax.fill_between(range(n_steps), env._revenue_history, alpha=0.3)
|
|
||||||
ax.plot(env._revenue_history, linewidth=2, label='Revenue')
|
|
||||||
ax.set_xlim(0, max(n_steps, 1))
|
|
||||||
ax.set_ylim(0, max(env._revenue_history) * 1.1 if env._revenue_history else 1)
|
|
||||||
|
|
||||||
ax2 = ax.twinx()
|
|
||||||
ax2.plot(range(n_steps), demand_std, linewidth=2, ls='-', alpha=0.9, label='sigma(Demand)')
|
|
||||||
d_min, d_max = min(demand_std), max(demand_std)
|
|
||||||
margin = (d_max - d_min) * 0.2 if d_max > d_min else 0.5
|
|
||||||
ax2.set_ylim(max(0, d_min - margin), d_max + margin)
|
|
||||||
ax2.set_ylabel('Demand sigma', fontsize=9)
|
|
||||||
|
|
||||||
style_axis(ax, "Revenue & Demand Dispersion", "Step", "Revenue ($)")
|
|
||||||
ax.legend(loc='upper left', fontsize=7, frameon=False)
|
|
||||||
ax2.legend(loc='upper right', fontsize=7, frameon=False)
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
if self.fig:
|
|
||||||
plt.close(self.fig)
|
|
||||||
self.fig = None
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
"""Economic metrics wrapper - calculates thesis-aligned KPIs and injects into info dict."""
|
|
||||||
|
|
||||||
import gymnasium as gym
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
|
||||||
class EconomicMetricsWrapper(gym.Wrapper):
|
|
||||||
"""Calculates thesis-aligned economic metrics per step, injects into info.
|
|
||||||
|
|
||||||
Metrics follow thesis definitions:
|
|
||||||
- COI level: E[P] - p_min (Definition 1)
|
|
||||||
- Margin: (avg_price - p_min) / avg_price
|
|
||||||
- Regret: 1 - (revenue / baseline_revenue)
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, env: gym.Env, p_min: float = 10.0, baseline_revenue: float | None = None
|
|
||||||
):
|
|
||||||
super().__init__(env)
|
|
||||||
self.p_min = p_min
|
|
||||||
self.baseline_revenue = baseline_revenue
|
|
||||||
self._price_history: list[np.ndarray] = []
|
|
||||||
self._revenue_history: list[float] = []
|
|
||||||
|
|
||||||
def reset(self, **kwargs):
|
|
||||||
obs, info = self.env.reset(**kwargs)
|
|
||||||
self._price_history = []
|
|
||||||
self._revenue_history = []
|
|
||||||
return obs, info
|
|
||||||
|
|
||||||
def step(self, action):
|
|
||||||
obs, reward, terminated, truncated, info = self.env.step(action)
|
|
||||||
|
|
||||||
# extract from unwrapped env
|
|
||||||
prices = self.env.unwrapped._prices
|
|
||||||
demand_dict = self.env.unwrapped._demand
|
|
||||||
demand = np.array([demand_dict.get(i, 0.0) for i in range(len(prices))])
|
|
||||||
alpha = self.env.unwrapped.alpha
|
|
||||||
|
|
||||||
# core calculations
|
|
||||||
revenue = float(np.sum(prices * demand))
|
|
||||||
avg_price = float(np.mean(prices))
|
|
||||||
margin = (avg_price - self.p_min) / max(avg_price, 1e-6)
|
|
||||||
coi_level = avg_price - self.p_min # E[P] - p_min per thesis Def 1
|
|
||||||
|
|
||||||
self._price_history.append(prices.copy())
|
|
||||||
self._revenue_history.append(revenue)
|
|
||||||
|
|
||||||
# regret vs baseline (golden path)
|
|
||||||
regret = 0.0
|
|
||||||
if self.baseline_revenue and self.baseline_revenue > 0:
|
|
||||||
regret = 1.0 - (revenue / self.baseline_revenue)
|
|
||||||
|
|
||||||
# inject structured metrics into info
|
|
||||||
info["economics"] = {
|
|
||||||
"revenue": revenue,
|
|
||||||
"margin": margin,
|
|
||||||
"coi_level": coi_level,
|
|
||||||
"regret": regret,
|
|
||||||
}
|
|
||||||
for key in ("coi_mix", "coi_base", "coi_leakage", "coi_penalty"):
|
|
||||||
if key in info:
|
|
||||||
info["economics"][key] = info[key]
|
|
||||||
info["prices"] = prices.copy()
|
|
||||||
info["demand"] = demand.copy()
|
|
||||||
|
|
||||||
return obs, reward, terminated, truncated, info
|
|
||||||
|
|
||||||
@property
|
|
||||||
def episode_revenue(self) -> float:
|
|
||||||
return sum(self._revenue_history)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def episode_mean_price(self) -> float:
|
|
||||||
if not self._price_history:
|
|
||||||
return 0.0
|
|
||||||
return float(np.mean([np.mean(p) for p in self._price_history]))
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
"""shared factor definitions for experimental designs"""
|
|
||||||
import numpy as np
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Callable, Any
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Factor:
|
|
||||||
name: str
|
|
||||||
levels: list
|
|
||||||
primary: bool = True # full cross vs sampled
|
|
||||||
|
|
||||||
# demand functions with compatible signatures
|
|
||||||
def demand_linear(mu, sigma, size): return np.maximum(0, np.random.normal(mu, sigma, size))
|
|
||||||
def demand_uniform(mu, sigma, size): return np.random.uniform(mu - sigma, mu + sigma, size)
|
|
||||||
def demand_exponential(mu, sigma, size): return np.random.exponential(mu, size)
|
|
||||||
def demand_logistic(mu, sigma, size): return np.random.logistic(mu, sigma, size)
|
|
||||||
|
|
||||||
DEMAND_FUNCTIONS = {
|
|
||||||
"linear": demand_linear,
|
|
||||||
"uniform": demand_uniform,
|
|
||||||
"exponential": demand_exponential,
|
|
||||||
"logistic": demand_logistic,
|
|
||||||
}
|
|
||||||
|
|
||||||
FACTORS = [
|
|
||||||
Factor("demand_fn", list(DEMAND_FUNCTIONS.keys()), primary=True),
|
|
||||||
Factor("alpha", [0.1, 0.3, 0.5, 0.7], primary=True),
|
|
||||||
Factor("n_products", [5, 15, 30, 50], primary=True),
|
|
||||||
Factor("demand_mu", [30.0, 50.0, 70.0], primary=False),
|
|
||||||
Factor("demand_sigma", [5.0, 10.0, 20.0], primary=False),
|
|
||||||
Factor("N", [100, 500, 1000], primary=False),
|
|
||||||
]
|
|
||||||
|
|
||||||
SEEDS_PER_CONFIG = 5
|
|
||||||
@@ -1,89 +0,0 @@
|
|||||||
"""full factorial design - all factor combinations"""
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "..")
|
|
||||||
import logging
|
|
||||||
from itertools import product
|
|
||||||
import json
|
|
||||||
import hashlib
|
|
||||||
from pathlib import Path
|
|
||||||
from concurrent.futures import ProcessPoolExecutor
|
|
||||||
from .factors import FACTORS, DEMAND_FUNCTIONS, SEEDS_PER_CONFIG
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
def generate_configs():
|
|
||||||
"""generate all factor combinations with seeds"""
|
|
||||||
all_levels = [f.levels for f in FACTORS]
|
|
||||||
names = [f.name for f in FACTORS]
|
|
||||||
|
|
||||||
configs = []
|
|
||||||
for combo in product(*all_levels):
|
|
||||||
base = {names[i]: combo[i] for i in range(len(names))}
|
|
||||||
for seed in range(SEEDS_PER_CONFIG):
|
|
||||||
cfg = {**base, "seed": seed}
|
|
||||||
cfg["id"] = hashlib.md5(json.dumps(cfg, sort_keys=True).encode()).hexdigest()[:8]
|
|
||||||
configs.append(cfg)
|
|
||||||
return configs
|
|
||||||
|
|
||||||
def run_single(cfg: dict) -> dict:
|
|
||||||
"""execute one experiment config, return metrics"""
|
|
||||||
from engine.wrapper import PHANTOM
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
np.random.seed(cfg["seed"])
|
|
||||||
demand_fn = DEMAND_FUNCTIONS[cfg["demand_fn"]]
|
|
||||||
|
|
||||||
env = PHANTOM(
|
|
||||||
n_products=cfg["n_products"],
|
|
||||||
alpha=cfg["alpha"],
|
|
||||||
N=cfg["N"],
|
|
||||||
)
|
|
||||||
env.market.demand = (demand_fn, (cfg["demand_mu"], cfg["demand_sigma"]))
|
|
||||||
|
|
||||||
obs, _ = env.reset()
|
|
||||||
total_reward, steps = 0.0, 0
|
|
||||||
|
|
||||||
for _ in range(100):
|
|
||||||
action = env.action_space.sample()
|
|
||||||
obs, reward, term, trunc, _ = env.step(action)
|
|
||||||
total_reward += reward
|
|
||||||
steps += 1
|
|
||||||
if term: break
|
|
||||||
|
|
||||||
env.close()
|
|
||||||
return {
|
|
||||||
"id": cfg["id"],
|
|
||||||
"config": cfg,
|
|
||||||
"total_reward": total_reward,
|
|
||||||
"avg_reward": total_reward / steps if steps > 0 else 0.0,
|
|
||||||
"steps": steps,
|
|
||||||
}
|
|
||||||
|
|
||||||
def run_study(max_workers: int = None, output: str = "results_full.jsonl"):
|
|
||||||
configs = generate_configs()
|
|
||||||
log.info(f"full factorial: {len(configs)} configs ({len(configs)//SEEDS_PER_CONFIG} unique × {SEEDS_PER_CONFIG} seeds)")
|
|
||||||
|
|
||||||
results = []
|
|
||||||
with ProcessPoolExecutor(max_workers=max_workers) as ex:
|
|
||||||
for i, result in enumerate(ex.map(run_single, configs)):
|
|
||||||
results.append(result)
|
|
||||||
if (i+1) % 100 == 0: log.info(f"progress: {i+1}/{len(configs)}")
|
|
||||||
|
|
||||||
Path(output).write_text("\n".join(json.dumps(r) for r in results))
|
|
||||||
log.info(f"wrote {len(results)} results to {output}")
|
|
||||||
return results
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import argparse
|
|
||||||
p = argparse.ArgumentParser()
|
|
||||||
p.add_argument("--workers", type=int, default=None)
|
|
||||||
p.add_argument("--output", default="results_full.jsonl")
|
|
||||||
p.add_argument("--dry-run", action="store_true", help="only show design size")
|
|
||||||
args = p.parse_args()
|
|
||||||
|
|
||||||
configs = generate_configs()
|
|
||||||
log.info(f"design: {len(configs)} runs | factors: {[f.name for f in FACTORS]} | levels: {[len(f.levels) for f in FACTORS]}")
|
|
||||||
|
|
||||||
if not args.dry_run:
|
|
||||||
run_study(args.workers, args.output)
|
|
||||||
@@ -1,106 +0,0 @@
|
|||||||
"""mixed design: full factorial on primary factors, latin hypercube on secondary"""
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "..")
|
|
||||||
import logging
|
|
||||||
from itertools import product
|
|
||||||
import json
|
|
||||||
import hashlib
|
|
||||||
from pathlib import Path
|
|
||||||
from concurrent.futures import ProcessPoolExecutor
|
|
||||||
import numpy as np
|
|
||||||
from scipy.stats.qmc import LatinHypercube
|
|
||||||
from factors import FACTORS, DEMAND_FUNCTIONS, SEEDS_PER_CONFIG
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
LH_SAMPLES = 10
|
|
||||||
|
|
||||||
def generate_configs(lh_samples: int = LH_SAMPLES):
|
|
||||||
primary = [f for f in FACTORS if f.primary]
|
|
||||||
secondary = [f for f in FACTORS if not f.primary]
|
|
||||||
|
|
||||||
primary_grid = list(product(*[f.levels for f in primary]))
|
|
||||||
lhs = LatinHypercube(d=len(secondary), seed=42)
|
|
||||||
|
|
||||||
configs = []
|
|
||||||
for p_combo in primary_grid:
|
|
||||||
samples = lhs.random(n=lh_samples)
|
|
||||||
for s in samples:
|
|
||||||
sec_vals = {
|
|
||||||
secondary[i].name: secondary[i].levels[int(s[i] * len(secondary[i].levels))]
|
|
||||||
for i in range(len(secondary))
|
|
||||||
}
|
|
||||||
base = {primary[i].name: p_combo[i] for i in range(len(primary))}
|
|
||||||
base.update(sec_vals)
|
|
||||||
|
|
||||||
for seed in range(SEEDS_PER_CONFIG):
|
|
||||||
cfg = {**base, "seed": seed}
|
|
||||||
cfg["id"] = hashlib.md5(json.dumps(cfg, sort_keys=True).encode()).hexdigest()[:8]
|
|
||||||
configs.append(cfg)
|
|
||||||
return configs
|
|
||||||
|
|
||||||
def run_single(cfg: dict) -> dict:
|
|
||||||
from engine.wrapper import PHANTOM
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
np.random.seed(cfg["seed"])
|
|
||||||
demand_fn = DEMAND_FUNCTIONS[cfg["demand_fn"]]
|
|
||||||
|
|
||||||
env = PHANTOM(
|
|
||||||
n_products=cfg["n_products"],
|
|
||||||
alpha=cfg["alpha"],
|
|
||||||
N=cfg["N"],
|
|
||||||
)
|
|
||||||
env.market.demand = (demand_fn, (cfg["demand_mu"], cfg["demand_sigma"]))
|
|
||||||
|
|
||||||
obs, _ = env.reset()
|
|
||||||
total_reward, steps = 0.0, 0
|
|
||||||
|
|
||||||
for _ in range(100):
|
|
||||||
action = env.action_space.sample()
|
|
||||||
obs, reward, term, trunc, _ = env.step(action)
|
|
||||||
total_reward += reward
|
|
||||||
steps += 1
|
|
||||||
if term: break
|
|
||||||
|
|
||||||
env.close()
|
|
||||||
return {
|
|
||||||
"id": cfg["id"],
|
|
||||||
"config": cfg,
|
|
||||||
"total_reward": total_reward,
|
|
||||||
"avg_reward": total_reward / steps,
|
|
||||||
"steps": steps,
|
|
||||||
}
|
|
||||||
|
|
||||||
def run_study(max_workers: int = None, output: str = "results_mixed.jsonl", lh_samples: int = LH_SAMPLES):
|
|
||||||
configs = generate_configs(lh_samples)
|
|
||||||
n_primary_cells = int(np.prod([len(f.levels) for f in FACTORS if f.primary]))
|
|
||||||
log.info(f"mixed LH: {len(configs)} configs ({n_primary_cells} primary × {lh_samples} LH × {SEEDS_PER_CONFIG} seeds)")
|
|
||||||
|
|
||||||
results = []
|
|
||||||
with ProcessPoolExecutor(max_workers=max_workers) as ex:
|
|
||||||
for i, result in enumerate(ex.map(run_single, configs)):
|
|
||||||
results.append(result)
|
|
||||||
if (i+1) % 100 == 0: log.info(f"progress: {i+1}/{len(configs)}")
|
|
||||||
|
|
||||||
Path(output).write_text("\n".join(json.dumps(r) for r in results))
|
|
||||||
log.info(f"wrote {len(results)} results to {output}")
|
|
||||||
return results
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import argparse
|
|
||||||
p = argparse.ArgumentParser()
|
|
||||||
p.add_argument("--workers", type=int, default=None)
|
|
||||||
p.add_argument("--output", default="results_mixed.jsonl")
|
|
||||||
p.add_argument("--lh-samples", type=int, default=10)
|
|
||||||
p.add_argument("--dry-run", action="store_true", help="only show design size")
|
|
||||||
args = p.parse_args()
|
|
||||||
|
|
||||||
primary = [f for f in FACTORS if f.primary]
|
|
||||||
secondary = [f for f in FACTORS if not f.primary]
|
|
||||||
configs = generate_configs(args.lh_samples)
|
|
||||||
log.info(f"design: {len(configs)} runs | primary: {[f.name for f in primary]} | secondary (LH): {[f.name for f in secondary]}")
|
|
||||||
|
|
||||||
if not args.dry_run:
|
|
||||||
run_study(args.workers, args.output, args.lh_samples)
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
method: random
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
algo:
|
|
||||||
values: [ppo, a2c, dqn, qtable]
|
|
||||||
total_timesteps:
|
|
||||||
values: [30000, 50000, 80000]
|
|
||||||
seed:
|
|
||||||
values: [13, 42, 77]
|
|
||||||
n_products:
|
|
||||||
values: [8, 10, 12]
|
|
||||||
alpha:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.1
|
|
||||||
max: 0.6
|
|
||||||
lambda_coi:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.6
|
|
||||||
robust_radius:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.0
|
|
||||||
max: 0.3
|
|
||||||
robust_points:
|
|
||||||
values: [3, 5, 7]
|
|
||||||
info_value:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.5
|
|
||||||
max: 2.0
|
|
||||||
revenue_weight:
|
|
||||||
values: [0.005, 0.01, 0.02]
|
|
||||||
learning_rate:
|
|
||||||
distribution: log_uniform_values
|
|
||||||
min: 1.0e-5
|
|
||||||
max: 1.0e-3
|
|
||||||
gamma:
|
|
||||||
values: [0.97, 0.99, 0.995]
|
|
||||||
buffer_size:
|
|
||||||
values: [20000, 50000, 100000]
|
|
||||||
batch_size:
|
|
||||||
values: [128, 256, 512]
|
|
||||||
tau:
|
|
||||||
values: [0.002, 0.005, 0.01]
|
|
||||||
train_freq:
|
|
||||||
values: [1, 4, 8]
|
|
||||||
learning_starts:
|
|
||||||
values: [500, 1000, 3000]
|
|
||||||
n_steps:
|
|
||||||
values: [512, 1024, 2048]
|
|
||||||
n_epochs:
|
|
||||||
values: [5, 10, 20]
|
|
||||||
gae_lambda:
|
|
||||||
values: [0.9, 0.95, 0.98]
|
|
||||||
clip_range:
|
|
||||||
values: [0.1, 0.2, 0.3]
|
|
||||||
ent_coef:
|
|
||||||
values: [0.0, 0.005, 0.01]
|
|
||||||
target_update_interval:
|
|
||||||
values: [500, 1000, 2000]
|
|
||||||
exploration_fraction:
|
|
||||||
values: [0.1, 0.2, 0.3]
|
|
||||||
exploration_final_eps:
|
|
||||||
values: [0.01, 0.03, 0.05]
|
|
||||||
action_levels:
|
|
||||||
values: [7, 9, 11]
|
|
||||||
action_scale_low:
|
|
||||||
values: [0.75, 0.8, 0.85]
|
|
||||||
action_scale_high:
|
|
||||||
values: [1.15, 1.2, 1.25]
|
|
||||||
q_lr:
|
|
||||||
values: [0.03, 0.05, 0.1, 0.2]
|
|
||||||
eps_start:
|
|
||||||
value: 1.0
|
|
||||||
eps_end:
|
|
||||||
values: [0.02, 0.05, 0.1]
|
|
||||||
eps_decay:
|
|
||||||
values: [0.999, 0.9995, 0.9999]
|
|
||||||
@@ -1,85 +0,0 @@
|
|||||||
method: grid
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
run_cap: 4
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
algo:
|
|
||||||
values: [ppo, a2c, dqn, qtable]
|
|
||||||
seed:
|
|
||||||
value: 42
|
|
||||||
total_timesteps:
|
|
||||||
value: 12000
|
|
||||||
eval_episodes:
|
|
||||||
value: 3
|
|
||||||
eval_freq:
|
|
||||||
value: 500
|
|
||||||
log_freq:
|
|
||||||
value: 100
|
|
||||||
revenue_weight:
|
|
||||||
value: 0.01
|
|
||||||
n_products:
|
|
||||||
value: 8
|
|
||||||
N:
|
|
||||||
value: 80
|
|
||||||
alpha:
|
|
||||||
value: 0.3
|
|
||||||
lambda_coi:
|
|
||||||
value: 0.2
|
|
||||||
robust_radius:
|
|
||||||
value: 0.0
|
|
||||||
robust_points:
|
|
||||||
value: 1
|
|
||||||
info_value:
|
|
||||||
value: 1.0
|
|
||||||
learning_rate:
|
|
||||||
value: 0.0003
|
|
||||||
gamma:
|
|
||||||
value: 0.99
|
|
||||||
buffer_size:
|
|
||||||
value: 20000
|
|
||||||
batch_size:
|
|
||||||
value: 128
|
|
||||||
tau:
|
|
||||||
value: 0.005
|
|
||||||
train_freq:
|
|
||||||
value: 1
|
|
||||||
learning_starts:
|
|
||||||
value: 500
|
|
||||||
n_steps:
|
|
||||||
value: 512
|
|
||||||
n_epochs:
|
|
||||||
value: 10
|
|
||||||
gae_lambda:
|
|
||||||
value: 0.95
|
|
||||||
clip_range:
|
|
||||||
value: 0.2
|
|
||||||
ent_coef:
|
|
||||||
value: 0.0
|
|
||||||
target_update_interval:
|
|
||||||
value: 500
|
|
||||||
exploration_fraction:
|
|
||||||
value: 0.2
|
|
||||||
exploration_final_eps:
|
|
||||||
value: 0.05
|
|
||||||
action_levels:
|
|
||||||
value: 7
|
|
||||||
action_scale_low:
|
|
||||||
value: 0.9
|
|
||||||
action_scale_high:
|
|
||||||
value: 1.1
|
|
||||||
q_lr:
|
|
||||||
value: 0.1
|
|
||||||
q_bins:
|
|
||||||
value: 6
|
|
||||||
eps_start:
|
|
||||||
value: 1.0
|
|
||||||
eps_end:
|
|
||||||
value: 0.05
|
|
||||||
eps_decay:
|
|
||||||
value: 0.9995
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
method: bayes
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
algo:
|
|
||||||
value: sac
|
|
||||||
total_timesteps:
|
|
||||||
values: [50000, 80000, 120000]
|
|
||||||
seed:
|
|
||||||
values: [13, 42, 77]
|
|
||||||
alpha:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.15
|
|
||||||
max: 0.55
|
|
||||||
n_products:
|
|
||||||
values: [8, 10, 12]
|
|
||||||
lambda_coi:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.5
|
|
||||||
robust_radius:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.3
|
|
||||||
robust_points:
|
|
||||||
values: [3, 5, 7]
|
|
||||||
info_value:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.5
|
|
||||||
max: 2.0
|
|
||||||
revenue_weight:
|
|
||||||
values: [0.005, 0.01, 0.02]
|
|
||||||
learning_rate:
|
|
||||||
distribution: log_uniform_values
|
|
||||||
min: 3.0e-5
|
|
||||||
max: 1.0e-3
|
|
||||||
gamma:
|
|
||||||
values: [0.98, 0.99, 0.995]
|
|
||||||
buffer_size:
|
|
||||||
values: [50000, 100000, 200000]
|
|
||||||
batch_size:
|
|
||||||
values: [128, 256, 512]
|
|
||||||
tau:
|
|
||||||
values: [0.002, 0.005, 0.01]
|
|
||||||
train_freq:
|
|
||||||
values: [1, 4, 8]
|
|
||||||
learning_starts:
|
|
||||||
values: [1000, 3000, 5000]
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
method: random
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
algo:
|
|
||||||
values: [ppo, a2c, dqn, qtable]
|
|
||||||
arch:
|
|
||||||
values: [tiny, small, medium]
|
|
||||||
activation:
|
|
||||||
values: [relu, tanh]
|
|
||||||
total_timesteps:
|
|
||||||
values: [8000, 12000, 20000]
|
|
||||||
seed:
|
|
||||||
values: [13, 42, 77]
|
|
||||||
n_products:
|
|
||||||
values: [6, 8, 10]
|
|
||||||
alpha:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.1
|
|
||||||
max: 0.5
|
|
||||||
lambda_coi:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.4
|
|
||||||
robust_radius:
|
|
||||||
values: [0.0, 0.1, 0.2]
|
|
||||||
robust_points:
|
|
||||||
values: [3, 5]
|
|
||||||
info_value:
|
|
||||||
values: [0.75, 1.0, 1.5]
|
|
||||||
revenue_weight:
|
|
||||||
values: [0.005, 0.01, 0.02]
|
|
||||||
learning_rate:
|
|
||||||
distribution: log_uniform_values
|
|
||||||
min: 1.0e-5
|
|
||||||
max: 5.0e-4
|
|
||||||
gamma:
|
|
||||||
values: [0.98, 0.99]
|
|
||||||
buffer_size:
|
|
||||||
values: [10000, 30000, 50000]
|
|
||||||
batch_size:
|
|
||||||
values: [64, 128, 256]
|
|
||||||
tau:
|
|
||||||
values: [0.002, 0.005, 0.01]
|
|
||||||
train_freq:
|
|
||||||
values: [1, 4]
|
|
||||||
learning_starts:
|
|
||||||
values: [500, 1000, 2000]
|
|
||||||
n_steps:
|
|
||||||
values: [256, 512, 1024]
|
|
||||||
n_epochs:
|
|
||||||
values: [5, 10]
|
|
||||||
gae_lambda:
|
|
||||||
values: [0.9, 0.95]
|
|
||||||
clip_range:
|
|
||||||
values: [0.1, 0.2]
|
|
||||||
ent_coef:
|
|
||||||
values: [0.0, 0.005]
|
|
||||||
target_update_interval:
|
|
||||||
values: [500, 1000]
|
|
||||||
exploration_fraction:
|
|
||||||
values: [0.1, 0.2]
|
|
||||||
exploration_final_eps:
|
|
||||||
values: [0.02, 0.05]
|
|
||||||
action_levels:
|
|
||||||
values: [5, 7, 9]
|
|
||||||
action_scale_low:
|
|
||||||
values: [0.85, 0.9]
|
|
||||||
action_scale_high:
|
|
||||||
values: [1.1, 1.15]
|
|
||||||
q_lr:
|
|
||||||
values: [0.05, 0.1, 0.2]
|
|
||||||
q_bins:
|
|
||||||
values: [4, 6, 8]
|
|
||||||
eps_start:
|
|
||||||
value: 1.0
|
|
||||||
eps_end:
|
|
||||||
values: [0.02, 0.05]
|
|
||||||
eps_decay:
|
|
||||||
values: [0.999, 0.9995]
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
method: bayes
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
# fixed: always use JAX backend so TPU chips are actually exercised
|
|
||||||
use_jax:
|
|
||||||
value: true
|
|
||||||
# all four algos have JAX implementations
|
|
||||||
algo:
|
|
||||||
values: [ppo, a2c, dqn, qtable]
|
|
||||||
total_timesteps:
|
|
||||||
values: [50000, 80000, 120000]
|
|
||||||
checkpoint_interval:
|
|
||||||
value: 200000
|
|
||||||
seed:
|
|
||||||
values: [13, 42, 77]
|
|
||||||
n_products:
|
|
||||||
values: [8, 10, 12]
|
|
||||||
# COI framework parameters -- primary research variables
|
|
||||||
alpha:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.1
|
|
||||||
max: 0.6
|
|
||||||
lambda_coi:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.6
|
|
||||||
robust_radius:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.0
|
|
||||||
max: 0.3
|
|
||||||
robust_points:
|
|
||||||
values: [3, 5, 7]
|
|
||||||
info_value:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.5
|
|
||||||
max: 2.0
|
|
||||||
revenue_weight:
|
|
||||||
values: [0.005, 0.01, 0.02]
|
|
||||||
# shared hyperparameters
|
|
||||||
learning_rate:
|
|
||||||
distribution: log_uniform_values
|
|
||||||
min: 1.0e-5
|
|
||||||
max: 1.0e-3
|
|
||||||
gamma:
|
|
||||||
values: [0.97, 0.99, 0.995]
|
|
||||||
# JAX parallelism -- key lever for TPU throughput
|
|
||||||
jax_num_envs:
|
|
||||||
values: [8, 16, 32]
|
|
||||||
jax_num_steps:
|
|
||||||
values: [64, 128, 256]
|
|
||||||
jax_num_minibatches:
|
|
||||||
values: [2, 4, 8]
|
|
||||||
jax_update_epochs:
|
|
||||||
values: [2, 4, 8]
|
|
||||||
# PPO/A2C specific
|
|
||||||
gae_lambda:
|
|
||||||
values: [0.9, 0.95, 0.98]
|
|
||||||
clip_range:
|
|
||||||
values: [0.1, 0.2, 0.3]
|
|
||||||
ent_coef:
|
|
||||||
values: [0.0, 0.005, 0.01]
|
|
||||||
# DQN specific
|
|
||||||
buffer_size:
|
|
||||||
values: [20000, 50000, 100000]
|
|
||||||
batch_size:
|
|
||||||
values: [128, 256, 512]
|
|
||||||
learning_starts:
|
|
||||||
values: [500, 1000, 3000]
|
|
||||||
exploration_fraction:
|
|
||||||
values: [0.1, 0.2, 0.3]
|
|
||||||
exploration_final_eps:
|
|
||||||
values: [0.01, 0.03, 0.05]
|
|
||||||
# QTable specific
|
|
||||||
q_lr:
|
|
||||||
values: [0.03, 0.05, 0.1, 0.2]
|
|
||||||
eps_end:
|
|
||||||
values: [0.02, 0.05, 0.1]
|
|
||||||
eps_decay:
|
|
||||||
values: [0.999, 0.9995, 0.9999]
|
|
||||||
# action space
|
|
||||||
action_levels:
|
|
||||||
values: [7, 9, 11]
|
|
||||||
action_scale_low:
|
|
||||||
values: [0.75, 0.8, 0.85]
|
|
||||||
action_scale_high:
|
|
||||||
values: [1.15, 1.2, 1.25]
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
method: bayes
|
|
||||||
metric:
|
|
||||||
name: sweep/score
|
|
||||||
goal: maximize
|
|
||||||
command:
|
|
||||||
- ${env}
|
|
||||||
- python
|
|
||||||
- -m
|
|
||||||
- engine.train
|
|
||||||
parameters:
|
|
||||||
use_jax:
|
|
||||||
value: true
|
|
||||||
# pmap requires all workers to compile the same computation graph shape,
|
|
||||||
# so structural params are fixed -- only research/scalar params are swept
|
|
||||||
algo:
|
|
||||||
values: [ppo, a2c]
|
|
||||||
jax_num_envs:
|
|
||||||
value: 32
|
|
||||||
jax_num_steps:
|
|
||||||
value: 128
|
|
||||||
jax_num_minibatches:
|
|
||||||
value: 4
|
|
||||||
jax_update_epochs:
|
|
||||||
value: 4
|
|
||||||
total_timesteps:
|
|
||||||
value: 100000
|
|
||||||
checkpoint_interval:
|
|
||||||
value: 200000
|
|
||||||
n_products:
|
|
||||||
value: 10
|
|
||||||
action_levels:
|
|
||||||
value: 9
|
|
||||||
# research parameters -- primary sweep targets
|
|
||||||
alpha:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.1
|
|
||||||
max: 0.6
|
|
||||||
lambda_coi:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.05
|
|
||||||
max: 0.6
|
|
||||||
robust_radius:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.0
|
|
||||||
max: 0.3
|
|
||||||
info_value:
|
|
||||||
distribution: uniform
|
|
||||||
min: 0.5
|
|
||||||
max: 2.0
|
|
||||||
revenue_weight:
|
|
||||||
values: [0.005, 0.01, 0.02]
|
|
||||||
# training hyperparameters
|
|
||||||
learning_rate:
|
|
||||||
distribution: log_uniform_values
|
|
||||||
min: 1.0e-5
|
|
||||||
max: 1.0e-3
|
|
||||||
gamma:
|
|
||||||
values: [0.97, 0.99, 0.995]
|
|
||||||
gae_lambda:
|
|
||||||
values: [0.9, 0.95, 0.98]
|
|
||||||
clip_range:
|
|
||||||
values: [0.1, 0.2, 0.3]
|
|
||||||
ent_coef:
|
|
||||||
values: [0.0, 0.005, 0.01]
|
|
||||||
568
engine/train.py
568
engine/train.py
@@ -1,568 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from .wandb_checkpoint import checkpoint_artifact_name, download_latest_checkpoint
|
|
||||||
|
|
||||||
try:
|
|
||||||
import wandb as _wandb
|
|
||||||
|
|
||||||
if hasattr(_wandb, "init") and callable(_wandb.init):
|
|
||||||
wandb = _wandb
|
|
||||||
HAS_WANDB = True
|
|
||||||
else:
|
|
||||||
wandb = None
|
|
||||||
HAS_WANDB = False
|
|
||||||
except ImportError:
|
|
||||||
wandb = None
|
|
||||||
HAS_WANDB = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
from stable_baselines3 import PPO, A2C, DQN
|
|
||||||
from stable_baselines3.common.callbacks import EvalCallback
|
|
||||||
from stable_baselines3.common.monitor import Monitor
|
|
||||||
|
|
||||||
HAS_SB3 = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_SB3 = False
|
|
||||||
|
|
||||||
from .jax import JAX_AVAILABLE
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_CFG = {
|
|
||||||
"project": "phantom-pricing",
|
|
||||||
"algo": "ppo",
|
|
||||||
"seed": 42,
|
|
||||||
"total_timesteps": 50_000,
|
|
||||||
"eval_episodes": 5,
|
|
||||||
"eval_freq": 1_000,
|
|
||||||
"log_freq": 100,
|
|
||||||
"revenue_weight": 0.01,
|
|
||||||
"n_products": 10,
|
|
||||||
"N": 100,
|
|
||||||
"alpha": 0.3,
|
|
||||||
"lambda_coi": 0.2,
|
|
||||||
"robust_radius": 0.15,
|
|
||||||
"robust_points": 5,
|
|
||||||
"info_value": 1.0,
|
|
||||||
"price_low": 10.0,
|
|
||||||
"price_high": 150.0,
|
|
||||||
"action_levels": 9,
|
|
||||||
"action_scale_low": 0.8,
|
|
||||||
"action_scale_high": 1.2,
|
|
||||||
"learning_rate": 3e-4,
|
|
||||||
"gamma": 0.99,
|
|
||||||
"buffer_size": 50_000,
|
|
||||||
"batch_size": 256,
|
|
||||||
"tau": 0.005,
|
|
||||||
"train_freq": 1,
|
|
||||||
"learning_starts": 1_000,
|
|
||||||
"target_update_interval": 1_000,
|
|
||||||
"exploration_fraction": 0.2,
|
|
||||||
"exploration_final_eps": 0.05,
|
|
||||||
"n_steps": 2_048,
|
|
||||||
"n_epochs": 10,
|
|
||||||
"gae_lambda": 0.95,
|
|
||||||
"clip_range": 0.2,
|
|
||||||
"ent_coef": 0.0,
|
|
||||||
"q_lr": 0.1,
|
|
||||||
"eps_start": 1.0,
|
|
||||||
"eps_end": 0.05,
|
|
||||||
"eps_decay": 0.9995,
|
|
||||||
"model_dir": "engine/models",
|
|
||||||
"arch": "small",
|
|
||||||
"activation": "relu",
|
|
||||||
"q_bins": 6,
|
|
||||||
"max_steps": 100,
|
|
||||||
"margin_floor": 0.05,
|
|
||||||
"margin_floor_patience": 5,
|
|
||||||
"use_jax": False,
|
|
||||||
"jax_num_envs": 16,
|
|
||||||
"jax_num_steps": 128,
|
|
||||||
"jax_num_minibatches": 4,
|
|
||||||
"jax_update_epochs": 4,
|
|
||||||
"jax_anneal_lr": True,
|
|
||||||
"checkpoint_interval": 200_000,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _truthy(value: str | bool | None) -> bool:
|
|
||||||
if isinstance(value, bool): return value
|
|
||||||
if value is None: return False
|
|
||||||
return str(value).strip().lower() in {"1", "true", "yes", "on"}
|
|
||||||
|
|
||||||
|
|
||||||
def _cfg(raw: dict | None = None) -> dict:
|
|
||||||
cfg = dict(DEFAULT_CFG)
|
|
||||||
if raw:
|
|
||||||
cfg.update({k: v for k, v in raw.items() if v is not None})
|
|
||||||
cfg["algo"] = str(cfg["algo"]).lower()
|
|
||||||
cfg["use_jax"] = _truthy(cfg.get("use_jax")) or _truthy(
|
|
||||||
os.environ.get("PHANTOM_USE_JAX")
|
|
||||||
)
|
|
||||||
return cfg
|
|
||||||
|
|
||||||
|
|
||||||
def _wandb_cfg_dict() -> dict:
|
|
||||||
return (
|
|
||||||
{k: wandb.config[k] for k in wandb.config.keys()}
|
|
||||||
if HAS_WANDB and wandb.run
|
|
||||||
else {}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def make_env(cfg: dict):
|
|
||||||
from gymnasium.wrappers import FlattenObservation
|
|
||||||
|
|
||||||
from .wrapper import PHANTOM
|
|
||||||
from .lib.wrappers import EconomicMetricsWrapper
|
|
||||||
|
|
||||||
env = PHANTOM(
|
|
||||||
n_products=int(cfg["n_products"]),
|
|
||||||
alpha=float(cfg["alpha"]),
|
|
||||||
N=int(cfg["N"]),
|
|
||||||
price_bounds=(float(cfg["price_low"]), float(cfg["price_high"])),
|
|
||||||
lambda_coi=float(cfg["lambda_coi"]),
|
|
||||||
robust_radius=float(cfg["robust_radius"]),
|
|
||||||
robust_points=int(cfg["robust_points"]),
|
|
||||||
info_value=float(cfg["info_value"]),
|
|
||||||
action_levels=int(cfg["action_levels"]),
|
|
||||||
action_scale_low=float(cfg["action_scale_low"]),
|
|
||||||
action_scale_high=float(cfg["action_scale_high"]),
|
|
||||||
max_steps=int(cfg.get("max_steps", 100)),
|
|
||||||
margin_floor=float(cfg.get("margin_floor", 0.05)),
|
|
||||||
margin_floor_patience=int(cfg.get("margin_floor_patience", 5)),
|
|
||||||
render_mode=None,
|
|
||||||
)
|
|
||||||
env = EconomicMetricsWrapper(env)
|
|
||||||
env = FlattenObservation(env)
|
|
||||||
return env
|
|
||||||
|
|
||||||
|
|
||||||
def _net_arch(name) -> list[int]:
|
|
||||||
presets = {
|
|
||||||
"tiny": [32, 32],
|
|
||||||
"small": [64, 64],
|
|
||||||
"medium": [128, 128],
|
|
||||||
"large": [256, 256],
|
|
||||||
}
|
|
||||||
if isinstance(name, (list, tuple)):
|
|
||||||
return [int(v) for v in name]
|
|
||||||
s = str(name).lower().strip()
|
|
||||||
if s in presets:
|
|
||||||
return presets[s]
|
|
||||||
if "x" in s:
|
|
||||||
try:
|
|
||||||
vals = [int(v) for v in s.split("x") if v]
|
|
||||||
return vals if vals else presets["small"]
|
|
||||||
except ValueError:
|
|
||||||
return presets["small"]
|
|
||||||
return presets["small"]
|
|
||||||
|
|
||||||
|
|
||||||
def _activation(name):
|
|
||||||
try:
|
|
||||||
import torch.nn as nn
|
|
||||||
except ImportError:
|
|
||||||
return None
|
|
||||||
return {
|
|
||||||
"relu": nn.ReLU,
|
|
||||||
"tanh": nn.Tanh,
|
|
||||||
"elu": nn.ELU,
|
|
||||||
"leaky_relu": nn.LeakyReLU,
|
|
||||||
}.get(str(name).lower().strip(), nn.ReLU)
|
|
||||||
|
|
||||||
|
|
||||||
def _policy_kwargs(cfg: dict) -> dict:
|
|
||||||
kw = {"net_arch": _net_arch(cfg.get("arch", "small"))}
|
|
||||||
act = _activation(cfg.get("activation", "relu"))
|
|
||||||
if act is not None:
|
|
||||||
kw["activation_fn"] = act
|
|
||||||
return kw
|
|
||||||
|
|
||||||
|
|
||||||
def _action(agent, obs, deterministic: bool = True):
|
|
||||||
out = agent.predict(obs, deterministic=deterministic)
|
|
||||||
a = out[0] if isinstance(out, tuple) else out
|
|
||||||
if isinstance(a, np.ndarray) and a.size == 1:
|
|
||||||
return int(a.reshape(-1)[0])
|
|
||||||
return a
|
|
||||||
|
|
||||||
|
|
||||||
def evaluate(agent, env, episodes: int) -> dict:
|
|
||||||
rewards, revenues = [], []
|
|
||||||
for _ in range(int(episodes)):
|
|
||||||
obs, _ = env.reset()
|
|
||||||
done, ep_r, ep_rev = False, 0.0, 0.0
|
|
||||||
while not done:
|
|
||||||
obs, reward, term, trunc, info = env.step(_action(agent, obs, True))
|
|
||||||
done = term or trunc
|
|
||||||
ep_r += float(reward)
|
|
||||||
ep_rev += float(
|
|
||||||
info.get("economics", {}).get("revenue", info.get("revenue", 0.0))
|
|
||||||
)
|
|
||||||
rewards.append(ep_r)
|
|
||||||
revenues.append(ep_rev)
|
|
||||||
return {
|
|
||||||
"eval/reward": float(np.mean(rewards)),
|
|
||||||
"eval/revenue": float(np.mean(revenues)),
|
|
||||||
"eval/reward_std": float(np.std(rewards)),
|
|
||||||
"eval/revenue_std": float(np.std(revenues)),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def build_model(cfg: dict, env):
|
|
||||||
algo = cfg["algo"]
|
|
||||||
policy_kwargs = _policy_kwargs(cfg)
|
|
||||||
if algo == "sac":
|
|
||||||
raise ValueError("sac is not supported with the discrete core env")
|
|
||||||
if algo == "ppo":
|
|
||||||
return PPO(
|
|
||||||
"MlpPolicy",
|
|
||||||
env,
|
|
||||||
verbose=1,
|
|
||||||
policy_kwargs=policy_kwargs,
|
|
||||||
seed=int(cfg["seed"]),
|
|
||||||
learning_rate=float(cfg["learning_rate"]),
|
|
||||||
n_steps=int(cfg["n_steps"]),
|
|
||||||
batch_size=int(cfg["batch_size"]),
|
|
||||||
n_epochs=int(cfg["n_epochs"]),
|
|
||||||
gamma=float(cfg["gamma"]),
|
|
||||||
gae_lambda=float(cfg["gae_lambda"]),
|
|
||||||
clip_range=float(cfg["clip_range"]),
|
|
||||||
ent_coef=float(cfg["ent_coef"]),
|
|
||||||
)
|
|
||||||
if algo == "a2c":
|
|
||||||
return A2C(
|
|
||||||
"MlpPolicy",
|
|
||||||
env,
|
|
||||||
verbose=1,
|
|
||||||
policy_kwargs=policy_kwargs,
|
|
||||||
seed=int(cfg["seed"]),
|
|
||||||
learning_rate=float(cfg["learning_rate"]),
|
|
||||||
n_steps=max(5, int(cfg["n_steps"]) // 32),
|
|
||||||
gamma=float(cfg["gamma"]),
|
|
||||||
gae_lambda=float(cfg["gae_lambda"]),
|
|
||||||
ent_coef=float(cfg["ent_coef"]),
|
|
||||||
)
|
|
||||||
if algo == "dqn":
|
|
||||||
return DQN(
|
|
||||||
"MlpPolicy",
|
|
||||||
env,
|
|
||||||
verbose=1,
|
|
||||||
policy_kwargs=policy_kwargs,
|
|
||||||
seed=int(cfg["seed"]),
|
|
||||||
learning_rate=float(cfg["learning_rate"]),
|
|
||||||
buffer_size=int(cfg["buffer_size"]),
|
|
||||||
batch_size=int(cfg["batch_size"]),
|
|
||||||
gamma=float(cfg["gamma"]),
|
|
||||||
train_freq=int(cfg["train_freq"]),
|
|
||||||
learning_starts=int(cfg["learning_starts"]),
|
|
||||||
target_update_interval=int(cfg["target_update_interval"]),
|
|
||||||
exploration_fraction=float(cfg["exploration_fraction"]),
|
|
||||||
exploration_final_eps=float(cfg["exploration_final_eps"]),
|
|
||||||
)
|
|
||||||
raise ValueError(f"unsupported algo '{algo}'")
|
|
||||||
|
|
||||||
|
|
||||||
def _sb3_model_cls(algo: str):
|
|
||||||
if algo == "ppo":
|
|
||||||
return PPO
|
|
||||||
if algo == "a2c":
|
|
||||||
return A2C
|
|
||||||
if algo == "dqn":
|
|
||||||
return DQN
|
|
||||||
raise ValueError(f"unsupported algo '{algo}'")
|
|
||||||
|
|
||||||
|
|
||||||
def train_qtable(cfg: dict) -> tuple[EventQTable, dict]:
|
|
||||||
from .lib.discrete import EventQTable
|
|
||||||
|
|
||||||
np.random.seed(int(cfg["seed"]))
|
|
||||||
env = make_env(cfg)
|
|
||||||
eval_env = make_env(cfg)
|
|
||||||
agent = EventQTable(
|
|
||||||
env.action_space.n,
|
|
||||||
int(cfg["n_products"]),
|
|
||||||
(float(cfg["price_low"]), float(cfg["price_high"])),
|
|
||||||
lr=float(cfg["q_lr"]),
|
|
||||||
gamma=float(cfg["gamma"]),
|
|
||||||
n_bins=int(cfg["q_bins"]),
|
|
||||||
)
|
|
||||||
eps = float(cfg["eps_start"])
|
|
||||||
obs, _ = env.reset(seed=int(cfg["seed"]))
|
|
||||||
for t in range(int(cfg["total_timesteps"])):
|
|
||||||
a, s = agent.act(obs, eps)
|
|
||||||
nxt, reward, term, trunc, info = env.step(a)
|
|
||||||
done = term or trunc
|
|
||||||
agent.update(s, a, float(reward), agent.encode(nxt), done)
|
|
||||||
eps = max(float(cfg["eps_end"]), eps * float(cfg["eps_decay"]))
|
|
||||||
if HAS_WANDB and wandb.run and (t + 1) % int(cfg["log_freq"]) == 0:
|
|
||||||
econ = info.get("economics", {})
|
|
||||||
wandb.log(
|
|
||||||
{
|
|
||||||
"train/reward": float(reward),
|
|
||||||
"train/revenue": float(econ.get("revenue", 0.0)),
|
|
||||||
"train/epsilon": float(eps),
|
|
||||||
},
|
|
||||||
step=t + 1,
|
|
||||||
)
|
|
||||||
obs = env.reset()[0] if done else nxt
|
|
||||||
metrics = evaluate(agent, eval_env, int(cfg["eval_episodes"]))
|
|
||||||
metrics["train/global_step"] = int(cfg["total_timesteps"])
|
|
||||||
env.close()
|
|
||||||
eval_env.close()
|
|
||||||
return agent, metrics
|
|
||||||
|
|
||||||
|
|
||||||
def train_sb3(cfg: dict) -> tuple[object, dict]:
|
|
||||||
if not HAS_SB3:
|
|
||||||
raise ImportError("stable-baselines3 is required for SB3 models")
|
|
||||||
from .lib.callbacks import CheckpointArtifactCallback, MetricsCallback
|
|
||||||
|
|
||||||
env = make_env(cfg)
|
|
||||||
eval_env = make_env(cfg)
|
|
||||||
env = Monitor(env)
|
|
||||||
eval_env = Monitor(eval_env)
|
|
||||||
model = build_model(cfg, env)
|
|
||||||
resume_step = 0
|
|
||||||
if HAS_WANDB and wandb.run is not None:
|
|
||||||
sweep_id = getattr(wandb.run, "sweep_id", None)
|
|
||||||
artifact_name = checkpoint_artifact_name(cfg, backend="sb3", sweep_id=sweep_id)
|
|
||||||
checkpoint_file = f"phantom_{cfg['algo']}_checkpoint.zip"
|
|
||||||
restored = download_latest_checkpoint(artifact_name, file_name=checkpoint_file)
|
|
||||||
if restored is not None:
|
|
||||||
checkpoint_path, metadata = restored
|
|
||||||
model = _sb3_model_cls(cfg["algo"]).load(
|
|
||||||
checkpoint_path.as_posix(), env=env
|
|
||||||
)
|
|
||||||
resume_step = int(metadata.get("step", getattr(model, "num_timesteps", 0)))
|
|
||||||
model.num_timesteps = max(
|
|
||||||
int(getattr(model, "num_timesteps", 0)), resume_step
|
|
||||||
)
|
|
||||||
|
|
||||||
cbs = [MetricsCallback(log_histograms=True, log_freq=int(cfg["log_freq"]))]
|
|
||||||
cbs.append(
|
|
||||||
CheckpointArtifactCallback(
|
|
||||||
cfg,
|
|
||||||
interval=int(cfg.get("checkpoint_interval", 10_000)),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
cbs.append(
|
|
||||||
EvalCallback(
|
|
||||||
eval_env,
|
|
||||||
eval_freq=int(cfg["eval_freq"]),
|
|
||||||
n_eval_episodes=int(cfg["eval_episodes"]),
|
|
||||||
deterministic=True,
|
|
||||||
verbose=0,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
target_steps = int(cfg["total_timesteps"])
|
|
||||||
remaining_steps = max(0, target_steps - int(getattr(model, "num_timesteps", 0)))
|
|
||||||
if remaining_steps > 0:
|
|
||||||
model.learn(
|
|
||||||
total_timesteps=remaining_steps,
|
|
||||||
callback=cbs,
|
|
||||||
reset_num_timesteps=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
model_path = Path(cfg["model_dir"])
|
|
||||||
model_path.mkdir(parents=True, exist_ok=True)
|
|
||||||
model.save(str(model_path / f"phantom_{cfg['algo']}"))
|
|
||||||
metrics = evaluate(model, eval_env, int(cfg["eval_episodes"]))
|
|
||||||
metrics["train/global_step"] = int(model.num_timesteps)
|
|
||||||
env.close()
|
|
||||||
eval_env.close()
|
|
||||||
return model, metrics
|
|
||||||
|
|
||||||
|
|
||||||
def train_once(cfg: dict) -> dict:
|
|
||||||
algo = cfg["algo"]
|
|
||||||
if cfg.get("use_jax"):
|
|
||||||
if not JAX_AVAILABLE:
|
|
||||||
raise ImportError(
|
|
||||||
"JAX backend requested but JAX is not installed. "
|
|
||||||
"Install engine/jax/requirements.txt and jax[tpu] for TPU runs."
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
from .jax.train import train_jax
|
|
||||||
except Exception as exc: # pragma: no cover
|
|
||||||
raise ImportError(f"Failed to import JAX trainer: {exc}") from exc
|
|
||||||
_, metrics = train_jax(cfg)
|
|
||||||
elif algo == "qtable":
|
|
||||||
_, metrics = train_qtable(cfg)
|
|
||||||
else:
|
|
||||||
_, metrics = train_sb3(cfg)
|
|
||||||
metrics["sweep/score"] = float(
|
|
||||||
metrics["eval/reward"] + float(cfg["revenue_weight"]) * metrics["eval/revenue"]
|
|
||||||
)
|
|
||||||
return metrics
|
|
||||||
|
|
||||||
|
|
||||||
def run_wandb(
|
|
||||||
project: str, overrides: dict, mode: str = "online", sweep_mode: bool = False
|
|
||||||
) -> dict:
|
|
||||||
if not HAS_WANDB:
|
|
||||||
raise ImportError("wandb is required for sweep runs")
|
|
||||||
if not sweep_mode:
|
|
||||||
pre_cfg = _cfg(overrides)
|
|
||||||
if pre_cfg.get("use_jax"):
|
|
||||||
try:
|
|
||||||
import jax
|
|
||||||
|
|
||||||
if jax.process_count() > 1 and jax.process_index() != 0:
|
|
||||||
return train_once(pre_cfg)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
init_kwargs = {"mode": mode}
|
|
||||||
if sweep_mode:
|
|
||||||
run = wandb.init(**init_kwargs)
|
|
||||||
else:
|
|
||||||
run = wandb.init(project=project, config=overrides, **init_kwargs)
|
|
||||||
|
|
||||||
try:
|
|
||||||
cfg = _cfg(_wandb_cfg_dict())
|
|
||||||
if sweep_mode:
|
|
||||||
for k, v in overrides.items():
|
|
||||||
if k not in wandb.config:
|
|
||||||
cfg[k] = v
|
|
||||||
|
|
||||||
metrics = train_once(cfg)
|
|
||||||
step = int(metrics.get("train/global_step", cfg["total_timesteps"]))
|
|
||||||
wandb.log(metrics, step=step)
|
|
||||||
for k, v in metrics.items():
|
|
||||||
run.summary[k] = v
|
|
||||||
return metrics
|
|
||||||
finally:
|
|
||||||
if wandb.run is not None:
|
|
||||||
wandb.finish()
|
|
||||||
|
|
||||||
|
|
||||||
def run_local(overrides: dict) -> dict:
|
|
||||||
cfg = _cfg(overrides)
|
|
||||||
metrics = train_once(cfg)
|
|
||||||
should_print = True
|
|
||||||
if cfg.get("use_jax"):
|
|
||||||
try:
|
|
||||||
import jax
|
|
||||||
|
|
||||||
should_print = jax.process_index() == 0
|
|
||||||
except Exception:
|
|
||||||
should_print = True
|
|
||||||
if should_print:
|
|
||||||
print(json.dumps(metrics, indent=2))
|
|
||||||
# sentinel line for machine-readable extraction; must stay on one line
|
|
||||||
print("PHANTOM_METRICS:" + json.dumps(metrics))
|
|
||||||
return metrics
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
p = argparse.ArgumentParser(description="PHANTOM training and W&B sweeps")
|
|
||||||
p.add_argument("--project", default=DEFAULT_CFG["project"])
|
|
||||||
p.add_argument("--algo", choices=["ppo", "a2c", "dqn", "qtable"])
|
|
||||||
p.add_argument("--seed", type=int)
|
|
||||||
p.add_argument("--total-timesteps", type=int)
|
|
||||||
p.add_argument("--alpha", type=float)
|
|
||||||
p.add_argument("--N", type=int)
|
|
||||||
p.add_argument("--n-products", type=int)
|
|
||||||
p.add_argument("--lambda-coi", type=float)
|
|
||||||
p.add_argument("--info-value", type=float)
|
|
||||||
p.add_argument("--robust-radius", type=float)
|
|
||||||
p.add_argument("--robust-points", type=int)
|
|
||||||
p.add_argument("--learning-rate", type=float)
|
|
||||||
p.add_argument("--gamma", type=float)
|
|
||||||
p.add_argument("--gae-lambda", type=float)
|
|
||||||
p.add_argument("--clip-range", type=float)
|
|
||||||
p.add_argument("--ent-coef", type=float)
|
|
||||||
p.add_argument("--revenue-weight", type=float)
|
|
||||||
p.add_argument("--price-low", type=float)
|
|
||||||
p.add_argument("--price-high", type=float)
|
|
||||||
p.add_argument("--action-levels", type=int)
|
|
||||||
p.add_argument("--action-scale-low", type=float)
|
|
||||||
p.add_argument("--action-scale-high", type=float)
|
|
||||||
p.add_argument("--max-steps", type=int)
|
|
||||||
p.add_argument("--margin-floor", type=float)
|
|
||||||
p.add_argument("--margin-floor-patience", type=int)
|
|
||||||
p.add_argument("--arch", type=str)
|
|
||||||
p.add_argument("--activation", type=str)
|
|
||||||
p.add_argument("--jax", action="store_true")
|
|
||||||
p.add_argument("--jax-num-envs", type=int)
|
|
||||||
p.add_argument("--jax-num-steps", type=int)
|
|
||||||
p.add_argument("--jax-num-minibatches", type=int)
|
|
||||||
p.add_argument("--jax-update-epochs", type=int)
|
|
||||||
p.add_argument("--jax-anneal-lr", type=str)
|
|
||||||
p.add_argument("--checkpoint-interval", type=int)
|
|
||||||
p.add_argument("--sweep-agent", action="store_true")
|
|
||||||
p.add_argument("--sweep-id", type=str)
|
|
||||||
p.add_argument("--count", type=int, default=0)
|
|
||||||
p.add_argument("--offline", action="store_true")
|
|
||||||
p.add_argument("--no-wandb", action="store_true")
|
|
||||||
args = p.parse_args()
|
|
||||||
|
|
||||||
overrides = {
|
|
||||||
"algo": args.algo,
|
|
||||||
"seed": args.seed,
|
|
||||||
"total_timesteps": args.total_timesteps,
|
|
||||||
"alpha": args.alpha,
|
|
||||||
"N": args.N,
|
|
||||||
"n_products": args.n_products,
|
|
||||||
"lambda_coi": args.lambda_coi,
|
|
||||||
"info_value": args.info_value,
|
|
||||||
"robust_radius": args.robust_radius,
|
|
||||||
"robust_points": args.robust_points,
|
|
||||||
"learning_rate": args.learning_rate,
|
|
||||||
"gamma": args.gamma,
|
|
||||||
"gae_lambda": args.gae_lambda,
|
|
||||||
"clip_range": args.clip_range,
|
|
||||||
"ent_coef": args.ent_coef,
|
|
||||||
"revenue_weight": args.revenue_weight,
|
|
||||||
"price_low": args.price_low,
|
|
||||||
"price_high": args.price_high,
|
|
||||||
"action_levels": args.action_levels,
|
|
||||||
"action_scale_low": args.action_scale_low,
|
|
||||||
"action_scale_high": args.action_scale_high,
|
|
||||||
"max_steps": args.max_steps,
|
|
||||||
"margin_floor": args.margin_floor,
|
|
||||||
"margin_floor_patience": args.margin_floor_patience,
|
|
||||||
"arch": args.arch,
|
|
||||||
"activation": args.activation,
|
|
||||||
"use_jax": args.jax,
|
|
||||||
"jax_num_envs": args.jax_num_envs,
|
|
||||||
"jax_num_steps": args.jax_num_steps,
|
|
||||||
"jax_num_minibatches": args.jax_num_minibatches,
|
|
||||||
"jax_update_epochs": args.jax_update_epochs,
|
|
||||||
"checkpoint_interval": args.checkpoint_interval,
|
|
||||||
"jax_anneal_lr": _truthy(args.jax_anneal_lr)
|
|
||||||
if args.jax_anneal_lr is not None
|
|
||||||
else None,
|
|
||||||
}
|
|
||||||
overrides = {k: v for k, v in overrides.items() if v is not None}
|
|
||||||
|
|
||||||
if args.sweep_agent:
|
|
||||||
if args.no_wandb:
|
|
||||||
raise ValueError("sweep agent requires wandb")
|
|
||||||
if not args.sweep_id:
|
|
||||||
raise ValueError("--sweep-id is required with --sweep-agent")
|
|
||||||
mode = "offline" if args.offline else "online"
|
|
||||||
wandb.agent(
|
|
||||||
args.sweep_id,
|
|
||||||
function=lambda: run_wandb(
|
|
||||||
args.project, overrides, mode=mode, sweep_mode=True
|
|
||||||
),
|
|
||||||
count=args.count if args.count > 0 else None,
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
if args.no_wandb or not HAS_WANDB:
|
|
||||||
run_local(overrides)
|
|
||||||
return
|
|
||||||
|
|
||||||
run_wandb(args.project, overrides, mode="offline" if args.offline else "online")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import hashlib
|
|
||||||
import json
|
|
||||||
import re
|
|
||||||
from pathlib import Path
|
|
||||||
from tempfile import TemporaryDirectory
|
|
||||||
from typing import Any, Mapping
|
|
||||||
|
|
||||||
try:
|
|
||||||
import wandb
|
|
||||||
from wandb.errors import CommError
|
|
||||||
|
|
||||||
HAS_WANDB = True
|
|
||||||
except ImportError:
|
|
||||||
HAS_WANDB = False
|
|
||||||
wandb = None # type: ignore[assignment]
|
|
||||||
CommError = RuntimeError # type: ignore[assignment]
|
|
||||||
|
|
||||||
|
|
||||||
def _safe_value(value: Any) -> Any:
|
|
||||||
if isinstance(value, (str, int, float, bool)) or value is None:
|
|
||||||
return value
|
|
||||||
if isinstance(value, (list, tuple)):
|
|
||||||
return [_safe_value(v) for v in value]
|
|
||||||
if isinstance(value, dict):
|
|
||||||
return {str(k): _safe_value(value[k]) for k in sorted(value)}
|
|
||||||
return str(value)
|
|
||||||
|
|
||||||
|
|
||||||
def _safe_scope(scope: str | None) -> str:
|
|
||||||
raw = "manual" if scope in (None, "") else str(scope)
|
|
||||||
cleaned = re.sub(r"[^A-Za-z0-9_.-]+", "-", raw).strip("-")
|
|
||||||
return cleaned or "manual"
|
|
||||||
|
|
||||||
|
|
||||||
def checkpoint_artifact_name(
|
|
||||||
cfg: Mapping[str, Any], *, backend: str, sweep_id: str | None = None
|
|
||||||
) -> str:
|
|
||||||
payload = {k: _safe_value(cfg[k]) for k in sorted(cfg)}
|
|
||||||
scope = _safe_scope(sweep_id)
|
|
||||||
canonical = json.dumps(
|
|
||||||
{"backend": backend, "scope": scope, "cfg": payload},
|
|
||||||
sort_keys=True,
|
|
||||||
separators=(",", ":"),
|
|
||||||
)
|
|
||||||
digest = hashlib.sha1(canonical.encode("utf-8")).hexdigest()[:14]
|
|
||||||
return f"phantom-{backend}-ckpt-{scope}-{digest}"[:128]
|
|
||||||
|
|
||||||
|
|
||||||
def _is_missing_artifact_error(exc: Exception) -> bool:
|
|
||||||
if isinstance(exc, CommError):
|
|
||||||
msg = str(exc).lower()
|
|
||||||
return "not found" in msg or "does not exist" in msg
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def download_latest_checkpoint(
|
|
||||||
artifact_name: str, *, file_name: str
|
|
||||||
) -> tuple[Path, dict[str, Any]] | None:
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return None
|
|
||||||
try:
|
|
||||||
artifact = wandb.run.use_artifact(f"{artifact_name}:latest")
|
|
||||||
except Exception as exc:
|
|
||||||
if _is_missing_artifact_error(exc):
|
|
||||||
return None
|
|
||||||
raise
|
|
||||||
directory = Path(artifact.download())
|
|
||||||
checkpoint_path = directory / file_name
|
|
||||||
if not checkpoint_path.exists():
|
|
||||||
return None
|
|
||||||
metadata = dict(getattr(artifact, "metadata", {}) or {})
|
|
||||||
return checkpoint_path, metadata
|
|
||||||
|
|
||||||
|
|
||||||
def _aliases_from_metadata(metadata: dict[str, Any] | None) -> list[str]:
|
|
||||||
aliases = ["latest"]
|
|
||||||
if metadata is None:
|
|
||||||
return aliases
|
|
||||||
if "step" in metadata:
|
|
||||||
try:
|
|
||||||
aliases.append(f"step-{int(metadata['step'])}")
|
|
||||||
except (TypeError, ValueError):
|
|
||||||
pass
|
|
||||||
return aliases
|
|
||||||
|
|
||||||
|
|
||||||
def log_checkpoint_bytes(
|
|
||||||
artifact_name: str,
|
|
||||||
*,
|
|
||||||
file_name: str,
|
|
||||||
payload: bytes,
|
|
||||||
metadata: dict[str, Any] | None = None,
|
|
||||||
) -> bool:
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return False
|
|
||||||
with TemporaryDirectory(prefix="phantom-ckpt-") as tmpdir:
|
|
||||||
path = Path(tmpdir) / file_name
|
|
||||||
path.write_bytes(payload)
|
|
||||||
artifact = wandb.Artifact(
|
|
||||||
name=artifact_name,
|
|
||||||
type="checkpoint",
|
|
||||||
metadata=metadata or {},
|
|
||||||
)
|
|
||||||
artifact.add_file(path.as_posix(), name=file_name)
|
|
||||||
wandb.log_artifact(artifact, aliases=_aliases_from_metadata(metadata))
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def log_checkpoint_file(
|
|
||||||
artifact_name: str,
|
|
||||||
*,
|
|
||||||
file_path: str | Path,
|
|
||||||
artifact_file_name: str,
|
|
||||||
metadata: dict[str, Any] | None = None,
|
|
||||||
) -> bool:
|
|
||||||
if not HAS_WANDB or wandb.run is None:
|
|
||||||
return False
|
|
||||||
src = Path(file_path)
|
|
||||||
if not src.exists():
|
|
||||||
return False
|
|
||||||
artifact = wandb.Artifact(
|
|
||||||
name=artifact_name,
|
|
||||||
type="checkpoint",
|
|
||||||
metadata=metadata or {},
|
|
||||||
)
|
|
||||||
artifact.add_file(src.as_posix(), name=artifact_file_name)
|
|
||||||
wandb.log_artifact(artifact, aliases=_aliases_from_metadata(metadata))
|
|
||||||
return True
|
|
||||||
@@ -1,366 +0,0 @@
|
|||||||
import gymnasium as gym
|
|
||||||
from gymnasium import spaces
|
|
||||||
import numpy as np
|
|
||||||
from .engine import Limbo, MarketEngine, PricingEngine
|
|
||||||
from .lib.render import DashboardRenderer
|
|
||||||
from .lib.coi import (
|
|
||||||
compute_uplift_coi,
|
|
||||||
extract_purchases,
|
|
||||||
compute_agent_probability,
|
|
||||||
)
|
|
||||||
from .lib.behavior import get_transition_models, trajectory_to_events
|
|
||||||
from .lib.wrappers import EconomicMetricsWrapper
|
|
||||||
|
|
||||||
|
|
||||||
class _ActionPricingEngine(PricingEngine):
|
|
||||||
def __init__(self, n_products: int, price_bounds: tuple):
|
|
||||||
self._prices = np.full(n_products, price_bounds[0], dtype=float)
|
|
||||||
|
|
||||||
def set_prices(self, prices: np.ndarray):
|
|
||||||
self._prices = np.asarray(prices, dtype=float)
|
|
||||||
|
|
||||||
def act(self, _):
|
|
||||||
return self._prices
|
|
||||||
|
|
||||||
|
|
||||||
class PHANTOM(gym.Env):
|
|
||||||
"""Gymnasium wrapper for Limbo pricing-market simulation implementing thesis COI framework
|
|
||||||
|
|
||||||
reward = R(p,d) - λ·COI_leak(p,τ') per thesis Section on DR-RL
|
|
||||||
COI_leak uses behavioral divergence to estimate agent probability f(τ')
|
|
||||||
robust inner step: min over alpha in Wasserstein interval around nominal alpha
|
|
||||||
actions are discrete global price-scale moves
|
|
||||||
"""
|
|
||||||
|
|
||||||
metadata = {"render_modes": ["human", "ansi"]}
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
n_products: int = 10,
|
|
||||||
alpha: float = 0.3,
|
|
||||||
N: int = 100,
|
|
||||||
human_params: tuple = (50.0, 10.0),
|
|
||||||
agent_params: tuple = (45.0, 15.0),
|
|
||||||
noise_std: float = 1.0,
|
|
||||||
price_bounds: tuple = (10.0, 150.0),
|
|
||||||
lambda_coi: float = 0.1,
|
|
||||||
coi_window: int = 10,
|
|
||||||
robust_radius: float = 0.0,
|
|
||||||
robust_points: int = 5,
|
|
||||||
info_value: float = 1.0,
|
|
||||||
action_levels: int = 9,
|
|
||||||
action_scale_low: float = 0.9,
|
|
||||||
action_scale_high: float = 1.1,
|
|
||||||
max_steps: int = 100,
|
|
||||||
margin_floor: float = 0.05,
|
|
||||||
margin_floor_patience: int = 5,
|
|
||||||
render_mode: str = None,
|
|
||||||
):
|
|
||||||
super().__init__()
|
|
||||||
self.n_products = n_products
|
|
||||||
self.price_bounds = price_bounds
|
|
||||||
self.lambda_coi = lambda_coi
|
|
||||||
self.coi_window = coi_window
|
|
||||||
self.max_steps = max(1, int(max_steps))
|
|
||||||
self.margin_floor = float(
|
|
||||||
margin_floor
|
|
||||||
) # terminate if avg margin stays below this for patience steps
|
|
||||||
self.margin_floor_patience = max(1, int(margin_floor_patience))
|
|
||||||
self.render_mode = render_mode
|
|
||||||
self.alpha = float(alpha)
|
|
||||||
self.nominal_alpha = float(alpha)
|
|
||||||
self.N = N
|
|
||||||
self.human_params = human_params
|
|
||||||
self.agent_params = agent_params
|
|
||||||
self.robust_radius = max(0.0, float(robust_radius))
|
|
||||||
self.robust_points = max(1, int(robust_points))
|
|
||||||
self.info_value = float(info_value)
|
|
||||||
self.action_levels = max(2, int(action_levels))
|
|
||||||
self._action_scales = np.linspace(
|
|
||||||
float(action_scale_low), float(action_scale_high), self.action_levels
|
|
||||||
)
|
|
||||||
|
|
||||||
self.market = MarketEngine(
|
|
||||||
alpha=alpha,
|
|
||||||
N=N,
|
|
||||||
human_params=human_params,
|
|
||||||
agent_params=agent_params,
|
|
||||||
noise_std=noise_std,
|
|
||||||
)
|
|
||||||
self._platform_stub = _ActionPricingEngine(n_products, price_bounds)
|
|
||||||
self._limbo = Limbo(self._platform_stub, self.market)
|
|
||||||
self._set_market_mix(self.nominal_alpha)
|
|
||||||
|
|
||||||
self.action_space = spaces.Discrete(self.action_levels)
|
|
||||||
self.observation_space = spaces.Dict(
|
|
||||||
{
|
|
||||||
"demand": spaces.Box(
|
|
||||||
low=0.0, high=100.0, shape=(n_products,), dtype=np.float32
|
|
||||||
),
|
|
||||||
"prices": spaces.Box(
|
|
||||||
low=price_bounds[0],
|
|
||||||
high=price_bounds[1],
|
|
||||||
shape=(n_products,),
|
|
||||||
dtype=np.float32,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
self._prices = None
|
|
||||||
self._demand = None
|
|
||||||
self._step_count = 0
|
|
||||||
self._demand_history = []
|
|
||||||
self._price_history = []
|
|
||||||
self._revenue_history = []
|
|
||||||
self._renderer = None
|
|
||||||
self._initial_episode_prices = None
|
|
||||||
self._trajectories = [] # session trajectories for agent prob calculation
|
|
||||||
self.baseline_prices = np.full(self.n_products, self.price_bounds[0])
|
|
||||||
self._low_margin_streak = 0 # consecutive steps below margin_floor
|
|
||||||
|
|
||||||
# load behavioral models for agent probability estimation
|
|
||||||
try:
|
|
||||||
self._human_trans, self._agent_trans = get_transition_models()
|
|
||||||
except Exception:
|
|
||||||
# fallback if behavioral data unavailable
|
|
||||||
self._human_trans, self._agent_trans = None, None
|
|
||||||
|
|
||||||
def _get_obs(self) -> dict:
|
|
||||||
demand_arr = np.array(
|
|
||||||
[self._demand.get(i, 0.0) for i in range(self.n_products)], dtype=np.float32
|
|
||||||
)
|
|
||||||
return {"demand": demand_arr, "prices": self._prices.astype(np.float32)}
|
|
||||||
|
|
||||||
def _set_market_mix(self, alpha: float):
|
|
||||||
alpha = float(np.clip(alpha, 0.0, 1.0))
|
|
||||||
n_agents = int(self.N * alpha)
|
|
||||||
self.alpha = alpha
|
|
||||||
self.market.alpha = alpha
|
|
||||||
self.market.Nagents = n_agents
|
|
||||||
self.market.Nhumans = self.N - n_agents
|
|
||||||
|
|
||||||
def _decode_action(self, action) -> np.ndarray:
|
|
||||||
base = (
|
|
||||||
self._prices
|
|
||||||
if self._prices is not None
|
|
||||||
else np.full(self.n_products, self.price_bounds[0], dtype=float)
|
|
||||||
)
|
|
||||||
if np.isscalar(action):
|
|
||||||
idx = int(np.clip(int(action), 0, self.action_levels - 1))
|
|
||||||
return np.clip(base * self._action_scales[idx], *self.price_bounds)
|
|
||||||
a = np.asarray(action)
|
|
||||||
if a.size == 1:
|
|
||||||
idx = int(np.clip(int(a.reshape(-1)[0]), 0, self.action_levels - 1))
|
|
||||||
return np.clip(base * self._action_scales[idx], *self.price_bounds)
|
|
||||||
return np.clip(a.astype(float), *self.price_bounds)
|
|
||||||
|
|
||||||
def _compute_agent_prob(self, trajectories=None) -> float:
|
|
||||||
trajectories = (
|
|
||||||
self.market.last_trajectories if trajectories is None else trajectories
|
|
||||||
)
|
|
||||||
if not trajectories or self._human_trans is None or self._agent_trans is None:
|
|
||||||
return float(self.market.alpha)
|
|
||||||
probs = []
|
|
||||||
for traj in trajectories:
|
|
||||||
events = trajectory_to_events(traj)
|
|
||||||
if len(events) < 2:
|
|
||||||
continue
|
|
||||||
probs.append(
|
|
||||||
compute_agent_probability(events, self._human_trans, self._agent_trans)
|
|
||||||
)
|
|
||||||
return float(np.mean(probs)) if probs else float(self.market.alpha)
|
|
||||||
|
|
||||||
def _compute_reward(
|
|
||||||
self, prices: np.ndarray, demand: dict, agent_prob: float, trajectories: list
|
|
||||||
) -> tuple[float, dict]:
|
|
||||||
demand_arr = np.array(
|
|
||||||
[demand.get(i, 0.0) for i in range(self.n_products)], dtype=float
|
|
||||||
)
|
|
||||||
revenue = float(np.dot(prices, demand_arr))
|
|
||||||
purchases = extract_purchases(trajectories)
|
|
||||||
coi_mix = compute_uplift_coi(prices, purchases, self.baseline_prices)
|
|
||||||
# multiplicative penalty so COI term scales with revenue magnitude
|
|
||||||
coi_leakage = float(agent_prob * self.info_value)
|
|
||||||
discount = float(np.clip(1.0 - self.lambda_coi * coi_leakage, 0.0, 1.0))
|
|
||||||
coi_penalty = revenue * (1.0 - discount) # absolute penalty in revenue units
|
|
||||||
reward = revenue * discount
|
|
||||||
return reward, {
|
|
||||||
"revenue": revenue,
|
|
||||||
"coi_mix": float(coi_mix),
|
|
||||||
"coi_base": 0.0,
|
|
||||||
"coi_leakage": coi_leakage,
|
|
||||||
"coi_penalty": coi_penalty,
|
|
||||||
"coi_discount": discount,
|
|
||||||
}
|
|
||||||
|
|
||||||
def _alpha_candidates(self) -> np.ndarray:
|
|
||||||
if self.robust_radius <= 0.0 or self.robust_points == 1:
|
|
||||||
return np.array([self.nominal_alpha], dtype=float)
|
|
||||||
lo = max(0.0, self.nominal_alpha - self.robust_radius)
|
|
||||||
hi = min(1.0, self.nominal_alpha + self.robust_radius)
|
|
||||||
return np.linspace(lo, hi, self.robust_points)
|
|
||||||
|
|
||||||
def _select_adversarial_alpha(
|
|
||||||
self, prices: np.ndarray
|
|
||||||
) -> tuple[float, dict, list, float]:
|
|
||||||
"""inner robust step: pick worst-case alpha and return its outcome directly to avoid double-sampling"""
|
|
||||||
candidates = self._alpha_candidates()
|
|
||||||
best_alpha, worst_reward = float(candidates[0]), np.inf
|
|
||||||
best_demand, best_trajectories, best_agent_prob = None, [], 0.0
|
|
||||||
for alpha in candidates:
|
|
||||||
self._set_market_mix(float(alpha))
|
|
||||||
demand = self.market.act(prices)
|
|
||||||
trajectories = list(self.market.last_trajectories)
|
|
||||||
agent_prob = self._compute_agent_prob(trajectories)
|
|
||||||
reward, _ = self._compute_reward(prices, demand, agent_prob, trajectories)
|
|
||||||
if reward < worst_reward:
|
|
||||||
worst_reward = reward
|
|
||||||
best_alpha, best_demand, best_trajectories, best_agent_prob = (
|
|
||||||
float(alpha),
|
|
||||||
demand,
|
|
||||||
trajectories,
|
|
||||||
agent_prob,
|
|
||||||
)
|
|
||||||
return best_alpha, best_demand, best_trajectories, best_agent_prob
|
|
||||||
|
|
||||||
def _record_history(self):
|
|
||||||
demand_arr = np.array(
|
|
||||||
[self._demand.get(i, 0.0) for i in range(self.n_products)]
|
|
||||||
)
|
|
||||||
self._demand_history.append(demand_arr)
|
|
||||||
self._price_history.append(self._prices.copy())
|
|
||||||
self._revenue_history.append(np.sum(self._prices * demand_arr))
|
|
||||||
|
|
||||||
def reset(self, seed=None, options=None):
|
|
||||||
super().reset(seed=seed)
|
|
||||||
self._set_market_mix(self.nominal_alpha)
|
|
||||||
self._limbo.reset()
|
|
||||||
self._prices = np.random.uniform(*self.price_bounds, size=self.n_products)
|
|
||||||
self._platform_stub.set_prices(self._prices)
|
|
||||||
self._limbo.step()
|
|
||||||
self._demand = self._limbo.step()
|
|
||||||
self._initial_episode_prices = self._prices.copy()
|
|
||||||
self._step_count = 0
|
|
||||||
self._low_margin_streak = 0
|
|
||||||
self._demand_history, self._price_history, self._revenue_history = [], [], []
|
|
||||||
self._trajectories = list(getattr(self.market, "last_trajectories", []))
|
|
||||||
self._record_history()
|
|
||||||
return self._get_obs(), {}
|
|
||||||
|
|
||||||
def step(self, action):
|
|
||||||
self._prices = self._decode_action(action)
|
|
||||||
# inner robust step returns worst-case outcome directly, no re-sampling
|
|
||||||
alpha_adv, self._demand, trajectories, agent_prob = (
|
|
||||||
self._select_adversarial_alpha(self._prices)
|
|
||||||
)
|
|
||||||
self._set_market_mix(alpha_adv)
|
|
||||||
self._platform_stub.set_prices(self._prices)
|
|
||||||
self._step_count += 1
|
|
||||||
self._trajectories.extend(trajectories)
|
|
||||||
|
|
||||||
reward, metrics = self._compute_reward(
|
|
||||||
self._prices, self._demand, agent_prob, trajectories
|
|
||||||
)
|
|
||||||
self._record_history()
|
|
||||||
|
|
||||||
# soft early termination when margin collapses for too long
|
|
||||||
avg_margin = float(np.mean(self._prices) - self.price_bounds[0]) / max(
|
|
||||||
float(np.mean(self._prices)), 1e-6
|
|
||||||
)
|
|
||||||
if avg_margin < self.margin_floor:
|
|
||||||
self._low_margin_streak += 1
|
|
||||||
else:
|
|
||||||
self._low_margin_streak = 0
|
|
||||||
margin_collapsed = self._low_margin_streak >= self.margin_floor_patience
|
|
||||||
terminated = self._step_count >= self.max_steps or margin_collapsed
|
|
||||||
|
|
||||||
info = {
|
|
||||||
"step": self._step_count,
|
|
||||||
"agent_prob": agent_prob,
|
|
||||||
"alpha_adv": float(alpha_adv),
|
|
||||||
"wasserstein_radius": float(self.robust_radius),
|
|
||||||
**metrics,
|
|
||||||
"raw_revenue": np.sum(
|
|
||||||
self._prices
|
|
||||||
* np.array([self._demand.get(i, 0.0) for i in range(self.n_products)])
|
|
||||||
),
|
|
||||||
}
|
|
||||||
return self._get_obs(), reward, terminated, False, info
|
|
||||||
|
|
||||||
def _compute_elasticity(self) -> np.ndarray:
|
|
||||||
"""point elasticity: e = (dQ/dP) * (P/Q) via finite differences, clipped to [-5, 5]"""
|
|
||||||
if len(self._price_history) < 2:
|
|
||||||
return np.zeros(self.n_products)
|
|
||||||
p, q = np.array(self._price_history), np.array(self._demand_history)
|
|
||||||
dp, dq = np.diff(p, axis=0), np.diff(q, axis=0)
|
|
||||||
valid = np.abs(dp) > 0.5
|
|
||||||
with np.errstate(divide="ignore", invalid="ignore"):
|
|
||||||
elasticity = np.where(
|
|
||||||
valid, (dq / dp) * (p[:-1] / np.maximum(q[:-1], 1.0)), 0.0
|
|
||||||
)
|
|
||||||
elasticity = np.nan_to_num(np.clip(elasticity, -5.0, 5.0), nan=0.0)
|
|
||||||
return (
|
|
||||||
np.mean(elasticity, axis=0)
|
|
||||||
if len(elasticity) > 0
|
|
||||||
else np.zeros(self.n_products)
|
|
||||||
)
|
|
||||||
|
|
||||||
def render(self):
|
|
||||||
if self.render_mode == "human":
|
|
||||||
if self._renderer is None:
|
|
||||||
self._renderer = DashboardRenderer()
|
|
||||||
self._renderer.render(self)
|
|
||||||
elif self.render_mode == "ansi":
|
|
||||||
return (
|
|
||||||
f"step={self._step_count}, prices={self._prices}, demand={self._demand}"
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
if self._renderer:
|
|
||||||
self._renderer.close()
|
|
||||||
self._renderer = None
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import wandb
|
|
||||||
from .lib import MetricsCallback
|
|
||||||
|
|
||||||
class RandomPolicy:
|
|
||||||
"""Minimal SB3-compatible random policy for baseline testing."""
|
|
||||||
|
|
||||||
def __init__(self, env):
|
|
||||||
self.env = env
|
|
||||||
self.num_timesteps = 0
|
|
||||||
|
|
||||||
def learn(self, total_timesteps, callback=None):
|
|
||||||
callback.model = self
|
|
||||||
callback.num_timesteps = 0
|
|
||||||
callback.locals = {}
|
|
||||||
callback.on_training_start({}, {})
|
|
||||||
|
|
||||||
obs, _ = self.env.reset()
|
|
||||||
for step in range(total_timesteps):
|
|
||||||
action = self.env.action_space.sample()
|
|
||||||
obs, reward, term, trunc, info = self.env.step(action)
|
|
||||||
self.num_timesteps = step + 1
|
|
||||||
callback.num_timesteps = self.num_timesteps
|
|
||||||
callback.locals = {"infos": [info]}
|
|
||||||
callback.on_step()
|
|
||||||
if term or trunc:
|
|
||||||
callback.on_rollout_end()
|
|
||||||
obs, _ = self.env.reset()
|
|
||||||
return self
|
|
||||||
|
|
||||||
def predict(self, obs, **kwargs):
|
|
||||||
return self.env.action_space.sample(), None
|
|
||||||
|
|
||||||
wandb.init(project="phantom-pricing", config={"policy": "random", "alpha": 0.3})
|
|
||||||
env = EconomicMetricsWrapper(PHANTOM(n_products=15, alpha=0.3, render_mode=None))
|
|
||||||
|
|
||||||
model = RandomPolicy(env)
|
|
||||||
model.learn(total_timesteps=1000, callback=MetricsCallback())
|
|
||||||
|
|
||||||
print(f"Episode revenue: {env.episode_revenue:.1f}")
|
|
||||||
wandb.finish()
|
|
||||||
env.close()
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
from supabase import create_client, Client
|
|
||||||
import os
|
|
||||||
import random
|
|
||||||
import asyncio
|
|
||||||
import json
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
|
|
||||||
from experiments.agents.agent import get_agent, AgentTypes
|
|
||||||
from lib.kafka_client import get_interactions
|
|
||||||
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
RESULTS="/home/velocitatem/Documents/Projects/PHANTOM/experiments/agents/collected_data/"
|
|
||||||
|
|
||||||
client = create_client(
|
|
||||||
os.getenv("NEXT_PUBLIC_SUPABASE_URL"),
|
|
||||||
os.getenv("NEXT_PUBLIC_SUPABASE_ANON_KEY")
|
|
||||||
)
|
|
||||||
def pick_random_task():
|
|
||||||
mode = 'hotel'
|
|
||||||
tasks = client.table("tasks").select("*").execute().data
|
|
||||||
if mode == 'hotel':
|
|
||||||
# drop all that have 'flight' in the description
|
|
||||||
tasks = [task for task in tasks if 'flight' not in task['task_description'].lower()]
|
|
||||||
return random.choice(tasks) if tasks else None
|
|
||||||
|
|
||||||
def clear_kafka_data():
|
|
||||||
"""Delete and recreate Kafka topics to clear all data"""
|
|
||||||
from kafka.admin import KafkaAdminClient, NewTopic
|
|
||||||
from kafka.errors import UnknownTopicOrPartitionError
|
|
||||||
import time
|
|
||||||
|
|
||||||
kafka_host = os.getenv('KAFKA_HOST', 'localhost')
|
|
||||||
kafka_port = os.getenv('KAFKA_PORT', '9092')
|
|
||||||
broker = f'{kafka_host}:{kafka_port}'
|
|
||||||
|
|
||||||
admin = KafkaAdminClient(bootstrap_servers=broker)
|
|
||||||
topics = ['user-interactions', 'price-logs']
|
|
||||||
|
|
||||||
try:
|
|
||||||
admin.delete_topics(topics, timeout_ms=5000)
|
|
||||||
print(f"Deleted topics: {topics}")
|
|
||||||
time.sleep(2)
|
|
||||||
except UnknownTopicOrPartitionError:
|
|
||||||
print("Topics don't exist, skipping delete")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error deleting topics: {e}")
|
|
||||||
|
|
||||||
new_topics = [
|
|
||||||
NewTopic(name='user-interactions', num_partitions=3, replication_factor=1),
|
|
||||||
NewTopic(name='price-logs', num_partitions=3, replication_factor=1)
|
|
||||||
]
|
|
||||||
|
|
||||||
try:
|
|
||||||
admin.create_topics(new_topics=new_topics, validate_only=False)
|
|
||||||
print(f"Recreated topics: {topics}")
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error creating topics: {e}")
|
|
||||||
finally:
|
|
||||||
admin.close()
|
|
||||||
|
|
||||||
def create_new_experiment(task_id):
|
|
||||||
import uuid
|
|
||||||
subject_name = f"agent_{str(uuid.uuid4())[:8]}"
|
|
||||||
experiment = {
|
|
||||||
"subject_name": subject_name,
|
|
||||||
"xp_human_only": False,
|
|
||||||
"xp_market_mode": "hotel",
|
|
||||||
"xp_task_id": task_id,
|
|
||||||
}
|
|
||||||
response = client.table("experiments").insert(experiment).execute()
|
|
||||||
return response.data[0] if response.data else None
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
clear_kafka_data()
|
|
||||||
|
|
||||||
task = pick_random_task()
|
|
||||||
if not task:
|
|
||||||
print("No tasks available")
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
experiment = create_new_experiment(task['id'])
|
|
||||||
exp_id = experiment['id']
|
|
||||||
exp_dir = f"{RESULTS}{exp_id}"
|
|
||||||
os.makedirs(exp_dir, exist_ok=True)
|
|
||||||
|
|
||||||
# construct experiment URL with uuid param
|
|
||||||
base_url = os.getenv('NEXT_PUBLIC_API_BASE', 'http://localhost:3000')
|
|
||||||
agent_url = f"{base_url}/start-task?uuid={exp_id}"
|
|
||||||
|
|
||||||
print(f"Created experiment {exp_id} for task {task['id']}")
|
|
||||||
print(f"Agent will interact with: {agent_url}")
|
|
||||||
|
|
||||||
# instantiate and run agent
|
|
||||||
agent = get_agent(
|
|
||||||
AgentTypes.GENERIC_BROWSER_USE_AGENT,
|
|
||||||
goal=task['task_description'],
|
|
||||||
url=agent_url,
|
|
||||||
timeout=300,
|
|
||||||
headless=True
|
|
||||||
)
|
|
||||||
|
|
||||||
result = asyncio.run(agent.act())
|
|
||||||
print(f"Agent result: {result}")
|
|
||||||
|
|
||||||
# export interaction and price data from kafka
|
|
||||||
interactions = get_interactions(topic='user-interactions', timeout_ms=3000)
|
|
||||||
prices = get_interactions(topic='price-logs', timeout_ms=3000)
|
|
||||||
|
|
||||||
with open(f"{exp_dir}/int.json", 'w') as f:
|
|
||||||
json.dump(interactions, f, indent=2)
|
|
||||||
|
|
||||||
with open(f"{exp_dir}/price.json", 'w') as f:
|
|
||||||
json.dump(prices, f, indent=2)
|
|
||||||
|
|
||||||
print(f"Experiment {exp_id} completed.")
|
|
||||||
print(f"Exported {len(interactions)} interactions and {len(prices)} price logs to {exp_dir}")
|
|
||||||
@@ -1,269 +0,0 @@
|
|||||||
"""
|
|
||||||
Session-Aware Pricing DAG
|
|
||||||
THIS implements the core pricing computation (policy layer).
|
|
||||||
|
|
||||||
Flow: τ → θ̂ → D → p*
|
|
||||||
1. Fetch recent sessions from Kafka (last 10 active)
|
|
||||||
2. Extract features per session (τ → θ̂)
|
|
||||||
3. Map features to demand proxy (θ̂ → D)
|
|
||||||
4. Compute optimal prices (D → p*)
|
|
||||||
5. Write to Redis session:{sessionId}:prices
|
|
||||||
|
|
||||||
Scheduled: every 1 minute when enabled
|
|
||||||
"""
|
|
||||||
from airflow import DAG
|
|
||||||
from airflow.operators.python import PythonOperator
|
|
||||||
from airflow.utils.dates import days_ago
|
|
||||||
from datetime import timedelta
|
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
import logging
|
|
||||||
import sys
|
|
||||||
import pickle
|
|
||||||
|
|
||||||
sys.path.insert(0, '/opt/airflow')
|
|
||||||
|
|
||||||
from procesing.context import PipelineContext
|
|
||||||
from procesing.providers import SupabaseProvider, BackendAPIProvider
|
|
||||||
from procesing.steps.session import ExtractSessionFeaturesStep
|
|
||||||
from procesing.pricers.simple import SimpleSurgePricer, session_features_to_demand
|
|
||||||
from procesing.pricing import StateSpace
|
|
||||||
from lib.model_registry import ModelRegistry
|
|
||||||
|
|
||||||
DEFAULT_ARGS = {
|
|
||||||
'owner': 'phantom-research',
|
|
||||||
'depends_on_past': False,
|
|
||||||
'email_on_failure': False,
|
|
||||||
'email_on_retry': False,
|
|
||||||
'retries': 1,
|
|
||||||
'retry_delay': timedelta(seconds=30),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class CompositeProvider(SupabaseProvider, BackendAPIProvider):
|
|
||||||
def __init__(self):
|
|
||||||
SupabaseProvider.__init__(self)
|
|
||||||
BackendAPIProvider.__init__(self)
|
|
||||||
|
|
||||||
|
|
||||||
def _get_context(store_mode: str = 'hotel') -> PipelineContext:
|
|
||||||
return PipelineContext(provider=CompositeProvider(), store_mode=store_mode)
|
|
||||||
|
|
||||||
|
|
||||||
def fetch_recent_sessions(**kwargs):
|
|
||||||
"""
|
|
||||||
Task: Fetch last N active sessions from Kafka.
|
|
||||||
Returns: DataFrame of interaction events for recent sessions.
|
|
||||||
"""
|
|
||||||
dag_conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
|
||||||
store_mode = dag_conf.get('store_mode', 'hotel')
|
|
||||||
session_limit = dag_conf.get('session_limit', 10)
|
|
||||||
|
|
||||||
ctx = _get_context(store_mode)
|
|
||||||
provider = ctx.provider
|
|
||||||
|
|
||||||
# fetch all recent interactions from Kafka
|
|
||||||
try:
|
|
||||||
interactions_df = provider.fetch_kafka_topic("user-interactions")
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to fetch interactions: {e}")
|
|
||||||
kwargs['ti'].xcom_push(key='sessions_data', value=pickle.dumps(pd.DataFrame()))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if interactions_df.empty or 'sessionId' not in interactions_df.columns:
|
|
||||||
kwargs['ti'].xcom_push(key='sessions_data', value=pickle.dumps(pd.DataFrame()))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
# identify last N active sessions (most recent by event count)
|
|
||||||
recent_sessions = interactions_df['sessionId'].value_counts().head(session_limit).index.tolist()
|
|
||||||
|
|
||||||
# filter to only those sessions
|
|
||||||
filtered_df = interactions_df[interactions_df['sessionId'].isin(recent_sessions)].copy()
|
|
||||||
|
|
||||||
kwargs['ti'].xcom_push(key='sessions_data', value=pickle.dumps(filtered_df))
|
|
||||||
kwargs['ti'].xcom_push(key='session_ids', value=recent_sessions)
|
|
||||||
|
|
||||||
logging.info(f"Fetched {len(filtered_df)} events for {len(recent_sessions)} sessions")
|
|
||||||
return len(recent_sessions)
|
|
||||||
|
|
||||||
|
|
||||||
def extract_session_features(**kwargs):
|
|
||||||
"""
|
|
||||||
Task: Extract behavioral features from session trajectories.
|
|
||||||
THIS implements τ → θ̂ transformation.
|
|
||||||
"""
|
|
||||||
ti = kwargs['ti']
|
|
||||||
sessions_df = pickle.loads(ti.xcom_pull(key='sessions_data'))
|
|
||||||
|
|
||||||
if sessions_df.empty:
|
|
||||||
ti.xcom_push(key='session_features', value=pickle.dumps(pd.DataFrame()))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
dag_conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
|
||||||
ctx = _get_context(dag_conf.get('store_mode', 'hotel'))
|
|
||||||
|
|
||||||
# extract features using vectorized pipeline
|
|
||||||
feature_extractor = ExtractSessionFeaturesStep(ctx)
|
|
||||||
features_df = feature_extractor.transform(sessions_df)
|
|
||||||
|
|
||||||
ti.xcom_push(key='session_features', value=pickle.dumps(features_df))
|
|
||||||
|
|
||||||
logging.info(f"Extracted {len(features_df.columns)} features for {len(features_df)} sessions")
|
|
||||||
logging.info(f"Feature columns: {list(features_df.columns)}")
|
|
||||||
logging.info(f"Sample features (first session):\n{features_df.iloc[0].to_dict()}")
|
|
||||||
|
|
||||||
return len(features_df)
|
|
||||||
|
|
||||||
|
|
||||||
def compute_session_prices(**kwargs):
|
|
||||||
"""
|
|
||||||
Task: Compute optimal prices for each session.
|
|
||||||
THIS implements θ̂ → D → p* transformation.
|
|
||||||
"""
|
|
||||||
ti = kwargs['ti']
|
|
||||||
features_df = pickle.loads(ti.xcom_pull(key='session_features'))
|
|
||||||
|
|
||||||
if features_df.empty:
|
|
||||||
ti.xcom_push(key='price_results', value=pickle.dumps({}))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
dag_conf = kwargs.get('dag_run').conf if kwargs.get('dag_run') else {}
|
|
||||||
store_mode = dag_conf.get('store_mode', 'hotel')
|
|
||||||
ctx = _get_context(store_mode)
|
|
||||||
|
|
||||||
# fetch product catalog for base prices
|
|
||||||
products_df = ctx.provider.fetch_products(store_mode)
|
|
||||||
if products_df.empty:
|
|
||||||
logging.error("No products found in catalog")
|
|
||||||
ti.xcom_push(key='price_results', value=pickle.dumps({}))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
products_df['base_price'] = products_df['metadata'].apply(
|
|
||||||
lambda m: m.get('base_price', 100.0) if isinstance(m, dict) else 100.0
|
|
||||||
)
|
|
||||||
|
|
||||||
# initialize pricing model
|
|
||||||
pricer = SimpleSurgePricer(
|
|
||||||
high_threshold=dag_conf.get('high_threshold', 10),
|
|
||||||
low_threshold=dag_conf.get('low_threshold', 2),
|
|
||||||
surge_multiplier=dag_conf.get('surge_multiplier', 1.15),
|
|
||||||
discount_multiplier=dag_conf.get('discount_multiplier', 0.95)
|
|
||||||
)
|
|
||||||
pricer.fit(products_df)
|
|
||||||
|
|
||||||
# compute prices per session
|
|
||||||
price_results = {}
|
|
||||||
n_products = len(products_df)
|
|
||||||
|
|
||||||
logging.info(f"Starting price computation for {len(features_df)} sessions, {n_products} products")
|
|
||||||
logging.info(f"Pricer config: high_thresh={pricer.high_threshold}, low_thresh={pricer.low_threshold}, surge_mult={pricer.surge_multiplier}")
|
|
||||||
|
|
||||||
for idx, session_row in features_df.iterrows():
|
|
||||||
session_id = session_row.get('sessionId')
|
|
||||||
if not session_id:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# map features to demand proxy (θ̂ → D)
|
|
||||||
session_features_single = pd.DataFrame([session_row])
|
|
||||||
demand_proxy = session_features_to_demand(session_features_single)
|
|
||||||
|
|
||||||
logging.info(f"[Session {session_id}] Features → Demand: {demand_proxy:.2f}")
|
|
||||||
logging.info(f"[Session {session_id}] Key features: velocity={session_row.get('interaction_velocity', 0):.2f}, cart_ratio={session_row.get('cart_to_view_ratio', 0):.2f}, item_views={session_row.get('item_views', 0)}")
|
|
||||||
|
|
||||||
# build state space
|
|
||||||
state_space = StateSpace(
|
|
||||||
demand=np.full(n_products, demand_proxy), # broadcast session demand to all products
|
|
||||||
prices=products_df['base_price'].values,
|
|
||||||
session_features=session_features_single
|
|
||||||
)
|
|
||||||
|
|
||||||
# compute optimal prices (D → p*)
|
|
||||||
optimal_prices = pricer.predict(state_space)
|
|
||||||
|
|
||||||
base_avg = products_df['base_price'].mean()
|
|
||||||
optimal_avg = optimal_prices.mean()
|
|
||||||
price_change_pct = ((optimal_avg - base_avg) / base_avg) * 100
|
|
||||||
|
|
||||||
logging.info(f"[Session {session_id}] Price adjustment: base_avg={base_avg:.2f}, optimal_avg={optimal_avg:.2f}, change={price_change_pct:+.1f}%")
|
|
||||||
|
|
||||||
# store as dict {productId: price}
|
|
||||||
price_map = {
|
|
||||||
str(products_df.iloc[i]['id']): float(optimal_prices[i])
|
|
||||||
for i in range(n_products)
|
|
||||||
}
|
|
||||||
|
|
||||||
price_results[session_id] = price_map
|
|
||||||
|
|
||||||
ti.xcom_push(key='price_results', value=pickle.dumps(price_results))
|
|
||||||
|
|
||||||
logging.info(f"Computed prices for {len(price_results)} sessions, {n_products} products each")
|
|
||||||
return len(price_results)
|
|
||||||
|
|
||||||
|
|
||||||
def publish_to_registry(**kwargs):
|
|
||||||
"""
|
|
||||||
Task: Write session prices to Redis registry.
|
|
||||||
THIS is the write path: prices → session:{sessionId}:prices
|
|
||||||
"""
|
|
||||||
ti = kwargs['ti']
|
|
||||||
price_results = pickle.loads(ti.xcom_pull(key='price_results'))
|
|
||||||
|
|
||||||
if not price_results:
|
|
||||||
logging.warning("No prices to publish")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
registry = ModelRegistry()
|
|
||||||
ttl = kwargs.get('dag_run').conf.get('ttl', 1800) if kwargs.get('dag_run') and kwargs.get('dag_run').conf else 1800
|
|
||||||
|
|
||||||
published_count = 0
|
|
||||||
for session_id, price_map in price_results.items():
|
|
||||||
registry.set_session_prices(session_id, price_map, ttl=ttl)
|
|
||||||
published_count += 1
|
|
||||||
|
|
||||||
logging.info(f"Published prices for {published_count} sessions to registry (TTL={ttl}s)")
|
|
||||||
|
|
||||||
return {
|
|
||||||
'sessions_published': published_count,
|
|
||||||
'products_per_session': len(next(iter(price_results.values()))) if price_results else 0,
|
|
||||||
'status': 'success'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# DAG definition
|
|
||||||
with DAG(
|
|
||||||
'session_pricing_pipeline',
|
|
||||||
default_args=DEFAULT_ARGS,
|
|
||||||
description='Session-aware pricing: extract features → compute prices → publish to registry',
|
|
||||||
schedule_interval='*/1 * * * *', # every 1 minute
|
|
||||||
start_date=days_ago(1),
|
|
||||||
catchup=False,
|
|
||||||
max_active_runs=1,
|
|
||||||
tags=['pricing', 'session-aware', 'research', 'real-time'],
|
|
||||||
) as dag:
|
|
||||||
|
|
||||||
t_fetch_sessions = PythonOperator(
|
|
||||||
task_id='fetch_recent_sessions',
|
|
||||||
python_callable=fetch_recent_sessions,
|
|
||||||
provide_context=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
t_extract_features = PythonOperator(
|
|
||||||
task_id='extract_session_features',
|
|
||||||
python_callable=extract_session_features,
|
|
||||||
provide_context=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
t_compute_prices = PythonOperator(
|
|
||||||
task_id='compute_session_prices',
|
|
||||||
python_callable=compute_session_prices,
|
|
||||||
provide_context=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
t_publish = PythonOperator(
|
|
||||||
task_id='publish_to_registry',
|
|
||||||
python_callable=publish_to_registry,
|
|
||||||
provide_context=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# linear dependency: fetch → extract → compute → publish
|
|
||||||
t_fetch_sessions >> t_extract_features >> t_compute_prices >> t_publish
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
from pandas.core.algorithms import factorize_array
|
|
||||||
from airflow import DAG
|
from airflow import DAG
|
||||||
from airflow.operators.python import PythonOperator
|
from airflow.operators.python import PythonOperator
|
||||||
from airflow.utils.dates import days_ago
|
from airflow.utils.dates import days_ago
|
||||||
@@ -209,12 +208,3 @@ def create_surge_pricing_dag(store_mode: str) -> DAG:
|
|||||||
# instantiate DAGs for Airflow to discover
|
# instantiate DAGs for Airflow to discover
|
||||||
dag_airline = create_surge_pricing_dag('airline')
|
dag_airline = create_surge_pricing_dag('airline')
|
||||||
dag_hotel = create_surge_pricing_dag('hotel')
|
dag_hotel = create_surge_pricing_dag('hotel')
|
||||||
|
|
||||||
# TODO: Refactor this factory from a surge pricing factory to a general pricing factory
|
|
||||||
# We will do this by passing a pricing strategy class to the factory, since the generic pipeline is:
|
|
||||||
# take all interaction data, group by sessionId and assign a new price vector to each session
|
|
||||||
# in the grouping we get a subset of the interactions per sessionId and we can map that to some Features
|
|
||||||
# we define a custom _get_features(interactions .) methodin the strategy class
|
|
||||||
# we then run only the inference which is the .predict(trajectory) per-session which will give us a new price vector
|
|
||||||
# this we then publish for each sessionId group
|
|
||||||
# this might include no deleting most of the pricers we have defined and starting with a super simple surge-pricing algorithm that is no-fit only predict. This we can then test end-to-end and observe changes to prices according to a desired strategy - we have to define this one as a very short term strategy because we run sessions that take only a few minutes.
|
|
||||||
|
|||||||
@@ -120,31 +120,15 @@ def apply_surge_pricing(**kwargs):
|
|||||||
# rename demand_score to demand for pricer compatibility
|
# rename demand_score to demand for pricer compatibility
|
||||||
data = product_features.rename(columns={'demand_score': 'demand'})
|
data = product_features.rename(columns={'demand_score': 'demand'})
|
||||||
|
|
||||||
high_thresh = dag_conf.get('high_threshold', 10)
|
|
||||||
low_thresh = dag_conf.get('low_threshold', 2)
|
|
||||||
surge_mult = dag_conf.get('surge_multiplier', 1.2)
|
|
||||||
discount_mult = dag_conf.get('discount_multiplier', 0.9)
|
|
||||||
|
|
||||||
logging.info(f"Surge pricing config: high_thresh={high_thresh}, low_thresh={low_thresh}, surge_mult={surge_mult}, discount_mult={discount_mult}")
|
|
||||||
logging.info(f"Demand stats: min={data['demand'].min():.2f}, max={data['demand'].max():.2f}, mean={data['demand'].mean():.2f}")
|
|
||||||
logging.info(f"Products with high demand (>={high_thresh}): {(data['demand'] >= high_thresh).sum()}")
|
|
||||||
logging.info(f"Products with low demand (<={low_thresh}): {(data['demand'] <= low_thresh).sum()}")
|
|
||||||
|
|
||||||
surge_pricer = SimpleSurgePricer(
|
surge_pricer = SimpleSurgePricer(
|
||||||
high_threshold=high_thresh,
|
high_threshold=dag_conf.get('high_threshold', 10),
|
||||||
low_threshold=low_thresh,
|
low_threshold=dag_conf.get('low_threshold', 2),
|
||||||
surge_multiplier=surge_mult,
|
surge_multiplier=dag_conf.get('surge_multiplier', 1.2),
|
||||||
discount_multiplier=discount_mult
|
discount_multiplier=dag_conf.get('discount_multiplier', 0.9)
|
||||||
)
|
)
|
||||||
surge_pricer.fit(data)
|
surge_pricer.fit(data)
|
||||||
data['optimal_price'] = surge_pricer.predict()
|
data['optimal_price'] = surge_pricer.predict()
|
||||||
|
|
||||||
base_avg = data['base_price'].mean()
|
|
||||||
optimal_avg = data['optimal_price'].mean()
|
|
||||||
price_change_pct = ((optimal_avg - base_avg) / base_avg) * 100
|
|
||||||
|
|
||||||
logging.info(f"Price adjustment: base_avg={base_avg:.2f}, optimal_avg={optimal_avg:.2f}, change={price_change_pct:+.1f}%")
|
|
||||||
|
|
||||||
prices_df = data[['productId', 'price', 'base_price', 'optimal_price', 'demand']].rename(columns={
|
prices_df = data[['productId', 'price', 'base_price', 'optimal_price', 'demand']].rename(columns={
|
||||||
'price': 'current_price',
|
'price': 'current_price',
|
||||||
'demand': 'demand_score'
|
'demand': 'demand_score'
|
||||||
|
|||||||
@@ -1,21 +1,11 @@
|
|||||||
from .evals import evaluate
|
from .evals import evaluate
|
||||||
from .arch import (
|
from .arch import (
|
||||||
XGBoostAgentClassifier,
|
XGBoostAgentClassifier,
|
||||||
LightGBMAgentClassifier,
|
LightGBMAgentClassifier
|
||||||
ContrastiveWeakClassifier,
|
|
||||||
TrajectoryEncoder,
|
|
||||||
WeakClassifier,
|
|
||||||
contrastive_loss,
|
|
||||||
featurize_trajectory,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
__all__ = [
|
__all__ =[
|
||||||
'evaluate',
|
'evaluate',
|
||||||
'XGBoostAgentClassifier',
|
'XGBoostAgentClassifier',
|
||||||
'LightGBMAgentClassifier',
|
'LightGBMAgentClassifier'
|
||||||
'ContrastiveWeakClassifier',
|
|
||||||
'TrajectoryEncoder',
|
|
||||||
'WeakClassifier',
|
|
||||||
'contrastive_loss',
|
|
||||||
'featurize_trajectory',
|
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1,212 +1,122 @@
|
|||||||
# sklearn compatible models for agent detection
|
# sklearn compatible models for agent detection
|
||||||
from sklearn.base import BaseEstimator, ClassifierMixin
|
from sklearn.base import BaseEstimator, ClassifierMixin
|
||||||
from typing import Any, Optional, Tuple, Dict, List
|
from procesing.context import PipelineContext
|
||||||
|
from typing import Any, Optional, Tuple
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from collections import defaultdict
|
import xgboost as xgb
|
||||||
|
import lightgbm as lgb
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
# add lib to path for imports
|
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent / 'lib'))
|
|
||||||
from lib.features import (
|
|
||||||
transition_histogram as _lib_transition_histogram,
|
|
||||||
temporal_signature as _lib_temporal_signature,
|
|
||||||
state_coverage as _lib_state_coverage,
|
|
||||||
transition_entropy as _lib_transition_entropy,
|
|
||||||
featurize_trajectory as _lib_featurize_trajectory,
|
|
||||||
parse_timestamp
|
|
||||||
)
|
|
||||||
from lib.state import event_to_state, get_event_name, get_timestamp
|
|
||||||
|
|
||||||
TASK = 'classification'
|
TASK = 'classification'
|
||||||
LABELS = ['human', 'agent']
|
LABELS = ['human', 'agent']
|
||||||
|
|
||||||
|
|
||||||
class WeakClassifier(BaseEstimator, ClassifierMixin, ABC):
|
class BaseAgentClassifier(BaseEstimator, ClassifierMixin, ABC):
|
||||||
# a simple contrastive machine learning model learns to distinguish human/agent behavior
|
"""Base class for tree-based agent detection classifiers with common logic"""
|
||||||
# using weakly supervised contrastive learning + augmentation
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
super().__init__()
|
|
||||||
self.model = None
|
|
||||||
self.kwargs = kwargs
|
|
||||||
|
|
||||||
|
def __init__(self, context: Optional[PipelineContext] = None, n_estimators: int = 200,
|
||||||
class TrajectoryEncoder(nn.Module):
|
max_depth: int = 6, learning_rate: float = 0.05,
|
||||||
"""Encode variable-length event sequences to fixed-dim embedding via bidirectional LSTM"""
|
early_stopping_rounds: int = 20):
|
||||||
def __init__(self, input_dim: int, embed_dim: int = 32, hidden_dim: int = 64):
|
self.context = context
|
||||||
super().__init__()
|
|
||||||
self.event_embed = nn.Linear(input_dim, hidden_dim)
|
|
||||||
self.lstm = nn.LSTM(hidden_dim, hidden_dim, batch_first=True, bidirectional=True)
|
|
||||||
self.proj = nn.Linear(hidden_dim * 2, embed_dim)
|
|
||||||
|
|
||||||
def forward(self, x: torch.Tensor) -> torch.Tensor: # x: (batch, seq_len, input_dim)
|
|
||||||
h = F.relu(self.event_embed(x))
|
|
||||||
_, (hn, _) = self.lstm(h)
|
|
||||||
hn = torch.cat([hn[-2], hn[-1]], dim=1) # concat bidirectional hidden states
|
|
||||||
return F.normalize(self.proj(hn), dim=1) # L2 normalized
|
|
||||||
|
|
||||||
|
|
||||||
class ContrastiveWeakClassifier(WeakClassifier):
|
|
||||||
"""Contrastive learning classifier for human/agent trajectory discrimination"""
|
|
||||||
def __init__(self, input_dim: int = 64, embed_dim: int = 32, margin: float = 1.0, **kwargs):
|
|
||||||
super().__init__(**kwargs)
|
|
||||||
self.input_dim = input_dim
|
|
||||||
self.embed_dim = embed_dim
|
|
||||||
self.margin = margin
|
|
||||||
self.encoder = TrajectoryEncoder(input_dim, embed_dim)
|
|
||||||
self.classifier = nn.Linear(embed_dim, 2)
|
|
||||||
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
||||||
self._fitted = False
|
|
||||||
|
|
||||||
def to_device(self):
|
|
||||||
self.encoder.to(self.device)
|
|
||||||
self.classifier.to(self.device)
|
|
||||||
return self
|
|
||||||
|
|
||||||
def encode(self, x: torch.Tensor) -> torch.Tensor:
|
|
||||||
return self.encoder(x.to(self.device))
|
|
||||||
|
|
||||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
||||||
emb = self.encode(x)
|
|
||||||
return self.classifier(emb)
|
|
||||||
|
|
||||||
def fit(self, X, y=None): # sklearn interface - actual training in weak.train.py
|
|
||||||
self._fitted = True
|
|
||||||
return self
|
|
||||||
|
|
||||||
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
||||||
self.encoder.eval()
|
|
||||||
self.classifier.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
x = torch.tensor(X, dtype=torch.float32).unsqueeze(1).to(self.device)
|
|
||||||
logits = self.forward(x)
|
|
||||||
return torch.argmax(logits, dim=1).cpu().numpy()
|
|
||||||
|
|
||||||
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
|
||||||
self.encoder.eval()
|
|
||||||
self.classifier.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
x = torch.tensor(X, dtype=torch.float32).unsqueeze(1).to(self.device)
|
|
||||||
logits = self.forward(x)
|
|
||||||
return F.softmax(logits, dim=1).cpu().numpy()
|
|
||||||
|
|
||||||
|
|
||||||
def contrastive_loss(anchor: torch.Tensor, positive: torch.Tensor, negative: torch.Tensor, margin: float = 0.3) -> torch.Tensor:
|
|
||||||
"""Triplet loss using cosine similarity (for L2-normalized embeddings). margin in [0,1] range."""
|
|
||||||
pos_sim = F.cosine_similarity(anchor, positive) # higher = more similar
|
|
||||||
neg_sim = F.cosine_similarity(anchor, negative)
|
|
||||||
return F.relu(neg_sim - pos_sim + margin).mean() # want pos_sim > neg_sim + margin
|
|
||||||
|
|
||||||
|
|
||||||
def nt_xent_loss(z_i: torch.Tensor, z_j: torch.Tensor, temperature: float = 0.5) -> torch.Tensor:
|
|
||||||
"""Normalized temperature-scaled cross entropy loss (SimCLR style)"""
|
|
||||||
batch_size = z_i.size(0)
|
|
||||||
z = torch.cat([z_i, z_j], dim=0) # (2N, embed_dim)
|
|
||||||
sim = F.cosine_similarity(z.unsqueeze(1), z.unsqueeze(0), dim=2) / temperature
|
|
||||||
mask = torch.eye(2 * batch_size, dtype=torch.bool, device=z.device)
|
|
||||||
sim.masked_fill_(mask, -float('inf'))
|
|
||||||
labels = torch.arange(batch_size, device=z.device)
|
|
||||||
labels = torch.cat([labels + batch_size, labels]) # positive pairs
|
|
||||||
return F.cross_entropy(sim, labels)
|
|
||||||
|
|
||||||
|
|
||||||
# feature extraction utilities - delegating to lib.features for unified implementation
|
|
||||||
# these wrappers maintain backwards compatibility for existing imports
|
|
||||||
|
|
||||||
def transition_histogram(events: List, state_fn, max_states: int = 50) -> np.ndarray:
|
|
||||||
"""Compute normalized histogram of state transitions in trajectory"""
|
|
||||||
return _lib_transition_histogram(events, state_fn, max_states)
|
|
||||||
|
|
||||||
|
|
||||||
def temporal_signature(events: List, ts_fn) -> np.ndarray:
|
|
||||||
"""Extract temporal features: mean/std/skew of inter-event times"""
|
|
||||||
return _lib_temporal_signature(events, ts_fn)
|
|
||||||
|
|
||||||
|
|
||||||
def state_coverage(events: List, state_fn, mdp_states: set) -> float:
|
|
||||||
"""Fraction of MDP states visited by trajectory"""
|
|
||||||
return _lib_state_coverage(events, state_fn, mdp_states)
|
|
||||||
|
|
||||||
|
|
||||||
def transition_entropy(events: List, state_fn) -> float:
|
|
||||||
"""Compute entropy of transition distribution (randomness of navigation)"""
|
|
||||||
return _lib_transition_entropy(events, state_fn)
|
|
||||||
|
|
||||||
|
|
||||||
def featurize_trajectory(events: List, mdp: Optional[Dict] = None, input_dim: int = 64) -> np.ndarray:
|
|
||||||
"""Convert trajectory to fixed-dim feature vector - uses lib.features implementation"""
|
|
||||||
mdp_states = set(mdp.get('states', [])) if mdp else set()
|
|
||||||
|
|
||||||
def _ts_fn(e):
|
|
||||||
return parse_timestamp(get_timestamp(e))
|
|
||||||
|
|
||||||
def _event_name_fn(e):
|
|
||||||
return get_event_name(e)
|
|
||||||
|
|
||||||
return _lib_featurize_trajectory(events, event_to_state, _ts_fn, _event_name_fn, mdp_states, input_dim)
|
|
||||||
|
|
||||||
|
|
||||||
# gradient boosting classifiers for comparison baselines
|
|
||||||
class XGBoostAgentClassifier(BaseEstimator, ClassifierMixin):
|
|
||||||
"""XGBoost classifier for human/agent detection from session features"""
|
|
||||||
def __init__(self, n_estimators: int = 100, max_depth: int = 6, learning_rate: float = 0.1, **kwargs):
|
|
||||||
self.n_estimators = n_estimators
|
self.n_estimators = n_estimators
|
||||||
self.max_depth = max_depth
|
self.max_depth = max_depth
|
||||||
self.learning_rate = learning_rate
|
self.learning_rate = learning_rate
|
||||||
self.model = None
|
self.early_stopping_rounds = early_stopping_rounds
|
||||||
self.kwargs = kwargs
|
self.model_ = None
|
||||||
|
self.feature_names_ = None
|
||||||
|
|
||||||
|
def _to_array(self, X):
|
||||||
|
"""Convert pandas structures to numpy arrays"""
|
||||||
|
return X.values if isinstance(X, (pd.DataFrame, pd.Series)) else X
|
||||||
|
|
||||||
|
def _compute_pos_weight(self, y_arr):
|
||||||
|
"""Calculate scale_pos_weight for class imbalance handling"""
|
||||||
|
n_neg, n_pos = (y_arr == 0).sum(), (y_arr == 1).sum()
|
||||||
|
return n_neg / n_pos if n_pos > 0 else 1.0
|
||||||
|
|
||||||
|
def _prepare_eval_set(self, eval_set):
|
||||||
|
"""Convert eval_set to numpy arrays if needed"""
|
||||||
|
if not eval_set:
|
||||||
|
return None
|
||||||
|
X_val, y_val = eval_set[0]
|
||||||
|
return [(self._to_array(X_val), self._to_array(y_val))]
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _build_model(self, scale_pos: float):
|
||||||
|
"""Build the underlying model instance (must be implemented by subclasses)"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def _fit_with_eval(self, X_arr, y_arr, eval_arr):
|
||||||
|
"""Fit model with evaluation set (must be implemented by subclasses)"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def fit(self, X, y, eval_set=None):
|
||||||
|
X_arr, y_arr = self._to_array(X), self._to_array(y)
|
||||||
|
|
||||||
|
if isinstance(X, pd.DataFrame):
|
||||||
|
self.feature_names_ = X.columns.tolist()
|
||||||
|
|
||||||
|
scale_pos = self._compute_pos_weight(y_arr)
|
||||||
|
self.model_ = self._build_model(scale_pos)
|
||||||
|
|
||||||
|
eval_arr = self._prepare_eval_set(eval_set)
|
||||||
|
if eval_arr:
|
||||||
|
self._fit_with_eval(X_arr, y_arr, eval_arr)
|
||||||
|
else:
|
||||||
|
self.model_.fit(X_arr, y_arr)
|
||||||
|
|
||||||
def fit(self, X: np.ndarray, y: np.ndarray):
|
|
||||||
try:
|
|
||||||
import xgboost as xgb
|
|
||||||
self.model = xgb.XGBClassifier(n_estimators=self.n_estimators, max_depth=self.max_depth,
|
|
||||||
learning_rate=self.learning_rate, **self.kwargs)
|
|
||||||
self.model.fit(X, y)
|
|
||||||
except ImportError:
|
|
||||||
raise ImportError("xgboost required for XGBoostAgentClassifier")
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def predict(self, X: np.ndarray) -> np.ndarray:
|
def predict(self, X):
|
||||||
if self.model is None:
|
return self.model_.predict(self._to_array(X))
|
||||||
raise ValueError("fit the model first")
|
|
||||||
return self.model.predict(X)
|
|
||||||
|
|
||||||
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
def predict_proba(self, X):
|
||||||
if self.model is None:
|
return self.model_.predict_proba(self._to_array(X))
|
||||||
raise ValueError("fit the model first")
|
|
||||||
return self.model.predict_proba(X)
|
@property
|
||||||
|
def feature_importances_(self):
|
||||||
|
return self.model_.feature_importances_ if self.model_ else None
|
||||||
|
|
||||||
|
|
||||||
class LightGBMAgentClassifier(BaseEstimator, ClassifierMixin):
|
class XGBoostAgentClassifier(BaseAgentClassifier):
|
||||||
"""LightGBM classifier for human/agent detection from session features"""
|
"""XGBoost binary classifier for agent detection with class imbalance handling"""
|
||||||
def __init__(self, n_estimators: int = 100, max_depth: int = -1, learning_rate: float = 0.1, **kwargs):
|
|
||||||
self.n_estimators = n_estimators
|
|
||||||
self.max_depth = max_depth
|
|
||||||
self.learning_rate = learning_rate
|
|
||||||
self.model = None
|
|
||||||
self.kwargs = kwargs
|
|
||||||
|
|
||||||
def fit(self, X: np.ndarray, y: np.ndarray):
|
def _build_model(self, scale_pos: float):
|
||||||
try:
|
return xgb.XGBClassifier(
|
||||||
import lightgbm as lgb
|
n_estimators=self.n_estimators,
|
||||||
self.model = lgb.LGBMClassifier(n_estimators=self.n_estimators, max_depth=self.max_depth,
|
max_depth=self.max_depth,
|
||||||
learning_rate=self.learning_rate, verbose=-1, **self.kwargs)
|
learning_rate=self.learning_rate,
|
||||||
self.model.fit(X, y)
|
scale_pos_weight=scale_pos,
|
||||||
except ImportError:
|
eval_metric='auc',
|
||||||
raise ImportError("lightgbm required for LightGBMAgentClassifier")
|
early_stopping_rounds=self.early_stopping_rounds,
|
||||||
return self
|
random_state=42,
|
||||||
|
tree_method='hist',
|
||||||
|
enable_categorical=False
|
||||||
|
)
|
||||||
|
|
||||||
def predict(self, X: np.ndarray) -> np.ndarray:
|
def _fit_with_eval(self, X_arr, y_arr, eval_arr):
|
||||||
if self.model is None:
|
self.model_.fit(X_arr, y_arr, eval_set=eval_arr, verbose=False)
|
||||||
raise ValueError("fit the model first")
|
|
||||||
return self.model.predict(X)
|
|
||||||
|
|
||||||
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
|
||||||
if self.model is None:
|
class LightGBMAgentClassifier(BaseAgentClassifier):
|
||||||
raise ValueError("fit the model first")
|
"""LightGBM binary classifier for agent detection with class imbalance handling"""
|
||||||
return self.model.predict_proba(X)
|
|
||||||
|
def _build_model(self, scale_pos: float):
|
||||||
|
return lgb.LGBMClassifier(
|
||||||
|
n_estimators=self.n_estimators,
|
||||||
|
max_depth=self.max_depth,
|
||||||
|
learning_rate=self.learning_rate,
|
||||||
|
scale_pos_weight=scale_pos,
|
||||||
|
metric='auc',
|
||||||
|
random_state=42,
|
||||||
|
verbosity=-1
|
||||||
|
)
|
||||||
|
|
||||||
|
def _fit_with_eval(self, X_arr, y_arr, eval_arr):
|
||||||
|
self.model_.fit(
|
||||||
|
X_arr, y_arr,
|
||||||
|
eval_set=eval_arr,
|
||||||
|
callbacks=[lgb.early_stopping(self.early_stopping_rounds, verbose=False)]
|
||||||
|
)
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
from .encoder import Window, extract_windows, build_windows, WindowDataset, PrototypeClassifier, train, loocv
|
|
||||||
@@ -1,210 +0,0 @@
|
|||||||
"""Contrastive encoder via trajectory windowing. Classification by prototype distance."""
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, "/home/velocitatem/Documents/Projects/PHANTOM/sim/rl/behavior_loader")
|
|
||||||
sys.path.insert(0, "/home/velocitatem/Documents/Projects/PHANTOM/experiments/ml")
|
|
||||||
|
|
||||||
from sim.rl.behavior_loader.loader import JointLoader, PayloadModel
|
|
||||||
from arch import TrajectoryEncoder, featurize_trajectory, nt_xent_loss
|
|
||||||
from typing import List, Dict, Tuple
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from datetime import datetime
|
|
||||||
import numpy as np, torch, torch.nn.functional as F, random, optuna
|
|
||||||
from torch.utils.data import Dataset, DataLoader
|
|
||||||
from torch.optim import Adam
|
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
|
||||||
|
|
||||||
RUNS = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/ml/runs"
|
|
||||||
AGENT_DIR = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/agents/collected_data/"
|
|
||||||
HUMAN_DIR = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/collected_data/"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Window:
|
|
||||||
events: List[PayloadModel]
|
|
||||||
traj_id: str
|
|
||||||
label: int # 0=human, 1=agent
|
|
||||||
|
|
||||||
|
|
||||||
def extract_windows(events: List[PayloadModel], traj_id: str, label: int,
|
|
||||||
sizes: List[int] = [5, 10, 15], stride: int = 2) -> List[Window]:
|
|
||||||
"""Multi-scale overlapping windows from trajectory"""
|
|
||||||
n = len(events)
|
|
||||||
wins = [Window(events[i:i+s], traj_id, label) for s in sizes if n >= s for i in range(0, n-s+1, stride)]
|
|
||||||
if n >= 3: wins.append(Window(events, traj_id, label)) # full traj
|
|
||||||
return wins
|
|
||||||
|
|
||||||
|
|
||||||
def build_windows(data: Dict[str, List], sizes=[5,10,15], stride=2) -> List[Window]:
|
|
||||||
return [w for tid, evts in data.items()
|
|
||||||
for w in extract_windows(evts, tid, 0 if tid.startswith('human_') else 1, sizes, stride)]
|
|
||||||
|
|
||||||
|
|
||||||
class WindowDataset(Dataset):
|
|
||||||
"""Yields (anchor, positive) pairs from same class"""
|
|
||||||
def __init__(self, windows: List[Window], dim: int = 64):
|
|
||||||
self.wins, self.dim = windows, dim
|
|
||||||
self.by_label = {0: [i for i,w in enumerate(windows) if w.label==0],
|
|
||||||
1: [i for i,w in enumerate(windows) if w.label==1]}
|
|
||||||
self.by_traj = {}
|
|
||||||
for i, w in enumerate(windows): self.by_traj.setdefault(w.traj_id, []).append(i)
|
|
||||||
|
|
||||||
def __len__(self): return len(self.wins)
|
|
||||||
|
|
||||||
def _feat(self, evts): return featurize_trajectory(evts, None, self.dim)
|
|
||||||
|
|
||||||
def _aug(self, evts): # subsample 70-100%
|
|
||||||
if len(evts) < 4: return evts
|
|
||||||
k = max(3, int(len(evts) * random.uniform(0.7, 1.0)))
|
|
||||||
start = random.randint(0, len(evts) - k)
|
|
||||||
return evts[start:start+k]
|
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
|
||||||
w = self.wins[idx]
|
|
||||||
pool = [i for i in self.by_label[w.label] if self.wins[i].traj_id != w.traj_id]
|
|
||||||
pos_idx = random.choice(pool) if pool else idx
|
|
||||||
a = torch.tensor(self._feat(self._aug(w.events)), dtype=torch.float32)
|
|
||||||
p = torch.tensor(self._feat(self._aug(self.wins[pos_idx].events)), dtype=torch.float32)
|
|
||||||
return a, p, w.label
|
|
||||||
|
|
||||||
|
|
||||||
class PrototypeClassifier:
|
|
||||||
"""Classify by distance to class centroids"""
|
|
||||||
def __init__(self, encoder: TrajectoryEncoder, device = 'cuda', dim=64):
|
|
||||||
self.enc, self.dev, self.dim = encoder, device, dim
|
|
||||||
self.centroids = {0: None, 1: None}
|
|
||||||
|
|
||||||
def fit(self, windows: List[Window]):
|
|
||||||
self.enc.eval()
|
|
||||||
embs = {0: [], 1: []}
|
|
||||||
with torch.no_grad():
|
|
||||||
for w in windows:
|
|
||||||
x = torch.tensor(featurize_trajectory(w.events, None, self.dim), dtype=torch.float32)
|
|
||||||
z = self.enc(x.unsqueeze(0).unsqueeze(1).to(self.dev))
|
|
||||||
embs[w.label].append(z)
|
|
||||||
self.centroids = {k: torch.cat(v).mean(0, keepdim=True) if v else None for k, v in embs.items()}
|
|
||||||
return self
|
|
||||||
|
|
||||||
def predict(self, events: List[PayloadModel]) -> Tuple[int, float, Dict]:
|
|
||||||
"""Returns (pred, confidence, debug). Confidence via softmax over -distances."""
|
|
||||||
self.enc.eval()
|
|
||||||
with torch.no_grad():
|
|
||||||
x = torch.tensor(featurize_trajectory(events, None, self.dim), dtype=torch.float32)
|
|
||||||
z = self.enc(x.unsqueeze(0).unsqueeze(1).to(self.dev))
|
|
||||||
dists = {k: torch.norm(z - c, dim=1).item() for k, c in self.centroids.items() if c is not None}
|
|
||||||
if not dists: return 0, 0.0, {'d': {}, 'p': [0.5, 0.5]}
|
|
||||||
pred = min(dists, key=dists.get)
|
|
||||||
d0, d1 = dists.get(0, 1e6), dists.get(1, 1e6) # softmax(-d) gives higher prob to closer centroid
|
|
||||||
probs = F.softmax(torch.tensor([[-d0, -d1]]), dim=1).squeeze()
|
|
||||||
return pred, probs[pred].item(), {'d': dists, 'p': probs.tolist()}
|
|
||||||
|
|
||||||
|
|
||||||
def train(epochs=200, lr=5e-4, batch=16, dim=64, emb=32, temp=0.5,
|
|
||||||
sizes=[5,10,15], stride=2, name=None, verbose=True):
|
|
||||||
data = JointLoader(HUMAN_DIR, AGENT_DIR).get_data()
|
|
||||||
wins = build_windows(data, sizes, stride)
|
|
||||||
if verbose: print(f"Windows: {len(wins)} ({sum(w.label==0 for w in wins)}h/{sum(w.label==1 for w in wins)}a)")
|
|
||||||
|
|
||||||
dev = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
||||||
enc = TrajectoryEncoder(dim, emb).to(dev)
|
|
||||||
opt = Adam(enc.parameters(), lr=lr)
|
|
||||||
loader = DataLoader(WindowDataset(wins, dim), batch_size=batch, shuffle=True, drop_last=True)
|
|
||||||
|
|
||||||
name = name or f"enc_{dim}_{emb}_{datetime.now():%Y%m%d_%H%M%S}"
|
|
||||||
writer = SummaryWriter(f"{RUNS}/encoder/{name}")
|
|
||||||
|
|
||||||
for ep in range(epochs):
|
|
||||||
enc.train()
|
|
||||||
total, n = 0.0, 0
|
|
||||||
for a, p, _ in loader:
|
|
||||||
loss = nt_xent_loss(enc(a.unsqueeze(1).to(dev)), enc(p.unsqueeze(1).to(dev)), temp)
|
|
||||||
opt.zero_grad(); loss.backward(); opt.step()
|
|
||||||
total += loss.item(); n += 1
|
|
||||||
avg = total / max(n, 1)
|
|
||||||
writer.add_scalar('loss-ntxent', avg, ep)
|
|
||||||
if verbose and (ep+1) % 20 == 0: print(f"Epoch {ep+1}: {avg:.4f}")
|
|
||||||
|
|
||||||
writer.close()
|
|
||||||
return enc, wins, dev
|
|
||||||
|
|
||||||
|
|
||||||
def loocv(epochs=100, lr=5e-4, dim=64, emb=32, temp=0.5, sizes=[5,10,15], stride=2, verbose=True):
|
|
||||||
"""Leave-one-trajectory-out CV"""
|
|
||||||
data = JointLoader(HUMAN_DIR, AGENT_DIR).get_data()
|
|
||||||
dev = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
||||||
results = []
|
|
||||||
|
|
||||||
for test_id in data:
|
|
||||||
train_data = {k: v for k, v in data.items() if k != test_id}
|
|
||||||
if not any(k.startswith('human_') for k in train_data) or not any(k.startswith('agent_') for k in train_data):
|
|
||||||
continue
|
|
||||||
|
|
||||||
wins = build_windows(train_data, sizes, stride)
|
|
||||||
enc = TrajectoryEncoder(dim, emb).to(dev)
|
|
||||||
opt = Adam(enc.parameters(), lr=lr)
|
|
||||||
loader = DataLoader(WindowDataset(wins, dim), batch_size=min(16, len(wins)//2 or 1),
|
|
||||||
shuffle=True, drop_last=len(wins)>2)
|
|
||||||
|
|
||||||
for _ in range(epochs):
|
|
||||||
enc.train()
|
|
||||||
for a, p, _ in loader:
|
|
||||||
loss = nt_xent_loss(enc(a.unsqueeze(1).to(dev)), enc(p.unsqueeze(1).to(dev)), temp)
|
|
||||||
opt.zero_grad(); loss.backward(); opt.step()
|
|
||||||
|
|
||||||
clf = PrototypeClassifier(enc, dev, dim).fit(wins)
|
|
||||||
pred, conf, dbg = clf.predict(data[test_id])
|
|
||||||
actual = 0 if test_id.startswith('human_') else 1
|
|
||||||
results.append((pred, actual, conf))
|
|
||||||
if verbose: print(f"{test_id[:18]}: pred={pred} conf={conf:.2f} actual={actual} {'OK' if pred==actual else 'MISS'}")
|
|
||||||
|
|
||||||
if results:
|
|
||||||
acc = sum(p==a for p,a,_ in results) / len(results)
|
|
||||||
if verbose: print(f"\nAccuracy: {acc:.1%} ({sum(p==a for p,a,_ in results)}/{len(results)})")
|
|
||||||
return acc, results
|
|
||||||
return 0.0, []
|
|
||||||
|
|
||||||
|
|
||||||
def hparam_tune(n_trials=50, epochs=60, n_jobs=2, verbose=True):
|
|
||||||
"""Optuna hyperparameter search maximizing LOOCV accuracy"""
|
|
||||||
def objective(trial):
|
|
||||||
lr = trial.suggest_float('lr', 1e-5, 1e-2, log=True)
|
|
||||||
dim = trial.suggest_categorical('dim', [32, 64, 128, 256])
|
|
||||||
emb = trial.suggest_categorical('emb', [16, 32, 64, 128])
|
|
||||||
temp = trial.suggest_float('temp', 0.05, 1.0)
|
|
||||||
stride = trial.suggest_int('stride', 1, 4)
|
|
||||||
sizes = [trial.suggest_int(f's{i}', 3, 20) for i in range(3)]
|
|
||||||
sizes = sorted(set(sizes)) # unique sorted
|
|
||||||
acc, _ = loocv(epochs, lr, dim, emb, temp, sizes, stride, verbose=False)
|
|
||||||
return acc
|
|
||||||
|
|
||||||
study = optuna.create_study(direction='maximize', study_name='encoder_hparam',
|
|
||||||
sampler=optuna.samplers.TPESampler(seed=42))
|
|
||||||
study.optimize(objective, n_trials=n_trials, n_jobs=n_jobs, show_progress_bar=verbose)
|
|
||||||
|
|
||||||
best = study.best_params
|
|
||||||
if verbose:
|
|
||||||
print(f"\nBest accuracy: {study.best_value:.1%}")
|
|
||||||
print(f"Best params: {best}")
|
|
||||||
return best, study
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import argparse
|
|
||||||
p = argparse.ArgumentParser()
|
|
||||||
p.add_argument('--mode', choices=['train', 'eval', 'hparam'], default='train')
|
|
||||||
p.add_argument('--epochs', type=int, default=200)
|
|
||||||
p.add_argument('--lr', type=float, default=5e-4)
|
|
||||||
p.add_argument('--dim', type=int, default=128)
|
|
||||||
p.add_argument('--emb', type=int, default=64)
|
|
||||||
p.add_argument('--temp', type=float, default=0.1)
|
|
||||||
p.add_argument('--sizes', type=str, default='5,10,15')
|
|
||||||
p.add_argument('--stride', type=int, default=2)
|
|
||||||
p.add_argument('--n_trials', type=int, default=50)
|
|
||||||
args = p.parse_args()
|
|
||||||
sizes = [int(x) for x in args.sizes.split(',')]
|
|
||||||
|
|
||||||
if args.mode == 'train':
|
|
||||||
enc, wins, dev = train(args.epochs, args.lr, 16, args.dim, args.emb, args.temp, sizes, args.stride)
|
|
||||||
elif args.mode == 'hparam':
|
|
||||||
best, study = hparam_tune(args.n_trials, min(args.epochs, 60))
|
|
||||||
else:
|
|
||||||
loocv(args.epochs, args.lr, args.dim, args.emb, args.temp, sizes, args.stride)
|
|
||||||
@@ -1,246 +0,0 @@
|
|||||||
import sys
|
|
||||||
sys.path.insert(0, "/home/velocitatem/Documents/Projects/PHANTOM/sim/rl/behavior_loader")
|
|
||||||
sys.path.insert(0, "/home/velocitatem/Documents/Projects/PHANTOM/experiments/ml")
|
|
||||||
|
|
||||||
from sim.rl.behavior_loader.loader import AgentLoader, Loader, JointLoader, PayloadModel
|
|
||||||
from sim.rl.behavior_loader.models import JointBehaviorModel
|
|
||||||
from arch import ContrastiveWeakClassifier, contrastive_loss, featurize_trajectory
|
|
||||||
from typing import List, Optional, Dict
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from copy import deepcopy
|
|
||||||
import numpy as np
|
|
||||||
import random
|
|
||||||
import torch
|
|
||||||
from torch.utils.data import Dataset, DataLoader
|
|
||||||
from torch.optim import Adam
|
|
||||||
from torch.utils.tensorboard import SummaryWriter
|
|
||||||
|
|
||||||
RUNS_DIR = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/ml/runs"
|
|
||||||
agent_dir = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/agents/collected_data/"
|
|
||||||
human_dir = "/home/velocitatem/Documents/Projects/PHANTOM/experiments/collected_data/"
|
|
||||||
|
|
||||||
|
|
||||||
def _perturb_ts(evt: PayloadModel, jitter_ms: int = 500) -> PayloadModel:
|
|
||||||
"""Add random jitter to event timestamp"""
|
|
||||||
new_evt = deepcopy(evt)
|
|
||||||
try:
|
|
||||||
ts = datetime.fromisoformat(evt.ts.replace('Z', '+00:00'))
|
|
||||||
delta = timedelta(milliseconds=random.randint(-jitter_ms, jitter_ms))
|
|
||||||
new_evt.ts = (ts + delta).isoformat()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
return new_evt
|
|
||||||
|
|
||||||
|
|
||||||
def augment_trajectory(trajectory: List[PayloadModel], rate: float = 0.1) -> List[PayloadModel]:
|
|
||||||
"""Apply random augmentation to trajectory for contrastive learning"""
|
|
||||||
if len(trajectory) < 2:
|
|
||||||
return trajectory
|
|
||||||
|
|
||||||
aug_type = random.choice(['window', 'shuffle', 'noise', 'drop'])
|
|
||||||
|
|
||||||
if aug_type == 'window': # random contiguous sub-sequence (70-100% length)
|
|
||||||
min_len = max(2, int(len(trajectory) * 0.7))
|
|
||||||
sub_len = random.randint(min_len, len(trajectory))
|
|
||||||
start = random.randint(0, len(trajectory) - sub_len)
|
|
||||||
return trajectory[start:start + sub_len]
|
|
||||||
|
|
||||||
elif aug_type == 'shuffle': # swap adjacent pairs with probability rate
|
|
||||||
result = list(trajectory)
|
|
||||||
for i in range(len(result) - 1):
|
|
||||||
if random.random() < rate:
|
|
||||||
result[i], result[i + 1] = result[i + 1], result[i]
|
|
||||||
return result
|
|
||||||
|
|
||||||
elif aug_type == 'drop': # drop events with probability rate
|
|
||||||
result = [e for e in trajectory if random.random() > rate]
|
|
||||||
return result if len(result) >= 2 else trajectory[:2]
|
|
||||||
|
|
||||||
elif aug_type == 'noise': # perturb timestamps
|
|
||||||
return [_perturb_ts(e, jitter_ms=500) for e in trajectory]
|
|
||||||
|
|
||||||
return trajectory
|
|
||||||
|
|
||||||
|
|
||||||
class TripletDataset(Dataset):
|
|
||||||
"""Generate (anchor, positive, negative) triplets on-the-fly with augmentation"""
|
|
||||||
def __init__(self, data: Dict[str, List[PayloadModel]], mdp: Optional[Dict], augment_fn, input_dim: int = 64, multiplier: int = 10):
|
|
||||||
self.sessions = list(data.items())
|
|
||||||
self.human_ids = [i for i, (sid, _) in enumerate(self.sessions) if sid.startswith('human_')]
|
|
||||||
self.agent_ids = [i for i, (sid, _) in enumerate(self.sessions) if sid.startswith('agent_')]
|
|
||||||
self.mdp = mdp
|
|
||||||
self.augment = augment_fn
|
|
||||||
self.input_dim = input_dim
|
|
||||||
self.multiplier = multiplier
|
|
||||||
|
|
||||||
if not self.human_ids or not self.agent_ids:
|
|
||||||
raise ValueError(f"Need both human ({len(self.human_ids)}) and agent ({len(self.agent_ids)}) sessions")
|
|
||||||
|
|
||||||
def __len__(self) -> int:
|
|
||||||
return len(self.sessions) * self.multiplier
|
|
||||||
|
|
||||||
def __getitem__(self, idx: int):
|
|
||||||
anchor_idx = idx % len(self.sessions)
|
|
||||||
sid, events = self.sessions[anchor_idx]
|
|
||||||
is_human = sid.startswith('human_')
|
|
||||||
|
|
||||||
anchor = featurize_trajectory(events, self.mdp, self.input_dim)
|
|
||||||
positive = featurize_trajectory(self.augment(events), self.mdp, self.input_dim)
|
|
||||||
|
|
||||||
neg_pool = self.agent_ids if is_human else self.human_ids
|
|
||||||
neg_idx = random.choice(neg_pool)
|
|
||||||
negative = featurize_trajectory(self.sessions[neg_idx][1], self.mdp, self.input_dim)
|
|
||||||
|
|
||||||
label = 0 if is_human else 1 # 0=human, 1=agent
|
|
||||||
return (torch.tensor(anchor, dtype=torch.float32),
|
|
||||||
torch.tensor(positive, dtype=torch.float32),
|
|
||||||
torch.tensor(negative, dtype=torch.float32),
|
|
||||||
torch.tensor(label, dtype=torch.long))
|
|
||||||
|
|
||||||
|
|
||||||
def train(epochs: int = 100, lr: float = 1e-3, batch_size: int = 4, input_dim: int = 64,
|
|
||||||
embed_dim: int = 32, margin: float = 0.3, verbose: bool = True, run_name: str = None):
|
|
||||||
"""Train contrastive weak classifier on human/agent trajectories"""
|
|
||||||
joint = JointLoader(human_dir, agent_dir)
|
|
||||||
data = joint.get_data()
|
|
||||||
if verbose:
|
|
||||||
print(f"Loaded {len(data)} sessions")
|
|
||||||
|
|
||||||
joint_model = JointBehaviorModel(human_dir, agent_dir)
|
|
||||||
ref_mdp = joint_model.build_MDP()
|
|
||||||
|
|
||||||
dataset = TripletDataset(data, ref_mdp, augment_trajectory, input_dim=input_dim)
|
|
||||||
loader = DataLoader(dataset, batch_size=batch_size, shuffle=True, drop_last=True)
|
|
||||||
|
|
||||||
model = ContrastiveWeakClassifier(input_dim=input_dim, embed_dim=embed_dim, margin=margin)
|
|
||||||
model.to_device()
|
|
||||||
|
|
||||||
run_name = run_name or f"d{input_dim}_e{embed_dim}_lr{lr}_m{margin}_{datetime.now():%Y%m%d_%H%M%S}"
|
|
||||||
writer = SummaryWriter(f"{RUNS_DIR}/train/{run_name}")
|
|
||||||
|
|
||||||
optimizer = Adam(list(model.encoder.parameters()) + list(model.classifier.parameters()), lr=lr)
|
|
||||||
ce_loss_fn = torch.nn.CrossEntropyLoss()
|
|
||||||
|
|
||||||
best_loss = float('inf')
|
|
||||||
for epoch in range(epochs):
|
|
||||||
model.encoder.train()
|
|
||||||
model.classifier.train()
|
|
||||||
total_loss, n_batches = 0.0, 0
|
|
||||||
|
|
||||||
for anchor, positive, negative, labels in loader:
|
|
||||||
anchor, positive, negative, labels = [t.to(model.device) for t in [anchor, positive, negative, labels]]
|
|
||||||
z_a, z_p, z_n = [model.encoder(t.unsqueeze(1)) for t in [anchor, positive, negative]]
|
|
||||||
|
|
||||||
trip_loss = contrastive_loss(z_a, z_p, z_n, margin=model.margin)
|
|
||||||
ce = ce_loss_fn(model.classifier(z_a), labels)
|
|
||||||
loss = trip_loss + 0.5 * ce
|
|
||||||
|
|
||||||
optimizer.zero_grad()
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
total_loss += loss.item()
|
|
||||||
n_batches += 1
|
|
||||||
|
|
||||||
avg_loss = total_loss / max(n_batches, 1)
|
|
||||||
writer.add_scalar('loss', avg_loss, epoch)
|
|
||||||
|
|
||||||
if verbose and (epoch + 1) % 10 == 0:
|
|
||||||
print(f"Epoch {epoch+1}/{epochs}: loss={avg_loss:.4f}")
|
|
||||||
if avg_loss < best_loss:
|
|
||||||
best_loss = avg_loss
|
|
||||||
|
|
||||||
writer.close()
|
|
||||||
if verbose:
|
|
||||||
print(f"Done. Best={best_loss:.4f} TB:{RUNS_DIR}/train/{run_name}")
|
|
||||||
|
|
||||||
return model, ref_mdp
|
|
||||||
|
|
||||||
|
|
||||||
def evaluate_loocv(input_dim: int = 64, embed_dim: int = 32, epochs_per_fold: int = 50,
|
|
||||||
lr: float = 1e-3, margin: float = 0.3, run_name: str = None):
|
|
||||||
"""Leave-one-out cross-validation given limited samples"""
|
|
||||||
joint = JointLoader(human_dir, agent_dir)
|
|
||||||
data = joint.get_data()
|
|
||||||
session_ids = list(data.keys())
|
|
||||||
|
|
||||||
joint_model = JointBehaviorModel(human_dir, agent_dir)
|
|
||||||
ref_mdp = joint_model.build_MDP()
|
|
||||||
|
|
||||||
run_name = run_name or f"loocv_d{input_dim}_e{embed_dim}_m{margin}_{datetime.now():%Y%m%d_%H%M%S}"
|
|
||||||
writer = SummaryWriter(f"{RUNS_DIR}/eval/{run_name}")
|
|
||||||
|
|
||||||
predictions, actuals = [], []
|
|
||||||
|
|
||||||
for fold_idx, test_sid in enumerate(session_ids):
|
|
||||||
train_data = {k: v for k, v in data.items() if k != test_sid}
|
|
||||||
test_events = data[test_sid]
|
|
||||||
test_label = 0 if test_sid.startswith('human_') else 1
|
|
||||||
|
|
||||||
n_human = sum(1 for k in train_data if k.startswith('human_'))
|
|
||||||
n_agent = sum(1 for k in train_data if k.startswith('agent_'))
|
|
||||||
if n_human == 0 or n_agent == 0:
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
|
||||||
dataset = TripletDataset(train_data, ref_mdp, augment_trajectory, input_dim=input_dim, multiplier=5)
|
|
||||||
loader = DataLoader(dataset, batch_size=2, shuffle=True, drop_last=True)
|
|
||||||
|
|
||||||
model = ContrastiveWeakClassifier(input_dim=input_dim, embed_dim=embed_dim, margin=margin)
|
|
||||||
model.to_device()
|
|
||||||
optimizer = Adam(list(model.encoder.parameters()) + list(model.classifier.parameters()), lr=lr)
|
|
||||||
|
|
||||||
model.encoder.train()
|
|
||||||
model.classifier.train()
|
|
||||||
for _ in range(epochs_per_fold):
|
|
||||||
for anchor, positive, negative, labels in loader:
|
|
||||||
z_a, z_p, z_n = [model.encoder(t.unsqueeze(1).to(model.device)) for t in [anchor, positive, negative]]
|
|
||||||
loss = contrastive_loss(z_a, z_p, z_n, margin=margin)
|
|
||||||
optimizer.zero_grad()
|
|
||||||
loss.backward()
|
|
||||||
optimizer.step()
|
|
||||||
|
|
||||||
test_feat = featurize_trajectory(test_events, ref_mdp, input_dim)
|
|
||||||
pred = model.predict(test_feat.reshape(1, -1))[0]
|
|
||||||
predictions.append(pred)
|
|
||||||
actuals.append(test_label)
|
|
||||||
print(f" {test_sid[:12]}...: pred={pred}, actual={test_label}, {'OK' if pred == test_label else 'MISS'}")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error: {e}")
|
|
||||||
|
|
||||||
if predictions:
|
|
||||||
acc = sum(p == a for p, a in zip(predictions, actuals)) / len(predictions)
|
|
||||||
tp = sum(1 for p, a in zip(predictions, actuals) if p == 1 and a == 1)
|
|
||||||
fp = sum(1 for p, a in zip(predictions, actuals) if p == 1 and a == 0)
|
|
||||||
fn = sum(1 for p, a in zip(predictions, actuals) if p == 0 and a == 1)
|
|
||||||
prec, rec = tp / max(tp + fp, 1), tp / max(tp + fn, 1)
|
|
||||||
f1 = 2 * prec * rec / max(prec + rec, 1e-10)
|
|
||||||
writer.add_scalar('accuracy', acc, 0)
|
|
||||||
writer.add_scalar('f1', f1, 0)
|
|
||||||
writer.add_scalar('precision', prec, 0)
|
|
||||||
writer.add_scalar('recall', rec, 0)
|
|
||||||
writer.close()
|
|
||||||
print(f"\nAccuracy: {acc:.2%} F1: {f1:.3f} TB:{RUNS_DIR}/eval/{run_name}")
|
|
||||||
return acc, predictions, actuals
|
|
||||||
writer.close()
|
|
||||||
return 0.0, [], []
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import argparse
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('--mode', choices=['train', 'eval'], default='train')
|
|
||||||
parser.add_argument('--epochs', type=int, default=100)
|
|
||||||
parser.add_argument('--lr', type=float, default=1e-3)
|
|
||||||
parser.add_argument('--margin', type=float, default=0.3)
|
|
||||||
parser.add_argument('--input-dim', type=int, default=64)
|
|
||||||
parser.add_argument('--embed-dim', type=int, default=32)
|
|
||||||
parser.add_argument('--run-name', type=str, default=None)
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
if args.mode == 'train':
|
|
||||||
model, mdp = train(epochs=args.epochs, lr=args.lr, input_dim=args.input_dim,
|
|
||||||
embed_dim=args.embed_dim, margin=args.margin, run_name=args.run_name)
|
|
||||||
else:
|
|
||||||
evaluate_loocv(input_dim=args.input_dim, embed_dim=args.embed_dim, epochs_per_fold=args.epochs,
|
|
||||||
lr=args.lr, margin=args.margin, run_name=args.run_name)
|
|
||||||
@@ -1,957 +0,0 @@
|
|||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 10,
|
|
||||||
"id": "62eafcd9-5462-4063-8873-0e7fb9add907",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"True"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 10,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"from kafka import KafkaConsumer\n",
|
|
||||||
"import pandas as pd\n",
|
|
||||||
"import json\n",
|
|
||||||
"import numpy as np\n",
|
|
||||||
"import os\n",
|
|
||||||
"from dotenv import load_dotenv\n",
|
|
||||||
"import matplotlib.pyplot as plt\n",
|
|
||||||
"from IPython.display import display, SVG, Image\n",
|
|
||||||
"load_dotenv()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 11,
|
|
||||||
"id": "4af65cb4-e8cf-4877-b2db-13ac19f3838f",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"<class 'pandas.core.frame.DataFrame'>\n",
|
|
||||||
"RangeIndex: 73 entries, 0 to 72\n",
|
|
||||||
"Data columns (total 13 columns):\n",
|
|
||||||
" # Column Non-Null Count Dtype \n",
|
|
||||||
"--- ------ -------------- ----- \n",
|
|
||||||
" 0 sessionId 73 non-null object \n",
|
|
||||||
" 1 eventName 73 non-null object \n",
|
|
||||||
" 2 page 73 non-null object \n",
|
|
||||||
" 3 productId 67 non-null object \n",
|
|
||||||
" 4 storeMode 73 non-null object \n",
|
|
||||||
" 5 userAgent 73 non-null object \n",
|
|
||||||
" 6 ts 73 non-null object \n",
|
|
||||||
" 7 metadata_referrer 6 non-null object \n",
|
|
||||||
" 8 metadata_roomType 45 non-null object \n",
|
|
||||||
" 9 metadata_price 45 non-null float64\n",
|
|
||||||
" 10 metadata_nights 45 non-null float64\n",
|
|
||||||
" 11 metadata_elementText 22 non-null object \n",
|
|
||||||
" 12 metadata_dwellTime 22 non-null float64\n",
|
|
||||||
"dtypes: float64(3), object(10)\n",
|
|
||||||
"memory usage: 7.5+ KB\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"KAFKA_PORT=os.getenv(\"KAFKA_PORT\", 9092)\n",
|
|
||||||
"topic = \"user-interactions\"\n",
|
|
||||||
"consumer = KafkaConsumer(\n",
|
|
||||||
" topic, \n",
|
|
||||||
" enable_auto_commit=True,\n",
|
|
||||||
" value_deserializer=lambda x: json.loads(x.decode('utf-8')),\n",
|
|
||||||
" auto_offset_reset='earliest', \n",
|
|
||||||
" bootstrap_servers=['localhost:9092'])\n",
|
|
||||||
"messages=consumer.poll(timeout_ms=1000,max_records=10000)\n",
|
|
||||||
"df = []\n",
|
|
||||||
"for m in messages.values():\n",
|
|
||||||
" for i in m:\n",
|
|
||||||
" df.append(i.value)\n",
|
|
||||||
"df = pd.DataFrame(df)\n",
|
|
||||||
"# explode metadata col json\n",
|
|
||||||
"df = df.join(pd.json_normalize(df.pop(\"metadata\"), sep=\".\").add_prefix(\"metadata_\"))\n",
|
|
||||||
"df.info()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 12,
|
|
||||||
"id": "f6819a1c-32ab-49c7-845b-5df7bf60f561",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/html": [
|
|
||||||
"<div>\n",
|
|
||||||
"<style scoped>\n",
|
|
||||||
" .dataframe tbody tr th:only-of-type {\n",
|
|
||||||
" vertical-align: middle;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe tbody tr th {\n",
|
|
||||||
" vertical-align: top;\n",
|
|
||||||
" }\n",
|
|
||||||
"\n",
|
|
||||||
" .dataframe thead th {\n",
|
|
||||||
" text-align: right;\n",
|
|
||||||
" }\n",
|
|
||||||
"</style>\n",
|
|
||||||
"<table border=\"1\" class=\"dataframe\">\n",
|
|
||||||
" <thead>\n",
|
|
||||||
" <tr style=\"text-align: right;\">\n",
|
|
||||||
" <th></th>\n",
|
|
||||||
" <th>sessionId</th>\n",
|
|
||||||
" <th>eventName</th>\n",
|
|
||||||
" <th>page</th>\n",
|
|
||||||
" <th>productId</th>\n",
|
|
||||||
" <th>storeMode</th>\n",
|
|
||||||
" <th>userAgent</th>\n",
|
|
||||||
" <th>ts</th>\n",
|
|
||||||
" <th>metadata_referrer</th>\n",
|
|
||||||
" <th>metadata_roomType</th>\n",
|
|
||||||
" <th>metadata_price</th>\n",
|
|
||||||
" <th>metadata_nights</th>\n",
|
|
||||||
" <th>metadata_elementText</th>\n",
|
|
||||||
" <th>metadata_dwellTime</th>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </thead>\n",
|
|
||||||
" <tbody>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>0</th>\n",
|
|
||||||
" <td>d176d7c9-4027-4702-9e31-2a71395cdda0</td>\n",
|
|
||||||
" <td>page_view</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:23:46.270Z</td>\n",
|
|
||||||
" <td></td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>1</th>\n",
|
|
||||||
" <td>f0317a5d-e424-44e9-b784-c8f7291ffe31</td>\n",
|
|
||||||
" <td>page_view</td>\n",
|
|
||||||
" <td>/</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck...</td>\n",
|
|
||||||
" <td>2025-11-14T13:26:00.291Z</td>\n",
|
|
||||||
" <td></td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>2</th>\n",
|
|
||||||
" <td>f0317a5d-e424-44e9-b784-c8f7291ffe31</td>\n",
|
|
||||||
" <td>page_view</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck...</td>\n",
|
|
||||||
" <td>2025-11-14T13:26:07.769Z</td>\n",
|
|
||||||
" <td></td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>3</th>\n",
|
|
||||||
" <td>f0317a5d-e424-44e9-b784-c8f7291ffe31</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck...</td>\n",
|
|
||||||
" <td>2025-11-14T13:26:15.010Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Premium Room</td>\n",
|
|
||||||
" <td>269.0</td>\n",
|
|
||||||
" <td>1.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>4</th>\n",
|
|
||||||
" <td>238dc588-a7ab-4c0e-bccd-6abca5076c66</td>\n",
|
|
||||||
" <td>page_view</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7...</td>\n",
|
|
||||||
" <td>2025-11-14T13:27:15.457Z</td>\n",
|
|
||||||
" <td></td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>5</th>\n",
|
|
||||||
" <td>238dc588-a7ab-4c0e-bccd-6abca5076c66</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7...</td>\n",
|
|
||||||
" <td>2025-11-14T13:27:15.591Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Premium Room</td>\n",
|
|
||||||
" <td>264.0</td>\n",
|
|
||||||
" <td>2.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>432</th>\n",
|
|
||||||
" <td>214d9fad-9b00-40c3-bd0e-7739b6acd654</td>\n",
|
|
||||||
" <td>click</td>\n",
|
|
||||||
" <td>1762448192425</td>\n",
|
|
||||||
" <td>DIV</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>/</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>1623.0</td>\n",
|
|
||||||
" <td>493.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>6</th>\n",
|
|
||||||
" <td>238dc588-a7ab-4c0e-bccd-6abca5076c66</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7...</td>\n",
|
|
||||||
" <td>2025-11-14T13:27:21.483Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Premium Room</td>\n",
|
|
||||||
" <td>264.0</td>\n",
|
|
||||||
" <td>2.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>7</th>\n",
|
|
||||||
" <td>238dc588-a7ab-4c0e-bccd-6abca5076c66</td>\n",
|
|
||||||
" <td>hover_over_title</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7...</td>\n",
|
|
||||||
" <td>2025-11-14T13:27:22.646Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Grand Plaza Hotel</td>\n",
|
|
||||||
" <td>1200.0</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>8</th>\n",
|
|
||||||
" <td>238dc588-a7ab-4c0e-bccd-6abca5076c66</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7...</td>\n",
|
|
||||||
" <td>2025-11-14T13:27:25.889Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Premium Room</td>\n",
|
|
||||||
" <td>264.0</td>\n",
|
|
||||||
" <td>2.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>35</th>\n",
|
|
||||||
" <td>013fc334-4045-4d5a-8739-dd0a8766a63b</td>\n",
|
|
||||||
" <td>page_view</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>None</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:53:59.993Z</td>\n",
|
|
||||||
" <td></td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>36</th>\n",
|
|
||||||
" <td>013fc334-4045-4d5a-8739-dd0a8766a63b</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:54:10.705Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Premium Room</td>\n",
|
|
||||||
" <td>223.0</td>\n",
|
|
||||||
" <td>3.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>37</th>\n",
|
|
||||||
" <td>013fc334-4045-4d5a-8739-dd0a8766a63b</td>\n",
|
|
||||||
" <td>hover_over_title</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-0</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:54:11.771Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>416.0</td>\n",
|
|
||||||
" <td>397.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Grand Plaza Hotel</td>\n",
|
|
||||||
" <td>1200.0</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>38</th>\n",
|
|
||||||
" <td>013fc334-4045-4d5a-8739-dd0a8766a63b</td>\n",
|
|
||||||
" <td>view_item_page</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-1</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:54:29.772Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Standard Room</td>\n",
|
|
||||||
" <td>267.0</td>\n",
|
|
||||||
" <td>5.0</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" <tr>\n",
|
|
||||||
" <th>39</th>\n",
|
|
||||||
" <td>013fc334-4045-4d5a-8739-dd0a8766a63b</td>\n",
|
|
||||||
" <td>hover_over_title</td>\n",
|
|
||||||
" <td>/products</td>\n",
|
|
||||||
" <td>htl-1</td>\n",
|
|
||||||
" <td>hotel</td>\n",
|
|
||||||
" <td>Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53...</td>\n",
|
|
||||||
" <td>2025-11-14T13:54:30.833Z</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>NaN</td>\n",
|
|
||||||
" <td>Seaside Resort</td>\n",
|
|
||||||
" <td>1200.0</td>\n",
|
|
||||||
" </tr>\n",
|
|
||||||
" </tbody>\n",
|
|
||||||
"</table>\n",
|
|
||||||
"</div>"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
" sessionId eventName page \\\n",
|
|
||||||
"0 d176d7c9-4027-4702-9e31-2a71395cdda0 page_view /products \n",
|
|
||||||
"1 f0317a5d-e424-44e9-b784-c8f7291ffe31 page_view / \n",
|
|
||||||
"2 f0317a5d-e424-44e9-b784-c8f7291ffe31 page_view /products \n",
|
|
||||||
"3 f0317a5d-e424-44e9-b784-c8f7291ffe31 view_item_page /products \n",
|
|
||||||
"4 238dc588-a7ab-4c0e-bccd-6abca5076c66 page_view /products \n",
|
|
||||||
"5 238dc588-a7ab-4c0e-bccd-6abca5076c66 view_item_page /products \n",
|
|
||||||
"6 238dc588-a7ab-4c0e-bccd-6abca5076c66 view_item_page /products \n",
|
|
||||||
"7 238dc588-a7ab-4c0e-bccd-6abca5076c66 hover_over_title /products \n",
|
|
||||||
"8 238dc588-a7ab-4c0e-bccd-6abca5076c66 view_item_page /products \n",
|
|
||||||
"35 013fc334-4045-4d5a-8739-dd0a8766a63b page_view /products \n",
|
|
||||||
"36 013fc334-4045-4d5a-8739-dd0a8766a63b view_item_page /products \n",
|
|
||||||
"37 013fc334-4045-4d5a-8739-dd0a8766a63b hover_over_title /products \n",
|
|
||||||
"38 013fc334-4045-4d5a-8739-dd0a8766a63b view_item_page /products \n",
|
|
||||||
"39 013fc334-4045-4d5a-8739-dd0a8766a63b hover_over_title /products \n",
|
|
||||||
"\n",
|
|
||||||
" productId storeMode userAgent \\\n",
|
|
||||||
"0 None hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"1 None hotel Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck... \n",
|
|
||||||
"2 None hotel Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck... \n",
|
|
||||||
"3 htl-0 hotel Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Geck... \n",
|
|
||||||
"4 None hotel Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7... \n",
|
|
||||||
"5 htl-0 hotel Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7... \n",
|
|
||||||
"6 htl-0 hotel Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7... \n",
|
|
||||||
"7 htl-0 hotel Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7... \n",
|
|
||||||
"8 htl-0 hotel Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7... \n",
|
|
||||||
"35 None hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"36 htl-0 hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"37 htl-0 hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"38 htl-1 hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"39 htl-1 hotel Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53... \n",
|
|
||||||
"\n",
|
|
||||||
" ts metadata_referrer metadata_roomType \\\n",
|
|
||||||
"0 2025-11-14T13:23:46.270Z NaN \n",
|
|
||||||
"1 2025-11-14T13:26:00.291Z NaN \n",
|
|
||||||
"2 2025-11-14T13:26:07.769Z NaN \n",
|
|
||||||
"3 2025-11-14T13:26:15.010Z NaN Premium Room \n",
|
|
||||||
"4 2025-11-14T13:27:15.457Z NaN \n",
|
|
||||||
"5 2025-11-14T13:27:15.591Z NaN Premium Room \n",
|
|
||||||
"6 2025-11-14T13:27:21.483Z NaN Premium Room \n",
|
|
||||||
"7 2025-11-14T13:27:22.646Z NaN NaN \n",
|
|
||||||
"8 2025-11-14T13:27:25.889Z NaN Premium Room \n",
|
|
||||||
"35 2025-11-14T13:53:59.993Z NaN \n",
|
|
||||||
"36 2025-11-14T13:54:10.705Z NaN Premium Room \n",
|
|
||||||
"37 2025-11-14T13:54:11.771Z NaN NaN \n",
|
|
||||||
"38 2025-11-14T13:54:29.772Z NaN Standard Room \n",
|
|
||||||
"39 2025-11-14T13:54:30.833Z NaN NaN \n",
|
|
||||||
"\n",
|
|
||||||
" metadata_price metadata_nights metadata_elementText metadata_dwellTime \n",
|
|
||||||
"0 NaN NaN NaN NaN \n",
|
|
||||||
"1 NaN NaN NaN NaN \n",
|
|
||||||
"2 NaN NaN NaN NaN \n",
|
|
||||||
"3 269.0 1.0 NaN NaN \n",
|
|
||||||
"4 NaN NaN NaN NaN \n",
|
|
||||||
"5 264.0 2.0 NaN NaN \n",
|
|
||||||
"6 264.0 2.0 NaN NaN \n",
|
|
||||||
"7 NaN NaN Grand Plaza Hotel 1200.0 \n",
|
|
||||||
"8 264.0 2.0 NaN NaN \n",
|
|
||||||
"35 NaN NaN NaN NaN \n",
|
|
||||||
"36 223.0 3.0 NaN NaN \n",
|
|
||||||
"37 NaN NaN Grand Plaza Hotel 1200.0 \n",
|
|
||||||
"38 267.0 5.0 NaN NaN \n",
|
|
||||||
"39 NaN NaN Seaside Resort 1200.0 "
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 12,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"df.groupby('sessionId').head()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 13,
|
|
||||||
"id": "380eca5f-8304-4fb2-be32-e8bcfd312085",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"text/plain": [
|
|
||||||
"['013fc334-4045-4d5a-8739-dd0a8766a63b',\n",
|
|
||||||
" '238dc588-a7ab-4c0e-bccd-6abca5076c66',\n",
|
|
||||||
" 'd176d7c9-4027-4702-9e31-2a71395cdda0',\n",
|
|
||||||
" 'f0317a5d-e424-44e9-b784-c8f7291ffe31']"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"execution_count": 13,
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "execute_result"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"sessions = list(set(df['sessionId'])); sessions # 238dc588-a7ab-4c0e-bccd-6abca5076c66"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 14,
|
|
||||||
"id": "f4ae6f81-dcb8-44be-aee7-30dbc3a6bae1",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# map sessions to experiments"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 15,
|
|
||||||
"id": "050d90a4-20a9-47f5-b998-c31178a54cb3",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"def build_transition_prob_matrix(df: pd.DataFrame):\n",
|
|
||||||
" df = df.dropna(subset=['eventName'])\n",
|
|
||||||
" events = df['eventName'].tolist()\n",
|
|
||||||
" labels = pd.Index(events).unique().tolist()\n",
|
|
||||||
" idx = {e:i for i,e in enumerate(labels)}\n",
|
|
||||||
" M = np.zeros((len(labels), len(labels)), dtype=float)\n",
|
|
||||||
" for a, b in zip(events, events[1:]):\n",
|
|
||||||
" M[idx[a], idx[b]] += 1\n",
|
|
||||||
" row_sums = M.sum(axis=1, keepdims=True)\n",
|
|
||||||
" with np.errstate(divide='ignore', invalid='ignore'):\n",
|
|
||||||
" P = np.divide(M, row_sums, where=row_sums>0) # row-normalized\n",
|
|
||||||
" return P, labels"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 16,
|
|
||||||
"id": "e68f9004-82f5-4826-aece-e3dc6e15a18f",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# https://medium.com/data-science/time-series-data-markov-transition-matrices-7060771e362b\n",
|
|
||||||
"from graphviz import Digraph\n",
|
|
||||||
"import numpy as np\n",
|
|
||||||
"import pandas as pd\n",
|
|
||||||
"\n",
|
|
||||||
"def _as_prob_df(matrix, labels=None):\n",
|
|
||||||
" \"\"\"Return a square DataFrame with index=columns=labels.\"\"\"\n",
|
|
||||||
" if isinstance(matrix, pd.DataFrame):\n",
|
|
||||||
" # Ensure square and aligned\n",
|
|
||||||
" assert (matrix.index == matrix.columns).all(), \"Index/columns must match.\"\n",
|
|
||||||
" return matrix\n",
|
|
||||||
" matrix = np.asarray(matrix, dtype=float)\n",
|
|
||||||
" assert matrix.shape[0] == matrix.shape[1], \"Matrix must be square.\"\n",
|
|
||||||
" if labels is None:\n",
|
|
||||||
" raise ValueError(\"labels are required when matrix is not a DataFrame\")\n",
|
|
||||||
" assert len(labels) == matrix.shape[0], \"labels length must match matrix size.\"\n",
|
|
||||||
" return pd.DataFrame(matrix, index=list(labels), columns=list(labels))\n",
|
|
||||||
"\n",
|
|
||||||
"def _df_to_edgelist(P: pd.DataFrame, threshold=0.0, round_digits=2):\n",
|
|
||||||
" \"\"\"Build weighted edges > threshold.\"\"\"\n",
|
|
||||||
" edges = []\n",
|
|
||||||
" for src in P.index:\n",
|
|
||||||
" for dst in P.columns:\n",
|
|
||||||
" w = float(P.loc[src, dst])\n",
|
|
||||||
" if w > threshold:\n",
|
|
||||||
" edges.append((str(src), str(dst), f\"{w:.{round_digits}f}\"))\n",
|
|
||||||
" return edges\n",
|
|
||||||
"\n",
|
|
||||||
"def render_graph(fname, matrix, ls_index=None, threshold=0.0, fmt=\"svg\", view=False):\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" fname: output file stem (no extension)\n",
|
|
||||||
" matrix: NumPy array or pandas DataFrame of transition PROBABILITIES\n",
|
|
||||||
" ls_index: ordered labels (required if matrix is not a DataFrame)\n",
|
|
||||||
" threshold: hide edges with weight <= threshold\n",
|
|
||||||
" fmt: 'svg'|'png'|'pdf' etc.\n",
|
|
||||||
" view: open after rendering\n",
|
|
||||||
" \"\"\"\n",
|
|
||||||
" P = _as_prob_df(matrix, labels=ls_index)\n",
|
|
||||||
" edges = _df_to_edgelist(P, threshold=threshold)\n",
|
|
||||||
"\n",
|
|
||||||
" g = Digraph(format=fmt)\n",
|
|
||||||
" g.attr(rankdir=\"LR\", size=\"30\")\n",
|
|
||||||
" g.attr(\"node\", shape=\"circle\")\n",
|
|
||||||
"\n",
|
|
||||||
" # ensure isolated nodes appear\n",
|
|
||||||
" for node in P.index:\n",
|
|
||||||
" g.node(str(node), width=\"1\", height=\"1\")\n",
|
|
||||||
"\n",
|
|
||||||
" for src, dst, label in edges:\n",
|
|
||||||
" g.edge(src, dst, label=label)\n",
|
|
||||||
"\n",
|
|
||||||
" g.render(fname, view=view, cleanup=True)\n",
|
|
||||||
" return g\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 17,
|
|
||||||
"id": "e255a2c1-6454-4e5e-89f6-ef8ac51ab6cc",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"013fc334-4045-4d5a-8739-dd0a8766a63b\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"image/svg+xml": [
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
|
||||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
|
||||||
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
|
||||||
"<!-- Generated by graphviz version 13.1.2 (0)\n",
|
|
||||||
" -->\n",
|
|
||||||
"<!-- Pages: 1 -->\n",
|
|
||||||
"<svg width=\"565pt\" height=\"354pt\"\n",
|
|
||||||
" viewBox=\"0.00 0.00 565.00 354.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
|
||||||
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 349.64)\">\n",
|
|
||||||
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-349.64 561.05,-349.64 561.05,4 -4,4\"/>\n",
|
|
||||||
"<!-- page_view -->\n",
|
|
||||||
"<g id=\"node1\" class=\"node\">\n",
|
|
||||||
"<title>page_view</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"48.19\" cy=\"-235.83\" rx=\"48.19\" ry=\"48.19\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"48.19\" y=\"-231.16\" font-family=\"Times,serif\" font-size=\"14.00\">page_view</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page -->\n",
|
|
||||||
"<g id=\"node2\" class=\"node\">\n",
|
|
||||||
"<title>view_item_page</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"232.88\" cy=\"-235.83\" rx=\"69.01\" ry=\"69.01\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-231.16\" font-family=\"Times,serif\" font-size=\"14.00\">view_item_page</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- page_view->view_item_page -->\n",
|
|
||||||
"<g id=\"edge1\" class=\"edge\">\n",
|
|
||||||
"<title>page_view->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M96.71,-235.83C113.69,-235.83 133.31,-235.83 152.25,-235.83\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"152.1,-239.33 162.1,-235.83 152.1,-232.33 152.1,-239.33\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"130.12\" y=\"-239.78\" font-family=\"Times,serif\" font-size=\"14.00\">1.00</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->view_item_page -->\n",
|
|
||||||
"<g id=\"edge2\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M214.74,-302.59C217.1,-314.51 223.14,-322.84 232.88,-322.84 239.27,-322.84 244.07,-319.26 247.28,-313.42\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"250.57,-314.62 250.52,-304.02 243.95,-312.33 250.57,-314.62\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-326.79\" font-family=\"Times,serif\" font-size=\"14.00\">0.68</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_title -->\n",
|
|
||||||
"<g id=\"node3\" class=\"node\">\n",
|
|
||||||
"<title>hover_over_title</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"463.22\" cy=\"-275.83\" rx=\"69.81\" ry=\"69.81\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"463.22\" y=\"-271.16\" font-family=\"Times,serif\" font-size=\"14.00\">hover_over_title</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->hover_over_title -->\n",
|
|
||||||
"<g id=\"edge3\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->hover_over_title</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M300.48,-250.14C307.03,-251.43 313.58,-252.69 319.89,-253.83 340.12,-257.51 362.05,-261.1 382.5,-264.27\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"381.77,-267.7 392.19,-265.76 382.83,-260.78 381.77,-267.7\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-263.17\" font-family=\"Times,serif\" font-size=\"14.00\">0.29</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_paragraph -->\n",
|
|
||||||
"<g id=\"node4\" class=\"node\">\n",
|
|
||||||
"<title>hover_over_paragraph</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"463.22\" cy=\"-93.83\" rx=\"93.83\" ry=\"93.83\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"463.22\" y=\"-89.16\" font-family=\"Times,serif\" font-size=\"14.00\">hover_over_paragraph</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->hover_over_paragraph -->\n",
|
|
||||||
"<g id=\"edge4\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->hover_over_paragraph</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M292.09,-199.63C316.79,-184.27 346.14,-166.02 373.44,-149.04\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"375.08,-152.15 381.72,-143.89 371.38,-146.2 375.08,-152.15\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-185.68\" font-family=\"Times,serif\" font-size=\"14.00\">0.04</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_title->view_item_page -->\n",
|
|
||||||
"<g id=\"edge5\" class=\"edge\">\n",
|
|
||||||
"<title>hover_over_title->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M399.53,-246.73C384.12,-240.88 367.42,-235.6 351.39,-232.58 339.13,-230.28 326.03,-229.26 313.19,-229.04\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"313.51,-225.54 303.51,-229.04 313.51,-232.54 313.51,-225.54\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-236.53\" font-family=\"Times,serif\" font-size=\"14.00\">1.00</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</svg>\n"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<graphviz.graphs.Digraph at 0x7f0779e818b0>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"[]\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"image/svg+xml": [
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
|
||||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
|
||||||
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
|
||||||
"<!-- Generated by graphviz version 13.1.2 (0)\n",
|
|
||||||
" -->\n",
|
|
||||||
"<!-- Pages: 1 -->\n",
|
|
||||||
"<svg width=\"8pt\" height=\"8pt\"\n",
|
|
||||||
" viewBox=\"0.00 0.00 8.00 8.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
|
||||||
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 4)\">\n",
|
|
||||||
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-4 4,-4 4,4 -4,4\"/>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</svg>\n"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<graphviz.graphs.Digraph at 0x7f6800fac980>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"[[0.00000000e+000 1.00000000e+000 0.00000000e+000 0.00000000e+000]\n",
|
|
||||||
" [0.00000000e+000 6.78571429e-001 2.85714286e-001 3.57142857e-002]\n",
|
|
||||||
" [0.00000000e+000 1.00000000e+000 0.00000000e+000 0.00000000e+000]\n",
|
|
||||||
" [2.05833592e-312 2.29175545e-312 4.94065646e-324 6.92110218e-310]]\n",
|
|
||||||
"238dc588-a7ab-4c0e-bccd-6abca5076c66\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"image/svg+xml": [
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
|
||||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
|
||||||
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
|
||||||
"<!-- Generated by graphviz version 13.1.2 (0)\n",
|
|
||||||
" -->\n",
|
|
||||||
"<!-- Pages: 1 -->\n",
|
|
||||||
"<svg width=\"565pt\" height=\"354pt\"\n",
|
|
||||||
" viewBox=\"0.00 0.00 565.00 354.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
|
||||||
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 349.64)\">\n",
|
|
||||||
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-349.64 561.05,-349.64 561.05,4 -4,4\"/>\n",
|
|
||||||
"<!-- page_view -->\n",
|
|
||||||
"<g id=\"node1\" class=\"node\">\n",
|
|
||||||
"<title>page_view</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"48.19\" cy=\"-109.83\" rx=\"48.19\" ry=\"48.19\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"48.19\" y=\"-105.16\" font-family=\"Times,serif\" font-size=\"14.00\">page_view</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page -->\n",
|
|
||||||
"<g id=\"node2\" class=\"node\">\n",
|
|
||||||
"<title>view_item_page</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"232.88\" cy=\"-197.83\" rx=\"69.01\" ry=\"69.01\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-193.16\" font-family=\"Times,serif\" font-size=\"14.00\">view_item_page</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- page_view->view_item_page -->\n",
|
|
||||||
"<g id=\"edge1\" class=\"edge\">\n",
|
|
||||||
"<title>page_view->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M92.02,-130.47C112.32,-140.25 137.13,-152.2 160.18,-163.3\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"158.39,-166.32 168.92,-167.51 161.43,-160.02 158.39,-166.32\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"130.12\" y=\"-157.78\" font-family=\"Times,serif\" font-size=\"14.00\">1.00</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->view_item_page -->\n",
|
|
||||||
"<g id=\"edge2\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M214.74,-264.59C217.1,-276.51 223.14,-284.84 232.88,-284.84 239.27,-284.84 244.07,-281.26 247.28,-275.42\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"250.57,-276.62 250.52,-266.02 243.95,-274.33 250.57,-276.62\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-288.79\" font-family=\"Times,serif\" font-size=\"14.00\">0.19</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_title -->\n",
|
|
||||||
"<g id=\"node3\" class=\"node\">\n",
|
|
||||||
"<title>hover_over_title</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"463.22\" cy=\"-275.83\" rx=\"69.81\" ry=\"69.81\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"463.22\" y=\"-271.16\" font-family=\"Times,serif\" font-size=\"14.00\">hover_over_title</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->hover_over_title -->\n",
|
|
||||||
"<g id=\"edge3\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->hover_over_title</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M289.6,-237.16C299.36,-242.77 309.67,-247.94 319.89,-251.83 339.45,-259.28 361.4,-264.43 382.1,-267.98\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"381.52,-271.43 391.95,-269.55 382.62,-264.52 381.52,-271.43\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-265.16\" font-family=\"Times,serif\" font-size=\"14.00\">0.38</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_paragraph -->\n",
|
|
||||||
"<g id=\"node4\" class=\"node\">\n",
|
|
||||||
"<title>hover_over_paragraph</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"463.22\" cy=\"-93.83\" rx=\"93.83\" ry=\"93.83\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"463.22\" y=\"-89.16\" font-family=\"Times,serif\" font-size=\"14.00\">hover_over_paragraph</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page->hover_over_paragraph -->\n",
|
|
||||||
"<g id=\"edge4\" class=\"edge\">\n",
|
|
||||||
"<title>view_item_page->hover_over_paragraph</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M300.22,-180.71C317.22,-175.46 335.24,-169.12 351.39,-161.83 358.97,-158.41 366.67,-154.57 374.29,-150.49\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"375.84,-153.63 382.92,-145.75 372.47,-147.5 375.84,-153.63\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-178.15\" font-family=\"Times,serif\" font-size=\"14.00\">0.44</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_title->view_item_page -->\n",
|
|
||||||
"<g id=\"edge5\" class=\"edge\">\n",
|
|
||||||
"<title>hover_over_title->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M398.52,-248.36C383.21,-242.16 366.82,-235.87 351.39,-230.58 338.42,-226.15 324.5,-221.86 310.94,-217.93\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"312.2,-214.65 301.62,-215.28 310.28,-221.39 312.2,-214.65\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-234.53\" font-family=\"Times,serif\" font-size=\"14.00\">1.00</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_paragraph->page_view -->\n",
|
|
||||||
"<g id=\"edge6\" class=\"edge\">\n",
|
|
||||||
"<title>hover_over_paragraph->page_view</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M369.13,-95.76C310.26,-97.17 232.59,-99.41 163.87,-102.58 145.72,-103.42 125.98,-104.58 108.06,-105.73\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"107.86,-102.24 98.1,-106.38 108.31,-109.22 107.86,-102.24\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-106.53\" font-family=\"Times,serif\" font-size=\"14.00\">0.14</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- hover_over_paragraph->view_item_page -->\n",
|
|
||||||
"<g id=\"edge7\" class=\"edge\">\n",
|
|
||||||
"<title>hover_over_paragraph->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M372.68,-119.15C354.84,-125.32 336.5,-132.51 319.89,-140.58 312.9,-143.98 305.81,-147.87 298.86,-151.98\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"297.49,-148.71 290.78,-156.91 301.14,-154.69 297.49,-148.71\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"335.64\" y=\"-144.53\" font-family=\"Times,serif\" font-size=\"14.00\">0.86</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</svg>\n"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<graphviz.graphs.Digraph at 0x7f6800f97110>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"[[0. 1. 0. 0. ]\n",
|
|
||||||
" [0. 0.1875 0.375 0.4375 ]\n",
|
|
||||||
" [0. 1. 0. 0. ]\n",
|
|
||||||
" [0.14285714 0.85714286 0. 0. ]]\n",
|
|
||||||
"d176d7c9-4027-4702-9e31-2a71395cdda0\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"image/svg+xml": [
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
|
||||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
|
||||||
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
|
||||||
"<!-- Generated by graphviz version 13.1.2 (0)\n",
|
|
||||||
" -->\n",
|
|
||||||
"<!-- Pages: 1 -->\n",
|
|
||||||
"<svg width=\"104pt\" height=\"104pt\"\n",
|
|
||||||
" viewBox=\"0.00 0.00 104.00 104.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
|
||||||
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 100.37)\">\n",
|
|
||||||
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-100.37 100.37,-100.37 100.37,4 -4,4\"/>\n",
|
|
||||||
"<!-- page_view -->\n",
|
|
||||||
"<g id=\"node1\" class=\"node\">\n",
|
|
||||||
"<title>page_view</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"48.19\" cy=\"-48.19\" rx=\"48.19\" ry=\"48.19\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"48.19\" y=\"-43.51\" font-family=\"Times,serif\" font-size=\"14.00\">page_view</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</svg>\n"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<graphviz.graphs.Digraph at 0x7f6800f97110>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"[[0.]]\n",
|
|
||||||
"f0317a5d-e424-44e9-b784-c8f7291ffe31\n"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"data": {
|
|
||||||
"image/svg+xml": [
|
|
||||||
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
|
|
||||||
"<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
|
|
||||||
" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
|
|
||||||
"<!-- Generated by graphviz version 13.1.2 (0)\n",
|
|
||||||
" -->\n",
|
|
||||||
"<!-- Pages: 1 -->\n",
|
|
||||||
"<svg width=\"310pt\" height=\"160pt\"\n",
|
|
||||||
" viewBox=\"0.00 0.00 310.00 160.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
|
|
||||||
"<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 156.44)\">\n",
|
|
||||||
"<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-156.44 305.89,-156.44 305.89,4 -4,4\"/>\n",
|
|
||||||
"<!-- page_view -->\n",
|
|
||||||
"<g id=\"node1\" class=\"node\">\n",
|
|
||||||
"<title>page_view</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"48.19\" cy=\"-69.01\" rx=\"48.19\" ry=\"48.19\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"48.19\" y=\"-64.33\" font-family=\"Times,serif\" font-size=\"14.00\">page_view</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- page_view->page_view -->\n",
|
|
||||||
"<g id=\"edge1\" class=\"edge\">\n",
|
|
||||||
"<title>page_view->page_view</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M33.03,-115.09C34.09,-126.6 39.14,-135.19 48.19,-135.19 53.98,-135.19 58.13,-131.66 60.65,-126.1\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"64.01,-127.11 62.98,-116.56 57.21,-125.45 64.01,-127.11\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"48.19\" y=\"-139.14\" font-family=\"Times,serif\" font-size=\"14.00\">0.50</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- view_item_page -->\n",
|
|
||||||
"<g id=\"node2\" class=\"node\">\n",
|
|
||||||
"<title>view_item_page</title>\n",
|
|
||||||
"<ellipse fill=\"none\" stroke=\"black\" cx=\"232.88\" cy=\"-69.01\" rx=\"69.01\" ry=\"69.01\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"232.88\" y=\"-64.33\" font-family=\"Times,serif\" font-size=\"14.00\">view_item_page</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"<!-- page_view->view_item_page -->\n",
|
|
||||||
"<g id=\"edge2\" class=\"edge\">\n",
|
|
||||||
"<title>page_view->view_item_page</title>\n",
|
|
||||||
"<path fill=\"none\" stroke=\"black\" d=\"M96.71,-69.01C113.69,-69.01 133.31,-69.01 152.25,-69.01\"/>\n",
|
|
||||||
"<polygon fill=\"black\" stroke=\"black\" points=\"152.1,-72.51 162.1,-69.01 152.1,-65.51 152.1,-72.51\"/>\n",
|
|
||||||
"<text xml:space=\"preserve\" text-anchor=\"middle\" x=\"130.12\" y=\"-72.96\" font-family=\"Times,serif\" font-size=\"14.00\">0.50</text>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</g>\n",
|
|
||||||
"</svg>\n"
|
|
||||||
],
|
|
||||||
"text/plain": [
|
|
||||||
"<graphviz.graphs.Digraph at 0x7f6800bf50f0>"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"metadata": {},
|
|
||||||
"output_type": "display_data"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"[[5.0e-001 5.0e-001]\n",
|
|
||||||
" [9.9e-324 1.5e-323]]\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
|
||||||
"def explore_session(session_id: str):\n",
|
|
||||||
" subset = df[df['sessionId'] == session_id]\n",
|
|
||||||
" print(session_id)\n",
|
|
||||||
" P, labels = build_transition_prob_matrix(subset)\n",
|
|
||||||
" g = render_graph(f\"session_{session_id}\", P, ls_index=labels, threshold=0.01, fmt=\"svg\", view=False)\n",
|
|
||||||
" display(g)\n",
|
|
||||||
" return P\n",
|
|
||||||
"for session in sessions:\n",
|
|
||||||
" print(explore_session(session))"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"kernelspec": {
|
|
||||||
"display_name": "Python (PHANTOM)",
|
|
||||||
"language": "python",
|
|
||||||
"name": "phantom"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"codemirror_mode": {
|
|
||||||
"name": "ipython",
|
|
||||||
"version": 3
|
|
||||||
},
|
|
||||||
"file_extension": ".py",
|
|
||||||
"mimetype": "text/x-python",
|
|
||||||
"name": "python",
|
|
||||||
"nbconvert_exporter": "python",
|
|
||||||
"pygments_lexer": "ipython3",
|
|
||||||
"version": "3.13.7"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 5
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,114 +0,0 @@
|
|||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import os
|
|
||||||
import random
|
|
||||||
from pathlib import Path
|
|
||||||
from types import SimpleNamespace
|
|
||||||
|
|
||||||
import pandas as pd
|
|
||||||
|
|
||||||
from lib.separability import estimate_alpha, load_artifacts, score_session
|
|
||||||
|
|
||||||
|
|
||||||
# use relative import when in package context, fallback for standalone
|
|
||||||
try:
|
|
||||||
from sim.rl.behavior_loader.models import AgentBehaviorModel
|
|
||||||
except ImportError:
|
|
||||||
import sys
|
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent / "sim" / "rl" / "behavior_loader"))
|
|
||||||
from models import AgentBehaviorModel
|
|
||||||
|
|
||||||
# paths should be configurable via environment or relative to project root
|
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent.parent
|
|
||||||
AGENT_DATA_DIR = Path(os.getenv('PHANTOM_AGENT_DATA_DIR', PROJECT_ROOT / "experiments" / "agents" / "collected_data"))
|
|
||||||
|
|
||||||
try:
|
|
||||||
SEPARABILITY_ARTIFACTS = load_artifacts()
|
|
||||||
except FileNotFoundError:
|
|
||||||
SEPARABILITY_ARTIFACTS = None
|
|
||||||
|
|
||||||
|
|
||||||
def remap_schema(df: pd.DataFrame, mapping: dict, on: str = "event_type") -> pd.DataFrame:
|
|
||||||
"""remap column values according to mapping dict, preserving unmapped values"""
|
|
||||||
df = df.copy()
|
|
||||||
df[on] = df[on].map(mapping).fillna(df[on])
|
|
||||||
return df
|
|
||||||
|
|
||||||
|
|
||||||
def _states_to_events(states: list[str]) -> list[SimpleNamespace]:
|
|
||||||
events: list[SimpleNamespace] = []
|
|
||||||
for idx, state in enumerate(states):
|
|
||||||
parts = state.split("|") if isinstance(state, str) else ["page", "product", str(state)]
|
|
||||||
page = f"/{parts[0]}" if parts else "/"
|
|
||||||
product = parts[1] if len(parts) > 1 else "unknown"
|
|
||||||
event_name = parts[2] if len(parts) > 2 else parts[-1]
|
|
||||||
events.append(
|
|
||||||
SimpleNamespace(
|
|
||||||
eventName=event_name,
|
|
||||||
page=page,
|
|
||||||
productId=product,
|
|
||||||
ts=float(idx),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return events
|
|
||||||
|
|
||||||
def contaminate_dataset(df: pd.DataFrame, on: str = "event_type",
|
|
||||||
contamination_rate: float = 0.1,
|
|
||||||
agent_data_dir: Path = None) -> pd.DataFrame:
|
|
||||||
"""inject synthetic agent trajectories into a dataset
|
|
||||||
contamination_rate: fraction of final dataset that should be agent data (0.1 = 10% agents)
|
|
||||||
"""
|
|
||||||
data_dir = agent_data_dir or AGENT_DATA_DIR
|
|
||||||
model = AgentBehaviorModel(str(data_dir))
|
|
||||||
model.build_MDP() # ensure MDP is built before sampling
|
|
||||||
|
|
||||||
# compute event distribution from original data
|
|
||||||
event_dist = df[on].value_counts(normalize=True).to_dict()
|
|
||||||
total = sum(event_dist.values())
|
|
||||||
event_dist = {k: v / total for k, v in event_dist.items()}
|
|
||||||
|
|
||||||
# calculate how many synthetic events to add
|
|
||||||
N = len(df)
|
|
||||||
N_final = N / (1 - contamination_rate)
|
|
||||||
N_contaminate = int(N_final - N)
|
|
||||||
|
|
||||||
# sample start states weighted by original distribution
|
|
||||||
start_events = random.choices(list(event_dist.keys()), weights=list(event_dist.values()), k=N_contaminate)
|
|
||||||
|
|
||||||
# generate synthetic trajectories
|
|
||||||
new_rows = []
|
|
||||||
alpha_estimates = []
|
|
||||||
|
|
||||||
for start_event in start_events:
|
|
||||||
# sample trajectory from agent model, using a state that contains the event type
|
|
||||||
mdp_states = model.mdp.get('states', []) if model.mdp else []
|
|
||||||
matching_starts = [s for s in mdp_states if start_event in s]
|
|
||||||
if not matching_starts:
|
|
||||||
continue # skip if no matching start state
|
|
||||||
start_state = random.choice(matching_starts)
|
|
||||||
trajectory = model.sample_traj(start_state, max_len=20)
|
|
||||||
score_payload: list[SimpleNamespace] = []
|
|
||||||
score: dict[str, float] = {}
|
|
||||||
if SEPARABILITY_ARTIFACTS:
|
|
||||||
score_payload = _states_to_events(trajectory)
|
|
||||||
score = score_session(score_payload, SEPARABILITY_ARTIFACTS)
|
|
||||||
alpha_estimates.append(
|
|
||||||
estimate_alpha(score["prob_agent"], score["delta_h"], score["delta_a"], temperature=2.0)
|
|
||||||
)
|
|
||||||
|
|
||||||
for state in trajectory:
|
|
||||||
parts = state.split('|') if isinstance(state, str) else [start_event]
|
|
||||||
new_rows.append({
|
|
||||||
on: parts[-1] if parts else start_event,
|
|
||||||
'source': 'synthetic_agent',
|
|
||||||
'prob_agent': score.get('prob_agent') if SEPARABILITY_ARTIFACTS and score_payload else None,
|
|
||||||
'delta_h': score.get('delta_h') if SEPARABILITY_ARTIFACTS and score_payload else None,
|
|
||||||
'delta_a': score.get('delta_a') if SEPARABILITY_ARTIFACTS and score_payload else None,
|
|
||||||
})
|
|
||||||
|
|
||||||
if new_rows:
|
|
||||||
contaminate_df = pd.DataFrame(new_rows)
|
|
||||||
df = pd.concat([df, contaminate_df], ignore_index=True)
|
|
||||||
if alpha_estimates:
|
|
||||||
df['estimated_alpha'] = sum(alpha_estimates) / len(alpha_estimates)
|
|
||||||
return df
|
|
||||||
@@ -7,6 +7,15 @@ import pandas as pd
|
|||||||
class PricingFunction(ABC):
|
class PricingFunction(ABC):
|
||||||
"""
|
"""
|
||||||
Abstract base for pricing functions.
|
Abstract base for pricing functions.
|
||||||
|
|
||||||
|
Defines mapping: f(Q_t, P_t, S_t, H_t) -> P_{t+1}
|
||||||
|
|
||||||
|
Where:
|
||||||
|
Q_t ∈ R^n: demand vector at time t
|
||||||
|
P_t ∈ R^n: price vector at time t
|
||||||
|
S_t: session features (behavioral signals, interactions)
|
||||||
|
H_t = {Q_{t-k}, P_{t-k}, S_{t-k}}: historical state trajectory
|
||||||
|
|
||||||
Objective:
|
Objective:
|
||||||
maximize E[R_T] = E[Σ P_t^T · Q_t]
|
maximize E[R_T] = E[Σ P_t^T · Q_t]
|
||||||
subject to:
|
subject to:
|
||||||
@@ -19,10 +28,10 @@ class PricingFunction(ABC):
|
|||||||
def fit(self, *kwargs):
|
def fit(self, *kwargs):
|
||||||
"""
|
"""
|
||||||
Offline training on historical data.
|
Offline training on historical data.
|
||||||
This is where we can think about some maximization of expected revenue
|
|
||||||
over historical trajectories to learn parameters of the pricing function.
|
|
||||||
(This however we cover move in the RL side of things)
|
|
||||||
|
|
||||||
|
Args:
|
||||||
|
historical_data: DataFrame with elasticity, prices, demand signals
|
||||||
|
**kwargs: additional training parameters
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -30,18 +39,12 @@ class PricingFunction(ABC):
|
|||||||
def predict(self, *kwargs) -> np.ndarray:
|
def predict(self, *kwargs) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
Generate optimal prices given current state.
|
Generate optimal prices given current state.
|
||||||
This is an abstract method that transitions from τ -> P*
|
|
||||||
which is the mapping from the trajectory to optimal prices under
|
|
||||||
some subset of session grouping (so, per sessionId)
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
@abstractmethod
|
Args:
|
||||||
def _get_features(self, *kwargs) -> np.ndarray:
|
state_space: StateSpace object containing Q_t, P_t, S_t, H_t
|
||||||
"""
|
|
||||||
Extract features from trajectory for pricing decision.
|
|
||||||
Returns:
|
Returns:
|
||||||
np.ndarray of shape (n_products, n_features)
|
P_{t+1}: price vector in R^n
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -57,13 +57,3 @@ class ElasticityBasedPricer(PricingFunction):
|
|||||||
# enforce bounds
|
# enforce bounds
|
||||||
prices = np.clip(prices, self.price_floor, self.price_ceil)
|
prices = np.clip(prices, self.price_floor, self.price_ceil)
|
||||||
return prices
|
return prices
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Extract elasticity, demand, and demand deviation for each product"""
|
|
||||||
if state_space is None or self.elasticity is None:
|
|
||||||
n = len(self.elasticity) if self.elasticity is not None else 0
|
|
||||||
return np.zeros((n, 3))
|
|
||||||
|
|
||||||
demand = np.asarray(state_space.demand)
|
|
||||||
demand_dev = (demand - self.mean_demand) / (self.mean_demand + 1e-6)
|
|
||||||
return np.column_stack([self.elasticity, demand, demand_dev])
|
|
||||||
|
|||||||
@@ -107,36 +107,6 @@ class SessionAwarePricer(PricingFunction):
|
|||||||
|
|
||||||
return prices
|
return prices
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Extract elasticity, demand, and session features"""
|
|
||||||
if state_space is None or self.elasticity is None:
|
|
||||||
n = len(self.elasticity) if self.elasticity is not None else 0
|
|
||||||
return np.zeros((n, 5))
|
|
||||||
|
|
||||||
demand = np.asarray(state_space.demand)
|
|
||||||
n_products = len(demand)
|
|
||||||
|
|
||||||
# extract session features
|
|
||||||
velocity = 0.0
|
|
||||||
view_depth = 0.0
|
|
||||||
cart_to_view = 0.0
|
|
||||||
|
|
||||||
if not state_space.session_features.empty:
|
|
||||||
sf = state_space.session_features.iloc[0]
|
|
||||||
velocity = sf.get('interaction_velocity', 0.0)
|
|
||||||
view_depth = sf.get('product_view_depth', 0.0)
|
|
||||||
cart_to_view = sf.get('cart_to_view_ratio', 0.0)
|
|
||||||
|
|
||||||
# broadcast session features to all products
|
|
||||||
features = np.column_stack([
|
|
||||||
self.elasticity,
|
|
||||||
demand,
|
|
||||||
np.full(n_products, velocity),
|
|
||||||
np.full(n_products, view_depth),
|
|
||||||
np.full(n_products, cart_to_view)
|
|
||||||
])
|
|
||||||
return features
|
|
||||||
|
|
||||||
|
|
||||||
class ProductSpecificSessionPricer(PricingFunction):
|
class ProductSpecificSessionPricer(PricingFunction):
|
||||||
"""
|
"""
|
||||||
@@ -200,12 +170,3 @@ class ProductSpecificSessionPricer(PricingFunction):
|
|||||||
|
|
||||||
prices = np.clip(base_prices, self.price_floor, self.price_ceil)
|
prices = np.clip(base_prices, self.price_floor, self.price_ceil)
|
||||||
return prices
|
return prices
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Extract elasticity and demand features for product-specific pricing"""
|
|
||||||
if state_space is None or self.elasticity is None:
|
|
||||||
n = len(self.elasticity) if self.elasticity is not None else 0
|
|
||||||
return np.zeros((n, 2))
|
|
||||||
|
|
||||||
demand = np.asarray(state_space.demand)
|
|
||||||
return np.column_stack([self.elasticity, demand])
|
|
||||||
|
|||||||
@@ -3,46 +3,6 @@ import pandas as pd
|
|||||||
from procesing.pricers.base import PricingFunction
|
from procesing.pricers.base import PricingFunction
|
||||||
|
|
||||||
|
|
||||||
def session_features_to_demand(session_features: pd.DataFrame) -> float:
|
|
||||||
"""
|
|
||||||
Map session behavioral features to demand proxy.
|
|
||||||
THIS is the critical θ̂ → D transformation for rule-based pricing.
|
|
||||||
|
|
||||||
Logic:
|
|
||||||
- High velocity → agent behavior → price up (revenue recovery)
|
|
||||||
- High cart ratio → purchase intent → price up
|
|
||||||
- Low activity → discount to convert
|
|
||||||
|
|
||||||
Returns: demand proxy score (0-20 range, higher = more demand)
|
|
||||||
"""
|
|
||||||
if session_features.empty:
|
|
||||||
return 1.0
|
|
||||||
|
|
||||||
feat = session_features.iloc[0] if len(session_features) > 0 else {}
|
|
||||||
|
|
||||||
velocity = feat.get('interaction_velocity', 0)
|
|
||||||
cart_ratio = feat.get('cart_to_view_ratio', 0)
|
|
||||||
item_views = feat.get('item_views', 0)
|
|
||||||
cart_adds = feat.get('cart_adds', 0)
|
|
||||||
|
|
||||||
# baseline demand
|
|
||||||
demand = 1.0
|
|
||||||
|
|
||||||
# agent detection: high velocity → treat as high "demand" to price up
|
|
||||||
if velocity > 2.0:
|
|
||||||
demand += 10.0 # strong agent signal
|
|
||||||
|
|
||||||
# conversion intent: cart interaction → price up
|
|
||||||
if cart_ratio > 0.1 or cart_adds > 0:
|
|
||||||
demand += 5.0
|
|
||||||
|
|
||||||
# browsing depth: many views → interest signal
|
|
||||||
if item_views > 3:
|
|
||||||
demand += min(item_views, 5.0)
|
|
||||||
|
|
||||||
return min(demand, 20.0) # cap at 20
|
|
||||||
|
|
||||||
|
|
||||||
class StaticPricer(PricingFunction):
|
class StaticPricer(PricingFunction):
|
||||||
"""Static pricing: always return fixed base prices"""
|
"""Static pricing: always return fixed base prices"""
|
||||||
|
|
||||||
@@ -65,11 +25,6 @@ class StaticPricer(PricingFunction):
|
|||||||
raise ValueError("Must call fit() or provide base_prices in constructor")
|
raise ValueError("Must call fit() or provide base_prices in constructor")
|
||||||
return self.base_prices.copy()
|
return self.base_prices.copy()
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Static pricer uses no features, returns empty array"""
|
|
||||||
n = len(self.base_prices) if self.base_prices is not None else 0
|
|
||||||
return np.zeros((n, 0))
|
|
||||||
|
|
||||||
|
|
||||||
class RandomPricer(PricingFunction):
|
class RandomPricer(PricingFunction):
|
||||||
"""Random pricing within bounds (for baseline comparison)"""
|
"""Random pricing within bounds (for baseline comparison)"""
|
||||||
@@ -92,11 +47,6 @@ class RandomPricer(PricingFunction):
|
|||||||
self.n_products = len(state_space.demand)
|
self.n_products = len(state_space.demand)
|
||||||
return self.rng.uniform(self.price_min, self.price_max, size=self.n_products)
|
return self.rng.uniform(self.price_min, self.price_max, size=self.n_products)
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Random pricer uses no features"""
|
|
||||||
n = self.n_products if self.n_products else 0
|
|
||||||
return np.zeros((n, 0))
|
|
||||||
|
|
||||||
|
|
||||||
class SimpleSurgePricer(PricingFunction):
|
class SimpleSurgePricer(PricingFunction):
|
||||||
"""
|
"""
|
||||||
@@ -117,25 +67,21 @@ class SimpleSurgePricer(PricingFunction):
|
|||||||
self.surge_multiplier = surge_multiplier
|
self.surge_multiplier = surge_multiplier
|
||||||
self.discount_multiplier = discount_multiplier
|
self.discount_multiplier = discount_multiplier
|
||||||
|
|
||||||
def fit(self, market_data: pd.DataFrame):
|
def fit(self, market_data : pd.DataFrame):
|
||||||
"""Extract base prices from product catalog or historical averages"""
|
"""Extract base prices from product catalog or historical averages"""
|
||||||
self.base_prices = market_data['base_price'].to_numpy() if 'base_price' in market_data.columns else market_data['price'].values
|
self.base_prices = market_data['base_price'].to_numpy() if 'base_price' in market_data.columns else market_data['price'].values
|
||||||
return self
|
self.demand_history = market_data['demand'].to_numpy() if 'demand' in market_data.columns else np.zeros_like(self.base_prices)
|
||||||
|
|
||||||
def predict(self, state_space) -> np.ndarray:
|
def predict(self) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
Adjust prices based on current demand using surge rules.
|
Adjust prices based on current demand using surge rules.
|
||||||
state_space.demand: demand proxy per product (from session features)
|
state_space.demand: demand counts per product
|
||||||
state_space.prices: base prices
|
state_space.prices: current prices (fallback if base_prices not set)
|
||||||
"""
|
"""
|
||||||
demand = np.asarray(state_space.demand) if state_space and hasattr(state_space, 'demand') else np.array([0])
|
current_prices = self.base_prices if self.base_prices is not None else np.ones_like(demand_vector) * 99.99
|
||||||
base = np.asarray(state_space.prices) if state_space and hasattr(state_space, 'prices') else self.base_prices
|
demand = self.demand_history if self.demand_history is not None else np.zeros_like(current_prices)
|
||||||
|
new_prices = current_prices.copy()
|
||||||
|
|
||||||
if base is None:
|
|
||||||
base = np.ones(len(demand)) * 99.99
|
|
||||||
|
|
||||||
# ensure float dtype to allow multiplication by float multipliers
|
|
||||||
new_prices = base.astype(np.float64).copy()
|
|
||||||
high_mask = demand >= self.high_threshold
|
high_mask = demand >= self.high_threshold
|
||||||
new_prices[high_mask] *= self.surge_multiplier
|
new_prices[high_mask] *= self.surge_multiplier
|
||||||
|
|
||||||
@@ -143,16 +89,3 @@ class SimpleSurgePricer(PricingFunction):
|
|||||||
new_prices[low_mask] *= self.discount_multiplier
|
new_prices[low_mask] *= self.discount_multiplier
|
||||||
|
|
||||||
return new_prices
|
return new_prices
|
||||||
|
|
||||||
def _get_features(self, state_space=None) -> np.ndarray:
|
|
||||||
"""Extract demand and base price features for each product"""
|
|
||||||
if state_space is None:
|
|
||||||
n = len(self.base_prices) if self.base_prices is not None else 0
|
|
||||||
return np.zeros((n, 2))
|
|
||||||
|
|
||||||
demand = np.asarray(state_space.demand) if hasattr(state_space, 'demand') else np.array([0])
|
|
||||||
base = np.asarray(state_space.prices) if hasattr(state_space, 'prices') else self.base_prices
|
|
||||||
if base is None:
|
|
||||||
base = np.ones(len(demand)) * 99.99
|
|
||||||
|
|
||||||
return np.column_stack([demand, base])
|
|
||||||
|
|||||||
@@ -135,7 +135,6 @@ class ExtractSessionFeaturesStep(BaseContextStep):
|
|||||||
Vectorized session feature extraction - replaces O(n^2) per-row loop.
|
Vectorized session feature extraction - replaces O(n^2) per-row loop.
|
||||||
Input: interactions_df
|
Input: interactions_df
|
||||||
Output: session-level feature matrix
|
Output: session-level feature matrix
|
||||||
THIS is our main mapping from tau (trajectory) to some features vector theta - we need to do this very well. This is what will go into demand esimation.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def transform(self, X: pd.DataFrame) -> pd.DataFrame:
|
def transform(self, X: pd.DataFrame) -> pd.DataFrame:
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ from procesing.steps import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
def test_compute_demand(pipeline_context):
|
def test_compute_demand(pipeline_context):
|
||||||
random.seed(42) # deterministic test
|
|
||||||
step = ComputeDemandStep(context=pipeline_context)
|
step = ComputeDemandStep(context=pipeline_context)
|
||||||
|
|
||||||
# Test with normal interaction data
|
# Test with normal interaction data
|
||||||
@@ -27,7 +26,6 @@ def test_compute_demand(pipeline_context):
|
|||||||
|
|
||||||
|
|
||||||
def test_compute_demand_skewed(pipeline_context):
|
def test_compute_demand_skewed(pipeline_context):
|
||||||
random.seed(42) # deterministic test
|
|
||||||
step = ComputeDemandStep(context=pipeline_context)
|
step = ComputeDemandStep(context=pipeline_context)
|
||||||
|
|
||||||
# Test with normal interaction data
|
# Test with normal interaction data
|
||||||
|
|||||||
@@ -1,165 +0,0 @@
|
|||||||
import pytest
|
|
||||||
import pandas as pd
|
|
||||||
import numpy as np
|
|
||||||
from procesing.steps.session import (
|
|
||||||
TemporalFeatureStep,
|
|
||||||
BehavioralFeatureStep,
|
|
||||||
ProductFeatureStep,
|
|
||||||
UserAgentFeatureStep,
|
|
||||||
ExtractSessionFeaturesStep,
|
|
||||||
JoinLabelsStep,
|
|
||||||
ValidateDataStep,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
# TemporalFeatureStep tests
|
|
||||||
def test_temporal_empty(pipeline_context):
|
|
||||||
result = TemporalFeatureStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
assert 'sessionId' in result.columns
|
|
||||||
assert result.empty
|
|
||||||
|
|
||||||
|
|
||||||
def test_temporal_basic(pipeline_context, session_interactions):
|
|
||||||
result = TemporalFeatureStep(pipeline_context).transform(session_interactions)
|
|
||||||
assert 'session_duration_sec' in result.columns
|
|
||||||
assert 'interaction_velocity' in result.columns
|
|
||||||
assert 'max_velocity_5min' in result.columns
|
|
||||||
assert result['total_interactions'].sum() == len(session_interactions)
|
|
||||||
|
|
||||||
|
|
||||||
def test_temporal_timeout(pipeline_context):
|
|
||||||
df = pd.DataFrame({
|
|
||||||
'sessionId': ['s1', 's1'],
|
|
||||||
'ts': ['2025-01-01T10:00:00Z', '2025-01-01T11:00:00Z'], # 1 hour gap
|
|
||||||
})
|
|
||||||
result = TemporalFeatureStep(pipeline_context, timeout_sec=900).transform(df)
|
|
||||||
assert result.iloc[0]['session_duration_sec'] == 0 # gap exceeds timeout
|
|
||||||
|
|
||||||
|
|
||||||
# BehavioralFeatureStep tests
|
|
||||||
def test_behavioral_empty(pipeline_context):
|
|
||||||
result = BehavioralFeatureStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
assert 'sessionId' in result.columns
|
|
||||||
|
|
||||||
|
|
||||||
def test_behavioral_counts(pipeline_context, session_interactions):
|
|
||||||
result = BehavioralFeatureStep(pipeline_context).transform(session_interactions)
|
|
||||||
assert 'page_views' in result.columns
|
|
||||||
assert 'item_views' in result.columns
|
|
||||||
assert 'hover_events' in result.columns
|
|
||||||
assert result['total_events'].sum() == len(session_interactions)
|
|
||||||
|
|
||||||
|
|
||||||
def test_behavioral_hover_prefix(pipeline_context):
|
|
||||||
df = pd.DataFrame({
|
|
||||||
'sessionId': ['s1', 's1'],
|
|
||||||
'eventName': ['hover_over_custom', 'hover_over_button'],
|
|
||||||
'page': ['/products', '/products'],
|
|
||||||
})
|
|
||||||
result = BehavioralFeatureStep(pipeline_context).transform(df)
|
|
||||||
assert result.iloc[0]['hover_events'] == 2
|
|
||||||
|
|
||||||
|
|
||||||
# ProductFeatureStep tests
|
|
||||||
def test_product_empty(pipeline_context):
|
|
||||||
result = ProductFeatureStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
assert 'sessionId' in result.columns
|
|
||||||
|
|
||||||
|
|
||||||
def test_product_features(pipeline_context, session_interactions):
|
|
||||||
result = ProductFeatureStep(pipeline_context).transform(session_interactions)
|
|
||||||
assert 'unique_products_viewed' in result.columns
|
|
||||||
assert 'price_range' in result.columns
|
|
||||||
assert result['unique_products_viewed'].sum() > 0
|
|
||||||
|
|
||||||
|
|
||||||
# UserAgentFeatureStep tests
|
|
||||||
def test_ua_empty(pipeline_context):
|
|
||||||
result = UserAgentFeatureStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
assert 'sessionId' in result.columns
|
|
||||||
|
|
||||||
|
|
||||||
def test_ua_headless_detection(pipeline_context):
|
|
||||||
df = pd.DataFrame({
|
|
||||||
'sessionId': ['s1', 's2'],
|
|
||||||
'userAgent': ['Mozilla/5.0 Chrome/120', 'HeadlessChrome/120'],
|
|
||||||
})
|
|
||||||
result = UserAgentFeatureStep(pipeline_context).transform(df)
|
|
||||||
assert 'is_headless' in result.columns
|
|
||||||
headless = dict(zip(result['sessionId'], result['is_headless']))
|
|
||||||
assert headless['s1'] == False
|
|
||||||
assert headless['s2'] == True
|
|
||||||
|
|
||||||
|
|
||||||
def test_ua_browser_family(pipeline_context):
|
|
||||||
df = pd.DataFrame({
|
|
||||||
'sessionId': ['s1', 's2', 's3'],
|
|
||||||
'userAgent': ['Mozilla/5.0 Firefox/120', 'Safari/605.1.15', 'Unknown'],
|
|
||||||
})
|
|
||||||
result = UserAgentFeatureStep(pipeline_context).transform(df)
|
|
||||||
browsers = dict(zip(result['sessionId'], result['browser_family']))
|
|
||||||
assert browsers['s1'] == 'Firefox'
|
|
||||||
assert browsers['s2'] == 'Safari'
|
|
||||||
assert browsers['s3'] == 'Other'
|
|
||||||
|
|
||||||
|
|
||||||
def test_ua_automation_detection(pipeline_context):
|
|
||||||
df = pd.DataFrame({
|
|
||||||
'sessionId': ['s1', 's2'],
|
|
||||||
'userAgent': ['Selenium WebDriver', 'Normal Chrome/120'],
|
|
||||||
})
|
|
||||||
result = UserAgentFeatureStep(pipeline_context).transform(df)
|
|
||||||
auto = dict(zip(result['sessionId'], result['is_automation']))
|
|
||||||
assert auto['s1'] == True
|
|
||||||
assert auto['s2'] == False
|
|
||||||
|
|
||||||
|
|
||||||
# ExtractSessionFeaturesStep tests
|
|
||||||
def test_extract_empty(pipeline_context):
|
|
||||||
result = ExtractSessionFeaturesStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
assert result.empty
|
|
||||||
|
|
||||||
|
|
||||||
def test_extract_merges_all(pipeline_context, session_interactions):
|
|
||||||
result = ExtractSessionFeaturesStep(pipeline_context).transform(session_interactions)
|
|
||||||
expected = ['session_duration_sec', 'total_events', 'unique_products_viewed', 'is_headless']
|
|
||||||
for col in expected:
|
|
||||||
assert col in result.columns
|
|
||||||
assert 'experimentId' in result.columns
|
|
||||||
|
|
||||||
|
|
||||||
# JoinLabelsStep tests
|
|
||||||
def test_join_labels_tuple_input(pipeline_context):
|
|
||||||
features = pd.DataFrame({'sessionId': ['s1'], 'experimentId': ['exp1'], 'total_events': [5]})
|
|
||||||
experiments = pd.DataFrame({'id': ['exp1'], 'xp_human_only': [True]})
|
|
||||||
result = JoinLabelsStep(pipeline_context).transform((features, experiments))
|
|
||||||
assert 'is_agent' in result.columns
|
|
||||||
assert result.iloc[0]['is_agent'] == False
|
|
||||||
|
|
||||||
|
|
||||||
def test_join_labels_empty_experiments(pipeline_context):
|
|
||||||
features = pd.DataFrame({'sessionId': ['s1'], 'experimentId': ['exp1']})
|
|
||||||
result = JoinLabelsStep(pipeline_context).transform((features, pd.DataFrame()))
|
|
||||||
assert pd.isna(result.iloc[0]['is_agent'])
|
|
||||||
|
|
||||||
|
|
||||||
# ValidateDataStep tests
|
|
||||||
def test_validate_empty(pipeline_context):
|
|
||||||
ValidateDataStep(pipeline_context).transform(pd.DataFrame())
|
|
||||||
report = pipeline_context.get_cached('validation_report')
|
|
||||||
assert report['status'] == 'empty'
|
|
||||||
|
|
||||||
|
|
||||||
def test_validate_missing_cols(pipeline_context):
|
|
||||||
df = pd.DataFrame({'sessionId': ['s1'], 'ts': ['2025-01-01']})
|
|
||||||
ValidateDataStep(pipeline_context).transform(df)
|
|
||||||
report = pipeline_context.get_cached('validation_report')
|
|
||||||
assert report['status'] == 'invalid'
|
|
||||||
assert 'eventName' in report['missing_cols']
|
|
||||||
|
|
||||||
|
|
||||||
def test_validate_valid(pipeline_context, session_interactions):
|
|
||||||
ValidateDataStep(pipeline_context).transform(session_interactions)
|
|
||||||
report = pipeline_context.get_cached('validation_report')
|
|
||||||
assert report['status'] == 'valid'
|
|
||||||
assert report['sessions'] > 0
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
"""PHANTOM shared library
|
|
||||||
Exports unified utilities for features, state, config, kafka, and model registry
|
|
||||||
"""
|
|
||||||
from .config import (
|
|
||||||
PROJECT_ROOT, DATA_DIR, EXPERIMENTS_DIR,
|
|
||||||
AGENT_DATA_DIR, HUMAN_DATA_DIR, SIM_RUNS_DIR, MODEL_REGISTRY_DIR,
|
|
||||||
COLLECTED_DATA_DIR, NOTEBOOK_OUTPUT_DIR,
|
|
||||||
ensure_dir, get_data_path, get_experiments_path, get_sim_path,
|
|
||||||
KAFKA_HOST, KAFKA_PORT, KAFKA_BROKER,
|
|
||||||
REDIS_HOST, REDIS_PORT,
|
|
||||||
SUPABASE_URL, SUPABASE_ANON_KEY,
|
|
||||||
BACKEND_PORT, PROVIDER_PORT
|
|
||||||
)
|
|
||||||
from .state import (
|
|
||||||
make_state_repr, event_to_state, parse_state,
|
|
||||||
get_event_name, get_timestamp,
|
|
||||||
create_state_fn, create_event_name_fn, create_timestamp_fn
|
|
||||||
)
|
|
||||||
from .features import (
|
|
||||||
transition_histogram, temporal_signature, state_coverage, transition_entropy,
|
|
||||||
event_type_distribution, featurize_trajectory, parse_timestamp
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
# config
|
|
||||||
'PROJECT_ROOT', 'DATA_DIR', 'EXPERIMENTS_DIR',
|
|
||||||
'AGENT_DATA_DIR', 'HUMAN_DATA_DIR', 'SIM_RUNS_DIR', 'MODEL_REGISTRY_DIR',
|
|
||||||
'COLLECTED_DATA_DIR', 'NOTEBOOK_OUTPUT_DIR',
|
|
||||||
'ensure_dir', 'get_data_path', 'get_experiments_path', 'get_sim_path',
|
|
||||||
'KAFKA_HOST', 'KAFKA_PORT', 'KAFKA_BROKER',
|
|
||||||
'REDIS_HOST', 'REDIS_PORT',
|
|
||||||
'SUPABASE_URL', 'SUPABASE_ANON_KEY',
|
|
||||||
'BACKEND_PORT', 'PROVIDER_PORT',
|
|
||||||
# state
|
|
||||||
'make_state_repr', 'event_to_state', 'parse_state',
|
|
||||||
'get_event_name', 'get_timestamp',
|
|
||||||
'create_state_fn', 'create_event_name_fn', 'create_timestamp_fn',
|
|
||||||
# features
|
|
||||||
'transition_histogram', 'temporal_signature', 'state_coverage', 'transition_entropy',
|
|
||||||
'event_type_distribution', 'featurize_trajectory', 'parse_timestamp',
|
|
||||||
]
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
"""Unified path configuration for PHANTOM project
|
|
||||||
All hardcoded paths should reference this module
|
|
||||||
Paths can be overridden via environment variables
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
# project root (directory containing lib/, experiments/, sim/, web/, backend/)
|
|
||||||
PROJECT_ROOT = Path(__file__).parent.parent.resolve()
|
|
||||||
|
|
||||||
# data directories
|
|
||||||
DATA_DIR = Path(os.getenv('PHANTOM_DATA_DIR', PROJECT_ROOT / 'data'))
|
|
||||||
EXPERIMENTS_DIR = Path(os.getenv('PHANTOM_EXPERIMENTS_DIR', PROJECT_ROOT / 'experiments'))
|
|
||||||
|
|
||||||
# agent/human interaction data
|
|
||||||
AGENT_DATA_DIR = Path(os.getenv('PHANTOM_AGENT_DATA_DIR', DATA_DIR / 'agents'))
|
|
||||||
HUMAN_DATA_DIR = Path(os.getenv('PHANTOM_HUMAN_DATA_DIR', DATA_DIR / 'humans'))
|
|
||||||
|
|
||||||
# RL simulation runs
|
|
||||||
SIM_RUNS_DIR = Path(os.getenv('PHANTOM_SIM_RUNS_DIR', PROJECT_ROOT / 'sim' / 'rl' / 'runs'))
|
|
||||||
|
|
||||||
# model artifacts
|
|
||||||
MODEL_REGISTRY_DIR = Path(os.getenv('PHANTOM_MODEL_REGISTRY_DIR', DATA_DIR / 'models'))
|
|
||||||
|
|
||||||
# collected experiment data
|
|
||||||
COLLECTED_DATA_DIR = Path(os.getenv('PHANTOM_COLLECTED_DATA_DIR', EXPERIMENTS_DIR / 'agents' / 'collected_data'))
|
|
||||||
|
|
||||||
# notebook outputs
|
|
||||||
NOTEBOOK_OUTPUT_DIR = Path(os.getenv('PHANTOM_NOTEBOOK_OUTPUT_DIR', EXPERIMENTS_DIR / 'notebooks' / 'outputs'))
|
|
||||||
|
|
||||||
|
|
||||||
def ensure_dir(path: Path) -> Path:
|
|
||||||
"""ensure directory exists, create if needed"""
|
|
||||||
path.mkdir(parents=True, exist_ok=True)
|
|
||||||
return path
|
|
||||||
|
|
||||||
|
|
||||||
def get_data_path(*parts: str) -> Path:
|
|
||||||
"""construct path relative to DATA_DIR"""
|
|
||||||
return DATA_DIR.joinpath(*parts)
|
|
||||||
|
|
||||||
|
|
||||||
def get_experiments_path(*parts: str) -> Path:
|
|
||||||
"""construct path relative to EXPERIMENTS_DIR"""
|
|
||||||
return EXPERIMENTS_DIR.joinpath(*parts)
|
|
||||||
|
|
||||||
|
|
||||||
def get_sim_path(*parts: str) -> Path:
|
|
||||||
"""construct path relative to SIM_RUNS_DIR"""
|
|
||||||
return SIM_RUNS_DIR.joinpath(*parts)
|
|
||||||
|
|
||||||
|
|
||||||
# service configuration (from .env)
|
|
||||||
KAFKA_HOST = os.getenv('KAFKA_HOST', 'localhost')
|
|
||||||
KAFKA_PORT = os.getenv('KAFKA_PORT', '9092')
|
|
||||||
KAFKA_BROKER = f"{KAFKA_HOST}:{KAFKA_PORT}"
|
|
||||||
|
|
||||||
REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
|
|
||||||
REDIS_PORT = int(os.getenv('REDIS_PORT', '6379'))
|
|
||||||
|
|
||||||
SUPABASE_URL = os.getenv('NEXT_PUBLIC_SUPABASE_URL', '')
|
|
||||||
SUPABASE_ANON_KEY = os.getenv('NEXT_PUBLIC_SUPABASE_ANON_KEY', '')
|
|
||||||
|
|
||||||
BACKEND_PORT = int(os.getenv('BACKEND_PORT', '5000'))
|
|
||||||
PROVIDER_PORT = int(os.getenv('PROVIDER_PORT', '5001'))
|
|
||||||
125
lib/features.py
125
lib/features.py
@@ -1,125 +0,0 @@
|
|||||||
"""Unified featurization utilities for trajectory -> feature vector conversion
|
|
||||||
Used by both experiments/ml/ and sim/rl/ components
|
|
||||||
"""
|
|
||||||
import numpy as np
|
|
||||||
from collections import defaultdict
|
|
||||||
from typing import List, Dict, Callable, Optional, Any, Set
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
|
|
||||||
def transition_histogram(events: List, state_fn: Callable, max_states: int = 50) -> np.ndarray:
|
|
||||||
"""compute normalized histogram of state transitions in trajectory
|
|
||||||
events: list of event objects/dicts
|
|
||||||
state_fn: function mapping event -> state string
|
|
||||||
max_states: maximum dimensions for histogram
|
|
||||||
"""
|
|
||||||
if len(events) < 2:
|
|
||||||
return np.zeros(max_states, dtype=np.float32)
|
|
||||||
states = [state_fn(e) for e in events]
|
|
||||||
trans_counts = defaultdict(int)
|
|
||||||
for s, s_next in zip(states, states[1:]):
|
|
||||||
trans_counts[(s, s_next)] += 1
|
|
||||||
total = sum(trans_counts.values())
|
|
||||||
hist = np.array(list(trans_counts.values())[:max_states], dtype=np.float32)
|
|
||||||
hist = np.pad(hist, (0, max(0, max_states - len(hist))))
|
|
||||||
return hist / (total + 1e-10)
|
|
||||||
|
|
||||||
|
|
||||||
def temporal_signature(events: List, ts_fn: Callable) -> np.ndarray:
|
|
||||||
"""extract temporal features: mean/std/skew of inter-event times plus count
|
|
||||||
events: list of event objects/dicts
|
|
||||||
ts_fn: function mapping event -> timestamp (float seconds)
|
|
||||||
returns: [mean_dt, std_dt, skew, n_intervals] array
|
|
||||||
"""
|
|
||||||
if len(events) < 2:
|
|
||||||
return np.zeros(4, dtype=np.float32)
|
|
||||||
times = sorted([ts_fn(e) for e in events])
|
|
||||||
diffs = np.diff(times).astype(np.float32)
|
|
||||||
if len(diffs) == 0:
|
|
||||||
return np.zeros(4, dtype=np.float32)
|
|
||||||
mean_dt, std_dt = np.mean(diffs), np.std(diffs) + 1e-10
|
|
||||||
skew = np.mean(((diffs - mean_dt) / std_dt) ** 3) if std_dt > 1e-8 else 0.0
|
|
||||||
return np.array([mean_dt, std_dt, skew, len(diffs)], dtype=np.float32)
|
|
||||||
|
|
||||||
|
|
||||||
def state_coverage(events: List, state_fn: Callable, mdp_states: Set[str]) -> float:
|
|
||||||
"""fraction of MDP states visited by trajectory
|
|
||||||
events: list of event objects/dicts
|
|
||||||
state_fn: function mapping event -> state string
|
|
||||||
mdp_states: set of all possible MDP states
|
|
||||||
"""
|
|
||||||
if not mdp_states:
|
|
||||||
return 0.0
|
|
||||||
visited = set(state_fn(e) for e in events)
|
|
||||||
return len(visited & mdp_states) / len(mdp_states)
|
|
||||||
|
|
||||||
|
|
||||||
def transition_entropy(events: List, state_fn: Callable) -> float:
|
|
||||||
"""compute entropy of transition distribution (randomness of navigation)
|
|
||||||
higher entropy = more random browsing pattern
|
|
||||||
"""
|
|
||||||
if len(events) < 2:
|
|
||||||
return 0.0
|
|
||||||
states = [state_fn(e) for e in events]
|
|
||||||
trans_counts = defaultdict(int)
|
|
||||||
for s, s_next in zip(states, states[1:]):
|
|
||||||
trans_counts[(s, s_next)] += 1
|
|
||||||
total = sum(trans_counts.values())
|
|
||||||
probs = [c / total for c in trans_counts.values()]
|
|
||||||
return -sum(p * np.log(p + 1e-10) for p in probs)
|
|
||||||
|
|
||||||
|
|
||||||
def event_type_distribution(events: List, event_name_fn: Callable) -> np.ndarray:
|
|
||||||
"""compute proportions of different event type categories
|
|
||||||
returns: [page_view_ratio, hover_ratio, cart_ratio, purchase_ratio]
|
|
||||||
"""
|
|
||||||
if not events:
|
|
||||||
return np.zeros(4, dtype=np.float32)
|
|
||||||
n = len(events)
|
|
||||||
names = [event_name_fn(e).lower() for e in events]
|
|
||||||
return np.array([
|
|
||||||
sum(1 for nm in names if 'page' in nm or 'view' in nm) / n,
|
|
||||||
sum(1 for nm in names if 'hover' in nm) / n,
|
|
||||||
sum(1 for nm in names if 'cart' in nm) / n,
|
|
||||||
sum(1 for nm in names if 'purchase' in nm or 'checkout' in nm) / n
|
|
||||||
], dtype=np.float32)
|
|
||||||
|
|
||||||
|
|
||||||
def featurize_trajectory(events: List, state_fn: Callable, ts_fn: Callable,
|
|
||||||
event_name_fn: Callable, mdp_states: Optional[Set[str]] = None,
|
|
||||||
output_dim: int = 64) -> np.ndarray:
|
|
||||||
"""convert trajectory to fixed-dimension feature vector
|
|
||||||
events: list of event objects/dicts
|
|
||||||
state_fn: function mapping event -> state string
|
|
||||||
ts_fn: function mapping event -> timestamp (float)
|
|
||||||
event_name_fn: function mapping event -> event name string
|
|
||||||
mdp_states: optional set of all MDP states for coverage calculation
|
|
||||||
output_dim: desired output dimension (will pad/truncate)
|
|
||||||
"""
|
|
||||||
feats = []
|
|
||||||
feats.extend(transition_histogram(events, state_fn, max_states=40)) # 40 dims
|
|
||||||
feats.extend(temporal_signature(events, ts_fn)) # 4 dims
|
|
||||||
feats.append(state_coverage(events, state_fn, mdp_states or set())) # 1 dim
|
|
||||||
feats.append(transition_entropy(events, state_fn)) # 1 dim
|
|
||||||
feats.append(float(len(events))) # trajectory length
|
|
||||||
feats.append(float(len(set(state_fn(e) for e in events)))) # unique states
|
|
||||||
feats.extend(event_type_distribution(events, event_name_fn)) # 4 dims
|
|
||||||
|
|
||||||
feats = np.array(feats[:output_dim], dtype=np.float32)
|
|
||||||
if len(feats) < output_dim:
|
|
||||||
feats = np.pad(feats, (0, output_dim - len(feats)))
|
|
||||||
return feats
|
|
||||||
|
|
||||||
|
|
||||||
def parse_timestamp(ts: Any) -> float:
|
|
||||||
"""parse various timestamp formats to float seconds"""
|
|
||||||
if ts is None:
|
|
||||||
return 0.0
|
|
||||||
if isinstance(ts, (int, float)):
|
|
||||||
return float(ts)
|
|
||||||
if isinstance(ts, str):
|
|
||||||
try:
|
|
||||||
return datetime.fromisoformat(ts.replace('Z', '+00:00')).timestamp()
|
|
||||||
except ValueError:
|
|
||||||
return 0.0
|
|
||||||
return 0.0
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
from kafka import KafkaConsumer
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from dotenv import load_dotenv
|
|
||||||
load_dotenv()
|
|
||||||
|
|
||||||
def get_interactions(
|
|
||||||
topic='user-interactions',
|
|
||||||
bootstrap_servers=None,
|
|
||||||
from_beginning=True,
|
|
||||||
max_records=None,
|
|
||||||
timeout_ms=5000
|
|
||||||
):
|
|
||||||
"""Consume interaction events from Kafka.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
topic: Kafka topic name
|
|
||||||
bootstrap_servers: Kafka broker address (default from env)
|
|
||||||
from_beginning: Start from earliest offset if True
|
|
||||||
max_records: Max number of records to fetch (None = all available)
|
|
||||||
timeout_ms: Consumer poll timeout
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List of parsed interaction event dicts
|
|
||||||
"""
|
|
||||||
if not bootstrap_servers:
|
|
||||||
host = os.getenv('KAFKA_HOST', 'localhost')
|
|
||||||
port = os.getenv('KAFKA_PORT', '9092')
|
|
||||||
bootstrap_servers = f'{host}:{port}'
|
|
||||||
|
|
||||||
consumer = KafkaConsumer(
|
|
||||||
topic,
|
|
||||||
bootstrap_servers=bootstrap_servers,
|
|
||||||
auto_offset_reset='earliest' if from_beginning else 'latest',
|
|
||||||
enable_auto_commit=False,
|
|
||||||
value_deserializer=lambda m: json.loads(m.decode('utf-8')),
|
|
||||||
consumer_timeout_ms=timeout_ms
|
|
||||||
)
|
|
||||||
|
|
||||||
events = []
|
|
||||||
try:
|
|
||||||
for msg in consumer:
|
|
||||||
events.append(msg.value)
|
|
||||||
if max_records and len(events) >= max_records:
|
|
||||||
break
|
|
||||||
finally:
|
|
||||||
consumer.close()
|
|
||||||
|
|
||||||
return events
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
interactions = get_interactions(max_records=10)
|
|
||||||
for event in interactions:
|
|
||||||
print(event)
|
|
||||||
@@ -2,14 +2,11 @@ import redis
|
|||||||
import pickle
|
import pickle
|
||||||
import json
|
import json
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from io import StringIO
|
|
||||||
from typing import Optional, Dict, Any
|
from typing import Optional, Dict, Any
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ModelRegistry:
|
class ModelRegistry:
|
||||||
"""
|
"""
|
||||||
Lightweight model registry using Redis for storing pricing models and elasticity data.
|
Lightweight model registry using Redis for storing pricing models and elasticity data.
|
||||||
@@ -17,23 +14,24 @@ class ModelRegistry:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, redis_host: str = None, redis_port: int = None):
|
def __init__(self, redis_host: str = None, redis_port: int = None):
|
||||||
host = redis_host or os.getenv("REDIS_HOST", "localhost")
|
host = redis_host or os.getenv('REDIS_HOST', 'localhost')
|
||||||
port = redis_port or int(os.getenv("REDIS_PORT", "6378"))
|
port = redis_port or int(os.getenv('REDIS_PORT', '6378'))
|
||||||
|
|
||||||
self.redis_client = redis.Redis(
|
self.redis_client = redis.Redis(
|
||||||
host=host, port=port, db=0, decode_responses=False
|
host=host,
|
||||||
|
port=port,
|
||||||
|
db=0,
|
||||||
|
decode_responses=False
|
||||||
)
|
)
|
||||||
self.metadata_prefix = "model:meta:"
|
self.metadata_prefix = "model:meta:"
|
||||||
self.data_prefix = "model:data:"
|
self.data_prefix = "model:data:"
|
||||||
self.elasticity_prefix = "elasticity:"
|
self.elasticity_prefix = "elasticity:"
|
||||||
self.prices_prefix = "prices:"
|
self.prices_prefix = "prices:"
|
||||||
|
|
||||||
def publish_elasticity(
|
def publish_elasticity(self,
|
||||||
self,
|
elasticity_df: pd.DataFrame,
|
||||||
elasticity_df: pd.DataFrame,
|
model_name: str = 'latest',
|
||||||
model_name: str = "latest",
|
metadata: Optional[Dict[str, Any]] = None):
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Store elasticity estimates in registry.
|
Store elasticity estimates in registry.
|
||||||
|
|
||||||
@@ -45,29 +43,25 @@ class ModelRegistry:
|
|||||||
key = f"{self.elasticity_prefix}{model_name}"
|
key = f"{self.elasticity_prefix}{model_name}"
|
||||||
|
|
||||||
# serialize dataframe as JSON
|
# serialize dataframe as JSON
|
||||||
data_json = elasticity_df.to_json(orient="records")
|
data_json = elasticity_df.to_json(orient='records')
|
||||||
|
|
||||||
# store data
|
# store data
|
||||||
self.redis_client.set(key, data_json)
|
self.redis_client.set(key, data_json)
|
||||||
|
|
||||||
# store metadata
|
# store metadata
|
||||||
meta = metadata or {}
|
meta = metadata or {}
|
||||||
meta.update(
|
meta.update({
|
||||||
{
|
'n_products': len(elasticity_df),
|
||||||
"n_products": len(elasticity_df),
|
'mean_elasticity': float(elasticity_df['elasticity'].mean()),
|
||||||
"mean_elasticity": float(elasticity_df["elasticity"].mean()),
|
'model_type': 'elasticity_snapshot'
|
||||||
"model_type": "elasticity_snapshot",
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
meta_key = f"{self.metadata_prefix}{model_name}"
|
meta_key = f"{self.metadata_prefix}{model_name}"
|
||||||
self.redis_client.set(meta_key, json.dumps(meta))
|
self.redis_client.set(meta_key, json.dumps(meta))
|
||||||
|
|
||||||
log.info(
|
log.info(f"Published elasticity model '{model_name}' with {len(elasticity_df)} products")
|
||||||
f"Published elasticity model '{model_name}' with {len(elasticity_df)} products"
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_elasticity(self, model_name: str = "latest") -> Optional[pd.DataFrame]:
|
def get_elasticity(self, model_name: str = 'latest') -> Optional[pd.DataFrame]:
|
||||||
"""Retrieve elasticity estimates from registry."""
|
"""Retrieve elasticity estimates from registry."""
|
||||||
key = f"{self.elasticity_prefix}{model_name}"
|
key = f"{self.elasticity_prefix}{model_name}"
|
||||||
data_json = self.redis_client.get(key)
|
data_json = self.redis_client.get(key)
|
||||||
@@ -77,16 +71,14 @@ class ModelRegistry:
|
|||||||
|
|
||||||
# decode bytes to string if needed
|
# decode bytes to string if needed
|
||||||
if isinstance(data_json, bytes):
|
if isinstance(data_json, bytes):
|
||||||
data_json = data_json.decode("utf-8")
|
data_json = data_json.decode('utf-8')
|
||||||
|
|
||||||
return pd.read_json(StringIO(data_json), orient="records")
|
return pd.read_json(data_json, orient='records')
|
||||||
|
|
||||||
def publish_pricing_model(
|
def publish_pricing_model(self,
|
||||||
self,
|
pricing_function,
|
||||||
pricing_function,
|
model_name: str = 'latest',
|
||||||
model_name: str = "latest",
|
metadata: Optional[Dict[str, Any]] = None):
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Store a fitted pricing function object.
|
Store a fitted pricing function object.
|
||||||
|
|
||||||
@@ -103,19 +95,17 @@ class ModelRegistry:
|
|||||||
|
|
||||||
# store metadata
|
# store metadata
|
||||||
meta = metadata or {}
|
meta = metadata or {}
|
||||||
meta.update(
|
meta.update({
|
||||||
{
|
'model_class': pricing_function.__class__.__name__,
|
||||||
"model_class": pricing_function.__class__.__name__,
|
'model_type': 'pricing_function'
|
||||||
"model_type": "pricing_function",
|
})
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
meta_key = f"{self.metadata_prefix}{model_name}"
|
meta_key = f"{self.metadata_prefix}{model_name}"
|
||||||
self.redis_client.set(meta_key, json.dumps(meta))
|
self.redis_client.set(meta_key, json.dumps(meta))
|
||||||
|
|
||||||
log.info(f"Published pricing model '{model_name}' ({meta['model_class']})")
|
log.info(f"Published pricing model '{model_name}' ({meta['model_class']})")
|
||||||
|
|
||||||
def get_pricing_model(self, model_name: str = "latest"):
|
def get_pricing_model(self, model_name: str = 'latest'):
|
||||||
"""Retrieve a pricing function from registry."""
|
"""Retrieve a pricing function from registry."""
|
||||||
key = f"{self.data_prefix}{model_name}"
|
key = f"{self.data_prefix}{model_name}"
|
||||||
model_bytes = self.redis_client.get(key)
|
model_bytes = self.redis_client.get(key)
|
||||||
@@ -130,23 +120,21 @@ class ModelRegistry:
|
|||||||
models = {}
|
models = {}
|
||||||
|
|
||||||
for key in self.redis_client.scan_iter(f"{self.metadata_prefix}*"):
|
for key in self.redis_client.scan_iter(f"{self.metadata_prefix}*"):
|
||||||
key_str = key.decode("utf-8") if isinstance(key, bytes) else key
|
key_str = key.decode('utf-8') if isinstance(key, bytes) else key
|
||||||
model_name = key_str.replace(self.metadata_prefix, "")
|
model_name = key_str.replace(self.metadata_prefix, '')
|
||||||
meta_json = self.redis_client.get(key)
|
meta_json = self.redis_client.get(key)
|
||||||
|
|
||||||
if meta_json:
|
if meta_json:
|
||||||
if isinstance(meta_json, bytes):
|
if isinstance(meta_json, bytes):
|
||||||
meta_json = meta_json.decode("utf-8")
|
meta_json = meta_json.decode('utf-8')
|
||||||
models[model_name] = json.loads(meta_json)
|
models[model_name] = json.loads(meta_json)
|
||||||
|
|
||||||
return models
|
return models
|
||||||
|
|
||||||
def publish_prices(
|
def publish_prices(self,
|
||||||
self,
|
prices_df: pd.DataFrame,
|
||||||
prices_df: pd.DataFrame,
|
model_name: str = 'latest',
|
||||||
model_name: str = "latest",
|
metadata: Optional[Dict[str, Any]] = None):
|
||||||
metadata: Optional[Dict[str, Any]] = None,
|
|
||||||
):
|
|
||||||
"""Store predicted prices in registry.
|
"""Store predicted prices in registry.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -155,19 +143,22 @@ class ModelRegistry:
|
|||||||
metadata: additional info
|
metadata: additional info
|
||||||
"""
|
"""
|
||||||
key = f"{self.prices_prefix}{model_name}"
|
key = f"{self.prices_prefix}{model_name}"
|
||||||
data_json = prices_df.to_json(orient="records")
|
data_json = prices_df.to_json(orient='records')
|
||||||
|
|
||||||
self.redis_client.set(key, data_json)
|
self.redis_client.set(key, data_json)
|
||||||
|
|
||||||
meta = metadata or {}
|
meta = metadata or {}
|
||||||
meta.update({"n_products": len(prices_df), "model_type": "predicted_prices"})
|
meta.update({
|
||||||
|
'n_products': len(prices_df),
|
||||||
|
'model_type': 'predicted_prices'
|
||||||
|
})
|
||||||
|
|
||||||
meta_key = f"{self.metadata_prefix}prices_{model_name}"
|
meta_key = f"{self.metadata_prefix}prices_{model_name}"
|
||||||
self.redis_client.set(meta_key, json.dumps(meta))
|
self.redis_client.set(meta_key, json.dumps(meta))
|
||||||
|
|
||||||
log.info(f"Published prices '{model_name}' for {len(prices_df)} products")
|
log.info(f"Published prices '{model_name}' for {len(prices_df)} products")
|
||||||
|
|
||||||
def get_prices(self, model_name: str = "latest") -> Optional[pd.DataFrame]:
|
def get_prices(self, model_name: str = 'latest') -> Optional[pd.DataFrame]:
|
||||||
"""Retrieve predicted prices from registry."""
|
"""Retrieve predicted prices from registry."""
|
||||||
key = f"{self.prices_prefix}{model_name}"
|
key = f"{self.prices_prefix}{model_name}"
|
||||||
data_json = self.redis_client.get(key)
|
data_json = self.redis_client.get(key)
|
||||||
@@ -176,9 +167,9 @@ class ModelRegistry:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
if isinstance(data_json, bytes):
|
if isinstance(data_json, bytes):
|
||||||
data_json = data_json.decode("utf-8")
|
data_json = data_json.decode('utf-8')
|
||||||
|
|
||||||
return pd.read_json(StringIO(data_json), orient="records")
|
return pd.read_json(data_json, orient='records')
|
||||||
|
|
||||||
def health_check(self) -> bool:
|
def health_check(self) -> bool:
|
||||||
"""Check if Redis connection is alive."""
|
"""Check if Redis connection is alive."""
|
||||||
@@ -187,55 +178,3 @@ class ModelRegistry:
|
|||||||
return True
|
return True
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def set_session_prices(
|
|
||||||
self, session_id: str, prices: Dict[str, float], ttl: int = 1800
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Store prices for a specific session.
|
|
||||||
THIS is the write path for session-aware pricing.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
session_id: session identifier
|
|
||||||
prices: dict of {productId: price}
|
|
||||||
ttl: time-to-live in seconds (default 30min)
|
|
||||||
"""
|
|
||||||
if not prices:
|
|
||||||
return
|
|
||||||
|
|
||||||
key = f"session:{session_id}:prices"
|
|
||||||
# use Redis hash for O(1) lookup per product
|
|
||||||
self.redis_client.hset(key, mapping={k: str(v) for k, v in prices.items()})
|
|
||||||
self.redis_client.expire(key, ttl)
|
|
||||||
|
|
||||||
def get_session_price(self, session_id: str, product_id: str) -> Optional[float]:
|
|
||||||
"""
|
|
||||||
Lookup price for (sessionId, productId).
|
|
||||||
THIS is the read path for fast provider lookup.
|
|
||||||
|
|
||||||
Returns: price or None if not found
|
|
||||||
"""
|
|
||||||
key = f"session:{session_id}:prices"
|
|
||||||
price_str = self.redis_client.hget(key, product_id)
|
|
||||||
|
|
||||||
if price_str is None:
|
|
||||||
return None
|
|
||||||
|
|
||||||
return float(
|
|
||||||
price_str.decode("utf-8") if isinstance(price_str, bytes) else price_str
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_session_all_prices(self, session_id: str) -> Dict[str, float]:
|
|
||||||
"""Get all prices for a session."""
|
|
||||||
key = f"session:{session_id}:prices"
|
|
||||||
prices_raw = self.redis_client.hgetall(key)
|
|
||||||
|
|
||||||
if not prices_raw:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
return {
|
|
||||||
(k.decode("utf-8") if isinstance(k, bytes) else k): float(
|
|
||||||
v.decode("utf-8") if isinstance(v, bytes) else v
|
|
||||||
)
|
|
||||||
for k, v in prices_raw.items()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,128 +0,0 @@
|
|||||||
"""Utilities for loading separability artifacts and scoring interaction sessions."""
|
|
||||||
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import json
|
|
||||||
from dataclasses import dataclass
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Dict, Iterable, List, Sequence
|
|
||||||
|
|
||||||
import joblib
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
from experiments.ml.arch import featurize_trajectory
|
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_ARTIFACT_DIR = Path("data/separability")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class SeparabilityArtifacts:
|
|
||||||
scaler: object
|
|
||||||
classifier: object
|
|
||||||
states: List[str]
|
|
||||||
event_transitions: Dict[str, Dict[str, float]]
|
|
||||||
feature_dim: int
|
|
||||||
|
|
||||||
|
|
||||||
def _normalize_events(raw_events: Sequence[object]) -> List[object]:
|
|
||||||
events: List[object] = []
|
|
||||||
for evt in raw_events:
|
|
||||||
if hasattr(evt, "value") and hasattr(evt.value, "payload"):
|
|
||||||
events.append(evt.value.payload)
|
|
||||||
else:
|
|
||||||
events.append(evt)
|
|
||||||
events.sort(key=lambda e: getattr(e, "ts", ""))
|
|
||||||
return events
|
|
||||||
|
|
||||||
|
|
||||||
def _event_transition_distribution(events: Sequence[object]) -> Dict[str, Dict[str, float]]:
|
|
||||||
counts: Dict[str, Dict[str, int]] = {}
|
|
||||||
for src_evt, dst_evt in zip(events, events[1:]):
|
|
||||||
src_name = getattr(src_evt, "eventName", "unknown")
|
|
||||||
dst_name = getattr(dst_evt, "eventName", "unknown")
|
|
||||||
counts.setdefault(src_name, {})
|
|
||||||
counts[src_name][dst_name] = counts[src_name].get(dst_name, 0) + 1
|
|
||||||
|
|
||||||
distribution: Dict[str, Dict[str, float]] = {}
|
|
||||||
for src, dsts in counts.items():
|
|
||||||
total = float(sum(dsts.values()))
|
|
||||||
distribution[src] = {dst: val / total for dst, val in dsts.items()} if total else {}
|
|
||||||
return distribution
|
|
||||||
|
|
||||||
|
|
||||||
def _kl_divergence(p: Dict[str, Dict[str, float]], q: Dict[str, Dict[str, float]]) -> float:
|
|
||||||
eps = 1e-10
|
|
||||||
total = 0.0
|
|
||||||
for src, dsts in p.items():
|
|
||||||
for dst, prob in dsts.items():
|
|
||||||
ref = q.get(src, {}).get(dst, 0.0)
|
|
||||||
total += (prob + eps) * np.log((prob + eps) / (ref + eps))
|
|
||||||
return float(total)
|
|
||||||
|
|
||||||
|
|
||||||
def load_artifacts(artifact_dir: Path | str = DEFAULT_ARTIFACT_DIR) -> SeparabilityArtifacts:
|
|
||||||
artifact_dir = Path(artifact_dir)
|
|
||||||
scaler_path = artifact_dir / "scaler.joblib"
|
|
||||||
model_path = artifact_dir / "classifier.joblib"
|
|
||||||
metadata_path = artifact_dir / "metadata.json"
|
|
||||||
|
|
||||||
if not (scaler_path.exists() and model_path.exists() and metadata_path.exists()):
|
|
||||||
raise FileNotFoundError(
|
|
||||||
f"Separability artifacts not found in {artifact_dir}. Run sim.strong_learner.train first."
|
|
||||||
)
|
|
||||||
|
|
||||||
scaler = joblib.load(scaler_path)
|
|
||||||
classifier = joblib.load(model_path)
|
|
||||||
with open(metadata_path, "r", encoding="utf-8") as fin:
|
|
||||||
metadata = json.load(fin)
|
|
||||||
|
|
||||||
return SeparabilityArtifacts(
|
|
||||||
scaler=scaler,
|
|
||||||
classifier=classifier,
|
|
||||||
states=list(metadata["reference_states"]),
|
|
||||||
event_transitions=metadata["event_transitions"],
|
|
||||||
feature_dim=int(metadata["feature_dim"]),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def score_session(
|
|
||||||
raw_events: Sequence[object],
|
|
||||||
artifacts: SeparabilityArtifacts,
|
|
||||||
) -> dict:
|
|
||||||
events = _normalize_events(raw_events)
|
|
||||||
if not events:
|
|
||||||
return {"prob_agent": 0.0, "delta_h": 0.0, "delta_a": 0.0}
|
|
||||||
|
|
||||||
reference_mdp = {"states": artifacts.states}
|
|
||||||
features = featurize_trajectory(events, mdp=reference_mdp, input_dim=artifacts.feature_dim)
|
|
||||||
scaled = artifacts.scaler.transform(features.reshape(1, -1))
|
|
||||||
prob_agent = float(artifacts.classifier.predict_proba(scaled)[0, 1])
|
|
||||||
|
|
||||||
session_dist = _event_transition_distribution(events)
|
|
||||||
delta_h = _kl_divergence(session_dist, artifacts.event_transitions.get("human", {}))
|
|
||||||
delta_a = _kl_divergence(session_dist, artifacts.event_transitions.get("agent", {}))
|
|
||||||
|
|
||||||
return {
|
|
||||||
"prob_agent": prob_agent,
|
|
||||||
"delta_h": delta_h,
|
|
||||||
"delta_a": delta_a,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def estimate_alpha(prob_agent: float, delta_h: float, delta_a: float, temperature: float = 1.0) -> float:
|
|
||||||
divergence_mass = delta_h + delta_a
|
|
||||||
if divergence_mass <= 1e-8:
|
|
||||||
return float(prob_agent)
|
|
||||||
|
|
||||||
ratio = delta_a / divergence_mass
|
|
||||||
blended = 0.5 * prob_agent + 0.5 * ratio
|
|
||||||
if temperature <= 0:
|
|
||||||
return float(np.clip(blended, 0.0, 1.0))
|
|
||||||
|
|
||||||
scaled = 1.0 / (1.0 + np.exp(-temperature * (blended - 0.5)))
|
|
||||||
return float(np.clip(scaled, 0.0, 1.0))
|
|
||||||
|
|
||||||
|
|
||||||
def score_sessions(raw_sessions: Iterable[Sequence[object]], artifacts: SeparabilityArtifacts) -> List[dict]:
|
|
||||||
return [score_session(events, artifacts) for events in raw_sessions]
|
|
||||||
72
lib/state.py
72
lib/state.py
@@ -1,72 +0,0 @@
|
|||||||
"""Unified state representation utilities for MDP state encoding
|
|
||||||
Used by both experiments/ and sim/ components for consistent state handling
|
|
||||||
"""
|
|
||||||
from typing import Any, Callable
|
|
||||||
|
|
||||||
|
|
||||||
def make_state_repr(page: str = None, product_id: str = None, event_name: str = None) -> str:
|
|
||||||
"""create canonical state representation string from components
|
|
||||||
format: page|productId|eventName
|
|
||||||
"""
|
|
||||||
p = page or 'unk'
|
|
||||||
pid = product_id or 'none'
|
|
||||||
en = event_name or 'unknown'
|
|
||||||
return f"{p}|{pid}|{en}"
|
|
||||||
|
|
||||||
|
|
||||||
def event_to_state(evt: Any) -> str:
|
|
||||||
"""convert event object/dict to state string
|
|
||||||
supports both object attributes and dict keys
|
|
||||||
"""
|
|
||||||
if isinstance(evt, dict):
|
|
||||||
return make_state_repr(
|
|
||||||
page=evt.get('page'),
|
|
||||||
product_id=evt.get('productId'),
|
|
||||||
event_name=evt.get('eventName') or evt.get('event_type')
|
|
||||||
)
|
|
||||||
return make_state_repr(
|
|
||||||
page=getattr(evt, 'page', None),
|
|
||||||
product_id=getattr(evt, 'productId', None),
|
|
||||||
event_name=getattr(evt, 'eventName', None) or getattr(evt, 'event_type', None)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_state(state_str: str) -> dict:
|
|
||||||
"""parse state string back to components
|
|
||||||
returns: {'page': str, 'productId': str, 'eventName': str}
|
|
||||||
"""
|
|
||||||
parts = state_str.split('|')
|
|
||||||
return {
|
|
||||||
'page': parts[0] if len(parts) > 0 and parts[0] != 'unk' else None,
|
|
||||||
'productId': parts[1] if len(parts) > 1 and parts[1] != 'none' else None,
|
|
||||||
'eventName': parts[2] if len(parts) > 2 and parts[2] != 'unknown' else None
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def get_event_name(evt: Any) -> str:
|
|
||||||
"""extract event name from event object/dict"""
|
|
||||||
if isinstance(evt, dict):
|
|
||||||
return evt.get('eventName') or evt.get('event_type') or ''
|
|
||||||
return getattr(evt, 'eventName', None) or getattr(evt, 'event_type', None) or ''
|
|
||||||
|
|
||||||
|
|
||||||
def get_timestamp(evt: Any) -> Any:
|
|
||||||
"""extract timestamp from event object/dict"""
|
|
||||||
if isinstance(evt, dict):
|
|
||||||
return evt.get('ts') or evt.get('timestamp')
|
|
||||||
return getattr(evt, 'ts', None) or getattr(evt, 'timestamp', None)
|
|
||||||
|
|
||||||
|
|
||||||
def create_state_fn() -> Callable:
|
|
||||||
"""factory for state representation function"""
|
|
||||||
return event_to_state
|
|
||||||
|
|
||||||
|
|
||||||
def create_event_name_fn() -> Callable:
|
|
||||||
"""factory for event name extraction function"""
|
|
||||||
return get_event_name
|
|
||||||
|
|
||||||
|
|
||||||
def create_timestamp_fn() -> Callable:
|
|
||||||
"""factory for timestamp extraction function (returns raw value, use features.parse_timestamp to convert)"""
|
|
||||||
return get_timestamp
|
|
||||||
@@ -42,10 +42,6 @@ EOF
|
|||||||
# Process each directory
|
# Process each directory
|
||||||
echo "Concatenating code from source directories..."
|
echo "Concatenating code from source directories..."
|
||||||
|
|
||||||
# Engine
|
|
||||||
find "$PROJECT_ROOT/engine" -type d \( -name ".venv" -o -name "__pycache__" -o -name "*.egg-info" -o -name "node_modules" -o -name ".pytest_cache" \) -prune -o -type f \( -name "*.py" -o -name "*.js" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" \) ! -name "*.pyc" ! -name "*.pyo" -print | sort | while read -r file; do
|
|
||||||
add_file "$file"
|
|
||||||
done
|
|
||||||
# Backend
|
# Backend
|
||||||
find "$PROJECT_ROOT/backend" -type d \( -name ".venv" -o -name "__pycache__" -o -name "*.egg-info" -o -name "node_modules" -o -name ".pytest_cache" \) -prune -o -type f \( -name "*.py" -o -name "*.js" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" \) ! -name "*.pyc" ! -name "*.pyo" -print | sort | while read -r file; do
|
find "$PROJECT_ROOT/backend" -type d \( -name ".venv" -o -name "__pycache__" -o -name "*.egg-info" -o -name "node_modules" -o -name ".pytest_cache" \) -prune -o -type f \( -name "*.py" -o -name "*.js" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" \) ! -name "*.pyc" ! -name "*.pyo" -print | sort | while read -r file; do
|
||||||
add_file "$file"
|
add_file "$file"
|
||||||
@@ -57,7 +53,7 @@ find "$PROJECT_ROOT/experiments" -type d \( -name ".venv" -o -name "__pycache__"
|
|||||||
done
|
done
|
||||||
|
|
||||||
# Docker
|
# Docker
|
||||||
find "$PROJECT_ROOT/docker" -type d \( -name ".venv" -o -name "__pycache__" -o -name "node_modules" \) -prune -o -type f \( -name "*.py" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" -o -name "*.Dockerfile*" \) ! -name "*.pyc" ! -name "*.pyo" -print | sort | while read -r file; do
|
find "$PROJECT_ROOT/docker" -type d \( -name ".venv" -o -name "__pycache__" -o -name "node_modules" \) -prune -o -type f \( -name "*.py" -o -name "*.sh" -o -name "*.yml" -o -name "*.yaml" -o -name "Dockerfile*" \) ! -name "*.pyc" ! -name "*.pyo" -print | sort | while read -r file; do
|
||||||
add_file "$file"
|
add_file "$file"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
"chapters/04-results"
|
"chapters/04-results"
|
||||||
"chapters/05-discussion"
|
"chapters/05-discussion"
|
||||||
"chapters/06-conclusion"
|
"chapters/06-conclusion"
|
||||||
|
"../build/concatenated_code"
|
||||||
"article"
|
"article"
|
||||||
"art12"))
|
"art12"))
|
||||||
:latex)
|
:latex)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/Q7J5EBEJ/3447815.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/Q7J5EBEJ/3447815.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@phdthesis{salassa_politecnico_2024,
|
@phdthesis{salassa_politecnico_nodate,
|
||||||
title = {Politecnico di {Torino} {Algorithmic} {Pricing} in the digital age "{Ethical} considerations on its economic and social implications, and an analysis of possible solutions to overcome its critical issues" {Tutor}: {Candidate}},
|
title = {Politecnico di {Torino} {Algorithmic} {Pricing} in the digital age "{Ethical} considerations on its economic and social implications, and an analysis of possible solutions to overcome its critical issues" {Tutor}: {Candidate}},
|
||||||
abstract = {Algorithmic pricing is an emerging business practice that uses computational algorithms to determine
|
abstract = {Algorithmic pricing is an emerging business practice that uses computational algorithms to determine
|
||||||
the prices of products and services based on a number of dynamic factors. The aim of this thesis is to
|
the prices of products and services based on a number of dynamic factors. The aim of this thesis is to
|
||||||
@@ -50,8 +50,6 @@ laws, for fair and non-discriminatory use.},
|
|||||||
urldate = {2025-11-12},
|
urldate = {2025-11-12},
|
||||||
school = {Politecnico di Torino},
|
school = {Politecnico di Torino},
|
||||||
author = {Salassa, Fabio and Pautassi, Paolo},
|
author = {Salassa, Fabio and Pautassi, Paolo},
|
||||||
month = apr,
|
|
||||||
year = {2024},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/L95WYQ8B/m-api-06aad998-d926-0d59-5593-82fdce5a678b.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/L95WYQ8B/m-api-06aad998-d926-0d59-5593-82fdce5a678b.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,12 +62,11 @@ laws, for fair and non-discriminatory use.},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/IZD3C5SR/m-api-26f6207c-cc89-4aed-29b6-34629f18fe9b.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/IZD3C5SR/m-api-26f6207c-cc89-4aed-29b6-34629f18fe9b.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@article{shahidi_coasean_2025,
|
@article{shahidi_coasean_nodate,
|
||||||
title = {The {Coasean} {Singularity}? {Demand}, {Supply}, and {Market} {Design} with {AI} {Agents}},
|
title = {The {Coasean} {Singularity}? {Demand}, {Supply}, and {Market} {Design} with {AI} {Agents}},
|
||||||
abstract = {AI agents—autonomous systems that perceive, reason, and act on behalf of human principals—are poised to transform digital markets by dramatically reducing transaction costs. This chapter evaluates the economic implications of this transition, adopting a consumeroriented view of agents as market participants that can search, negotiate, and transact directly. From the demand side, agent adoption reflects derived demand: users trade off decision quality against effort reduction, with outcomes mediated by agent capability and task context. On the supply side, firms will design, integrate, and monetize agents, with outcomes hinging on whether agents operate within or across platforms. At the market level, agents create efficiency gains from lower search, communication, and contracting costs, but also introduce frictions such as congestion and price obfuscation. By lowering the costs of preference elicitation, contract enforcement, and identity verification, agents expand the feasible set of market designs but also raise novel regulatory challenges. While the net welfare effects remain an empirical question, the rapid onset of AI-mediated transactions presents a unique opportunity for economic research to inform real-world policy and market design.},
|
abstract = {AI agents—autonomous systems that perceive, reason, and act on behalf of human principals—are poised to transform digital markets by dramatically reducing transaction costs. This chapter evaluates the economic implications of this transition, adopting a consumeroriented view of agents as market participants that can search, negotiate, and transact directly. From the demand side, agent adoption reflects derived demand: users trade off decision quality against effort reduction, with outcomes mediated by agent capability and task context. On the supply side, firms will design, integrate, and monetize agents, with outcomes hinging on whether agents operate within or across platforms. At the market level, agents create efficiency gains from lower search, communication, and contracting costs, but also introduce frictions such as congestion and price obfuscation. By lowering the costs of preference elicitation, contract enforcement, and identity verification, agents expand the feasible set of market designs but also raise novel regulatory challenges. While the net welfare effects remain an empirical question, the rapid onset of AI-mediated transactions presents a unique opportunity for economic research to inform real-world policy and market design.},
|
||||||
language = {en},
|
language = {en},
|
||||||
author = {Shahidi, Peyman and Rusak, Gili and Manning, Benjamin S and Fradkin, Andrey and Horton, John J},
|
author = {Shahidi, Peyman and Rusak, Gili and Manning, Benjamin S and Fradkin, Andrey and Horton, John J},
|
||||||
year = {2025},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/TQCAPJDP/Shahidi et al. - The Coasean Singularity Demand, Supply, and Market Design with AI Agents.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/TQCAPJDP/Shahidi et al. - The Coasean Singularity Demand, Supply, and Market Design with AI Agents.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,14 +84,10 @@ laws, for fair and non-discriminatory use.},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/ZLJQ4DQ9/Byrnes - 2025 - Intro to Brain-Like-AGI Safety.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/ZLJQ4DQ9/Byrnes - 2025 - Intro to Brain-Like-AGI Safety.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@article{shannon_mathematical_1948,
|
@article{shannon_mathematical_nodate,
|
||||||
title = {A {Mathematical} {Theory} of {Communication}},
|
title = {A {Mathematical} {Theory} of {Communication}},
|
||||||
volume = {27},
|
|
||||||
language = {en},
|
language = {en},
|
||||||
journal = {Bell System Technical Journal},
|
|
||||||
author = {Shannon, C E},
|
author = {Shannon, C E},
|
||||||
month = oct,
|
|
||||||
year = {1948},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/FJRFRWK2/Shannon - A Mathematical Theory of Communication.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/FJRFRWK2/Shannon - A Mathematical Theory of Communication.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,13 +96,11 @@ laws, for fair and non-discriminatory use.},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/D3QRGY9Z/order_stats.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/D3QRGY9Z/order_stats.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@article{devine_nonlinear_2017,
|
@article{devine_nonlinear_nodate,
|
||||||
title = {Nonlinear {Pricing} with {Costly} {Information} {Acquisition}},
|
title = {Nonlinear {Pricing} with {Costly} {Information} {Acquisition}},
|
||||||
abstract = {This paper examines a nonlinear pricing model where the firm can choose to acquire costly information prior to offering contract menus to consumers; such as paying a consultant or investing in machine learning technologies. Information provides the firm with a signal about consumers types, whose accuracy increases as the firm acquires larger amounts of information. We show that the firm chooses to acquire information, only if it can purchase a sufficient amount that could alter its initial prior beliefs. Relative to standard settings where firms cannot acquire information, we identify how information acquisition changes optimal contract offers, equilibrium profits, information rents, and welfare. A better-informed firm increases its expected profits, but it can also increase expected utility when the cost of information is intermediate. Our results recommend balanced online privacy laws.},
|
abstract = {This paper examines a nonlinear pricing model where the firm can choose to acquire costly information prior to offering contract menus to consumers; such as paying a consultant or investing in machine learning technologies. Information provides the firm with a signal about consumers types, whose accuracy increases as the firm acquires larger amounts of information. We show that the firm chooses to acquire information, only if it can purchase a sufficient amount that could alter its initial prior beliefs. Relative to standard settings where firms cannot acquire information, we identify how information acquisition changes optimal contract offers, equilibrium profits, information rents, and welfare. A better-informed firm increases its expected profits, but it can also increase expected utility when the cost of information is intermediate. Our results recommend balanced online privacy laws.},
|
||||||
language = {en},
|
language = {en},
|
||||||
author = {Devine, Brett R and Munoz-Garcia, Felix},
|
author = {Devine, Brett R and Munoz-Garcia, Felix},
|
||||||
month = nov,
|
|
||||||
year = {2017},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/GQ28KVBF/Devine and Munoz-Garcia - Nonlinear Pricing with Costly Information Acquisition.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/GQ28KVBF/Devine and Munoz-Garcia - Nonlinear Pricing with Costly Information Acquisition.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -211,11 +202,10 @@ laws, for fair and non-discriminatory use.},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/U7A5Q78V/Karten et al. - 2025 - LLM Economist Large Population Models and Mechanism Design in Multi-Agent Generative Simulacra.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/U7A5Q78V/Karten et al. - 2025 - LLM Economist Large Population Models and Mechanism Design in Multi-Agent Generative Simulacra.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@techreport{mullapudi_reinforcement_2025,
|
@techreport{mullapudi_reinforcement_nodate,
|
||||||
title = {A {Reinforcement} {Learning} {Approach} to {Dynamic} {Pricing}},
|
title = {A {Reinforcement} {Learning} {Approach} to {Dynamic} {Pricing}},
|
||||||
abstract = {Dynamic pricing represents a critical strategic challenge in modern e-commerce, where firms must navigate fluctuating demand, inventory constraints, and aggressive competitor actions. Traditional static and heuristic-based pricing models often fail to capture the complex, non-linear dynamics of competitive digital markets, leading to suboptimal profitability. This paper proposes a model-free reinforcement learning (RL) framework to address this challenge. Specifically, we design, implement, and evaluate a Q-learning agent capable of learning an optimal, state-dependent pricing policy. The agent is trained and evaluated within a simulated market environment constructed from the publicly available "Retail Price Optimization" dataset from Kaggle, which provides a rich feature set including historical sales, product characteristics, seasonality, and, crucially, competitor pricing data. The problem is formulated as a Markov Decision Process (MDP), where the agent's state incorporates its price position relative to competitors, competitor price trends, and seasonal factors. The agent's performance is benchmarked against three baseline strategies: static pricing, a reactive "follow-the-leader" heuristic, and random pricing. The results demonstrate that the Q-learning agent achieves a substantial increase in total cumulative profit over the evaluation period, outperforming all baselines by learning a nuanced policy that strategically balances price adjustments in response to market conditions. This work provides a practical and reproducible blueprint for applying reinforcement learning to optimize pricing decisions in a simulated yet realistic competitive retail environment, highlighting the potential of RL to automate complex strategic decision-making.},
|
abstract = {Dynamic pricing represents a critical strategic challenge in modern e-commerce, where firms must navigate fluctuating demand, inventory constraints, and aggressive competitor actions. Traditional static and heuristic-based pricing models often fail to capture the complex, non-linear dynamics of competitive digital markets, leading to suboptimal profitability. This paper proposes a model-free reinforcement learning (RL) framework to address this challenge. Specifically, we design, implement, and evaluate a Q-learning agent capable of learning an optimal, state-dependent pricing policy. The agent is trained and evaluated within a simulated market environment constructed from the publicly available "Retail Price Optimization" dataset from Kaggle, which provides a rich feature set including historical sales, product characteristics, seasonality, and, crucially, competitor pricing data. The problem is formulated as a Markov Decision Process (MDP), where the agent's state incorporates its price position relative to competitors, competitor price trends, and seasonal factors. The agent's performance is benchmarked against three baseline strategies: static pricing, a reactive "follow-the-leader" heuristic, and random pricing. The results demonstrate that the Q-learning agent achieves a substantial increase in total cumulative profit over the evaluation period, outperforming all baselines by learning a nuanced policy that strategically balances price adjustments in response to market conditions. This work provides a practical and reproducible blueprint for applying reinforcement learning to optimize pricing decisions in a simulated yet realistic competitive retail environment, highlighting the potential of RL to automate complex strategic decision-making.},
|
||||||
author = {Mullapudi, Pavan},
|
author = {Mullapudi, Pavan},
|
||||||
year = {2025},
|
|
||||||
note = {Publication Title: International Journal on Science and Technology (IJSAT) IJSAT25049558
|
note = {Publication Title: International Journal on Science and Technology (IJSAT) IJSAT25049558
|
||||||
Volume: 16
|
Volume: 16
|
||||||
Issue: 4},
|
Issue: 4},
|
||||||
@@ -304,11 +294,10 @@ Issue: 4},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/S8635QX6/varian95a.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/S8635QX6/varian95a.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@book{russell_artificial_2021,
|
@book{russell_artificial_nodate,
|
||||||
title = {Artificial {Intelligence} {A} {Modern} {Approach} {Fourth} {Edition} {Global} {Edition}},
|
title = {Artificial {Intelligence} {A} {Modern} {Approach} {Fourth} {Edition} {Global} {Edition}},
|
||||||
isbn = {978-1-292-40117-1},
|
isbn = {978-1-292-40117-1},
|
||||||
author = {Russell, Stuart and Norvig, Peter},
|
author = {Russell, Stuart and Norvig, Peter},
|
||||||
year = {2021},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/6B8W8S27/efdd4d1d4c2087fe1cbe03d9ced67f34.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/6B8W8S27/efdd4d1d4c2087fe1cbe03d9ced67f34.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,11 +312,10 @@ Volume: 21},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/N9JNXFJW/live-1333-2265-jair.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/N9JNXFJW/live-1333-2265-jair.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@techreport{shoham_multiagent_2009,
|
@techreport{shoham_multiagent_nodate,
|
||||||
title = {Multiagent {Systems}: {Algorithmic}, {Game}-{Theoretic}, and {Logical} {Foundations}},
|
title = {Multiagent {Systems}: {Algorithmic}, {Game}-{Theoretic}, and {Logical} {Foundations}},
|
||||||
url = {http://www.masfoundations.org.},
|
url = {http://www.masfoundations.org.},
|
||||||
author = {Shoham, Yoav and Leyton-Brown, Kevin},
|
author = {Shoham, Yoav and Leyton-Brown, Kevin},
|
||||||
year = {2009},
|
|
||||||
keywords = {algorithms, auctions, communication, competition, cooperation, distributed problem solving, game theory, learning, logic, mechanism design, social choice},
|
keywords = {algorithms, auctions, communication, competition, cooperation, distributed problem solving, game theory, learning, logic, mechanism design, social choice},
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/QZVYS7V9/shoham09a.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/QZVYS7V9/shoham09a.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
@@ -343,13 +331,11 @@ Volume: 21},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/H8IS64AW/2411.13768v2.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/H8IS64AW/2411.13768v2.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@techreport{xie_osworld_2024,
|
@techreport{xie_osworld_nodate,
|
||||||
title = {{OSWORLD}: {Benchmarking} {Multimodal} {Agents} for {Open}-{Ended} {Tasks} in {Real} {Computer} {Environments}},
|
title = {{OSWORLD}: {Benchmarking} {Multimodal} {Agents} for {Open}-{Ended} {Tasks} in {Real} {Computer} {Environments}},
|
||||||
url = {https://os-world.github.io},
|
url = {https://os-world.github.io},
|
||||||
abstract = {Autonomous agents that accomplish complex computer tasks with minimal human interventions have the potential to transform human-computer interaction, significantly enhancing accessibility and productivity. However, existing benchmarks either lack an interactive environment or are limited to environments specific to certain applications or domains, failing to reflect the diverse and complex nature of real-world computer use, thereby limiting the scope of tasks and agent scalability. To address this issue, we introduce OSWORLD, the first-of-its-kind scalable, real computer environment for multimodal agents, supporting task setup, execution-based evaluation, and interactive learning across various operating systems such as Ubuntu, Windows, and macOS. OSWORLD can serve as a unified, integrated computer environment for assessing open-ended computer tasks that involve arbitrary applications. Building upon OSWORLD, we create a benchmark of 369 computer tasks involving real web and desktop apps in open domains, OS file I/O, and workflows spanning multiple applications. Each task example is derived from real-world computer use cases and includes a detailed initial state setup configuration and a custom execution-based evaluation script for reliable, reproducible evaluation. Extensive evaluation of state-of-the-art LLM/VLM-based agents on OSWORLD reveals significant deficiencies in their ability to serve as computer assistants. While humans can accomplish over 72.36\% of the tasks, the best model achieves only 12.24\% success, primarily struggling with GUI grounding and operational knowledge. Comprehensive analysis using OSWORLD provides valuable insights for developing multimodal generalist agents that were not possible with previous benchmarks. Our code, environment, baseline models, and data are publicly available at https://os-world.github.io.},
|
abstract = {Autonomous agents that accomplish complex computer tasks with minimal human interventions have the potential to transform human-computer interaction, significantly enhancing accessibility and productivity. However, existing benchmarks either lack an interactive environment or are limited to environments specific to certain applications or domains, failing to reflect the diverse and complex nature of real-world computer use, thereby limiting the scope of tasks and agent scalability. To address this issue, we introduce OSWORLD, the first-of-its-kind scalable, real computer environment for multimodal agents, supporting task setup, execution-based evaluation, and interactive learning across various operating systems such as Ubuntu, Windows, and macOS. OSWORLD can serve as a unified, integrated computer environment for assessing open-ended computer tasks that involve arbitrary applications. Building upon OSWORLD, we create a benchmark of 369 computer tasks involving real web and desktop apps in open domains, OS file I/O, and workflows spanning multiple applications. Each task example is derived from real-world computer use cases and includes a detailed initial state setup configuration and a custom execution-based evaluation script for reliable, reproducible evaluation. Extensive evaluation of state-of-the-art LLM/VLM-based agents on OSWORLD reveals significant deficiencies in their ability to serve as computer assistants. While humans can accomplish over 72.36\% of the tasks, the best model achieves only 12.24\% success, primarily struggling with GUI grounding and operational knowledge. Comprehensive analysis using OSWORLD provides valuable insights for developing multimodal generalist agents that were not possible with previous benchmarks. Our code, environment, baseline models, and data are publicly available at https://os-world.github.io.},
|
||||||
author = {Xie, Tianbao and Zhang, Danyang and Chen, Jixuan and Li, Xiaochuan and Zhao, Siheng and Cao, Ruisheng and Jing Hua, Toh and Cheng, Zhoujun and Shin, Dongchan and Lei, Fangyu and Liu, Yitao and Xu, Yiheng and Zhou, Shuyan and Savarese, Silvio and Xiong, Caiming and Zhong, Victor and Yu, Tao},
|
author = {Xie, Tianbao and Zhang, Danyang and Chen, Jixuan and Li, Xiaochuan and Zhao, Siheng and Cao, Ruisheng and Jing Hua, Toh and Cheng, Zhoujun and Shin, Dongchan and Lei, Fangyu and Liu, Yitao and Xu, Yiheng and Zhou, Shuyan and Savarese, Silvio and Xiong, Caiming and Zhong, Victor and Yu, Tao},
|
||||||
month = may,
|
|
||||||
year = {2024},
|
|
||||||
note = {arXiv: 2404.07972v2},
|
note = {arXiv: 2404.07972v2},
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/LLRKXIC7/full-text.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/LLRKXIC7/full-text.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
@@ -378,21 +364,17 @@ Volume: 21},
|
|||||||
file = {PDF:/home/velocitatem/Zotero/storage/QNXZJLRM/S2444883425000038.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/QNXZJLRM/S2444883425000038.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@misc{ghaffary_amazon_2025,
|
@misc{ghaffary_amazon_nodate,
|
||||||
title = {Amazon {Sues} to {Stop} {Perplexity} {From} {Using} {AI} {Tool} to {Buy} {Stuff}},
|
title = {Amazon {Sues} to {Stop} {Perplexity} {From} {Using} {AI} {Tool} to {Buy} {Stuff}},
|
||||||
url = {https://www.bloomberg.com/news/articles/2025-11-04/amazon-demands-perplexity-stop-ai-agent-from-making-purchases},
|
url = {https://www.bloomberg.com/news/articles/2025-11-04/amazon-demands-perplexity-stop-ai-agent-from-making-purchases},
|
||||||
author = {Ghaffary, Shirin and Day, Matt},
|
author = {Ghaffary, Shirin and Day, Matt},
|
||||||
month = nov,
|
|
||||||
year = {2025},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/IQL6FPWE/Amazon Sues to Stop Perplexity From Using AI Tool to Buy Stuff - Bloomberg.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/IQL6FPWE/Amazon Sues to Stop Perplexity From Using AI Tool to Buy Stuff - Bloomberg.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@techreport{besbes_dynamic_2007,
|
@techreport{besbes_dynamic_nodate,
|
||||||
title = {Dynamic {Pricing} {Without} {Knowing} the {Demand} {Function}: {Risk} {Bounds} and {Near}-{Optimal} {Algorithms} *},
|
title = {Dynamic {Pricing} {Without} {Knowing} the {Demand} {Function}: {Risk} {Bounds} and {Near}-{Optimal} {Algorithms} *},
|
||||||
abstract = {We consider a single product revenue management problem where, given an initial inventory, the objective is to dynamically adjust prices over a finite sales horizon to maximize expected revenues. Realized demand is observed over time, but the underlying functional relationship between price and mean demand rate that governs these observations (otherwise known as the demand function or demand curve), is not known. We consider two instances of this problem: i.) a setting where the demand function is assumed to belong to a known parametric family with unknown parameter values; and ii.) a setting where the demand function is assumed to belong to a broad class of functions that need not admit any parametric representation. In each case we develop policies that learn the demand function "on the fly," and optimize prices based on that. The performance of these algorithms is measured in terms of the regret: the revenue loss relative to the maximal revenues that can be extracted when the demand function is known prior to the start of the selling season. We derive lower bounds on the regret that hold for any admissible pricing policy, and then show that our proposed algorithms achieve a regret that is "close" to this lower bound. The magnitude of the regret can be interpreted as the economic value of prior knowledge on the demand function; manifested as the revenue loss due to model uncertainty.},
|
abstract = {We consider a single product revenue management problem where, given an initial inventory, the objective is to dynamically adjust prices over a finite sales horizon to maximize expected revenues. Realized demand is observed over time, but the underlying functional relationship between price and mean demand rate that governs these observations (otherwise known as the demand function or demand curve), is not known. We consider two instances of this problem: i.) a setting where the demand function is assumed to belong to a known parametric family with unknown parameter values; and ii.) a setting where the demand function is assumed to belong to a broad class of functions that need not admit any parametric representation. In each case we develop policies that learn the demand function "on the fly," and optimize prices based on that. The performance of these algorithms is measured in terms of the regret: the revenue loss relative to the maximal revenues that can be extracted when the demand function is known prior to the start of the selling season. We derive lower bounds on the regret that hold for any admissible pricing policy, and then show that our proposed algorithms achieve a regret that is "close" to this lower bound. The magnitude of the regret can be interpreted as the economic value of prior knowledge on the demand function; manifested as the revenue loss due to model uncertainty.},
|
||||||
author = {Besbes, Omar and Zeevi, Assaf},
|
author = {Besbes, Omar and Zeevi, Assaf},
|
||||||
month = dec,
|
|
||||||
year = {2007},
|
|
||||||
note = {Publication Title: Operations Research},
|
note = {Publication Title: Operations Research},
|
||||||
keywords = {learning, asymptotic analysis, estimation, exploration-exploitation, pricing, Revenue management, value of information},
|
keywords = {learning, asymptotic analysis, estimation, exploration-exploitation, pricing, Revenue management, value of information},
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/SBAIB4V2/Dp_wo_demand_risk_ob_az_posted.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/SBAIB4V2/Dp_wo_demand_risk_ob_az_posted.pdf:application/pdf},
|
||||||
@@ -441,178 +423,3 @@ Volume: 21},
|
|||||||
keywords = {Computer Science - Computation and Language},
|
keywords = {Computer Science - Computation and Language},
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/3Z2XK4QC/Ganie - 2025 - Uncertainty in Authorship Why Perfect AI Detection Is Mathematically Impossible.pdf:application/pdf},
|
file = {PDF:/home/velocitatem/Zotero/storage/3Z2XK4QC/Ganie - 2025 - Uncertainty in Authorship Why Perfect AI Detection Is Mathematically Impossible.pdf:application/pdf},
|
||||||
}
|
}
|
||||||
|
|
||||||
@article{shi_distributionally_2024,
|
|
||||||
title = {Distributionally {Robust} {Model}-{Based} {Offline} {Reinforcement} {Learning} with {Near}-{Optimal} {Sample} {Complexity}},
|
|
||||||
abstract = {This paper concerns the central issues of model robustness and sample efficiency in offline reinforcement learning (RL), which aims to learn to perform decision making from history data without active exploration. Due to uncertainties and variabilities of the environment, it is critical to learn a robust policy—with as few samples as possible—that performs well even when the deployed environment deviates from the nominal one used to collect the history dataset. We consider a distributionally robust formulation of offline RL, focusing on tabular robust Markov decision processes with an uncertainty set specified by the Kullback-Leibler divergence in both finite-horizon and infinite-horizon settings. To combat with sample scarcity, a model-based algorithm that combines distributionally robust value iteration with the principle of pessimism in the face of uncertainty is proposed, by penalizing the robust value estimates with a carefully designed data-driven penalty term. Under a mild and tailored assumption of the history dataset that measures distribution shift without requiring full coverage of the state-action space, we establish the finite-sample complexity of the proposed algorithms. We further develop an informationtheoretic lower bound, which suggests that learning RMDPs is at least as hard as the standard MDPs when the uncertainty level is sufficient small, and corroborates the tightness of our upper bound up to polynomial factors of the (effective) horizon length for a range of uncertainty levels. To the best our knowledge, this provides the first provably near-optimal robust offline RL algorithm that learns under model uncertainty and partial coverage.},
|
|
||||||
language = {en},
|
|
||||||
author = {Shi, Laixi and Chi, Yuejie},
|
|
||||||
month = jun,
|
|
||||||
year = {2024},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/K56G4EIP/Shi and Chi - Distributionally Robust Model-Based Offline Reinforcement Learning with Near-Optimal Sample Complexity.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@article{dutting_mechanism_2025,
|
|
||||||
title = {Mechanism {Design} for {Large} {Language} {Models} ({Extended} {Abstract})},
|
|
||||||
abstract = {We investigate auction mechanisms for AIgenerated content, focusing on applications like ad creative generation. In our model, agents’ preferences over stochastically generated content are encoded as large language models (LLMs). We propose an auction format that operates on a tokenby-token basis, and allows LLM agents to influence content creation through single dimensional bids. We formulate two desirable incentive properties and prove their equivalence to a monotonicity condition on output aggregation. This equivalence enables a second-price rule design, even absent explicit agent valuation functions. Our design is supported by demonstrations on a publicly available LLM.},
|
|
||||||
language = {en},
|
|
||||||
author = {Dütting, Paul and Mirrokni, Vahab and Leme, Renato Paes and Xu, Haifeng and Zuo, Song},
|
|
||||||
year = {2025},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/2ABDEYDN/Dütting et al. - Mechanism Design for Large Language Models (Extended Abstract).pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{fcmi_machine_2025,
|
|
||||||
title = {Machine {Speed} {Markets}: {AI} {Agent} {Market} {Strategy} \& {Growth}},
|
|
||||||
shorttitle = {Machine {Speed} {Markets}},
|
|
||||||
url = {https://www.360strategy.co.uk/post/machine-speed-markets-ai-agents},
|
|
||||||
abstract = {Recent research by NBER economists suggests these AI agents in particular, could drive a "Coasean singularity," a point where transaction costs fall towards zero, radically reshaping how markets function. In essence, tasks like finding information, negotiating deals, and enforcing contracts which are traditionally costly frictions in commerce, may become nearly instantaneous and costless.},
|
|
||||||
language = {en},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
journal = {360 Strategy},
|
|
||||||
author = {FCMi, CMgr, Mark Evans MBA},
|
|
||||||
month = nov,
|
|
||||||
year = {2025},
|
|
||||||
file = {Snapshot:/home/velocitatem/Zotero/storage/Z22P9JJH/machine-speed-markets-ai-agents.html:text/html},
|
|
||||||
}
|
|
||||||
|
|
||||||
@article{coase_nature_1937,
|
|
||||||
title = {The {Nature} of the {Firm}},
|
|
||||||
volume = {4},
|
|
||||||
issn = {1468-0335},
|
|
||||||
url = {https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1468-0335.1937.tb00002.x},
|
|
||||||
doi = {10.1111/j.1468-0335.1937.tb00002.x},
|
|
||||||
language = {en},
|
|
||||||
number = {16},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
journal = {Economica},
|
|
||||||
author = {Coase, R. H.},
|
|
||||||
year = {1937},
|
|
||||||
pages = {386--405},
|
|
||||||
file = {Full Text PDF:/home/velocitatem/Zotero/storage/TABLLPEU/Coase - 1937 - The Nature of the Firm.pdf:application/pdf;Snapshot:/home/velocitatem/Zotero/storage/Q5RFW9LJ/j.1468-0335.1937.tb00002.html:text/html},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{fish_algorithmic_2025,
|
|
||||||
title = {Algorithmic {Collusion} by {Large} {Language} {Models}},
|
|
||||||
url = {http://arxiv.org/abs/2404.00806},
|
|
||||||
doi = {10.48550/arXiv.2404.00806},
|
|
||||||
abstract = {The rise of algorithmic pricing raises concerns of algorithmic collusion. We conduct experiments with algorithmic pricing agents based on Large Language Models (LLMs). We find that LLM-based pricing agents quickly and autonomously reach supracompetitive prices and profits in oligopoly settings and that variation in seemingly innocuous phrases in LLM instructions (“prompts”) may substantially influence the degree of supracompetitive pricing. Off-path analysis using novel techniques uncovers price-war concerns as contributing to these phenomena. Our results extend to auction settings. Our findings uncover unique challenges to any future regulation of LLM-based pricing agents, and AI-based pricing agents more broadly.},
|
|
||||||
language = {en},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
publisher = {arXiv},
|
|
||||||
author = {Fish, Sara and Gonczarowski, Yannai A. and Shorrer, Ran I.},
|
|
||||||
month = sep,
|
|
||||||
year = {2025},
|
|
||||||
note = {arXiv:2404.00806 [econ]},
|
|
||||||
keywords = {Computer Science - Computer Science and Game Theory, Computer Science - Artificial Intelligence, Economics - General Economics},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/QHWVISCZ/Fish et al. - 2025 - Algorithmic Collusion by Large Language Models.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{hardt_strategic_2015,
|
|
||||||
title = {Strategic {Classification}},
|
|
||||||
url = {http://arxiv.org/abs/1506.06980},
|
|
||||||
doi = {10.48550/arXiv.1506.06980},
|
|
||||||
abstract = {Machine learning relies on the assumption that unseen test instances of a classification problem follow the same distribution as observed training data. However, this principle can break down when machine learning is used to make important decisions about the welfare (employment, education, health) of strategic individuals. Knowing information about the classifier, such individuals may manipulate their attributes in order to obtain a better classification outcome. As a result of this behavior—often referred to as gaming—the performance of the classifier may deteriorate sharply. Indeed, gaming is a well-known obstacle for using machine learning methods in practice; in financial policy-making, the problem is widely known as Goodhart’s law. In this paper, we formalize the problem, and pursue algorithms for learning classifiers that are robust to gaming.},
|
|
||||||
language = {en},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
publisher = {arXiv},
|
|
||||||
author = {Hardt, Moritz and Megiddo, Nimrod and Papadimitriou, Christos and Wootters, Mary},
|
|
||||||
month = nov,
|
|
||||||
year = {2015},
|
|
||||||
note = {arXiv:1506.06980 [cs]},
|
|
||||||
keywords = {Computer Science - Machine Learning},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/HNCDYGWS/Hardt et al. - 2015 - Strategic Classification.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{liu_contextual_2024,
|
|
||||||
title = {Contextual {Dynamic} {Pricing} with {Strategic} {Buyers}},
|
|
||||||
url = {http://arxiv.org/abs/2307.04055},
|
|
||||||
doi = {10.48550/arXiv.2307.04055},
|
|
||||||
abstract = {Personalized pricing, which involves tailoring prices based on individual characteristics, is commonly used by firms to implement a consumer-specific pricing policy. In this process, buyers can also strategically manipulate their feature data to obtain a lower price, incurring certain manipulation costs. Such strategic behavior can hinder firms from maximizing their profits. In this paper, we study the contextual dynamic pricing problem with strategic buyers. The seller does not observe the buyer's true feature, but a manipulated feature according to buyers' strategic behavior. In addition, the seller does not observe the buyers' valuation of the product, but only a binary response indicating whether a sale happens or not. Recognizing these challenges, we propose a strategic dynamic pricing policy that incorporates the buyers' strategic behavior into the online learning to maximize the seller's cumulative revenue. We first prove that existing non-strategic pricing policies that neglect the buyers' strategic behavior result in a linear \$Ω(T)\$ regret with \$T\$ the total time horizon, indicating that these policies are not better than a random pricing policy. We then establish that our proposed policy achieves a sublinear regret upper bound of \$O({\textbackslash}sqrt\{T\})\$. Importantly, our policy is not a mere amalgamation of existing dynamic pricing policies and strategic behavior handling algorithms. Our policy can also accommodate the scenario when the marginal cost of manipulation is unknown in advance. To account for it, we simultaneously estimate the valuation parameter and the cost parameter in the online pricing policy, which is shown to also achieve an \$O({\textbackslash}sqrt\{T\})\$ regret bound. Extensive experiments support our theoretical developments and demonstrate the superior performance of our policy compared to other pricing policies that are unaware of the strategic behaviors.},
|
|
||||||
language = {en},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
publisher = {arXiv},
|
|
||||||
author = {Liu, Pangpang and Yang, Zhuoran and Wang, Zhaoran and Sun, Will Wei},
|
|
||||||
month = jun,
|
|
||||||
year = {2024},
|
|
||||||
note = {arXiv:2307.04055 [stat]},
|
|
||||||
keywords = {Computer Science - Machine Learning, Statistics - Machine Learning, Computer Science - Computer Science and Game Theory, Computer Science - Artificial Intelligence},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/MVJNULK3/Liu et al. - 2024 - Contextual Dynamic Pricing with Strategic Buyers.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@techreport{dhir_http_2025,
|
|
||||||
type = {Internet {Draft}},
|
|
||||||
title = {{HTTP} {Agent} {Profile} ({HAP}): {Authenticated} and {Monetized} {Agent} {Traffic} on the {Web}},
|
|
||||||
shorttitle = {{HTTP} {Agent} {Profile} ({HAP})},
|
|
||||||
url = {https://datatracker.ietf.org/doc/draft-dhir-http-agent-profile},
|
|
||||||
abstract = {Autonomous agents such as LLM-powered crawlers, browser-integrated assistants, and task-oriented bots are rapidly becoming first-class HTTP clients on the Web. Today’s infrastructure largely assumes a human behind a browser and monetizes content through advertising and coarse subscriptions. Automated agents consume content at scale without rendering pages or viewing ads, exacerbating bot-mitigation arms races and economic misalignment between content providers and AI systems. This document describes an HTTP Agent Profile (HAP) that enables: (1) cryptographic authentication of agent traffic using HTTP Message Signatures; (2) clear separation between human and agent traffic using privacy-preserving human tokens; and (3) protocol-level value exchange for agents via HTTP status code 402 ("Payment Required") and pluggable micropayment mechanisms. The profile reuses existing HTTP features and is designed for incremental deployment via reverse proxies, CDNs, and agent libraries.},
|
|
||||||
number = {draft-dhir-http-agent-profile-00},
|
|
||||||
urldate = {2026-01-20},
|
|
||||||
institution = {Internet Engineering Task Force},
|
|
||||||
author = {Dhir, Sanat},
|
|
||||||
month = nov,
|
|
||||||
year = {2025},
|
|
||||||
note = {Num Pages: 13},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{noauthor_amazoncom_2026,
|
|
||||||
title = {Amazon.com {Services} {LLC} v. {Perplexity} {AI}, {Inc}},
|
|
||||||
language = {en},
|
|
||||||
month = jan,
|
|
||||||
year = {2026},
|
|
||||||
note = {No. 3:25-cv-09514-MMC},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/4JWZSTXJ/Posner - UNITED STATES DISTRICT COURT NORTHERN DISTRICT OF CALIFORNIA SAN FRANCISCO DIVISION.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@article{wright_2026_2025,
|
|
||||||
title = {2026 {Artificial} {Intelligence} {Outlook}: {The} {Great} {Competition} {Wars} {Have} {Begun}},
|
|
||||||
language = {en},
|
|
||||||
journal = {Pitchbook},
|
|
||||||
author = {Wright, Brian and Javaheri, Ali and Bellomo, Eric and Hernandez, Derek and Yang, Rudy and MacDonagh, John and DeGagne, Aaron and Frederick, Alex and Geurkink, Jonathan and Zabelin, Dimitri and Ulan, James},
|
|
||||||
month = dec,
|
|
||||||
year = {2025},
|
|
||||||
file = {PDF:/home/velocitatem/Zotero/storage/AIY5K3TX/Wright et al. - 2025 - Institutional Research Group.pdf:application/pdf},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{rachitsky_marc_2026,
|
|
||||||
title = {Marc {Andreessen}: {The} real {AI} boom hasn’t even started yet},
|
|
||||||
shorttitle = {Marc {Andreessen}},
|
|
||||||
url = {https://www.lennysnewsletter.com/p/marc-andreessen-the-real-ai-boom},
|
|
||||||
abstract = {On raising kids, why job loss fears are overblown, the future of PM/eng/design careers, and the macro force you should pay attention to},
|
|
||||||
language = {en},
|
|
||||||
urldate = {2026-02-01},
|
|
||||||
author = {Rachitsky, Lenny},
|
|
||||||
month = feb,
|
|
||||||
year = {2026},
|
|
||||||
file = {Snapshot:/home/velocitatem/Zotero/storage/DGW8PHMV/marc-andreessen-the-real-ai-boom.html:text/html},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{noauthor_tpu_2025,
|
|
||||||
title = {{TPU} v6e},
|
|
||||||
url = {https://cloud.google.com/tpu/docs/v6e},
|
|
||||||
language = {es-419-x-mtfrom-en},
|
|
||||||
urldate = {2026-02-17},
|
|
||||||
journal = {Google Cloud Documentation},
|
|
||||||
month = dec,
|
|
||||||
year = {2025},
|
|
||||||
file = {Snapshot:/home/velocitatem/Zotero/storage/RNMB32KD/v6e.html:text/html},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{noauthor_tpu_2025-1,
|
|
||||||
title = {{TPU} v5e {\textbar} {Google} {Cloud} {Documentation}},
|
|
||||||
url = {https://cloud.google.com/tpu/docs/v5e},
|
|
||||||
language = {es-419-x-mtfrom-en},
|
|
||||||
urldate = {2026-02-17},
|
|
||||||
month = dec,
|
|
||||||
year = {2025},
|
|
||||||
file = {Snapshot:/home/velocitatem/Zotero/storage/BLLG9NZC/v5e.html:text/html},
|
|
||||||
}
|
|
||||||
|
|
||||||
@misc{noauthor_tpu_2026,
|
|
||||||
title = {{TPU} v4 {\textbar} {Google} {Cloud} {Documentation}},
|
|
||||||
url = {https://cloud.google.com/tpu/docs/v4},
|
|
||||||
language = {es-419-x-mtfrom-en},
|
|
||||||
urldate = {2026-02-17},
|
|
||||||
month = feb,
|
|
||||||
year = {2026},
|
|
||||||
file = {Snapshot:/home/velocitatem/Zotero/storage/N724QGF6/v4.html:text/html},
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -10,37 +10,27 @@
|
|||||||
|
|
||||||
In this paper we present an exploration and defense against the presence of new commercial entities in digitally powered platforms, preserving market equilibrium in the age of AI. This research establishes the following contributions: definition and formalization of non-human transactors in e-commerce platforms, development of a testing-ground for capturing the behavioral essence of these transactors across a large variety of digital systems, construction of a discriminative model (to prove separability) as a strong learner for downstream mitigation of contamination by non-human entities, translation of such learned separability into existing dynamic pricing machine learning loops, and finally establishment of a high-level KPI-affecting causal effect and cost-saving framework for the future of internet commerce in the presence of such non-human learners.
|
In this paper we present an exploration and defense against the presence of new commercial entities in digitally powered platforms, preserving market equilibrium in the age of AI. This research establishes the following contributions: definition and formalization of non-human transactors in e-commerce platforms, development of a testing-ground for capturing the behavioral essence of these transactors across a large variety of digital systems, construction of a discriminative model (to prove separability) as a strong learner for downstream mitigation of contamination by non-human entities, translation of such learned separability into existing dynamic pricing machine learning loops, and finally establishment of a high-level KPI-affecting causal effect and cost-saving framework for the future of internet commerce in the presence of such non-human learners.
|
||||||
|
|
||||||
This research effort touches a large variety of domains, spanning behavioral economics for understanding the rationality of behavior as theorized by the concept of homo economicus, agent-based modeling to translate our learned separability into disjoint dynamic pricing systems, reinforcement learning which serves as the SOTA for price-learners, and dynamic pricing and market equilibrium theory to understand the risks of possible supra-competitive pricing phenomena in cases of adversarial pricing systems driving the market out of equilibrium. \footnote{Given the rapid evolution of the field we acknowledge all developments with a cutoff set at the date of March 31st 2026.}
|
This research effort touches a large variety of domains, spanning behavioral economics for understanding the rationality of behavior as theorized by the concept of homo economicus, agent-based modeling to translate our learned separability into disjoint dynamic pricing systems, reinforcement learning which serves as the SOTA for price-learners, and dynamic pricing and market equilibrium theory to understand the risks of possible supra-competitive pricing phenomena in cases of adversarial pricing systems driving the market out of equilibrium.
|
||||||
|
|
||||||
\subsection{Motivation and Market Context}
|
\subsection{Motivation and Market Context}
|
||||||
|
|
||||||
The current innovation boom in generative artificial intelligence and its applications to knowledge-based work tasks has brought many competing technologies for browser-use automation, with benchmarks and evaluations \parencite{xia_evaluation-driven_2025} motivating the development of capabilities focused on commercial research, understanding, and transaction execution \parencite{xie_osworld_2024}. The ``AI Agent'' market is forecasted to grow from around USD 5-8 billion in 2025 to USD 42-52 billion by 2030. This surge reflects adoption in e-commerce, customer service, and enterprise automation, where agents handle interactions previously done by humans, raising the question of how these systems should be designed for future robustness as well as how to maintain a competitive edge in the analytical components of e-commerce platforms \parencite{markntel_advisors_global_2025}.
|
The current innovation boom in generative artificial intelligence and its applications to knowledge-based work tasks has brought many competing technologies for browser-use automation, with benchmarks and evaluations \cite{xia_evaluation-driven_2025} motivating the development of capabilities focused on commercial research, understanding, and transaction execution \cite{xie_osworld_nodate}. The ``AI Agent'' market is forecasted to grow from around USD 5-8 billion in 2025 to USD 42-52 billion by 2030. This surge reflects adoption in e-commerce, customer service, and enterprise automation, where agents handle interactions previously done by humans, raising the question of how these systems should be designed for future robustness as well as how to maintain a competitive edge in the analytical components of e-commerce platforms \cite{markntel_advisors_global_2025}.
|
||||||
|
|
||||||
The key stakeholders affected by the threat of increasing agent-driven traffic include online businesses and platform operators (especially in bot-heavy sectors like retail, travel, and financial services), their security, fraud, and engineering teams, end users whose accounts and data are exposed and whose experience degrades, regulators and legal stakeholders responding to breaches and fraud, and the attackers or bot operators driving the automation \parencite{imperva_rapid_2025}.
|
The key stakeholders affected by the threat of increasing agent-driven traffic include online businesses and platform operators (especially in bot-heavy sectors like retail, travel, and financial services), their security, fraud, and engineering teams, end users whose accounts and data are exposed and whose experience degrades, regulators and legal stakeholders responding to breaches and fraud, and the attackers or bot operators driving the automation \cite{imperva_rapid_2025}.
|
||||||
|
|
||||||
The industry has already seen legal action in cases like Amazon against Perplexity \parencite{ghaffary_amazon_2025}, stemming from the difficulty of identifying traffic from hybrid systems like the Commet browser. This paper explores such systems to better understand what the interaction data looks like and what it means for dynamic pricing and recommendation systems downstream. This observed impact indicates a need for prevention of secondary negative effects on the ``legacy'' systems which power modern revenue sources for many companies. Dynamic pricing algorithms rely on directly translating demand features $q$ to new price assignments $\hat{p}$ across a catalogue of products of size $N$. This opens opportunities to design a \textit{tabula rasa} of digital market mechanisms that will shape the future of commerce in the age of artificial intelligence.
|
The industry has already seen legal action in cases like Amazon against Perplexity \cite{ghaffary_amazon_nodate}, stemming from the difficulty of identifying traffic from hybrid systems like the Commet browser. This paper explores such systems to better understand what the interaction data looks like and what it means for dynamic pricing and recommendation systems downstream. This observed impact indicates a need for prevention of secondary negative effects on the ``legacy'' systems which power modern revenue sources for many companies. Dynamic pricing algorithms rely on directly translating demand features $q$ to new price assignments $\hat{p}$ across a catalogue of products of size $N$. This opens opportunities to design a \textit{tabula rasa} of digital market mechanisms that will shape the future of commerce in the age of artificial intelligence.
|
||||||
|
|
||||||
\subsection{Solution Space Overview}
|
\subsection{Solution Space Overview}
|
||||||
Dynamic pricing systems, as presented by \textcite{mueller_low-rank_2019}, often deal with sparse low-rank data of demand signals which, combined with contamination from agents, creates complex interactions that impact pricing. To further complicate the problem, certain commercial settings such as the one presented by \textcite{amjad_censored_2017} must address the true demand of products under censored observations. This provides a formulation for handling demand in our case with multiple kinds of commercial mediators: $\hat{q} \gets q_A + q_H$ where $q_A$ represents the distribution of demand generated by agentic mediators and $q_H$ represents that of true human demand, these are two distinct populations with divergent objective functions.
|
Dynamic pricing systems, as presented in \cite{mueller_low-rank_2019}, often deal with sparse low-rank data of demand signals which, combined with contamination from agents, creates complex interactions that impact pricing. To further complicate the problem, certain commercial settings such as the one presented in \cite{amjad_censored_2017} must address the true demand of products under censored observations. This provides a formulation for handling demand in our case with multiple kinds of commercial mediators: $\hat{q} \gets q_A + q_H$ where $q_A$ represents the distribution of demand generated by agentic mediators and $q_H$ represents that of true human demand, these are two distinct populations with divergent objective functions.
|
||||||
|
|
||||||
We formally define interaction data as coming from some actor which can either be an agent ($A$) or human ($H$). For purposes of this research, an agent is an algorithmic loop with the ability to access a web platform and perform actions such as clicks, scrolls, and input field fills. The loop terminates when the internal large language model judges the provided task definition as complete. A detailed breakdown can be found in \cref{algagent-loop}.
|
We formally define interaction data as coming from some actor which can either be an agent ($A$) or human ($H$). For purposes of this research, an agent is an algorithmic loop with the ability to access a web platform and perform actions such as clicks, scrolls, and input field fills. The loop terminates when the internal large language model judges the provided task definition as complete. A detailed breakdown can be found in \cref{algagent-loop}.
|
||||||
|
|
||||||
\subsection{Research Questions}
|
|
||||||
|
|
||||||
This dissertation is organized around one main research question and three supporting sub-questions:
|
|
||||||
\begin{enumerate}
|
|
||||||
\item[\textbf{Main RQ}] How can dynamic pricing systems preserve margin integrity when transaction orchestration is increasingly mediated by non-human agents?
|
|
||||||
\item[\textbf{SQ1}] \textit{Separability}: Can agent and human sessions be reliably distinguished from behavioral interaction signals alone, without relying on network-level or device fingerprinting?
|
|
||||||
\item[\textbf{SQ2}] \textit{Theoretical Impact}: What is the formal relationship between agent contamination levels and the erosion of pricing power in dynamic pricing systems?
|
|
||||||
\item[\textbf{SQ3}] \textit{Robust Mitigation}: How can pricing policies be constructed to maintain margin integrity under unknown and non-stationary levels of agent contamination?
|
|
||||||
\end{enumerate}
|
|
||||||
|
|
||||||
|
|
||||||
\begin{algorithm}[t]
|
\begin{algorithm}[t]
|
||||||
\DontPrintSemicolon
|
\DontPrintSemicolon
|
||||||
|
|
||||||
\SetKwInput{Input}{Input}
|
\SetKwInOut{Input}{Input}
|
||||||
\SetKwInput{Output}{Output}
|
\SetKwInOut{Output}{Output}
|
||||||
|
|
||||||
\Input{Goal $G$, Platform URL $u$, LLM $\mathcal{M}$}
|
\Input{Goal $G$, Platform URL $u$, LLM $\mathcal{M}$}
|
||||||
\Output{Task completion result $r$}
|
\Output{Task completion result $r$}
|
||||||
@@ -64,4 +54,4 @@ Extract final result $r$ from terminal state\;
|
|||||||
\end{algorithm}
|
\end{algorithm}
|
||||||
|
|
||||||
|
|
||||||
The previously described goal of separability allows us to formulate a task which entails taking raw interaction data for either actor and creating a composite demand estimate $\hat{q}$. We propose a robust optimization objective defined in our methodology, transforming the pricing problem into a form of Distributionally Robust Optimization \parencite{kuhn_distributionally_2025} where the learner must guard against adversarial contamination in observed demand distributors. In this setting we must learn to make decision that perform under the assumption of not having a single estimated probability distribution but under an ambiguity set of any distribution, of which we have limited information. In our case as stated is a mixture of distributions with a parameter which is unknown and non-stationary.
|
The previously described goal of separability allows us to formulate a task which entails taking raw interaction data for either actor and creating a composite demand estimate $\hat{q}$. We propose a robust optimization objective defined in our methodology, transforming the pricing problem into a form of Distributionally Robust Optimization \cite{kuhn_distributionally_2025} where the learner must guard against adversarial contamination in observed demand distributors. In this setting we must learn to make decision that perform under the assumption of not having a single estimated probability distribution but under an ambiguity set of any distribution, of which we have limited information. In our case as stated is a mixture of distributions with a parameter which is unknown and non-stationary.
|
||||||
|
|||||||
@@ -1,29 +1,28 @@
|
|||||||
\section{Literature Review}
|
\section{Literature Review}
|
||||||
|
|
||||||
To better understand all wedges of the current works, we must start by exploring the nature of agents, agentic computer use and web automation, complementing that with economic reasoning and strategic interaction. The final surface to cover, leads us to data-driven dynamic pricing under uncertainty. The key technical risk is not ``agents buying things'' per se, but agents shaping the behavioral and demand signals that downstream pricing systems consume and depend on. This latter case of agents shopping is currently pending legal action in the case of \textcite{noauthor_amazoncom_2026} which is currently being treated as a violation of the Computer Fraud and Abuse Act. The introduction of these mediating actor entities into economic systems, is further creating a threat of false-name bidding \parencite{yokoo_effect_2004}, which prior research has explored in a trading context. Other research on pseudonyms in dynamic systems, demonstrate whitewashing in AI agents which can ignore defensive mechanisms by re-entry with different identities \parencite{feldman_free-riding_2004}. Dynamic pricing assumes demand proxies are behaviorally meaningful, while bot detection aims at security and access control. The missing bridge is a principled framework for separating non-human reconnaissance from genuine human demand expression and integrating that separation into pricing heuristics without degrading legitimate user experience (in our research tracked by the user-experience index). This gap, is what our contribution aims to address, particularly for the aforementioned stakeholder groups.
|
To better understand all wedges of the work, we must start by exploring the nature of agents and agentic computer use and web automation, complementing that with economic reasoning and strategic interaction. The final surface to cover, leads us to data-driven dynamic pricing under uncertainty. The key technical risk is not ``agents buying things'' per se, but agents shaping the behavioral and demand signals that downstream pricing systems consume and depend on. The introduction of these mediating actor entities into economic systems, is further creating a threat of false-name bidding \cite{yokoo_effect_2004}, which prior research has explored in a trading context. Other research on pseudonyms in dynamic systems, demonstrate whitewashing in AI agents which can ignore defensive mechanisms by re-entry with different identities \cite{feldman_free-riding_2004}. Dynamic pricing assumes demand proxies are behaviorally meaningful, while bot detection aims at security and access control. The missing bridge is a principled framework for separating non-human reconnaissance from genuine human demand expression and integrating that separation into pricing heuristics without degrading legitimate user experience (in our research tracked by the user-experience index). This gap, is what our contribution aims to address, particularly for the aforementioned stakeholder groups.
|
||||||
|
|
||||||
\subsection{Agent Taxonomy and Definitions}
|
\subsection{Agent Taxonomy and Definitions}
|
||||||
|
|
||||||
An agent in the context of artificial intelligence is generally defined by anything that can reason and act upon observations of its environments (collected through some sensory inputs) and carry out actions through effectors. Moreover, a rational agent is an entity that is capable of perceiving the world around them and taking actions to advance specified goals. This definition by \textcite{russell_artificial_2021} is further developed in an economic context by \textcite{parkes_economic_2015}, suggesting AI research attempts to construct a synthetic \textit{homo economicus}, which may also be termed \textit{machina economicus}.
|
An agent in the context of artificial intelligence is generally defined by anything that can reason and act upon observations of its environments (collected through some sensory inputs) and carry out actions through effectors. Moreover, a rational agent is an entity that is capable of perceiving the world around them and taking actions to advance specified goals. This definition by \cite{russell_artificial_nodate} is further developed in an economic context by \cite{parkes_economic_2015}, suggesting AI research attempts to construct a synthetic \textit{homo economicus}, which may also be termed \textit{machina economicus}.
|
||||||
A specific class or taxon of this \textit{machina economicus}, the Large Language Model (LLM) agent, is defined as an autonomous system capable of achieving goals and adapting post-training, often without needing explicit code or fundamental model changes \parencite{xia_evaluation-driven_2025}.
|
A specific class or taxon of this \textit{machina economicus}, the Large Language Model (LLM) agent, is defined as an autonomous system capable of achieving goals and adapting post-training, often without needing explicit code or fundamental model changes. \cite{xia_evaluation-driven_2025}
|
||||||
|
|
||||||
We must however acknowledge the current SOTA as presented by OSWORLD simulations by \textcite{xie_osworld_2024} have demonstrated that multi-modal tasks across desktop and web interaction modes, have a top-performing score of only 12.24\% success, whereas humans have a higher 72\% success rate; this is linked to the lack of grounding of these agents and their inability of handling unexpected errors. This weakness matters for this research because it clarifies the near-term threat model: practical exploitation does not require a fully competent ``computer assistant'', only enough automation to perform high-volume reconnaissance actions (search/filter/open product pages, probe availability/price boundaries) that can contaminate behavioral signals. With the expected growth of these capabilities, this threat only becomes more perilous to revenue management systems.
|
We must however acknowledge the current SOTA as presented by OSWORLD simulations in \cite{xie_osworld_nodate} have demonstrated that multi-modal tasks across desktop and web interaction modes, have a top-performing score of only 12.24\% success, whereas humans have a higher 72\% success rate. This weakness matters for this research because it clarifies the near-term threat model: practical exploitation does not require a fully competent ``computer assistant'', only enough automation to perform high-volume reconnaissance actions (search/filter/open product pages, probe availability/price boundaries) that can contaminate behavioral signals. With the expected growth of these capabilities, this threat only becomes more perilous to revenue management systems.
|
||||||
|
|
||||||
We model an agent session as producing some events with lower in-session conversion levels relative to humans, this we state in our assumption that $P(\text{purchase} \vert A) < P(\text{purchase} \vert H)$ but with a potentially higher volatility in $\hat{q}$, which we observe through the look-to-book metrics in our simulation.
|
We model an agent session as producing some events with lower in-session conversion levels relative to humans, this we state in our assumption that $P(\text{purchase} \vert A) \ll P(\text{purchase} \vert H)$ but with a potentially higher volatility in $\hat{q}$, which we observe through the look-to-book metrics in our simulation.
|
||||||
|
|
||||||
\subsection{Economic Agents: From Homo Economicus to Machina Economicus}
|
\subsection{Economic Agents: From Homo Economicus to Machina Economicus}
|
||||||
|
|
||||||
Existing behavioral economic models tend to be criticized for the assumption of rational behavior, as is embodied in the term of homo economicus. The definition of a machina economicus by \textcite{parkes_economic_2015} is quite appropriate for our case, particularly because these assumptions of rationality have been argued to be a very adequate reference for AI research by \textcite{varian_economic_1995} due to its expected utility maximizing nature. For modeling this behavior, the trajectories of these agents can be formally defined to be partially observable Markov decision processes \parencite{xie_osworld_2024}. Agents are however not to be confused with web-bots which have previously been known as automated software applications or scrapers which are set with a purpose of carrying out specific tasks on the internet, without a higher level of internal judgement \parencite{imperva_rapid_2025}. In our research, we refer to this actor simply as an Agent belonging to the distribution $A$.
|
Existing behavioral economic models tend to be criticized for the assumption of rational behavior, as is embodied in the term of homo economicus. The definition of a machina economicus by \cite{parkes_economic_2015} is quite appropriate for our case, particularly because these assumptions of rationality have been argued to be a very adequate reference for AI research by \cite{varian_economic_1995}. For modeling this behavior, the trajectories of these agents can be formally defined to be partially observable Markov decision processes. \cite{xie_osworld_nodate} Agents are however not to be confused with web-bots which have previously been known as automated software applications or scrapers which are set with a purpose of carrying out specific tasks on the internet, without a higher level of internal judgement. \cite{imperva_rapid_2025} In our research, we refer to this actor simply as an Agent belonging to the distribution $A$.
|
||||||
|
|
||||||
This economic framing also helps separate two related but distinct phenomena of agents as buyers (changing market demand composition), and agents as information gatherers (changing the observed interactions used by pricing/recommendation systems). The thesis focuses on the second, where information acquisition strategically precedes purchase execution. We do not however dismiss the proposed expectation that existing economic systems serving humans, will not be populated by AIs across multiple channels and with various possibly misaligned goals as stated by \textcite{parkes_economic_2015}.
|
This economic framing also helps separate two related but distinct phenomena of agents as buyers (changing market demand composition), and agents as information gatherers (changing the observed interactions used by pricing/recommendation systems). The thesis focuses on the second, where information acquisition strategically precedes purchase execution. We do not however dismiss the proposed expectation that existing economic systems serving humans, will not be populated by AIs across multiple channels and with various possibly misaligned goals as stated by \cite{parkes_economic_2015}.
|
||||||
|
|
||||||
A HAP (HTTP Agent Profile) protocol has been developed as an internet draft by \textcite{dhir_http_2025} in an effort to separate agentic and human internet traffic, however the majority adoption by both the sellers and agent providers would be required for the implementation of such a solution.
|
|
||||||
|
|
||||||
\subsection{Problem Evidence and Market Impact}
|
\subsection{Problem Evidence and Market Impact}
|
||||||
|
|
||||||
The statistical issue of contamination in dynamic pricing systems that observe demand features as a means to update prices has been documented in various previous contexts. The airline industry (which has accounted for 24\% of observed disruptions) has seen malicious activity with a measureable impact on skewing key performance indicators by behavior visible in the look-to-book metrics. Excessive reconnaissance traffic inflates search volume without corresponding completed bookings, thereby skewing demand forecasts and disrupting dynamic pricing models. Demand proxies have also been observed to cause significant threat to inventory management by creating artificial scarcity that distorts the demand-supply relationships in the enterprise model. Censored demand as shown by \textcite{amjad_censored_2017} can also be observed in low-bias demand under-estimation caused by a distortion effect coming from non-human traffic data \parencite{imperva_rapid_2025}.
|
The statistical issue of contamination in dynamic pricing systems that observe demand features as a means to update prices has been documented in various previous contexts. The airline industry (which has accounted for 24\% of observed disruptions) has seen malicious activity with a measureable impact on skewing key performance indicators by behavior visible in the look-to-book metrics. Excessive reconnaissance traffic inflates search volume without corresponding completed bookings, thereby skewing demand forecasts and disrupting dynamic pricing models. Demand proxies have also been observed to cause significant threat to inventory management by creating artificial scarcity that distorts the demand-supply relationships in the enterprise model. Censored demand as shown in \cite{amjad_censored_2017} can also be observed in low-bias demand under-estimation caused by a distortion effect coming from non-human traffic data. \cite{imperva_rapid_2025}
|
||||||
|
|
||||||
When dynamic pricing algorithms operate on highly contaminated or noisy data, the risk grows significantly in creating inaccurate price inferences. The emergent mitigation driven by un-informed reward and regret signals might lead to price suppression for sales continuity which results in harming margins and resulting in a revenue loss. System that poorly fit undesired behavior might result in price gouging, which calls for strong guardrails while preserving targeted business strategy \parencite{mullapudi_reinforcement_2025}.
|
When dynamic pricing algorithms operate on highly contaminated or noisy data, the risk grows significantly in creating inaccurate price inferences. The emergent mitigation driven by un-informed reward and regret signals might lead to price suppression for sales continuity which results in harming margins and resulting in a revenue loss. System that poorly fit undesired behavior might result in price gouging, which calls for strong guardrails while preserving targeted business strategy. \cite{mullapudi_reinforcement_nodate}
|
||||||
|
|
||||||
|
|
||||||
%Documented instances of agent-driven market disruptions - Quantitative evidence of pricing manipulation - Case studies from affected industries
|
%Documented instances of agent-driven market disruptions - Quantitative evidence of pricing manipulation - Case studies from affected industries
|
||||||
@@ -31,42 +30,17 @@ When dynamic pricing algorithms operate on highly contaminated or noisy data, th
|
|||||||
\subsection{Theoretical Foundations: Economic Parallels}
|
\subsection{Theoretical Foundations: Economic Parallels}
|
||||||
|
|
||||||
|
|
||||||
Early hints of exploration of prices in a standard English auction explored by \textcite{varian_economic_1995} which hints at exploration of prices in a sequential manner, which leads to a marginally different cost to the bidder than the reservation price of the seller. This is a setting in which there is no cost incured by the buyer for their actions or exploring prices in the market. They propose that any agent responsable for the pricing of a good must be imune to dynamic strategies which might extract private information from a market. A key take-away which relates to the Vickery auction mechanism (also called a \textit{direct mechanism}) suggests that not only would defenses against such exploitation be necessary, but the construction of a mechanism in which revelation of the true willingness to pay is the dominant strategy for commerce.
|
|
||||||
|
|
||||||
Like in classical revenue-maximizing auctions \parencite{roughgarden_cs364a_2013} we assume that the human actor in our system has a private valuation $v$ which we formally draw from intrinsically defined distributions. The important note here is that the agent proxy does not have a mechanism to convey this private information into the demand data which directly impacts the pricing systems.
|
Early hints of exploration of prices in a standard English auction explored in \cite{varian_economic_1995} which hints at exploration of prices in a sequential manner, which leads to a marginally different cost to the bidder than the reservation price of the seller. This is a setting in which there is no cost incured by the buyer for their actions or exploring prices in the market. They propose that any agent responsable for the pricing of a good must be imune to dynamic strategies which might extract private information from a market. A key take-away which relates to the Vickery auction mechanism (also called a \textit{direct mechanism}) suggests that not only would defenses against such exploitation be necessary, but the construction of a mechanism in which revelation of the true willingness to pay is the dominant strategy for commerce.
|
||||||
|
|
||||||
The key component of this mediation between agents and commercial platforms lays in the transaction costs related to information gathering and negotiation. As proposed by \textcite{shahidi_coasean_2025} these costs are bound to collapse towards zero (which we demonstrate mathematically), calling for a re-evaluation of the boundaries between firms and markets. As argued by \textcite{coase_nature_1937}, the market participation and time associated with that participation, is critical part of the Coasean transaction cost logic which includes the discovery or relevant pricing within a given market. This process of price discovery without the presence of AI Agents can be time consuming and resource intensive. To build on top of this work we provide a proof of optimal conditions theorised by Coaes as an extension to AI-mediated markets.
|
Like in classical revenue-maximizing auctions \cite{roughgarden_cs364a_2013} we assume that the human actor in our system has a private valuation $v$ which we formally draw from later defined distributions. The important note here is that the agent proxy does not have a mechanism to convey this private information into the demand data which directly impacts the pricing systems.
|
||||||
|
|
||||||
% Economic foundations: relating the problem to options pricing theory. Cost of Information (COI) concept and its relevance
|
% Economic foundations: relating the problem to options pricing theory. Cost of Information (COI) concept and its relevance
|
||||||
|
|
||||||
|
% Link Coasean Singularity and other economic market theory and highlight specific information of supra competitive pricing.
|
||||||
|
|
||||||
|
|
||||||
\subsection{Landscape of Existing Work}
|
\subsection{Landscape of Existing Work}
|
||||||
|
|
||||||
Explorations of the algorithmic collusion by LLMs \parencite{fish_algorithmic_2025} has demonstrated a cross-model tendency of market division with a strong sensitivity to instructions provided in the ``system prompt''. If a dynamic pricing algorithm which is trained to respond to market signals learns to coordinate with competitor agents (or become manipulated by those agents), the market equilibrium is under threat of destabilization. This is particularly true for Q-learning pricing learners as demonstrated by \textcite{calvano_artificial_2018}.
|
Previous efforts in adversarial computer use LLM agents, show how multi-faceted the whole problem is
|
||||||
|
Here we can show a market visualization (venn-like-diagram)
|
||||||
Our effort to combat contamination stems from research by \textcite{hardt_strategic_2015} on strategic classification, in conjunction with \textcite{liu_contextual_2024} who demonstrate a linear regret if contamination is ignored. The strategic classification adversarial effect comes from an effort to manipulate some representative features used in a learning pipeline, which can result in lower prices on loans or lower prices from dynamic pricing algorithms.
|
|
||||||
|
|
||||||
To bridge the gap between detection and robust pricing, we look at work in Distributionally Robust Optimization (DRO). As defined by \textcite{kuhn_wasserstein_2024}, DRO provides a framework for decision-making under ambiguity, where the true data distribution is unknown but lies within a ``Wasserstein ball'' of a target distribution. In our context, the ``ambiguity set'' represents the uncertainty introduced by agentic reconnaissance. By optimizing for the worst-case distribution within this set, pricing mechanisms can become resilient to the distributional shifts such as the ones caused by non-human actors, effectively robustifying the revenue function against the contamination described in our problem statement.
|
|
||||||
|
|
||||||
In order to create an environment in which prices can be tested against a demand estimate generated by some behavioral model, we take inspiration from the architecture proposed by \textcite{ie_recsim_2019} in the RecSim platform built for recommendation systems. By modeling the distinct user behavior as POMDPs we can generate faithful interactions which allow us to generalize, past the constraint which is also present in recommendation systems, of rarely having enough experience with individual actor's interactions for good recommendations without generalization. The key inspiration comes from the user choice modeling which we translate to a user transition model for each distinct actor type (agent or human). We further consider the possibility of modeling our quantitative research platform using dynamic Bayesian networks for the sake of tractability within the system. The contribution or RecSim enables researchers to better understand learning algorithms in fixed environments, a gap we identify as needing to be bridged within the space of dynamic pricing.
|
|
||||||
% TODO: mention https://github.com/meta-pytorch/OpenEnv/tree/main/envs/browsergym_env
|
|
||||||
|
|
||||||
We also acknowledge the difficulty in similarly affected fields such as authorship, where \textcite{ganie_uncertainty_2025} demonstrate the theoretical limits of the distributional divergence between text authored by a human or large language model. Their approach of computing the divergence between two distributions demonstrates purely theoretically that no classifier can outperform random guessing on their particular task. This is yet another factor to take into consideration when exploring the potential mitigation strategies.
|
|
||||||
|
|
||||||
The setting of our work is quite complex and covers a wide range of topics, each with its own set of issues that further complicate the task at hand. There is however promise in the field of reinforcement learning and adversarial robustness to combat these problems. We can summarize the characteristics learned from the review of our environment as:
|
|
||||||
\begin{enumerate*}[label=(\roman*)]
|
|
||||||
\item non-stationary demand with temporal noise $\epsilon_t$
|
|
||||||
\item contaminated behavioral signals from mixed human-agent traffic with unknown mixing ratio $\alpha$
|
|
||||||
\item partial observability where only demand proxies $\hat{q}$ are available, not true demand $d(\cdot)$
|
|
||||||
\item strategic actors capable of feature manipulation to influence pricing outcomes
|
|
||||||
\item information asymmetry with private valuations $v$ drawn from unknown distributions
|
|
||||||
\item session-based interactions modeled as POMDPs with trajectories $\tau_s$
|
|
||||||
\item low conversion probability for agents: $P(\text{purchase} \mid A) < P(\text{purchase} \mid H)$
|
|
||||||
\item distributional uncertainty requiring robust optimization within Wasserstein ambiguity sets
|
|
||||||
\item potential for adversarial exploitation through false-name bidding and identity whitewashing.
|
|
||||||
\end{enumerate*}
|
|
||||||
|
|
||||||
|
|
||||||
%Previous efforts in adversarial computer use .LLM agents, show how multi-faceted the whole problem is
|
|
||||||
%Here we can show a market visualization (venn-like-diagram)
|
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
\section{Methodology}
|
\section{Methodology}
|
||||||
|
|
||||||
% Extra notes and clarifications: we observed some humans and get their transition probabilities between event types
|
|
||||||
% We modify behavioral profiles of transition matrices with price elasticity matrices generated by sample valuations of a distributing.
|
|
||||||
|
|
||||||
This section details the theoretical and practical framework developed to address dynamic pricing under the influence of non-human actors. We begin by formalizing the problem environment and the nature of the actors. We then derive the \textit{Cost of Information} (COI) theorem, proving the erosion of pricing power in the limit of agent saturation. Following this, we outline our generative contamination strategy using GOFAI-driven separability and transition probability learning. Finally, we formulate the robust control problem as a Stackelberg game solved via Distributionally Robust Reinforcement Learning (DR-RL) with constructed ambiguity sets.
|
This section details the theoretical and practical framework developed to address dynamic pricing under the influence of non-human actors. We begin by formalizing the problem environment and the nature of the actors. We then derive the \textit{Cost of Information} (COI) theorem, proving the erosion of pricing power in the limit of agent saturation. Following this, we outline our generative contamination strategy using GOFAI-driven separability and transition probability learning. Finally, we formulate the robust control problem as a Stackelberg game solved via Distributionally Robust Reinforcement Learning (DR-RL) with constructed ambiguity sets.
|
||||||
|
|
||||||
\subsection{Problem Formalization}
|
\subsection{Problem Formalization}
|
||||||
@@ -22,21 +19,13 @@ where:
|
|||||||
|
|
||||||
The platform does not directly observe the true underlying demand function $d(p)$. Instead, it observes a behavioral proxy $\hat{q}_t$, which is a composite signal derived from the mixture of actor types. We define the demand proxy for product $i$ at epoch $t$ as a weighted aggregation of events:
|
The platform does not directly observe the true underlying demand function $d(p)$. Instead, it observes a behavioral proxy $\hat{q}_t$, which is a composite signal derived from the mixture of actor types. We define the demand proxy for product $i$ at epoch $t$ as a weighted aggregation of events:
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\label{eq:qhat}
|
|
||||||
\hat{q}_{t,i} = \sum_{s \in \mathcal{S}_t} \sum_{k=1}^{L_s} \omega(a_{s,k}) \cdot \mathbb{1}[i_{s,k} = i]
|
\hat{q}_{t,i} = \sum_{s \in \mathcal{S}_t} \sum_{k=1}^{L_s} \omega(a_{s,k}) \cdot \mathbb{1}[i_{s,k} = i]
|
||||||
\end{equation}
|
\end{equation}
|
||||||
where $\omega: \mathcal{A} \to \mathbb{R}_+$ assigns weights to actions based on their signal strength regarding willingness to pay.
|
where $\omega: \mathcal{A} \to \mathbb{R}_+$ assigns weights to actions based on their signal strength regarding willingness to pay.
|
||||||
|
|
||||||
In the current engine implementation, we use the normalized variant of this proxy for each step:
|
|
||||||
\begin{equation}
|
|
||||||
\tilde q_{t,i} = 100 \cdot \frac{\hat q_{t,i}}{\sum_{j=1}^{N}\hat q_{t,j} + \varepsilon}
|
|
||||||
\end{equation}
|
|
||||||
with fixed category-level weights (cart, dwell, nav, filter) following the same rank order from Table~\ref{tab:action_space}. This keeps the signal dense and directly usable in the simulator.
|
|
||||||
|
|
||||||
\subsubsection{Actor Types and Demand Curves}
|
\subsubsection{Actor Types and Demand Curves}
|
||||||
We formalize the heterogeneity of actors by introducing a type space $\Theta$. An actor of class $Y_s$ is further parameterized by a type $\theta \sim \mathcal{D}_{Y}$. This type determines the actor's demand response function $d(p; \theta)$, sampled from a distribution of possible demand curves. The total observed demand is a stochastic process governed by the naively defined mixture:
|
We formalize the heterogeneity of actors by introducing a type space $\Theta$. An actor of class $Y_s$ is further parameterized by a type $\theta \sim \mathcal{D}_{Y}$. This type determines the actor's demand response function $d(p; \theta)$, sampled from a distribution of possible demand curves. The total observed demand is a stochastic process governed by the mixture:
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\label{eq:mixture_demand}
|
|
||||||
Q(p) = (1-\alpha) \cdot \mathbb{E}_{\theta \sim \mathcal{D}_H}[d(p; \theta)] + \alpha \cdot \mathbb{E}_{\theta \sim \mathcal{D}_A}[d(p; \theta)] + \epsilon_t
|
Q(p) = (1-\alpha) \cdot \mathbb{E}_{\theta \sim \mathcal{D}_H}[d(p; \theta)] + \alpha \cdot \mathbb{E}_{\theta \sim \mathcal{D}_A}[d(p; \theta)] + \epsilon_t
|
||||||
\end{equation}
|
\end{equation}
|
||||||
where $\alpha \in [0, 1]$ represents the contamination parameter (proportion of agents) and $\epsilon_t$ is non-stationary market noise.
|
where $\alpha \in [0, 1]$ represents the contamination parameter (proportion of agents) and $\epsilon_t$ is non-stationary market noise.
|
||||||
@@ -45,18 +34,15 @@ where $\alpha \in [0, 1]$ represents the contamination parameter (proportion of
|
|||||||
|
|
||||||
\subsection{Cost of Information (COI) Framework}
|
\subsection{Cost of Information (COI) Framework}
|
||||||
|
|
||||||
The platform's pricing power comes from information asymmetry: users who express strong interest signals pay more than the base price. We quantify this markup as the \textit{Cost of Information} (COI), which represents the average premium extracted above marginal cost. COI measures the revenue at risk when information asymmetry collapses.
|
The \textit{Cost of Information} (COI) represents the markup a pricing policy $\pi$ attempts to extract from the market by leveraging demand signals. We define COI as the expected premium over the minimum viable price $\underline{p}$ (or marginal cost). This also speaks to the financial urgency as a consequence of information asymmetry between the platform and the actors.
|
||||||
A top-level view in the current AI discourse is that sufficiently large productivity gains can induce vertical deflation through cost compression and supply expansion \parencite{rachitsky_marc_2026}. Our contribution is narrower and mechanism-level: even under long-run deflation, platform revenue still depends on short-run information costs to the user. We formalize that rent as the Cost of Information (COI) and study how agentic reconnaissance accelerates its erosion.
|
|
||||||
|
|
||||||
\begin{definition}[Cost of Information]
|
\begin{definition}[Cost of Information]
|
||||||
Let $\pi(\tau)$ be a pricing policy mapping interaction histories to prices. The COI is defined as:
|
Let $\pi(\tau)$ be a pricing policy mapping interaction histories to prices. The COI is defined as:
|
||||||
\begin{equation}
|
\begin{align}
|
||||||
\text{COI} = \mathbb{E}[P] - \underline{p}
|
\text{COI} &= \mathbb{E}[P] - \underline{p} \\
|
||||||
\end{equation}
|
&= \int_{\underline{p}}^{\bar{p}} (1 - F_\pi(p)) \, dp
|
||||||
where $\mathbb{E}[P]$ is the expected price charged by the policy and $\underline{p}$ is the minimum viable price (marginal cost).
|
\end{align}
|
||||||
% Alternative survival function representation (used in proof):
|
where $F_\pi(p)$ is the cumulative distribution function of prices generated by $\pi$ under standard operating conditions.
|
||||||
% COI = \int_{\underline{p}}^{\bar{p}} (1 - F_\pi(p)) \, dp
|
|
||||||
% where F_\pi(p) is the CDF of prices generated by \pi
|
|
||||||
\end{definition}
|
\end{definition}
|
||||||
|
|
||||||
\begin{figure}[ht]
|
\begin{figure}[ht]
|
||||||
@@ -93,39 +79,46 @@ where $\mathbb{E}[P]$ is the expected price charged by the policy and $\underlin
|
|||||||
|
|
||||||
We now formally demonstrate that standard dynamic pricing mechanisms are not incentive-compatible with high-frequency agentic traffic. As the number of independent competitive agents $N$ querying the system grows, the platform's ability to sustain a COI vanishes.
|
We now formally demonstrate that standard dynamic pricing mechanisms are not incentive-compatible with high-frequency agentic traffic. As the number of independent competitive agents $N$ querying the system grows, the platform's ability to sustain a COI vanishes.
|
||||||
|
|
||||||
A fundamental assumption for our claim lies in the alignment of the AI agent through its prompt which has been demonstrated by \cite{fish_algorithmic_2025} to cause strong collusive behavior under linguistic nudges. This assumption can be generalized to the human user asking the agent to research products with a minimizing objective.
|
|
||||||
|
|
||||||
\begin{theorem}[COI Erosion in the Limit]
|
\begin{theorem}[COI Erosion in the Limit]
|
||||||
Let $N$ be the number of independent, utility-maximizing agents querying the platform. Let $p_{(1)}$ be the first order statistic (minimum) of the prices offered to these agents. As $N \to \infty$, the Cost of Information converges to 0.
|
Let $N$ be the number of independent, utility-maximizing agents querying the platform. Let $p_{(1)}$ be the first order statistic (minimum) of the prices offered to these agents. As $N \to \infty$, the Cost of Information converges to 0.
|
||||||
\end{theorem}
|
\end{theorem}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
\begin{proof}
|
\begin{proof}
|
||||||
Consider $N$ independent agents querying the platform, each receiving a price sample $p_i$ drawn from the pricing policy's distribution $F(p)$ bounded by $[\underline{p}, \bar{p}]$. A strategic agent conducting reconnaissance will select the minimum observed price: $p_{(1)} = \min(p_1, \ldots, p_N)$.
|
Let $p_1, \ldots, p_N$ be independent and identically distributed (i.i.d.) price samples drawn from the policy's distribution $F(p)$ with support $[\underline{p}, \bar{p}]$. The realizable price for an optimal searching agent is the first order statistic $p_{(1)} = \min(p_1, \ldots, p_N)$.
|
||||||
% support here means that its the range of possible outputs.
|
|
||||||
The probability that the minimum price exceeds some threshold $t$ is:
|
The survival function (or reliability function) of the minimum price is given by:
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
P(p_{(1)} > t) = P(\text{all } p_i > t) = [1 - F(t)]^N
|
S_{p_{(1)}}(t) = P(p_{(1)} > t) = [1 - F(t)]^N
|
||||||
\end{equation}
|
\end{equation}
|
||||||
|
|
||||||
For any price $t > \underline{p}$, the CDF satisfies $F(t) > 0$, so $1 - F(t) < 1$. As $N$ grows, this probability decays exponentially: $[1 - F(t)]^N \to 0$.
|
To determine the expected value $\mathbb{E}[p_{(1)}]$, we recall the property that for any continuous random variable $X$ with support $[A, B]$, the expectation can be expressed as the lower bound plus the integral of the survival function:
|
||||||
|
|
||||||
The expected minimum price can be written as:
|
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\mathbb{E}[p_{(1)}] = \underline{p} + \int_{\underline{p}}^{\bar{p}} [1 - F(t)]^N \, dt
|
\mathbb{E}[X] = A + \int_{A}^{B} P(X > t) \, dt
|
||||||
\end{equation}
|
\end{equation}
|
||||||
|
|
||||||
Since the integrand vanishes as $N \to \infty$ for all $t > \underline{p}$, the integral converges to zero. Therefore:
|
Applying this to our pricing statistic where the lower bound is $\underline{p}$:
|
||||||
|
\begin{align}
|
||||||
|
\mathbb{E}[p_{(1)}] &= \underline{p} + \int_{\underline{p}}^{\bar{p}} P(p_{(1)} > t) \, dt \\
|
||||||
|
&= \underline{p} + \int_{\underline{p}}^{\bar{p}} [1 - F(t)]^N \, dt
|
||||||
|
\end{align}
|
||||||
|
|
||||||
|
Since $F(t)$ is a valid CDF, for any $t > \underline{p}$, we have strict inequality $F(t) > 0$, implying $0 \le 1 - F(t) < 1$. By the properties of limits, as $N \to \infty$, the term $[1 - F(t)]^N$ converges to 0 pointwise for all $t > \underline{p}$.
|
||||||
|
|
||||||
|
Applying the Lebesgue Dominated Convergence Theorem (noting that the integrand is bounded by 1 on the finite interval $[\underline{p}, \bar{p}]$):
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\lim_{N \to \infty} \text{COI} = \lim_{N \to \infty} (\mathbb{E}[p_{(1)}] - \underline{p}) = 0
|
\lim_{N \to \infty} \int_{\underline{p}}^{\bar{p}} [1 - F(t)]^N \, dt = \int_{\underline{p}}^{\bar{p}} 0 \, dt = 0
|
||||||
\end{equation}
|
\end{equation}
|
||||||
|
|
||||||
|
Substituting this back into the expression for COI:
|
||||||
|
\begin{align}
|
||||||
|
\lim_{N \to \infty} \text{COI} &= \lim_{N \to \infty} (\mathbb{E}[p_{(1)}] - \underline{p}) \\
|
||||||
|
&= \lim_{N \to \infty} \left( (\underline{p} + 0) - \underline{p} \right) \\
|
||||||
|
&= 0
|
||||||
|
\end{align}
|
||||||
\end{proof}
|
\end{proof}
|
||||||
|
|
||||||
|
|
||||||
This result naively proves that standard pricing policies $\pi$ fail to extract surplus in the presence of large-scale agentic search, necessitating a robust counter-mechanism.
|
This result proves that standard pricing policies $\pi$ fail to extract surplus in the presence of large-scale agentic search, necessitating a robust counter-mechanism.
|
||||||
|
|
||||||
% The DRO objective creates a lower bound on COI extraction, effectively guaranteeing a minimum margin even in the presence of adversarial agents. we need to prove this and demonstrate that in a theorem.
|
% The DRO objective creates a lower bound on COI extraction, effectively guaranteeing a minimum margin even in the presence of adversarial agents. we need to prove this and demonstrate that in a theorem.
|
||||||
|
|
||||||
@@ -136,18 +129,14 @@ This result naively proves that standard pricing policies $\pi$ fail to extract
|
|||||||
|
|
||||||
In order for our research to have grounding in interactions we built a robust e-commerce web-platform. We initially conducted a survey of the leading platforms of airlines and hotel booking sites to identify the specific interface patterns that effectively manage complex travel data. Our analysis revealed a clear industry standard: while both sectors rely on tabbed service selection and left-sidebar filtering to streamline navigation, they diverge in result presentation: airlines utilize visual date-price bars and multi-step wizards to optimize for logistical transparency, whereas hotel platforms leverage image-led cards and scarcity triggers to drive emotional engagement and urgency. Our web framework defines a highly agnostic boilerplate which can be seeded with any data-modality with an easy-to-tailor pattern, which we leverage to define a \texttt{hotel} and \texttt{airline} mode. Both modes are then individually deployed via an environment level argument which adjusts the proxy routing with a custom middleware inside next.js to render only the desired mode. The purpose of this was to create a baseline adaptable to any use-case or desired commercial application.
|
In order for our research to have grounding in interactions we built a robust e-commerce web-platform. We initially conducted a survey of the leading platforms of airlines and hotel booking sites to identify the specific interface patterns that effectively manage complex travel data. Our analysis revealed a clear industry standard: while both sectors rely on tabbed service selection and left-sidebar filtering to streamline navigation, they diverge in result presentation: airlines utilize visual date-price bars and multi-step wizards to optimize for logistical transparency, whereas hotel platforms leverage image-led cards and scarcity triggers to drive emotional engagement and urgency. Our web framework defines a highly agnostic boilerplate which can be seeded with any data-modality with an easy-to-tailor pattern, which we leverage to define a \texttt{hotel} and \texttt{airline} mode. Both modes are then individually deployed via an environment level argument which adjusts the proxy routing with a custom middleware inside next.js to render only the desired mode. The purpose of this was to create a baseline adaptable to any use-case or desired commercial application.
|
||||||
|
|
||||||
The architecture of this platform begins with the deployed web-apps posting interaction data to our backend which processes them and stores each ingested interaction into a kafka cluster. This serves as our data reservoir tracking and associating each interaction with its session and importantly with which experiment it belongs to. Not only do we track the behavioral interactions, but our pricing provider micro-service, once called by the frontend reports the observed/queried price-product into kafka. This kafka cluster is subscribed to by our pipeline which is configured on a schedule in Airflow, with the possibility of manual trigger. The final stage of the pricing pipeline, submits computed dynamic pricing results into a redis database for quick updates which is then read by the pricing provider and displayed on the webapp. This is a very generic end-to-end mechanism which is applicable to a variety of different e-commerce tasks. We intentionally put emphasis on the development of this infrastructure to establish a reproducible framework for interaction and to minimize any noise.
|
|
||||||
|
|
||||||
\paragraph{Public Web Artifact} We transition the Kappa like architecture of the data collection to a Lambda architecture for actual learning in a surrogate environment. This allows us to move faster on data which is provided and helps us create a feedback loop for production deployment. To support further research in this intersection of fields we release P4P \footnote{\url{https://github.com/velocitatem/p4p}} as a public repository providing the interaction layer of the PHANTOM framework. This provides a configurable storefront which can be tailored to any commercial setting with a standardized session-level event tracking. We document the API adapters or what the framework expects in terms of schemas for pricing providers and log ingestion servicse. The repository is intended for controlled experimentation and method replication rather than production commerce deployment.
|
The architecture of this platform begins with the deployed web-apps posting interaction data to our backend which processes them and stores each ingested interaction into a kafka cluster. This serves as our data reservoir tracking and associating each interaction with its session and importantly with which experiment it belongs to. Not only do we track the behavioral interactions, but our pricing provider micro-service, once called by the frontend reports the observed/queried price-product into kafka. This kafka cluster is subscribed to by our pipeline which is configured on a schedule in Airflow, with the possibility of manual trigger. The final stage of the pricing pipeline, submits computed dynamic pricing results into a redis database for quick updates which is then read by the pricing provider and displayed on the webapp. This is a very generic end-to-end mechanism which is applicable to a variety of different e-commerce tasks. We intentionally put emphasis on the development of this infrastructure to establish a reproducible framework for interaction and to minimize any noise.
|
||||||
|
|
||||||
|
|
||||||
\subsubsection{DevOps Principles}
|
\subsubsection{DevOps Principles}
|
||||||
|
|
||||||
Reproducible results are key to quality research platforms, this is taken into mind when deploying and working with our research platform. From a deployment standpoint the platform can be deployed across a large variety of providers and can be run locally. When developing a new interaction modality apart from the ones that come out of the box, a simple template pattern can be followed. The middleware of the framework is designed to properly render the chosen modality from environmental variables, thus deployment of different or parallel version of the software can be easily parametrized.
|
|
||||||
|
|
||||||
\subsubsection{Online Dynamic Pricing}
|
\subsubsection{Online Dynamic Pricing}
|
||||||
|
|
||||||
In order to collect data from actors under correct conditions we replicate a naive and simple dynamic pricing algorithm which runs in the background during the experiments.
|
|
||||||
The dynamic pricing done is handled by a pipeline which computes a demand estimate on a per-product basis of a specific window of the data, defined by the period $T$ which by default is 5 minutes. This dynamic pricing pipeline computes a demand estimate vector $\hat{q} \in \mathbb{R}^N$ by a weighted sum of interactions for each product, it additionally computes a price elasticity vector $\hat{\epsilon}$ in the same dimensions as our demand. The final features matrix is of the size $N \times 2$ which we translate to a new price vector $\hat{p} \in \mathbb{R}^N$. The transformation that governs this dynamic pricing is a very simple surge-based pricing (a special case of our later defined policy $\pi$):
|
The dynamic pricing done is handled by a pipeline which computes a demand estimate on a per-product basis of a specific window of the data, defined by the period $T$ which by default is 5 minutes. This dynamic pricing pipeline computes a demand estimate vector $\hat{q} \in \mathbb{R}^N$ by a weighted sum of interactions for each product, it additionally computes a price elasticity vector $\hat{\epsilon}$ in the same dimensions as our demand. The final features matrix is of the size $N \times 2$ which we translate to a new price vector $\hat{p} \in \mathbb{R}^N$. The transformation that governs this dynamic pricing is a very simple surge-based pricing (a special case of our later defined policy $\pi$):
|
||||||
|
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
@@ -161,31 +150,22 @@ p_{0,i} & \text{otherwise}
|
|||||||
|
|
||||||
where $p_0 \in \mathbb{R}^N$ is the base price vector (which is seeded into our database distinctly for each mode of the commerce platform), $\theta_{\text{high}}, \theta_{\text{low}} \in \mathbb{R}$ are demand thresholds defining surge and discount regions, and $\lambda_{\text{surge}}, \lambda_{\text{disc}} \in \mathbb{R}^+$ are multiplicative factors with typical values $\lambda_{\text{surge}} = 1.2$ and $\lambda_{\text{disc}} = 0.9$. This piecewise function enables rapid price adjustment in response to observed demand without requiring complex elasticity estimation or historical calibration, allowing us to expose actors within our experiments to a system with a dynamic component of pricing.
|
where $p_0 \in \mathbb{R}^N$ is the base price vector (which is seeded into our database distinctly for each mode of the commerce platform), $\theta_{\text{high}}, \theta_{\text{low}} \in \mathbb{R}$ are demand thresholds defining surge and discount regions, and $\lambda_{\text{surge}}, \lambda_{\text{disc}} \in \mathbb{R}^+$ are multiplicative factors with typical values $\lambda_{\text{surge}} = 1.2$ and $\lambda_{\text{disc}} = 0.9$. This piecewise function enables rapid price adjustment in response to observed demand without requiring complex elasticity estimation or historical calibration, allowing us to expose actors within our experiments to a system with a dynamic component of pricing.
|
||||||
|
|
||||||
% For our offline experimental setting, we generalize a master value function that can encompass different demand estimation and pricing strategies.
|
We will for our offilne experimental intents generalize a master function for encompasing distinct demand estimation and pricing strategies.
|
||||||
%
|
|
||||||
% \begin{align}
|
\begin{align}
|
||||||
% V(\cdot) = \max_{p_t} \min_{Q \in \mathcal{U}(\hat{d})}{\mathbb{E}_{d\sim Q} [p_t \times d(p_t, x_t ; \theta) + \psi V_{t+1}(\cdot)]}
|
V(\cdot) = \max_{p_t} \min_{Q \in \mathcal{U}(\hat{d})}{\mathbb{E}_{d\sim Q} [p_t \times d(p_t, x_t ; \theta) + \psi V_{t+1}(\cdot)]}
|
||||||
% \end{align}
|
\end{align}
|
||||||
%
|
|
||||||
% We evaluate different substitutions of this objective, which later serve as hyperparameters in the simulator.
|
We follow differnet substitutouns which will server as hyperparameters later on.
|
||||||
|
|
||||||
\subsection{Experimental Design}
|
\subsection{Experimental Design}
|
||||||
|
|
||||||
We start from a practical constraint: we do not have access to proprietary production data. Because of that, we design our own fictional platform that still represents how commercial platforms work in the real world. The design comes from a survey of hotel and airline websites, where we extracted common interface components and used them as a high-level template for dynamic pricing environments.
|
The experimentation begins with the design of goals, with careful consideration to assure a uniform spanning across different variables within each product-architecture of either the hotel or airline platforms. Our crafted collection of goals (jobs to be done) is then tracked in a postgress database with one table to track goals and another table to track different experiment runs, and their associated goals in a experiment-goal one-to-one relationship.
|
||||||
|
|
||||||
The interface is organized as a product catalog where each product belongs to a time-bounded price vector (for example, a daily pricing period). During each period we collect interaction data by instrumenting UI components and predefined action templates that are still customizable. This gives us control without losing realism.
|
The purpose of this effort to gather data on interactions, is the first half of our research. With this collected data on behavioral characteristics, enhanced by our feature augmentation, we can create distribution separation into two bins $y \in \{A,H\}$ with a certain probability $p$ dependent on the session-specific features. To address the second loop of our system, we use this gained capability of discrimination to enhance the learner design involved in our surrogate dynamic pricing task which simulates an independent dynamic pricing scenario under which we can train a more controlled policy with the ability to account for true demand signals under conditions of contamination from non-human actors.
|
||||||
|
|
||||||
Since users act with motivations, we define a pool of tasks (jobs to be done) and assign tasks randomly to participants.
|
|
||||||
% TODO: describe the task pool in detail here -- list the specific tasks used in the experiments
|
|
||||||
A representative task is to find the cheapest feasible catalog item under explicit constraints while removing strict financial limits so we avoid trivial optimization behavior. Participants are also randomly assigned to one experimental platform mode (hotel or airline). Once assigned, they are dropped into the experiment with an actor ID. Under each experiment ID, we can observe multiple sessions across time and gather long interaction traces for the same actor.
|
|
||||||
|
|
||||||
The human data collection involved 18 participants, all of whom provided explicit informed consent prior to their session. Participants had an average age of 21 years and were recruited from a university population. Alongside the 18 human sessions we ran 18 agent sessions of equivalent task scope, giving a balanced dataset of 36 labeled trajectories. Each participant was assigned a single platform mode and a single task drawn from the pool, and completed the session independently without guidance on navigation or pricing strategy.
|
Our approach can be well summarized by a three-stage division, first we intend to observe and \textit{vectorize} the behavioral interaction data from our experiments, we then develop the separability which helps us deepen the semantic understanding of the behavioral patterns. Finally we use our newly gained learner to leverage a defensive mechanism within the simulation stage of a controlled dynamic pricing loop.
|
||||||
|
|
||||||
To evaluate quality and realism of the setup, we store both structured event logs and full interaction transcripts. This lets us combine quantitative analysis with transcript-level qualitative findings. The result is an isolated system where we can control the interaction process while preserving realistic behavior.
|
|
||||||
|
|
||||||
Operationally, goals and experiment runs are tracked in PostgreSQL (goal table, run table, and assignment mapping). This data-acquisition phase is the first half of the methodology and is intentionally a disconnected component that feeds the later contributions. The second half uses collected behavioral traces to separate classes $y \in \{A,H\}$ with session-conditioned probability estimates, then injects those estimates into the pricing learner.
|
|
||||||
|
|
||||||
Our process follows three stages: (1) observe and \textit{vectorize} behavioral interactions, (2) learn separability to characterize human versus agent patterns, and (3) use the learned signal to train a defensive policy in a controlled dynamic-pricing simulator.
|
|
||||||
|
|
||||||
\begin{figure}[ht]
|
\begin{figure}[ht]
|
||||||
\resizebox{\columnwidth}{!}{%
|
\resizebox{\columnwidth}{!}{%
|
||||||
@@ -194,142 +174,23 @@ Our process follows three stages: (1) observe and \textit{vectorize} behavioral
|
|||||||
\caption{Overview of the Dynamic Pricing Tasks.}
|
\caption{Overview of the Dynamic Pricing Tasks.}
|
||||||
\end{figure}
|
\end{figure}
|
||||||
|
|
||||||
Our web platform (developed in similar spirit to RecSim \parencite{ie_recsim_2019}) gives us a controlled environment where tasks are assigned to human and agentic actors and then executed. Each actor receives a browser-level experiment identifier that may persist across multiple session IDs. We then group by experiment and extract session trajectories using the schema below.
|
|
||||||
|
|
||||||
To speak to realism, user interviews reported that the platform architecture mirrored standard booking interfaces and reduced the cognitive load required to learn the system. One participant described the flow as ``intuitive'' and close to a ``normal'' transaction, suggesting observed behavior was primarily driven by pricing treatment rather than interface novelty.
|
Study methodology and approach. Data acquisition strategy. Defined objectives and success criteria. Observable metrics and KPIs.
|
||||||
|
|
||||||
The dynamic pricing mechanism elicited immediate behavioral adjustments. Participants were sensitive to price volatility: sudden boosts triggered urgency and faster booking attempts, while large listing-to-final discrepancies triggered deeper comparison behavior. This is comforting because the controlled setup still produces commercially relevant interaction data.
|
|
||||||
|
|
||||||
|
|
||||||
\subsubsection{Design of Training Factorial Study}
|
|
||||||
|
|
||||||
The simulator has multiple configurable factors. We design a multi-factor study across five axes derived from the sweep configurations: (1) RL algorithm (\texttt{ppo}, \texttt{a2c}, \texttt{dqn}, \texttt{qtable}; 4 levels), (2) contamination ratio $\alpha$ sampled from $[0.1, 0.6]$ at four representative levels, (3) robustness radius $\epsilon_\alpha \in \{0.0, 0.15, 0.3\}$ (3 levels), (4) COI penalty weight $\lambda_\text{coi}$ at two reference levels, and (5) pricing action granularity (two discretization settings for \texttt{action\_levels}); giving a grid of $4\times4\times3\times2\times2 = 192$ configurations. Statistical power for the behavioral comparisons is determined by a two-sample test over per-session KL divergence scores; a formal power analysis with minimum detectable effect size at $n=18+18$ is reported in the results.
|
|
||||||
% Power analysis plan: apply a two-sample Mann-Whitney U (or permutation test) on per-session (delta_H - delta_A) divergence scores comparing the human and agent groups. Compute minimum detectable effect size at alpha=0.05, power=0.8, given n=18 per group. Bootstrap confidence intervals on mean KL are a cleaner complement given the non-normality of divergence distributions.
|
|
||||||
While this scale is generally expensive for reinforcement learning, we execute it on a large TPU cluster to make the sweep tractable.
|
|
||||||
|
|
||||||
Our training budget is provisioned through TPU Research Cloud and spans 384 chips across TPU v4, v5e, and v6e generations, with a spot-heavy allocation plus an on-demand reserve. At peak BF16 throughput this corresponds to approximately 160 PFLOPS of aggregate compute, which makes repeated seeds, ablations, and sensitivity sweeps feasible within practical wall-clock limits. We allocate v6e capacity to the highest-intensity policy training jobs, use v5e for wider hyperparameter exploration where throughput-per-dollar is favorable, and reserve on-demand v4 capacity for runs that should not be interrupted.
|
|
||||||
|
|
||||||
\begin{table}[ht]
|
|
||||||
\centering
|
|
||||||
\caption{Compact comparison of TPU generations used in the training stack.}
|
|
||||||
\label{tab:tpu_specs}
|
|
||||||
\begin{tabular}{@{}llll@{}}
|
|
||||||
\toprule
|
|
||||||
\textbf{Feature} & \textbf{TPU v4} & \textbf{TPU v5e} & \textbf{TPU v6e (Trillium)} \\
|
|
||||||
\midrule
|
|
||||||
Peak BF16 per chip (TFLOPS) & 275 & 197 & 918 \\
|
|
||||||
HBM capacity per chip (GB) & 32 & 16 & 32 \\
|
|
||||||
HBM bandwidth per chip (GB/s) & 1200 & 819 & 1600 \\
|
|
||||||
TensorCores per chip & 2 & 1 & 1 \\
|
|
||||||
Interconnect topology & 3D mesh/torus & 2D torus & 2D torus \\
|
|
||||||
Max pod size (chips) & 4096 & 256 & 256 \\
|
|
||||||
\bottomrule
|
|
||||||
\end{tabular}
|
|
||||||
\end{table}
|
|
||||||
|
|
||||||
\begin{table}[ht]
|
|
||||||
\centering
|
|
||||||
\caption{TPU allocation used for the factorial study.}
|
|
||||||
\label{tab:tpu_allocation}
|
|
||||||
\begin{tabular}{@{}llll@{}}
|
|
||||||
\toprule
|
|
||||||
\textbf{TPU Type} & \textbf{Total Chips} & \textbf{Zone(s)} & \textbf{Provisioning} \\
|
|
||||||
\midrule
|
|
||||||
v6e & 128 (64 + 64) & europe-west4-a, us-east1-d & Spot \\
|
|
||||||
v5e & 128 (64 + 64) & us-central1-a, europe-west4-b & Spot \\
|
|
||||||
v4 & 64 (32 + 32) & us-central2-b & 32 Spot + 32 On-demand \\
|
|
||||||
\bottomrule
|
|
||||||
\end{tabular}
|
|
||||||
\end{table}
|
|
||||||
|
|
||||||
For connections from Madrid, we prioritize the europe-west4 allocation for latency-sensitive runs with the benefit of having the most grouped chips within a single region. This regional grouping is important for the deployment of our Kubernetes cluster which cannot span multiple regions. All sweep metadata, model checkpoints, and reward traces are logged in Weights \& Biases. Hardware specifications are from the official Google Cloud TPU documentation \parencite{noauthor_tpu_2026,noauthor_tpu_2025-1,noauthor_tpu_2025}.
|
|
||||||
|
|
||||||
Design of training processes: we build docker image with the fact in mind of different caching over layers in order to most speed up docker re-building and such we place the most volatile steps towards the end of the image building. What is means in practice is that any dependency installations are isolated so edits to source code do no trigger rebuilds. Only if we update our entry point of training a sweep, Docker will also rebuild the source-code copy stage.
|
|
||||||
|
|
||||||
Due to the preemptive nature of the current demand of TPU chips we sttle for running our on demeaned as the primary source of compute. The on demand TPU pod of 32 chips spread across 4 virtual hosts creates a relatively unique parallelization setup. Despite our desire to use a traditional approach of clustering and perhaps deploying SLURM jobs of our sweep agent, the lack of predictability in provisioning each instance of a compute resource makes this an high friction layer we do not want to add.
|
|
||||||
|
|
||||||
\subsubsection{Interaction Schema}
|
|
||||||
|
|
||||||
We extend the basic event tuple $e_{s,k}$ to capture the full observational signal available to the platform. An interaction event is defined as the extended tuple:
|
|
||||||
\begin{equation}
|
|
||||||
e_{s,k} = \left( a_{s,k}, \, i_{s,k}, \, t_{s,k}, \, \mu_{s,k}, \, \delta_{s,k} \right)
|
|
||||||
\end{equation}
|
|
||||||
where $\mu_{s,k} \in \mathcal{M}$ is a metadata record containing action-specific context (e.g., price observed, filter parameters, element text), and $\delta_{s,k} \in \mathbb{R}_+$ is the dwell time in milliseconds for attention-based actions.
|
|
||||||
|
|
||||||
A session $s$ is itself a structured record:
|
|
||||||
\begin{equation}
|
|
||||||
s = \left( \text{sid}, \, \text{eid}, \, t_0, \, \phi, \, \mathcal{U}, \, \tau_s \right)
|
|
||||||
\end{equation}
|
|
||||||
where $\text{sid}$ is a unique session identifier (UUID), $\text{eid}$ optionally links to an experiment, $t_0$ is the session start timestamp, $\phi \in \{\texttt{hotel}, \texttt{airline}\}$ denotes the platform mode, $\mathcal{U}$ is the user-agent string, and $\tau_s$ is the trajectory of events.
|
|
||||||
|
|
||||||
The action space $\mathcal{A}$ is partitioned into four semantic categories based on the behavioral signal each action conveys:
|
|
||||||
|
|
||||||
\begin{table}[ht]
|
|
||||||
\centering
|
|
||||||
\caption{Action space partition $\mathcal{A} = \mathcal{A}_{\text{nav}} \cup \mathcal{A}_{\text{cart}} \cup \mathcal{A}_{\text{filter}} \cup \mathcal{A}_{\text{dwell}}$ with signal interpretation.}
|
|
||||||
\label{tab:action_space}
|
|
||||||
\begin{tabular}{@{}llll@{}}
|
|
||||||
\toprule
|
|
||||||
\textbf{Category} & \textbf{Actions} & \textbf{Signal} & $\boldsymbol{\omega}$ \\
|
|
||||||
\midrule
|
|
||||||
$\mathcal{A}_{\text{cart}}$ & \texttt{add\_item}, \texttt{remove}, \texttt{checkout}, \texttt{purchase} & Purchase intent & High \\
|
|
||||||
$\mathcal{A}_{\text{dwell}}$ & \texttt{hover\_title}, \texttt{hover\_paragraph}, \texttt{hover\_link} & Sustained attention & Medium \\
|
|
||||||
$\mathcal{A}_{\text{nav}}$ & \texttt{page\_view}, \texttt{view\_item}, \texttt{learn\_more} & Discovery & Low \\
|
|
||||||
$\mathcal{A}_{\text{filter}}$ & \texttt{search}, \texttt{filter\_date}, \texttt{filter\_price}, \texttt{sort} & Preference refinement & Lowest \\
|
|
||||||
\bottomrule
|
|
||||||
\end{tabular}
|
|
||||||
\end{table}
|
|
||||||
|
|
||||||
This partition enables the weight function $\omega$ from Eq.~\ref{eq:qhat} to assign category-specific signal strengths, with $\omega(\mathcal{A}_{\text{cart}}) > \omega(\mathcal{A}_{\text{dwell}}) > \omega(\mathcal{A}_{\text{nav}}) > \omega(\mathcal{A}_{\text{filter}})$ reflecting decreasing commitment.
|
|
||||||
|
|
||||||
In the simulator baseline this order is encoded with a compact fixed scale: cart $=4.0$, dwell $=2.0$, nav $=1.0$, filter $=0.5$. Unknown actions are mapped by prefix heuristics to the nearest category.
|
|
||||||
|
|
||||||
The metadata record $\mu$ varies by action type. For product views, $\mu$ contains the observed price $p_{\text{obs}}$ and product attributes. For dwell events, $\mu$ includes the element text and accumulated hover duration. This heterogeneous structure is captured via a schema-on-read approach in our Kafka ingestion pipeline, where events are validated against type-specific schemas before storage.
|
|
||||||
|
|
||||||
In addition to behavioral events, the platform logs price observations to a separate Kafka topic. Each price query generates a record $(i, p, \text{sid}, \phi, t)$ associating the product, displayed price, requesting session, platform mode, and timestamp. This dual-stream architecture enables joint analysis of price exposure and behavioral response.
|
|
||||||
|
|
||||||
|
|
||||||
\subsection{Generative Contamination and Separability}
|
\subsection{Generative Contamination and Separability}
|
||||||
|
|
||||||
To train a robust pricing learner, we need a simulator that can generate realistic interaction data under controlled contamination. We build this from Phantom data using a two-stage approach.
|
To develop a robust pricing agent, we require a simulation environment capable of generating realistic, contaminated interaction data. We achieve this by learning from our Phantom platform data using a two-stage approach.
|
||||||
|
|
||||||
|
|
||||||
\subsubsection{Ground-Truth Separability}
|
|
||||||
Because sessions are collected under controlled experimental conditions where each actor is assigned a known type at the start of the trial, labels $y_s \in \{H, A\}$ are available as ground truth rather than as the output of a heuristic classifier. We therefore estimate separate transition kernels directly from each labeled partition $\mathcal{D}_H$ and $\mathcal{D}_A$, treating the resulting $\hat{\mathcal{T}}_H$ and $\hat{\mathcal{T}}_A$ as the ground-truth behavioral profiles for each class. We then ask a direct methodological question: are the kernels separable enough to justify downstream pricing control that depends on that separability?
|
|
||||||
|
|
||||||
To answer this, we compute average KL divergence between transition probability matrices. This statistic gives global separability and event-level diagnostics at the same time. In our balanced dataset (50\% human, 50\% agent), the average divergence is approximately $1.8$. To contextualize this divergence metric we compare with an intra-class comparison baseline of randomly selected transitions.
|
\subsubsection{GOFAI-Based Separability}
|
||||||
% To contextualize this figure a useful intra-class baseline is to randomly split D_H into two equal halves, estimate a kernel from each half, compute the same average KL statistic, and repeat for B bootstrap samples (e.g. B=100). The resulting null distribution (mean +/- std) gives the divergence expected purely from estimation noise at this sample size. A between-class KL substantially above this null confirms the separation is real and not a finite-sample artefact. In practice: for each of B splits, partition D_H 50/50 without replacement, run build_kernel() on each half, average the per-state KL values, and collect the B scores into a reference distribution to compare against the 1.8 figure.
|
We employ Good Old-Fashioned AI (GOFAI) heuristics to generate initial weak labels for separability. We define a set of rule-based predicates $\phi_j: \tau \to \{0, 1\}$ to partition the dataset $\mathcal{D}$ into high-confidence sets $\mathcal{D}_H$ and $\mathcal{D}_A$. We construct distinct MDPs per each behavioral profile of humans and agents and from those we establish $D_{KL}$. From initial findings we compute a KL divergence of $\approx 2.0236$ across transition probabilities between states which can be seen in \ref{fig:human_mdp_viz} and \ref{fig:agent_mdp_viz}.
|
||||||
|
|
||||||
\begin{definition}[Kullback-Leibler Divergence for Transition Distributions]
|
|
||||||
Let $P_e$ and $Q_e$ be categorical distributions over destination states following event $e$, derived from human and agent trajectories respectively. The KL divergence between these distributions is:
|
|
||||||
\begin{equation}
|
|
||||||
D_{\mathrm{KL}}(P_e \parallel Q_e) = \sum_{k \in \mathcal{S}_e} P_e(k) \log \frac{P_e(k)}{Q_e(k)}
|
|
||||||
\end{equation}
|
|
||||||
where $\mathcal{S}_e$ denotes the set of destination events that follow $e$ in the human trajectories.
|
|
||||||
\end{definition}
|
|
||||||
|
|
||||||
To obtain this statistic, we aggregate transitions by triggering event $e$ and treat normalized outgoing probabilities as categorical distributions $P_e$ (human) and $Q_e$ (agent). We intersect shared event labels, then accumulate log-ratio contributions over shared destinations. Large contributions, including near-zero $Q_e(k)$ cases, identify transitions where one actor class is difficult to mimic.
|
|
||||||
|
|
||||||
With these divergence features we train a contrastive model to estimate a weak agent probability $f(\tau)\in[0,1]$, which we later use as a weighting and control signal.
|
|
||||||
|
|
||||||
|
|
||||||
\subsubsection{Transition Probability Estimation}
|
|
||||||
\label{sec:tpe}
|
|
||||||
|
|
||||||
|
|
||||||
For both subsets, we model session dynamics as an MDP and estimate transition kernel $\mathcal{T}$. For each actor type we estimate global kernels $\hat{\mathcal{T}}_A$ and $\hat{\mathcal{T}}_H$, then cluster into behavioral sub-kernels $\hat{\mathcal{T}}_y^i$ to avoid collapsing all behavior into one average profile. Transition probabilities are estimated by maximum likelihood:
|
|
||||||
\begin{equation}
|
|
||||||
\hat{P}(s' \mid s) = \frac{N(s, s')}{\sum_{k \in \mathcal{S}} N(s, k)}
|
|
||||||
\end{equation}
|
|
||||||
where $N(s, s')$ is the observed transition count. This allows us to construct a \textit{Contamination Generator} $\mathcal{G}(\alpha)$. Given a clean trajectory dataset, $\mathcal{G}$ injects synthetic agent trajectories sampled from $\hat{\mathcal{T}}_A$ until the effective mixing ratio reaches $\alpha$. The properties of an MDP such as ... should be preserved by the operation described below.
|
|
||||||
|
|
||||||
To scale this to catalog-level pricing, we expand the base event transition matrix from $T\times T$ into product-specific transitions using the current demand condition. In practice, we normalize the demand vector across products and use it to weight how much transition mass each product pair receives. Concretely, each cell of the base matrix becomes an $N\times N$ block (for $N$ products), so the transition matrix grows from $T\times T$ to $(T\cdot N)\times(T\cdot N)$. Finally, we add $C$ generic states (homepage, login, checkout terminal states), which gives the full kernel size $(T\cdot N + C)\times(T\cdot N + C)$.
|
|
||||||
% The validity of this demand-weighted block expansion is still subject to formal proof: it needs to be shown that the resulting matrix retains row-stochasticity (rows summing to 1) and that the weighting by the demand vector preserves the Markov property for the expanded state space. In the engine source this is the target of ongoing validation before the expansion is relied on for behavioral generation at scale.
|
|
||||||
|
|
||||||
\begin{figure}[ht]
|
\begin{figure}[ht]
|
||||||
\centering
|
\centering
|
||||||
\includegraphics[width=0.8\textwidth]{chapters/mdp_human.pdf}
|
\includegraphics[width=0.8\textwidth]{chapters/mdp_human.pdf}
|
||||||
\caption{Markov Decision Process visualization illustrating the behavioral transition dynamics for \textbf{human} actions.}
|
\caption{Markov Decision Process visualization illustrating the behavioral transition dynamics for human actions.}
|
||||||
\label{fig:human_mdp_viz}
|
\label{fig:human_mdp_viz}
|
||||||
\end{figure}
|
\end{figure}
|
||||||
|
|
||||||
@@ -340,74 +201,36 @@ To scale this to catalog-level pricing, we expand the base event transition matr
|
|||||||
\label{fig:agent_mdp_viz}
|
\label{fig:agent_mdp_viz}
|
||||||
\end{figure}
|
\end{figure}
|
||||||
|
|
||||||
|
\subsubsection{Transition Probability Estimation}
|
||||||
\subsection{Second-Stage Classification}
|
For both subsets, we model the session dynamics as a Markov Decision Process (MDP) and estimate the transition kernel $\mathcal{T}$. The probability of transitioning to state $s'$ given state $s$ is estimated via maximum likelihood:
|
||||||
After contamination, we run a second classification stage. We remap events into a semantically aligned feature space, apply richer feature engineering, and retrain to obtain cleaner label probabilities across the full dataset. This classifier is then used directly in the reinforcement-learning reward structure.
|
\begin{equation}
|
||||||
|
\hat{P}(s' \mid s) = \frac{N(s, s')}{\sum_{k \in \mathcal{S}} N(s, k)}
|
||||||
|
\end{equation}
|
||||||
|
where $N(s, s')$ is the count of observed transitions. This allows us to construct a \textit{Contamination Generator} $\mathcal{G}(\alpha)$. Given a clean trajectory dataset, $\mathcal{G}$ injects synthetic agent trajectories sampled from the learned transition matrix $\hat{P}_A$ until the effective mixing ratio reaches $\alpha$.
|
||||||
|
|
||||||
\subsection{Distributionally Robust Reinforcement Learning (DR-RL)}
|
\subsection{Distributionally Robust Reinforcement Learning (DR-RL)}
|
||||||
|
|
||||||
We formulate pricing as a Stackelberg game: the platform (leader) sets prices $p_t$, and the population (follower) responds through trajectories and demand. A useful intuition is that the platform behaves like a distorted mirror at a 45-degree angle: what it mirrors is population demand into an estimated demand proxy, and that proxy drives revenue.
|
We formulate the pricing problem as a Stackelberg Game where the Platform (Leader) sets prices $p_t$ and the Aggregate Demand (Follower) responds. However, the exact mixing parameter $\alpha$ and the demand distribution shift are non-stationary and unknown in online settings. Relying on a simple error term $\epsilon$ is insufficient. Instead, we adopt a Distributionally Robust Optimization (DRO) objective.
|
||||||
|
|
||||||
Because contamination level $\alpha$ and demand shift are non-stationary online, a simple error term is not enough. We therefore use a Distributionally Robust Optimization objective. Let $\tau'$ be a newly observed trajectory generated by an unknown actor profile (sampled from the behavioral models in Section~\ref{sec:tpe}). We need a demand mapping conditioned on price and trajectory, $\hat{Q}(p,\tau')$. For each $\tau'$, we compute $\hat{\mathcal{T}}'$ and compare it with controlled baselines $\bar{\mathcal{T}}_H$ and $\bar{\mathcal{T}}_A$:
|
|
||||||
|
|
||||||
\begin{align}
|
|
||||||
\label{eq:delta_H}
|
|
||||||
\Delta_H &= D_{KL}(\hat{\mathcal{T}}^\prime \parallel \bar{\mathcal{T}}_H) \\
|
|
||||||
\label{eq:delta_A}
|
|
||||||
\Delta_A &= D_{KL}(\hat{\mathcal{T}}^\prime \parallel \bar{\mathcal{T}}_A)
|
|
||||||
\end{align}
|
|
||||||
|
|
||||||
This yields two centroid-like heuristics that act as a session-level agent score in the engine. On a per-customer or use-case basis a similar study should be done in order to obtain ground truth behavior models for humans and agents and their specific interaction with a given products website.
|
|
||||||
|
|
||||||
In implementation, we maintain an alternating game-history stack (our \textit{Limbo} stack) and execute it explicitly every epoch with exactly two transitions: first the platform publishes a price vector (leader move), then the market responds with trajectory-derived demand (follower move).
|
|
||||||
|
|
||||||
% Mention discretized action space and the clipping and over shotting in continuous action spaces
|
|
||||||
% Also talk about catastrophic economics, we add termination on bankrupcy or zero demand so market collaps
|
|
||||||
|
|
||||||
\subsubsection{Ambiguity Set Construction}
|
\subsubsection{Ambiguity Set Construction}
|
||||||
We define an ambiguity set $\mathcal{U}_\epsilon(\hat{P}_N)$ centered around our empirical reference distribution $\hat{P}_N$ (derived from the generator $\mathcal{G}$). We utilize the Wasserstein distance metric to define the set of plausible demand distributions the agent might face:
|
We define an ambiguity set $\mathcal{U}_p(\hat{P}_N)$ centered around our empirical reference distribution $\hat{P}_N$ (derived from the generator $\mathcal{G}$). We utilize the Wasserstein distance metric to define the set of plausible demand distributions the agent might face:
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\mathcal{U}_\epsilon(\hat{P}_N) = \left\{ Q \in \mathcal{P}(\Xi) : W_p(Q, \hat{P}_N) \le \epsilon \right\}
|
\mathcal{U}_\epsilon(\hat{P}_N) = \left\{ Q \in \mathcal{P}(\Xi) : W_p(Q, \hat{P}_N) \le \epsilon \right\}
|
||||||
\end{equation}
|
\end{equation}
|
||||||
This set captures all distributions that are statistically close to our observed training data but allows for adversarial shifts.
|
This set captures all distributions that are statistically close to our observed training data but allows for adversarial shifts (e.g., sudden bot spikes).
|
||||||
|
|
||||||
For the current engine baseline, we use a compact inner-robust approximation by applying ambiguity over contamination in a local interval around nominal contamination $\alpha_0$:
|
|
||||||
\begin{equation}
|
|
||||||
\mathcal{A}_{\epsilon_\alpha}(\alpha_0)=\left\{\alpha\in[0,1]:\lvert\alpha-\alpha_0\rvert\le\epsilon_\alpha\right\}
|
|
||||||
\end{equation}
|
|
||||||
and we evaluate a small fixed grid in $\mathcal{A}_{\epsilon_\alpha}(\alpha_0)$ per step, selecting the worst-case candidate for the learner.
|
|
||||||
% A proper Wasserstein ball implementation over the full demand distribution (rather than a scalar alpha interval) would use the POT library (Python Optimal Transport): compute W_2 between the empirical reference P_hat and each candidate Q using ot.emd2() or ot.sliced_wasserstein_distance() for scalability, then accept only candidates within epsilon. In practice the inner minimization becomes: candidates = [G(alpha) for alpha in linspace]; dists = [ot.emd2(p_hat, q, M) for q in candidates]; worst = candidates[argmin(reward[dists <= epsilon])]. The current grid-on-alpha approximation is a computationally cheap substitute; moving to a true Wasserstein ball would tighten the worst-case guarantee but requires specifying the ground metric M over the demand space.
|
|
||||||
|
|
||||||
\subsubsection{The Min-Max Objective}
|
\subsubsection{The Min-Max Objective}
|
||||||
The robust policy $\pi^*$ is obtained by solving the maximin problem:
|
The robust policy $\pi^*$ is obtained by solving the maximin problem:
|
||||||
\begin{equation}
|
\begin{equation}
|
||||||
\label{eq:robust_policy}
|
\pi^* = \arg \max_{\pi} \min_{Q \in \mathcal{U}_\epsilon} \mathbb{E}_{d \sim Q} \left[ R(p, d) - \lambda \cdot \text{COI}(p) \right]
|
||||||
\pi^* = \arg \max_{\pi} \min_{Q \in \mathcal{U}_\epsilon} \mathbb{E}_{d \sim Q} \left[ R(p, d) - \lambda \cdot \text{COI}_{\text{leak}}(p,\tau') \right]
|
|
||||||
\end{equation}
|
\end{equation}
|
||||||
where $R(p, d)$ is the revenue function and $\lambda$ weighs the information-leakage penalty.
|
where $R(p, d)$ is the revenue function and $\lambda$ weighs the penalty for information leakage (COI).
|
||||||
|
|
||||||
In practice, we parameterize this with a session-level leakage term:
|
|
||||||
\begin{equation}
|
|
||||||
\text{COI}_{\text{leak}}(p,\tau') = f(\tau')\cdot \text{InfoValue}(p,\tau')
|
|
||||||
\end{equation}
|
|
||||||
where $f(\tau')$ is the weak agent probability and $\text{InfoValue}$ is implemented either as a constant query-tax surrogate or as a revelation surrogate $-\log\pi(p\mid\tau')$.
|
|
||||||
|
|
||||||
For the baseline engine reported here, we intentionally use the constant query-tax surrogate to keep the mechanism minimal:
|
|
||||||
\begin{equation}
|
|
||||||
r_t = R(p_t,\tilde q_t) - \lambda\,f(\tau_t')\,c_{\text{info}}
|
|
||||||
\end{equation}
|
|
||||||
with fixed $c_{\text{info}}>0$.
|
|
||||||
|
|
||||||
|
|
||||||
Another possible extension is to adapt the ambiguity radius online, e.g., $\epsilon(\Delta_H)$, so the Wasserstein ball changes with live divergence. We keep this as future work and retain a fixed-radius setup because Wasserstein ambiguity already handles heavy-tail and ``black swan'' behavior without absolute continuity assumptions \parencite{kuhn_wasserstein_2024}.
|
|
||||||
|
|
||||||
\subsubsection{Actor Implementation}
|
\subsubsection{Actor Implementation}
|
||||||
In our simulation, the ``follower'' is implemented as a set of Actors. Each Actor is initialized with a type $\theta$ which samples a specific demand curve $d(p; \theta)$ from the latent distribution. This formalization ensures that our DR-RL agent does not overfit to a single deterministic demand function but learns a policy robust to the distributional uncertainty defined by $\mathcal{U}_\epsilon$.
|
In our simulation, the "Follower" is implemented as a set of Actors. Each Actor is initialized with a type $\theta$ which samples a specific demand curve $d(p; \theta)$ from the latent distribution. This formalization ensures that our DR-RL agent does not overfit to a single deterministic demand function but learns a policy robust to the distributional uncertainty defined by $\mathcal{U}_\epsilon$.
|
||||||
|
|
||||||
Practical implementation of browser agents is a strongly evolving field with near-weekly releases of SOTA architectures. In this thesis implementation we abstract that layer into trajectory generators learned from observed human/agent transition kernels.
|
|
||||||
|
|
||||||
|
|
||||||
As part of reward engineering, we keep a UX factor ($UX\in[0,1]$) as an auxiliary evaluation axis. In the current baseline it is not injected into the core reward; it is tracked separately to compare policy trade-offs.
|
As part of our reward engineering we think about the UX factor ($UX \in [0,1]$) whic his our proxy for user experience degradation, this is computed as a mixture of contribution from the separability model metric of $\frac{1}{\text{Specificity}}$.
|
||||||
|
|
||||||
\begin{figure}[ht]
|
\begin{figure}[ht]
|
||||||
\centering
|
\centering
|
||||||
@@ -417,40 +240,12 @@ As part of reward engineering, we keep a UX factor ($UX\in[0,1]$) as an auxiliar
|
|||||||
\caption{Introducing the UX index allows us to better distinguish the kind of impact different methods have and allows us to compare them on this Pareto-like scale.}
|
\caption{Introducing the UX index allows us to better distinguish the kind of impact different methods have and allows us to compare them on this Pareto-like scale.}
|
||||||
\end{figure}
|
\end{figure}
|
||||||
|
|
||||||
We also consider taxation-like overlays for agent traffic under strategy-proof mechanism design (e.g., Vickrey-Clarke-Groves style rules). This remains an extension path and is not part of the main implementation in this thesis.
|
We also need to think about a policy like taxation to the agents Strategy-Proof Mechanism Design, specifically the Vickrey-Clarke-Groves (VCG) payment rule. We link and prove that this would create an incentive for the dominant strategy to become truth-telling.
|
||||||
|
|
||||||
\subsubsection{Pricing Mechanism Summary}
|
\section{Heuristics as part of neuro-inspired steering systems}
|
||||||
|
|
||||||
We now present the complete pricing mechanism that integrates the behavioral separability, contamination estimation, and robust optimization components developed in the preceding sections. Algorithm~\ref{alg:phantom_loop_clean} formalizes the defensive pricing loop as a Stackelberg game where the platform (leader) sets prices and the aggregate demand (follower) responds through observed session trajectories.
|
Steve Burns, superior culliculus (face heuristics) we create this sort of part of the 'brain' + amortized inference.
|
||||||
|
|
||||||
\begin{algorithm}[t]
|
We could say that a DQN for example is the learnin subsystem and then within our reward mechanism or some other computational method we introduce a steering subsystem which acts as the proposed ``pricing heuristic'' against the given non human transaction data.
|
||||||
\caption{PHANTOM defensive pricing loop}
|
|
||||||
\label{alg:phantom_loop_clean}
|
|
||||||
\DontPrintSemicolon
|
|
||||||
\SetKwInput{Input}{Input}
|
|
||||||
\SetKwInput{Output}{Output}
|
|
||||||
|
|
||||||
\Input{catalog size \(N\); action scale grid \(\mathcal{S}_{act}\); nominal contamination \(\alpha_0\); ambiguity radius \(\epsilon_\alpha\); candidate count \(K\); horizon \(T\); sessions per step \(M\); behavior kernels \(\bar T_H,\bar T_A\); event weights \(\omega\); COI penalty \(\lambda\)}
|
\section{Market construction}
|
||||||
\Output{trajectory \(\{(p_t,\hat Q_t,\alpha_t^*)\}_{t=0}^{T-1}\)}
|
|
||||||
\For{\(t \leftarrow 0\) \KwTo \(T-1\)}{
|
|
||||||
observe \(o_t=[\hat Q_{t-1}, p_{t-1}]\)\;
|
|
||||||
choose discrete action \(a_t \in \{1,\dots,|\mathcal{S}_{act}|\}\) from policy \(\pi\)\;
|
|
||||||
set \(p_t \leftarrow \mathrm{clip}(p_{t-1} \cdot \mathcal{S}_{act}[a_t])\)\;
|
|
||||||
|
|
||||||
define local ambiguity interval \(\mathcal{A}_{\epsilon_\alpha}(\alpha_0)=\{\alpha:\lvert\alpha-\alpha_0\rvert\le\epsilon_\alpha\}\)\;
|
|
||||||
\For{\(k \leftarrow 1\) \KwTo \(K\)}{
|
|
||||||
set \(\alpha_k \in \mathcal{A}_{\epsilon_\alpha}(\alpha_0)\) from a uniform grid\;
|
|
||||||
sample \(M\) sessions from mixture \((1-\alpha_k)\bar T_H + \alpha_k \bar T_A\)\;
|
|
||||||
compute demand proxy \(\hat Q_t^{(k)} = \sum_{m=1}^{M}\sum_j \omega(a_{m,j})\,\mathbf{1}[i_{m,j}=i]\)\;
|
|
||||||
compute \((\Delta_H^{(k)},\Delta_A^{(k)})\) and session score \(f_t^{(k)}\) from KL divergence\;
|
|
||||||
compute candidate reward \(r_t^{(k)} = R(p_t,\hat Q_t^{(k)}) - \lambda\,f_t^{(k)}\,c_{info}\)\;
|
|
||||||
}
|
|
||||||
choose \(k^* \leftarrow \arg\min_k r_t^{(k)}\), set \(\alpha_t^* \leftarrow \alpha_{k^*}\)\;
|
|
||||||
set \(\hat Q_t \leftarrow \hat Q_t^{(k^*)}\), \(r_t \leftarrow r_t^{(k^*)}\)\;
|
|
||||||
}
|
|
||||||
\end{algorithm}
|
|
||||||
|
|
||||||
|
|
||||||
The algorithm operates in discrete epochs indexed by $t$. At each epoch, the platform applies one discrete multiplicative price action, the environment samples a batch of sessions, and demand is recomputed from weighted events. Robustness is implemented as an inner minimization over a small local grid of contamination candidates around nominal $\alpha_0$, matching the current engine implementation. The history buffer $\mathcal{L}$ (``Limbo'' in our implementation) enforces the alternating Stackelberg structure by preserving the temporal sequence of price publications and demand observations.
|
|
||||||
|
|
||||||
%The defensive price update in Line 24 implements contamination-aware margin shrinkage: as estimated contamination $\hat{\alpha}_t$ rises, the margin $(p^{\mathrm{ref}} - c)$ is reduced by factor $\kappa\in[0,1]$, with projection $\Pi_{\mathcal{P}}$ ensuring feasibility. In subsequent experiments this heuristic rule is replaced by DR-RL policy $\pi^*$ from Eq.~\ref{eq:robust_policy}.
|
|
||||||
|
|||||||
@@ -1,10 +1,4 @@
|
|||||||
\section{Results}
|
\section{Results}
|
||||||
\begin{figure}[ht]
|
|
||||||
\centering
|
|
||||||
\input{chapters/figures/supra.tex}
|
|
||||||
\caption{Evolution of price distributions over experiment steps. The heatmap illustrates the density of price offerings. This is an early baseline simulation which demonstrates supra-competitive price-setting in deep learning agents such as SAC as can be clearly seen by the high density at the highest available price.}
|
|
||||||
\label{fig:supra_heatmap}
|
|
||||||
\end{figure}
|
|
||||||
|
|
||||||
\subsection{Behavioral Analysis}
|
\subsection{Behavioral Analysis}
|
||||||
|
|
||||||
|
|||||||
@@ -1,131 +0,0 @@
|
|||||||
import pandas as pd
|
|
||||||
import json
|
|
||||||
import numpy as np
|
|
||||||
import sys
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
def process_supra(input_file, output_file):
|
|
||||||
print(f"Processing {input_file} -> {output_file}")
|
|
||||||
|
|
||||||
# Read the CSV
|
|
||||||
try:
|
|
||||||
# The CSV has a weird format: "Step","giddy-deluge-6 - distributions/prices"
|
|
||||||
# The header is on line 1.
|
|
||||||
# Let's verify the file content format first effectively.
|
|
||||||
# The previous read showed standard CSV with quoted fields.
|
|
||||||
df = pd.read_csv(input_file, quotechar='"', skipinitialspace=True)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading CSV: {e}")
|
|
||||||
return
|
|
||||||
|
|
||||||
# Prepare for re-binning
|
|
||||||
# We need a common set of bins to plot a heatmap (surface)
|
|
||||||
# First, let's collect all data to determine range
|
|
||||||
all_min = float("inf")
|
|
||||||
all_max = float("-inf")
|
|
||||||
|
|
||||||
parsed_data = []
|
|
||||||
|
|
||||||
# The column names might be dynamic, so let's rely on indices
|
|
||||||
# Column 0: Step
|
|
||||||
# Column 1: JSON blob
|
|
||||||
|
|
||||||
for index, row in df.iterrows():
|
|
||||||
try:
|
|
||||||
step = int(row.iloc[0])
|
|
||||||
json_str = row.iloc[1]
|
|
||||||
|
|
||||||
# Cleaning potential double quotes issue if pandas didn't catch it perfect
|
|
||||||
# but pandas read_csv usually handles standard CSV escaping well.
|
|
||||||
|
|
||||||
data = json.loads(json_str)
|
|
||||||
|
|
||||||
bins = np.array(data["bins"])
|
|
||||||
values = np.array(data["values"])
|
|
||||||
|
|
||||||
# Update global range
|
|
||||||
if bins.min() < all_min:
|
|
||||||
all_min = bins.min()
|
|
||||||
if bins.max() > all_max:
|
|
||||||
all_max = bins.max()
|
|
||||||
|
|
||||||
parsed_data.append({"step": step, "bins": bins, "values": values})
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Skipping row {index} due to error: {e}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
if not parsed_data:
|
|
||||||
print("No data parsed.")
|
|
||||||
return
|
|
||||||
|
|
||||||
print(f"Found {len(parsed_data)} steps. Range: {all_min} to {all_max}")
|
|
||||||
|
|
||||||
# Define common grid
|
|
||||||
# Y-axis (Price)
|
|
||||||
# Using 100 bins for resolution
|
|
||||||
y_bins_edges = np.linspace(all_min, all_max, 101)
|
|
||||||
y_bin_centers = (y_bins_edges[:-1] + y_bins_edges[1:]) / 2
|
|
||||||
|
|
||||||
# Open output file
|
|
||||||
with open(output_file, "w") as f:
|
|
||||||
# PGFPlots 3D format often prefers no header or a specific header.
|
|
||||||
# We will use named columns.
|
|
||||||
f.write("step,price,density\n")
|
|
||||||
|
|
||||||
# Sort by step to ensure correct mesh ordering
|
|
||||||
parsed_data.sort(key=lambda x: x["step"])
|
|
||||||
|
|
||||||
for item in parsed_data:
|
|
||||||
step = item["step"]
|
|
||||||
original_bins = item["bins"]
|
|
||||||
original_values = item["values"]
|
|
||||||
|
|
||||||
# Re-binning logic
|
|
||||||
current_new_hist = np.zeros(len(y_bin_centers))
|
|
||||||
|
|
||||||
for i, (new_start, new_end) in enumerate(
|
|
||||||
zip(y_bins_edges[:-1], y_bins_edges[1:])
|
|
||||||
):
|
|
||||||
val = 0.0
|
|
||||||
# This inner loop is slightly inefficient O(N*M) but N~3000, M~100 -> 300k ops, totally fine.
|
|
||||||
for j in range(len(original_values)):
|
|
||||||
b_start = original_bins[j]
|
|
||||||
# Handle cases where values array might be 1 shorter than bins (histogram edges vs centers)
|
|
||||||
# The provided JSON has "bins" array larger than "values" by 1 usually for edges.
|
|
||||||
if j + 1 >= len(original_bins):
|
|
||||||
break
|
|
||||||
|
|
||||||
b_end = original_bins[j + 1]
|
|
||||||
b_width = b_end - b_start
|
|
||||||
|
|
||||||
if b_width <= 0:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Calculate overlap
|
|
||||||
overlap_start = max(new_start, b_start)
|
|
||||||
overlap_end = min(new_end, b_end)
|
|
||||||
overlap = max(0, overlap_end - overlap_start)
|
|
||||||
|
|
||||||
if overlap > 0:
|
|
||||||
# Add proportional count
|
|
||||||
val += original_values[j] * (overlap / b_width)
|
|
||||||
|
|
||||||
current_new_hist[i] = val
|
|
||||||
|
|
||||||
# Write row to file for this step
|
|
||||||
for price, density in zip(y_bin_centers, current_new_hist):
|
|
||||||
# PGFPlots expects x y z
|
|
||||||
f.write(f"{step},{price},{density}\n")
|
|
||||||
|
|
||||||
# Add a blank line for PGFPlots matrix format (essential for 'mesh' or 'surf')
|
|
||||||
f.write("\n")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# Resolve relative paths relative to where script is run, or use absolute
|
|
||||||
base_dir = os.path.dirname(os.path.abspath(__file__))
|
|
||||||
input_path = os.path.join(base_dir, "supra.csv")
|
|
||||||
output_path = os.path.join(base_dir, "supra_data.csv")
|
|
||||||
|
|
||||||
process_supra(input_path, output_path)
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
"Step","giddy-deluge-6 - distributions/prices"
|
|
||||||
"100","{""values"":[2,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1],""_type"":""histogram"",""bins"":[15.76888656616211,17.813893377780914,19.85890018939972,21.903907001018524,23.94891381263733,25.993920624256134,28.03892743587494,30.083934247493744,32.12894105911255,34.173947870731354,36.21895468235016,38.263961493968964,40.30896830558777,42.35397511720657,44.39898192882538,46.44398874044418,48.48899555206299,50.53400236368179,52.5790091753006,54.6240159869194,56.66902279853821,58.71402961015701,60.75903642177582,62.80404323339462,64.84905004501343,66.89405685663223,68.93906366825104,70.98407047986984,73.02907729148865,75.07408410310745,77.11909091472626,79.16409772634506,81.20910453796387,83.25411134958267,85.29911816120148,87.34412497282028,89.38913178443909,91.43413859605789,93.4791454076767,95.5241522192955,97.5691590309143,99.61416584253311,101.65917265415192,103.70417946577072,105.74918627738953,107.79419308900833,109.83919990062714,111.88420671224594,113.92921352386475,115.97422033548355,118.01922714710236,120.06423395872116,122.10924077033997,124.15424758195877,126.19925439357758,128.24426120519638,130.28926801681519,132.334274828434,134.3792816400528,136.4242884516716,138.4692952632904,140.5143020749092,142.55930888652802,144.60431569814682,146.64932250976562]}"
|
|
||||||
"200","{""_type"":""histogram"",""values"":[1,1,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,3],""bins"":[10.439504623413086,12.620137363672256,14.800770103931427,16.981402844190598,19.162035584449768,21.34266832470894,23.52330106496811,25.70393380522728,27.88456654548645,30.06519928574562,32.24583202600479,34.42646476626396,36.60709750652313,38.7877302467823,40.96836298704147,43.148995727300644,45.329628467559814,47.510261207818985,49.690893948078156,51.871526688337326,54.0521594285965,56.23279216885567,58.41342490911484,60.59405764937401,62.77469038963318,64.95532312989235,67.13595587015152,69.31658861041069,71.49722135066986,73.67785409092903,75.8584868311882,78.03911957144737,80.21975231170654,82.40038505196571,84.58101779222488,86.76165053248405,88.94228327274323,91.1229160130024,93.30354875326157,95.48418149352074,97.66481423377991,99.84544697403908,102.02607971429825,104.20671245455742,106.38734519481659,108.56797793507576,110.74861067533493,112.9292434155941,115.10987615585327,117.29050889611244,119.47114163637161,121.65177437663078,123.83240711688995,126.01303985714912,128.1936725974083,130.37430533766747,132.55493807792664,134.7355708181858,136.91620355844498,139.09683629870415,141.27746903896332,143.4581017792225,145.63873451948166,147.81936725974083,150]}"
|
|
||||||
"300","{""bins"":[92.91828918457031,93.81018829345703,94.70209503173828,95.593994140625,96.48589324951172,97.37779998779297,98.26969909667969,99.1615982055664,100.05350494384766,100.94540405273438,101.83731079101562,102.72920989990234,103.62110900878906,104.51301574707031,105.40491485595703,106.29681396484375,107.188720703125,108.08061981201172,108.97251892089844,109.86442565917969,110.7563247680664,111.64822387695312,112.54013061523438,113.4320297241211,114.32392883300781,115.21583557128906,116.10773468017578,116.9996337890625,117.89154052734375,118.78343963623047,119.67533874511719,120.56724548339844,121.45914459228516,122.35104370117188,123.24295043945312,124.13484954833984,125.02674865722656,125.91865539550781,126.81055450439453,127.70245361328125,128.5943603515625,129.48626708984375,130.37815856933594,131.2700653076172,132.16195678710938,133.05386352539062,133.94577026367188,134.83767700195312,135.7295684814453,136.62147521972656,137.51336669921875,138.4052734375,139.29718017578125,140.1890869140625,141.0809783935547,141.97288513183594,142.86477661132812,143.75668334960938,144.64859008789062,145.54049682617188,146.43238830566406,147.3242950439453,148.21620178222656,149.10809326171875,150],""_type"":""histogram"",""values"":[1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1,1,0,3]}"
|
|
||||||
"400","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,3,0,0,0,3],""bins"":[141.8555450439453,141.98280334472656,142.1100616455078,142.23731994628906,142.3645782470703,142.49183654785156,142.6190948486328,142.746337890625,142.87359619140625,143.0008544921875,143.12811279296875,143.25537109375,143.38262939453125,143.5098876953125,143.63714599609375,143.764404296875,143.89166259765625,144.0189208984375,144.14617919921875,144.2734375,144.4006805419922,144.52793884277344,144.6551971435547,144.78245544433594,144.9097137451172,145.03697204589844,145.1642303466797,145.29148864746094,145.4187469482422,145.54600524902344,145.6732635498047,145.80052185058594,145.92776489257812,146.05502319335938,146.18228149414062,146.30953979492188,146.43679809570312,146.56405639648438,146.69131469726562,146.81857299804688,146.94583129882812,147.07308959960938,147.20034790039062,147.32760620117188,147.45486450195312,147.58212280273438,147.70936584472656,147.8366241455078,147.96388244628906,148.0911407470703,148.21839904785156,148.3456573486328,148.47291564941406,148.6001739501953,148.72743225097656,148.8546905517578,148.98194885253906,149.1092071533203,149.2364501953125,149.36370849609375,149.490966796875,149.61822509765625,149.7454833984375,149.87274169921875,150],""_type"":""histogram""}"
|
|
||||||
"500","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,3],""bins"":[142.30267333984375,142.42294311523438,142.543212890625,142.66348266601562,142.78375244140625,142.90402221679688,143.0242919921875,143.14456176757812,143.26483154296875,143.38511657714844,143.50538635253906,143.6256561279297,143.7459259033203,143.86619567871094,143.98646545410156,144.1067352294922,144.2270050048828,144.34727478027344,144.46754455566406,144.5878143310547,144.7080841064453,144.82835388183594,144.94862365722656,145.0688934326172,145.18917846679688,145.3094482421875,145.42971801757812,145.54998779296875,145.67025756835938,145.79052734375,145.91079711914062,146.03106689453125,146.15133666992188,146.2716064453125,146.39187622070312,146.51214599609375,146.63241577148438,146.752685546875,146.87295532226562,146.99322509765625,147.11349487304688,147.23377990722656,147.3540496826172,147.4743194580078,147.59458923339844,147.71485900878906,147.8351287841797,147.9553985595703,148.07566833496094,148.19593811035156,148.3162078857422,148.4364776611328,148.55674743652344,148.67701721191406,148.7972869873047,148.9175567626953,149.037841796875,149.15811157226562,149.27838134765625,149.39865112304688,149.5189208984375,149.63919067382812,149.75946044921875,149.87973022460938,150]}"
|
|
||||||
"600","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,1,0,1,0,0,0,0,3],""bins"":[143.02142333984375,143.13046264648438,143.239501953125,143.34854125976562,143.45758056640625,143.56661987304688,143.6756591796875,143.78469848632812,143.89373779296875,144.00279235839844,144.11183166503906,144.2208709716797,144.3299102783203,144.43894958496094,144.54798889160156,144.6570281982422,144.7660675048828,144.87510681152344,144.98414611816406,145.0931854248047,145.2022247314453,145.31126403808594,145.42030334472656,145.5293426513672,145.63839721679688,145.7474365234375,145.85647583007812,145.96551513671875,146.07455444335938,146.18359375,146.29263305664062,146.40167236328125,146.51071166992188,146.6197509765625,146.72879028320312,146.83782958984375,146.94686889648438,147.055908203125,147.16494750976562,147.27398681640625,147.38302612304688,147.49208068847656,147.6011199951172,147.7101593017578,147.81919860839844,147.92823791503906,148.0372772216797,148.1463165283203,148.25535583496094,148.36439514160156,148.4734344482422,148.5824737548828,148.69151306152344,148.80055236816406,148.9095916748047,149.0186309814453,149.127685546875,149.23672485351562,149.34576416015625,149.45480346679688,149.5638427734375,149.67288208007812,149.78192138671875,149.89096069335938,150],""_type"":""histogram""}"
|
|
||||||
"700","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,3],""bins"":[147.6887969970703,147.72491455078125,147.76101684570312,147.79713439941406,147.833251953125,147.86935424804688,147.9054718017578,147.94158935546875,147.97769165039062,148.01380920410156,148.0499267578125,148.08602905273438,148.1221466064453,148.15826416015625,148.19436645507812,148.23048400878906,148.2666015625,148.30270385742188,148.3388214111328,148.37493896484375,148.41104125976562,148.44715881347656,148.4832763671875,148.51937866210938,148.5554962158203,148.59161376953125,148.62771606445312,148.66383361816406,148.699951171875,148.73605346679688,148.7721710205078,148.80828857421875,148.84439086914062,148.88050842285156,148.9166259765625,148.95274353027344,148.9888458251953,149.02496337890625,149.0610809326172,149.09718322753906,149.13330078125,149.16941833496094,149.2055206298828,149.24163818359375,149.2777557373047,149.31385803222656,149.3499755859375,149.38609313964844,149.4221954345703,149.45831298828125,149.4944305419922,149.53053283691406,149.566650390625,149.60276794433594,149.6388702392578,149.67498779296875,149.7111053466797,149.74720764160156,149.7833251953125,149.81944274902344,149.8555450439453,149.89166259765625,149.9277801513672,149.96388244628906,150]}"
|
|
||||||
"800","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,3],""bins"":[149.21865844726562,149.23086547851562,149.24307250976562,149.25527954101562,149.26748657226562,149.27969360351562,149.2919158935547,149.3041229248047,149.3163299560547,149.3285369873047,149.3407440185547,149.3529510498047,149.3651580810547,149.3773651123047,149.3895721435547,149.4017791748047,149.41400146484375,149.42620849609375,149.43841552734375,149.45062255859375,149.46282958984375,149.47503662109375,149.48724365234375,149.49945068359375,149.51165771484375,149.52386474609375,149.53607177734375,149.5482940673828,149.5605010986328,149.5727081298828,149.5849151611328,149.5971221923828,149.6093292236328,149.6215362548828,149.6337432861328,149.6459503173828,149.6581573486328,149.6703643798828,149.68258666992188,149.69479370117188,149.70700073242188,149.71920776367188,149.73141479492188,149.74362182617188,149.75582885742188,149.76803588867188,149.78024291992188,149.79244995117188,149.80465698242188,149.81687927246094,149.82908630371094,149.84129333496094,149.85350036621094,149.86570739746094,149.87791442871094,149.89012145996094,149.90232849121094,149.91453552246094,149.92674255371094,149.93896484375,149.951171875,149.96337890625,149.9755859375,149.98779296875,150]}"
|
|
||||||
"900","{""_type"":""histogram"",""values"":[1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,3],""bins"":[149.2405548095703,149.25242614746094,149.2642822265625,149.27615356445312,149.28802490234375,149.2998809814453,149.31175231933594,149.32362365722656,149.33547973632812,149.34735107421875,149.35922241210938,149.37107849121094,149.38294982910156,149.3948211669922,149.40667724609375,149.41854858398438,149.430419921875,149.44227600097656,149.4541473388672,149.4660186767578,149.47787475585938,149.48974609375,149.50161743164062,149.5134735107422,149.5253448486328,149.53721618652344,149.549072265625,149.56094360351562,149.57281494140625,149.5846710205078,149.59654235839844,149.60841369628906,149.62026977539062,149.63214111328125,149.64401245117188,149.6558837890625,149.66773986816406,149.6796112060547,149.6914825439453,149.70333862304688,149.7152099609375,149.72708129882812,149.7389373779297,149.7508087158203,149.76268005371094,149.7745361328125,149.78640747070312,149.79827880859375,149.8101348876953,149.82200622558594,149.83387756347656,149.84573364257812,149.85760498046875,149.86947631835938,149.88133239746094,149.89320373535156,149.9050750732422,149.91693115234375,149.92880249023438,149.940673828125,149.95252990722656,149.9644012451172,149.9762725830078,149.98812866210938,150]}"
|
|
||||||
"1000","{""bins"":[149.04977416992188,149.0646209716797,149.0794677734375,149.0943145751953,149.10916137695312,149.12400817871094,149.13885498046875,149.15370178222656,149.16854858398438,149.1833953857422,149.1982421875,149.2130889892578,149.22793579101562,149.24278259277344,149.25762939453125,149.27247619628906,149.28732299804688,149.30218505859375,149.31703186035156,149.33187866210938,149.3467254638672,149.361572265625,149.3764190673828,149.39126586914062,149.40611267089844,149.42095947265625,149.43580627441406,149.45065307617188,149.4654998779297,149.4803466796875,149.4951934814453,149.51004028320312,149.52488708496094,149.53973388671875,149.55458068847656,149.56942749023438,149.5842742919922,149.59912109375,149.6139678955078,149.62881469726562,149.64366149902344,149.65850830078125,149.67335510253906,149.68820190429688,149.7030487060547,149.7178955078125,149.7327423095703,149.74758911132812,149.762451171875,149.7772979736328,149.79214477539062,149.80699157714844,149.82183837890625,149.83668518066406,149.85153198242188,149.8663787841797,149.8812255859375,149.8960723876953,149.91091918945312,149.92576599121094,149.94061279296875,149.95545959472656,149.97030639648438,149.9851531982422,150],""_type"":""histogram"",""values"":[1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,3]}"
|
|
||||||
"1100","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,0,0,1,0,0,0,0,3],""bins"":[149.34432983398438,149.3545684814453,149.3648223876953,149.37506103515625,149.38531494140625,149.3955535888672,149.40579223632812,149.41604614257812,149.42628479003906,149.43653869628906,149.44677734375,149.45701599121094,149.46726989746094,149.47750854492188,149.48776245117188,149.4980010986328,149.50823974609375,149.51849365234375,149.5287322998047,149.5389862060547,149.54922485351562,149.55947875976562,149.56971740722656,149.5799560546875,149.5902099609375,149.60044860839844,149.61070251464844,149.62094116210938,149.6311798095703,149.6414337158203,149.65167236328125,149.66192626953125,149.6721649169922,149.68240356445312,149.69265747070312,149.70289611816406,149.71315002441406,149.723388671875,149.73362731933594,149.74388122558594,149.75411987304688,149.76437377929688,149.7746124267578,149.78485107421875,149.79510498046875,149.8053436279297,149.8155975341797,149.82583618164062,149.83609008789062,149.84632873535156,149.8565673828125,149.8668212890625,149.87705993652344,149.88731384277344,149.89755249023438,149.9077911376953,149.9180450439453,149.92828369140625,149.93853759765625,149.9487762451172,149.95901489257812,149.96926879882812,149.97950744628906,149.98976135253906,150]}"
|
|
||||||
"1200","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,3],""bins"":[148.93704223632812,148.95364379882812,148.9702606201172,148.9868621826172,149.00347900390625,149.02008056640625,149.0366973876953,149.0532989501953,149.06991577148438,149.08651733398438,149.10313415527344,149.11973571777344,149.1363525390625,149.1529541015625,149.16957092285156,149.18617248535156,149.20278930664062,149.21939086914062,149.23599243164062,149.2526092529297,149.2692108154297,149.28582763671875,149.30242919921875,149.3190460205078,149.3356475830078,149.35226440429688,149.36886596679688,149.38548278808594,149.40208435058594,149.418701171875,149.435302734375,149.45191955566406,149.46852111816406,149.48512268066406,149.50173950195312,149.51834106445312,149.5349578857422,149.5515594482422,149.56817626953125,149.58477783203125,149.6013946533203,149.6179962158203,149.63461303710938,149.65121459960938,149.66783142089844,149.68443298339844,149.7010498046875,149.7176513671875,149.7342529296875,149.75086975097656,149.76747131347656,149.78408813476562,149.80068969726562,149.8173065185547,149.8339080810547,149.85052490234375,149.86712646484375,149.8837432861328,149.9003448486328,149.91696166992188,149.93356323242188,149.95018005371094,149.96678161621094,149.9833984375,150],""_type"":""histogram""}"
|
|
||||||
"1300","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,3],""bins"":[149.0462646484375,149.06117248535156,149.07606506347656,149.09097290039062,149.10586547851562,149.1207733154297,149.13568115234375,149.15057373046875,149.1654815673828,149.18038940429688,149.19528198242188,149.21018981933594,149.22509765625,149.239990234375,149.25489807128906,149.26979064941406,149.28469848632812,149.2996063232422,149.3144989013672,149.32940673828125,149.34429931640625,149.3592071533203,149.37411499023438,149.38900756835938,149.40391540527344,149.4188232421875,149.4337158203125,149.44862365722656,149.46353149414062,149.47842407226562,149.4933319091797,149.5082244873047,149.52313232421875,149.5380401611328,149.5529327392578,149.56784057617188,149.58273315429688,149.59764099121094,149.612548828125,149.62744140625,149.64234924316406,149.65725708007812,149.67214965820312,149.6870574951172,149.70196533203125,149.71685791015625,149.7317657470703,149.7466583251953,149.76156616210938,149.77647399902344,149.79136657714844,149.8062744140625,149.8211669921875,149.83607482910156,149.85098266601562,149.86587524414062,149.8807830810547,149.89569091796875,149.91058349609375,149.9254913330078,149.94039916992188,149.95529174804688,149.97019958496094,149.98509216308594,150]}"
|
|
||||||
"1400","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,4],""bins"":[148.76895141601562,148.78819274902344,148.8074188232422,148.82666015625,148.84588623046875,148.86512756347656,148.88436889648438,148.90359497070312,148.92283630371094,148.9420623779297,148.9613037109375,148.9805450439453,148.99977111816406,149.01901245117188,149.03823852539062,149.05747985839844,149.07672119140625,149.095947265625,149.1151885986328,149.13441467285156,149.15365600585938,149.17288208007812,149.19212341308594,149.21136474609375,149.2305908203125,149.2498321533203,149.26905822753906,149.28829956054688,149.3075408935547,149.32676696777344,149.34600830078125,149.365234375,149.3844757080078,149.40371704101562,149.42294311523438,149.4421844482422,149.46141052246094,149.48065185546875,149.49989318847656,149.5191192626953,149.53836059570312,149.55758666992188,149.5768280029297,149.5960693359375,149.61529541015625,149.63453674316406,149.6537628173828,149.67300415039062,149.69223022460938,149.7114715576172,149.730712890625,149.74993896484375,149.76918029785156,149.7884063720703,149.80764770507812,149.82688903808594,149.8461151123047,149.8653564453125,149.88458251953125,149.90382385253906,149.92306518554688,149.94229125976562,149.96153259277344,149.9807586669922,150]}"
|
|
||||||
"1500","{""bins"":[149.55426025390625,149.56121826171875,149.5681915283203,149.5751495361328,149.58212280273438,149.58908081054688,149.59605407714844,149.60301208496094,149.6099853515625,149.616943359375,149.6239013671875,149.63087463378906,149.63783264160156,149.64480590820312,149.65176391601562,149.6587371826172,149.6656951904297,149.6726531982422,149.67962646484375,149.68658447265625,149.6935577392578,149.7005157470703,149.70748901367188,149.71444702148438,149.72140502929688,149.72837829589844,149.73533630371094,149.7423095703125,149.749267578125,149.75624084472656,149.76319885253906,149.77017211914062,149.77713012695312,149.78408813476562,149.7910614013672,149.7980194091797,149.80499267578125,149.81195068359375,149.8189239501953,149.8258819580078,149.83285522460938,149.83981323242188,149.84677124023438,149.85374450683594,149.86070251464844,149.86767578125,149.8746337890625,149.88160705566406,149.88856506347656,149.89552307128906,149.90249633789062,149.90945434570312,149.9164276123047,149.9233856201172,149.93035888671875,149.93731689453125,149.94427490234375,149.9512481689453,149.9582061767578,149.96517944335938,149.97213745117188,149.97911071777344,149.98606872558594,149.9930419921875,150],""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,3]}"
|
|
||||||
"1600","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,3],""bins"":[149.17001342773438,149.1829833984375,149.19595336914062,149.20892333984375,149.22189331054688,149.23486328125,149.24781799316406,149.2607879638672,149.2737579345703,149.28672790527344,149.29969787597656,149.3126678466797,149.3256378173828,149.33860778808594,149.35157775878906,149.3645477294922,149.37750244140625,149.39047241210938,149.4034423828125,149.41641235351562,149.42938232421875,149.44235229492188,149.455322265625,149.46829223632812,149.48126220703125,149.49423217773438,149.5072021484375,149.52015686035156,149.5331268310547,149.5460968017578,149.55906677246094,149.57203674316406,149.5850067138672,149.5979766845703,149.61094665527344,149.62391662597656,149.6368865966797,149.6498565673828,149.66281127929688,149.67578125,149.68875122070312,149.70172119140625,149.71469116210938,149.7276611328125,149.74063110351562,149.75360107421875,149.76657104492188,149.779541015625,149.79251098632812,149.8054656982422,149.8184356689453,149.83140563964844,149.84437561035156,149.8573455810547,149.8703155517578,149.88328552246094,149.89625549316406,149.9092254638672,149.9221954345703,149.93515014648438,149.9481201171875,149.96109008789062,149.97406005859375,149.98703002929688,150]}"
|
|
||||||
"1700","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,3],""bins"":[149.36141967773438,149.37139892578125,149.38137817382812,149.391357421875,149.40133666992188,149.41131591796875,149.42127990722656,149.43125915527344,149.4412384033203,149.4512176513672,149.46119689941406,149.47117614746094,149.4811553955078,149.4911346435547,149.50111389160156,149.51109313964844,149.52105712890625,149.53103637695312,149.541015625,149.55099487304688,149.56097412109375,149.57095336914062,149.5809326171875,149.59091186523438,149.60089111328125,149.61087036132812,149.620849609375,149.6308135986328,149.6407928466797,149.65077209472656,149.66075134277344,149.6707305908203,149.6807098388672,149.69068908691406,149.70066833496094,149.7106475830078,149.7206268310547,149.73060607910156,149.74057006835938,149.75054931640625,149.76052856445312,149.7705078125,149.78048706054688,149.79046630859375,149.80044555664062,149.8104248046875,149.82040405273438,149.83038330078125,149.84036254882812,149.85032653808594,149.8603057861328,149.8702850341797,149.88026428222656,149.89024353027344,149.9002227783203,149.9102020263672,149.92018127441406,149.93016052246094,149.9401397705078,149.95010375976562,149.9600830078125,149.97006225585938,149.98004150390625,149.99002075195312,150]}"
|
|
||||||
"1800","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,3],""bins"":[149.42300415039062,149.43202209472656,149.4410400390625,149.45005798339844,149.4590606689453,149.46807861328125,149.4770965576172,149.48611450195312,149.49513244628906,149.504150390625,149.51315307617188,149.5221710205078,149.53118896484375,149.5402069091797,149.54922485351562,149.55824279785156,149.5672607421875,149.57626342773438,149.5852813720703,149.59429931640625,149.6033172607422,149.61233520507812,149.62135314941406,149.63035583496094,149.63937377929688,149.6483917236328,149.65740966796875,149.6664276123047,149.67544555664062,149.6844482421875,149.69346618652344,149.70248413085938,149.7115020751953,149.72052001953125,149.7295379638672,149.73855590820312,149.74755859375,149.75657653808594,149.76559448242188,149.7746124267578,149.78363037109375,149.7926483154297,149.80165100097656,149.8106689453125,149.81968688964844,149.82870483398438,149.8377227783203,149.84674072265625,149.85574340820312,149.86476135253906,149.873779296875,149.88279724121094,149.89181518554688,149.9008331298828,149.90985107421875,149.91885375976562,149.92787170410156,149.9368896484375,149.94590759277344,149.95492553710938,149.9639434814453,149.9729461669922,149.98196411132812,149.99098205566406,150],""_type"":""histogram""}"
|
|
||||||
"1900","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,1,0,1,0,0,3],""bins"":[148.7623748779297,148.78170776367188,148.80105590820312,148.8203887939453,148.8397216796875,148.85906982421875,148.87840270996094,148.89773559570312,148.91708374023438,148.93641662597656,148.95574951171875,148.97509765625,148.9944305419922,149.01376342773438,149.03311157226562,149.0524444580078,149.07177734375,149.09112548828125,149.11045837402344,149.12979125976562,149.14913940429688,149.16847229003906,149.18780517578125,149.2071533203125,149.2264862060547,149.24581909179688,149.26516723632812,149.2845001220703,149.3038330078125,149.32318115234375,149.34251403808594,149.36184692382812,149.38119506835938,149.40052795410156,149.41986083984375,149.43919372558594,149.4585418701172,149.47787475585938,149.49720764160156,149.5165557861328,149.535888671875,149.5552215576172,149.57456970214844,149.59390258789062,149.6132354736328,149.63258361816406,149.65191650390625,149.67124938964844,149.6905975341797,149.70993041992188,149.72926330566406,149.7486114501953,149.7679443359375,149.7872772216797,149.80662536621094,149.82595825195312,149.8452911376953,149.86463928222656,149.88397216796875,149.90330505371094,149.9226531982422,149.94198608398438,149.96131896972656,149.9806671142578,150]}"
|
|
||||||
"2000","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,2,1,0,0,0,0,1,0,0,0,0,0,1,3],""bins"":[149.5408477783203,149.5480194091797,149.55519104003906,149.5623779296875,149.56954956054688,149.57672119140625,149.58389282226562,149.591064453125,149.59823608398438,149.6054229736328,149.6125946044922,149.61976623535156,149.62693786621094,149.6341094970703,149.6412811279297,149.64846801757812,149.6556396484375,149.66281127929688,149.66998291015625,149.67715454101562,149.684326171875,149.69151306152344,149.6986846923828,149.7058563232422,149.71302795410156,149.72019958496094,149.7273712158203,149.73455810546875,149.74172973632812,149.7489013671875,149.75607299804688,149.76324462890625,149.77041625976562,149.77760314941406,149.78477478027344,149.7919464111328,149.7991180419922,149.80628967285156,149.8134765625,149.82064819335938,149.82781982421875,149.83499145507812,149.8421630859375,149.84933471679688,149.8565216064453,149.8636932373047,149.87086486816406,149.87803649902344,149.8852081298828,149.8923797607422,149.89956665039062,149.90673828125,149.91390991210938,149.92108154296875,149.92825317382812,149.9354248046875,149.94261169433594,149.9497833251953,149.9569549560547,149.96412658691406,149.97129821777344,149.9784698486328,149.98565673828125,149.99282836914062,150]}"
|
|
||||||
"2100","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,3],""bins"":[149.5018310546875,149.50961303710938,149.51739501953125,149.52517700195312,149.532958984375,149.54075622558594,149.5485382080078,149.5563201904297,149.56410217285156,149.57188415527344,149.5796661376953,149.5874481201172,149.59524536132812,149.60302734375,149.61080932617188,149.61859130859375,149.62637329101562,149.6341552734375,149.64193725585938,149.64971923828125,149.65750122070312,149.66529846191406,149.67308044433594,149.6808624267578,149.6886444091797,149.69642639160156,149.70420837402344,149.7119903564453,149.71978759765625,149.72756958007812,149.7353515625,149.74313354492188,149.75091552734375,149.75869750976562,149.7664794921875,149.77426147460938,149.78204345703125,149.7898406982422,149.79762268066406,149.80540466308594,149.8131866455078,149.8209686279297,149.82875061035156,149.83653259277344,149.84432983398438,149.85211181640625,149.85989379882812,149.86767578125,149.87545776367188,149.88323974609375,149.89102172851562,149.8988037109375,149.90658569335938,149.9143829345703,149.9221649169922,149.92994689941406,149.93772888183594,149.9455108642578,149.9532928466797,149.96107482910156,149.9688720703125,149.97665405273438,149.98443603515625,149.99221801757812,150]}"
|
|
||||||
"2200","{""bins"":[149.558349609375,149.56524658203125,149.5721435546875,149.5790557861328,149.58595275878906,149.5928497314453,149.59976196289062,149.60665893554688,149.61355590820312,149.62045288085938,149.62734985351562,149.63426208496094,149.6411590576172,149.64805603027344,149.65496826171875,149.661865234375,149.66876220703125,149.6756591796875,149.68255615234375,149.68946838378906,149.6963653564453,149.70326232910156,149.71017456054688,149.71707153320312,149.72396850585938,149.73086547851562,149.73776245117188,149.7446746826172,149.75157165527344,149.7584686279297,149.765380859375,149.77227783203125,149.7791748046875,149.78607177734375,149.79296875,149.7998809814453,149.80677795410156,149.8136749267578,149.82058715820312,149.82748413085938,149.83438110351562,149.84127807617188,149.84817504882812,149.85508728027344,149.8619842529297,149.86888122558594,149.87579345703125,149.8826904296875,149.88958740234375,149.896484375,149.90338134765625,149.91029357910156,149.9171905517578,149.92408752441406,149.93099975585938,149.93789672851562,149.94479370117188,149.95169067382812,149.95858764648438,149.9654998779297,149.97239685058594,149.9792938232422,149.9862060546875,149.99310302734375,150],""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,2,1,1,0,0,0,3]}"
|
|
||||||
"2300","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,2,1,0,0,3],""bins"":[149.1126251220703,149.12649536132812,149.14035034179688,149.1542205810547,149.1680908203125,149.18194580078125,149.19581604003906,149.20968627929688,149.22354125976562,149.23741149902344,149.25128173828125,149.26513671875,149.2790069580078,149.29287719726562,149.30673217773438,149.3206024169922,149.33447265625,149.34832763671875,149.36219787597656,149.37606811523438,149.38992309570312,149.40379333496094,149.41766357421875,149.4315185546875,149.4453887939453,149.45925903320312,149.47311401367188,149.4869842529297,149.5008544921875,149.51470947265625,149.52857971191406,149.54244995117188,149.55630493164062,149.57017517089844,149.58404541015625,149.59791564941406,149.6117706298828,149.62564086914062,149.63951110839844,149.6533660888672,149.667236328125,149.6811065673828,149.69496154785156,149.70883178710938,149.7227020263672,149.73655700683594,149.75042724609375,149.76429748535156,149.7781524658203,149.79202270507812,149.80589294433594,149.8197479248047,149.8336181640625,149.8474884033203,149.86134338378906,149.87521362304688,149.8890838623047,149.90293884277344,149.91680908203125,149.93067932128906,149.9445343017578,149.95840454101562,149.97227478027344,149.9861297607422,150]}"
|
|
||||||
"2400","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,2,0,0,0,0,0,0,0,0,0,3],""bins"":[149.71124267578125,149.71575927734375,149.7202606201172,149.7247772216797,149.7292938232422,149.73379516601562,149.73831176757812,149.74282836914062,149.74734497070312,149.75184631347656,149.75636291503906,149.76087951660156,149.765380859375,149.7698974609375,149.7744140625,149.77891540527344,149.78343200683594,149.78794860839844,149.79244995117188,149.79696655273438,149.80148315429688,149.8059844970703,149.8105010986328,149.8150177001953,149.81951904296875,149.82403564453125,149.82855224609375,149.83306884765625,149.8375701904297,149.8420867919922,149.8466033935547,149.85110473632812,149.85562133789062,149.86013793945312,149.86463928222656,149.86915588378906,149.87367248535156,149.878173828125,149.8826904296875,149.88720703125,149.8917236328125,149.89622497558594,149.90074157714844,149.90525817871094,149.90975952148438,149.91427612304688,149.91879272460938,149.9232940673828,149.9278106689453,149.9323272705078,149.93682861328125,149.94134521484375,149.94586181640625,149.9503631591797,149.9548797607422,149.9593963623047,149.96389770507812,149.96841430664062,149.97293090820312,149.97744750976562,149.98194885253906,149.98646545410156,149.99098205566406,149.9954833984375,150]}"
|
|
||||||
"2500","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0,0,0,0,0,3],""bins"":[149.58355712890625,149.59005737304688,149.59657287597656,149.6030731201172,149.60958862304688,149.6160888671875,149.6226043701172,149.6291046142578,149.6356201171875,149.64212036132812,149.64862060546875,149.65513610839844,149.66163635253906,149.66815185546875,149.67465209960938,149.68116760253906,149.6876678466797,149.6941680908203,149.70068359375,149.70718383789062,149.7136993408203,149.72019958496094,149.72671508789062,149.73321533203125,149.73971557617188,149.74623107910156,149.7527313232422,149.75924682617188,149.7657470703125,149.7722625732422,149.7787628173828,149.7852783203125,149.79177856445312,149.79827880859375,149.80479431152344,149.81129455566406,149.81781005859375,149.82431030273438,149.83082580566406,149.8373260498047,149.84384155273438,149.850341796875,149.85684204101562,149.8633575439453,149.86985778808594,149.87637329101562,149.88287353515625,149.88938903808594,149.89588928222656,149.9023895263672,149.90890502929688,149.9154052734375,149.9219207763672,149.9284210205078,149.9349365234375,149.94143676757812,149.94793701171875,149.95445251464844,149.96095275878906,149.96746826171875,149.97396850585938,149.98048400878906,149.9869842529297,149.99349975585938,150],""_type"":""histogram""}"
|
|
||||||
"2600","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,3],""bins"":[149.67474365234375,149.67982482910156,149.68490600585938,149.6899871826172,149.695068359375,149.7001495361328,149.70523071289062,149.71031188964844,149.71539306640625,149.72048950195312,149.72557067871094,149.73065185546875,149.73573303222656,149.74081420898438,149.7458953857422,149.7509765625,149.7560577392578,149.76113891601562,149.76622009277344,149.77130126953125,149.77638244628906,149.78146362304688,149.7865447998047,149.7916259765625,149.79672241210938,149.8018035888672,149.806884765625,149.8119659423828,149.81704711914062,149.82212829589844,149.82720947265625,149.83229064941406,149.83737182617188,149.8424530029297,149.8475341796875,149.8526153564453,149.85769653320312,149.86277770996094,149.86785888671875,149.87294006347656,149.87802124023438,149.88311767578125,149.88819885253906,149.89328002929688,149.8983612060547,149.9034423828125,149.9085235595703,149.91360473632812,149.91868591308594,149.92376708984375,149.92884826660156,149.93392944335938,149.9390106201172,149.944091796875,149.9491729736328,149.95425415039062,149.9593505859375,149.9644317626953,149.96951293945312,149.97459411621094,149.97967529296875,149.98475646972656,149.98983764648438,149.9949188232422,150]}"
|
|
||||||
"2700","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,3],""_type"":""histogram"",""bins"":[149.65234375,149.65777587890625,149.6632080078125,149.66864013671875,149.674072265625,149.67950439453125,149.6849365234375,149.69036865234375,149.69580078125,149.70123291015625,149.7066650390625,149.71209716796875,149.717529296875,149.72296142578125,149.7283935546875,149.73382568359375,149.7392578125,149.74468994140625,149.7501220703125,149.75555419921875,149.760986328125,149.76641845703125,149.7718505859375,149.77728271484375,149.78271484375,149.78814697265625,149.7935791015625,149.79901123046875,149.804443359375,149.80987548828125,149.8153076171875,149.82073974609375,149.826171875,149.83160400390625,149.8370361328125,149.84246826171875,149.847900390625,149.85333251953125,149.8587646484375,149.86419677734375,149.86962890625,149.87506103515625,149.8804931640625,149.88592529296875,149.891357421875,149.89678955078125,149.9022216796875,149.90765380859375,149.9130859375,149.91851806640625,149.9239501953125,149.92938232421875,149.934814453125,149.94024658203125,149.9456787109375,149.95111083984375,149.95654296875,149.96197509765625,149.9674072265625,149.97283935546875,149.978271484375,149.98370361328125,149.9891357421875,149.99456787109375,150]}"
|
|
||||||
"2800","{""bins"":[149.7410430908203,149.74508666992188,149.74913024902344,149.75318908691406,149.75723266601562,149.7612762451172,149.76531982421875,149.7693634033203,149.77340698242188,149.7774658203125,149.78150939941406,149.78555297851562,149.7895965576172,149.79364013671875,149.7976837158203,149.80174255371094,149.8057861328125,149.80982971191406,149.81387329101562,149.8179168701172,149.82196044921875,149.82601928710938,149.83006286621094,149.8341064453125,149.83815002441406,149.84219360351562,149.8462371826172,149.8502960205078,149.85433959960938,149.85838317871094,149.8624267578125,149.86647033691406,149.87051391601562,149.87457275390625,149.8786163330078,149.88265991210938,149.88670349121094,149.8907470703125,149.89480590820312,149.8988494873047,149.90289306640625,149.9069366455078,149.91098022460938,149.91502380371094,149.91908264160156,149.92312622070312,149.9271697998047,149.93121337890625,149.9352569580078,149.93930053710938,149.943359375,149.94740295410156,149.95144653320312,149.9554901123047,149.95953369140625,149.9635772705078,149.96763610839844,149.9716796875,149.97572326660156,149.97976684570312,149.9838104248047,149.98785400390625,149.99191284179688,149.99595642089844,150],""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,3]}"
|
|
||||||
"2900","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,1,0,0,3],""bins"":[149.65863037109375,149.66397094726562,149.66929626464844,149.6746368408203,149.67996215820312,149.685302734375,149.6906280517578,149.6959686279297,149.7012939453125,149.70663452148438,149.71197509765625,149.71730041503906,149.72264099121094,149.72796630859375,149.73330688476562,149.73863220214844,149.7439727783203,149.7493133544922,149.754638671875,149.75997924804688,149.7653045654297,149.77064514160156,149.77597045898438,149.78131103515625,149.78665161132812,149.79197692871094,149.7973175048828,149.80264282226562,149.8079833984375,149.8133087158203,149.8186492919922,149.823974609375,149.82931518554688,149.83465576171875,149.83998107910156,149.84532165527344,149.85064697265625,149.85598754882812,149.86131286621094,149.8666534423828,149.87197875976562,149.8773193359375,149.88265991210938,149.8879852294922,149.89332580566406,149.89865112304688,149.90399169921875,149.90931701660156,149.91465759277344,149.9199981689453,149.92532348632812,149.9306640625,149.9359893798828,149.9413299560547,149.9466552734375,149.95199584960938,149.95733642578125,149.96266174316406,149.96800231933594,149.97332763671875,149.97866821289062,149.98399353027344,149.9893341064453,149.99465942382812,150]}"
|
|
||||||
"3000","{""_type"":""histogram"",""values"":[2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3],""bins"":[149.756103515625,149.75991821289062,149.76373291015625,149.7675323486328,149.77134704589844,149.77516174316406,149.77896118164062,149.78277587890625,149.78659057617188,149.7904052734375,149.79421997070312,149.7980194091797,149.8018341064453,149.80564880371094,149.8094482421875,149.81326293945312,149.81707763671875,149.82089233398438,149.82470703125,149.82850646972656,149.8323211669922,149.8361358642578,149.83993530273438,149.84375,149.84756469726562,149.85137939453125,149.85519409179688,149.85899353027344,149.86280822753906,149.8666229248047,149.87042236328125,149.87423706054688,149.8780517578125,149.88186645507812,149.88568115234375,149.8894805908203,149.89329528808594,149.89710998535156,149.90090942382812,149.90472412109375,149.90853881835938,149.912353515625,149.91616821289062,149.9199676513672,149.9237823486328,149.92759704589844,149.931396484375,149.93521118164062,149.93902587890625,149.94284057617188,149.9466552734375,149.95045471191406,149.9542694091797,149.9580841064453,149.96188354492188,149.9656982421875,149.96951293945312,149.97332763671875,149.97714233398438,149.98094177246094,149.98475646972656,149.9885711669922,149.99237060546875,149.99618530273438,150]}"
|
|
||||||
"3100","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,4],""bins"":[149.4569091796875,149.46539306640625,149.473876953125,149.48236083984375,149.4908447265625,149.4993438720703,149.50782775878906,149.5163116455078,149.52479553222656,149.5332794189453,149.54176330566406,149.5502471923828,149.55874633789062,149.56723022460938,149.57571411132812,149.58419799804688,149.59268188476562,149.60116577148438,149.60964965820312,149.61813354492188,149.62661743164062,149.63511657714844,149.6436004638672,149.65208435058594,149.6605682373047,149.66905212402344,149.6775360107422,149.68601989746094,149.69451904296875,149.7030029296875,149.71148681640625,149.719970703125,149.72845458984375,149.7369384765625,149.74542236328125,149.75390625,149.76239013671875,149.77088928222656,149.7793731689453,149.78785705566406,149.7963409423828,149.80482482910156,149.8133087158203,149.82179260253906,149.83029174804688,149.83877563476562,149.84725952148438,149.85574340820312,149.86422729492188,149.87271118164062,149.88119506835938,149.88967895507812,149.89816284179688,149.9066619873047,149.91514587402344,149.9236297607422,149.93211364746094,149.9405975341797,149.94908142089844,149.9575653076172,149.966064453125,149.97454833984375,149.9830322265625,149.99151611328125,150]}"
|
|
||||||
"3200","{""bins"":[149.8720245361328,149.8740234375,149.8760223388672,149.87802124023438,149.88002014160156,149.88201904296875,149.88401794433594,149.88601684570312,149.8880157470703,149.8900146484375,149.8920135498047,149.89402770996094,149.89602661132812,149.8980255126953,149.9000244140625,149.9020233154297,149.90402221679688,149.90602111816406,149.90802001953125,149.91001892089844,149.91201782226562,149.9140167236328,149.916015625,149.9180145263672,149.92001342773438,149.92201232910156,149.92401123046875,149.92601013183594,149.92800903320312,149.9300079345703,149.9320068359375,149.9340057373047,149.93600463867188,149.93801879882812,149.9400177001953,149.9420166015625,149.9440155029297,149.94601440429688,149.94801330566406,149.95001220703125,149.95201110839844,149.95401000976562,149.9560089111328,149.9580078125,149.9600067138672,149.96200561523438,149.96400451660156,149.96600341796875,149.96800231933594,149.97000122070312,149.9720001220703,149.9739990234375,149.9759979248047,149.97799682617188,149.98001098632812,149.9820098876953,149.9840087890625,149.9860076904297,149.98800659179688,149.99000549316406,149.99200439453125,149.99400329589844,149.99600219726562,149.9980010986328,150],""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3]}"
|
|
||||||
"3300","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,0,3],""bins"":[149.5899658203125,149.59637451171875,149.602783203125,149.60919189453125,149.6156005859375,149.6219940185547,149.62840270996094,149.6348114013672,149.64122009277344,149.6476287841797,149.65403747558594,149.6604461669922,149.66683959960938,149.67324829101562,149.67965698242188,149.68606567382812,149.69247436523438,149.69888305664062,149.70529174804688,149.71170043945312,149.71810913085938,149.72450256347656,149.7309112548828,149.73731994628906,149.7437286376953,149.75013732910156,149.7565460205078,149.76295471191406,149.76934814453125,149.7757568359375,149.78216552734375,149.78857421875,149.79498291015625,149.8013916015625,149.80780029296875,149.814208984375,149.82061767578125,149.82701110839844,149.8334197998047,149.83982849121094,149.8462371826172,149.85264587402344,149.8590545654297,149.86546325683594,149.87185668945312,149.87826538085938,149.88467407226562,149.89108276367188,149.89749145507812,149.90390014648438,149.91030883789062,149.91671752929688,149.92312622070312,149.9295196533203,149.93592834472656,149.9423370361328,149.94874572753906,149.9551544189453,149.96156311035156,149.9679718017578,149.974365234375,149.98077392578125,149.9871826171875,149.99359130859375,150]}"
|
|
||||||
"3400","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,3],""bins"":[149.78488159179688,149.78823852539062,149.79161071777344,149.7949676513672,149.79832458496094,149.8016815185547,149.8050537109375,149.80841064453125,149.811767578125,149.8151397705078,149.81849670410156,149.8218536376953,149.82521057128906,149.82858276367188,149.83193969726562,149.83529663085938,149.83865356445312,149.84202575683594,149.8453826904297,149.84873962402344,149.85211181640625,149.85546875,149.85882568359375,149.8621826171875,149.8655548095703,149.86891174316406,149.8722686767578,149.87564086914062,149.87899780273438,149.88235473632812,149.88571166992188,149.8890838623047,149.89244079589844,149.8957977294922,149.899169921875,149.90252685546875,149.9058837890625,149.90924072265625,149.91261291503906,149.9159698486328,149.91932678222656,149.92269897460938,149.92605590820312,149.92941284179688,149.93276977539062,149.93614196777344,149.9394989013672,149.94285583496094,149.94622802734375,149.9495849609375,149.95294189453125,149.956298828125,149.9596710205078,149.96302795410156,149.9663848876953,149.96974182128906,149.97311401367188,149.97647094726562,149.97982788085938,149.9832000732422,149.98655700683594,149.9899139404297,149.99327087402344,149.99664306640625,150]}"
|
|
||||||
"3500","{""bins"":[149.75067138671875,149.7545623779297,149.7584686279297,149.76235961914062,149.76625061035156,149.77015686035156,149.7740478515625,149.77793884277344,149.78182983398438,149.78573608398438,149.7896270751953,149.79351806640625,149.79742431640625,149.8013153076172,149.80520629882812,149.80911254882812,149.81300354003906,149.81689453125,149.82080078125,149.82469177246094,149.82858276367188,149.83248901367188,149.8363800048828,149.84027099609375,149.84417724609375,149.8480682373047,149.85195922851562,149.85585021972656,149.85975646972656,149.8636474609375,149.86753845214844,149.87144470214844,149.87533569335938,149.8792266845703,149.8831329345703,149.88702392578125,149.8909149169922,149.8948211669922,149.89871215820312,149.90260314941406,149.906494140625,149.910400390625,149.91429138183594,149.91818237304688,149.92208862304688,149.9259796142578,149.92987060546875,149.93377685546875,149.9376678466797,149.94155883789062,149.94546508789062,149.94935607910156,149.9532470703125,149.9571533203125,149.96104431152344,149.96493530273438,149.96884155273438,149.9727325439453,149.97662353515625,149.9805145263672,149.9844207763672,149.98831176757812,149.99220275878906,149.99610900878906,150],""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,0,0,0,0,1,0,0,0,3]}"
|
|
||||||
"3600","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,3],""bins"":[149.89866638183594,149.90025329589844,149.90184020996094,149.90341186523438,149.90499877929688,149.90658569335938,149.90817260742188,149.9097442626953,149.9113311767578,149.9129180908203,149.9145050048828,149.91607666015625,149.91766357421875,149.91925048828125,149.92083740234375,149.9224090576172,149.9239959716797,149.9255828857422,149.9271697998047,149.9287567138672,149.93032836914062,149.93191528320312,149.93350219726562,149.93508911132812,149.93666076660156,149.93824768066406,149.93983459472656,149.94142150878906,149.9429931640625,149.944580078125,149.9461669921875,149.94775390625,149.9493408203125,149.95091247558594,149.95249938964844,149.95408630371094,149.95567321777344,149.95724487304688,149.95883178710938,149.96041870117188,149.96200561523438,149.9635772705078,149.9651641845703,149.9667510986328,149.9683380126953,149.96990966796875,149.97149658203125,149.97308349609375,149.97467041015625,149.97625732421875,149.9778289794922,149.9794158935547,149.9810028076172,149.9825897216797,149.98416137695312,149.98574829101562,149.98733520507812,149.98892211914062,149.99049377441406,149.99208068847656,149.99366760253906,149.99525451660156,149.996826171875,149.9984130859375,150]}"
|
|
||||||
"3700","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,3],""bins"":[149.76803588867188,149.77166748046875,149.77528381347656,149.77891540527344,149.78253173828125,149.78616333007812,149.78977966308594,149.7934112548828,149.79702758789062,149.8006591796875,149.8042755126953,149.8079071044922,149.8115234375,149.81515502929688,149.8187713623047,149.82240295410156,149.82601928710938,149.82965087890625,149.83328247070312,149.83689880371094,149.8405303955078,149.84414672851562,149.8477783203125,149.8513946533203,149.8550262451172,149.858642578125,149.86227416992188,149.8658905029297,149.86952209472656,149.87313842773438,149.87677001953125,149.88038635253906,149.88401794433594,149.8876495361328,149.89126586914062,149.8948974609375,149.8985137939453,149.9021453857422,149.90576171875,149.90939331054688,149.9130096435547,149.91664123535156,149.92025756835938,149.92388916015625,149.92750549316406,149.93113708496094,149.93475341796875,149.93838500976562,149.9420166015625,149.9456329345703,149.9492645263672,149.952880859375,149.95651245117188,149.9601287841797,149.96376037597656,149.96737670898438,149.97100830078125,149.97462463378906,149.97825622558594,149.98187255859375,149.98550415039062,149.98912048339844,149.9927520751953,149.99636840820312,150]}"
|
|
||||||
"3800","{""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,2,0,1,0,0,1,0,0,0,3],""bins"":[149.3803253173828,149.3900146484375,149.39968872070312,149.4093780517578,149.41905212402344,149.42874145507812,149.43841552734375,149.44810485839844,149.45777893066406,149.46746826171875,149.47714233398438,149.48683166503906,149.49652099609375,149.50619506835938,149.51588439941406,149.5255584716797,149.53524780273438,149.544921875,149.5546112060547,149.5642852783203,149.573974609375,149.58364868164062,149.5933380126953,149.60302734375,149.61270141601562,149.6223907470703,149.63206481933594,149.64175415039062,149.65142822265625,149.66111755371094,149.67079162597656,149.68048095703125,149.69015502929688,149.69984436035156,149.70953369140625,149.71920776367188,149.72889709472656,149.7385711669922,149.74826049804688,149.7579345703125,149.7676239013672,149.7772979736328,149.7869873046875,149.7966766357422,149.8063507080078,149.8160400390625,149.82571411132812,149.8354034423828,149.84507751464844,149.85476684570312,149.86444091796875,149.87413024902344,149.88380432128906,149.89349365234375,149.90318298339844,149.91285705566406,149.92254638671875,149.93222045898438,149.94190979003906,149.9515838623047,149.96127319335938,149.970947265625,149.9806365966797,149.9903106689453,150],""_type"":""histogram""}"
|
|
||||||
"3900","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,3],""bins"":[149.67698669433594,149.68203735351562,149.6870880126953,149.69212341308594,149.69717407226562,149.7022247314453,149.707275390625,149.71231079101562,149.7173614501953,149.722412109375,149.7274627685547,149.7324981689453,149.737548828125,149.7425994873047,149.74765014648438,149.752685546875,149.7577362060547,149.76278686523438,149.76783752441406,149.77288818359375,149.77792358398438,149.78297424316406,149.78802490234375,149.79307556152344,149.79811096191406,149.80316162109375,149.80821228027344,149.81326293945312,149.81829833984375,149.82334899902344,149.82839965820312,149.8334503173828,149.8385009765625,149.84353637695312,149.8485870361328,149.8536376953125,149.8586883544922,149.8637237548828,149.8687744140625,149.8738250732422,149.87887573242188,149.8839111328125,149.8889617919922,149.89401245117188,149.89906311035156,149.9040985107422,149.90914916992188,149.91419982910156,149.91925048828125,149.92430114746094,149.92933654785156,149.93438720703125,149.93943786621094,149.94448852539062,149.94952392578125,149.95457458496094,149.95962524414062,149.9646759033203,149.96971130371094,149.97476196289062,149.9798126220703,149.98486328125,149.98989868164062,149.9949493408203,150]}"
|
|
||||||
"4000","{""_type"":""histogram"",""values"":[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,3],""bins"":[149.80003356933594,149.80316162109375,149.80628967285156,149.8094024658203,149.81253051757812,149.81565856933594,149.81878662109375,149.8218994140625,149.8250274658203,149.82815551757812,149.83128356933594,149.8343963623047,149.8375244140625,149.8406524658203,149.84378051757812,149.84689331054688,149.8500213623047,149.8531494140625,149.8562774658203,149.85940551757812,149.86251831054688,149.8656463623047,149.8687744140625,149.8719024658203,149.87501525878906,149.87814331054688,149.8812713623047,149.8843994140625,149.88751220703125,149.89064025878906,149.89376831054688,149.8968963623047,149.9000244140625,149.90313720703125,149.90626525878906,149.90939331054688,149.9125213623047,149.91563415527344,149.91876220703125,149.92189025878906,149.92501831054688,149.92813110351562,149.93125915527344,149.93438720703125,149.93751525878906,149.9406280517578,149.94375610351562,149.94688415527344,149.95001220703125,149.95314025878906,149.9562530517578,149.95938110351562,149.96250915527344,149.96563720703125,149.96875,149.9718780517578,149.97500610351562,149.97813415527344,149.9812469482422,149.984375,149.9875030517578,149.99063110351562,149.99374389648438,149.9968719482422,150]}"
|
|
||||||
|
@@ -1,27 +0,0 @@
|
|||||||
\begin{tikzpicture}
|
|
||||||
\begin{axis}[
|
|
||||||
view={0}{90}, % Top-down view for heatmap
|
|
||||||
xlabel={Step},
|
|
||||||
ylabel={Price},
|
|
||||||
ymin=90,
|
|
||||||
colorbar,
|
|
||||||
colorbar style={
|
|
||||||
title={Density},
|
|
||||||
ylabel={},
|
|
||||||
},
|
|
||||||
colormap/viridis,
|
|
||||||
% Adjust these axis limits if necessary based on data
|
|
||||||
enlargelimits=false,
|
|
||||||
axis on top,
|
|
||||||
width=0.9\columnwidth,
|
|
||||||
height=0.5\columnwidth,
|
|
||||||
]
|
|
||||||
|
|
||||||
\addplot3[
|
|
||||||
surf,
|
|
||||||
shader=flat,
|
|
||||||
mesh/check=false % Disable check to rely on empty lines
|
|
||||||
] table [col sep=comma, x=step, y=price, z=density] {chapters/figures/supra_data.csv};
|
|
||||||
|
|
||||||
\end{axis}
|
|
||||||
\end{tikzpicture}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -49,11 +49,11 @@
|
|||||||
\node[greenbox, minimum width=3.5cm] (commerce) at (-3.5, 2) {Commerce Experiment};
|
\node[greenbox, minimum width=3.5cm] (commerce) at (-3.5, 2) {Commerce Experiment};
|
||||||
\node[greenbox, minimum width=1.5cm] (raw) at (-6.5, 0) {Raw\\Logs};
|
\node[greenbox, minimum width=1.5cm] (raw) at (-6.5, 0) {Raw\\Logs};
|
||||||
\node[greenbox, minimum width=1.5cm] (features) at (-4, -2.5) {Features};
|
\node[greenbox, minimum width=1.5cm] (features) at (-4, -2.5) {Features};
|
||||||
\node[greenbox, minimum width=2.5cm] (classification) at (-0.8, 0) {Classification\\Training A/H};
|
\node[greenbox, minimum width=2.5cm] (classification) at (-1, -0.5) {Classification\\Training A/H};
|
||||||
|
|
||||||
% Right Loop (Blue) Nodes
|
% Right Loop (Blue) Nodes
|
||||||
\node[bluebox, minimum width=2.5cm] (trainedpricing) at (3.2, 2) {Trained Pricing};
|
\node[bluebox, minimum width=2.5cm] (trainedpricing) at (3.2, 2) {Trained Pricing};
|
||||||
\node[bluebox, minimum width=1.5cm] (policy) at (6.5, 0) {Trained\\Pricing\\Policy};
|
\node[bluebox, minimum width=2.5cm] (policy) at (6.5, 0) {Trained Pricing\\Policy};
|
||||||
\node[bluebox, minimum width=2.5cm] (rlgym) at (3.2, -2.2) {RL Gym\\Training};
|
\node[bluebox, minimum width=2.5cm] (rlgym) at (3.2, -2.2) {RL Gym\\Training};
|
||||||
|
|
||||||
% --- Background Dashed Loops ---
|
% --- Background Dashed Loops ---
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
|
|
||||||
\section{Problem Formulation: A Stackelberg Game Approach}
|
|
||||||
\label{sec:math_formulation}
|
|
||||||
|
|
||||||
We formalize the interaction between the dynamic pricing system and non-human actors as a \textit{Stackelberg Game} (Leader-Follower) with incomplete information. This framework captures the hierarchical nature of the problem: the Platform (Leader) sets a pricing policy, and the Actors (Followers)---both Humans and Agents---observe these prices and react strategically.
|
|
||||||
|
|
||||||
\subsection{The Players and Objectives}
|
|
||||||
|
|
||||||
Let $t \in \{1, \dots, T\}$ denote discrete time steps. At each step, the system interactions are defined by the following entities:
|
|
||||||
|
|
||||||
\paragraph{1. The Leader (The Platform)}
|
|
||||||
The e-commerce platform acts as the leader, choosing a pricing policy $\pi$ to maximize total expected revenue. At time $t$, given a state $s_t \in \mathcal{S}$ (representing inventory, time of day, and historical interactions), the platform sets a price $p_t \in [p_{\min}, p_{\max}]$.
|
|
||||||
|
|
||||||
The platform's goal is to maximize the cumulative revenue from genuine human transactions while mitigating the distortion caused by agent interactions.
|
|
||||||
|
|
||||||
\paragraph{2. The Followers (The Demand Mixture)}
|
|
||||||
The observed demand is not a monolithic signal but a mixture of two distinct populations with divergent objective functions. Let $u$ denote an incoming actor. The type of the actor $\theta \in \{H, A\}$ is a latent variable, where $H$ denotes a Human and $A$ denotes an Agent.
|
|
||||||
|
|
||||||
\begin{itemize}
|
|
||||||
\item \textbf{The Human ($H$):} Acts as a \textit{myopic utility maximizer}. A human $i$ has a private valuation $v_i$ for the product. They execute a purchase decision $d_i \in \{0, 1\}$ based on the consumer surplus:
|
|
||||||
\begin{equation}
|
|
||||||
d_i(p_t) = \mathbb{I}(v_i - p_t \geq 0)
|
|
||||||
\end{equation}
|
|
||||||
where $\mathbb{I}(\cdot)$ is the indicator function. The aggregate human demand $q_H(p_t)$ follows a standard downward-sloping demand curve $D(p_t)$.
|
|
||||||
|
|
||||||
\item \textbf{The Agent ($A$):} Acts as an \textit{information maximizer} (reconnaissance). The agent does not intend to purchase at the displayed price $p_t$ unless an arbitrage condition is met. Instead, the agent generates interaction events (queries) to estimate the platform's pricing function $f(p)$. The agent's reward function $R_A$ is defined by Information Gain:
|
|
||||||
\begin{equation}
|
|
||||||
R_A(p_t) = H(\mathcal{P}) - H(\mathcal{P} \mid p_t) - c_{query}
|
|
||||||
\end{equation}
|
|
||||||
where $H(\mathcal{P})$ is the entropy of the agent's belief regarding the price distribution, and $c_{query}$ is the marginal cost of interaction (assumed $\approx 0$ for LLMs).
|
|
||||||
\end{itemize}
|
|
||||||
|
|
||||||
\subsection{The Demand Contamination Model}
|
|
||||||
|
|
||||||
% MAYBE alpha has to be \lambda which we also need to formally define still
|
|
||||||
|
|
||||||
The core difficulty in this setting is that the platform observes only the aggregate interaction volume $\hat{q}_t$, which is a contaminated signal. Let $\alpha_t \in [0, 1]$ represent the proportion of traffic generated by agents at time $t$. The observed signal is:
|
|
||||||
|
|
||||||
\begin{equation}
|
|
||||||
\hat{q}_t(p_t) = (1 - \alpha_t) \cdot q_H(p_t) + \alpha_t \cdot q_A(p_t) + \epsilon_t
|
|
||||||
\end{equation}
|
|
||||||
|
|
||||||
where:
|
|
||||||
\begin{itemize}
|
|
||||||
\item $q_H(p_t)$ is the \textit{true signal} (conversion intent).
|
|
||||||
\item $q_A(p_t)$ is the \textit{adversarial noise} (reconnaissance queries).
|
|
||||||
\item $\epsilon_t$ is random market noise.
|
|
||||||
\end{itemize}
|
|
||||||
|
|
||||||
Crucially, $q_A(p_t)$ is often inversely correlated with $q_H(p_t)$ in terms of utility; agents may flood the system with queries during high-volatility periods to map price boundaries, artificially inflating $\hat{q}_t$ without converting.
|
|
||||||
|
|
||||||
\subsection{The Optimization Objective: Robust Revenue}
|
|
||||||
|
|
||||||
Standard dynamic pricing algorithms (e.g., Thompson Sampling or UCB) assume $\alpha_t = 0$, estimating demand $\hat{D}(p) \approx \mathbb{E}[\hat{q} | p]$. In the presence of agents ($\alpha_t > 0$), this estimator becomes biased, leading to the \textit{Cost of Information} (COI) defined in Section 3.2.
|
|
||||||
|
|
||||||
We propose a robust optimization objective. The platform seeks a pricing policy $\pi^*$ that maximizes worst-case revenue over a statistically plausible set of contamination rates $\alpha$:
|
|
||||||
|
|
||||||
\begin{equation}
|
|
||||||
\pi^* = \argmax_{\pi} \sum_{t=1}^T \mathbb{E}_{s_t} \left[ \min_{\alpha} \left( p_t \cdot \hat{q}_t(p_t | \theta=H) \right) - \lambda \cdot \mathcal{L}_{detect}(\hat{q}_t) \right]
|
|
||||||
\end{equation}
|
|
||||||
|
|
||||||
Here:
|
|
||||||
\begin{itemize}
|
|
||||||
\item The first term, $p_t \cdot \hat{q}_t(p_t | \theta=H)$, represents the revenue generated strictly from the estimated human segment.
|
|
||||||
\item $\mathcal{L}_{detect}$ is a penalty term for failing to separate distributions (the cost of confusion).
|
|
||||||
\item $\lambda$ is a hyperparameter balancing revenue exploitation vs. robust detection.
|
|
||||||
\end{itemize}
|
|
||||||
|
|
||||||
This formulation effectively transforms the pricing problem into a \textit{Distributionally Robust Optimization (DRO)} problem, where the learner must guard against adversarial perturbations (Agent traffic) in the observed demand distribution.
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 64 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 84 KiB |
@@ -1,24 +0,0 @@
|
|||||||
from PIL import Image, ImageDraw, ImageFont
|
|
||||||
|
|
||||||
text = open("banner.txt", "r", encoding="utf-8").read()
|
|
||||||
|
|
||||||
scale = 4 # 2–6 is typical
|
|
||||||
pad = 10
|
|
||||||
font_px = 18
|
|
||||||
|
|
||||||
font = ImageFont.truetype("DejaVuSansMono.ttf", font_px * scale)
|
|
||||||
|
|
||||||
# Measure at high res
|
|
||||||
dummy = Image.new("RGB", (1, 1), "white")
|
|
||||||
d = ImageDraw.Draw(dummy)
|
|
||||||
bbox = d.multiline_textbbox((0, 0), text, font=font)
|
|
||||||
w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
|
|
||||||
|
|
||||||
# Render at high res
|
|
||||||
hi = Image.new("RGB", (w + 2*pad*scale, h + 2*pad*scale), "white")
|
|
||||||
d = ImageDraw.Draw(hi)
|
|
||||||
d.multiline_text((pad*scale, pad*scale), text, font=font, fill="black")
|
|
||||||
|
|
||||||
# Downscale with a good filter
|
|
||||||
out = hi.resize((hi.width // scale, hi.height // scale), resample=Image.Resampling.LANCZOS)
|
|
||||||
out.save("banner.png", dpi=(300, 300))
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
Actors Trajectories
|
|
||||||
■════■ interact ┌────────────┐ ┌──┐
|
|
||||||
║Agent──────┬──────▻Web Platform├──┐ │τ1│ ┌▻Q (demand estimate)─┐
|
|
||||||
║Human──────┘ └──────△─────┘ └──▻..│──┘ │
|
|
||||||
╚════■ │ │τK│ │
|
|
||||||
△ │ └──┘ │
|
|
||||||
│motivate │ │
|
|
||||||
└────────┐ │Setting ┌──┐ Pricing Engine │
|
|
||||||
▲ ┌──┐│ │Prices │p1│ ┌──────────────┐ │
|
|
||||||
│ ┌─┘ ││ └───────────┤..│◅────│▒▒▒▒▒▒▒▒▒▒▒▒▒▒│◅──┘
|
|
||||||
│ │ └──┐ │pN│ └─────┬──┬─────┘
|
|
||||||
│ ┌─┘ │ └──┘ │ │
|
|
||||||
└─┴─────────┴─▶ │ │
|
|
||||||
Private Valuations │ │
|
|
||||||
│ │
|
|
||||||
╔═══════════════════════════════════════════════════╧══╧════════╗
|
|
||||||
║ Training Loop / SAC PPO DQN A2C ║
|
|
||||||
║ ■═════════════════════════════■ ║
|
|
||||||
║ Q̂_t,i = Σ_s Σ_k ω(a_s,k) · 1[i_s,k = i] │ ║
|
|
||||||
║ f(τ') from KL( T' || T_H ) and KL( T' || T_A ) │ ║
|
|
||||||
║ α* = argmin_{α ∈ Aε(α0)} [ Revenue(p, Q^α) - λ·COI_leak ] │ ║
|
|
||||||
║ r_t = Revenue - λ·f(τ') | a* ▽ ║
|
|
||||||
╚═══════════════════════════════════════════════════════════════╝
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 19 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 8.3 KiB |
@@ -1,35 +1,30 @@
|
|||||||
% -*- TeX-master: t -*-
|
% -*- TeX-master: t -*-
|
||||||
\documentclass[12pt,letterpaper]{article}
|
\documentclass[12pt,letterpaper]{article}
|
||||||
|
|
||||||
|
\pagestyle{plain}
|
||||||
|
|
||||||
\input{preamble}
|
\input{preamble}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
\begin{titlepage}
|
\title{Adversarially Distributionally Robust Optimization and Reinforcement Learning for Informed Dynamic Pricing under Strategic Demand Contamination}
|
||||||
\centering
|
|
||||||
\includegraphics[width=\textwidth]{graphics/banner.png}\\[0.8cm]
|
\author{
|
||||||
\LARGE\textbf{PHANTOM: Pricing Heuristics Against Non-human Transaction Orchestration Mechanisms}\\[0.5cm]
|
Daniel Rösel\thanks{Primary author and student researcher. Email: daniel@alves.world} \\
|
||||||
\Large\textbf{Daniel Rösel}\\
|
IE University, Madrid, Spain \\[1em]
|
||||||
\large\textit{Bachelor of Computer Science \& Artificial Intelligence}\\[0.5cm]
|
Alberto Martín Izquierdo\thanks{Thesis advisor. Email: amartini@faculty.ie.edu} \\
|
||||||
\Large\textit{Supervised by:}\\
|
IE University, Madrid, Spain
|
||||||
\Large\textbf{Alberto Martín Izquierdo}\\
|
}
|
||||||
\large\textit{IE University, Madrid, Spain}\\[1cm]
|
|
||||||
\large\today
|
\date{\today}
|
||||||
\end{titlepage}
|
|
||||||
|
\maketitle
|
||||||
|
|
||||||
\begin{abstract}
|
\begin{abstract}
|
||||||
With accelerated growth of Lager Language Model agents in e-commerce a novel adversarial dynamic to digital markets emerges. This paper address the vulnerability of dynamic pricing systems to AI intermediaries that decouple the information gather stages from the transaction execution. By conducing reconnaissance isolates sessions, agents circumvent the ``Cost of Information'' (COI) defined as the accumulated price premium typically thought demand expression estimators.
|
The primary objective of this thesis is to develop and validate pricing heuristics that protect e-commerce platforms from systematic exploitation by Large Language Model (LLM) agents within dynamic pricing environments. As AI agents increasingly mediate consumer transactions, they enable users to circumvent the Cost of Information (the price premium accumulated through demand signal expression) by conducting reconnaissance in isolated sessions before executing purchases through clean sessions at base prices. This research will make an anticipatory contribution by adapting recommendation system methodologies to distinguish between genuine human browsing behavior and agent-orchestrated information gathering, thereby enabling pricing systems to maintain margin integrity without degrading the user experience for legitimate customers or getting rid of leads generated by LLMs.
|
||||||
We formally define this phenomenon and derive the Cost of Information Theorem, proving that as the saturation of independent, utility-maximizing agents increases, the platform’s ability to sustain a COI converges to zero, rendering standard dynamic pricing mechanisms incentive-incompatible.
|
|
||||||
To respond to this threat we propose a defensive framework which integrates behavioral economics with Adversarially Distributionally Robust Optimization (DRO). We introduce a custom e-commerce research platform built on hybrid Kappa-Lambda architecture, designed to capture and simulate high-fidelity controlled interaction trajectories. We further demonstrate through modeling that human and agent behaviors exhibit distinct transition probability kernels, enabling the construction of discriminative models based on Kullback-Leibler divergence.
|
|
||||||
These behavioral signals serve as inputs for a Distributionally Robust Reinforcement Learning (DR-RL) agent. We formulate the pricing problem as a Stackelberg game where the learner optimizes against an ambiguity set of demand distributions defined by the Wasserstein distance. This approach allows the pricing policy to remain robust against non-stationary contamination without overfitting to deterministic demand curves. The research validates a mechanism for preserving margin integrity and market equilibrium in an agent-mediated economy, while minimizing degradation to the legitimate human user experience (UX).
|
|
||||||
\end{abstract}
|
\end{abstract}
|
||||||
|
|
||||||
\noindent\textbf{Keywords:} Dynamic Pricing, LLM Agents, Adversarial Machine Learning, E-commerce, Behavioral Detection, Reinforcement Learning
|
|
||||||
|
|
||||||
\vspace{1em}
|
|
||||||
\noindent\textbf{Acknowledgments:} This research was supported by the TPU Research Cloud program, which provided access to Google Cloud TPU accelerators (including TPU v4, v5e, and v6e).
|
|
||||||
|
|
||||||
\clearpage
|
|
||||||
\input{chapters/01-intro}
|
\input{chapters/01-intro}
|
||||||
\input{chapters/02-literature-review}
|
\input{chapters/02-literature-review}
|
||||||
\input{chapters/03-methodology}
|
\input{chapters/03-methodology}
|
||||||
@@ -37,6 +32,11 @@ These behavioral signals serve as inputs for a Distributionally Robust Reinforce
|
|||||||
\input{chapters/05-discussion}
|
\input{chapters/05-discussion}
|
||||||
\input{chapters/06-conclusion}
|
\input{chapters/06-conclusion}
|
||||||
|
|
||||||
|
|
||||||
|
\section*{Acknowledgments}
|
||||||
|
Eugene Bykovets, PhD - ETH for helping with problem formulation.
|
||||||
|
Research supported with Cloud TPUs from Google's TPU Research Cloud (TRC).
|
||||||
|
|
||||||
\printbibliography
|
\printbibliography
|
||||||
|
|
||||||
\clearpage
|
\clearpage
|
||||||
@@ -46,6 +46,6 @@ These behavioral signals serve as inputs for a Distributionally Robust Reinforce
|
|||||||
\item[Agent $A$] An actor of non-human nature, powered by an LLM.
|
\item[Agent $A$] An actor of non-human nature, powered by an LLM.
|
||||||
\item[Human $H$] An individual human with some job to be done.
|
\item[Human $H$] An individual human with some job to be done.
|
||||||
\end{description}
|
\end{description}
|
||||||
% \input{../build/concatenated_code}
|
\input{../build/concatenated_code}
|
||||||
|
|
||||||
\end{document}
|
\end{document}
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
% Encoding
|
|
||||||
\usepackage[utf8]{inputenc}
|
|
||||||
|
|
||||||
% Math packages (load before fonts to avoid conflicts)
|
% Math packages (load before fonts to avoid conflicts)
|
||||||
\usepackage{amsmath}
|
\usepackage{amsmath}
|
||||||
\usepackage{amsthm}
|
\usepackage{amsthm}
|
||||||
\usepackage{appendix}
|
|
||||||
\usepackage[inline]{enumitem}
|
|
||||||
|
|
||||||
% Define theorem environments
|
% Define theorem environments
|
||||||
\newtheorem{theorem}{Theorem}
|
\newtheorem{theorem}{Theorem}
|
||||||
@@ -29,14 +24,11 @@
|
|||||||
\usepackage{subcaption}
|
\usepackage{subcaption}
|
||||||
\usepackage{siunitx}
|
\usepackage{siunitx}
|
||||||
\usepackage{tikz}
|
\usepackage{tikz}
|
||||||
\usepackage{pgfplots}
|
|
||||||
\pgfplotsset{compat=1.18}
|
|
||||||
\usepackage{listings}
|
\usepackage{listings}
|
||||||
\usepackage{xcolor}
|
\usepackage{xcolor}
|
||||||
\usepackage[ruled,vlined]{algorithm2e}
|
\usepackage[ruled,vlined]{algorithm2e}
|
||||||
\usepackage{cleveref}
|
\usepackage{cleveref}
|
||||||
\usepackage{adjustbox}
|
|
||||||
\usetikzlibrary{trees}
|
|
||||||
% Configure cleveref for algorithm2e
|
% Configure cleveref for algorithm2e
|
||||||
\crefname{algocf}{Algorithm}{Algorithms}
|
\crefname{algocf}{Algorithm}{Algorithms}
|
||||||
|
|
||||||
@@ -57,16 +49,6 @@
|
|||||||
literate={·}{{\textperiodcentered}}1 {−}{{\textminus}}1 {—}{{---}}1 {–}{{--}}1
|
literate={·}{{\textperiodcentered}}1 {−}{{\textminus}}1 {—}{{---}}1 {–}{{--}}1
|
||||||
}
|
}
|
||||||
|
|
||||||
% Use biblatex with authoryear style for in-text citations like (Author, Year)
|
% Use biblatex instead of natbib (acmart default)
|
||||||
\usepackage[backend=bibtex,style=authoryear,natbib=true,maxcitenames=2]{biblatex}
|
\usepackage[backend=bibtex,style=numeric]{biblatex}
|
||||||
\addbibresource{bib/references.bib}
|
\addbibresource{bib/references.bib}
|
||||||
|
|
||||||
% Page headers (SciTech format)
|
|
||||||
\usepackage{fancyhdr}
|
|
||||||
\setlength{\headheight}{14.5pt}
|
|
||||||
\addtolength{\topmargin}{-2.5pt}
|
|
||||||
\pagestyle{fancy}
|
|
||||||
\fancyhf{}
|
|
||||||
\fancyhead[L]{PHANTOM}
|
|
||||||
\fancyhead[R]{\thepage}
|
|
||||||
\renewcommand{\headrulewidth}{0pt}
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
[build-system]
|
|
||||||
requires = ["setuptools>=45", "wheel"]
|
|
||||||
build-backend = "setuptools.build_meta"
|
|
||||||
|
|
||||||
[project]
|
|
||||||
name = "phantom"
|
|
||||||
version = "0.1.0"
|
|
||||||
description = "Pricing Heuristics Against Non-human Transaction Orchestration Mechanisms"
|
|
||||||
requires-python = ">=3.8"
|
|
||||||
|
|
||||||
[tool.setuptools.packages.find]
|
|
||||||
include = ["experiments*", "lib*"]
|
|
||||||
@@ -12,4 +12,3 @@ uv
|
|||||||
scikit-learn
|
scikit-learn
|
||||||
supabase
|
supabase
|
||||||
pymc
|
pymc
|
||||||
wandb
|
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
# Executed on each TPU pod worker via `gcloud tpu-vm scp` + `gcloud tpu-vm ssh --worker=all`.
|
|
||||||
# Authenticates with Artifact Registry using the VM's service account metadata token,
|
|
||||||
# pulls the TPU trainer image, then runs the W&B sweep agent inside Docker.
|
|
||||||
# TPU chip devices (/dev/accel*) are exposed via --privileged + /dev volume mount.
|
|
||||||
# Required env vars: WANDB_API_KEY, SWEEP_ID
|
|
||||||
# Optional: AGENT_COUNT (default 1, 0 = run until sweep ends)
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
IMAGE="us-central1-docker.pkg.dev/phantom-trc/phantom/phantom-trainer:tpu-latest"
|
|
||||||
AGENT_COUNT="${AGENT_COUNT:-1}"
|
|
||||||
|
|
||||||
# use VM service account — no manual key needed on the pod
|
|
||||||
TOKEN=$(curl -sf -H "Metadata-Flavor: Google" \
|
|
||||||
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" \
|
|
||||||
| python3 -c 'import sys, json; print(json.load(sys.stdin)["access_token"])')
|
|
||||||
|
|
||||||
echo "$TOKEN" | sudo docker login -u oauth2accesstoken \
|
|
||||||
--password-stdin https://us-central1-docker.pkg.dev
|
|
||||||
|
|
||||||
sudo docker pull "$IMAGE"
|
|
||||||
|
|
||||||
# --privileged + /dev mount gives the container access to /dev/accel* (TPU chips)
|
|
||||||
# --network host lets JAX reach the other pod workers for distributed init
|
|
||||||
sudo docker run --rm \
|
|
||||||
--privileged \
|
|
||||||
--network host \
|
|
||||||
--volume /dev:/dev \
|
|
||||||
-e WANDB_API_KEY="$WANDB_API_KEY" \
|
|
||||||
-e SWEEP_ID="$SWEEP_ID" \
|
|
||||||
-e AGENT_COUNT="$AGENT_COUNT" \
|
|
||||||
"$IMAGE"
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
TPU_NAME="${TPU_NAME:?TPU_NAME is required}"
|
|
||||||
TPU_ZONE="${TPU_ZONE:-us-central2-b}"
|
|
||||||
TPU_PROJECT="${TPU_PROJECT:-phantom-trc}"
|
|
||||||
LOCAL_REPO_DIR="${LOCAL_REPO_DIR:-$(pwd)}"
|
|
||||||
REMOTE_REPO_DIR="${REMOTE_REPO_DIR:-/tmp/PHANTOM}"
|
|
||||||
ARCHIVE_PATH="${ARCHIVE_PATH:-/tmp/phantom-sync.tgz}"
|
|
||||||
|
|
||||||
FILE_LIST="$(mktemp /tmp/phantom-sync-files.XXXXXX)"
|
|
||||||
CLEANUP_LIST=true
|
|
||||||
|
|
||||||
cleanup() {
|
|
||||||
if [ "$CLEANUP_LIST" = "true" ]; then
|
|
||||||
rm -f "$FILE_LIST"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
trap cleanup EXIT
|
|
||||||
|
|
||||||
if [ ! -d "$LOCAL_REPO_DIR" ]; then
|
|
||||||
echo "local repo directory not found: $LOCAL_REPO_DIR"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if git -C "$LOCAL_REPO_DIR" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
|
|
||||||
git -C "$LOCAL_REPO_DIR" ls-files -co --exclude-standard > "$FILE_LIST"
|
|
||||||
python3 - "$FILE_LIST" <<'PY'
|
|
||||||
import sys
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
file_list = Path(sys.argv[1])
|
|
||||||
skip_prefixes = (
|
|
||||||
"wandb/",
|
|
||||||
".venv/",
|
|
||||||
"venv/",
|
|
||||||
"node_modules/",
|
|
||||||
".next/",
|
|
||||||
".turbo/",
|
|
||||||
"__pycache__/",
|
|
||||||
".mypy_cache/",
|
|
||||||
".pytest_cache/",
|
|
||||||
".ruff_cache/",
|
|
||||||
"paper/build/",
|
|
||||||
"tests/e2e/test-results/",
|
|
||||||
)
|
|
||||||
|
|
||||||
rows = file_list.read_text().splitlines()
|
|
||||||
kept = [
|
|
||||||
row
|
|
||||||
for row in rows
|
|
||||||
if row and not any(row == p.rstrip("/") or row.startswith(p) for p in skip_prefixes)
|
|
||||||
]
|
|
||||||
file_list.write_text("\n".join(kept) + ("\n" if kept else ""))
|
|
||||||
PY
|
|
||||||
tar -czf "$ARCHIVE_PATH" -C "$LOCAL_REPO_DIR" -T "$FILE_LIST"
|
|
||||||
else
|
|
||||||
tar \
|
|
||||||
--exclude-vcs \
|
|
||||||
--exclude=".venv" --exclude="*/.venv" \
|
|
||||||
--exclude="venv" --exclude="*/venv" \
|
|
||||||
--exclude="node_modules" --exclude="*/node_modules" \
|
|
||||||
--exclude=".next" --exclude="*/.next" \
|
|
||||||
--exclude=".turbo" --exclude="*/.turbo" \
|
|
||||||
--exclude="__pycache__" --exclude="*/__pycache__" \
|
|
||||||
--exclude=".mypy_cache" --exclude="*/.mypy_cache" \
|
|
||||||
--exclude=".pytest_cache" --exclude="*/.pytest_cache" \
|
|
||||||
--exclude=".ruff_cache" --exclude="*/.ruff_cache" \
|
|
||||||
--exclude="wandb" --exclude="*/wandb" \
|
|
||||||
--exclude="paper/build" \
|
|
||||||
--exclude="tests/e2e/test-results" \
|
|
||||||
-czf "$ARCHIVE_PATH" \
|
|
||||||
-C "$LOCAL_REPO_DIR" .
|
|
||||||
fi
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm scp "$ARCHIVE_PATH" "$TPU_NAME:/tmp/phantom-sync.tgz" \
|
|
||||||
--zone="$TPU_ZONE" --project="$TPU_PROJECT" --worker=all
|
|
||||||
|
|
||||||
gcloud compute tpus tpu-vm ssh "$TPU_NAME" \
|
|
||||||
--zone="$TPU_ZONE" --project="$TPU_PROJECT" --worker=all \
|
|
||||||
--command="rm -rf '$REMOTE_REPO_DIR' && mkdir -p '$REMOTE_REPO_DIR' && tar -xzf /tmp/phantom-sync.tgz -C '$REMOTE_REPO_DIR' && rm -f /tmp/phantom-sync.tgz"
|
|
||||||
|
|
||||||
rm -f "$ARCHIVE_PATH"
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import shlex
|
|
||||||
import subprocess
|
|
||||||
import time
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import wandb
|
|
||||||
|
|
||||||
|
|
||||||
CLI_MAP: dict[str, str] = {
|
|
||||||
"algo": "--algo",
|
|
||||||
"total_timesteps": "--total-timesteps",
|
|
||||||
"alpha": "--alpha",
|
|
||||||
"N": "--N",
|
|
||||||
"n_products": "--n-products",
|
|
||||||
"lambda_coi": "--lambda-coi",
|
|
||||||
"info_value": "--info-value",
|
|
||||||
"robust_radius": "--robust-radius",
|
|
||||||
"robust_points": "--robust-points",
|
|
||||||
"learning_rate": "--learning-rate",
|
|
||||||
"gamma": "--gamma",
|
|
||||||
"gae_lambda": "--gae-lambda",
|
|
||||||
"clip_range": "--clip-range",
|
|
||||||
"ent_coef": "--ent-coef",
|
|
||||||
"revenue_weight": "--revenue-weight",
|
|
||||||
"max_steps": "--max-steps",
|
|
||||||
"margin_floor": "--margin-floor",
|
|
||||||
"margin_floor_patience": "--margin-floor-patience",
|
|
||||||
"arch": "--arch",
|
|
||||||
"activation": "--activation",
|
|
||||||
"jax_num_envs": "--jax-num-envs",
|
|
||||||
"jax_num_steps": "--jax-num-steps",
|
|
||||||
"jax_num_minibatches": "--jax-num-minibatches",
|
|
||||||
"jax_update_epochs": "--jax-update-epochs",
|
|
||||||
"jax_anneal_lr": "--jax-anneal-lr",
|
|
||||||
"checkpoint_interval": "--checkpoint-interval",
|
|
||||||
"action_levels": "--action-levels",
|
|
||||||
"action_scale_low": "--action-scale-low",
|
|
||||||
"action_scale_high": "--action-scale-high",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def _to_cli_args(cfg: dict) -> str:
|
|
||||||
parts: list[str] = ["--jax", "--no-wandb"]
|
|
||||||
for key, flag in CLI_MAP.items():
|
|
||||||
if key not in cfg:
|
|
||||||
continue
|
|
||||||
value = cfg[key]
|
|
||||||
if value is None:
|
|
||||||
continue
|
|
||||||
if isinstance(value, bool):
|
|
||||||
if key == "jax_anneal_lr":
|
|
||||||
parts.extend([flag, "true" if value else "false"])
|
|
||||||
elif value:
|
|
||||||
parts.append(flag)
|
|
||||||
continue
|
|
||||||
parts.extend([flag, str(value)])
|
|
||||||
return " ".join(shlex.quote(p) for p in parts)
|
|
||||||
|
|
||||||
|
|
||||||
_SENTINEL = "PHANTOM_METRICS:"
|
|
||||||
|
|
||||||
|
|
||||||
def _extract_metrics(output: str) -> dict:
|
|
||||||
# fast path: look for the dedicated sentinel line emitted by run_local
|
|
||||||
for line in output.splitlines():
|
|
||||||
if line.startswith(_SENTINEL):
|
|
||||||
try:
|
|
||||||
return json.loads(line[len(_SENTINEL) :])
|
|
||||||
except Exception:
|
|
||||||
break
|
|
||||||
# fallback: scan for any JSON block containing eval/sweep keys;
|
|
||||||
# use greedy match to capture the largest possible block first
|
|
||||||
for block in re.findall(r"\{[^{}]*\}", output):
|
|
||||||
try:
|
|
||||||
obj = json.loads(block)
|
|
||||||
except Exception:
|
|
||||||
continue
|
|
||||||
if isinstance(obj, dict) and ("sweep/score" in obj or "eval/reward" in obj):
|
|
||||||
return obj
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
|
||||||
p = argparse.ArgumentParser(
|
|
||||||
description="Run W&B sweep where each trial uses full TPU pod"
|
|
||||||
)
|
|
||||||
p.add_argument("--sweep-id", required=True)
|
|
||||||
p.add_argument("--tpu-name", required=True)
|
|
||||||
p.add_argument("--tpu-zone", default="us-central2-b")
|
|
||||||
p.add_argument("--tpu-project", default="phantom-trc")
|
|
||||||
p.add_argument("--tpu-repo-dir", default="/tmp/PHANTOM")
|
|
||||||
p.add_argument("--count", type=int, default=0)
|
|
||||||
p.add_argument("--workdir", default=str(Path(__file__).resolve().parents[1]))
|
|
||||||
args = p.parse_args()
|
|
||||||
|
|
||||||
workdir = Path(args.workdir).resolve()
|
|
||||||
env = os.environ.copy()
|
|
||||||
|
|
||||||
prepare_cmd = [
|
|
||||||
"make",
|
|
||||||
"train.tpu.vm.prepare",
|
|
||||||
f"TPU_NAME={args.tpu_name}",
|
|
||||||
f"TPU_ZONE={args.tpu_zone}",
|
|
||||||
f"TPU_PROJECT={args.tpu_project}",
|
|
||||||
f"TPU_REPO_DIR={args.tpu_repo_dir}",
|
|
||||||
]
|
|
||||||
prepare = subprocess.run(
|
|
||||||
prepare_cmd,
|
|
||||||
cwd=workdir,
|
|
||||||
env=env,
|
|
||||||
text=True,
|
|
||||||
capture_output=False,
|
|
||||||
check=False,
|
|
||||||
)
|
|
||||||
if prepare.returncode != 0:
|
|
||||||
raise RuntimeError("Failed to prepare TPU workers for sweep")
|
|
||||||
|
|
||||||
def run_trial() -> None:
|
|
||||||
run = None
|
|
||||||
try:
|
|
||||||
run = wandb.init()
|
|
||||||
cfg = dict(wandb.config)
|
|
||||||
cli_args = _to_cli_args(cfg)
|
|
||||||
env_trial = dict(env)
|
|
||||||
env_trial["LOCAL_TRAIN_ARGS"] = cli_args
|
|
||||||
|
|
||||||
cmd = [
|
|
||||||
"make",
|
|
||||||
"train.tpu.vm.run",
|
|
||||||
f"TPU_NAME={args.tpu_name}",
|
|
||||||
f"TPU_ZONE={args.tpu_zone}",
|
|
||||||
f"TPU_PROJECT={args.tpu_project}",
|
|
||||||
f"TPU_REPO_DIR={args.tpu_repo_dir}",
|
|
||||||
]
|
|
||||||
|
|
||||||
proc = subprocess.run(
|
|
||||||
cmd,
|
|
||||||
cwd=workdir,
|
|
||||||
env=env_trial,
|
|
||||||
text=True,
|
|
||||||
capture_output=True,
|
|
||||||
check=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
if proc.stdout:
|
|
||||||
print(proc.stdout)
|
|
||||||
if proc.stderr:
|
|
||||||
print(proc.stderr)
|
|
||||||
|
|
||||||
if proc.returncode != 0:
|
|
||||||
if run is not None:
|
|
||||||
run.summary["runner/exit_code"] = proc.returncode
|
|
||||||
raise RuntimeError(f"TPU trial failed with exit code {proc.returncode}")
|
|
||||||
|
|
||||||
metrics = _extract_metrics(proc.stdout)
|
|
||||||
if metrics:
|
|
||||||
wandb.log(metrics)
|
|
||||||
for k, v in metrics.items():
|
|
||||||
run.summary[k] = v
|
|
||||||
run.summary["runner/exit_code"] = 0
|
|
||||||
except Exception:
|
|
||||||
time.sleep(2)
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
if run is not None and wandb.run is not None:
|
|
||||||
wandb.finish()
|
|
||||||
|
|
||||||
wandb.agent(
|
|
||||||
args.sweep_id,
|
|
||||||
function=run_trial,
|
|
||||||
count=args.count if args.count > 0 else None,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
#!/usr/bin/env sh
|
|
||||||
set -eu
|
|
||||||
|
|
||||||
REPO_DIR="${REPO_DIR:-$HOME/PHANTOM}"
|
|
||||||
PYTHON_BIN="${PYTHON_BIN:-python3}"
|
|
||||||
TRAIN_ARGS="${TRAIN_ARGS:---algo ppo --jax --total-timesteps 200000 --jax-num-envs 32 --jax-num-steps 128 --jax-num-minibatches 4 --jax-update-epochs 4}"
|
|
||||||
EXTRA_PIP="${EXTRA_PIP:-flax optax distrax}"
|
|
||||||
INSTALL_FULL_REQUIREMENTS="${INSTALL_FULL_REQUIREMENTS:-0}"
|
|
||||||
|
|
||||||
if [ ! -d "$REPO_DIR" ]; then
|
|
||||||
echo "repo directory not found: $REPO_DIR"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
cd "$REPO_DIR"
|
|
||||||
|
|
||||||
if [ -d "wandb" ]; then
|
|
||||||
rm -rf wandb
|
|
||||||
fi
|
|
||||||
|
|
||||||
# keep install idempotent and avoid re-installing jax/libtpu each run
|
|
||||||
if [ "$INSTALL_FULL_REQUIREMENTS" = "1" ] && [ -f "requirements.txt" ]; then
|
|
||||||
$PYTHON_BIN -m pip install -r requirements.txt
|
|
||||||
fi
|
|
||||||
if ! $PYTHON_BIN -c 'import flax, optax, distrax' >/dev/null 2>&1; then
|
|
||||||
if [ -f "engine/jax/requirements.txt" ]; then
|
|
||||||
$PYTHON_BIN -m pip install -r engine/jax/requirements.txt
|
|
||||||
fi
|
|
||||||
$PYTHON_BIN -m pip install -U $EXTRA_PIP
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "${WANDB_API_KEY:-}" ]; then
|
|
||||||
if ! $PYTHON_BIN -c 'import wandb; import inspect; assert hasattr(wandb, "init") and callable(wandb.init)' >/dev/null 2>&1; then
|
|
||||||
$PYTHON_BIN -m pip install -U wandb
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "${WANDB_API_KEY:-}" ]; then
|
|
||||||
export WANDB_API_KEY
|
|
||||||
exec $PYTHON_BIN -m engine.train $TRAIN_ARGS
|
|
||||||
fi
|
|
||||||
|
|
||||||
exec $PYTHON_BIN -m engine.train $TRAIN_ARGS --no-wandb
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user