diff --git a/ABC/PrefrencesABC.py b/ABC/PrefrencesABC.py index 1641f22..db7a9f6 100644 --- a/ABC/PrefrencesABC.py +++ b/ABC/PrefrencesABC.py @@ -54,7 +54,14 @@ def calculate_diversity(groups: List[List[Student]]) -> float: preference_score = 0 for group in groups: - scores = [student.get_score() for student in group] + scores = [] + if group[0].experiment: + for i, student1 in enumerate(group): + for student2 in group[i+1:]: + scores.append(student1.get_score(student2)) + else: + scores = [student.get_score() for student in group] + if len(scores) > 1: # סטיית תקן מוגדרת רק עבור יותר מנתון אחד diversity = statistics.stdev(scores) else: @@ -173,7 +180,6 @@ def abc_algorithm_with_prefrences(students: List[Student], num_groups: int, num_ if best_fitness > GLOBAL_MAX_VAL: GLOBAL_MAX_VAL = best_fitness GLOBAL_MAX = solutions[scores.index(GLOBAL_MAX_VAL)] - print(f"Iteration {iteration + 1}, Best Fitness: {GLOBAL_MAX_VAL}") # בסוף, מחזירים את הפתרון הטוב ביותר return GLOBAL_MAX diff --git a/ABC/StandardABC.py b/ABC/StandardABC.py index 766e34b..a47f885 100644 --- a/ABC/StandardABC.py +++ b/ABC/StandardABC.py @@ -131,9 +131,6 @@ def abc_algorithm(students: List[Student], num_groups: int, num_iterations: int solutions[i] = new_sol scores[i] = calculate_diversity(new_sol) stagnation[i] = 0 - # הדפסת מידע על הדור - best_fitness = max(scores) - print(f"Iteration {iteration + 1}, Best Fitness: {best_fitness}") # בסוף, מחזירים את הפתרון הטוב ביותר best_index = scores.index(max(scores)) diff --git a/Genetic/PreferencesGenetic.py b/Genetic/PreferencesGenetic.py index afc91ec..7b311a5 100644 --- a/Genetic/PreferencesGenetic.py +++ b/Genetic/PreferencesGenetic.py @@ -53,7 +53,14 @@ def calculate_diversity(groups: List[List[Student]]) -> float: preference_score = 0 for group in groups: - scores = [student.get_score() for student in group] + scores = [] + if group[0].experiment: + for i, student1 in enumerate(group): + for student2 in group[i+1:]: + scores.append(student1.get_score(student2)) + else: + scores = [student.get_score() for student in group] + if len(scores) > 1: # סטיית תקן מוגדרת רק עבור יותר מנתון אחד diversity = statistics.stdev(scores) else: @@ -188,10 +195,6 @@ def genetic_algorithm_with_preferences(students: List[Student], num_groups: int, # עדכון האוכלוסייה update_population(population, fitness_scores, mutated_child) - # הדפסת מידע על הדור - #best_fitness = max(fitness_scores) - #print(f"Generation {generation + 1}, Best Fitness: {best_fitness}") - # מחזירים את הפתרון הטוב ביותר best_index = fitness_scores.index(max(fitness_scores)) return population[best_index] diff --git a/Genetic/StandardGenetic.py b/Genetic/StandardGenetic.py index 13b4d5b..149b8d7 100644 --- a/Genetic/StandardGenetic.py +++ b/Genetic/StandardGenetic.py @@ -151,10 +151,6 @@ def genetic_algorithm(students: List[Student], num_groups: int, population_size: # עדכון האוכלוסייה update_population(population, fitness_scores, mutated_child) - # הדפסת מידע על הדור - best_fitness = max(fitness_scores) - print(f"Generation {generation + 1}, Best Fitness: {best_fitness}") - # מחזירים את הפתרון הטוב ביותר best_index = fitness_scores.index(max(fitness_scores)) return population[best_index] diff --git a/experiments/ABC/Iterations_Fitness.csv b/experiments/ABC/Iterations_Fitness.csv new file mode 100644 index 0000000..e09ccd5 --- /dev/null +++ b/experiments/ABC/Iterations_Fitness.csv @@ -0,0 +1,21 @@ +Iteration,AVG Score +1,88.7211497181652 +2,88.7211497181652 +3,89.1052713126468 +4,89.22915077725476 +5,89.25699984324228 +6,89.53983026739552 +7,89.75942600900592 +8,90.19848966650157 +9,90.19848966650157 +10,90.19848966650157 +11,90.19848966650157 +12,90.28018097627236 +13,90.53506593084174 +14,90.63147377849803 +15,90.63147377849803 +16,90.63147377849803 +17,90.63147377849803 +18,90.790154029262 +19,90.95219584823126 +20,90.95443689370146 diff --git a/experiments/ABC/Iterations_Time.csv b/experiments/ABC/Iterations_Time.csv new file mode 100644 index 0000000..e8b39b7 --- /dev/null +++ b/experiments/ABC/Iterations_Time.csv @@ -0,0 +1,21 @@ +Iteration,AVG Time +1,0.006540608406066894 +2,0.010796785354614258 +3,0.01384124755859375 +4,0.017898941040039064 +5,0.021931099891662597 +6,0.026218819618225097 +7,0.031563854217529295 +8,0.03766334056854248 +9,0.037829041481018066 +10,0.04230003356933594 +11,0.04445006847381592 +12,0.050631237030029294 +13,0.05401666164398193 +14,0.05841057300567627 +15,0.06447982788085938 +16,0.06548664569854737 +17,0.06842453479766845 +18,0.0749204158782959 +19,0.07750654220581055 +20,0.07419106960296631 diff --git a/experiments/data/RanInt_n060_ss_01.txt b/experiments/data/RanInt_n060_ss_01.txt new file mode 100644 index 0000000..70181a1 --- /dev/null +++ b/experiments/data/RanInt_n060_ss_01.txt @@ -0,0 +1,1771 @@ +60 6 ss 10 10 10 10 10 10 10 10 10 10 10 10 +0 1 96 +0 2 100 +0 3 49 +0 4 82 +0 5 8 +0 6 52 +0 7 78 +0 8 69 +0 9 9 +0 10 62 +0 11 60 +0 12 43 +0 13 48 +0 14 58 +0 15 24 +0 16 31 +0 17 68 +0 18 13 +0 19 57 +0 20 3 +0 21 80 +0 22 93 +0 23 59 +0 24 24 +0 25 85 +0 26 21 +0 27 39 +0 28 83 +0 29 63 +0 30 78 +0 31 7 +0 32 14 +0 33 75 +0 34 20 +0 35 54 +0 36 66 +0 37 30 +0 38 3 +0 39 83 +0 40 21 +0 41 24 +0 42 91 +0 43 100 +0 44 100 +0 45 100 +0 46 1 +0 47 98 +0 48 96 +0 49 58 +0 50 25 +0 51 12 +0 52 57 +0 53 14 +0 54 74 +0 55 82 +0 56 75 +0 57 36 +0 58 72 +0 59 19 +1 2 69 +1 3 40 +1 4 94 +1 5 28 +1 6 71 +1 7 62 +1 8 22 +1 9 44 +1 10 35 +1 11 83 +1 12 34 +1 13 89 +1 14 50 +1 15 13 +1 16 48 +1 17 12 +1 18 56 +1 19 51 +1 20 3 +1 21 43 +1 22 15 +1 23 21 +1 24 76 +1 25 84 +1 26 56 +1 27 50 +1 28 2 +1 29 55 +1 30 76 +1 31 99 +1 32 100 +1 33 33 +1 34 13 +1 35 95 +1 36 80 +1 37 18 +1 38 2 +1 39 16 +1 40 20 +1 41 56 +1 42 20 +1 43 75 +1 44 3 +1 45 85 +1 46 44 +1 47 94 +1 48 43 +1 49 61 +1 50 2 +1 51 46 +1 52 62 +1 53 99 +1 54 81 +1 55 58 +1 56 46 +1 57 55 +1 58 7 +1 59 48 +2 3 30 +2 4 39 +2 5 80 +2 6 56 +2 7 24 +2 8 1 +2 9 95 +2 10 41 +2 11 4 +2 12 84 +2 13 69 +2 14 66 +2 15 36 +2 16 38 +2 17 96 +2 18 11 +2 19 81 +2 20 19 +2 21 74 +2 22 54 +2 23 84 +2 24 3 +2 25 57 +2 26 8 +2 27 94 +2 28 18 +2 29 94 +2 30 59 +2 31 72 +2 32 60 +2 33 78 +2 34 18 +2 35 29 +2 36 70 +2 37 95 +2 38 67 +2 39 64 +2 40 13 +2 41 18 +2 42 91 +2 43 10 +2 44 31 +2 45 47 +2 46 40 +2 47 74 +2 48 37 +2 49 60 +2 50 67 +2 51 59 +2 52 90 +2 53 97 +2 54 69 +2 55 93 +2 56 67 +2 57 72 +2 58 26 +2 59 53 +3 4 4 +3 5 65 +3 6 82 +3 7 24 +3 8 28 +3 9 68 +3 10 57 +3 11 98 +3 12 13 +3 13 75 +3 14 5 +3 15 43 +3 16 9 +3 17 71 +3 18 47 +3 19 39 +3 20 8 +3 21 25 +3 22 49 +3 23 38 +3 24 67 +3 25 31 +3 26 0 +3 27 41 +3 28 39 +3 29 6 +3 30 56 +3 31 74 +3 32 63 +3 33 2 +3 34 42 +3 35 31 +3 36 81 +3 37 92 +3 38 42 +3 39 69 +3 40 77 +3 41 52 +3 42 77 +3 43 99 +3 44 25 +3 45 89 +3 46 86 +3 47 66 +3 48 48 +3 49 36 +3 50 1 +3 51 24 +3 52 39 +3 53 65 +3 54 12 +3 55 96 +3 56 49 +3 57 19 +3 58 70 +3 59 64 +4 5 83 +4 6 49 +4 7 68 +4 8 96 +4 9 69 +4 10 64 +4 11 98 +4 12 76 +4 13 79 +4 14 36 +4 15 59 +4 16 15 +4 17 23 +4 18 50 +4 19 91 +4 20 91 +4 21 48 +4 22 22 +4 23 65 +4 24 18 +4 25 81 +4 26 52 +4 27 42 +4 28 42 +4 29 67 +4 30 82 +4 31 61 +4 32 42 +4 33 74 +4 34 73 +4 35 52 +4 36 33 +4 37 75 +4 38 100 +4 39 4 +4 40 95 +4 41 26 +4 42 13 +4 43 53 +4 44 58 +4 45 44 +4 46 8 +4 47 54 +4 48 7 +4 49 60 +4 50 44 +4 51 66 +4 52 1 +4 53 95 +4 54 81 +4 55 71 +4 56 34 +4 57 17 +4 58 85 +4 59 61 +5 6 24 +5 7 68 +5 8 4 +5 9 83 +5 10 8 +5 11 89 +5 12 46 +5 13 85 +5 14 93 +5 15 30 +5 16 94 +5 17 17 +5 18 45 +5 19 34 +5 20 20 +5 21 29 +5 22 34 +5 23 13 +5 24 23 +5 25 40 +5 26 4 +5 27 96 +5 28 91 +5 29 28 +5 30 52 +5 31 26 +5 32 54 +5 33 6 +5 34 89 +5 35 97 +5 36 86 +5 37 41 +5 38 91 +5 39 71 +5 40 15 +5 41 59 +5 42 15 +5 43 25 +5 44 31 +5 45 66 +5 46 7 +5 47 26 +5 48 19 +5 49 1 +5 50 100 +5 51 27 +5 52 64 +5 53 48 +5 54 48 +5 55 77 +5 56 30 +5 57 58 +5 58 40 +5 59 4 +6 7 68 +6 8 96 +6 9 57 +6 10 52 +6 11 92 +6 12 0 +6 13 91 +6 14 14 +6 15 98 +6 16 39 +6 17 56 +6 18 46 +6 19 38 +6 20 28 +6 21 63 +6 22 34 +6 23 82 +6 24 92 +6 25 37 +6 26 63 +6 27 37 +6 28 16 +6 29 95 +6 30 83 +6 31 54 +6 32 43 +6 33 61 +6 34 80 +6 35 8 +6 36 4 +6 37 100 +6 38 74 +6 39 75 +6 40 44 +6 41 81 +6 42 32 +6 43 35 +6 44 16 +6 45 29 +6 46 58 +6 47 7 +6 48 18 +6 49 47 +6 50 67 +6 51 35 +6 52 43 +6 53 0 +6 54 32 +6 55 71 +6 56 94 +6 57 4 +6 58 98 +6 59 25 +7 8 64 +7 9 3 +7 10 45 +7 11 69 +7 12 55 +7 13 28 +7 14 50 +7 15 74 +7 16 46 +7 17 84 +7 18 22 +7 19 39 +7 20 54 +7 21 65 +7 22 84 +7 23 65 +7 24 3 +7 25 26 +7 26 31 +7 27 54 +7 28 44 +7 29 95 +7 30 36 +7 31 20 +7 32 98 +7 33 67 +7 34 68 +7 35 100 +7 36 83 +7 37 47 +7 38 32 +7 39 80 +7 40 53 +7 41 52 +7 42 14 +7 43 29 +7 44 40 +7 45 32 +7 46 89 +7 47 21 +7 48 53 +7 49 16 +7 50 21 +7 51 26 +7 52 67 +7 53 11 +7 54 76 +7 55 28 +7 56 1 +7 57 78 +7 58 54 +7 59 5 +8 9 57 +8 10 75 +8 11 69 +8 12 92 +8 13 1 +8 14 5 +8 15 49 +8 16 78 +8 17 84 +8 18 30 +8 19 92 +8 20 14 +8 21 60 +8 22 77 +8 23 83 +8 24 19 +8 25 96 +8 26 93 +8 27 56 +8 28 78 +8 29 54 +8 30 39 +8 31 87 +8 32 58 +8 33 46 +8 34 45 +8 35 86 +8 36 93 +8 37 56 +8 38 95 +8 39 44 +8 40 36 +8 41 87 +8 42 53 +8 43 18 +8 44 99 +8 45 74 +8 46 95 +8 47 65 +8 48 28 +8 49 55 +8 50 12 +8 51 43 +8 52 81 +8 53 46 +8 54 82 +8 55 100 +8 56 33 +8 57 22 +8 58 42 +8 59 89 +9 10 79 +9 11 4 +9 12 92 +9 13 33 +9 14 84 +9 15 58 +9 16 87 +9 17 5 +9 18 7 +9 19 23 +9 20 36 +9 21 53 +9 22 34 +9 23 28 +9 24 45 +9 25 34 +9 26 97 +9 27 29 +9 28 9 +9 29 72 +9 30 85 +9 31 90 +9 32 32 +9 33 35 +9 34 85 +9 35 30 +9 36 41 +9 37 27 +9 38 84 +9 39 19 +9 40 38 +9 41 79 +9 42 2 +9 43 22 +9 44 30 +9 45 73 +9 46 47 +9 47 18 +9 48 81 +9 49 89 +9 50 59 +9 51 19 +9 52 7 +9 53 46 +9 54 41 +9 55 4 +9 56 84 +9 57 69 +9 58 84 +9 59 69 +10 11 30 +10 12 66 +10 13 15 +10 14 65 +10 15 60 +10 16 90 +10 17 58 +10 18 20 +10 19 26 +10 20 86 +10 21 64 +10 22 71 +10 23 72 +10 24 49 +10 25 93 +10 26 65 +10 27 17 +10 28 11 +10 29 30 +10 30 88 +10 31 94 +10 32 41 +10 33 3 +10 34 90 +10 35 84 +10 36 61 +10 37 25 +10 38 55 +10 39 100 +10 40 11 +10 41 46 +10 42 59 +10 43 63 +10 44 60 +10 45 51 +10 46 93 +10 47 25 +10 48 12 +10 49 60 +10 50 3 +10 51 17 +10 52 50 +10 53 32 +10 54 66 +10 55 58 +10 56 58 +10 57 2 +10 58 29 +10 59 69 +11 12 81 +11 13 53 +11 14 79 +11 15 41 +11 16 13 +11 17 51 +11 18 51 +11 19 74 +11 20 20 +11 21 85 +11 22 75 +11 23 40 +11 24 34 +11 25 50 +11 26 72 +11 27 59 +11 28 78 +11 29 30 +11 30 30 +11 31 50 +11 32 11 +11 33 38 +11 34 26 +11 35 79 +11 36 42 +11 37 23 +11 38 17 +11 39 78 +11 40 69 +11 41 22 +11 42 42 +11 43 74 +11 44 9 +11 45 27 +11 46 33 +11 47 83 +11 48 10 +11 49 6 +11 50 36 +11 51 93 +11 52 58 +11 53 21 +11 54 46 +11 55 74 +11 56 66 +11 57 53 +11 58 59 +11 59 19 +12 13 17 +12 14 10 +12 15 66 +12 16 85 +12 17 76 +12 18 8 +12 19 68 +12 20 37 +12 21 47 +12 22 17 +12 23 74 +12 24 34 +12 25 79 +12 26 72 +12 27 36 +12 28 47 +12 29 92 +12 30 99 +12 31 46 +12 32 72 +12 33 5 +12 34 52 +12 35 28 +12 36 52 +12 37 21 +12 38 36 +12 39 55 +12 40 94 +12 41 88 +12 42 52 +12 43 51 +12 44 29 +12 45 1 +12 46 75 +12 47 3 +12 48 22 +12 49 71 +12 50 5 +12 51 32 +12 52 64 +12 53 16 +12 54 95 +12 55 35 +12 56 41 +12 57 48 +12 58 5 +12 59 40 +13 14 45 +13 15 64 +13 16 32 +13 17 1 +13 18 86 +13 19 64 +13 20 91 +13 21 5 +13 22 94 +13 23 2 +13 24 70 +13 25 73 +13 26 78 +13 27 79 +13 28 94 +13 29 52 +13 30 13 +13 31 63 +13 32 99 +13 33 13 +13 34 19 +13 35 72 +13 36 13 +13 37 38 +13 38 36 +13 39 23 +13 40 84 +13 41 93 +13 42 44 +13 43 86 +13 44 9 +13 45 100 +13 46 53 +13 47 11 +13 48 67 +13 49 45 +13 50 68 +13 51 75 +13 52 31 +13 53 74 +13 54 3 +13 55 63 +13 56 0 +13 57 44 +13 58 46 +13 59 20 +14 15 8 +14 16 62 +14 17 92 +14 18 15 +14 19 76 +14 20 92 +14 21 43 +14 22 67 +14 23 36 +14 24 63 +14 25 92 +14 26 57 +14 27 5 +14 28 68 +14 29 46 +14 30 41 +14 31 22 +14 32 43 +14 33 0 +14 34 17 +14 35 5 +14 36 9 +14 37 22 +14 38 59 +14 39 99 +14 40 42 +14 41 70 +14 42 29 +14 43 8 +14 44 52 +14 45 72 +14 46 85 +14 47 87 +14 48 29 +14 49 43 +14 50 61 +14 51 13 +14 52 77 +14 53 83 +14 54 59 +14 55 43 +14 56 77 +14 57 91 +14 58 85 +14 59 31 +15 16 51 +15 17 69 +15 18 17 +15 19 6 +15 20 73 +15 21 80 +15 22 40 +15 23 6 +15 24 5 +15 25 48 +15 26 18 +15 27 58 +15 28 29 +15 29 55 +15 30 52 +15 31 38 +15 32 77 +15 33 62 +15 34 16 +15 35 71 +15 36 97 +15 37 14 +15 38 84 +15 39 75 +15 40 13 +15 41 70 +15 42 63 +15 43 62 +15 44 71 +15 45 51 +15 46 60 +15 47 34 +15 48 100 +15 49 45 +15 50 50 +15 51 13 +15 52 71 +15 53 28 +15 54 34 +15 55 82 +15 56 97 +15 57 99 +15 58 28 +15 59 88 +16 17 24 +16 18 12 +16 19 61 +16 20 1 +16 21 44 +16 22 57 +16 23 28 +16 24 23 +16 25 81 +16 26 0 +16 27 10 +16 28 67 +16 29 36 +16 30 34 +16 31 50 +16 32 40 +16 33 7 +16 34 55 +16 35 33 +16 36 39 +16 37 7 +16 38 19 +16 39 12 +16 40 97 +16 41 31 +16 42 52 +16 43 34 +16 44 27 +16 45 81 +16 46 12 +16 47 37 +16 48 8 +16 49 100 +16 50 47 +16 51 22 +16 52 66 +16 53 64 +16 54 96 +16 55 59 +16 56 52 +16 57 71 +16 58 37 +16 59 59 +17 18 5 +17 19 72 +17 20 9 +17 21 41 +17 22 89 +17 23 30 +17 24 19 +17 25 72 +17 26 85 +17 27 63 +17 28 12 +17 29 26 +17 30 89 +17 31 43 +17 32 35 +17 33 9 +17 34 62 +17 35 83 +17 36 65 +17 37 10 +17 38 62 +17 39 6 +17 40 28 +17 41 91 +17 42 51 +17 43 82 +17 44 38 +17 45 80 +17 46 98 +17 47 32 +17 48 65 +17 49 91 +17 50 57 +17 51 59 +17 52 60 +17 53 17 +17 54 36 +17 55 60 +17 56 81 +17 57 85 +17 58 84 +17 59 42 +18 19 23 +18 20 22 +18 21 94 +18 22 24 +18 23 94 +18 24 23 +18 25 61 +18 26 57 +18 27 97 +18 28 13 +18 29 31 +18 30 12 +18 31 37 +18 32 25 +18 33 88 +18 34 3 +18 35 80 +18 36 92 +18 37 52 +18 38 63 +18 39 100 +18 40 7 +18 41 64 +18 42 52 +18 43 4 +18 44 54 +18 45 45 +18 46 90 +18 47 70 +18 48 52 +18 49 38 +18 50 1 +18 51 32 +18 52 9 +18 53 34 +18 54 47 +18 55 94 +18 56 62 +18 57 68 +18 58 98 +18 59 96 +19 20 6 +19 21 31 +19 22 100 +19 23 8 +19 24 11 +19 25 12 +19 26 89 +19 27 0 +19 28 44 +19 29 82 +19 30 91 +19 31 89 +19 32 98 +19 33 38 +19 34 80 +19 35 86 +19 36 55 +19 37 27 +19 38 95 +19 39 77 +19 40 72 +19 41 44 +19 42 60 +19 43 7 +19 44 99 +19 45 26 +19 46 82 +19 47 32 +19 48 94 +19 49 4 +19 50 60 +19 51 84 +19 52 43 +19 53 97 +19 54 2 +19 55 0 +19 56 73 +19 57 52 +19 58 46 +19 59 31 +20 21 71 +20 22 67 +20 23 14 +20 24 21 +20 25 91 +20 26 7 +20 27 16 +20 28 41 +20 29 64 +20 30 45 +20 31 79 +20 32 98 +20 33 28 +20 34 20 +20 35 89 +20 36 75 +20 37 22 +20 38 37 +20 39 59 +20 40 91 +20 41 0 +20 42 52 +20 43 25 +20 44 31 +20 45 53 +20 46 33 +20 47 36 +20 48 59 +20 49 50 +20 50 87 +20 51 17 +20 52 19 +20 53 22 +20 54 80 +20 55 93 +20 56 6 +20 57 13 +20 58 39 +20 59 44 +21 22 17 +21 23 94 +21 24 42 +21 25 64 +21 26 72 +21 27 84 +21 28 100 +21 29 90 +21 30 31 +21 31 95 +21 32 96 +21 33 20 +21 34 15 +21 35 85 +21 36 98 +21 37 43 +21 38 5 +21 39 58 +21 40 84 +21 41 65 +21 42 17 +21 43 50 +21 44 64 +21 45 78 +21 46 88 +21 47 43 +21 48 94 +21 49 83 +21 50 70 +21 51 10 +21 52 5 +21 53 58 +21 54 19 +21 55 40 +21 56 96 +21 57 84 +21 58 8 +21 59 4 +22 23 65 +22 24 74 +22 25 87 +22 26 51 +22 27 70 +22 28 23 +22 29 33 +22 30 31 +22 31 63 +22 32 81 +22 33 44 +22 34 69 +22 35 8 +22 36 30 +22 37 15 +22 38 82 +22 39 92 +22 40 61 +22 41 19 +22 42 44 +22 43 51 +22 44 76 +22 45 71 +22 46 88 +22 47 91 +22 48 68 +22 49 2 +22 50 80 +22 51 31 +22 52 91 +22 53 29 +22 54 55 +22 55 58 +22 56 9 +22 57 49 +22 58 31 +22 59 85 +23 24 32 +23 25 72 +23 26 87 +23 27 83 +23 28 15 +23 29 71 +23 30 51 +23 31 18 +23 32 52 +23 33 92 +23 34 40 +23 35 21 +23 36 74 +23 37 70 +23 38 54 +23 39 97 +23 40 99 +23 41 61 +23 42 12 +23 43 53 +23 44 3 +23 45 5 +23 46 50 +23 47 3 +23 48 67 +23 49 0 +23 50 72 +23 51 64 +23 52 87 +23 53 96 +23 54 89 +23 55 73 +23 56 93 +23 57 72 +23 58 34 +23 59 76 +24 25 6 +24 26 95 +24 27 44 +24 28 99 +24 29 90 +24 30 24 +24 31 73 +24 32 66 +24 33 69 +24 34 2 +24 35 75 +24 36 42 +24 37 4 +24 38 10 +24 39 22 +24 40 55 +24 41 90 +24 42 17 +24 43 81 +24 44 84 +24 45 77 +24 46 89 +24 47 45 +24 48 32 +24 49 30 +24 50 68 +24 51 53 +24 52 37 +24 53 2 +24 54 38 +24 55 64 +24 56 31 +24 57 77 +24 58 53 +24 59 42 +25 26 68 +25 27 74 +25 28 87 +25 29 53 +25 30 50 +25 31 1 +25 32 96 +25 33 70 +25 34 25 +25 35 64 +25 36 15 +25 37 40 +25 38 9 +25 39 51 +25 40 90 +25 41 73 +25 42 31 +25 43 60 +25 44 66 +25 45 99 +25 46 80 +25 47 96 +25 48 14 +25 49 74 +25 50 17 +25 51 58 +25 52 56 +25 53 98 +25 54 44 +25 55 98 +25 56 11 +25 57 97 +25 58 9 +25 59 31 +26 27 77 +26 28 84 +26 29 26 +26 30 41 +26 31 26 +26 32 29 +26 33 67 +26 34 54 +26 35 14 +26 36 7 +26 37 68 +26 38 23 +26 39 17 +26 40 73 +26 41 33 +26 42 68 +26 43 69 +26 44 86 +26 45 20 +26 46 71 +26 47 88 +26 48 7 +26 49 56 +26 50 24 +26 51 73 +26 52 94 +26 53 21 +26 54 60 +26 55 100 +26 56 18 +26 57 66 +26 58 17 +26 59 27 +27 28 0 +27 29 88 +27 30 83 +27 31 29 +27 32 35 +27 33 61 +27 34 60 +27 35 3 +27 36 88 +27 37 59 +27 38 96 +27 39 59 +27 40 8 +27 41 60 +27 42 35 +27 43 53 +27 44 86 +27 45 85 +27 46 1 +27 47 58 +27 48 82 +27 49 6 +27 50 34 +27 51 13 +27 52 72 +27 53 43 +27 54 13 +27 55 36 +27 56 22 +27 57 65 +27 58 8 +27 59 0 +28 29 99 +28 30 86 +28 31 31 +28 32 76 +28 33 1 +28 34 86 +28 35 96 +28 36 76 +28 37 87 +28 38 61 +28 39 23 +28 40 3 +28 41 68 +28 42 93 +28 43 4 +28 44 22 +28 45 10 +28 46 66 +28 47 22 +28 48 77 +28 49 82 +28 50 19 +28 51 94 +28 52 98 +28 53 1 +28 54 14 +28 55 90 +28 56 34 +28 57 23 +28 58 57 +28 59 27 +29 30 53 +29 31 40 +29 32 34 +29 33 22 +29 34 44 +29 35 84 +29 36 24 +29 37 64 +29 38 84 +29 39 45 +29 40 33 +29 41 43 +29 42 31 +29 43 71 +29 44 24 +29 45 9 +29 46 24 +29 47 47 +29 48 51 +29 49 10 +29 50 36 +29 51 5 +29 52 89 +29 53 47 +29 54 53 +29 55 87 +29 56 3 +29 57 24 +29 58 51 +29 59 61 +30 31 75 +30 32 71 +30 33 84 +30 34 73 +30 35 64 +30 36 86 +30 37 61 +30 38 24 +30 39 1 +30 40 62 +30 41 94 +30 42 77 +30 43 44 +30 44 86 +30 45 58 +30 46 50 +30 47 45 +30 48 12 +30 49 23 +30 50 13 +30 51 17 +30 52 7 +30 53 98 +30 54 6 +30 55 8 +30 56 2 +30 57 98 +30 58 30 +30 59 19 +31 32 69 +31 33 89 +31 34 42 +31 35 5 +31 36 6 +31 37 100 +31 38 3 +31 39 21 +31 40 81 +31 41 87 +31 42 75 +31 43 34 +31 44 35 +31 45 34 +31 46 93 +31 47 23 +31 48 59 +31 49 13 +31 50 52 +31 51 26 +31 52 5 +31 53 68 +31 54 79 +31 55 41 +31 56 40 +31 57 86 +31 58 6 +31 59 71 +32 33 98 +32 34 91 +32 35 93 +32 36 39 +32 37 36 +32 38 72 +32 39 59 +32 40 49 +32 41 53 +32 42 82 +32 43 80 +32 44 88 +32 45 2 +32 46 8 +32 47 16 +32 48 90 +32 49 40 +32 50 98 +32 51 17 +32 52 14 +32 53 100 +32 54 28 +32 55 86 +32 56 93 +32 57 34 +32 58 6 +32 59 0 +33 34 78 +33 35 64 +33 36 94 +33 37 71 +33 38 0 +33 39 62 +33 40 11 +33 41 88 +33 42 46 +33 43 24 +33 44 83 +33 45 75 +33 46 94 +33 47 84 +33 48 42 +33 49 21 +33 50 77 +33 51 89 +33 52 57 +33 53 90 +33 54 33 +33 55 55 +33 56 7 +33 57 36 +33 58 48 +33 59 27 +34 35 55 +34 36 97 +34 37 21 +34 38 5 +34 39 98 +34 40 90 +34 41 17 +34 42 48 +34 43 80 +34 44 8 +34 45 22 +34 46 98 +34 47 74 +34 48 88 +34 49 68 +34 50 18 +34 51 63 +34 52 96 +34 53 11 +34 54 41 +34 55 53 +34 56 76 +34 57 82 +34 58 81 +34 59 76 +35 36 25 +35 37 12 +35 38 98 +35 39 69 +35 40 71 +35 41 21 +35 42 63 +35 43 40 +35 44 44 +35 45 32 +35 46 30 +35 47 100 +35 48 29 +35 49 11 +35 50 83 +35 51 38 +35 52 74 +35 53 78 +35 54 75 +35 55 79 +35 56 89 +35 57 22 +35 58 71 +35 59 76 +36 37 40 +36 38 36 +36 39 66 +36 40 56 +36 41 71 +36 42 43 +36 43 3 +36 44 28 +36 45 51 +36 46 31 +36 47 26 +36 48 82 +36 49 14 +36 50 21 +36 51 18 +36 52 21 +36 53 79 +36 54 12 +36 55 89 +36 56 22 +36 57 88 +36 58 11 +36 59 52 +37 38 39 +37 39 61 +37 40 33 +37 41 56 +37 42 22 +37 43 50 +37 44 55 +37 45 52 +37 46 42 +37 47 93 +37 48 60 +37 49 13 +37 50 55 +37 51 100 +37 52 57 +37 53 68 +37 54 55 +37 55 60 +37 56 95 +37 57 20 +37 58 46 +37 59 25 +38 39 29 +38 40 53 +38 41 74 +38 42 87 +38 43 53 +38 44 39 +38 45 54 +38 46 44 +38 47 7 +38 48 43 +38 49 1 +38 50 96 +38 51 44 +38 52 0 +38 53 18 +38 54 32 +38 55 98 +38 56 48 +38 57 30 +38 58 28 +38 59 40 +39 40 14 +39 41 77 +39 42 78 +39 43 0 +39 44 36 +39 45 3 +39 46 100 +39 47 76 +39 48 31 +39 49 24 +39 50 84 +39 51 0 +39 52 99 +39 53 27 +39 54 67 +39 55 56 +39 56 9 +39 57 28 +39 58 56 +39 59 52 +40 41 43 +40 42 33 +40 43 91 +40 44 3 +40 45 60 +40 46 69 +40 47 79 +40 48 44 +40 49 1 +40 50 16 +40 51 16 +40 52 5 +40 53 59 +40 54 28 +40 55 19 +40 56 82 +40 57 27 +40 58 71 +40 59 79 +41 42 57 +41 43 21 +41 44 61 +41 45 11 +41 46 28 +41 47 54 +41 48 55 +41 49 10 +41 50 95 +41 51 52 +41 52 56 +41 53 97 +41 54 75 +41 55 74 +41 56 81 +41 57 28 +41 58 12 +41 59 55 +42 43 57 +42 44 56 +42 45 11 +42 46 98 +42 47 8 +42 48 93 +42 49 99 +42 50 87 +42 51 12 +42 52 89 +42 53 60 +42 54 98 +42 55 17 +42 56 32 +42 57 86 +42 58 43 +42 59 29 +43 44 80 +43 45 6 +43 46 44 +43 47 100 +43 48 12 +43 49 39 +43 50 15 +43 51 63 +43 52 98 +43 53 54 +43 54 55 +43 55 1 +43 56 43 +43 57 14 +43 58 13 +43 59 50 +44 45 5 +44 46 22 +44 47 83 +44 48 35 +44 49 81 +44 50 77 +44 51 68 +44 52 75 +44 53 10 +44 54 21 +44 55 61 +44 56 70 +44 57 75 +44 58 44 +44 59 99 +45 46 67 +45 47 18 +45 48 15 +45 49 16 +45 50 46 +45 51 52 +45 52 99 +45 53 21 +45 54 70 +45 55 69 +45 56 52 +45 57 29 +45 58 21 +45 59 30 +46 47 73 +46 48 22 +46 49 22 +46 50 92 +46 51 45 +46 52 11 +46 53 94 +46 54 18 +46 55 96 +46 56 64 +46 57 16 +46 58 18 +46 59 69 +47 48 38 +47 49 18 +47 50 1 +47 51 1 +47 52 7 +47 53 4 +47 54 79 +47 55 50 +47 56 72 +47 57 24 +47 58 33 +47 59 66 +48 49 7 +48 50 1 +48 51 57 +48 52 61 +48 53 57 +48 54 6 +48 55 87 +48 56 75 +48 57 1 +48 58 96 +48 59 58 +49 50 39 +49 51 84 +49 52 7 +49 53 27 +49 54 44 +49 55 2 +49 56 94 +49 57 59 +49 58 94 +49 59 99 +50 51 71 +50 52 53 +50 53 7 +50 54 58 +50 55 88 +50 56 98 +50 57 21 +50 58 65 +50 59 26 +51 52 77 +51 53 46 +51 54 95 +51 55 8 +51 56 42 +51 57 21 +51 58 35 +51 59 62 +52 53 9 +52 54 60 +52 55 33 +52 56 59 +52 57 78 +52 58 7 +52 59 3 +53 54 65 +53 55 92 +53 56 59 +53 57 72 +53 58 55 +53 59 37 +54 55 96 +54 56 45 +54 57 44 +54 58 95 +54 59 69 +55 56 35 +55 57 52 +55 58 82 +55 59 7 +56 57 2 +56 58 23 +56 59 86 +57 58 89 +57 59 78 +58 59 14 diff --git a/experiments/data/acceptance60_1.txt b/experiments/data/acceptance60_1.txt new file mode 100644 index 0000000..342b465 --- /dev/null +++ b/experiments/data/acceptance60_1.txt @@ -0,0 +1,60 @@ +0 37 26 29 47 25 13 18 53 2 33 +1 48 30 22 20 9 49 41 12 33 27 +2 17 32 52 58 27 35 18 10 7 55 +3 41 31 8 22 5 25 19 4 14 18 +4 1 57 47 30 2 15 50 39 36 14 +5 1 22 28 35 47 26 20 53 18 9 +6 44 29 31 19 57 12 15 26 51 48 +7 14 6 10 23 18 37 39 50 32 3 +8 58 5 28 20 33 38 48 19 34 44 +9 1 13 50 14 55 11 15 12 3 0 +10 15 13 21 34 45 6 20 33 26 54 +11 45 15 22 14 35 21 46 5 58 30 +12 5 13 31 25 0 37 29 53 1 49 +13 12 7 29 16 43 50 19 22 3 41 +14 20 59 30 43 36 19 24 52 55 49 +15 59 29 10 39 8 0 51 47 23 35 +16 52 39 28 3 19 57 7 8 30 5 +17 49 53 34 3 47 29 25 58 16 30 +18 16 8 57 23 1 51 0 40 49 50 +19 8 58 38 20 23 36 53 42 59 7 +20 13 6 30 1 47 15 46 31 45 33 +21 14 32 8 0 4 50 15 41 25 37 +22 0 2 10 56 52 40 29 55 9 17 +23 44 46 27 58 36 0 43 18 25 34 +24 13 54 27 15 32 40 33 23 52 22 +25 5 13 27 37 50 33 7 6 42 18 +26 34 23 44 15 17 3 41 1 31 50 +27 29 53 35 57 9 20 58 23 36 10 +28 18 24 52 8 5 50 9 40 37 13 +29 46 50 11 19 0 39 25 54 23 16 +30 5 14 55 2 49 6 43 18 23 10 +31 29 36 18 28 37 57 49 47 1 5 +32 15 27 44 57 7 1 26 25 55 43 +33 39 57 29 27 6 55 45 50 7 11 +34 23 10 54 11 39 18 0 17 31 37 +35 18 4 42 29 30 27 25 38 12 56 +36 46 58 30 40 22 53 21 18 24 51 +37 12 42 48 59 25 58 30 49 11 4 +38 59 39 27 36 8 22 55 13 11 43 +39 38 33 2 27 41 22 59 13 53 0 +40 57 35 15 41 30 24 28 48 58 44 +41 19 33 28 48 13 58 17 50 18 42 +42 5 48 19 15 44 22 12 8 9 1 +43 29 13 28 42 2 30 56 0 17 21 +44 18 6 32 41 16 24 15 30 55 33 +45 56 47 29 1 42 54 35 34 57 37 +46 54 42 20 10 32 58 2 4 13 36 +47 26 7 55 21 37 20 5 27 39 57 +48 33 22 12 35 17 20 1 23 59 54 +49 15 35 50 24 51 55 19 48 54 36 +50 6 43 10 29 56 11 21 54 27 36 +51 38 23 31 43 10 41 28 40 37 42 +52 49 28 14 58 45 47 11 41 29 3 +53 6 55 49 39 7 17 16 59 9 35 +54 12 0 3 1 53 26 56 7 45 29 +55 1 54 56 31 36 7 52 12 25 9 +56 49 18 40 45 24 37 25 55 12 4 +57 18 30 19 32 6 1 7 28 2 38 +58 25 30 42 39 38 14 15 24 0 52 +59 56 39 45 5 18 41 13 3 1 0 diff --git a/main.py b/main.py index 322615e..7fea115 100644 --- a/main.py +++ b/main.py @@ -1,16 +1,17 @@ -from utils.helperFunctions import generate_random_student_list, generate_students_json, print_students_table, run_generations_experiment, run_mutation_experiment, run_mutation_timing_experiment, run_population_experiment, run_population_timing_experiment, run_timing_experiment, translate_file_to_students +from utils.helperFunctions import generate_random_student_list, generate_students_json, print_students_table, run_generations_experiment, run_mutation_experiment, run_mutation_timing_experiment, run_population_experiment, run_population_timing_experiment, run_timing_experiment, translate_file_to_students, parse_problem_files from Genetic.StandardGenetic import genetic_algorithm from Genetic.PreferencesGenetic import genetic_algorithm_with_preferences from ABC.StandardABC import abc_algorithm from ABC.PrefrencesABC import abc_algorithm_with_prefrences +import utils.experimentsFunctions as experiments +from termcolor import colored def get_filename(num_students: int, num_criteria: int): return f"samples\students({num_students})_criteria({num_criteria}).json" -def test_standard_genetic(students): +def test_standard_genetic(students, num_groups): # פרמטרים - num_groups = 3 population_size = 5 generations = 50 mutation_rate = 0.3 @@ -19,51 +20,58 @@ def test_standard_genetic(students): best_solution = genetic_algorithm(students, num_groups, population_size, generations, mutation_rate) # הדפסת הפתרון הטוב ביותר - print("\nBest Solution:") - for i, group in enumerate(best_solution, 1): - print(f"Group {i}: {group}") + print_solution_with_highlights(best_solution) -def test_preferences_genetic(students): +def test_preferences_genetic(students, num_groups): # פרמטרים - num_groups = 3 population_size = 5 - generations = 50 + generations = 200 mutation_rate = 0.3 # הרצה best_solution = genetic_algorithm_with_preferences(students, num_groups, population_size, generations, mutation_rate) # הדפסת הפתרון הטוב ביותר - print("\nBest Solution:") - for i, group in enumerate(best_solution, 1): - print(f"Group {i} ({len(group)}): {group}") + print_solution_with_highlights(best_solution) -def test_standard_abc(students): +def test_standard_abc(students, num_groups): # פרמטרים - num_groups = 3 num_iterations = 50 limit = 10 # הרצה best_solution = abc_algorithm(students, num_groups, num_iterations, limit) # הדפסת הפתרון הטוב ביותר - print("\nBest Solution:") - for i, group in enumerate(best_solution, 1): - print(f"Group {i}: {group}") + print_solution_with_highlights(best_solution) -def test_prefrences_abc(students): +def test_prefrences_abc(students, num_groups): # פרמטרים - num_groups = 3 num_iterations = 50 limit = 10 # הרצה best_solution = abc_algorithm_with_prefrences(students, num_groups, num_iterations, limit) # הדפסת הפתרון הטוב ביותר + print_solution_with_highlights(best_solution) + +def print_solution_with_highlights(solution): + """ + מדפיסה את הפתרון בצורה מוארת: ירוק אם התלמיד בקבוצה עם מישהו שהוא רוצה להיות איתו, אדום אם לא. + """ print("\nBest Solution:") - for i, group in enumerate(best_solution, 1): - print(f"Group {i}: {group}") + for i, group in enumerate(solution, 1): + print(f"Group {i} ({len(group)}):") + group_ids = [student.id for student in group] + for student in group: + in_group_with_preferences = any(pref in group_ids for pref in student.preferences) + if in_group_with_preferences: + print(colored(f" {student.id}: {student.preferences}", "green")) + else: + print(colored(f" {student.id}: {student.preferences}", "red")) + if __name__ == "__main__": - students = translate_file_to_students(get_filename(15, 1)) - test_prefrences_abc(students) + ranint_path = "RanInt_n060_ss_01.txt" + acceptance_path = "acceptance60_1.txt" + students, num_groups = parse_problem_files(ranint_path, acceptance_path) + experiments.experiment_ABC_iterations(students, num_groups, 10) diff --git a/requirements.txt b/requirements.txt index 89f7140..9980be8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -tabulate \ No newline at end of file +tabulate +termcolor \ No newline at end of file diff --git a/utils/experimentsFunctions.py b/utils/experimentsFunctions.py new file mode 100644 index 0000000..a768c6e --- /dev/null +++ b/utils/experimentsFunctions.py @@ -0,0 +1,49 @@ +import random +import csv +import json +import time +from Genetic.PreferencesGenetic import calculate_diversity, genetic_algorithm_with_preferences +from ABC.PrefrencesABC import abc_algorithm_with_prefrences +from utils.student import Student +from typing import List, Tuple +from tabulate import tabulate + +def experiment_ABC_iterations(students: List[Student], num_groups: int, limit: int): + output_file = "ABC/Iterations" + # ציר X + iterations = list(range(1, 1001)) + # ציר Y + results_fitness = [] + results_time = [] + + for iteration in iterations: + print(f"Running ABC on {iteration} iterations...") + score_sum = 0 + time_sum = 0 + for _ in range(10): + start_time = time.time() + result = abc_algorithm_with_prefrences(students, num_groups, iteration, limit) + elapsed_time = time.time() - start_time + time_sum += elapsed_time + score = calculate_diversity(result) + score_sum += score + results_fitness.append((score_sum/10)) + results_time.append((time_sum/10)) + + with open(f"experiments/{output_file}_Fitness.csv", mode="w", newline='') as file: + fieldnames = ["Iteration", "AVG Score"] + writer = csv.writer(file) + writer.writerow(fieldnames) + writer.writerows(zip(iterations, results_fitness)) + + print(f"Experiment completed! Results saved to {output_file}_Fitness.csv") + + with open(f"experiments/{output_file}_Time.csv", mode="w", newline='') as file: + fieldnames = ["Iteration", "AVG Time"] + writer = csv.writer(file) + writer.writerow(fieldnames) + writer.writerows(zip(iterations, results_time)) + + print(f"Experiment completed! Results saved to {output_file}_Time.csv") + + \ No newline at end of file diff --git a/utils/helperFunctions.py b/utils/helperFunctions.py index 9e244a4..25c4f19 100644 --- a/utils/helperFunctions.py +++ b/utils/helperFunctions.py @@ -4,7 +4,7 @@ import time from Genetic.PreferencesGenetic import calculate_diversity, genetic_algorithm_with_preferences from utils.student import Student -from typing import List +from typing import List, Tuple from tabulate import tabulate def generate_random_student_list(amount: int, criteria: List[dict]) -> List[Student]: @@ -131,6 +131,49 @@ def translate_file_to_students(file_name: str) -> List[Student]: print(f"קרתה שגיאה לא יודעה: {e}") return [] +def parse_problem_files(ranint_path: str, acceptance_path: str) -> Tuple[List[Student], int]: + """ + פונקציה שמעבדת את זוג הקבצים RanInt ו-acceptance ומחזירה את רשימת הסטודנטים וכמות הקבוצות. + + :param ranint_path: הנתיב לקובץ RanInt. + :param acceptance_path: הנתיב לקובץ acceptance. + :return: זוג (רשימת סטודנטים, מספר קבוצות). + """ + # קריאת קובץ RanInt + with open('experiments/data/' + ranint_path, 'r') as f: + lines = f.readlines() + + first_line = lines[0].strip().split() + num_students = int(first_line[0]) + num_groups = int(first_line[1]) + + criteria = {i: {} for i in range(num_students)} + for line in lines[1:]: + student1, student2, score = map(int, line.strip().split()) + if student2 not in criteria[student1]: + criteria[student1][student2] = score + if student1 not in criteria[student2]: + criteria[student2][student1] = score + + # קריאת קובץ acceptance + with open('experiments/data/' + acceptance_path, 'r') as f: + acceptance_lines = f.readlines() + + # יצירת אובייקטים של סטודנטים + students = [] + for line in acceptance_lines: + student_id = int(line.strip().split()[0]) + preferences = list(map(int, line.strip().split()[1:])) + data = { + "id": student_id, + "name": f'Student_{student_id}', + "preferences": preferences, + "criteria": criteria[student_id] + } + students.append(Student(data, experiment = True)) + + return students, num_groups + def run_generations_experiment(students: List[Student], num_groups: int, population_size: int, mutation_rate: float, output_file: str): generations_to_test = range(10, 501, 10) results = [] diff --git a/utils/student.py b/utils/student.py index 3549045..e00a1b6 100644 --- a/utils/student.py +++ b/utils/student.py @@ -2,7 +2,7 @@ class Student: """ מחלקה לייצוג סטודנט, הכוללת נתונים כמו מזהה, שם, העדפות וקריטריונים לחישוב ציון. """ - def __init__(self, student_data: dict): + def __init__(self, student_data: dict, experiment: bool = False): """ אתחול אובייקט סטודנט. @@ -14,29 +14,36 @@ def __init__(self, student_data: dict): """ self.id = student_data.get("id") self.name = student_data.get("name") - self.preferences = student_data.get("preferences", []) - self.criteria = student_data.get("criteria", []) + self.preferences = student_data.get("preferences", []) + self.criteria = student_data.get("criteria", {} if experiment else []) + self.experiment = experiment - def get_score(self) -> float: + def get_score(self, student = None) -> float: """ מחשב את הציון הכולל של הסטודנט על בסיס הקריטריונים. :return: הציון הכולל (float). """ - total_score = 0.0 - for criteria in self.criteria: - criteria_type = criteria.get("type") - value = float(criteria.get("value", 0)) + if self.experiment: + if student: + return self.criteria[student.id] + else: + return 0 + else: + total_score = 0.0 + for criteria in self.criteria: + criteria_type = criteria.get("type") + value = float(criteria.get("value", 0)) - # חישוב הציון לפי סוג הקריטריון - if criteria_type == "0-1": - total_score += value * 100 - elif criteria_type == "0-10": - total_score += value * 10 - elif criteria_type == "0-100": - total_score += value + # חישוב הציון לפי סוג הקריטריון + if criteria_type == "0-1": + total_score += value * 100 + elif criteria_type == "0-10": + total_score += value * 10 + elif criteria_type == "0-100": + total_score += value - return total_score + return total_score def __repr__(self): """