From 423f3ae6f962017a75a0c24ff63e877088e6e502 Mon Sep 17 00:00:00 2001 From: timer Date: Wed, 12 Sep 2018 22:05:31 +0800 Subject: [PATCH 1/2] fix bug about initialization of NUM_SEP_BUFFER --- src/gridworld/GridWorld.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gridworld/GridWorld.cc b/src/gridworld/GridWorld.cc index d225844d0..b83a5dbd7 100644 --- a/src/gridworld/GridWorld.cc +++ b/src/gridworld/GridWorld.cc @@ -29,6 +29,7 @@ GridWorld::GridWorld() { random_engine.seed(0); counter_x = counter_y = nullptr; + NUM_SEP_BUFFER = 1; } GridWorld::~GridWorld() { From da573449536b2e17b2e7fe20b75a3e54574d1eab Mon Sep 17 00:00:00 2001 From: timer Date: Sat, 22 Sep 2018 16:50:24 +0800 Subject: [PATCH 2/2] Fixed bugs add update render/frontend + [model.py] In class ProcessingModel: add client_proc.join() when quit() + [render/frontend] Add and modify some default values Add help info in index.html + [train_single.py] Fix bug about useless args.name + [Gridworld.cc] Move initialization of NUM_SEP_BUFFER to reset() --- examples/train_single.py | 5 ++- python/magent/model.py | 9 +++++ src/gridworld/GridWorld.cc | 4 +-- src/render/frontend/index.html | 45 +++++++++++++++++++++++-- src/render/frontend/js/render-handle.js | 18 +++++++--- 5 files changed, 70 insertions(+), 11 deletions(-) diff --git a/examples/train_single.py b/examples/train_single.py index 27f8eb2e0..c28cb7100 100755 --- a/examples/train_single.py +++ b/examples/train_single.py @@ -138,7 +138,6 @@ def round_list(l): return [round(x, 2) for x in l] env.set_render_dir("build/render") # two groups of agents - names = [args.name + "-l", args.name + "-r"] handles = env.get_handles() # sample eval observation set @@ -158,14 +157,14 @@ def round_list(l): return [round(x, 2) for x in l] models = [] if args.alg == 'dqn': from magent.builtin.tf_model import DeepQNetwork - models.append(DeepQNetwork(env, handles[0], "battle", + models.append(DeepQNetwork(env, handles[0], args.name, batch_size=batch_size, learning_rate=3e-4, memory_size=2 ** 21, target_update=target_update, train_freq=train_freq, eval_obs=eval_obs)) elif args.alg == 'drqn': from magent.builtin.tf_model import DeepRecurrentQNetwork - models.append(DeepRecurrentQNetwork(env, handles[0], "battle", + models.append(DeepRecurrentQNetwork(env, handles[0], args.name, learning_rate=3e-4, batch_size=batch_size/unroll_step, unroll_step=unroll_step, memory_size=2 * 8 * 625, target_update=target_update, diff --git a/python/magent/model.py b/python/magent/model.py index bddb37a6a..a60e79331 100644 --- a/python/magent/model.py +++ b/python/magent/model.py @@ -149,6 +149,7 @@ def __init__(self, env, handle, name, port, sample_buffer_capacity=1000, args=(addr, sample_buffer_capacity, RLModel, kwargs), ) + self.client_proc = proc proc.start() listener = multiprocessing.connection.Listener(addr) self.conn = listener.accept() @@ -273,7 +274,15 @@ def check_done(self): def quit(self): """ quit """ + proc = self.client_proc + self.client_proc = None self.conn.send(["quit"]) + proc.join() + + def __del__(self): + """ quit in destruction """ + if self.client_proc is not None: + quit() def model_client(addr, sample_buffer_capacity, RLModel, model_args): diff --git a/src/gridworld/GridWorld.cc b/src/gridworld/GridWorld.cc index b83a5dbd7..d8ff7153e 100644 --- a/src/gridworld/GridWorld.cc +++ b/src/gridworld/GridWorld.cc @@ -29,7 +29,6 @@ GridWorld::GridWorld() { random_engine.seed(0); counter_x = counter_y = nullptr; - NUM_SEP_BUFFER = 1; } GridWorld::~GridWorld() { @@ -82,7 +81,8 @@ void GridWorld::reset() { } move_buffers = new std::vector[NUM_SEP_BUFFER]; turn_buffers = new std::vector[NUM_SEP_BUFFER]; - } + } else + NUM_SEP_BUFFER = 1; // reset map map.reset(width, height, food_mode); diff --git a/src/render/frontend/index.html b/src/render/frontend/index.html index 2f9daf455..eee6c8c12 100644 --- a/src/render/frontend/index.html +++ b/src/render/frontend/index.html @@ -32,14 +32,16 @@
+ placeholder="Enter magent configuration file here" + value="config.json">
+ placeholder="Enter magent map file here" + value="video_1.txt">
@@ -83,6 +85,45 @@
diff --git a/src/render/frontend/js/render-handle.js b/src/render/frontend/js/render-handle.js index e1ec38a10..8b8498ec9 100644 --- a/src/render/frontend/js/render-handle.js +++ b/src/render/frontend/js/render-handle.js @@ -97,24 +97,32 @@ function _drawNumbers() { function _onkeydown(event) { var windowCenterX, windowCenterY; if (event.keyCode === 37) { - _offsetX = _offsetX - Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); // Left + // Left + _offsetX = _offsetX - Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); _isWindowChanged = true; } else if (event.keyCode === 38) { - _offsetY = _offsetY - Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); // Up + // Up + _offsetY = _offsetY - Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); _isWindowChanged = true; } else if (event.keyCode === 39) { + // Right _offsetX = _offsetX + Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); _isWindowChanged = true; } else if (event.keyCode === 40) { + // Down _offsetY = _offsetY + Math.max(1, Math.round(MOVE_SPACING * 10 / gridSize)); _isWindowChanged = true; } else if (event.keyCode === 69) { + // E: Edit file name $("#magnet-file-modal").modal('show'); } else if (event.keyCode === 72) { + // H: Help $("#magnet-help-modal").modal('show'); } else if (event.keyCode === 83) { + // S: Settings $("#magnet-settings-modal").modal('show'); } else if (event.keyCode === 188) { + // ,<: Zoom Out windowCenterX = _gridCTX.canvas.width / 2 / gridSize + _offsetX; windowCenterY = _gridCTX.canvas.height / 2 / gridSize + _offsetY; gridSize = Math.max(1, gridSize - 1); @@ -123,6 +131,7 @@ function _onkeydown(event) { _isGridSizeChanged = true; _isWindowChanged = true; } else if (event.keyCode === 190) { + // .>: Zoom In windowCenterX = _gridCTX.canvas.width / 2 / gridSize + _offsetX; windowCenterY = _gridCTX.canvas.height / 2 / gridSize + _offsetY; gridSize = Math.min(100, gridSize + 1); @@ -131,6 +140,7 @@ function _onkeydown(event) { _isGridSizeChanged = true; _isWindowChanged = true; } else if (event.keyCode === 80) { + // P: Pause _mapForcedPause ^= 1; } } @@ -229,7 +239,7 @@ function run() { _offsetY = 0; gridSize = 10; _mapAnimateTick = 0; - _mapSpeed = 80; + _mapSpeed = 250; _mapForcedPause = false; _gridCTX = document.getElementById('magnet-canvas-grid').getContext('2d'); @@ -278,7 +288,7 @@ function run() { .removeAttr('disabled') .attr('min', 0) .attr('max', ANIMATE_STEP) - .attr('value', 80) + .attr('value', 250) .bind('change', function () { _mapSpeed = parseInt($('#magnet-settings-speed').val()); });