Skip to content
Snippets Groups Projects
Commit cca8bcb5 authored by Benoit Chevillon's avatar Benoit Chevillon Committed by Balthazar Wilson
Browse files

Gagnant de condorcet

parent 0815c019
No related branches found
No related tags found
1 merge request!7Partie serveur
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
}
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
}
......@@ -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))
}
......@@ -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
}
*/
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment