Introduction To The Knapsack Problem - 113848
Introduction To The Knapsack Problem - 113848
Mini Project
of
Artificial Intelligence
Supervised By:-
Presented by:-
Dr Abhijit Baruah
Bhabarnab Bhattacharjya (CSE-
05/20)
Bishal Basfore(CSE-06/20)
Digbijoy Chetry(CSE-11/20) Institution: DUIET, Dibrugarh
University
Introduction to the Knapsack Problem
The Knapsack Problem is a classic optimization problem in computer science and operations research.
It involves selecting a subset of items to be placed in a knapsack (or bag) such that the total value of the selected items is
maximized while ensuring that the total weight does not exceed the knapsack's capacity.
Example Scenario:
Suppose you have a knapsack that can hold a maximum weight of 10 pounds, and you have the following items:
•Item 1: Weight = 3 pounds, Value = $10
•Item 2: Weight = 4 pounds, Value = $20
•Item 3: Weight = 5 pounds, Value = $15
•Item 4: Weight = 6 pounds, Value = $25
The goal is to choose a combination of items that maximizes the total value while keeping the total weight within the knapsack's
capacity of 10 pounds.
Evaluating Fitness
Calculate the fitness of each candidate solution based on the total
value and weight of the selected items.
Code Explanation
• A structure item is defined to represent each item with its weight and value.
• An array items of size MAX_ITEMS is declared to store the items.
• num_items is a variable to store the actual number of items entered by the user.
• knapsack_capacity is a variable to store the maximum weight capacity of the knapsack.
• The fitness function calculates the fitness value (total value) of a given chromosome (solution).
• It iterates through all the items and adds the weight and value of the selected items (represented by 1 in the chromosome)
to the weight and value variables, respectively.
• If the total weight exceeds the knapsack capacity, it returns 0; otherwise, it returns the total value.
• initialize_population function initializes the population by randomly generating chromosomes (solutions).
• It iterates through each chromosome and each item, assigning a random value of 0 or 1 to represent
whether the item is selected or not.
• The crossover function performs the crossover operation between two parent chromosomes to create two new
offspring chromosomes.
• It selects a random crossover point and copies the genes (item selections) from the first parent to the first child
up to the crossover point.
• After the crossover point, the genes are swapped between the parents to create the remaining parts of the
children.
• The mutate function introduces random mutations in a chromosome.
• It iterates through each gene (item) in the chromosome and flips the value (0 to 1 or 1 to 0) with a probability equal to
the MUTATION_RATE.
• This helps introduce variation in the population and explore new solutions.
• The genetic_algorithm function is the main function that implements the genetic algorithm loop.
• It initializes the population, evaluates the fitness of each chromosome, selects the fittest chromosomes as parents,
performs crossover and mutation to create a new population, and repeats the process until an optimal solution is
found or the maximum number of generations is reached.
Main function:
Example output:
• If no optimal solution is found after the maximum number of generations, it will print a message indicating that no
optimal solution was found.
Main Genetic Algorithm Loop
Initialize Population
Generate a set of random candidate solutions (chromosomes) to
1 represent the initial population.
Evaluate Fitness
2 Calculate the fitness of each candidate solution based on the
defined fitness function.
Select Parents
3 Use selection methods like tournament or roulette
wheel to choose parent chromosomes for reproduction.
Perform Crossover
4 Apply crossover operators to the selected parent
chromosomes to create new offspring.
Mutate Offspring
Randomly modify the offspring
5
chromosomes using mutation operators to
introduce genetic diversity.
Time Complexity Analysis
The time complexity of the genetic algorithm implementation for the Knapsack Problem is primarily driven
by the size of the population and the number of generations.
The algorithm iterates through the population, performing crossover and mutation operations, and evaluating
the fitness of each individual solution.
The time complexity of the genetic algorithm can be approximated as O(G * P * n), where G is the number of
generations, P is the size of the population, and n is the number of items in the Knapsack Problem.
This is because the algorithm needs to evaluate the fitness of each individual in the population for each
generation, and the fitness evaluation requires iterating through the n items.
Space Complexity Analysis
The space complexity of the genetic algorithm for the Knapsack Problem is primarily determined by the size of the population
and the number of generations.
The algorithm maintains a population of candidate solutions, each represented as a binary string. The space required to store this
population scales linearly with the population size and the length of the binary strings, which is proportional to the number of
items in the Knapsack Problem.
O(N) O(N)
Population Size Binary Strings
The space complexity is proportional to the The length of the binary strings representing the
population size, which is typically a small candidate solutions is proportional to the number
constant. of items in the Knapsack Problem.
Additionally, the algorithm may require temporary storage for crossover and mutation operations, but this storage is
typically small compared to the population size.
Overall, the space complexity of the genetic algorithm for the Knapsack Problem is linear in the number of items, making
it suitable for solving problems of moderate size.