Skip to content

Commit

Permalink
WIP: Update tutorial part 7 to work with cadCAD 0.4.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jzaki committed Aug 27, 2020
1 parent 07f654a commit baf6634
Showing 1 changed file with 57 additions and 47 deletions.
104 changes: 57 additions & 47 deletions tutorials/robot-marbles-part-7/robot-marbles-part-7.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"# List of all the state variables in the system and their initial values\n",
"initial_conditions = {\n",
"genesis_states = {\n",
" 'box_A': 10, # as per the description of the example, box_A starts out with 10 marbles in it\n",
" 'box_B': 0 # as per the description of the example, box_B starts out empty\n",
"}\n",
Expand All @@ -53,7 +53,7 @@
"# `N` is the number of times the simulation will be run (Monte Carlo runs)\n",
"# In this example, we'll run the simulation once (N=1) and its duration will be of 10 timesteps\n",
"# We'll cover the `M` key in a future article. For now, let's leave it empty\n",
"simulation_parameters = {\n",
"sim_config_dict = {\n",
" 'T': range(10),\n",
" 'N': 50, # We'll run the same simulation 50 times; the random events in each simulation are independent\n",
" #'M': {}\n",
Expand All @@ -62,7 +62,7 @@
"\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"# We specify the robot arm's logic in a Policy Function\n",
"def robot_arm(params, step, sL, s, capacity=1):\n",
"def robot_arm(params, step, sH, s, capacity=1):\n",
" add_to_A = 0\n",
" if (s['box_A'] > s['box_B']):\n",
" add_to_A = -capacity\n",
Expand All @@ -72,17 +72,17 @@
" \n",
"robots_probabilities = [0.5,1/3] # Robot 1 acts with a 50% probability; Robot 2, 33.33%\n",
"\n",
"def robot_arm_1(params, step, sL, s):\n",
"def robot_arm_1(params, step, sH, s):\n",
" _robotId = 1\n",
" if rand()<robots_probabilities[_robotId-1]: # draw a random number between 0 and 1; if it's smaller than the robot's parameter, it acts\n",
" return robot_arm(params, step, sL, s)\n",
" return robot_arm(params, step, sH, s)\n",
" else:\n",
" return({'add_to_A': 0, 'add_to_B': 0}) # otherwise, the robot doesn't interfere with the system\n",
"\n",
"def robot_arm_2(params, step, sL, s):\n",
"def robot_arm_2(params, step, sH, s):\n",
" _robotId = 2\n",
" if rand()<robots_probabilities[_robotId-1]: # draw a random number between 0 and 1; if it's smaller than the robot's parameter, it acts\n",
" return robot_arm(params, step, sL, s)\n",
" return robot_arm(params, step, sH, s)\n",
" else:\n",
" return({'add_to_A': 0, 'add_to_B': 0}) # otherwise, the robot doesn't interfere with the system\n",
"\n",
Expand All @@ -93,12 +93,12 @@
"# We make the state update functions less \"intelligent\",\n",
"# ie. they simply add the number of marbles specified in _input \n",
"# (which, per the policy function definition, may be negative)\n",
"def increment_A(params, step, sL, s, _input):\n",
"def increment_A(params, step, sH, s, _input):\n",
" y = 'box_A'\n",
" x = s['box_A'] + _input['add_to_A']\n",
" return (y, x)\n",
"\n",
"def increment_B(params, step, sL, s, _input):\n",
"def increment_B(params, step, sH, s, _input):\n",
" y = 'box_B'\n",
" x = s['box_B'] + _input['add_to_B']\n",
" return (y, x)\n",
Expand Down Expand Up @@ -133,21 +133,24 @@
"source": [
"%%capture\n",
"from cadCAD.configuration.utils import config_sim\n",
"from cadCAD.configuration import append_configs\n",
"from cadCAD.configuration import Experiment\n",
"from cadCAD import configs\n",
"\n",
"c = config_sim(simulation_parameters)\n",
"del configs[:]\n",
"\n",
"exp = Experiment()\n",
"c = config_sim(sim_config_dict)\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"# The configurations above are then packaged into a `Configuration` object\n",
"append_configs(initial_state=initial_conditions, #dict containing variable names and initial values\n",
"exp.append_configs(initial_state=genesis_states, #dict containing variable names and initial values\n",
" partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions\n",
" sim_configs=c #preprocessed dictionaries containing simulation parameters\n",
" )\n",
"\n",
"from cadCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
"exec_mode = ExecutionMode()\n",
"exec_context = ExecutionContext(exec_mode.local_mode)\n",
"executor = Executor(exec_context, configs) # Pass the configuration object inside an array\n",
"local_mode_ctx = ExecutionContext(exec_mode.local_mode)\n",
"executor = Executor(local_mode_ctx, configs) # Pass the configuration object inside an array\n",
"raw_result, tensor, sessions = executor.execute() # The `execute()` method returns a tuple; its first elements contains the raw results\n",
"\n",
"%matplotlib inline\n",
Expand Down Expand Up @@ -185,7 +188,7 @@
" colormap = 'RdYlGn',\n",
" ax = ax\n",
" )\n",
"print_plot(simulation_parameters['N'])"
"print_plot(sim_config_dict['N'])"
]
},
{
Expand All @@ -206,19 +209,20 @@
"source": [
"robots_probabilities = [0.5,1/3] # Robot 1 acts with a 50% probability; Robot 2, 33.33%\n",
"\n",
"def robot_arm_1(params, step, sL, s):\n",
"def robot_arm_1(params, step, sH, s):\n",
" params\n",
" _robotId = 1\n",
" capacity = params['capacity']['robot_1']\n",
" if rand()<robots_probabilities[_robotId-1]: # draw a random number between 0 and 1; if it's smaller than the robot's parameter, it acts\n",
" return robot_arm(params, step, sL, s, capacity)\n",
" return robot_arm(params, step, sH, s, capacity)\n",
" else:\n",
" return({'add_to_A': 0, 'add_to_B': 0}) # otherwise, the robot doesn't interfere with the system\n",
"\n",
"def robot_arm_2(params, step, sL, s):\n",
"def robot_arm_2(params, step, sH, s):\n",
" _robotId = 2\n",
" capacity = params['capacity']['robot_2']\n",
" if rand()<robots_probabilities[_robotId-1]: # draw a random number between 0 and 1; if it's smaller than the robot's parameter, it acts\n",
" return robot_arm(params, step, sL, s, capacity)\n",
" return robot_arm(params, step, sH, s, capacity)\n",
" else:\n",
" return({'add_to_A': 0, 'add_to_B': 0}) # otherwise, the robot doesn't interfere with the system\n",
"\n",
Expand Down Expand Up @@ -256,13 +260,14 @@
"# In this example, we'll run the simulation once (N=50) and its duration will be of 10 timesteps\n",
"# `M` is a dictionary of parameters key in a future article. For now, let's leave it empty\n",
"\n",
"simulation_parameters = {\n",
"sim_config_dict = {\n",
" 'T': range(10),\n",
" 'N': 50, # We'll run the same simulation 50 times; the random events in each simulation are independent\n",
" 'M': {\n",
" 'capacity': {'robot_1': 2, 'robot_2': 1 }\n",
" }\n",
"}\n",
"\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "
]
},
Expand All @@ -283,28 +288,28 @@
"\n",
"del configs[:]\n",
"\n",
"c = config_sim(simulation_parameters)\n",
"c = config_sim(sim_config_dict)\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"# The configurations above are then packaged into a `Configuration` object\n",
"append_configs(initial_state=initial_conditions, #dict containing variable names and initial values\n",
"exp.append_configs(initial_state=genesis_states, #dict containing variable names and initial values\n",
" partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions\n",
" sim_configs=c #preprocessed dictionaries containing simulation parameters\n",
" )\n",
"\n",
"exec_mode = ExecutionMode()\n",
"exec_context = ExecutionContext(exec_mode.multi_mode)\n",
"executor = Executor(exec_context, configs) # Pass the configuration object inside an array\n",
"local_mode_ctx = ExecutionContext(exec_mode.local_mode)\n",
"run = Executor(local_mode_ctx, configs) # Pass the configuration object inside an array\n",
"\n",
"%matplotlib inline\n",
"import pandas as pd\n",
"\n",
"# results = []\n",
"# raw_result, tensor, sessions in executor.execute():\n",
"# df = pd.DataFrame(raw_result)\n",
"# results.append(df)\n",
"results = []\n",
"for raw_result, tensor, sessions in executor.execute():\n",
" df = pd.DataFrame(raw_result)\n",
" results.append(df)\n",
"\n",
"raw_result, tensor, sessions = executor.execute()\n",
"df = pd.DataFrame(raw_result)\n"
"# raw_result, tensor, sessions = run.execute()\n",
"# df = pd.DataFrame(raw_result)\n"
]
},
{
Expand All @@ -316,7 +321,7 @@
"outputs": [],
"source": [
"for df in results:\n",
" print_plot(simulation_parameters['N'])"
" print_plot(sim_config_dict['N'])"
]
},
{
Expand All @@ -336,8 +341,8 @@
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"# We use `config_sim` to generate a list of configurations\n",
"from cadCAD.configuration.utils import config_sim\n",
"simulation_parameters = config_sim(\n",
" {\n",
"\n",
"sim_config_dict = {\n",
" 'T': range(10),\n",
" 'N': 50, # We'll run the same simulation 50 times; the random events in each simulation are independent\n",
" 'M': {\n",
Expand All @@ -348,10 +353,9 @@
" ]\n",
" }\n",
" }\n",
")\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # \n",
"\n",
"simulation_parameters"
"c = config_sim(sim_config_dict)\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # "
]
},
{
Expand All @@ -367,14 +371,15 @@
"metadata": {},
"outputs": [],
"source": [
"from cadCAD.configuration import append_configs\n",
"from cadCAD import configs\n",
"# from cadCAD.configuration import append_configs\n",
"# from cadCAD import configs\n",
"del configs[:]\n",
"# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\n",
"# The configurations above are then packaged into a `Configuration` object\n",
"append_configs(\n",
" initial_state=initial_conditions, #dict containing variable names and initial values\n",
"exp.append_configs(\n",
" initial_state=genesis_states, #dict containing variable names and initial values\n",
" partial_state_update_blocks=partial_state_update_blocks, #dict containing state update functions\n",
" sim_configs=simulation_parameters #dict containing simulation parameters\n",
" sim_configs=c #dict containing simulation parameters\n",
")"
]
},
Expand All @@ -392,8 +397,8 @@
"outputs": [],
"source": [
"exec_mode = ExecutionMode()\n",
"exec_context = ExecutionContext(exec_mode.multi_proc)\n",
"executor = Executor(exec_context, configs) # Pass an array configuration objects"
"multi_mode_ctx = ExecutionContext(exec_mode.multi_mode)\n",
"executor = Executor(multi_mode_ctx, configs) # Pass an array configuration objects"
]
},
{
Expand All @@ -411,9 +416,12 @@
"source": [
"%%capture\n",
"results = []\n",
"for raw_result, tensor in executor.execute():\n",
"for raw_result, tensor, sessions in executor.execute():\n",
" df = pd.DataFrame(raw_result)\n",
" results.append(df)"
" results.append(df)\n",
"\n",
"# raw_result, tensor, sessions in executor.execute()\n",
"# df = pd.DataFrame(raw_result)"
]
},
{
Expand All @@ -423,7 +431,9 @@
"outputs": [],
"source": [
"for df in results:\n",
" print_plot(simulation_parameters[0]['N'])"
" print_plot(c[0]['N'])\n",
"\n",
"# print_plot(c[0]['N'])"
]
},
{
Expand Down Expand Up @@ -459,7 +469,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.11-final"
"version": "3.8.2-final"
}
},
"nbformat": 4,
Expand Down

0 comments on commit baf6634

Please sign in to comment.