Last active
April 2, 2016 14:32
-
-
Save oyamad/8ff02480bb0759b0e365b2124bf2aa96 to your computer and use it in GitHub Desktop.
ddp_solve_lp_py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import openopt\n", | |
"import quantecon as qe\n", | |
"from quantecon.markov.ddp import DPSolveResult" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def solve(ddp, solver='pclp', iprint=-1):\n", | |
" n = ddp.num_states\n", | |
" L = ddp.num_sa_pairs\n", | |
" \n", | |
" # min f v\n", | |
" # s.t. A v <= b\n", | |
" A = ddp.Q * ddp.beta\n", | |
" b = -ddp.R\n", | |
" if ddp._sa_pair:\n", | |
" A[np.arange(L), ddp.s_indices] -= 1\n", | |
" else:\n", | |
" A[np.arange(n), :, np.arange(n)] -= 1\n", | |
" A.shape = (L, n)\n", | |
" b.shape = (L,)\n", | |
" f = np.ones(n)\n", | |
" \n", | |
" p = openopt.LP(f=f, A=A, b=b)\n", | |
" r = p.minimize(solver, iprint=iprint)\n", | |
" v = r.xf\n", | |
" \n", | |
" sigma = ddp.compute_greedy(v)\n", | |
" mc = ddp.controlled_mc(sigma)\n", | |
" \n", | |
" res = DPSolveResult(v=v,\n", | |
" sigma=sigma,\n", | |
" mc=mc,\n", | |
" method='linear programming')\n", | |
" return res" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"seed = 1234" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"ddp = qe.markov.random_discrete_dp(3, 2, 0.95, random_state=seed)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" mc: Markov chain with transition matrix \n", | |
"P = \n", | |
"[[ 0.27646426 0.52540792 0.19812782]\n", | |
" [ 0.35781727 0.14317786 0.49900487]\n", | |
" [ 0.01376845 0.48931472 0.49691683]]\n", | |
" num_iter: 1\n", | |
" max_iter: 250\n", | |
" sigma: array([0, 0, 1])\n", | |
" method: 'policy iteration'\n", | |
" v: array([ 19.63935279, 20.45805973, 20.18065736])" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ddp.solve()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" mc: Markov chain with transition matrix \n", | |
"P = \n", | |
"[[ 0.27646426 0.52540792 0.19812782]\n", | |
" [ 0.35781727 0.14317786 0.49900487]\n", | |
" [ 0.01376845 0.48931472 0.49691683]]\n", | |
" sigma: array([0, 0, 1])\n", | |
" method: 'linear programming'\n", | |
" v: array([ 19.63935279, 20.45805973, 20.18065736])" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"solve(ddp)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" mc: Markov chain with transition matrix \n", | |
"P = \n", | |
"[[ 0.27646426 0.52540792 0.19812782]\n", | |
" [ 0.35781727 0.14317786 0.49900487]\n", | |
" [ 0.01376845 0.48931472 0.49691683]]\n", | |
" sigma: array([0, 0, 1])\n", | |
" method: 'linear programming'\n", | |
" v: array([ 19.63935279, 20.45805973, 20.18065736])" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ddp_sa = qe.markov.random_discrete_dp(3, 2, 0.95, sa_pair=True, random_state=seed)\n", | |
"solve(ddp_sa)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
" mc: Markov chain with transition matrix \n", | |
"P = \n", | |
" (0, 2)\t0.198127822465\n", | |
" (0, 1)\t0.525407922392\n", | |
" (0, 0)\t0.276464255143\n", | |
" (1, 2)\t0.499004874477\n", | |
" (1, 1)\t0.143177855566\n", | |
" (1, 0)\t0.357817269958\n", | |
" (2, 2)\t0.496916834692\n", | |
" (2, 1)\t0.489314715717\n", | |
" (2, 0)\t0.0137684495907\n", | |
" sigma: array([0, 0, 1])\n", | |
" method: 'linear programming'\n", | |
" v: array([ 19.63935279, 20.45805973, 20.18065736])" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ddp_sp = qe.markov.random_discrete_dp(3, 2, 0.95, sparse=True, random_state=seed)\n", | |
"solve(ddp_sp)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"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.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment