0% found this document useful (0 votes)
9 views17 pages

Introduction To The Knapsack Problem - 113848

The document presents a mini project on the Knapsack Problem, detailing its optimization nature and the application of genetic algorithms to solve it. It outlines the process of encoding the problem, initializing a population, evaluating fitness, and the main genetic algorithm loop, along with time and space complexity analyses. The project aims to maximize the total value of selected items within a weight constraint using evolutionary strategies.

Uploaded by

alankritadeka12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Introduction To The Knapsack Problem - 113848

The document presents a mini project on the Knapsack Problem, detailing its optimization nature and the application of genetic algorithms to solve it. It outlines the process of encoding the problem, initializing a population, evaluating fitness, and the main genetic algorithm loop, along with time and space complexity analyses. The project aims to maximize the total value of selected items within a weight constraint using evolutionary strategies.

Uploaded by

alankritadeka12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Group – 12

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.

The problem can be formally stated as:


Given a set of items, each with a weight and a value, and a knapsack with a maximum weight capacity, find the subset of items that
maximizes the total value while ensuring that the total weight does not exceed the knapsack's capacity.
Overview of Genetic Algorithms

Inspiration from Iterative Fitness-Based Diverse


Nature Optimization Evaluation Exploration

Genetic algorithms These algorithms Each solution is Genetic algorithms


are inspired by the start with an initial evaluated based on excel at exploring a
natural process of population of a defined fitness large, complex
evolution, where potential solutions, function, which search space and
organisms adapt and then iteratively apply quantifies how discovering global
evolve over genetic operators well the solution optima, unlike local
successive like selection, solves the problem. search methods that
generations to better crossover, and Fitter solutions are can get stuck in
suit their mutation to produce more likely to be suboptimal
environment. new, fitter selected for solutions.
generations. reproduction.
Genetic Algorithm Flowchart

 The genetic algorithm follows a structured flow to solve


complex optimization problems like the Knapsack
Problem.
 It begins by generating an initial population of candidate
solutions, then iteratively applies selection, crossover, and
mutation to evolve the population towards better solutions
over successive generations.
Defining the Fitness Function

Objective Function Weight Constraints Maximize Value


The objective function is the The fitness function must The ultimate goal is to
key to evaluating the quality of account for the weight limit of maximize the total value of the
a candidate solution. It the knapsack, ensuring that the items selected, subject to the
determines how well a given set total weight of the selected weight constraint. The fitness
of items satisfies the knapsack items does not exceed the function should guide the
constraints. capacity. genetic algorithm towards this
objective.
Implementing the Genetic Algorithm

Encoding the Problem


Represent the knapsack problem as a sequence of binary genes, where each gene
corresponds to including or excluding an item.

Initializing the Population


Generate an initial population of candidate solutions, each with a
unique set of genes representing item selections.

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:

The main function serves as the entry point of the program:


1. It seeds the random number generator using the current time (srand(time(NULL))).
2. It prompts the user to enter the number of items and reads the input using scanf.
3. It prompts the user to enter the weight and value of each item and reads the inputs using scanf.
4. It prompts the user to enter the knapsack capacity and reads the input using scanf.
5. Finally, it calls the genetic_algorithm function, which implements the genetic algorithm to solve
the Knapsack problem.
Output:

• The program will output one of two possible results:


• If an optimal solution is found within the maximum number of generations (MAX_GENERATIONS), it will print the generation number
and the selected items (with their weights and values) that form the optimal solution.

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.

You might also like