{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:13:12.648377Z", "start_time": "2022-09-02T11:13:12.165387Z" }, "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:14:10.246402Z", "start_time": "2022-09-02T11:14:10.235135Z" }, "collapsed": false }, "outputs": [], "source": [ "class Walker:\n", " \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n", "\n", " def __init__(self, sigma_i, sigma_j, size, map_type='flat'):\n", " self.sigma_i = sigma_i\n", " self.sigma_j = sigma_j\n", " self.size = size\n", "\n", " if map_type == 'flat':\n", " context_map = np.ones((size, size))\n", " elif map_type == 'hills':\n", " grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n", " i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n", " i_waves /= i_waves.max()\n", " j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) + \\\n", " np.sin(grid_jj / 10)\n", " j_waves /= j_waves.max()\n", " context_map = j_waves + i_waves\n", " elif map_type == 'labyrinth':\n", " context_map = np.ones((size, size))\n", " context_map[50:100, 50:60] = 0\n", " context_map[20:89, 80:90] = 0\n", " context_map[90:120, 0:10] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", "\n", " context_map[50:60, 50:200] = 0\n", " context_map[179:189, 80:130] = 0\n", " context_map[110:120, 0:190] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", " context_map /= context_map.sum()\n", " self.context_map = context_map\n", "\n", " # Pre-compute a 2D grid of coordinates for efficiency\n", " self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n", "\n", "walker = Walker(sigma_i=3, sigma_j=4, size=200, map_type='hills')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:15:44.357965Z", "start_time": "2022-09-02T11:15:44.351558Z" }, "collapsed": false }, "outputs": [], "source": [ "class Walker:\n", " \"\"\" The Walker knows how to walk at random on a context map. \"\"\"\n", "\n", " def __init__(self, sigma_i, sigma_j, size, context_map):\n", " self.sigma_i = sigma_i\n", " self.sigma_j = sigma_j\n", " self.size = size\n", " self.context_map = context_map\n", " # Pre-compute a 2D grid of coordinates for efficiency\n", " self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n", "\n", " @classmethod\n", " def from_context_map_type(cls, sigma_i, sigma_j, size, map_type):\n", " \"\"\" Create an instance of Walker with a context map defined by type.\"\"\"\n", " if map_type == 'flat':\n", " context_map = np.ones((size, size))\n", " elif map_type == 'hills':\n", " grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n", " i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n", " i_waves /= i_waves.max()\n", " j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) +\\\n", " np.sin(grid_jj / 10)\n", " j_waves /= j_waves.max()\n", " context_map = j_waves + i_waves\n", " elif map_type == 'labyrinth':\n", " context_map = np.ones((size, size))\n", " context_map[50:100, 50:60] = 0\n", " context_map[20:89, 80:90] = 0\n", " context_map[90:120, 0:10] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", "\n", " context_map[50:60, 50:200] = 0\n", " context_map[179:189, 80:130] = 0\n", " context_map[110:120, 0:190] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", "\n", " context_map /= context_map.sum()\n", " return cls(sigma_i, sigma_j, size, context_map)\n" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:15:49.092194Z", "start_time": "2022-09-02T11:15:49.086575Z" }, "collapsed": true }, "outputs": [], "source": [ "walker = Walker.from_context_map_type(sigma_i=3, sigma_j=4, size=200, map_type='hills')" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:19:37.723607Z", "start_time": "2022-09-02T11:19:37.717518Z" }, "collapsed": true }, "outputs": [], "source": [ "def flat_context_map_builder(size):\n", " \"\"\" A context map where all positions are equally likely. \"\"\"\n", " return np.ones((size, size))\n", "\n", "\n", "def hills_context_map_builder(size):\n", " \"\"\" A context map with bumps and valleys. \"\"\"\n", " grid_ii, grid_jj = np.mgrid[0:size, 0:size]\n", " i_waves = np.sin(grid_ii / 130) + np.sin(grid_ii / 10)\n", " i_waves /= i_waves.max()\n", " j_waves = np.sin(grid_jj / 100) + np.sin(grid_jj / 50) + \\\n", " np.sin(grid_jj / 10)\n", " j_waves /= j_waves.max()\n", " context_map = j_waves + i_waves\n", " return context_map\n", "\n", "\n", "def labyrinth_context_map_builder(size):\n", " \"\"\" A context map that looks like a labyrinth. \"\"\"\n", " context_map = np.ones((size, size))\n", " context_map[50:100, 50:60] = 0\n", " context_map[20:89, 80:90] = 0\n", " context_map[90:120, 0:10] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", "\n", " context_map[50:60, 50:200] = 0\n", " context_map[179:189, 80:130] = 0\n", " context_map[110:120, 0:190] = 0\n", " context_map[120:size, 30:40] = 0\n", " context_map[180:190, 50:60] = 0\n", "\n", " return context_map" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:20:25.815725Z", "start_time": "2022-09-02T11:20:25.811489Z" }, "collapsed": true }, "outputs": [], "source": [ "class Walker:\n", "\n", " def __init__(self, sigma_i, sigma_j, size, context_map):\n", " self.sigma_i = sigma_i\n", " self.sigma_j = sigma_j\n", " self.size = size\n", " self.context_map = context_map\n", " # Pre-compute a 2D grid of coordinates for efficiency\n", " self._grid_ii, self._grid_jj = np.mgrid[0:size, 0:size]\n", "\n", " @classmethod\n", " def from_context_map_builder(cls, sigma_i, sigma_j, size, context_map_builder):\n", " \"\"\"Initialize the context map from an external builder.\n", "\n", " `builder` is a callable that takes a `size` as input parameter\n", " and outputs a `size` x `size` numpy array of positive values.\n", " \"\"\"\n", " context_map = context_map_builder(size)\n", " context_map /= context_map.sum()\n", " return cls(sigma_i, sigma_j, size, context_map)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2022-09-02T11:20:26.367914Z", "start_time": "2022-09-02T11:20:26.362287Z" }, "collapsed": true }, "outputs": [], "source": [ "walker = Walker.from_context_map_builder(\n", " sigma_i=3, \n", " sigma_j=4, \n", " size=200, \n", " context_map_builder=hills_context_map_builder,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a" ] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python [conda env:bog]", "language": "python", "name": "conda-env-bog-py" }, "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.6.5" }, "toc": { "nav_menu": { "height": "12px", "width": "252px" }, "navigate_menu": true, "number_sections": true, "sideBar": true, "threshold": 4, "toc_cell": false, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }