From 23126687e36ad766a41223b4b4e643ac88549028 Mon Sep 17 00:00:00 2001
From: Gabrielle van de Vijver <gabrielle.van-de-vijver@etu.utc.fr>
Date: Sun, 5 Nov 2023 17:08:21 +0100
Subject: [PATCH] fix MajoritySWF

---
 cmd/main.go             |  4 ++-
 comsoc/HelpFunctions.go |  1 -
 comsoc/Majority.go      | 22 +++++++++++----
 comsoc/TieBreak.go      | 60 ++++++++++++++++++++++-------------------
 4 files changed, 52 insertions(+), 35 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go
index d8e3326..12e20fe 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -10,5 +10,7 @@ func main() {
 	//comsoc.Test_maxCount()
 	//comsoc.Test_checkProfile()
 	//comsoc.Test_checkProfileAlternative()
-	comsoc.Test_MajoritySWF()
+	//comsoc.Test_MajoritySWF()
+	//comsoc.Test_sWFFactory()
+	comsoc.Test_majority()
 }
diff --git a/comsoc/HelpFunctions.go b/comsoc/HelpFunctions.go
index fdf1558..b5718b2 100644
--- a/comsoc/HelpFunctions.go
+++ b/comsoc/HelpFunctions.go
@@ -5,7 +5,6 @@ import (
 	"fmt"
 )
 
-// cours TODO
 func plusN(n int) func(i int) int {
 	f := func(i int) int {
 		return (i + n)
diff --git a/comsoc/Majority.go b/comsoc/Majority.go
index cce9c75..f35f70c 100644
--- a/comsoc/Majority.go
+++ b/comsoc/Majority.go
@@ -1,15 +1,18 @@
 package comsoc
+import ("fmt")
 
-// majorité simple
-// fonctions de bien-être social (social welfare function, SWF) :
-// retournent un décompte à partir d'un profil
-// En utilisation la méthode de la majorité simple
+// Majorité simple
 func MajoritySWF(p Profile) (count Count, err error) {
-	count = make(Count)
 	err = checkProfileFromProfile(p)
 	if err != nil {
 		return nil, err
 	}
+//initialisation de la map : comptes à 0
+	count = make(Count,len(p))
+	for _,alt := range p[0]{
+		count[alt]=0
+	}
+//actualisation du compte à l'aide du scrutin
 	for _, pref := range p {
 		count[pref[0]]++
 	}
@@ -23,3 +26,12 @@ func MajoritySCF(p Profile) (bestAlts []Alternative, err error) {
 	}
 	return maxCount(count), err
 }
+
+func Test_majority() {
+	profil := GenerateProfile(3, 5)
+	fmt.Println("Profil :", profil)
+	count, _ := MajoritySWF(profil)
+	fmt.Println("Décompte :", count)
+	//winners, _ := MajoritySCF(profil)
+	//fmt.Println("Vainqueur(s) :", winners)
+}
\ No newline at end of file
diff --git a/comsoc/TieBreak.go b/comsoc/TieBreak.go
index 814c306..d9f2565 100644
--- a/comsoc/TieBreak.go
+++ b/comsoc/TieBreak.go
@@ -62,6 +62,28 @@ func SWFFactory(swf func(Profile) (Count, error), tb func([]Alternative) (Altern
 	}
 }
 
+func Test_sWFFactory() {
+	//Définition de l'Ordre strict
+	orderedAlts := []Alternative{8, 9, 6, 1, 3, 2}
+	fmt.Println("Ordre strict :", orderedAlts)
+
+	//Construction d'un profil avec alternatives ex aequo
+	profil := make([][]Alternative, 2)
+	profil[0] = []Alternative{1, 2, 3, 4, 5, 6}
+	profil[1] = []Alternative{3, 2, 1, 4, 5, 6}
+	fmt.Println("Profil :", profil)
+
+	//Construction de la fonction Tie Break
+	lambda := TieBreakFactory(orderedAlts)
+	//Construction de la Social Welfare Factory à partir de la fonction swf + la fonction de TieBreak
+	mu := SWFFactory(MajoritySWF, lambda)
+	// mu := SWFFactory(BordaSWF, lambda)
+	//Construction d'une fonction
+	sorted_alts, _ := mu(profil)
+	fmt.Println("Alternatives strictement ordonnées selon la méthode de Borda :", sorted_alts)
+}
+
+
 func SWFFactoryWithOptions(
 	swf func(Profile, []int) (Count, error),
 	tb func([]Alternative) (Alternative, error),
@@ -103,30 +125,14 @@ func SWFFactoryWithOptions(
 	}
 }
 
-func Test_sWFFactory() {
-	//Définition de l'Ordre strict
-	orderedAlts := []Alternative{8, 9, 6, 1, 3, 2}
-	fmt.Println("Ordre strict :", orderedAlts)
 
-	//Construction d'un profil avec alternatives ex aequo
-	profil := make([][]Alternative, 2)
-	profil[0] = []Alternative{1, 2, 3, 4, 5, 6}
-	profil[1] = []Alternative{3, 2, 1, 4, 5, 6}
-	fmt.Println("Profil :", profil)
-
-	//Construction de la fonction Tie Break
-	lambda := TieBreakFactory(orderedAlts)
-	//Construction de la Social Welfare Factory à partir de la fonction swf + la fonction de TieBreak
-	mu := SWFFactory(BordaSWF, lambda)
-	//Construction d'une fonction
-	sorted_alts, _ := mu(profil)
-	fmt.Println("Alternatives strictement ordonnées selon la méthode de Borda :", sorted_alts)
-}
-
-func SCFFactory(scf func(p Profile) ([]Alternative, error), tb func([]Alternative) (Alternative, error)) func(Profile) (Alternative, error) {
-	return func(p Profile) (Alternative, error) {
+func SCFFactoryWithOptions(
+	scf func(Profile, []int) ([]Alternative, error),
+	tb func([]Alternative) (Alternative, error),
+) func(Profile, []int) (Alternative, error) {
+	return func(p Profile, o []int) (Alternative, error) {
 		//récupération des meilleures alternatives
-		bestAlts, errSCF := scf(p)
+		bestAlts, errSCF := scf(p, o)
 		if errSCF != nil {
 			return Alternative(0), errSCF
 		}
@@ -136,13 +142,10 @@ func SCFFactory(scf func(p Profile) ([]Alternative, error), tb func([]Alternativ
 	}
 }
 
-func SCFFactoryWithOptions(
-	scf func(Profile, []int) ([]Alternative, error),
-	tb func([]Alternative) (Alternative, error),
-) func(Profile, []int) (Alternative, error) {
-	return func(p Profile, o []int) (Alternative, error) {
+func SCFFactory(scf func(p Profile) ([]Alternative, error), tb func([]Alternative) (Alternative, error)) func(Profile) (Alternative, error) {
+	return func(p Profile) (Alternative, error) {
 		//récupération des meilleures alternatives
-		bestAlts, errSCF := scf(p, o)
+		bestAlts, errSCF := scf(p)
 		if errSCF != nil {
 			return Alternative(0), errSCF
 		}
@@ -152,6 +155,7 @@ func SCFFactoryWithOptions(
 	}
 }
 
+
 func Test_sCFFactory() {
 	//Définition de l'Ordre strict
 	orderedAlts := []Alternative{8, 9, 6, 1, 3, 2}
-- 
GitLab