Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
96 views

Genetic Algorithms - Knapsack Problem - Knapsack Problem

Genetic algorithms are a type of machine learning algorithm inspired by biological evolution. They use techniques such as mutation, crossover, and selection to evolve solutions to a problem over multiple generations. A genetic algorithm represents potential solutions as strings called chromosomes and uses a fitness function to evaluate them. Through processes mimicking natural selection, the genetic algorithm iteratively improves solutions until an optimal or near-optimal solution is found. The document provides an example of using a genetic algorithm to solve the knapsack problem by representing solutions as binary strings and maximizing total value within a weight limit.

Uploaded by

Lekshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Genetic Algorithms - Knapsack Problem - Knapsack Problem

Genetic algorithms are a type of machine learning algorithm inspired by biological evolution. They use techniques such as mutation, crossover, and selection to evolve solutions to a problem over multiple generations. A genetic algorithm represents potential solutions as strings called chromosomes and uses a fitness function to evaluate them. Through processes mimicking natural selection, the genetic algorithm iteratively improves solutions until an optimal or near-optimal solution is found. The document provides an example of using a genetic algorithm to solve the knapsack problem by representing solutions as binary strings and maximizing total value within a weight limit.

Uploaded by

Lekshmi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Genetic Algorithms

- Knapsack Problem
Genetic Algorithms
• An algorithm is a set of instructions that is repeated to
solve a problem.
• A genetic algorithm conceptually follows steps inspired
by the biological processes of evolution.
• Genetic Algorithms follow the idea of SURVIVAL OF THE
FITTEST- Better and better solutions evolve from
previous generations until a near optimal solution is
obtained.
• Also known as evolutionary algorithms, genetic
algorithms demonstrate self organization and adaptation
similar to the way that the fittest biological organism
survive and reproduce.
Genetic Algorithms
• A genetic algorithm is an iterative procedure that
represents its candidate solutions as strings of genes
called chromosomes.
• Genetic Algorithms are often used to improve the
performance of other AI methods such as expert
systems or neural networks.
• The method learns by producing offspring that are
better and better as measured by a fitness function,
which is a measure of the objective to be obtained
(maximum or minimum).
How offspring are produced
• Reproduction- Through reproduction, genetic
algorithms produce new generations of improved
solutions by selecting parents with higher fitness
ratings or by giving such parents a greater probability
of being contributors and by using random selection.
• Crossover- Many genetic algorithms use strings of
binary symbols for chromosomes, as in our Knapsack
example, to represent solutions. Crossover means
choosing a random position in the string (say, after 2
digits) and exchanging the segments either to the right
or to the left of this point with another string
partitioned similarly to produce two new off spring.
Crossover Example

• Parent A 011011
• Parent B 101100
• “Mate the parents by splitting each number as
shown between the second and third digits
(position is randomly selected)
• 01*1011 10*1100
Crossover Example

• Now combine the first digits of A with the last digits of B,


and the first digits of B with the last digits of A
• This gives you two new offspring
– 011100
– 101011
• If these new solutions, or offspring, are better solutions
than the parent solutions, the system will keep these as
more optimal solutions and they will become parents.
This is repeated until some condition (for example
number of populations or improvement of the best
solution) is satisfied.
How offspring are produced
• Mutation- Mutation is an arbitrary change in a
situation. Sometimes it is used to prevent the
algorithm from getting stuck. The procedure
changes a 1 to a 0 to a 1 instead of duplicating
them. This change occurs with a very low
probability (say 1 in 1000)
Genetic Algorithm Operators
Mutation and Crossover

Parent 1 1 0 1 0 1 1 1

Parent 2 1 1 0 0 0 1 1

Child 1 1 0 1 0 0 1 1

Child 2 1 1 0 0 1 1 0 Mutation
Outline of the Basic Genetic Algorithm
1. [Start] Generate random population of n
chromosomes (suitable solutions for the problem)
2. [Fitness] Evaluate the fitness f(x) of each
chromosome x in the population
3. [New population] Create a new population by
repeating following steps until the new population
is complete
4. [Selection] Select two parent chromosomes
from a population according to their fitness (the
better fitness, the bigger chance to be selected)
The idea is to choose the better parents.
Outline of the Basic Genetic Algorithm
5. [Crossover] With a crossover probability cross over the
parents to form a new offspring (children). If no crossover
was performed, offspring is an exact copy of parents.
6. [Mutation] With a mutation probability mutate new
offspring at each locus (position in chromosome).
7. [Accepting] Place new offspring in a new
population
8. [Replace] Use new generated population for a further
run of algorithm
9. [Test] If the end condition is satisfied, stop, and return
the best solution in current population
10. [Loop] Go to step 2
Flow Diagram of the Genetic
Algorithm Process
Describe
Problem

Generate
Initial
Solutions

Step 1 Test: is initial Yes Stop


solution good enough?

No

Step 2 Select parents


to reproduce

Step 3 Apply crossover process


and create a set of offspring
Step 4
Step 5 Apply random mutation
Example: The Knapsack Problem
• You are going on an overnight hike and have a
number of items that you could take along. Each
item has a weight (in pounds) and a benefit or value
to you on the hike(for measurements sake let’s say,
in US dollars), and you can take one of each item at
most. There is a capacity limit on the weight you can
carry (constraint). This problem only illustrates one
constraint, but in reality there could be many
constraints including volume, time, etc.
GA Example: The Knapsack Problem

• Item: 1 2 3 4 5 6 7
• Benefit: 5 8 3 2 7 9 4
• Weight: 7 8 4 10 4 6 4
• Knapsack holds a maximum of 22 pounds
• Fill it to get the maximum benefit
• Solutions take the form of a string of 1’s and 0’s
• Solutions: Also known as strings of genes called
Chromosomes
– 1. 0101010
– 2. 1101100
– 3. 0100111
Example: The Knapsack Problem
• We represent a solution as a string of seven 1s and
0s and the fitness function as the total benefit, which
is the sum of the gene values in a string solution
times their representative benefit coefficient.
• The method generates a set of random solutions
(initial parents), uses total benefit as the fitness
function and selects the parents randomly to create
generations of offspring by crossover and mutation.
Knapsack Example
• Typically, a string of 1s and 0s can represent a
solution.
• Possible solutions generated by the system
using Reproduction, Crossover, or Mutations
– 1. 0101010
– 2. 1101100
– 3. 0100111
Knapsack Example
Solution 1
Item 1 2 3 4 5 6 7

Solution 0 1 0 1 0 1 0

Benefit 5 8 3 2 7 9 4

Weight 7 8 4 10 4 6 4

• Benefit 8 + 2 + 9 = 19
• Weight 8 + 10 + 6 = 24
Knapsack Example
Solution 2
Item 1 2 3 4 5 6 7
Solution 1 1 0 1 1 0 0

Benefit 5 8 3 2 7 9 4
Weight 7 8 4 10 4 6 4

• Benefit 5 + 8 + 7 = 20
• Weight 7 + 8 + 4 = 19
Knapsack Example
Solution 3
Item 1 2 3 4 5 6 7

Solution 0 1 0 0 1 1 1

Benefit 5 8 3 2 7 9 4

Weight 7 8 4 10 4 6 4

• Benefit 8 + 7 + 9 + 4 = 28
• Weight 8 + 4 + 6 + 4 = 22
Knapsack Example
• Solution 3 is clearly the best solution and has met
our conditions, therefore, item number 2, 5, 6, and 7
will be taken on the hiking trip. We will be able to get
the most benefit out of these items while still having
weight equal to 22 pounds.
• This is a simple example illustrating a genetic
algorithm approach.
Genetic Algorithms
• Genetic Algorithms are a type of machine learning
for representing and solving complex problems.
• They provide a set of efficient, domain-independent
search heuristics for a broad spectrum of
applications.
• A genetic algorithm interprets information that
enables it to reject inferior solutions and accumulate
good ones, and thus it learns about its universe.
Genetic Algorithm Application Areas

• Dynamic process control


• Induction of rule optimization
• Discovering new connectivity topologies
• Simulating biological models of behavior and evolution
• Complex design of engineering structures
• Pattern recognition
• Scheduling
• Transportation
• Layout and circuit design
• Telecommunication
• Graph-based problems
Example: The Knapsack Problem

• Represent a solution as a string of seven 1s and 0s


Fitness function as the total benefit
Selects the parents randomly
Reproduction to create generations of offspring by crossover
and mutation.
Knapsack Example

Item 1 2 3 4 5 6 7
Solution 1 0 1 0 1 0 1 0 • Benefit 8 + 2 + 9 = 19
Benefit 5 8 3 2 7 9 4 • Weight 8 + 10 + 6 = 24
Weight 7 8 4 10 4 6 4

Item 1 2 3 4 5 6 7
Solution 2 1 1 0 0 1 0 0  Benefit 5 + 8 + 7 = 20
 Weight 7 + 8 + 4 = 19
Benefit 5 8 3 2 7 9 4
Weight 7 8 4 10 4 6 4

Item 1 2 3 4 5 6 7
Solution 3 0 1 0 0 1 1 1  Benefit 8 + 7 + 9 + 4 = 28
Benefit 5 8 3 2 7 9 4  Weight 8 + 4 + 6 + 4 = 22
Weight 7 8 4 1 4 6 4
0
Application in M/C learning
• M/C learning key application field of GA
• Major concern is with artificial neural nets
• Works with NN in 3 basic ways
1. Adjust parameters – act as learning algorithm
2. Determine the structure of neural net ie. the no.
of neurons in each layer
3. Automatically adjust the parameters of a prototype
learning equation
GA alternative to BP learning
Adaptation of learning rules/control law by GA
GA alternative to BP learning
• BP adjust the weight.
• It may some time trap at local minima
• GA- mutation overcome this
• i/p : x1,x2,x3
• o/p :Y1,Y2
• Weight 1st layer :W1…W6
• Weight 2nd layer : G1..G4
• Desired o/p (d1,d2)
• Selection function is to minimize Z
• Z=[(d1-y1)2+(d2-y2)2]1/2
• Chromosome present has 10 fields (w1,…,W6,G1,..G4)
• Typical crossover and mutation can be used to evolve the
weight
Adaptation of learning
• Supervised learning must generate desired o/p
for a given i/p
• Assume multilayered feed forward NN is used as the learning
agent
• Ok be the kth o/p node It be the tth i/p node
• wij weight connected from node i to node j
• The learning rule in general can be written as ∆wij =f(It,Oj,wij)
• wij(t+1)= wij(t)+∆ wij
• Let f(Ii, Oj, wij) be a function of a,b,c,d,e
• Fitness of the chromosome is measured by error signal at the
o/p layer
• After number of evolution the near optimal value of a,b,c,d,e
can be found – ie. new learning rule is framed
Issues for GA Practitioners

• Choosing basic implementation issues:


– representation
– population size, mutation rate, ...
– selection, deletion policies
– crossover, mutation operators
• Termination Criteria
• Performance, scalability
• Solution is only as good as the evaluation function (often
hardest part)
Benefits of Genetic Algorithms
• Concept is easy to understand
• Modular, separate from application
• Supports multi-objective optimization
• Good for “noisy” environments
• Always an answer; answer gets better with time
• Inherently parallel; easily distributed
• Easy to exploit previous or alternate solutions
• Many ways to speed up and improve a GA-based application
as knowledge about problem domain is gained

You might also like