Skip to content
Snippets Groups Projects
Commit e00a3667 authored by Benoit Chevillon's avatar Benoit Chevillon
Browse files

Merge branch 'benoit' into 'main'

Benoit

See merge request gvandevi/ia04binome2a!2
parents 7d4200e4 a5fe5c10
No related branches found
No related tags found
No related merge requests found
package comsoc package comsoc
import"fmt"
func Test1() { // Vote par approbation
fmt.Println("Hello, World!") // thresholds est un slice d'entiers strictement positifs
} func ApprovalSWF(p Profile, thresholds []int) (count Count, err error) {
\ No newline at end of file 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 package comsoc
\ No newline at end of file
// 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
}
package comsoc package comsoc
import ( import (
"fmt"
"errors" "errors"
"fmt"
) )
//cours TODO // cours TODO
func plusN (n int) (func (i int)(int)){ func plusN(n int) func(i int) int {
f:=func(i int) int { f := func(i int) int {
return (i+n) return (i + n)
} }
return f return f
} }
// renvoie l'indice ou se trouve alt dans prefs // renvoie l'indice ou se trouve alt dans prefs
func rank(alt Alternative, prefs []Alternative) int { func rank(alt Alternative, prefs []Alternative) int {
for i := 0; i < len(prefs);i++{ for i := 0; i < len(prefs); i++ {
if prefs[i]==alt{ if prefs[i] == alt {
return i return i
} }
} }
return -1 return -1
} }
func Test_rank(){ func Test_rank() {
tableau := []Alternative{1, 2, 3, 4, 5} tableau := []Alternative{1, 2, 3, 4, 5}
indice := rank(tableau[3],tableau) indice := rank(tableau[3], tableau)
fmt.Print(indice) fmt.Print(indice)
} }
// renvoie vrai ssi alt1 est préférée à alt2 // renvoie vrai ssi alt1 est préférée à alt2
func isPref(alt1, alt2 Alternative, prefs []Alternative) bool { func isPref(alt1, alt2 Alternative, prefs []Alternative) bool {
// if (alt1 <0 || alt2 <0){ // if (alt1 <0 || alt2 <0){
// return errors.New("l'une des deux valeurs n'est pas présente dans le tableau") // return errors.New("l'une des deux valeurs n'est pas présente dans le tableau")
// }else { // }else {
return rank(alt1,prefs)<rank(alt2,prefs) return rank(alt1, prefs) < rank(alt2, prefs)
} }
// } // }
func Test_isPref(){ func Test_isPref() {
tableau := []Alternative{1, 2, 3, 4, 5} tableau := []Alternative{1, 2, 3, 4, 5}
alt1 := Alternative(1) alt1 := Alternative(1)
alt2 := Alternative(2) alt2 := Alternative(2)
response := isPref(alt1, alt2, tableau) response := isPref(alt1, alt2, tableau)
fmt.Print(response) fmt.Print(response)
} }
// renvoie les meilleures alternatives pour un décompte donné // renvoie les meilleures alternatives pour un décompte donné
func maxCount(count Count) (bestAlts []Alternative) { func maxCount(count Count) (bestAlts []Alternative) {
maxPoints:=-1 maxPoints := -1
for i, points := range count{ for i, points := range count {
if points > maxPoints { if points > maxPoints {
bestAlts = []Alternative{Alternative(i)} bestAlts = []Alternative{Alternative(i)}
maxPoints = points maxPoints = points
}else if points == maxPoints{ } else if points == maxPoints {
bestAlts=append(bestAlts,Alternative(i)) bestAlts = append(bestAlts, Alternative(i))
} }
} }
return bestAlts return bestAlts
} }
func Test_maxCount(){ func Test_maxCount() {
count := make(map[Alternative]int) count := make(map[Alternative]int)
count[Alternative(1)] = 3 count[Alternative(1)] = 3
count[Alternative(2)] = 5 count[Alternative(2)] = 5
...@@ -63,25 +66,25 @@ func Test_maxCount(){ ...@@ -63,25 +66,25 @@ func Test_maxCount(){
fmt.Println(maxCount(count)) fmt.Println(maxCount(count))
} }
// // vérifie les préférences d'un agent, par ex. // // vérifie les préférences d'un agent, par ex.
//qu'ils sont tous complets et que chaque alternative n'apparaît qu'une seule fois // qu'ils sont tous complets et que chaque alternative n'apparaît qu'une seule fois
func checkProfile(prefs []Alternative, alts []Alternative) error { func checkProfile(prefs []Alternative, alts []Alternative) error {
//vérifier //vérifier
if (len (prefs) < len(alts)){ if len(prefs) < len(alts) {
return errors.New("Il manque des alternatives") return errors.New("Il manque des alternatives")
}else if(len (prefs)>len(alts)){ } else if len(prefs) > len(alts) {
return errors.New("Il y a des alternatives en trop.") return errors.New("Il y a des alternatives en trop.")
}else{//vérifier complet } else { //vérifier complet
for _, element := range alts{ for _, element := range alts {
if rank(element, prefs) == -1 { if rank(element, prefs) == -1 {
return errors.New("au moins une alternative est absente des préférences") return errors.New("au moins une alternative est absente des préférences")
} }
} }
} }
return nil return nil
} }
func Test_checkProfile(){ func Test_checkProfile() {
alts := []Alternative{1, 2, 3, 4, 5} alts := []Alternative{1, 2, 3, 4, 5}
//prefs := []Alternative{3, 1, 5, 2, 4} //prefs := []Alternative{3, 1, 5, 2, 4}
//prefs := []Alternative{1, 2, 3, 4, 5,6} //prefs := []Alternative{1, 2, 3, 4, 5,6}
...@@ -89,23 +92,35 @@ func Test_checkProfile(){ ...@@ -89,23 +92,35 @@ func Test_checkProfile(){
fmt.Println(checkProfile(prefs, alts)) fmt.Println(checkProfile(prefs, alts))
} }
// vérifie le profil donné, par ex. qu'ils sont tous complets // vérifie le profil donné, par ex. qu'ils sont tous complets
// et que chaque alternative de alts apparaît exactement une fois par préférences // et que chaque alternative de alts apparaît exactement une fois par préférences
func checkProfileAlternative(prefs Profile, alts []Alternative) error { func checkProfileAlternative(prefs Profile, alts []Alternative) error {
for _, row := range prefs{ for _, row := range prefs {
e:=checkProfile(row, alts) e := checkProfile(row, alts)
if e != nil{ if e != nil {
return e return e
} }
} }
return nil return nil
} }
func Test_checkProfileAlternative(){ func Test_checkProfileAlternative() {
alts := []Alternative{1, 2, 3, 4, 5} alts := []Alternative{1, 2, 3, 4, 5}
pref1 := []Alternative{5, 2, 1, 4, 3} pref1 := []Alternative{5, 3, 1, 4, 2}
pref2 := []Alternative{1, 5, 4, 3, 2} pref2 := []Alternative{1, 5, 4, 3, 2}
Pref := [][]Alternative{pref1,pref2} Pref := [][]Alternative{pref1, pref2}
profil := Profile(Pref) profil := Profile(Pref)
fmt.Println(checkProfileAlternative(profil, alts)) fmt.Println(checkProfileAlternative(profil, alts))
} }
\ No newline at end of file
func Test_MajoritySWF() {
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))
}
...@@ -3,15 +3,19 @@ package comsoc ...@@ -3,15 +3,19 @@ package comsoc
//majorité simple //majorité simple
// fonctions de bien-être social (social welfare function, SWF) : // fonctions de bien-être social (social welfare function, SWF) :
// retournent un décompte à partir d'un profil // retournent un décompte à partir d'un profil
func MajoritySWF(p Profile) (count Count, err error){ // En utilisation la méthode de la majorité simple
//1) checkProfileAlternative(prefs Profile, alts []Alternative) func MajoritySWF(p Profile) (count Count, err error) {
count := make(map[Alternative]int) count = make(Count)
for _, row := range p{ for _, pref := range p {
count[row[0]]+=1 count[pref[0]]++
} }
return count return count, nil
} }
// fonctions de choix social (choix social, social choice function, SCF) func MajoritySCF(p Profile) (bestAlts []Alternative, err error) {
// renvoient uniquement les alternatives préférées. count, err := MajoritySWF(p)
func MajoritySCF(p Profile) (bestAlts []Alternative, err error){} if err != nil {
return nil, err
}
return maxCount(count), nil
}
package main package main
import ( import (
"gitlab.utc.fr/gvandevi/ia04/comsoc" "gitlab.utc.fr/gvandevi/ia04binome2a/comsoc"
) )
func main(){ func main() {
//Fonctions utilitaires //Fonctions utilitaires
//comsoc.Test_rank() //comsoc.Test_rank()
//comsoc.Test_maxCount() //comsoc.Test_maxCount()
//comsoc.Test_checkProfile() //comsoc.Test_checkProfile()
comsoc.Test_checkProfileAlternative() //comsoc.Test_checkProfileAlternative()
comsoc.Test_MajoritySWF()
} }
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