diff --git a/comsoc/Approval.go b/comsoc/Approval.go index e1b7639e6857b9af90e2355236a4721cc9bef147..4450607b23ce3ca5c8c8ab224635c8300a8da32b 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 7076a9b4de20578f2bfe7749ab88012274203de2..60cfdd1f2a8cfea06e84c80aab3147a961977530 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 d6cca4a141370ea3cdae8fdc4577af2fa6d42d3c..3621bd00572494b84f98a8e33c35670340a2eada 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 d60f0463e70cfef8499426d2af6c3e677091c178..5f3ecfafd5443e2b7e706bbaf1b0e2e35b5e2a39 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 -} -*/