This repository contains Python code that implements a genetic algorithm to optimize the placement of communication towers in a city. The goal of this optimization is to maximize user satisfaction while minimizing the total cost of tower construction and maintenance.
To run the code, you will need the following libraries:
- Clone this repository to your local machine.
- Make sure you have the required libraries installed using:
pip install numpy matplotlib- Run the code in the
main.ipynbnotebook.
main.ipynb
This script contains the main code for the genetic algorithm. It defines the following functions:
-
crossover(parent1, parent2): Performs crossover between two parent cities to create two child cities. -
mutation(child): Introduces random mutations to a child city. -
selection(population, limit=50): Selects the best cities from the population based on their fitness scores. -
genetic_algorithm(max_generation=200, population=50, mutation_rate=0.1, crossover_rate=0.9): Implements the genetic algorithm to optimize tower placement. -
output(generation, j): Writes the results of each generation to a text file.
The script runs the genetic algorithm with different sets of parameters and saves the results to text files. It also generates fitness plots for each set of parameters.
utils.py
This script contains utility classes and functions required for tower placement optimization. It defines the following classes:
-
City: Represents a city with towers and blocks. It calculates fitness scores, user satisfaction, and bandwidth for each block and tower. -
Block: Represents a block in the city with its position, connected tower, population, and bandwidth. -
Tower: Represents a communication tower with its position and bandwidth capacity.
The genetic algorithm requires two input files:
-
blocks_population.txt: Contains the population data for each block in the city. -
problem_config.txt: Contains configuration data for the problem, including tower construction cost, maintenance cost, user satisfaction levels, and corresponding satisfaction scores.
Make sure to provide the appropriate data in these files before running the algorithm.
The output of the genetic algorithm is saved in text files named generationX.txt, where X represents the generation number. Each output file contains the following information for each generation:
- The number of towers in the city.
- Tower positions and their bandwidth capacity.
- The assignment of blocks to towers and their respective positions. Additionally, the script generates fitness plots showing the fitness score for each generation with different sets of parameters.
The genetic algorithm implemented in this code is meant for educational and illustrative purposes. It may not be optimized for large-scale real-world applications. The input data and parameters used for optimization should be adjusted according to specific use cases and problem requirements.
Read the complete documentation HERE. Also you can read THIS code review for more details.
Please note that the provided code may have room for improvements and optimization. Feel free to modify and adapt it to suit your needs.
Happy optimizing!