From a5fe5c10baf3b17609f70932d06de47602daef9d Mon Sep 17 00:00:00 2001
From: Benoit <benoit.chevillon@etu.utc.fr>
Date: Sun, 1 Oct 2023 18:41:47 +0200
Subject: [PATCH] Gagnant de condorcet

---
 comsoc/Approval.go        | 24 +++++++++++++++---
 comsoc/CondorcetWinner.go | 30 +++++++++++++++++++++-
 comsoc/HelpFunctions.go   |  9 ++++---
 comsoc/Majority.go        | 52 ---------------------------------------
 4 files changed, 54 insertions(+), 61 deletions(-)

diff --git a/comsoc/Approval.go b/comsoc/Approval.go
index e1b7639..4450607 100644
--- a/comsoc/Approval.go
+++ b/comsoc/Approval.go
@@ -1,5 +1,21 @@
 package comsoc
-import"fmt"
-func Test1() {
-    fmt.Println("Hello, World!")
-}
\ No newline at end of file
+
+// Vote par approbation
+// thresholds est un slice d'entiers strictement positifs
+func ApprovalSWF(p Profile, thresholds []int) (count Count, err error) {
+	count = make(Count)
+	for _, pref := range p {
+		for index, alt := range pref {
+			count[alt] += thresholds[len(thresholds)-1-index]
+		}
+	}
+	return count, nil
+}
+
+func ApprovalSCF(p Profile, thresholds []int) (bestAlts []Alternative, err error) {
+	count, err := ApprovalSWF(p, thresholds)
+	if err != nil {
+		return nil, err
+	}
+	return maxCount(count), nil
+}
diff --git a/comsoc/CondorcetWinner.go b/comsoc/CondorcetWinner.go
index 7076a9b..60cfdd1 100644
--- a/comsoc/CondorcetWinner.go
+++ b/comsoc/CondorcetWinner.go
@@ -1 +1,29 @@
-package comsoc
\ No newline at end of file
+package comsoc
+
+// Gagnant de condorcet, retourne un slice vide ou de 1 élément
+// A vérifier avec plus d'exemples
+func CondorcetWinner(p Profile) (bestAlts []Alternative, err error) {
+	for _, alt1 := range p[0] {
+		winner := true
+		for _, alt2 := range p[0] {
+			if alt1 != alt2 {
+				nbAlt1 := 0
+				nbAlt2 := 0
+				for _, pref := range p {
+					if isPref(alt1, alt2, pref) {
+						nbAlt1++
+					} else {
+						nbAlt2++
+					}
+				}
+				if nbAlt1 <= nbAlt2 {
+					winner = false
+				}
+			}
+		}
+		if winner {
+			return []Alternative{alt1}, nil
+		}
+	}
+	return []Alternative{}, nil
+}
diff --git a/comsoc/HelpFunctions.go b/comsoc/HelpFunctions.go
index d6cca4a..3621bd0 100644
--- a/comsoc/HelpFunctions.go
+++ b/comsoc/HelpFunctions.go
@@ -114,12 +114,13 @@ func Test_checkProfileAlternative() {
 }
 
 func Test_MajoritySWF() {
-	pref1 := []Alternative{5, 3, 1, 4, 2}
-	pref2 := []Alternative{2, 1, 4, 3, 5}
-	Pref := [][]Alternative{pref1, pref2}
+	pref1 := []Alternative{1, 2, 3}
+	pref2 := []Alternative{2, 3, 1}
+	pref3 := []Alternative{3, 1, 2}
+	Pref := [][]Alternative{pref1, pref2, pref3}
 	profil := Profile(Pref)
 	fmt.Println(MajoritySWF(profil))
 	c, _ := ApprovalSWF(profil, []int{1, 2, 3, 4, 5})
 	fmt.Println(c[Alternative(2)])
-	//fmt.Println(CondorcetWinner(profil))
+	fmt.Println(CondorcetWinner(profil))
 }
diff --git a/comsoc/Majority.go b/comsoc/Majority.go
index d60f046..5f3ecfa 100644
--- a/comsoc/Majority.go
+++ b/comsoc/Majority.go
@@ -19,55 +19,3 @@ func MajoritySCF(p Profile) (bestAlts []Alternative, err error) {
 	}
 	return maxCount(count), nil
 }
-
-func ApprovalSWF(p Profile, thresholds []int) (count Count, err error) {
-	count = make(Count)
-	for _, pref := range p {
-		for index, alt := range pref {
-			count[alt] += thresholds[len(thresholds)-1-index]
-		}
-	}
-	return count, nil
-}
-
-func ApprovalSCF(p Profile, thresholds []int) (bestAlts []Alternative, err error) {
-	count, err := ApprovalSWF(p, thresholds)
-	if err != nil {
-		return nil, err
-	}
-	return maxCount(count), nil
-}
-
-// Gagnant de condorcet, retourne un slice vide ou de 1 élément
-// Utilise la majorité simple sur chaque paire de candidat
-/*
-func CondorcetWinner(p Profile) (bestAlts []Alternative, err error) {
-	for _, alt1 := range p[0] {
-		winner := true
-		for _, alt2 := range p[0] {
-			if alt1 != alt2 {
-				p2 := make(Profile, len(p))
-				for index, pref := range p {
-					if isPref(alt1, alt2, pref) {
-						p2[index] = append(p2[index], alt1)
-					} else {
-						p2[index] = append(p2[index], alt2)
-					}
-				}
-				count, err := MajoritySCF(p2)
-				if err != nil {
-					return nil, err
-				}
-				if count[alt2] >= count[alt1] {
-					winner = false
-					break
-				}
-			}
-		}
-		if winner {
-			return []Alternative{alt1}, nil
-		}
-	}
-	return []Alternative{}, nil
-}
-*/
-- 
GitLab