Skip to content

Commit

Permalink
Merge pull request #40 from TimerChen/master
Browse files Browse the repository at this point in the history
Fix some bugs. Improve render
  • Loading branch information
merrymercy authored Sep 25, 2018
2 parents 872680a + da57344 commit 92256aa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
5 changes: 2 additions & 3 deletions examples/train_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions python/magent/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion src/gridworld/GridWorld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ void GridWorld::reset() {
}
move_buffers = new std::vector<MoveAction>[NUM_SEP_BUFFER];
turn_buffers = new std::vector<TurnAction>[NUM_SEP_BUFFER];
}
} else
NUM_SEP_BUFFER = 1;

// reset map
map.reset(width, height, food_mode);
Expand Down
45 changes: 43 additions & 2 deletions src/render/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ <h4 class="modal-title">Files</h4>
<label for="magnet-file-form-conf" class="col-sm-2 control-label">Configuration</label>
<div class="col-sm-10">
<input class="form-control" id="magnet-file-form-conf"
placeholder="Enter magent configuration file here">
placeholder="Enter magent configuration file here"
value="config.json">
</div>
</div>
<div class="form-group">
<label for="magnet-file-form-map" class="col-sm-2 control-label">Map File</label>
<div class="col-sm-10">
<input class="form-control" id="magnet-file-form-map"
placeholder="Enter magent map file here">
placeholder="Enter magent map file here"
value="video_1.txt">
</div>
</div>
<div class="form-group">
Expand Down Expand Up @@ -83,6 +85,45 @@ <h4 class="modal-title">Settings</h4>
<h4 class="modal-title">Help</h4>
</div>
<div class="modal-body">
<h4><strong>Shortcuts</strong></h4>

<table class="Shortcuts table">
<tbody>
<tr><th width="52%">Description</th><th width="24%">Key</th></tr>
<tr>
<td>Move the map</td>
<td>arrows</td>
</tr>
<tr>
<td>Edit config and video file names</td>
<td>e</td>
</tr>
<tr>
<td>Help information</td>
<td>h</td>
</tr>
<tr>
<td>Edit settings</td>
<td>s</td>
</tr>
<tr>
<td>Zoom Out</td>
<td>,</td>
</tr>
<tr>
<td>Zoom In</td>
<td>.</td>
</tr>
<tr>
<td>Pause</td>
<td>p</td>
</tr>
<tr>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
Expand Down
18 changes: 14 additions & 4 deletions src/render/frontend/js/render-handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -131,6 +140,7 @@ function _onkeydown(event) {
_isGridSizeChanged = true;
_isWindowChanged = true;
} else if (event.keyCode === 80) {
// P: Pause
_mapForcedPause ^= 1;
}
}
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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());
});
Expand Down

0 comments on commit 92256aa

Please sign in to comment.