Professional Documents
Culture Documents
Submitted By:
1. Smit Patel (21UCS205)
2. Yash Chugh (21UCS234)
3. Arnit Gupta (21UCS029)
4. Shashvat Jain (21UCS189)
Page 2 of 15
Membership Functions
Triangular membership functions were employed for all fuzzy classes to represent the linguistic variables of dirt,
grease, and wash time. These membership functions allow for a smooth transition between different classes.
SG MG HG
SD VST ST MT
MD ST MT HT
HD MT HT VHT
2. Grease
3. Wash Time
Page 4 of 15
Implementation:
This imports the necessary libraries, skfuzzy for fuzzy logic operations and numpy for numerical operations.
Here we de nedthe universes of discourse for dirt (0 to 100), grease (0 to 50), and wash time (0 to 60) using
NumPy arrays.
Here we de ned the triangular membership functions for linguistic variables within their respective ranges. The
graph of which we have plotted before in our Report.
fi
fi
fi
fi
Page 5 of 15
Fuzzy rules based on combinations of dirt and grease levels, associating them with wash time.
Next we created a control system with the de ned rules. Initialised the simulation object using this control
system.
Next, we have speci ed input values for dirt and grease in lists, then iterate through the lists, inputting values
into the simulation, computing the wash time, and printing the results.
Visualising the fuzzy sets for dirt, grease, and wash time using the view method. And that is the output of
our code also for the following values of dirt and grease:-
fi
Page 7 of 15
Result:
The program successfully determines the wash time for any given combination of dirt and grease values
within the speci ed ranges.
Objective:
The task involves solving the Traveling Salesman Problem (TSP) using a Genetic Algorithm (GA). The TSP
consists of nding the most ef cient route that visits each city exactly once. The number of cities is 15, and the
weight matrix represents the distances between cities.The algorithm will run for 20 iterations with a population
size of 15 chromosomes.
A population of 15 chromosomes, each representing a unique sequence of cities, is randomly generated. The
chromosomes encode potential solutions to the TSP.
• Selection:
Roulette-wheel based selection is employed to choose parents for crossover. Roulette-wheel based selection
in genetic algorithms allocates selection probabilities to individuals proportional to their tness. After
normalising tness values, a virtual roulette wheel is created, and individuals are chosen based on randomly
generated numbers. This method ensures that tter individuals have higher chances of selection while
maintaining diversity. Selected individuals are then used as parents for crossover, contributing to the evolution
of the population toward optimal solutions in optimisation problems
• Crossover:
A one-point crossover is performed between randomly selected parents. The crossover point is chosen
randomly to introduce diversity in the offspring.
• Mutation:
To maintain diversity in the population, a mutation operation is applied with a low probability. Mutation involves
swapping two cities in a chromosome.
• Elitism:
The best-performing chromosomes from the current population are directly transferred to the next generation,
ensuring that the best solutions are preserved.
fi
fi
fi
fi
fi
Page 9 of 15
Implementation:
This section initialises various parameters and data structures used in the genetic algorithm for solving the
Traveling Salesman Problem (TSP).
• population: List to hold paths (routes) for the individuals in the population.
• population_size: Number of individuals in each generation.
• mutate_prob: Probability of mutation occurring during reproduction.
• n_generations: Number of generations to run the genetic algorithm.
• routes_length: List to store the lengths of routes in the population.
• tness: List to store the tness values of individuals in the population.
• best_path: Initially set to a high value; it will be updated as the algorithm progresses.
• distances: 2D list representing the distance matrix between cities. It’s an 15x15 matrix.
• cities: List representing the cities to be visited.
• calc_distance: Function to calculate the distance between two cities.
This section de nes functions for creating initial random routes, calculating the total length of a route, and
creating the initial population.
• create_route(): Generates a random route by shuf ing the cities.
• calc_route_length(): Calculates the total length of each route in the population and updates routes_length
and tness.
• create_population(): Initialises the population by generating random routes.
fi
fi
fl
Page 11 of 15
This section includes functions for parent selection and nding the ttest individual.
• roulette_wheel_selection(): Implements Fitness Proportionate Selection (Roulette Wheel Selection) to choose
a parent based on their tness.
• nd_ ttest(): Finds the index of the ttest individual in the current population.
fi
fi
fi
fi
fi
fi
Page 13 of 15
This section initialises the algorithm, prints initial population details, and contains the main loop for the
speci ed number of generations.
This section includes crossover, mutation, and selection operations within the main loop.
• one_point_crossover(ind1, ind2) is used for crossover, and swap_mutation(ind) is used for mutation.
• The combined population is created by combining the current population and the offspring generated
through crossover.
• The best individuals are selected for the next generation using elitism.
• The overall best path and its length are updated if a better path is found during the current generation.
• This section prints the results of each generation, including the best route and its length.
• Finally, the code prints the nal result, which is the best path found by the genetic algorithm.
fi
Page 15 of 15
• Output
After running it successfully for 20 iterations in this case we got the output 85 but different output will be
shown when we run it again because of the randomisation involved in this process.
Result:
After 20 iterations, the algorithm converges, providing a sequence of cities representing the optimal route for
the traveling salesman. The distance covered by this route is the minimum among the population. We have
shown its correct implementation above.