41
Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology (CIIT) Islamabad, Pakistan.

Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Embed Size (px)

Citation preview

Page 1: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Artificial IntelligenceLecture No. 31

Dr. Asad Ali Safi

Assistant Professor,Department of Computer Science,

COMSATS Institute of Information Technology (CIIT) Islamabad, Pakistan.

Page 2: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Summary of Previous Lecture

• Unsupervised learning• Unsupervised learning Approaches • Self Organizing Map (SOM)

Page 3: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Today’s Lecture

• Genetic algorithms• GA Requirements• Theory of Evolution• GA Strengths• GA Weaknesses

Page 4: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic algorithms

• Genetic algorithms were formally introduced in the United States in the 1970s by John Holland at University of Michigan.

• The continuing price/performance improvements of computational systems has made them attractive for some types of optimization.

• In particular, genetic algorithms work very well on mixed (continuous and discrete), combinatorial problems. They are less susceptible to getting 'stuck' at local optima than gradient search methods. But they tend to be computationally expensive.

Page 5: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic Algorithms• Genetic algorithms are a particular class of

evolutionary algorithms that use techniques inspired by evolutionary biology such as inheritance, mutation, selection, and crossover (also called recombination).

Page 6: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

What is GA

• Genetic algorithms are implemented as a computer simulation in which a population of abstract representations (called chromosomes or the genotype or the genome) of candidate solutions (called individuals, creatures, or phenotypes) to an optimization problem evolves toward better solutions.

• Traditionally, solutions are represented in binary as strings of 0s and 1s, but other encodings are also possible.

Page 7: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

What is GA

• The evolution usually starts from a population of randomly generated individuals and happens in generations.

• In each generation, the fitness of every individual in the population is evaluated, multiple individuals are selected from the current population (based on their fitness), and modified (recombined and possibly mutated) to form a new population.

Page 8: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

What is GA

• The new population is then used in the next iteration of the algorithm.

• Commonly, the algorithm terminates when either a maximum number of generations has been produced, or a satisfactory fitness level has been reached for the population.

• If the algorithm has terminated due to a maximum number of generations, a satisfactory solution may or may not have been reached.

Page 9: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Chromosome, Genes andGenomes

Page 10: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genotype and Phenotype

• Genotype:– Particular set of genes in a genome

• Phenotype:– Physical characteristic of the genotype (smart, beautiful, healthy, etc.)

Page 11: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

GA Requirements• A typical genetic algorithm requires two things to be defined:• a genetic representation of the solution domain, and• a fitness function to evaluate the solution domain.

• A standard representation of the solution is as an array of bits. Arrays of other types and structures can be used in essentially the same way.

• The main property that makes these genetic representations convenient is that their parts are easily allied due to their fixed size, that facilitates simple crossover operation.

• Variable length representations may also be used, but crossover implementation is more complex in this case.

• Tree-like representations are explored in Genetic programming.

Page 12: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Representation

Chromosomes could be:– Bit strings (0101 ... 1100)– Real numbers (43.2 -33.1 ... 0.0 89.2) – Permutations of element (E11 E3 E7 ... E1 E15)– Lists of rules (R1 R2 R3 ... R22 R23)– Program elements (genetic programming)– ... any data structure ...

Page 13: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

GA Requirements• The fitness function is defined over the genetic representation

and measures the quality of the represented solution. • The fitness function is always problem dependent. • For instance, we want to maximize the total value of objects

that we can put in a knapsack of some fixed capacity. • A representation of a solution might be an array of bits, where

each bit represents a different object, and the value of the bit (0 or 1) represents whether or not the object is in the knapsack.

• Not every such representation is valid, as the size of objects may exceed the capacity of the knapsack.

• The fitness of the solution is the sum of values of all objects in the knapsack if the representation is valid, or 0 otherwise. In some problems, it is hard or even impossible to define the fitness expression; in these cases, interactive genetic algorithms are used.

Page 14: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

A fitness function

Page 15: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Basics of GA• The most common type of genetic algorithm works

like this: – a population is created with a group of individuals created randomly. – The individuals in the population are then evaluated. – The evaluation function is provided by the programmer and gives the

individuals a score based on how well they perform at the given task. – Two individuals are then selected based on their fitness, the higher

the fitness, the higher the chance of being selected. – These individuals then "reproduce" to create one or more offspring,

after which the offspring are mutated randomly. – This continues until a suitable solution has been found or a certain

number of generations have passed, depending on the needs of the programmer.

Page 16: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Theory of Evolution• Every organism has unique attributes that can be

transmitted to its offspring• Offspring are unique and have attributes from each

parent• Selective breeding can be used to manage changes

from one generation to the next• Nature applies certain pressures that cause

individuals to evolve over time

Page 17: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Evolutionary Pressures• Environment

– Creatures must work to survive by finding resources like food and water

• Competition– Creatures within the same species compete with each

other on similar tasks– Rivalry– Different species affect each other by direct confrontation

(e.g. hunting) or indirectly by fighting for the same resources

Page 18: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Natural Selection• Creatures that are not good at completing tasks like

hunting have fewer chances of having offspring• Creatures that are successful in completing basic

tasks are more likely to transmit their attributes to the next generation since there will be more creatures born that can survive and pass on these attributes

Page 19: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic Algorithmic Process

• Potential solution for problem domains are encoded using machine representation (e.g. bit strings) that supports variation and selection operations

• Mating and mutation operations produce new generation of solutions from parent encodings

• Fitness function judges the individuals that are “best” suited (e.g. most appropriate problem solution) for “survival”

Page 20: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Initialization• Initial population must be a representative sample of

the search space• Random initialization can be a good idea (if the

sample is large enough)• Random number generator can not be biased• Can reuse or seed population with existing genotypes

based on algorithms or expert opinion or previous evolutionary cycles

Page 21: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Evaluation• Each member of the population can be seen as

candidate solution to a problem• The fitness function determines the quality of each

solution• The fitness function takes a phenotype and returns a

floating point number as its score– It is problem dependent so can be very simple– It can be a bottleneck if it is not carefully thought out

(there are magic ways to create them)

Page 22: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Selection

• Want to give preference to “better” individuals to add to mating pool

• If entire population ends up being selected it may be desirable to conduct a tournament to order individuals in population

• Would like to keep the best in the mating pool and drop the worst (elitism)

• Elitism is trade-off with search space completeness

Page 23: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Crossover

• In reproduction the genetic codes of both parents are combined to create offspring

• A crossover has no impact on the mating pool• Would like to keep 60/40 split between parent

contributions• 95/5 splits negate the benefits of crossover

Page 24: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Crossover

• If we have selected two stringsA = 11111 and B = 00000

• We might choose a uniformly random site (e.g. position 3) and trade bits

• This would create two new stringsA’ =11100 and B’ = 00011

• These new strings might then be added to the mating pool if they are “fit”

Page 25: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Mutation

• Mutations happen at the genome level (rarely and not good) and the genotype level (better for the GA process)

• Mutation is important for maintaining diversity in the genetic code

• In humans, mutation was responsible for the evolution of intelligence

• Example: The occasional (low probably) alteration of a bit position in a string

Page 26: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Operators

• Selection and mutation– When used together give us a genetic algorithm equivalent

of to parallel, noise tolerant, hill climbing algorithm

• Selection, crossover, and mutation– Provide an insurance policy against losing population

diversity and avoiding some of the pitfalls of ordinary “hill climbing”

Page 27: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Replacement

• Determine when to insert new offspring into the mating pool and which individuals to drop out based on fitness

• Steady state evolution calls for the same number of individuals in the population, so each new offspring processed one at a time so fit individuals can remain a long time

• In generational evolution, the offspring are placed into a new population with all other offspring (genetic code only survives in kids)

Page 28: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic Algorithm

Set time t = 0Initialize population P(t)While termination condition not met

Evaluate fitness of each member of P(t)Select members from P(t) based on fitnessProduce offspring from the selected pairsReplace members of P(t) with better offspringSet time t = t + 1

Page 29: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Why use genetic algorithms?

• They can solve hard problems• Easy to interface genetic algorithms to existing

simulations and models• GA’s are extensible • GA’s are easy to hybridize• GA’s work by sampling, so populations can be

sized to detect differences with specified error rates

• Use little problem specific code

Page 30: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Traveling Salesman Problem

• To use a genetic algorithm to solve the traveling salesman problem we could begin by creating a population of candidate solutions

• We need to define mutation, crossover, and selection methods to aid in evolving a solution from this population

• At random pick two solutions and combine them to create a child solution, then a fitness function is used to rank the solutions

Page 31: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Traveling Salesman Problem

• For crossover we might take two paths (P1 and P2) break them at arbitrary points and define new solutions Left1+Right2 and Left2+Right1

• For mutation we might randomly switch two cites in an existing path

Page 32: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Evolve Algorithm for TSP

• Set up initial population• For G generations

– Create M mutations and add them to the population– Subject mutations to population constraints and determine

their relative fitness– Create C crossovers and add them to the population– Subject crossovers to population constraints and determine

their relative fitness

Page 33: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Solving TSP using GASteps:

1. Create group of random tours• Stored as sequence of numbers (parents)

2. Choose 2 of the better solutions• Combine and create new sequences (children)

Problems here:

City 1 repeated in Child 1

City 5 repeated in Child 2

Page 34: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Modifications Needed

• Algorithm must not allow repeated cities• Also, order must be considered

– 12345 is same as 32154• Based upon these considerations, a computer

model for N cities can be created• Gets quite detailed

Page 35: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic Algorithm Example

A AB B

C C

D D

E E

Parent A Parent B

Page 36: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

A B

C

D

E

Genetic Algorithm Example

A

A A

AA

B

B

B

B

B

Combined Path

Page 37: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Genetic Algorithm Example

B

A B

C

D

E

A

A

B

B

Child

Page 38: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Mutations

Chance of 1 in 50 to introduce a mutation to the next generation (the child if it replaces a parent, or the first parent)

E B F D G A C

R1 R2

E A G D F B C

Page 39: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

GA Strengths

• Do well at avoiding local minima and can often times find near optimal solutions since search is not restricted to small search areas

• Easy to extend by creating custom operators • Perform well for global optimizations• Work required to choose representations and

conversion routines is acceptable

Page 40: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

GA Weaknesses

• Do not take advantage of domain knowledge• Not very efficient at local optimization (fine tuning

solutions)• Randomness inherent in GA make them hard to

predict (solutions can take a long time to stumble upon)

• Require entire populations to work (takes lots of time and memory) and may not work well for real-time applications

Page 41: Artificial Intelligence Lecture No. 31 Dr. Asad Ali Safi  Assistant Professor, Department of Computer Science, COMSATS Institute of Information Technology

Summery of Today’s Lecture

• Genetic algorithms• GA Requirements• Theory of Evolution• GA Strengths• GA Weaknesses