5
5
import warnings
6
6
warnings .filterwarnings ("ignore" )
7
7
8
-
9
8
df = pd .read_excel ('products.xlsx' )
10
9
obj = np .array (df ['Product' ])
11
10
space = np .array (df ['Space' ])
12
11
13
12
14
13
function_inputs = space # Function space.
15
- desired_output = 3 # Function output.
16
- sol_per_population = int (math .pow (2 ,11 ))
14
+ desired_output = 4.4 # volume capacity we want to reach.
15
+ sol_per_population = int (math .pow (2 ,11 )) # 2^11 number of sample in the initial population
16
+
17
17
18
18
def fitness_func (solution , solution_idx ):
19
- # The fitness function calulates the sum of products between each input and its corresponding space.
19
+ """This function calculates the total fitness value by summing the product of
20
+ each input and its corresponding space.
21
+ """
20
22
output = np .sum (solution * function_inputs )
21
- if (output > desired_output ):
23
+ if (output > desired_output ): # discard a sample if the sum is more than the space available
22
24
fitness = 0.0
23
25
else :
24
26
# The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0.
25
27
fitness = 1.0 / (np .abs (output - desired_output ) + 0.000001 )
26
28
return fitness
27
29
28
30
def print_selected_obj (ga_instance ,obj ,space ):
31
+
32
+ """This function generates all van loading combinations
33
+ considering capacity, weight and size. It returns a list of possible combinations.
34
+ """
29
35
solution ,_ ,_ = ga_instance .best_solution ()
30
36
solution = np .array (solution ).astype (float )
31
37
print ('\n ' )
@@ -34,25 +40,29 @@ def print_selected_obj(ga_instance,obj,space):
34
40
print (f'object selected { obj [np .where (solution == 1 )]} ' )
35
41
print ('\n ' )
36
42
43
+
44
+
37
45
def save_model (ga_instance ):
38
46
filename = 'genetic'
39
47
ga_instance .save (filename = filename )
40
48
49
+
41
50
def plot_fitnes (ga_instance ):
42
51
ga_instance .plot_fitness (title = "PyGAD with Adaptive Mutation" , linewidth = 5 )
43
52
44
53
45
54
def show_best_genes (ga_instance ):
55
+ """The function will identify the genes (inputs) with
56
+ the highest fitness value and display them as the best options.
57
+ """
46
58
solution , solution_fitness , solution_idx = ga_instance .best_solution ()
59
+
47
60
print ('\n ' )
48
61
print ("Parameters of the best solution : {solution}" .format (solution = solution ))
49
62
print ("Fitness value of the best solution = {solution_fitness}" .format (solution_fitness = solution_fitness ))
50
63
51
64
print ('\n ' )
52
65
53
- #Let's save the model.
54
-
55
-
56
66
57
67
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
58
68
ga_instance = pygad .GA (num_generations = 100 ,
@@ -78,6 +88,8 @@ def show_best_genes(ga_instance):
78
88
print_selected_obj (ga_instance ,obj ,space )
79
89
80
90
show_best_genes (ga_instance )
91
+ #Let's save the model.
92
+
81
93
save_model (ga_instance )
82
94
plot_fitnes (ga_instance )
83
95
0 commit comments