📜 ⬆️ ⬇️

As I decided to quietly teach a python, but I got into the wilds of CS188.1x Artificial Intelligence

Hi Habr, or introduction


image

I'll tell you a little background.

As once again, I was tired of picking up the next controller, the circuit and the pcb, and I decided that I was depressed by the average salary of an ordinary electronics engineer in the market - I want to be a programmer again.
')
I can not say that I was already a programmer, but I got an education 3 years ago in the specialty “Information Systems and Technologies” in Voenmech. And fate brought in electronic circuit engineers in the days of uni. Previously, frequent business trips to facilities were being saved (while young and single — interesting), and the last year was all completely boring.
Reading Habr, chose to myself Python.

These first second recent articles helped determine how and what to start.

Actually, on the recommendation and began in early September with the study: Mark Lutz. Learning Python, 4th edition. Clenching his teeth and clenching into a fist all that squeezed - on the remainder of motivation, read up to the 803rd page, doing all the exercises along the way. In the middle of the PLO I finally twisted. The book is good, but dreary - no strength.

I just didn’t want to quit, and then I tried Google’s python class. Wow, how cool it turned out to solve the final puzzle! Two evenings flew in an instant.

Then I realized that it was probably not the best option to press through a book. And he remembered that he had seen a post about courses in Western universities. I used to stop the knowledge of spoken English, but after all I understood Nick Parlante!

It is said - done, and here I am already enrolled in two courses. About the first already written here , about the second - here . And the fact that in one python 2.7, in another 3.2 is also better, I thought. After the meticulous Lutz, the first 2 weeks of both courses were snapped like nuts. Special thanks to the progress bar for motivation. And clicking on the links he was found - CS188.1x: Artificial Intelligence . What do they write?

PREREQUISITES
  • Programming
    1. Object-Oriented Programming
    2. Recursion
    3. Python or the ability to learn Python quickly (short referesher provided)
  • Data structures
    1. Lists vs. Sets (Arrays, Hashtables)
    2. Queuing (Stacks, Queues, Priority Queues)
    3. Trees vs. Graphs (Traversal, Backpointers)
  • Math
    1. Probability, Random Variables, and Expectations (Discrete)
    2. Basic Asymptotic Complexity (Big-O)
    3. Basic Counting (Combinations and Permutations)



Cool! I will remember matan, I will train my little python for the time being, and nothing that the course has been going for the 2nd week.

Full course program
Introduction to AI
  • Overview
  • Agents: Perception, Decisions, and Actuation

Search and Planning
  • Uninformed Search (Depth-First, Breadth-First, Uniform-Cost)
  • Informed Search (A *, Greedy Search)
  • Heuristics and Optimality

Constraint Satisfaction Problems
  • Backtracking search
  • Constraint Propagation (Forward Checking, Arc Consistency)
  • Exploiting graph structure

Game Trees and Decision Theory
  • Decision theory
    Preferences, Rationality, and Utilities
    Maximum Expected Utility

  • Game Trees and Tree-Structured Computation
    Minimax, Expectimax, Combinations
    Evaluation Functions and Approximations
    Alpha-Beta Pruning

Markov Decision Processes (MDPs)
  • Policies, Rewards, and Values
  • Value iteration
  • Policy iteration

Reinforcement Learning (RL)
  • TD / Q Learning
  • Exploration
  • Approximation

Conclusion and Wrap-Up
No Lecture, Final Exam Week


First encounter with reality

I'll tell you about the course in more detail. The introductory lecture made a very favorable impression. Pleasant talking (and most importantly, not bearded!) Uncle, well-designed slides (later I learned that a separate designer worked on the pictures). Lectures are recorded from a real audience, students' reactions are heard (mostly laughter). They told what they mean by AI today, what has changed since the 50s, showed videos from Google machines, as well as their robot, which neatly folds shirts and T-shirts. Hmm, in my university, the transfer of knowledge about AI began and ended with a story about a Turing test.

image

Minus for me is the inability to download video with subtitles to yourself locally.

Lectures are good, but practice? Aha in the first week we have (Optional) Math Self Diagnostic, (Optional) Python Refresher.
Here is an example of a question from the Math section:

You can play three cards (marked 1 through 10). You can win if your card is a 10 or if your card is odd.
How many winning hands are there?
What is your chance of winning?


Actually, a higher technical education whispers: “Somewhere this has already happened ...” With the help of Wikipedia, mathematics was remembered. In the python refresher was peeked, but the confidence in my abilities, which settled after Lutz and two courses, gave vent to laziness, and I didn’t do the task, deciding to move on to week two.

Search and trees

With the concept of graphs and the disclosure of the search tree was introduced to the university. Therefore, I expected that it would be boring. But shown by the teacher yellow old Pacman kept interest. Depth First, Breadth First, Uniform Cost, Greedy, A * (with heuristic) Search - all this was consistently explained, and as an example served as Pacman, merrily running through the maze for points in accordance with the search algorithm. And not a single line of code in the lectures. I like it.

On the same day homework was mastered with lectures — graphs and general questions about bugs in the maze
But the further tab project 1 plunged into a stupor. Download the archive (more details here , thanks to Nbooo ), run under python 2.7 pacman.py - play (you can also run with the -h key for help). Now write your functions in the search.py ​​module, your pacman should find food in the corner of the maze. This is how it looks.

image

How so, because how to write it we were not taught. But since I took it. Work put aside. On the first day, several Depth First Search implementations were written, and Packman regularly began to get to the food. But they were ruthlessly removed, it turned out the number of branches opened by the search and the optimality of the path is strictly controlled. Your functions and methods are tested automatically after downloading the source texts to the server. Once again, erasing the code page, threw his sword and took up shouting. The leaves of the paper were covered with trees, pacmen and the labyrinth. Colleagues electronics engineers considered it their duty to grin, passing by. As a result, the implementation was found. Who wants to learn - it is worth torment yourself. From those who are with python and algorithms, I will be glad to hear comments on the case.

Code
I think everything is clear, util.Stack () and utilQueue () are actually lists wrapped in classes for the convenience of students, with the methods of push and pop, FIFO and LIFO respectively:

def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first Your search algorithm needs to return a list of actions that reaches the goal. Make sure to implement a graph search algorithm To get started, you might want to try some of these simple commands to understand the search problem that is being passed in: print "Start:", problem.getStartState() >>> Start: (4, 3) print "Is the start a goal?", problem.isGoalState(problem.getStartState()) >>> Is the start a goal? False print "Start's successors:", problem.getSuccessors(problem.getStartState()) >>> Start's successors: [((4, 4), 'North', 1), ((4, 2), 'South', 1), ((5, 3), 'East', 1), ((3, 3), 'West', 1)] """ def fromXYtoXY(coord1, coord2): ''' tuple(int,int), tuple(int,int) -> str Takes near coord1 coord2 tuples, returns string way, or error sample for tinyMaze: >>>fromXYtoXY((5,5),(4,5)) >>>'West' >>>fromXYtoXY((1,3),(2,3)) >>>'East' ''' if coord1[0]==coord2[0]: if coord1[1]-coord2[1]==1: return 'South' else: return 'North' elif coord1[1]==coord2[1]: if coord1[0]-coord2[0]==1: return 'West' else: return 'East' else: return ('Path not found from %s to %s' % (coord1, coord2)) Fringe = util.Stack() currState=problem.getStartState() Fringe.push([currState]) while True: currState=Fringe.pop() if problem.isGoalState(currState[-1]): listWays=[] fromState=currState[0] #      for state in currState[1:]: # ex: ['South', 'West'] listWays.append(fromXYtoXY(fromState,state)) fromState=state return listWays break for State, Way, Price in problem.getSuccessors(currState[-1]): if not State in currState: nextPath=currState[:] nextPath.append(State) Fringe.push(nextPath) 



The implementation of Breadth First Search, in my opinion, was already a little more beautiful.

Code
 def breadthFirstSearch(problem): """ Search the shallowest nodes in the search tree first. """ fringe=util.Queue() visitedNodes=[] fringe.push([problem.getStartState(),[]]) #    +   while not fringe.isEmpty(): #     currNode = fringe.pop() #  if currNode[0] not in visitedNodes: #    visitedNodes.append(currNode[0]) # for State, Way, Price in problem.getSuccessors(currNode[0]): path=currNode[1][:] #     path.append(Way) if problem.isGoalState(State): return path else: fringe.push([State, path]) 



Then, Uniform Cost Search and A * Search were relatively easy to implement (with Euclidean distance to food as a heuristic function)

Code
 def uniformCostSearch(problem): "Search the node of least total cost first. " fringe=util.PriorityQueue() visitedNodes=[] fringe.push([problem.getStartState(),[],0],0) while not fringe.isEmpty(): currNode = fringe.pop() if problem.isGoalState(currNode[0]): return currNode[1] if currNode[0] not in visitedNodes: #    visitedNodes.append(currNode[0]) # for State, Way, Price in problem.getSuccessors(currNode[0]): path=currNode[1][:] #     totalCost=currNode[2]+Price path.append(Way) fringe.push([State, path, totalCost],totalCost) def aStarSearch(problem, heuristic=nullHeuristic): "Search the node that has the lowest combined cost and heuristic first." "*** YOUR CODE HERE ***" fringe=util.PriorityQueue() visitedNodes=[] fringe.push([problem.getStartState(),[],0],0) while not fringe.isEmpty(): currNode = fringe.pop() if problem.isGoalState(currNode[0]): print 'Success!!', currNode[1] return currNode[1] if currNode[0] not in visitedNodes: #    visitedNodes.append(currNode[0]) # for State, Way, Price in problem.getSuccessors(currNode[0]): #print 'succ', State, Way, Price path=currNode[1][:] #     totalCost=currNode[2]+Price path.append(Way) fringe.push([State, path, totalCost],totalCost+heuristic(State,problem)) 



In the second part of project 1, students were asked to modify search problems in the searchAgents.py module by adapting the methods of the child classes. If earlier there was one Pac-man and one meal, then now there is one Pac-man and food in 4 corners, which need to be followed along the optimal path. And also come up with a heuristic-function for A * Search, which aims to reduce the costs of opening the branches of the search tree (the rating system is as follows: 1 point for expanding fewer than 1600 nodes . 1 point for expanding fewer than 1200 nodes.).

Project 1 has ended with a solution to the problem of optimally eating a multitude of points in the maze.

image

As a result, I spent almost 3 working days on solving these problems; well, there was some lull at work. It was interesting to break my head and hopefully useful in the future.

I want to finish with the funniest words of the teacher in one of the lectures:

Anything you want to do, NP-hard. Sorry, this is an AI class. Everything is hard.
I hope the review helped a little someone. The course (or just solve problems) I advise all beginners to get acquainted with python, and I myself am looking forward to the project 2.

upd. part 2

Source: https://habr.com/ru/post/154959/


All Articles