Skip to content

Instantly share code, notes, and snippets.

View zh4ngx's full-sized avatar

Andy Zhang zh4ngx

  • San Francisco, CA
View GitHub Profile
@zh4ngx
zh4ngx / binary_tree_marshal.py
Created October 10, 2013 02:42
Function to marshal binary tree (represented as dict). Done kind of in a hurry (like 20m)....
# Arbitary binary tree (integers)
### 1
### 2 3
### 6 4 5
## {1:{"left":2,"right":{3:{"left":4,"right":5}}}}
## Goal: -> '{1:{left:2,right:{3:{left:4,right:5}}}}'
# 1
# 2 3
## '{1:{left:2,right:3}}'
@zh4ngx
zh4ngx / lexical_closure.go
Created October 28, 2013 08:22
Quick demo/reminder of lexical closures (in Go)
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
func main() {
fmt.Println("Hello, 世界")
f := who()
g := who()
@zh4ngx
zh4ngx / interview_debug.py
Last active December 28, 2015 17:39
Simple interview question to test basic debugging & communication skills. May require a live person to answer questions (particularly if the candidate doesn't know Python). TODO: rewrite this in a few languages and put in a repo.
def foo(a, b):
"""Please document me.
What am I supposed to do?
Rename anything you see fit.
Also, I think I have bugs. Please debug me.
"""
bar = {}
for x in a:
bar[x] = True
@zh4ngx
zh4ngx / karabiner-import.sh
Created February 10, 2015 19:09
Quick settings for OS X Karabiner
#!/bin/sh
cli=/Applications/Karabiner.app/Contents/Library/bin/karabiner
$cli set general.dont_remap_internal 1
/bin/echo -n .
$cli set remap.commandL2optionL 1
/bin/echo -n .
$cli set general.dont_remap_apple_pointing 1
/bin/echo -n .
@zh4ngx
zh4ngx / hill_climb_1
Created July 1, 2017 01:39
Basic Hill Climb with variance reduction for MC Policy Evaluation
import gym
import numpy as np
from gym.wrappers.monitoring import Monitor
env = gym.make('CartPole-v0')
env = Monitor(env, 'tmp/cart-pole-hill-climb-1', force=True)
print("Action space: {0}".format(env.action_space))
print("Observation space: {0}\n\tLow: {1}\n\tHigh: {2}".format(
env.observation_space,
@zh4ngx
zh4ngx / hill_climb_2.py
Created July 1, 2017 02:34
CartPole-v0 Hill Climb with MC(10) Eval + Simulated Annealing
import gym
import numpy as np
from gym.wrappers.monitoring import Monitor
env = gym.make('CartPole-v0')
env = Monitor(env, 'tmp/cart-pole-hill-climb-2', force=True)
print("Action space: {0}".format(env.action_space))
print("Observation space: {0}\n\tLow: {1}\n\tHigh: {2}".format(
env.observation_space,
@zh4ngx
zh4ngx / hill_climb_3.py
Created July 1, 2017 03:20
CartPole-v0 Hill Climb + MC(10) + Gaussian Noise (Sigma 0.5)
import gym
import numpy as np
from gym.wrappers.monitoring import Monitor
MC_POLICY_EVAL_EP = 10
BASE_NOISE_FACTOR = 0.5
NUM_POLICY_EVAL = 500
env = gym.make('CartPole-v0')
@zh4ngx
zh4ngx / hill_climb_4
Created July 1, 2017 03:53
CartPole - Hill Climb v4 - Correct hill climb and reduced noise & variance (MC-10)
import gym
import numpy as np
from gym.wrappers.monitoring import Monitor
MC_POLICY_EVAL_EP = 10
BASE_NOISE_FACTOR = 0.1
NUM_POLICY_EVAL = 500
env = gym.make('CartPole-v0')
env = Monitor(env, 'tmp/cart-pole-hill-climb-4', force=True)
@zh4ngx
zh4ngx / cart_pole_cem_2.py
Created July 4, 2017 01:28
CartPole-v0 Cross Entropy Method with no bias
# Source: http://rl-gym-doc.s3-website-us-west-2.amazonaws.com/mlss/lab1.html
import gym
import numpy as np
from gym.spaces import Discrete, Box
from gym.wrappers.monitoring import Monitor
# ================================================================
# Policies
# ================================================================
@zh4ngx
zh4ngx / cart_pole_cem_1
Created July 4, 2017 01:35
CartPole-v0 Cross Entropy Method - Affine Function and Base HyperParams
# Source: http://rl-gym-doc.s3-website-us-west-2.amazonaws.com/mlss/lab1.html
import gym
import numpy as np
from gym.spaces import Discrete, Box
from gym.wrappers.monitoring import Monitor
# ================================================================
# Policies
# ================================================================