AI-mod1

March 11, 2018 | Author: Rose Maria Dominic | Category: Technology, Artificial Intelligence, Intelligence, Computer Science, Artificial Neural Network
Share Embed Donate


Short Description

Download AI-mod1...

Description

CS010 802

Artificial Intelligence

MODULE I

What is Intelligence??? Intelligence is the ability to learn about, to learn from, to understand about, and interact with one‟s environment.Intelligence is the faculty of understanding

Artificial intelligence (AI) The study of computer systems that attempt to model and apply the intelligence of the human mind is known as Artificial Intelligence(AI). For example, writing a program to pick out objects in a picture Artificial Intelligence (AI) is usually defined as the science of making computers do things that require intelligence when done by humans. A.I is the study of ideas that enable computers to be intelligent. Artificial intelligence works with the help of Artificial Neurons (Artificial Neural Network) and Scientific theorems (If-Then Statements, Logics)

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 1

CS010 802

Artificial Intelligence

Need for AI: To supplement natural intelligence for e.g we are building intelligence in an object so that it can do what we want it to do, as for example-- robots, thus reducing human labour and reducing human mistakes.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 2

CS010 802

Artificial Intelligence

Definitions Definitions of artificial intelligence according to 8 text books are given below. 

Artificial intelligence is a system that thinks like human beings. 

1.AI is an exciting effort to make computers think… machines with minds, in the full and literal sense. 2.AI is the automation of activities that we associate with human thinking, activities such as decision making, problem solving, learning..etc.  

Artificial intelligence is a system that thinks rationally.  3. AI is the study of mental faculties through the use of computational models. 4.AI is the study of computations that make it possible to perceive, reason and act.

 

Artificial intelligence is a system that acts like human beings. 

5. AI is the art of creating machines that perform functions that require intelligence when performed by people. 6. AI is the study of how to make computers do things at which , at the moment , people do better.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 3

CS010 802  

Artificial Intelligence

Artificial intelligence is a system that acts rationally.  7. AI is the study of the design of intelligent agents. 8. AI is concerned with intelligent behavior in artifacts.

AI application areas The 2 most fundamental concerns of AI researchers are    

Knowledge representation  Search. Other Areas  • Game Playing • Heuristics • Automated reasoning & theorem proving • Expert systems • Natural language understanding & semantic modeling • Modeling human performance • Planning & robotics • Machine learning • Neural nets & genetic algorithms

Knowledge representation It addresses the problem of capturing the full range of knowledge required for intelligent behavior in a formal language, i.e. One suitable for computer manipulation. Eg. Predicate calculus,LISP, Prolog, Python Search It is a problem solving technique that systematically explores a space of problem states,ie, successive and alternative stages in the problem solving process. The following explains the major application areas of AI. Game playing Much of the early research in AI was done using common board games such as checkers, chess and the 15 puzzle. Board games have certain properties that made them ideal for AI research. Most games are played using a well defined set of rules. This makes it easy to generate Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 4

CS010 802

Artificial Intelligence

the search space. The board configuration used in playing these games can be easily represented on a computer. As games can be easily played, testing a game playing program presents no financial or ethical burden. Heuristics Heuristic technique designed for solving problem more quickly when classic methods are too slow, or find an approximate solution when classic methods fails to find any exact solution. This is achieved by trading optimality, completeness, accuracy or precision for speed. Games can generate extremely large search spaces. So we use powerful techniques called heuristics to explore the problem space. Games provide a rich domain for the study of heuristic search. Automated reasoning and theorem proving Examples for automatic theorem provers are Newell and Simon’s Logic Theorist,General Problem Solver(GPS).Theorem proving research is responsible for the development of languages such as predicate calculus and prolog. The attraction of automated theorem proving lies in the rigor and generality of logic. A wide variety of problems can be attacked by representing the problem description as logical axioms and treating problem instances as theorems to be proved. Reasoning based on formal mathematical logic is also attractive. Many important problems such as design and verification of logic circuits, verification of the correctness of computer programs and control of complex systems come in this category. Task domains of AI

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 5

CS010 802

Artificial Intelligence

Expert systems Here comes the importance of domain specific knowledge. A doctor, for example, is effective at diagnosing illness because she possesses some innate general problem solving skill; she is effective because she knows a lot about medicine. A geologist is effective at discovering mineral deposits. Expert knowledge is a combination of theoretical understanding of the problem and a collection of heuristic problem solving rules that experience has shown to be effective in the domain. Expert systems are constructed by obtaining this knowledge from a human expert and coding it into a form that a computer may apply to similar problems. To develop such a system, we must obtain knowledge from a human domain expert. Examples for domain experts are doctor, chemist, geologist, engineer etc.. The domain expert provides the necessary knowledge of the problem domain. The AI specialist is responsible for Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 6

CS010 802

Artificial Intelligence

implementing this knowledge in a program. Once such a program has been written, it is necessary to refine its expertise through a process of giving it example problems to solve and making any required changes or modifications to the program‟s knowledge.

    



Dendral is an expert system designed to infer the structure of organic molecules from their chemical formulas and mass spectrographic information about the chemical bonds present in the molecules. 



Mycin is an expert system which uses expert medical knowledge to diagnose and prescribe treatment for spinal meningitis and bacterial infections of the blood. 



Prospector is an expert system for determining the probable location and type of ore deposits based on geological information about a site.  Internist is an expert system for performing diagnosis in the area of internal medicine. 

  

The dipmeter advisor is an expert system for interpreting the results of oil well drilling logs.  Xcon is an expert system for configuring VAX computers. 

Natural language understanding and semantic modeling One goal of AI is the creation of programs that are capable of understanding and generating human language. Systems that can use natural language with the flexibility and generality that characterize human speech are beyond current methodologies. Understanding natural language involves much more than parsing sentences into their individual parts of speech and looking those words up in a dictionary. Real understanding depends on extensive background knowledge. Consider for example, the difficulties in carrying out a conversation about baseball with an individual who understands English but knows nothing about the rules of the game. This person will not be able to understand the meaning of the sentence. “With none down in the top of the ninth and the go ahead run at second, the manager called his relief from the bull pen”. Even though hall of the words in the sentence may be individually understood, this sentence would be difficult to even the most intelligent non base ball fan. The task of collecting and organizing this background knowledge in such a way that it may be applied to language comprehension forms the major problem in automating natural language understanding. Modeling human performance Human intelligence is a reference point in considering artificial intelligence. It does not mean that programs should pattern themselves after the organization of the human mind. Programs that take non human approaches to solving problems are often more successful than

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 7

CS010 802

Artificial Intelligence

their human counterparts. Still, the design of systems that explicitly model some aspect of human performance has been a fertile area of research in both AI and psychology. Planning and robotics Research in planning began as an effort to design robots that could perform their tasks with some degree of flexibility and responsiveness to outside world. Planning assumes a robot that is capable of performing certain atomic actions. Planning is a difficult problem because of the size of the space of possible sequences of moves. Even an extremely simple robot is capable of generating a vast number of potential move sequences. One method that human beings use in planning is hierarchical problem decomposition. If we are planning a trip to London, we will generally treat the problems of arranging a flight, getting to the air port, making airline connections and finding ground transportation in London separately. Each of these may be further decomposed into smaller sub problems. Creating a computer program that can do the same is a difficult challenge. A robot that blindly performs a sequence of actions without responding to changes in its environment cannot be considered intelligent. Often, a robot will have to formulate a plan based on the incomplete information and correct its behavior. A robot may not have adequate sensors to locate all obstacles in the way of a projected path. Organizing plans in a fashion that allows response to changing environmental conditions is a major problem for planning. Machine learning An expert system may perform extensive and costly computations to solve a problem. But if it is given the same or similar problem a second time, it usually does not remember the solution. It performs the same sequence of computations again. This is not the behavior of an intelligent problem solver. The programs must learn on their own. Learning is a difficult area. But there are several programs that suggest that it is possible. One program is AM, the automated mathematician which was designed to discover mathematical laws. Initially given the concepts and axioms of set theory, AM was able to induce important mathematical concepts such as cardinality, integer arithmetic and many of the results of number theory. AM conjectured new theorems by modifying its current knowledge base. Early work includes Winston’s research on the induction of structural concepts such as “arch” from a set of examples in the blocks world. The ID3 algorithm has proved successful in learning general patterns from examples. Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 8

CS010 802

Artificial Intelligence

Meta dendral learns rules for interpreting mass spectrographic data in organic chemistry from examples of data on compounds of known structure. Teiresias, an intelligent front end for expert systems, converts high level advice into new rules for its knowledge base. There are also now many important biological and sociological models of learning. Neural nets and genetic algorithms An approach to build intelligent programs is to use models that parallel the structure of neurons in the human brain. A neuron consists of a cell body that has a number of branched protrusions called dendrites and a single branch called the axon. Dendrites receive signals from other neurons. When these combined impulses exceed a certain threshold, the neuron fires and an impulse or spike passes down the axon.

Figure 1: Structure of neuron

This description of the neuron captures features that are relevant to neural models of computation. Each computational unit computes some function of its inputs and passes the result along to connected units in the network; the final results are produced by the parallel and distributed processing of this network of neural connection and threshold weights. Genetic algorithm- examples 

 

Ant Colony Optimization (ACO)  Particle Swarm Optimization (PSO) 

PROBLEMS •

It is the question which is to be solved



Needs to be precisely defined

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 9

CS010 802 •

Artificial Intelligence

Definitions include start state , goal state

What are AI problems? Solving Mundane tasks, formal tasks, and expert tasks To build an AI computational system to solve a particular problem the following activities are needed to be performed 1.Define the problem precisely The definitions must include precise specifications of initial state and final states of the problem 2.Analyze the problem 3.Isolate& represent the knowledge that is needed to solve the problem 4. Choose the best problem solving technique and apply it to particular problem Problem space/ search space It is the complete set of states including start & goal states where the answer of problem is to be searched Search It is the process of finding solution in search space. The input of search space algorithm is problem and output is solution in form of action sequence. Well defined problem •

Problem description includes

Initial state Final state Space including transition function or path function Path cost Solution of the problem Path from initial state to goal state is the solution. It is guided by transition rules. Optimal solution means among all solutions, whichever solution has least path cost.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 10

CS010 802

Artificial Intelligence

Features of good representation of problem

Representation of AI problems It includes 4 parts    

Lexical part  Structural part  Procedural part  Semantic part 

1. Lexical part Determines which symbols are allowed in the representation of problem Example-Problem of “playing chess” Board position 8x8 array Official chess opening position Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 11

CS010 802

Artificial Intelligence

Goal :Opponent does not have a legal move and his king in under attack 2. Structural part • Constraints on how the symbols can be arranged • Example Legal moves Way of getting into initial sate & goal state Set of rules L.H.S –pattern to be matched against current board position R.H.S –changes to be made to board position to reflect the move 3. Procedural part • Specifies access procedures that enable to create descriptions, to modify them, and to answer questions using them • Example Set of winning moves • Legal moves Vs useful moves • Legal moves Moves which are permissible according to game rules • •

10 120 Useful moves Legal moves which bring the game in a winning position Useful moves subset of legal moves

4. Semantic part • Way of associating meaning with the descriptions • Example No hidden meaning associated with any piece

PRODUCTION SYSTEM • AI system developed for solution of any problem • Used for application of rules and obtaining the solution • Components Production rules Knowledge/data bases Control strategy Rule applier 1.Production rules • P→Q • L.H.S-current problem state Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 12

CS010 802

Artificial Intelligence

• R.H.S-result or generated output state 2.Knowledge/data bases • Contain all appropriate information for the particular task • Permanent/ solution of current problem 3.Control strategy  Order in which rules are compared to the database of rules and a way of resolving the conflicts that arise when several rules match simultaneously  4.Rule applier • Checks applicability of rule • By matching current state with L.H.S of rule • Finds the appropriate rule from database of rules Characteristics of production system • 1. Data Structure • 2. Control Strategies 1. Data Structure After defining the problem, it will be required to represent in a suitable data structure. The data structures best suited for traditional AI problems are Graphs &trees. Nodes in graph correspond to problem state and arcs between nodes correspond to valid transitions. Ex: 8-puzzle problem There is a special type of tree representation called AND-OR graph which is used in AI problem. 2. Control Strategies These are also called search strategies. Control Strategies are adopted for applying the rules and searching the problem solution in search space. The control strategy is responsible for obtaining the solution of the problem. Hence, if the wrong control strategy is applied, it may be possible that a solution is never obtained, if it exists. Salient characteristics of control strategies 1)It should cause motion. Whenever we apply a rule the movement of the problem should be in the direction of obtaining solution 2) It should be systematic

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 13

CS010 802

Artificial Intelligence

PROBLEM CHARATERISTICS 1. Path Finding Problems 2. Decomposable problems 3. Recoverable Problems 4. Predictable problems 5. Problems affecting the quality of solution 6 State Finding problems 7. Problems requiring interaction 8. Knowledge intensive problems 1. Path Finding problems Solution involves reporting of path ( or sequence of steps used to obtain the solution).Ex: Travelling salesperson problem 2. Decomposable problems The problem can be broken into small subproblems and the solution of the main problem is obtained by joining the solutions of small subproblems Eg: mathematical equation Suppose we want to solve the problem of computing the 2 2 2 expression ∫ ( x + 3x + Sin x . Cos x ) dx 2 We can solve this problem by breaking it down into 3 smaller problemsi)∫ x , ii) ∫3x iii) ∫ 2 2 Sin x . Cos x each of which can then solve by using a small collection of specific rules. 3. Recoverable problems Application of operator can be reverted if required and on initial state , a new operator can be applied Ex: 8-puzzle problem The recoverability of a problem plays an important role in determining the control structure necessary for problem solution. Ignorable problem Like “theorem proving” where the steps can be ignored. These can be solved using simple control structures which is easy to implement. Irrecoverable problem Like medical diagnosis problem where if an operator is applied, it cannot be reverted to original state. the selection of the operator is very critical because if a wrong operator is applied, it may be possible that a solution never obtained even if it exists. Another example is chess playing.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 14

CS010 802

Artificial Intelligence

4. Predictable problems Certain outcome problems (eg. 8 puzzle) Suppose we are playing with the 8 puzzle problem. Every time we make a move, we know exactly what will happen. This means that it is possible to plan an entire sequence of moves and be confident that we know what the resulting state will be. Uncertain outcome problems

(eg. Bridge)

However, in games such as bridge, this planning may not be possible. One of the decisions we will have to make is which card to play on the first trick. What we would like to do is to plan the entire hand before making that first play. But now it is not possible to do such planning with certainty since we cannot know exactly where all the cards are or what the other players will do on their turns. One of the hardest types of problems to solve is the irrecoverable, uncertain outcome. Examples of such problems are Playing bridge, Controlling a robot arm, Helping a lawyer decide how to defend his client against a murder charge. 5. Problems affecting the quality of solution There are certain type of AI problems where the process of finding solution stops by just finding one solution and there is no need to ensure the validity of this solution by finding other solutions. Ex: database query application In query applications whenever query is answered the possible answers are not checked. •

6 State Finding problem In this type of AI problem, the answer is reporting a state instead of path. Ex: natural language understanding applications. In such type of applications the interpretation of a particular sentence is required and it is not important how the solution is obtained. As far the interpretation of the input sentence is correct the steps used in obtaining the solution can be ignored. State and path problems Problems where the solution demands reporting of state as well as path. These are the problems requiring justification. Eg. Water jug problem

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 15

CS010 802

Artificial Intelligence

In water jug problem, it is not sufficient to report that we have solved the problem and that the final state is (2,0). For this kind of problem, what we really must report is not the final state, but the path that we found to that state. Another example is medical diagnosis problem using expert system. •

7.Problems requiring interaction This kind of problems, which requires asking some questions from the users or interacting with the user.

Solitary problems Here the computer is given a problem description and produces an answer with no intermediate communication and with no demand for an explanation for the reasoning process. Consider the problem of proving mathematical theorems. If 1. All we want is to know that there is a proof. 2. The program is capable of finding a proof by itself. Then it does not matter what strategy the program takes to find the proof. Conversational problems In which there is intermediate communication between a person and the computer, either to provide additional assistance to the computer or to provide additional information to the user. Eg. Suppose we are trying to prove some new, very difficult theorem. Then the program may not know where to start. At the moment, people are still better at doing the high level strategy required for a proof. So the computer might like to be able to ask for advice. To exploit such advice, the computer‟s reasoning must be analogous to that of its human advisor, at least on a few levels. •

8. Knowledge intensive problems These are the kind of AI problems, which require large amount of knowledge for their solution. Eg: Tic-Tac-Toe Chess Medical expert system Data query Natural language understanding (will require vast amount of knowledge comprising of syntactic, semantic and pragmatic knowledge)

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 16

CS010 802

Artificial Intelligence

The problem characteristics can be summarized as follows 1. Is the problem is decomposable into independently smaller or easier subproblems 2. Is backtracking possible or not 3. Is the problem‟s universe predictable 4. Is a good solution to the problem obvious without comparison to all other possible solutions 5. Is the desired solution a state or a path. 6. Is a large amount of knowledge absolutely required to solve the problem or is knowledge important only to constrain the search. GENERAL EXAMPLE PROBLEMS Problems can be classified as toy problems and real world problems. Toy problems •

Illustrate or exercise various problem solving methods



Used by different researchers to compare performance of algorithm



Examples are •

Vaccum world



8-puzzle problem



8-Queens problem

vacuum world The agent is in one of the 2 locations, each of which might or might not contain dirt. Any state can be designated as the initial state. After trying these actions (Left, Right, Suck), we get another state. The goal test checks whether all squares are clean.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 17

CS010 802

Artificial Intelligence

8 – puzzle problem It consists of a 3 X 3 board with 8 numbered tiles and a blank space as shown below.

A tile adjacent to the blank space can slide into the space. The aim is to reach a specified goal state, such as the one shown on the right of the figure. 8 – Queens problem The goal of 8- queens‟ problem is to place 8 queens on a chess board such that no queen attacks any other.

(a queen attacks any piece in the same row, column or diagonal). Figure shows an attempted solution that that fails: the queen in the rightmost column is attacked by the queen at the top left. 

  • • • • • •

Real world problems  Whose solutions actually people care about  Examples Route finding problems Travelling Salesperson Problem(TSP) VLSI layout problem Robot navigation Automatic assembly sequencing of complex objects by a robot Protein design Internet searching

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 18

CS010 802

Artificial Intelligence

Route finding problem Route finding algorithms are used in a variety of applications, such as routing in computer networks, military operations planning and air line travel planning systems. Touring problems They are related to route finding problems. For example, consider the figure.

Visit every city in the figure at least once starting and ending in same location. Traveling salesperson problem (TSP) Is a touring problem in which each city must be visited exactly once. The aim is to .find the shortest tour. VLSI layout problem It requires positioning of millions of components and connections on a chip to minimize area, minimize circuit delays, minimize stray capacitances, and maximize manufacturing yield.

Robot navigation It is a generalization of the route finding problem. Rather than a discrete set of routes, a robot can move in a continuous space with an infinite set of possible actions and states. Automatic assembly sequencing of complex objects by a robot The assembly of intricate objects such as electric motors is economically feasible. In assembly problems, the aim is to find an order in which to assemble the parts of some object. If the wrong order is chosen, there will be no way to add some part later in the sequence without undoing some of the work already done. Protein design :Here the aim is to find a sequence of amino acids that will fold into a three dimensional protein with the right properties to cure some disease. Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 19

CS010 802

Artificial Intelligence

Internet searching: It means looking for answers to questions for related information or for shopping details. Software robots are being developed for performing this internet searching. Other Examples of AI problems •

1) Tic-Tac-Toe problem



2) Water jug problem



3) 8- Puzzle problem



4) 8-Queens problem



5) Chess problem



6) Missionaries & cannibals problem



7)Tower of Hanoi problem



8) Travelling salesperson problem



9) Magic square



10) Language understanding problem



11)Monkey & banana problem



12) Cryptarithmatic problem



13) Block world problems

• 1)Tic-Tac-Toe problem Starting with an empty board, the first player may place an X in any one of nine places. The player who is able to make his marks in vertical, horizontal or diagonal straight line first is declared as a winner. Each of these moves yields a different board that will allow the opponent 8 possible responses and so on. We can represent this collection of possible moves and responses by regarding each board configuration as a node in a graph. The links of the graph represent legal moves from one board configuration to another. These nodes correspond to different states of the game board. The resulting structure is called a state space graph.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 20

CS010 802

Artificial Intelligence

2) water jug problem The problem is defined as “we are given two water jugs having no measuring marks on these. The capacities of jugs are 3 liter and 4 liter. It is required to fill the bigger jug exactly 2 liter of water. The water can be filled in a jug from a tap”. The state space of this problem can be described by set of ordered pairs of two variables (x,y) where x represents the water in 4-liter jug and y represents water in 3-liter jug. Variable xcan take the value 0,1,2,3,4 and variable y can take the value 0,1,2,3. The start state is (0,0) and the goal state is (2,0). The production rules are formulated as follows

Partial search tree of water jug problem

Solution of the problem will include applying appropriate rules in a specific sequence to transform the start state to goal state. One solution is applying the rules in the sequence 2,9,2,7,5,9.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 21

CS010 802

Artificial Intelligence

3) 8-puzzle problem The 8-puzzle belongs to the category of “sliding block puzzle” types of problems. It is described as •

“ It has set of 3x3 board having 9 block spaces out of which, 8 blocks are having tiles bearing number from 1 to 8. One space is left blank. The tile adjacent to blank space can move into it. We have to arrange the tiles in a sequence”

Partial search tree of an 8 puzzle problem

4.)8-Queens problem This problem is presented as follows •

“ We have 8 queens and a 8x8 chessboard having alternate black and white squares. The queens are placed on the chessboard. Any queen can attack any other queen placed on same row, or column, or diagonal. We have to find the proper placement of queens on the chessboard in as such a way that no queen attacks other queen”

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 22

CS010 802

Artificial Intelligence

One possible board configuration of 8 queens problem is

5) Chess problem In a chess game problem, the start state is the initial configuration of chess board. The final or goal state is any configuration, which is a winning position for any player. It is estimated that the 120 chess game has more than 10 possible states.

6)Missionaries and Cannibals problem The problem is stated as follows “ Three missionaries and three cannibals are present at one side of a river and need to cross the river. There is only one boat available . At any point of time, the number of cannibals should not outnumber the number of missionaries at that bank. It is also known that only two persons can occupy the boat available at a time.”

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 23

CS010 802

Artificial Intelligence

7) Tower of hanoi Problem This is a historic problem. It can be described as follows: “Near the city of Hanoi there is a monastery. There are three tall posts in the courtyard of the monastery. One of these posts is having sixty-four disks, all having a hole in the center and are of different diameters, placed one over the other in such a way that always a smaller disk is placed over the bigger disk. The monks of the monastery are busy in the task of shifting he disks from one post to some other, in such a way that one point of time , a bigger disk is placed above smaller disk. Only one disk can be removed at a time. Moreover, at every point of time, during the process , all other disks than the one remove d, should be on one of the posts. The third post can be used as a temporary resting place for the disks. We have to help the monks in finding the easiest and quickest way to do so” Finding the solution of this problem even with the quickest method might take longer time than the time left for the whole world to finish. 8) Travelling salesperson Problem “Given n cities connected by roads, and distances between each pair of cities. A sales person is required to travel each of the cities exactly once. We are required to find the route of the salesperson so that by covering minimum distance, he can travel all the cities and come back to the city from where the journey was started”. 9) Magic Square “ We are given square of same number of rows and columns, where consecutive numbers are filled in blank spaces of each row and column such that numbers in each row, column and two diagonals, and add up to the same number. The numbers have to be consecutive and one number can be used only once” 6

1

8

7

5

3

2

9

4

10)Language Understanding Problems This type of problems include the understanding of natural languages, conversion of one language to another language, language comprehension, design of intelligent natural language interfaces etc.Iit also includes answering a query using a database.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 24

CS010 802

Artificial Intelligence

11)Monkey and banana problem This problem is described as follows “ A monkey and a bunch of banana are present in a room. The bananas are hanging from the ceiling. The monkey cannot reach the bananas directly. However, in the room there is one chair and a stick. The monkey can reach the banana standing on the chair. We have to find the sequence of events by which monkey can reach the bananas” 12) Cryptarithmatic puzzle It is a puzzle involving decoding of digit represented by a character. It is in the form of some arithmetic equation where digits are distinctly represented by some characters. The problem requires finding of the digit represented by each character. Example +R I N G DOOR _________________ BELL These type of problems require constraint satisfaction. Constraints are that all the laws of arithmetic must hold good and any letter should represent the same digit wherewver it comes in the puzzle.

SEARCHING STRATEGIES The process of finding a solution of AI problem is known as searching. It searches the path from start state to goal state. This very important aspect of problem solving because search techniques not only helps in finding most viable path to reach goal state, but also make entire process efficient and economical. Basic Search Techniques 1) Blind (or unguided or uninformed) 2) Heuristic (or guided or informed) 1) Blind Search Search methodology having no additional information about states beyond that provided in the problem definitions. In this search total search space is looked for solution. It includes 1) Breadth-first Search(BFS) Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 25

CS010 802

Artificial Intelligence 2) Depth- First search(DFS) 3) Depth- Limited Search(DLS) 4) Bidirectional Search

1)Breadth first search(BFS)

Consider the graph shown below

States are labeled (A, B, C….).Breadth first search explores the space in a level by level fashion. Only when there are no more states to be explored at a given level does the algorithm move on to the next level. A breadth first search of the above graph considers the states in the order A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U. We implement breadth first search using lists, open and closed, to keep track of progress through the state space. „open‟ lists states that have been generated but whose children have not been examined. The order in which states are removed from open determines the order of the search. „closed‟ records states that have already been examined. void breadth_ first _ search ( ) { open = [ start ]; closed = [ ]; while ( open not empty ) { Remove the leftmost state from open, call it X;

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 26

CS010 802

Artificial Intelligence

if X is a goal, then return SUCCESS; else { Generate children of X; Put X on closed; Discard children of x, if already on open or closed; Put remaining children on right end of open; } } return FAIL; }

Child states are generated by inference rules, legal moves of a game or other state transition operators. Each iteration produces all children of the state x and adds them to open. Note that open is maintained as a queue (FIFO) data structure. States are added to the right of the list and removed from the left. A trace of the breadth first search on the graph appears below. Open

closed

A

empty

BCD

A

CDEF

BA

DEFGH

CBA

EFGHIJ

DCBA

FGHIJKL

EDCBA

GHIJKLM

FEDCBA

HIJKLMN

GFEDCBA

. Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 27

CS010 802

Artificial Intelligence

. And so on until open = [ ]. Because breadth first search considers every node at each level of the graph before going deeper in to the space, all states are first reached along the shortest path from the start state. Breadth first search is therefore guaranteed to find the shortest path from the start state to goal. 2)Depth first search (DFS) Dept first search goes deeper in to the search space whenever this is possible. Consider the graph

Depth first search examines the states in the graph in the order A, B, E, K, S, L, T, F, M, C, G, N, H, O, P, U, D, I, Q, J, R. In depth first search, the descendent states are added and removed from the left end of open. void depth_first_search () { open = [start]; closed = [ ]; while (open not empty ) { remove leftmost state from open, call it X; if X is a goal state then return SUCCESS; else { Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 28

CS010 802

Artificial Intelligence generate children of X; put X on closed; discard children of X, if already on open or closed; put remaining children on left end of open;

} } return FAIL; } A trace of depth first search on the above graph is shown below. open

closed

A

empty

BCD

A

EFCD

BA

KLFCD

EBA

SLFCD

KEBA

LFCD

SKEBA

TFCD

LSKEBA

FCD TLSKEBA MCDFTLSKEBA CDMFTLSKEBAGH DCMFTLSKEBA

And so on until open = [ ]; „open‟ records all states that are discovered and „closed‟ contains all states that are already considered. DFS is not guaranteed to find the shortest path to a state the first time that state is encountered.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 29

CS010 802

Artificial Intelligence

3)Depth limited search To limit the depth of DFS, we can supply a predetermined depth limit, l to DFS. That is nodes at depth l are treated as if they have no successors. This approach is called depth limited search. Depth first search can be viewed as a special case of depth limited search with l = ∞. Sometimes depth limit can be based on knowledge of the problem. For example, on the map of Romania, there are 20 cities. Therefore, we know that if there is a solution, it must be of length 19 at the longest, so l = 19 is a possible choice. But in fact if we studied the map carefully, we would discover that any city can be reached from any other city in at most 9 steps. This number, known as the diameter of the state space, gives us a better depth limit , which leads to a more efficient depth limited search. 4) Bidirectional search The idea behind bidirectional search is to run two simultaneous searches- one forward from the initial state and other backward from the goal, stopping when the 2 searches meet in the middle.

The most difficult case for bidirectional search is when the goal test gives only an implicit description of some possibly large set of goal states. For example, all the states satisfying the “check mate” goal test in chess. ii)HEURISTICS Search techniques where additional information about the problem is provided in order to guide the search in a specific direction. Types of heuristic search techniques •

1) Generate & test



2) Best-first-search



3) Problem reduction

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 30

CS010 802

Artificial Intelligence



4) Hill Climbing search



5) Constraint satisfaction



6) Means ends analysis



7) Mini –Max search Need for heuristic search techniques 1) The problem on hands does not have any exact solution because of inherent ambiguities in the problem statement or the data available. Eg: Medical diagnosis 2) A problem having exact solution, but cost of finding that solution is unbearable. Eg: Chess game 120 No: of possible states are huge around 10 states.

Basic concept of heuristic search This is also called guided search, because the search is guided in a specific direction. Heuristics :In heuristic search, besides normal production rules, additional information or clue is called heuristics. Heuristic function: Additional information given for search in terms of heuristic function. Heuristic knowledge: The additional task domain knowledge is called heuristic knowledge. Because of the presence of additional information, heuristic search is called informed search. Example One person wants to search a particular house in a city. If the available information is only the address of the house, then it is a blind search. Now an additional information or clue regarding a house is also given” house is under search is a pink colored building”. Then it‟s a heuristic search Combinatorial explosion : In an AI problem, the no: of states grow exponentially with the no: of basic elements is known as combinatorial explosion. Eg:chess 120

It has more than 10

possible states.

Heuristic function



   

It relates the current search state with the desired goal state.  It is the function that maps from problem state descriptions to measures of desirability  Usually represented as numbers  Well designed heuristic function can play an important part efficiently guiding a search process towards a solution. 

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 31

CS010 802  



Artificial Intelligence

Sometimes very simple heuristic function can provide a fairly good estimate of whether the path is good or not.  The purpose of heuristic function is to guide the search process in the most profitable direction by suggesting which path to follow first when more than one is available. 

Example: 8 puzzle problem The 8-puzzle was one of the earliest heuristic search problems.

Consider two heuristics for the 8 puzzle problem. h1 = the number of misplaced tiles For the above figure, for the start state value of h1 is 8. h2 = the sum of the distances of every tile in any board position from the corresponding tile in the standard goal position.We calculate sum of horizontal and vertical positions. For the above figure, for the start state the value of h2 = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18.

GENERATE AND TEST It is simplest of all the heuristic search approaches. This approach is simply to generate the result and test whether it is desired solution of the given problem. Algorithm Do the following until a satisfactory solution is obtained or no more solutions can be generated. 1) Generate a possible solution. 2) Test to see if this is a solution. 3) If a solution has been found, quit, otherwise return to step 1 Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 32

CS010 802

Artificial Intelligence

2 methods to complete generate and test process 1) Random solution Generate a random solution and test it. If found wrong, create another solution and test it again. Sometimes this method will give solution at first trial. 2) Systematic approach A systematic approach of generating a solution is applied and we are sure of ultimately getting the right solution if it exists. But, the difficulty of this method could be that it might take a lot of time if problem space is large. When random as well as systematic both the methods are not serving the purpose adopt a middle path. That is, to proceed the search process systematically, but discard some paths in-between which are not likely to provide the solution.

An AI program developed in 1980 and known as DANDRAL uses a strategy called plangenerate-test, in which planning part uses constraint satisfaction techniques to create list of recommended substructures, and generate – and – test method then uses these lists to explore only limited set of structures. Thus in such type of combined uses, generate- andtest approach has proved to be very effective. HILL CLIMBING It is a variant of generate and test in which feedback from the test procedure is used in deciding in which direction the search should proceed. Hill climbing strategies expand the current state in the search and evaluate its children. The best child is selected for further expansion; neither its siblings nor its parent is retained. Search halts when it reaches a state that is better than any of its children. Hill climbing is named for the strategy that might be used by an eager, but blind mountain climber: go uphill along the steepest possible path until you can go no farther. Because it keeps no history, the algorithm cannot recover from failures of its strategy. There are three various strategies for hill climbing. They are 1)Simple hill climbing, 2)Steepest ascent hill climbing and Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 33

CS010 802

Artificial Intelligence

3)Simulated annealing. Simple hill climbing The simplest way to implement hill climbing is as follows. Algorithm 1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise continue with the initial state as the current state. 2. Loop until a solution is found or until there are no new operators left to be applied in the current state: a. Select an operator that has not yet been applied to the current state and apply it to produce a new state. b. Evaluate the new state, i. If it is a goal state, then return it and quit. ii. If it is not a goal state, but it is better than the current state, then make it the current state. iii. If it is not better than the current state, then continue in the loop. Example: A problem is given. Given the start state as „a‟ and the goal state as „f‟. Suppose we have a heuristic function h (n) for evaluating the states. Assume that a lower value of heuristic function indicates a better state.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 34

CS010 802

Artificial Intelligence

Here a has an evaluation value of 5. a is set as the current state. Generate a successor of a. Here it is b. The value of b is 3. It is less than that of a. that means b is better than the current state a. So set b as the new current state. Generate a successor of b. it is d. D has a value of 4. It is not better than the current state b (3). So generate another successor of b. it is e. It has a value of 6. It is not better than the current state b (3). Then generate another successor of b. We get k. It has an evaluation value of 2. k is better than the current state b (3). So set k as the new current state. Now start hill climbing from k. Proceed with this, we may get the goal state f. ii) Steepest ascent hill climbing A useful variation on simple hill climbing considers all the moves from the current state and selects the best one as the next state. It is also called gradient search. This contrasts with the basic method in which first state is better then current state is selected. Algorithm 1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise, continue with the initial state as the current state. 2. Loop until a solution is found or until a complete iteration produces no change to current state. a. Let SUCC be a state such that any possible successor of the current will be better than SUCC. b. For each operator that applies to the current state, do: i. Apply the operator and generate a new state. ii. Evaluate the new state. If it is a goal state, then return it and quit. If not, compare it to SUCC. If it is better, then set SUCC to this state. If it is not better, leave SUCC alone. c. if the SUCC is better than current state, then set current state to SUCC.

An example is shown below. Consider a problem. Initial state is given as a. the final state is m. Let h(n) be a heuristic function for evaluating the states . Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 35

CS010 802

Artificial Intelligence

In this, all the child states (successors) of a are generated first. The state d has the least value of heuristic function. So d is the best among the successors of a. then d is better than the current state a. so make d as the new current state. Then start hill climbing from d. Both basic and steepest ascent hill climbing may fail to find a solution. Either algorithm may stop not by finding a goal state but by getting to a state from which no better states can be generated. This will happen if the program has reached either a local maximum, a plateau or a ridge. State space of hill climbing

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 36

CS010 802

Artificial Intelligence

ISSUES IN HILL CLIMBING 3 problems are 1) Local Maxima 2) Ridge 3) Plateau Here in problem solving, our aim is to find the global maximum. 1)Local maxima A local maximum is a state that is better than all its neighbors but is not better than some other states farther away. It is simply a peak.

2)Ridge It is a special kind of local maxima having very steep slope which is difficult to be traced in one calculation of objective function.

3)Plateau A plateau is a flat area of the search space in which a whole set of neighboring states have the same value, where next move does not give better solution than present state. On a plateau, it is not possible to determine the best direction in which to move by making local comparisons.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 37

CS010 802

Artificial Intelligence

There are some ways of dealing with these problems. Backtrack to some earlier node and try going in a different direction. This is a fairly good way of dealing with local maxima. Make a big jump in some direction to try to get to a new section of the search space. This is a good way of dealing with plateaus. Apply 2 or more rules before doing the test. This corresponds to moving in several directions at once. This is a good way for dealing with ridges

iii)n Simulated annealing We have seen that hill climbing never makes a down hill move. As a result, it can stuck on a local maximum. In contrast, a purely random wall- that is, moving to a successor chosen uniformly at random from the set of successors- is complete, but extremely inefficient. Simulated annealing combines hill climbing with a random walk. As a result, it yields efficiency and completeness. Simulated annealing is a variation of hill climbing in which, at the beginning of the process, some downhill moves may be made. The idea is to do enough exploration of the whole space early on so that the final solution is relatively insensitive to the starting state. This should lower the chances of getting caught at a local maximum, a plateau or a ridge. In metallurgy, annealing is the process in which metals are melted and then gradually cooled until some solid state is reached. It is used to tamper or harden metals and glass by heating them to a high temperature and then gradually cooling them, thus allowing the material to coalesce in to a low energy crystalline state. Physical substances usually move from higher energy configurations to lower ones. But there is some probability that a transition to a higher energy state will occur. This probability is given by the function

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 38

CS010 802

Artificial Intelligence

P = e-∆E / kT Where ∆E is positive change in the energy level, T is the temperature and k is Boltzmann‟s constant. Physical annealing has some properties. The probability of a large uphill move is lower than the probability of a small one. Also, the probability that an uphill move will be made decreases as the temperature decreases. Large uphill moves may occur early on, but as the process progresses, only relatively small upward moves are allowed until finally the process converges to a local maximum configuration. If cooling occurs so rapidly, stable regions of high energy will form. If however, a slower schedule is used, a uniform crystalline structure which corresponds to a global minimum is more likely to develop. If the schedule is too slow, time is wasted. These properties of physical annealing can be used to define the process of simulated annealing. In this process, ∆E represents change in the value of heuristic evaluation function instead of change in energy level. k can be integrated in to T. Hence we use the revised probability formula

P‟ = e-∆E / T The proper annealing scheduling is maintained to monitor T. differences from hill-climbing search i)The annealing schedule is maintained. ii)Moves to worse states are also accepted iii)In addition to current state, the best state record is also maintained. Algorithm 1. Evaluate the initial state. If it is also a goal state, then return it and quit. Otherwise, continue with the initial state as the current state. 2. Initialise BEST-SO-FAR to the current state. 3. Initialize T according to the annealing schedule. 4. Loop until a solution is found or until there are no new operators left to be applied in the current state. a. Select an operator that has not yet been applied to the current state and apply it to produce a new state. b. Evaluate the new state. Compute

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 39

CS010 802

Artificial Intelligence

∆E = (value of current) – (value of new state)    



If the new state is a goal state, then return it and quit. 



If it is not a goal state, but is better than the current state, then make it the current state. Also set BEST-SO-FAR to this new state. 



If it is not better than the current state, then make it the current state with probability p‟ as defined above. This step is usually implemented by invoking a random number generator to produce a number in the range [ 0,  1]. If that number is less than p‟, then the move is accepted. Otherwise, do nothing. 

c. Revise T as necessary according to the annealing schedule. 5. Return BEST- SO- FAR as the answer. To implement this algorithm, it is necessary to maintain an annealing schedule. That is first we must decide the initial value to be used for temperature. The second criterion is to decide when the temperature of the system should be reduced. The third is the amount by which the temperature will be reduced each time it is changed. In this algorithm, instead of picking the best move, it picks a random move. If the move improves the situation, it is always accepted. Otherwise the algorithm accepts the move with some probability less than 1. The probability decreases exponentially with the amount ∆E by which the evaluation is worsened. The probability also decreases as the temperature T goes down. Thus bad moves are more likely to be allowed at the start when the temperature is high, and they become more unlikely as T decreases. Simulated annealing was first used extensively to solve VLSI layout problems.

PYTHON Python is a high-level, interpreted, interactive and object-oriented scripting language. Python was designed to be highly readable which uses English keywords frequently where as other languages use punctuation and it has fewer syntactical constructions than other languages. • Python is interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. This is similar to PERL and PHP. • Python is Interactive: This means that you can actually sit at a Python prompt and interact with the interpreter directly to write your programs. • Python is Object-Oriented: This means that Python supports Object-Oriented style or technique of programming that encapsulates code within objects.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 40

CS010 802

Artificial Intelligence

• Python is Beginner's Language: Python is a great language for the beginner programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games Python Features: Python's feature highlights include: • Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language in a relatively short period of time. • Easy-to-read: Python code is much more clearly defined and visible to the eyes. • Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain. • A broad standard library: One of Python's greatest strengths are the bulk of the library is very portable and cross-platform compatible on UNIX, Windows and Macintosh. Interactive Mode: Support for an interactive mode in which you can enter results from a terminal right to the language, allowing interactive testing and debugging of snippets of code. • Portable: Python can run on a wide variety of hardware platforms and has the same interface on all platforms. • Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient. • Databases: Python provides interfaces to all major commercial databases. • GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh and the X Window system of UNIX. • Scalable: Python provides a better structure and support for large programs than shell scripting. Apart from the above-mentioned features, Python has a big list of good features, few are listed below: • Support for functional and structured programming methods as well as OOP. • It can be used as a scripting language or can be compiled to byte-code for building large applications. • Very high-level dynamic data types and supports dynamic type checking. • Supports automatic garbage collection. • It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 41

CS010 802

Artificial Intelligence

Python Data Structures. The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth. Python has six built-in types of sequences, but the most common ones are lists and tuples, which we would see in this tutorial. There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements. Python Lists: The list is a most versatile data type available in Python which can be written as a list of commaseparated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type. Creating a list is as simple as putting different comma-separated values between square brackets. For example: list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5] list3 = ["a", "b", "c", "d"] Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on. Accessing Values in Lists: To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example: list1 = ['physics', 'chemistry', 1997, 2000] list2 = [1, 2, 3, 4, 5, 6, 7 ] print "list1[0]: ", list1[0] print "list2[1:5]: ", list2[1:5] When the above code is executed, it produces the following result: list1[0]: physics list2[1:5]: [2, 3, 4, 5] Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 42

CS010 802

Artificial Intelligence

Updating Lists: You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

Delete List Elements: To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. Following is a simple example: list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1 When the above code is executed, it produces following result:

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 43

CS010 802

Artificial Intelligence

['physics', 'chemistry', 1997, 2000] After deleting value at index 2 : ['physics', 'chemistry', 2000]

Note: remove() method is discussed in subsequent section. Basic List Operations: Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string. In fact, lists respond to all of the general sequence operations we used on strings in the prior chapter.

Python Tuples Tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The only difference is that tuples can't be changed i.e., tuples are immutable and tuples use parentheses and lists use square brackets. Creating a tuple is as simple as putting different comma-separated values and optionally you can put these comma-separated values between parentheses also. For example: tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5 ) tup3 = "a", "b", "c", "d" The empty tuple is written as two parentheses containing nothing: Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 44

CS010 802

Artificial Intelligence

tup1 = () To write a tuple containing a single value you have to include a comma, even though there is only one value: tup1 = (50,) Like string indices, tuple indices start at 0, and tuples can be sliced, concatenated and so on Accessing Values in Tuples: To access values in tuple, use the square brackets for slicing along with the index or indices to obtain value available at that index. Following is a simple example: tup1 = ('physics', 'chemistry', 1997, 2000) tup2 = (1, 2, 3, 4, 5, 6, 7 ) print "tup1[0]: ", tup1[0] print "tup2[1:5]: ", tup2[1:5]

When the above code is executed, it produces the following result: tup1[0]: physics tup2[1:5]: [2, 3, 4, 5] Updating Tuples: Tuples are immutable which means you cannot update them or change values of tuple elements. But we able to take portions of an existing tuples to create a new tuples as follows. Following is a simple example: tup1 = (12, 34.56) tup2 = ('abc', 'xyz') # Following action is not valid for tuples # tup1[0] = 100 # So let's create a new tuple as follows tup3 = tup1 + tup2 print tup3

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 45

CS010 802

Artificial Intelligence

When the above code is executed, it produces the following result: (12, 34.56, 'abc', 'xyz') Delete Tuple Elements: Removing individual tuple elements is not possible. There is, of course, nothing wrong with putting together another tuple with the undesired elements discarded. To explicitly remove an entire tuple, just use the del statement. Following is a simple example: tup = ('physics', 'chemistry', 1997, 2000) printtup deltup print "After deleting tup : " print tup This will produce following result. Note an exception raised, this is because after deltup tuple does not exist anymore: ('physics', 'chemistry', 1997, 2000) After deleting tup : Traceback (most recent call last): File "test.py", line 9, in printtup; NameError: name 'tup' is not defined Basic Tuples Operations: Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string.

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 46

CS010 802

Artificial Intelligence

’ Indexing, Slicing, and Matrixes: Because tuples are sequences, indexing and slicing work the same way for tuples as they do for strings. Assuming following input: L = ('spam', 'Spam', 'SPAM!')

Python Dictionary A dictionary is mutable and is another container type that can store any number of Python objects, including other container types. Dictionaries consist of pairs (called items) of keys and their corresponding values. Python dictionaries are also known as associative arrays or hash tables. The general syntax of a dictionary is as follows: dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'} You can create dictionary in the following way as well: dict1 = { 'abc': 456 } Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 47

CS010 802

Artificial Intelligence

dict2 = { 'abc': 123, 98.6: 37 } Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}. Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

Accessing Values in Dictionary: To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example. dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Name']: ", dict['Name'] print "dict['Age']: ", dict['Age'] When the above code is executed, it produces the following result: dict['Name']: Zara dict['Age']: 7 If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} print "dict['Alice']: ", dict['Alice'] When the above code is executed, it produces the following result: dict['Zara']: Traceback (most recent call last): File "test.py", line 4, in print "dict['Alice']: ", dict['Alice']; KeyError: 'Alice' Updating Dictionary: Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 48

CS010 802

Artificial Intelligence

You can update a dictionary by adding a new entry or item (i.e., a key-value pair), modifying an existing entry, or deleting an existing entry as shown below in the simple example: dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8; # update existing entry dict['School'] = "DPS School"; # Add new entry print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School'] When the above code is executed, it produces the following result: dict['Age']: 8 dict['School']: DPS School Delete Dictionary Elements: You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation. To explicitly remove an entire dictionary, just use the delstatement. Following is a simple example: ict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} deldict['Name']; # remove entry with key 'Name' dict.clear(); # remove all entries in dict deldict ; # delete entire dictionary print "dict['Age']: ", dict['Age'] print "dict['School']: ", dict['School']; This will produce the following result. Note an exception raised, this is because after deldict dictionary does not exist anymore: dict['Age']: Traceback (most recentcall last): File "test.py", line 8, in

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 49

CS010 802

Artificial Intelligence

print "dict['Age']: ", dict['Age']; TypeError: 'type' object is unsubscriptable

Note: del() method is discussed in subsequent section. Properties of Dictionary Keys: Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys. There are two important points to remember about dictionary keys: (a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins. Following is a simple example: dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'} print "dict['Name']: ", dict['Name'] When the above code is executed, it produces the following result: dict['Name']: Manni (b) Keys must be immutable. Which means you can use strings, numbers or tuples as dictionary keys but something like ['key'] is not allowed. Following is a simple example: dict = {['Name']: 'Zara', 'Age': 7} print "dict['Name']: ", dict['Name'] When the above code is executed, it produces the following result: Traceback (most recent call last): File "test.py", line 3, in dict = {['Name']: 'Zara', 'Age': 7} TypeError: list objects are unhashable SAMPLE PROGRAMS IN PYTHON Program No:1 To read values from the keyboard print("enter a no")

Jeena Thomas, Asst. Professor, CSE, SJCET, Palai

Page 50

CS010 802

Artificial Intelligence

a=input() print (a) Program No:2 IF STATEMENT IN PYTHON. print ("IF STATEMENT IN PYTHON") print ("enter a no") a=int (input()) if a
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF