Sunteți pe pagina 1din 14

0/1 Knapsack

with Genetic
Algorithms

swatimali@somaiya.edu,
K. J. Somaiya CoE,
Mumbai, Maharashtra.
RCID-1016
Problem characteristics[1]
Isthe problem decomposable?
Does the problem have a unique solution
or set of solutions?
Any solution or best solution?
Solution is a state or a path?
Can solution steps be undone?
Optimization problems
Choose the best solution amongst others
Quality
Efficiency
Why Genetic algorithms?[2]
Problem solving through searching
Disadvantages with Linear search
Genetic algorithms

A successor state is generated by


combining two parent states
Start with k randomly generated states
(population)
A state is represented as a string over a
finite alphabet (often a string of 0s and 1s)
Evaluation function (fitness function).
Higher values for better states.
Produce the next generation of states by
selection, crossover, and mutation
Genetic Algorithms- General Method
function GENETIC-ALGORITHM(Population, Fitness-FN)returns an
individual
inputs: population, a set of individuals
FITNESS-FN, a function that measures the fitness of an individual
repeat
new-population empty set
loop for i from 1 to Size(population)do
x <- Random-Selection(Population, Fitness-FN)
x <- Random-Selection(Population, Fitness-FN)
child REPRODUCE(x,y)
if (small random probability) then child Mutate(child)
add child to new-population
population new-population
until some individual is fit enough, or enough time has elapsed
return the best individual in population, according to FITNESS-FN
0/1 Knapsack
Problem definition:
Given a knapsack with capacity M and N
items with weights wi and benefit bi for
1<=i<=N, the problem is to
Maximize Pi*Xi ,
Subject to the condition
Wi*X* <=M where Xi= 0 or 1.
0/1 Knapsack Solved with GA
Let N=4, (w,b)={ (7,42), (3,12), (4,40),(5,25)}, M=10
Step 1: Generate Random Initial Population with some
Fitness Function (Profit)
A B C D Weight Profit

0 0 1 0 4 40

0 1 0 1 8 37

1 1 0 0 10 54

1 0 0 0 7 42

0 1 1 0 7 52

0 0 0 1 5 25

0 0 1 0 3 12
0/1 Knapsack Solved with GA
Step 2: Random selection of chromosomes
x1= 0 0 1 0
y1= 0 1 0 1
X2= 1 1 0 0
y2= 1 0 0 0
x3= 0 1 1 0
y3= 0 0 0 1

Step 3: Reproduce with some crossover point


Weight Profit
x1'= 0 0 0 1 5 25
y1'= 0 1 1 0 7 52
X2'= 1 1 0 0 10 54
y2'= 1 0 0 0 7 42
x3'= 0 1 0 1 8 37
y3'= 0 0 1 0 4 40
Mutation ;(Flip Random bit)
Weight Profit

x1'= 0 0 1 1 9 65

y1'= 1 1 1 0 >M NA

X2'= 1 1 0 1 >M NA

y2'= 0 0 0 0 0 0

x3'= 0 1 0 0 3 12

y3'= 0 1 1 0 7 52

After first Generation, profit=65


Genetic algorithms

Fitness function: number of non-attacking pairs


of queens (min = 0, max = 8 7/2 = 28)
24/(24+23+20+11) = 31%
23/(24+23+20+11) = 29%
Genetic algorithms
Merits & Demerits
Solution in linear amount of time.[4]
Limitation - Doesn't necessarily find the global
optimal solution
Short running time
Memory usage depends on number of individuals
but is generally manageable
Quality of solution depends on choosing an
efficient representation + letting it run long enough
Reasonably simple to implement, design decisions
may be a little more complex, especially if you
don't have significant experience with GAs[2]
References
1. Rich, Knight, Artificial Intelligence, Tata
McGraw Hill Publication, 2nd Edition
2. http://stackoverflow.com/questions/14848874/w
hich-is-the-best-method-between-genetic-
algorithm-and-dynamic-programming-to-so,
retrieved April 02,2015
3. Rusel, Norving, Artificial Intelligence: A modern
Approach, Pearson Education, 2nd Ed
4. Singh, R.P., "Solving 01 Knapsack problem using
Genetic Algorithms," Communication Software
and Networks (ICCSN), 2011 IEEE 3rd International
Conference on , vol., no., pp.591,595, 27-29 May
2011, doi: 10.1109/ICCSN.2011.6013975

S-ar putea să vă placă și