You are on page 1of 8

Case study

The knapsack problem is a common problem in computer science and operations


research. It involves trying to fit a set of items into a knapsack (a bag or container) with limited
capacity. The goal is to maximize the value of the items that are included in the knapsack. The
problem is often formulated as a mathematical optimization problem, where the objective is to
maximize the value of the items subject to the constraint that the total weight of the items must
be less than or equal to the capacity of the knapsack.

Hikers may need to optimize the items in their bag to make sure that they have enough
room to carry all the necessary supplies, such as food, water, and shelter. By using a genetic
algorithm to solve the knapsack problem, hikers can determine the optimal combination of
items to include in their bag, given the limited space and weight capacity. This can help hikers
ensure that they have the supplies they need without overloading themselves and hindering
their ability to move quickly and efficiently. Additionally, by maximizing the value of the items
in their bag, hikers can ensure that they have the most useful and necessary supplies for their
trip.

The knapsack problem can be solved using a variety of techniques, including exact
algorithms, approximation algorithms, and heuristics. Exact algorithms, such as dynamic
programming, can be used to find the optimal solution to the problem, but they may be
computationally expensive for large and complex instances of the problem. Approximation
algorithms, such as the greedy algorithm, can find a solution that is close to optimal, but may
not necessarily be the best possible solution. Heuristics, such as local search and simulated
annealing, can be used to explore the solution space and find good solutions, but may not
guarantee optimality.

One common approach to solving the knapsack problem is to use a genetic algorithm
(GA), which is a type of optimization algorithm that is inspired by the principles of natural
evolution. A GA encodes the items as chromosomes in a population of candidate solutions, and
uses genetic operations, such as selection, crossover, and mutation, to generate new candidate
solutions and improve the quality of the solutions over time. GAs can be effective at solving
the knapsack problem, but they can be computationally expensive and may require careful
tuning of the parameters and settings to achieve good performance.

This problem can be solved by maximize the total number of survival points that can
be obtained by selecting a subset of items from a given set. The constraint in this problem is
the weight of the selected items, which must not exceed the capacity of the knapsack which is
25kg. To solve this problem, we must carefully consider the weight and survival points value
of each item and decide on which items to include in the knapsack to maximize the total
survival points while staying within the weight constraint. This problem can be formally
defined as the optimization of an objective function, represented by the total survival points
achieved, subject to a constraint on the weight of the selected items.

Objective Function

The objective function plays a crucial role in a genetic algorithm. It is the objective
function that the GA is trying to optimize, and it is used to evaluate the solutions generated by
the GA. In the case of the knapsack problem, the objective function would consider the weights
and values of the items in the knapsack, as well as the maximum weight capacity of the
knapsack. It would then use this information to calculate the "fitness" of a given solution, which
is a measure of how good that solution is in terms of maximizing the value of the items in the

Chromosome representation

The individual chromosomes in the population of a genetic algorithm used to solve the
knapsack problem in the context of a hiking survival scenario are represented by two
characteristics: weight and survival points. The weight corresponds to the physical weight of
the items that can be carried in the knapsack, while the survival points represent the value or
importance of these items for the hiker's survival. The goal of the algorithm is to find the
combination of items that maximizes the total survival points while staying within the weight
limit of the knapsack. In this case, the values of the items are presented as 0-1, meaning that
each item has a binary value of either 0 or 1, indicating whether the item is included or not
included in the knapsack.
Programming Language

R is a programming language and free software environment that is popular among


statisticians and data miners for statistical computing and data analysis. It is an open-source
implementation of the S programming language, developed in the 1970s at Bell Labs, and has
a large and active user community. R is known for its wide range of statistical and graphical
techniques, as well as the numerous packages that are available to extend its capabilities. These
features make R a powerful and widely used language for statistical computing and data
analysis, including optimization projects such as those involving genetic algorithms.

One of the main reasons why R is a good choice for GA optimization is its wide range
of statistical and graphical techniques that can be used to analyze and visualize the results of a
GA optimization. In addition, R has several packages specifically designed for GA
optimization, including algorithms and visualization tools. The large and active user
community of R also makes it easy to find help and support online. Lastly, as an open-source
language, R is freely available and can be modified and distributed by anyone, which means
there is a diverse community of developers working to improve the language. Overall, these
features make R well-suited for use in GA optimization projects.
Step in Genetic Algorithm and steps use for coding

No Steps Coding
1. Import GA library. The
ga() function is used to
perform the genetic
algorithm optimization.
It takes a number of
arguments such as the
type of encoding used
for the chromosomes
(binary in this case),
the fitness function to
use for evaluation, the
number of bits (i.e., the
number of items) in
each chromosome, the
maximum number of
iterations to perform,
the population size, and
a seed for the random
number generator. The
keepBest argument
specifies that the best
chromosome found
during the optimization
should be kept.
2. Cromosomes
representation:
Initialize the vector for
item, weight, survival
and max weight. Then
create the data frame
which is item, weight
and survival
3. Initialize a
chromosomes vector
which is a binary
vector with a length
equal to the number of
rows in the ‘data’ data
frame. Each element in
the vector corresponds
to a row in the data
frame, with a value of
1 indicating that the
corresponding row
should be included and
a value of 0 indicating
that it should be
excluded.
4. Create a ‘fitness’
function that is used to
evaluate the
combination of items
represented by a
particular chromosome.
The function calculates
the total survival points
and weight of the
combination and
returns 0 if the weight
exceeds the maximum
weight, otherwise it
returns the total
survival points.
5. Selection, crossover
mutation and
replacement:
Do the optimization
process with GA
library. We can
initialize our own
generation number for
the optimization
process. For
reproducibility, we
write the seed
argument and keep the
best solution. Finally
create a summary of
the result and plot the
graph
Result and Discussion

We use different generation to get the best solution. Here, were looking for the best
fitness achieve by observing the mean of the fitness. the mean fitness is the average fitness of
all the chromosomes in the population being evolved by the genetic algorithm. A higher mean
fitness indicates that the average fitness of the chromosomes in the population is higher, which
generally implies that the chromosomes in the population are more fit or have better solutions
to the problem being solved.

However, it is important to note that a higher mean fitness does not necessarily imply
that the best solution has been found. The mean fitness is only an average and does not
necessarily reflect the fitness of the best chromosome in the population. To determine whether
the best solution has been found, it is necessary to evaluate the fitness of the best chromosome
in the population and compare it to the desired fitness goal. If the fitness of the best
chromosome meets or exceeds the desired fitness goal, then the optimization process can be
stopped. If the fitness of the best chromosome is below the desired fitness goal, then the
optimization process can continue to try to find an even better solution.

No Parameters Results Representation


1. Mutation: 0.1 Best mean: 38.92
Crossover: 0.8
Generation: 10
2 Mutation: 0.1 Best mean: 42.6
Crossover: 0.8
Generation: 30

3 Mutation: 0.1 Best mean: 44.9


Crossover: 0.8
Generation: 50

4 Mutation: 0.1 Best mean: 52.72


Crossover: 0.8
Generation: 100
Conclusion

The number of generations, also referred to as iterations, can impact the optimization
process of a genetic algorithm. In a genetic algorithm, candidate solutions, known as
chromosomes, are generated and evaluated to find the best solution to a given problem. These
iterations continue until a satisfactory solution is found or the maximum number of iterations
is reached.

Increasing the number of iterations allows for a wider exploration of the search space,
potentially leading to the discovery of better solutions. However, this also prolongs the
optimization process. Conversely, decreasing the number of iterations can hasten the
optimization process, but may result in suboptimal solutions being found. It is advisable to
select an appropriate number of iterations based on the balance between the time required for
optimization and the quality of the obtained solutions. It may be useful to begin with a moderate
number of iterations and adjust as needed based on the performance of the algorithm.

You might also like