Skip to content

Instantly share code, notes, and snippets.

@konabuta
Last active June 11, 2020 05:38
Show Gist options
  • Save konabuta/439e4445565c8f0f8e907f3e91590c8c to your computer and use it in GitHub Desktop.
Save konabuta/439e4445565c8f0f8e907f3e91590c8c to your computer and use it in GitHub Desktop.
###
# MSFT Bonsai
# Copyright 2020 Microsoft
# This code is licensed under MIT license (see LICENSE for details)
# Moab Tutorial 1
# This introductory sample demonstrates how to teach a policy for
# controlling a ball on the plate of a "Moab" hardware device.
# To understand this Inkling better, please follow our tutorial walkthrough:
# https://aka.ms/moab/tutorial1
###
inkling "2.0"
using Math
using Goal
# Distances measured in meters
const RadiusOfPlate = 0.1125 # m
# Velocities measured in meters per sec.
const MaxVelocity = 1.0
# Threshold for ball placement
const CloseEnough = 0.02
# State received from the simulator after each iteration
# イテレーションごとにシミュレータから状態を受け取る (座標と速度)
type ObservableState {
# Ball X,Y position (ボールのポジション)
ball_x: number<-RadiusOfPlate .. RadiusOfPlate>, # -0.1125 から +0.1125
ball_y: number<-RadiusOfPlate .. RadiusOfPlate>, # -0.1125 から +0.1125
# Ball X,Y velocity (ボールの速度)
ball_vel_x: number<-MaxVelocity .. MaxVelocity>,
ball_vel_y: number<-MaxVelocity .. MaxVelocity>,
}
# Action provided as output by policy and sent as
# input to the simulator
# Brain がシミュレーション環境に対して行う Action を定義
# -> ピッチとロールの調整
type SimAction {
# Range -1 to 1 is a scaled value that represents
# the full plate rotation range supported by the hardware.
input_pitch: number<-1 .. 1>, # rotate about x-axis (ジョイスティクからみて前後に傾ける)
input_roll: number<-1 .. 1>, # rotate about y-axis (ジョイスティクからみて左右に傾ける)
}
# Per-episode configuration that can be sent to the simulator.
# All iterations within an episode will use the same configuration.
type SimConfig {
# Model initial ball conditions
initial_x: number<-RadiusOfPlate .. RadiusOfPlate>, # in (m)
initial_y: number<-RadiusOfPlate .. RadiusOfPlate>,
# Model initial ball velocity conditions
initial_vel_x: number<-MaxVelocity .. MaxVelocity>, # in (m/s)
initial_vel_y: number<-MaxVelocity .. MaxVelocity>,
# Range -1 to 1 is a scaled value that represents
# the full plate rotation range supported by the hardware.
initial_pitch: number<-1 .. 1>,
initial_roll: number<-1 .. 1>,
}
# Define a concept graph with a single concept
graph (input: ObservableState) {
concept MoveToCenter(input): SimAction {
curriculum {
# The source of training for this concept is a simulator that
# - can be configured for each episode using fields defined in SimConfig,
# - accepts per-iteration actions defined in SimAction, and
# - outputs states with the fields defined in SimState.
source simulator MoabSim (Action: SimAction, Config: SimConfig): ObservableState {
# Automatically launch the simulator with this
# registered package name.
package "Moab"
}
# The objective of training is expressed as a goal with two
# subgoals: don't let the ball fall off the plate, and drive
# the ball to the center of the plate.
# ゴール・目標を定義する
goal (State: ObservableState) {
# avoid : 避けたい領域
# 中心からの半径 80 % 以上の領域を避ける
avoid `Fall Off Plate`:
Math.Hypot(State.ball_x, State.ball_y) in Goal.RangeAbove(RadiusOfPlate * 0.8) # X,Y 座標から斜辺を算出
# drive : 早く到着して、滞在したい領域
# 中心に到着して滞在する
drive `Center Of Plate`:
[State.ball_x, State.ball_y] in Goal.Sphere([0, 0], CloseEnough)
}
training {
# Limit episodes to 250 iterations instead of the default 1000.
EpisodeIterationLimit: 250
}
lesson `Randomize Start` {
# Specify the configuration parameters that should be varied
# from one episode to the next during this lesson.
scenario {
initial_x: number<-RadiusOfPlate * 0.5 .. RadiusOfPlate * 0.5>,
initial_y: number<-RadiusOfPlate * 0.5 .. RadiusOfPlate * 0.5>,
initial_vel_x: number<-MaxVelocity * 0.02 .. MaxVelocity * 0.02>,
initial_vel_y: number<-MaxVelocity * 0.02 .. MaxVelocity * 0.02>,
initial_pitch: number<-0.2 .. 0.2>,
initial_roll: number<-0.2 .. 0.2>,
}
}
}
}
}
# Special string to hook up the simulator visualizer
# in the web interface.
const SimulatorVisualizer = "/moabviz/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment