-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a28b31
commit ce1f8cd
Showing
3 changed files
with
214 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Notebook controller to update the state of Simulation\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 68, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import time\n", | ||
"import socket\n", | ||
"import pickle\n", | ||
"\n", | ||
"import numpy as np\n", | ||
"import jax.numpy as jnp\n", | ||
"from flax import serialization\n", | ||
"\n", | ||
"from MultiAgentsSim.two_d_simulation import SimpleSimulation\n", | ||
"from MultiAgentsSim.utils.network import SERVER" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 70, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"PORT = 5050\n", | ||
"ADDR = (SERVER, PORT)\n", | ||
"DATA_SIZE = 10835\n", | ||
"EVAL_TIME = 10\n", | ||
"\n", | ||
"color_map = {\"r\": (1.0, 0.0, 0.0),\n", | ||
" \"g\": (0.0, 1.0, 0.0),\n", | ||
" \"b\": (0.0, 0.0, 1.0)}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 71, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Start the server and intialize connection\n", | ||
"\n", | ||
"def connect_client():\n", | ||
" client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n", | ||
" client.connect(ADDR)\n", | ||
" print(f\"Connected to {ADDR}\")\n", | ||
"\n", | ||
" msg = client.recv(1024).decode()\n", | ||
" state_example = pickle.loads(client.recv(DATA_SIZE))\n", | ||
" state_bytes_size = len(serialization.to_bytes(state_example))\n", | ||
" response = \"NOTEBOOK\"\n", | ||
" client.send(response.encode())\n", | ||
" time.sleep(1)\n", | ||
"\n", | ||
" return client, state_example, state_bytes_size" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 78, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def close_client(client):\n", | ||
" client.send(\"CLOSE_CONNECTION\".encode())\n", | ||
"\n", | ||
"def get_state(client, state_example, state_bytes_size):\n", | ||
" client.send(\"GET_STATE\".encode())\n", | ||
" response = client.recv(state_bytes_size)\n", | ||
" return serialization.from_bytes(state_example, response)\n", | ||
"\n", | ||
"def change_state_agent_color(state, idx, color):\n", | ||
" colors = np.array(state.colors)\n", | ||
" colors[idx] = color_map[color]\n", | ||
" state = state.replace(colors=colors) \n", | ||
" return state\n", | ||
"\n", | ||
"def set_color(client, agent_idx, color, state_example, state_bytes_size):\n", | ||
" client.send(\"SET_STATE\".encode())\n", | ||
" current_state = serialization.from_bytes(state_example, client.recv(state_bytes_size))\n", | ||
" response_state = change_state_agent_color(current_state, agent_idx, color)\n", | ||
" client.send(serialization.to_bytes(response_state))\n", | ||
" return " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 73, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Connected to ('localhost', 5050)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"client, state_example, state_bytes_size = connect_client()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 77, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"state = get_state(client, state_example, state_bytes_size)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 54, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"state = get_state()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 81, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"set_color(client, 2, 'g', state_example, state_bytes_size)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 83, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"close_client(client)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "myvenv", | ||
"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.10.12" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains 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