Skip to content

Commit 1c39de8

Browse files
committed
update
1 parent 9cdff41 commit 1c39de8

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

.~lock.products.xlsx#

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,bauricenafack,pop-os,27.01.2023 16:48,file:///home/bauricenafack/.config/libreoffice/4;

generic_algorithm.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,33 @@
55
import warnings
66
warnings.filterwarnings("ignore")
77

8-
98
df = pd.read_excel('products.xlsx')
109
obj= np.array(df['Product'])
1110
space = np.array(df['Space'])
1211

1312

1413
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+
1717

1818
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+
"""
2022
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
2224
fitness=0.0
2325
else:
2426
# The value 0.000001 is used to avoid the Inf value when the denominator numpy.abs(output - desired_output) is 0.0.
2527
fitness = 1.0 / (np.abs(output - desired_output) + 0.000001)
2628
return fitness
2729

2830
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+
"""
2935
solution,_,_ = ga_instance.best_solution()
3036
solution = np.array(solution).astype(float)
3137
print('\n')
@@ -34,25 +40,29 @@ def print_selected_obj(ga_instance,obj,space):
3440
print(f'object selected {obj[np.where(solution == 1)]}')
3541
print('\n')
3642

43+
44+
3745
def save_model(ga_instance):
3846
filename = 'genetic'
3947
ga_instance.save(filename=filename)
4048

49+
4150
def plot_fitnes(ga_instance):
4251
ga_instance.plot_fitness(title="PyGAD with Adaptive Mutation", linewidth=5)
4352

4453

4554
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+
"""
4658
solution, solution_fitness, solution_idx = ga_instance.best_solution()
59+
4760
print('\n')
4861
print("Parameters of the best solution : {solution}".format(solution=solution))
4962
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
5063

5164
print('\n')
5265

53-
#Let's save the model.
54-
55-
5666

5767
# Creating an instance of the GA class inside the ga module. Some parameters are initialized within the constructor.
5868
ga_instance = pygad.GA(num_generations=100,
@@ -78,6 +88,8 @@ def show_best_genes(ga_instance):
7888
print_selected_obj(ga_instance,obj,space)
7989

8090
show_best_genes(ga_instance)
91+
#Let's save the model.
92+
8193
save_model(ga_instance)
8294
plot_fitnes(ga_instance)
8395

genetic.pkl

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)