-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_algorithm.h
More file actions
146 lines (125 loc) · 5.18 KB
/
Copy pathgenetic_algorithm.h
File metadata and controls
146 lines (125 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef __GENETIC_ALGORITHM_H__
#define __GENETIC_ALGORITHM_H__
#define MIN_CROSS_SELECTION 0.7
#define MAX_TIME_PATH 180.0
#define NB_DELIVERERS_WEIGHT 1000
#define TOTAL_TIME_WEIGHT 1
#define TOTAL_DISTANCE_WEIGHT 10
#include "initial_population.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "repair_individual.h"
/**
* @brief make and return a copy of a individual
*/
Individual* clone_individual(Individual* source);
/**
* @brief Structure holding two parent individuals for crossover operations
*/
typedef struct {
Individual* parent1; /* First parent individual */
Individual* parent2; /* Second parent individual */
} Parents;
/**
* @brief Evaluate fitness for entire population using roulette wheel selection
* Calculates fitness values for all individuals and creates cumulative fitness array
* @param population Pointer to the population to evaluate
* @param total_fitness Pointer to store the sum of all fitness values
* @return Array of cumulative fitness values, or NULL on failure
*/
float* evaluate_fitness(Population* population, float* total_fitness);
/**
* @brief Initialize a new Parents structure
* Allocates memory for a Parents structure and initializes pointers to NULL
* @return Pointer to newly allocated Parents structure, or NULL on failure
*/
Parents* initialize_parents();
/**
* @brief Free memory allocated for a Parents structure
* Safely deallocates memory for a Parents structure (note: does not free the individuals themselves)
* @param parents Pointer to the Parents structure to free
*/
void free_parents(Parents* parents);
/**
* @brief Select two parent individuals using roulette wheel selection
* Uses fitness-proportionate selection to choose two different parents for reproduction
* @param population Population to select from
* @param cumulative_fitness Array of cumulative fitness values
* @param total_fitness Total fitness of the population
* @return Pointer to Parents structure containing two selected individuals, or NULL on failure
*/
Parents* selection(Population* population, float* cumulative_fitness, float total_fitness);
/**
* @brief Perform crossover operation between two parents to create offspring
* Combines genetic material from two parent individuals to create a new child individual
* @param graph 2D array representing the network with distances and times
* @param parents Pointer to Parents structure containing the two parent individuals
* @return Pointer to newly created child Individual, or NULL on failure
*/
Individual* crossover(Node** graph, Parents* parents);
/**
* @brief Create a deep copy of a path structure
* Allocates new memory and copies all data from the original path
* @param original Pointer to the Path to copy
* @return Deep copy of the original path
*/
Path copy_path_deep(Path* original);
/**
* @brief Create a deep copy of an individual
* Allocates new memory and copies all data including all paths from the original individual
* @param individual Pointer to the Individual to copy
* @return Pointer to newly created copy, or NULL on failure
*/
Individual* copy_individual(Individual* individual);
/**
* @brief Apply mutation to an individual
* Randomly swaps cities within deliverer paths based on mutation rate
* @param individual Individual to mutate
* @param graph 2D array representing the network (used to recalculate metrics)
*/
void mutate(Individual* individual, Node** graph);
/**
* @brief Replace a random individual in the population with a new child
* Updates the population by replacing a randomly selected individual with the new child
* @param population Population to update
* @param child New individual to add to the population
*/
void update_population(Population* population, Individual* child);
/**
* @brief Find the individual with the highest fitness in the population
* Searches through the population to find the best performing individual
* @param population Population to search
* @return Pointer to the individual with highest fitness, or NULL if population is empty
*/
Individual* best_fitness(Population* population);
/**
* @brief Update time and distance metrics for a single path
* Recalculates total_time and total_distance based on the path and graph data
* @param graph 2D array representing the network with distances and times
* @param path Path to update metrics for
*/
void update_path_metrics(Node** graph, Path* path);
/**
* @brief Check if a path respects the time constraint
* Verifies that the path's total time does not exceed the maximum allowed time
* @param path Path to check
* @return 1 if path time <= MAX_TIME_PATH, 0 otherwise
*/
int check_path(Path* path);
/**
* @brief Update all metrics for an individual
* Recalculates path metrics and total metrics for the entire individual
* @param graph 2D array representing the network
* @param individual Individual to update
*/
void update_indiv_metrics(Node** graph, Individual* individual);
/**
* @brief Calculate and update fitness score for an individual
* Computes fitness based on number of deliverers, total time, and total distance
* @param individual Individual to update fitness for
*/
void update_fitness(Individual* individual);
#endif