Thursday, August 31, 2017

Django and more Python

I am interviewing for a Python developer position. I always find my knowledge gap or shortage. I will not stop learning.

Django

With Django, you can take Web applications from concept to launch in a matter of hours.
part 1
source activate py3
conda install django
python -m django —version    # 1.11.4
django-admin startproject mysite    # initailize a project by creating manage.py and mysite folder with starting codes
cd mysite
python manage.py runserver    # create db.sqlite3
http://127.0.0.1:8000/
python manage.py startapp polls  # create polls folder
cd polls
# change text in vews.py
touch urls.py  # create file and change text
cd ..
touch urls.py # create file and change text
part 2
python manage.py migrate  # install apps
cd polls
subl models.py  # add models
cd ../mysite
subl setting.py # add 'polls.apps.PollsConfig' to apps
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate
python manage.py shell
# play with API
python manage.py createsuperuser
Username: admin
password: j*89*****
cd polls
subl admin.py # make poll app modifiable in the admin

Difference between function and generator

star sign before a variable name?

def foo(x,*y, **z):
  print(x)
  print(y)
  print(z)
foo(1,2,3, a=4,b=5)
lis = [2,3]
dic = {'a':4, 'b':5}
foo(1,*lis,**dic)
Inside a function header:
* collects all the positional arguments in a tuple
** collects all the keyword arguments in a dictionary
In a function call:
* unpacks an list or tuple into position arguments
** unpacks an dictionary into keyword arguments

extract lines begin with “error: “

result = [l for l in open(“input.txt”) if l.startswith(“error: “)]

try/execept handling

x = 1
y = 0
z = 0
result = 2
try:
    z= z+1  # 1
    result = x/y
except ZeroDivisionError:
    z = z + result #3
else:
    z= z+1  # if no error occurs
finally:
    z = z+1 #4
z = z+1  #5
print(z)

try:
    fuck
except NameError as e: # or Exception
    print("watch your language")
finally:
    print("go home")

sort

a = [-1,-4,2]
a.sort()  # inplace 
sorted(a, key = abs)

separate string into partitioned tuple

"hello".partition("e")

Python generator vs python function

generator save state at yield, so the result will keep going for next call unless it is exhausted.
import math
def get_primes(number):
    while True:
        if is_prime(number):
            yield number
        number += 1

def is_prime(number):
    if number > 1:
        if number == 2:
            return True
        if number % 2 == 0:
            return False
        for current in range(3, int(math.sqrt(number) + 1), 2):
            #print(current)
            if number % current == 0: 
                return False
        return True
    return False

a = get_primes(2)
a.__next__()

split vs rsplit

rsplit is from the right

class method

class vehicles:
    count = 0
    def __init__(self,value):
        self.value = value
        vehicles.count +=1
    def getval(self):
        return self.value
    def getcount(cls):
        return vehicles.count
    counter = classmethod(getcount)

type1 = vehicles("car")
type2 = vehicles("Bus")
type3 = vehicles("bike")

print(type1.getval(),type2.getval(),vehicles.counter(),type2.getcount()) 
# car Bus 3 3

function decorator

Decorator is a handy way to wrap a function so it temporaly becomes an embedded function.
class Foo():
    @classmethod
    def class_foo(cls):
        print("class: %s" % cls)
Foo.class_foo()   
# equals to
class Foo():
    def class_foo(cls):
        print("class: %s" % cls)
    class_foo = classmethod(class_foo)    
Foo.class_foo()
another example
def my_decorator(some_function):
    def wrapper():
          print("before some_function() is called.")
        some_function()
        print("after some_function() is called.")
    return wrapper

@my_decorator
def just_some_function():
    print("Wheee!")
just_some_function()

zip()

The syntax of zip() is:
zip(*iterables)
example
coordinate = ['x', 'y', 'z']
value = [3, 4, 5, 0, 9]

result = zip(coordinate, value)
resultList = list(result)
print(resultList)

c, v =  zip(*resultList)
print('c =', c)
print('v =', v)

unicode

unicode

The default encoding for python 2 is ASCII, for python 3 is UTF-8, Unicode Transformation Format 8 bit.
for i in range(256):
    if chr(i)== 'A' or chr(i) == 'a' or chr(i)=='0':
        print("attentions!")
    print(i,":",chr(i),end=" ")
chr() and ord() is for ASCII decoding and encoding.
so char ‘0’-‘9’ are encoded as 48-57; char ‘A’-‘Z’ : 65-90, char ‘a’-‘z’: 97-122. Actually, the first 0-127 characters (7 bits) are the same with ASCII defined in 1968, which is Amercian Standard.
UTF-8 is elastic encoding, that each character is represented by 1 to 4 bytes, depending on its belonging. https://en.wikipedia.org/wiki/UTF-8#Description. It is capable of encoding 1,112,064 valid code points in Unicode. (17*2^16-2048, or 0x10FFFF)

中文编码

标准中文电码(Chinese Telegraphic/Commercial code): (http://code.usvisa-application.com/) is 4 digits, from 0000 to 9999.
中国国家标准:
GB2312: in 1981, use 2 bytes, 1st byte(0xA1-0xFE), 2nd byte (0xA1-0xFE), so 83x93= 8000, enough for 3755+3008 = 6763 characters.
GBK/GB13030: use 2 bytes, 2nd byte can be (0x00-0xFF), so 86x256 = 22016
Unicode 10.0: example link , has 87, 882 CJK Unified Ideographs, out of totally 136,755 characters. In unicode 1.01, out of total 28,359 characters, 20,914 is occupied by CJK Ideographs.
Unicode has 17 planes, each plan is 2 bytes, or 2^16= 65,536 coding points.
It requires 3 k characters for general literacy, but 40 k characters for reasoanably complete coverage.
Source separate principle. The term ideograph may be misleading because Chinese script is not strictly a picuture writing system.
For a freqently-used Chinese Character, Unicode uses 2 bytes, UTF-8 uses 3 bytes.

bytes vs string

Bytes are bytes; characters are an abstraction. An immutable sequence of Unicode is called a string. An immutable sequence of numbers (0-256) is called a bytes objects. Each byte within the byte literal can be an ascii character or an encoded hexadecimal number from \x00 to \xff (0–255).
In python 3, string is default unicoded, so no need to use u'literal'.
You can’t mix bytes and strings. (TypeError)
# http://graphemica.com/unicode/characters/page/80
u"\u0041"  # 'A'
u"\u4dff"  # '䷿'
u"\u4E00"  # '一'

b = b'abc' # bytes, immutable
barr = betearray(b) # bytearray, mutable
barr[0] = 102 #  = \x66 = f
barr  # bytearray(b'fbc')

# try different encoding
'一'.encode('raw_unicode_escape')  # b'\\u4e00'
u'哈'.encode('utf8')  # b'\xe5\x93\x88'
u'哈'.encode('gbk')   # b'\xb9\xfe'
u'哈'.encode('big5')  # b'\xab\xa2'

Tuesday, August 29, 2017

Robot ND A1, Mapping

2 terms, 3 months/term. 1st cohort begins at 2017-5-8
time dedication: 15 hours/week
hiring partner: Bosch, Kuka, Lockheed Martin, iRobot, Uber ATG, and Alphabet’s Moonshot Factory, X
Term 1:
  1. search and sample return
  2. turlesim in ROS, kinematics mechanics to pick and place. https://www.amazonrobotics.com/#/roboticschallenge
  3. 3D perception: point cloud library, random sample consensus, PR2 simulator, MoveIt packages
  4. controls
  5. deep learning: follow me
Term 2:
  1. Motion planning
  2. Localization
  3. hardware integration

Robot:
  1. perception
  2. decision making
  3. action/actuation
The first project is a quick demo of these 3 steps, in which perception is the major focus. You will use computer vision to segment image into grounds, obstacles, and rocks by using color threshold.
The major challenge is to spend some time to digest the codes that connect each step together.

NASA Centennial Challenge

NASA space competition inducement prize contests for non-government funded technological achievements by American teams.
The prize contests are named “Centennial” in honor of the 100 years since the Wright brothers‘ first flight in 1903.
A key advantage of prizes over traditional grants is that money is only paid when the goal is achieved.
One of the challenges is sample return robot challenge, partner with WPI. It has hosted 5 annual events from 2012 to 2016. Team Mountaineers from West Virginia University led by Yu Gu completed both level 1 and level 2 challenges.
Yu Gu, WVU Mechanical, and Aerospace Engineering. The project was mechanical, electrical, programming, computer vision, navigation, a lot of different things.

Project 1: autonomous navigation and mapping

Unity simulator, more photorealistic image quality
  • move and steer: arrow key/awsd
  • change perspective: Tab
  • zoom: mouse scroll
  • pick up: right mouse/ enter
  • grid: G key. A grid squares on the ground = 1 meter square
  • record; r key
Gazebo simulator, powerful physics engine, seamless integration with ROS.
# show 2 images side by side
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(21, 7), sharey=True)
f.tight_layout()
ax1.imshow(image)
ax1.set_title('Original Image', fontsize=40)

ax2.imshow(colorsel, cmap='gray')
ax2.set_title('Your Result', fontsize=40)
plt.subplots_adjust(left=0., right=1, top=0.9, bottom=0.)
#plt.show() # Uncomment if running on your local machine
The remote server has indicated you are using invalid credentials for this channel.
anaconda logout
conda env create -f environment.yml
create a RoboND env with 1.38 GB
python drive_rover.py which will be the back-end artificial intelligence to run the simulator in “autonomous mode”.

Saturday, August 26, 2017

Artificial Intelligence A3, planning

Note: Due to time constraint, I have to pause AIND at the planning section. If I have time in the future, I may restore my study in the following:
  1. pac-man lab
  2. simulated annealing lab
  3. airport transportation planning project.

Peter Norvig: AI is all about figuring out what to do when you don’t know what to do. Regular programming is writing instructions to make the computer do what you want when you do know what to do. In AI, it’s for when you don’t know. search is one of key areas where we can figure out what to do by planning a sequence of steps, even when we have no idea what the first step should be until we solve the search problem.
  • breadth first search (shortest)
  • uniform cost search (cheapest)
  • depth first search (least storage space, fastest computing time)
Also called best estimated total path cost first
only expanse the minimum value. f = g+h
g(path) = path cost so far
h(path) = h(s) = estimated distance to goal = straigh line distance
g is to keep the path short, h is to keep us focus on the goal.
A search finds the lowest cost path if *h(s) < true cost. h is optimistic.
A problem solver that uses a heuristic function given to it really is not intelligent at all.
crossing out parts of the rules is called generating a relaxed problem.
searching is one of the most fundamental methods of solving problems in AI.
Peter: To me, AI is programming a computer to do the right thing when you don’t know for sure the right thing is.
The book was successful because of a fortuitous accident of timing. we happened to come along just as AI was making this transition from Boolean logic to probability and from handcarfted knowledge to machine learning.

lab: pac man

simulated annealing

do the stupid thing first, and only add intelligence when necessary.
global optimum: an element of randomness is what you need
  • random restart
  • genetic algorithms
  • simulated annealing
taboo search: it is taboo if somewhere you have already been.
stage algorithm: keep a list of all local maximum and try to get the general shape of the problem,try to predict where a new maximum might be, given the areas I haven’t explored yet.
local beam search/ stochastic beam search: keep track of k particles. At each time frame, we randomly generate neighbors of these particles and keep the best ones for the next iteration. There are messages sharing between these particles.
genetic algorithm is a fancy version of beam search that happens to make a nice analogy to biology. some people call it the 2nd best solution to any problem.
We’ll see these ideas repeatedly in aI, in techniques like particle filters, pattern recognition with hidden Markov models, and Monte Carlo Markov chains. Sometimes let them go and embrace randomness is the key to the solution.

lab

Follow the instructions to implement Simulated Annealing from AIMA

constraint satisfaction

techniques:
  • backtracking
  • forward checking
  • minimum remaining values
  • least constraining value
problems with preference constraints are called constraint optimization problems and are with solving linear programming.
Examples:
  • car assembly
  • job scheduling in factories
  • class scheduling
  • spreadsheets
  • transportation scheduling
  • floor planning
Constraint Satisfaction Problems by solving the N-queens problem using symbolic constraints and backtracking search: https://github.com/udacity/AIND-Constraint_Satisfaction
complete the constraints for the N-Queens CSP and implement Backtracking-Search from AIMA (chapter 6)

logic

AI started in the 50s~70s with the idea that what we needed was better language and systems for writing down logical statements. Once we have done enough of them, we should be able to do anything of them. Some progress but 2 major issues:
  1. the world is uncertain. We didn’t get good algorithm to deal with probability until mid 1980s
  2. too laborious to try to write down everything by hand.
Today, logic is still useful in many domains where we can eliminate much of the uncertainty of real world and solve significant problems. Like FedEx planning all the deliveries of its packages.
Expert system.
future of planning algorithms:
  • Learning from examples would be an important area.
  • transfer learning across domain.
  • interactive planning
  • explainable, adaptable.
value iteration is a key concept for Markov decision processing.
  • first order logic: relation , object,function
  • propositional logic: facts
  • probability theory

planning

Blindfold Hiker Test: We can’t just plan ahead and come up with a whole plan. We need feedback from environment. We have to interweave planning and execution:
  • environment is stochastic.
  • multiagent
  • partial observability
  • unkown/imcomplete knowledge
  • hierarchical
When we start, we only have the high level parts of the plan, then during execution that we schedule the rest of low level parts of the plan.
sensorless planning in a deterministic world.
observation updates our believe state. Observation alone can’t create new states, but can partition them into pieces.

project

implement my_air_cargo_problems.py and test it by python run_search.py -p 1 -s 5 in which 5 is uniform cost search/ best_first_graph_search.
my_planning_graph.py
hierachy structure of code:
void main (vector<int> p, vector<int> s){
  void run_search(Problem p, Search_func(),para){
    void show_solution()
  }
}
FluentState(vector<Expr>,vector<Expr>)

string encode_state(FluentState fs,vector<Expr> fluent_map){}  // state 

AirCargoProblem(vector<string> cargos, vector<string> planes, vector<string> airports, FluentState init, vector<Expr> goal){
  vector<Action> get_action()
}

Action(Expr action,array<vector<Expr>,2> precond, array<vector<Expr>,2> effect)
-> represent return type, : represent input type.
expr('At(C1, SFO)') is an Expr object.
precondition is only very small part of the whole initial state.
what next?:
  • try single Action and Expr, see how it works
  • try understand various planning algorithms
submit requirement when udacity submit
  • my_air_cargo_problems.py
  • my_planning_graph.py
  • heuristic_analysis.pdf (if present)
  • research_review.pdf (one-page report on three of these developments, highlighting the relationships between the developments and their impact on the field of AI as a whole. Appropriate sources (such as books or magazine or journal articles) should be cited. There’s a lot of resources at the end of AIMA chapter 10 )

AIMA

codes: aima.cs.berkeley.edu

AIMA chapter 2, Intelligent Agents

In a sense, all areas of engineering can be seen as designing artifacts that interact with the world; AI operates at the most interesting end of the spectrum, where the artifacts have signficant computational resources and the task environment requires nontrivial decision making.
As a general rule, it is better to design performance measures according to what one actually wants in the environment, rather than according to how one thinks the agent should behave.
which is better— a reckless life of highs and lows, or a safe but humdrum existence?
rational agent: given percept sequence and built-in knowledge it has, select an action that is expected to maximize its performance measure.
Rationality maximizes expected performance, while perfection maximizes actual performance. The latter is impossible unless you have a crystall ball or time machine.
autonomous: an agent not only relies on its prior knowledeg of its designer; it can use its own percepts to learn and compensate for partial or incorrect prior knowledge.
Task environment: performance, environment, actuatoors, sensors.
“percept,action” table-driven approach is doomed to failture due to super large number of entries, say, 10^{250B} much larger than 10^{80} (number of atoms in the observable universe).
4 kinds of agent programs:
  1. Simple reflex agents: condition-action rule
  2. Model-based. handle partial observability: maintain internal state. The model of “how the world works”: how the world evolves independently of the agent, how the agent’s own actions affect the world. Uncertainty about the current state is unavoidable, but the agent still has to make a decision—best guess.
  3. Goal-based: external performance measure, crude binary distinction between happy and unhappy states.
  4. Utility-based: use an internal utility function to measure goals in greater details. If there are conflicting goals (e.g. speed and safety), the utility function specifies the appropriate tradeoff. when uncertainty, provides a way in which the likelihood of success can be weighted against the importance of the goals.
Any rational agent must behave as if it possesses a utility function whose expected value it tries to maximize.
learning agent:
  1. performance element: responsible for selecting external actions.
  2. learning element: responsible for making improvements
  3. critic: a fixed performance standard. It can be thought as outside the agent because the agent must not modify it to fit its own standard.
  4. probelm generator: suggest new exploratory actions to discover much better actions for the long run.
state representations:
  • atomic: inside is an identical black box. Algorithms: search, game-playing, hidden markov models, markov decision process
  • factored: splits up each state into a fixed set of key-value pairs. Algorithms: constraint satisfaction, propositional logic, planning, Bayesian networks, machine learning.
  • structured: having thins in it that are related to each other, like a class object. Algorithms: relational databases, first-order logic, first-order probablity models, knowledge-based learning, natural language processing.
More expressive representation is much more concise.

AIMA chapter 10, classical planning

fully observable, deterministic, static environments with single agents
factored representation.
a fluent is a condition that can change over time. In logical approaches to reasoning about actions, fluents can be represented in first-order logic by predicates having an argument that depends on time.
To describe the planning problem concisely, we use PDDL (Planning Domain Definition Language):
  • initial state: a conjunction of fluents that are ground, functionless atoms.
  • actions: a set of action schemas that consists of action name, a list of all the variables, a precondition and an effect.
  • result
  • goal test