def run(): dummy_env = get_env(env_name) model = ActorCritic(dummy_env.observation_space.shape[0] * num_stack, dummy_env.action_space) del dummy_env optimizer = optimizer = optim.RMSprop( model.parameters(), lr, eps=eps, alpha=alpha) train(model, optimizer, lambda: get_env(env_name), num_envs, num_stack, num_steps, num_updates, gamma, value_loss_coef, entropy_coef, max_grad_norm)
def test_get_env(): ret = env.get_env(TEST_BASH_FILE) assert ret['FIRST'] == '1' assert ret['FILE'] == 'the file' assert os.environ['FIRST'] == '1' assert os.environ['FILE'] == 'the file' ret2 = env.get_env(profile_dir=dirname(TEST_BASH_FILE), prefix='ctools') print(ret2) assert ret2['CTOOLS'] == 'YES'
def __init__(self, X, Y, Z, pos=None, rpy=None, color='r', name=None, pose=None, visible=True, transparency=None): """ Create a new rectangular box. X -- box half-length Y -- box half-width Z -- box half-height pos -- initial position in inertial frame rpy -- initial orientation in inertial frame color -- color letter in ['r', 'g', 'b'] name -- object's name (optional) pose -- initial pose (supersedes pos and rpy) visible -- initial box visibility transparency -- transparency value, 0. is opaque and 1. is transparent """ if not name: name = "Box-%s" % str(uuid.uuid1())[0:3] self.X = X self.Y = Y self.Z = Z aabb = [0, 0, 0, X, Y, Z] env = get_env() with env: box = openravepy.RaveCreateKinBody(env, '') box.InitFromBoxes(array([array(aabb)]), True) super(Box, self).__init__( box, pos=pos, rpy=rpy, color=color, name=name, pose=pose, visible=visible, transparency=transparency) env.Add(box, True)
def _setup(self): self.env_id = "CartPole-v0" self.env, self.val_env = get_env(self.env_id) self.config = { "num_trials": 5, "num_frames": 30000, "num_val_trials": 10, "batch_size": 32, "gamma": 0.99, "method": 'our', "var": 1., "mean": 0., "decision_eps": 1., "theta": 1., "cnn": False, "invert_actions": False, "return_latent": 'second_hidden', "num_val_trials": 10, "batch_size": 32, "gamma": 0.99, "num_trials": 5, "USE_CUDA": True, "device": "", "eps": 1., "num_workers": 0, "num_gpus": 0, # These params are tuned from a fixed starting value. "lr": 1e-4 # These params start off randomly drawn from a set. }
def draw_3d_cone(apex, axis, section, combined='r-#', color=None, linewidth=2., pointsize=0.05): """ Draw a 3D cone defined from its apex, axis vector and a cross-section polygon (defined in the plane orthogonal to the axis vector). INPUT: - ``apex`` -- position of the origin of the cone in world coordinates - ``axis`` -- unit vector directing the cone axis and lying inside the cone - ``combined`` -- (default: 'g-#') drawing spec in matplotlib fashion: color letter followed by faces to draw (see ``color`` and ``faces``) - ``linewidth`` -- thickness of the edges of the cone - ``pointsize`` -- point size in meters OUTPUT: A list of OpenRAVE handles. Must be stored in some variable, otherwise the drawn object will vanish instantly. """ if len(section) < 1: warn("Trying to draw an empty cone") return [] color = color if color is not None else _matplotlib_to_rgba(combined[0]) env = get_env() handles = draw_polygon( points=section, normal=axis, combined=combined, color=color) edges = vstack([[apex, vertex] for vertex in section]) edges = array(edges) edge_color = array(color) * 0.7 edge_color[3] = 1. handles.append(env.drawlinelist( edges, linewidth=linewidth, colors=edge_color)) return handles
def check_env_vars(env_vars): """Checks if the env variables are set as required to run the test. Returns: True if all the env variables are set as required, otherwise False. """ if not env_vars: return True for key in env_vars: if env.get_env(key) != env_vars.get(key): return False return True
def draw_force_lines(self, length=0.25): env = get_env() self.gui_handles = [] for c in self.vertices: color = [0.1, 0.1, 0.1] color[numpy.random.randint(3)] += 0.2 for f in self.force_span: self.gui_handles.append(env.drawlinelist( array([c, c + length * f]), linewidth=1, colors=color)) self.gui_handles.append(env.drawlinelist( array([c, c + length * self.n]), linewidth=5, colors=color))
def get(self, url_nonce): util.EnableCors(self) user = model.User.all().filter("url_nonce =", url_nonce).get() if user is None: self.error(404) self.response.write("user not found") return # maybe we should do sum instead? biggest_pledge = None biggest_amount = 0 for pledge in itertools.chain( model.Pledge.all().filter("email =", user.email), model.WpPledge.all().filter("email =", user.email) ): if (pledge.amountCents or 0) >= biggest_amount: biggest_pledge = pledge biggest_amount = pledge.amountCents or 0 if biggest_pledge is None: self.error(404) self.response.write("user not found") return stripe.api_key = env.get_env().stripe_backend.stripe_private_key cus = stripe.Customer.retrieve(biggest_pledge.stripeCustomer) if len(cus.cards.data) == 0: self.error(404) self.response.write("user not found") return if user.first_name or user.last_name: # TODO(jt): we should backfill this information user_name = "%s %s" % (user.first_name or "", user.last_name or "") else: user_name = cus.cards.data[0].name zip_code = cus.cards.data[0].address_zip self.response.headers["Content-Type"] = "application/javascript" self.response.write( json.dumps( { "user": { "name": user_name, "email": user.email, "pledge_amount_cents": biggest_amount, "zip_code": zip_code, } } ) )
def get(self, url_nonce): util.EnableCors(self) user = model.User.all().filter('url_nonce =', url_nonce).get() if user is None: self.error(404) self.response.write('user not found') return # maybe we should do sum instead? biggest_pledge = None biggest_amount = 0 for pledge in itertools.chain( model.Pledge.all().filter('email =', user.email), model.WpPledge.all().filter('email =', user.email)): if (pledge.amountCents or 0) >= biggest_amount: biggest_pledge = pledge biggest_amount = (pledge.amountCents or 0) if biggest_pledge is None: self.error(404) self.response.write("user not found") return cc_user_name = None cc_zip_code = None stripe.api_key = env.get_env().stripe_backend.stripe_private_key if biggest_pledge.stripeCustomer: cus = stripe.Customer.retrieve(biggest_pledge.stripeCustomer) if len(cus.sources.data) > 0: cc_user_name = cus.sources.data[0].name cc_zip_code = cus.sources.data[0].address_zip if user.first_name or user.last_name: # TODO(jt): we should backfill this information user_name = "%s %s" % (user.first_name or "", user.last_name or "") else: user_name = cc_user_name zip_code = cc_zip_code self.response.headers['Content-Type'] = 'application/javascript' self.response.write( json.dumps({ "user": { "name": user_name, "email": user.email, "pledge_amount_cents": biggest_amount, "zip_code": zip_code } }))
def __init__(self, path=None, xml=None, qd_lim=None): """ Create a new robot model. INPUT: - ``path`` -- path to the COLLADA model of the robot - ``xml`` -- (optional) string in OpenRAVE XML format - ``qd_lim`` -- maximum angular joint velocity (in [rad] / [s]) """ assert path is not None or xml is not None name = basename(splitext(path)[0]) if xml is None: xml = Robot.__default_xml % (path, name) env = get_env() env.LoadData(xml) rave = env.GetRobot(name) nb_dofs = rave.GetDOF() set_default_background_color() # [dirty] reset by LoadData q_min, q_max = rave.GetDOFLimits() rave.SetDOFVelocities([0] * nb_dofs) rave.SetDOFVelocityLimits([1000.] * nb_dofs) if qd_lim is None: qd_lim = 10. # [rad] / [s]; this is already quite fast qd_max = +qd_lim * ones(nb_dofs) qd_min = -qd_lim * ones(nb_dofs) self.active_dofs = None self.has_free_flyer = False self.ik = None # created by self.init_ik() self.ik_lock = None self.ik_thread = None self.is_visible = True self.mass = sum([link.GetMass() for link in rave.GetLinks()]) self.nb_dofs = nb_dofs self.q_max = q_max self.q_max.flags.writeable = False self.q_max_active = None self.q_min = q_min self.q_min.flags.writeable = False self.q_min_active = None self.qd_max = qd_max self.qd_max_active = None self.qd_min = qd_min self.qd_min_active = None self.qdd_max = None # set in child class self.rave = rave self.tau_max = None # set by hand in child robot class self.transparency = 0. # initially opaque
def draw_arrow(p1, p2, color='r', linewidth=0.02): """ Draw an arrow between two points. INPUT: - ``p1`` -- 3D coordinates of the origin of the arrow - ``p2`` -- 3D coordinates of the end of the arrow - ``color`` -- (default: 'r') matplotlib color letter or RGB triplet - ``linewidth`` -- thickness of force vector OUTPUT: And OpenRAVE handle. Must be stored in some variable, otherwise the drawn object will vanish instantly. """ if type(color) is str: color = _matplotlib_to_rgb(color) return get_env().drawarrow(p1, p2, linewidth=linewidth, color=color)
def draw_line(p1, p2, color='g', linewidth=1.): """ Draw a line between points p1 and p2. INPUT: - ``p1`` -- one end of the line - ``p2`` -- other end of the line - ``color`` -- (default: 'g') matplotlib color letter or RGB triplet - ``linewidth`` -- thickness of drawn line OUTPUT: And OpenRAVE handle. Must be stored in some variable, otherwise the drawn object will vanish instantly. """ if type(color) is str: color = _matplotlib_to_rgb(color) return get_env().drawlinelist( array([p1, p2]), linewidth=linewidth, colors=color)
def init_components(args, unknown_args) -> Tuple[Env, Agent]: # Initialize environment env = get_env(args.env, **unknown_args) # Get model class if args.model is not None: model_cls = model_registry.get(args.model) else: env_type = _get_gym_env_type(args.env) if env_type == 'atari': model_cls = model_registry.get('qcnn') elif env_type == 'classic_control': model_cls = model_registry.get('qmlp') else: raise NotImplementedError( f'No default model for environment: {args.env!r})') # Initialize agent agent_cls = agent_registry.get(args.alg) agent = agent_cls(model_cls, env.get_observation_space(), env.get_action_space(), args.agent_config, **unknown_args) return env, agent
def draw_force(p, f, scale=0.005, color='r', linewidth=0.015): """ Draw a force acting at a given point. INPUT: - ``p`` -- point where the force is acting - ``f`` -- force vector - ``color`` -- (default: 'r') matplotlib color letter or RGB triplet - ``scale`` -- scaling factor between Euclidean and Force spaces - ``linewidth`` -- thickness of force vector OUTPUT: And OpenRAVE handle. Must be stored in some variable, otherwise the drawn object will vanish instantly. """ if type(color) is str: color = _matplotlib_to_rgb(color) f_scale = scale * f if dot(f_scale, f_scale) < 1e-6: return None return get_env().drawarrow( p, p + f_scale, linewidth=linewidth, color=color)
self.response.write('Command started.') def _get_cmd(self): if 'cmds' not in self.app.registry: self.app.registry['cmds'] = {} if cmd_cls.SHORT_NAME not in self.app.registry['cmds']: self.app.registry['cmds'][cmd_cls.SHORT_NAME] = cmd_cls( self.app.config) return self.app.registry['cmds'][cmd_cls.SHORT_NAME] return ('/admin/command/' + cmd_cls.SHORT_NAME, H) COMMAND_HANDLERS = [MakeCommandHandler(c) for c in commands.COMMANDS] app = webapp2.WSGIApplication([ ('/admin/generate/pledge_amounts.csv', GeneratePledgeAmountsCsvHandler), ('/admin/generate/pledges.csv', GeneratePledgesCsvHandler), ('/admin/generate/pledges_full_data.csv', GeneratePledgesFullDataCsvHandler), ('/admin/files(.+)/pledge_amounts.csv', CsvHandler), ('/admin/files(.+)/pledges.csv', CsvHandler), ('/admin/files(.+)/pledges_full_data.csv', CsvHandler), ('/admin/pledges_export', PledgesExportHandler), ('/admin/pledges_export/pledges.json', PledgesExportJSONHandler), ('/admin/stretch', StretchHandler), ('/admin/?', AdminDashboardHandler), ] + COMMAND_HANDLERS, debug=False, config=dict(env=env.get_env()))
logging.config.fileConfig('logging.ini') from apscheduler.triggers.cron import CronTrigger from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler from env import get_env from server import AuthServer from dump import dump_database from slack import send_slack_message logger = logging.getLogger('ddgscheduler') if __name__ == "__main__": environment = get_env() if environment.get('TEST'): import time time.sleep(10) dump_database(environment) time.sleep(300) else: if environment.get('START_MANUAL_MANAGEMENT_SERVER'): dumper_scheduler = BackgroundScheduler() else: dumper_scheduler = BlockingScheduler() dumper_scheduler.add_job(lambda: dump_database(environment), CronTrigger.from_crontab(environment.get('CRON'))) dumper_scheduler.start()
to be executed. """ class H(webapp2.RequestHandler): def get(self): self.response.write(""" <h1>You are about to run command "{}". Are you sure?</h1> <form action="" method="POST"> <button>Punch it</button> </form>""".format(self._get_cmd().NAME)) def post(self): deferred.defer(self._get_cmd().run) self.response.write('Command started.') def _get_cmd(self): if 'cmds' not in self.app.registry: self.app.registry['cmds'] = {} if cmd_cls.SHORT_NAME not in self.app.registry['cmds']: self.app.registry['cmds'][cmd_cls.SHORT_NAME] = cmd_cls(self.app.config) return self.app.registry['cmds'][cmd_cls.SHORT_NAME] return ('/admin/command/' + cmd_cls.SHORT_NAME, H) COMMAND_HANDLERS = [MakeCommandHandler(c) for c in commands.COMMANDS] app = webapp2.WSGIApplication([ ('/admin/pledges.csv', PledgesCsvHandler), ('/admin/?', AdminDashboardHandler), ] + COMMAND_HANDLERS, debug=False, config=dict(env=env.get_env()))
def main(): main_arg_parser = argparse.ArgumentParser(description="The PrAE Learner") subparsers = main_arg_parser.add_subparsers(title="subcommands", dest="subcommand") train_arg_parser = subparsers.add_parser("train", help="parser for training") train_arg_parser.add_argument("--epochs", type=int, default=200, help="the number of training epochs") train_arg_parser.add_argument("--batch-size", type=int, default=32, help="size of batch") train_arg_parser.add_argument("--seed", type=int, default=1234, help="random number seed") train_arg_parser.add_argument( "--device", type=int, default=0, help="device index for GPU; if GPU unavailable, leave it as default") train_arg_parser.add_argument( "--dataset", type=str, default="/home/chizhang/Datasets/RAVEN-10000/", help="dataset path") train_arg_parser.add_argument("--config", type=str, default="distribute_four", help="the configuration used for training") train_arg_parser.add_argument("--checkpoint-dir", type=str, default="./runs/ckpt/", help="checkpoint save path") train_arg_parser.add_argument("--save-dir", type=str, default="./runs/save/", help="final model save path") train_arg_parser.add_argument("--log-dir", type=str, default="./runs/log/", help="log save path") train_arg_parser.add_argument("--img-size", type=int, default=32, help="image region size for training") train_arg_parser.add_argument("--lr", type=float, default=0.95e-4, help="learning rate") train_arg_parser.add_argument( "--weight-decay", type=float, default=0.0, help="weight decay of optimizer, same as l2 reg") train_arg_parser.add_argument( "--error", type=str, default="JSD", help="error used to measure difference between distributions") train_arg_parser.add_argument("--num-workers", type=int, default=2, help="number of workers for data loader") train_arg_parser.add_argument("--resume", type=str, default=None, help="resume from a initialized model") train_arg_parser.add_argument("--aux", type=float, default=1.0, help="weight of auxiliary training") test_arg_parser = subparsers.add_parser("test", help="parser for testing") test_arg_parser.add_argument("--batch-size", type=int, default=32, help="size of batch") test_arg_parser.add_argument( "--device", type=int, default=0, help="device index for GPU; if GPU unavailable, leave it as default") test_arg_parser.add_argument("--dataset", type=str, default="/home/chizhang/Datasets/RAVEN-10000", help="dataset path") test_arg_parser.add_argument("--config", type=str, default="distribute_four", help="the configuration used for testing") test_arg_parser.add_argument("--model-path", type=str, required=True, help="path to a trained model") test_arg_parser.add_argument("--img-size", type=int, default=32, help="image region size for training") test_arg_parser.add_argument( "--error", type=str, default="JSD", help="error used to measure difference betweeen distributions") test_arg_parser.add_argument("--shuffle", type=int, default=0, help="whether to shuffle the dataset") test_arg_parser.add_argument("--num-workers", type=int, default=2, help="number of workers for data loader") args = main_arg_parser.parse_args() args.cuda = torch.cuda.is_available() device = torch.device("cpu") # device = torch.device("cuda:{}".format(args.device) if args.cuda else "cpu") env = get_env(args.config, device) if env is None: print("ERROR: Unsupported environment") sys.exit(1) if args.subcommand is None: print("ERROR: Specify train or test") sys.exit(1) if args.subcommand == "train": check_paths(args) train(args, env, device) elif args.subcommand == "test": test(args, env, device) else: print("ERROR: Unknown subcommand") sys.exit(1)
if __name__ == "__main__": import sys from presets import preset preset.load_default() exp_settings = preset.experiment env_settings = preset.env env_settings.enable_rendering = True for i in range(1, len(sys.argv) - 1): preset.load_custom(sys.argv[i]) preset.load_env_override() checkpoint = sys.argv[-1] from env import get_env test_env = get_env(exp_settings.env)(seed=0, evaluate=True) from algorithm import algorithm_bundle_dict algorithm_name = preset.experiment.algorithm model_class = algorithm_bundle_dict[algorithm_name].model_class from model import model_dict load_model = model_dict[model_class].loader model = load_model(checkpoint) test_model(test_env, model, exp_settings.record_motion)
def draw_points(points, color='g', pointsize=0.05): if type(color) is str: color = _matplotlib_to_rgba(color, alpha=1.) return get_env().plot3( array(points), pointsize=pointsize, drawstyle=1, colors=color)
import sys from presets import preset preset.load_default() exp_settings = preset.experiment env_settings = preset.env env_settings.enable_rendering = True for i in range(1, len(sys.argv) - 1): preset.load_custom(sys.argv[i]) preset.load_env_override() checkpoint = sys.argv[-1] from env import get_env test_env = get_env(exp_settings.env)(seed=0, checkpoint=checkpoint, evaluate=True) from algorithm import algorithm_bundle_dict algorithm_name = preset.experiment.algorithm model_class = algorithm_bundle_dict[algorithm_name].model_class from model import model_dict load_model = model_dict[model_class].loader model = load_model(checkpoint) test_model(test_env, model, exp_settings.record_motion)
def remove(self): """Remove body from OpenRAVE environment.""" env = get_env() with env: env.Remove(self.rave)
def rl2d(config: dict, run_args: dict) -> VAEExperiment: run_config = config['run_params']['config'] env = get_env(run_config['env_config']['name']) run_config = {**run_config, 'framework': 'torch', 'env': env} config['run_params']['config'] = run_config tune.run(config['algorithm'], loggers=[TBXLogger], **config['run_params'])
json.dumps( { "user": { "name": user_name, "email": user.email, "pledge_amount_cents": biggest_amount, "zip_code": zip_code, } } ) ) def options(self): util.EnableCors(self) app = webapp2.WSGIApplication( [ ("/total", GetTotalHandler), (r"/user-update/(\w+)", UserUpdateHandler), (r"/user-info/(\w+)", UserInfoHandler), ("/campaigns/may-one/?", EmbedHandler), ("/contact.do", ContactHandler), # See wp_import # ('/import.do', wp_import.ImportHandler), ] + handlers.HANDLERS, debug=False, config=dict(env=env.get_env()), )
import time import board import neopixel import math import env e = env.get_env() COUNTDOWN_DELAY = e['COUNTDOWNDELAY'] # Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 # NeoPixels must be connected to D10, D12, D18 or D21 to work. pixel_pin = board.D18 # The number of NeoPixels num_pixels = 20 # The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! # For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. ORDER = neopixel.GRB DEFAULT_BRIGHTNESS = 0.2 pixels = neopixel.NeoPixel(pixel_pin, num_pixels, brightness=DEFAULT_BRIGHTNESS, auto_write=False, pixel_order=ORDER) rainbow_count = 0 def wheel(pos):
def draw_polyhedron(points, combined='g-#', color=None, faces=None, linewidth=1., pointsize=0.01, hull=None): """ Draw a polyhedron defined as the convex hull of a set of points. INPUT: - ``points`` -- list of 3D points - ``combined`` -- (default: 'g-#') drawing spec in matplotlib fashion: color letter followed by faces to draw (see ``color`` and ``faces``) - ``color`` -- color letter or RGBA tuple - ``faces`` -- string of symbols indicating the faces of the polyhedron to draw: use '.' for vertices, '-' for edges and '#' for facets - ``hull`` -- used in the 2D case where the hull has zero volume - ``linewidth`` -- line thickness in meters - ``pointsize`` -- point size in meters OUTPUT: And OpenRAVE handle. Must be stored in some variable, otherwise the drawn object will vanish instantly. """ is_2d = hull is not None if color is None: color = combined[0] if faces is None: faces = combined[1:] if type(color) is str: color = _matplotlib_to_rgba(color) if hull is None: try: hull = ConvexHull(points) except QhullError: warn("QhullError: maybe polyhedron is empty?") return [] vertices = array([points[i] for i in hull.vertices]) points = array(points) color = array(color if color is not None else (0.0, 0.5, 0.0, 0.5)) handles = [] env = get_env() if '-' in faces: # edges edge_color = color * 0.7 edge_color[3] = 1. edges = vstack([[points[i], points[j]] for s in hull.simplices for (i, j) in itertools.combinations(s, 2)]) edges = array(edges) handles.append(env.drawlinelist( edges, linewidth=linewidth, colors=edge_color)) if '#' in faces: # facets if is_2d: nv = len(vertices) indices = array([(0, i, i + 1) for i in xrange(nv - 1)], int64) handles.append(env.drawtrimesh(vertices, indices, colors=color)) else: indices = array(hull.simplices, int64) handles.append(env.drawtrimesh(points, indices, colors=color)) if '.' in faces: # vertices color[:3] *= 0.75 color[3] = 1. handles.append(env.plot3( vertices, pointsize=pointsize, drawstyle=1, colors=color)) return handles