diff --git a/_test.go b/_test.go new file mode 100644 index 0000000000000000000000000000000000000000..304d5802f7341eb71fdcb412d80c13933c015a49 --- /dev/null +++ b/_test.go @@ -0,0 +1,12 @@ +package main +import ( + "gitlab.utc.fr/gvandevi/ia04/comsoc" + // "testing" +) + +// func TestSum1(t * Testing.T){ +// res:=Sum(0,1) +// if res !=1 { +// t.Errorf("mauvaise somme!") +// } +// } \ No newline at end of file diff --git a/comsoc/Approval.go b/comsoc/Approval.go new file mode 100644 index 0000000000000000000000000000000000000000..e1b7639e6857b9af90e2355236a4721cc9bef147 --- /dev/null +++ b/comsoc/Approval.go @@ -0,0 +1,5 @@ +package comsoc +import"fmt" +func Test1() { + fmt.Println("Hello, World!") +} \ No newline at end of file diff --git a/comsoc/Borda.go b/comsoc/Borda.go new file mode 100644 index 0000000000000000000000000000000000000000..7076a9b4de20578f2bfe7749ab88012274203de2 --- /dev/null +++ b/comsoc/Borda.go @@ -0,0 +1 @@ +package comsoc \ No newline at end of file diff --git a/comsoc/CondorcetWinner.go b/comsoc/CondorcetWinner.go new file mode 100644 index 0000000000000000000000000000000000000000..7076a9b4de20578f2bfe7749ab88012274203de2 --- /dev/null +++ b/comsoc/CondorcetWinner.go @@ -0,0 +1 @@ +package comsoc \ No newline at end of file diff --git a/comsoc/Copeland.go b/comsoc/Copeland.go new file mode 100644 index 0000000000000000000000000000000000000000..7076a9b4de20578f2bfe7749ab88012274203de2 --- /dev/null +++ b/comsoc/Copeland.go @@ -0,0 +1 @@ +package comsoc \ No newline at end of file diff --git a/comsoc/HelpFunctions.go b/comsoc/HelpFunctions.go new file mode 100644 index 0000000000000000000000000000000000000000..b7b90d8de9a9f61de28e278e6fecaf150f75c8c2 --- /dev/null +++ b/comsoc/HelpFunctions.go @@ -0,0 +1,111 @@ +package comsoc +import ( + "fmt" + "errors" +) + +//cours TODO +func plusN (n int) (func (i int)(int)){ + f:=func(i int) int { + return (i+n) + } + return f +} +// renvoie l'indice ou se trouve alt dans prefs +func rank(alt Alternative, prefs []Alternative) int { + for i := 0; i < len(prefs);i++{ + if prefs[i]==alt{ + return i + } + } + return -1 + } +func Test_rank(){ + tableau := []Alternative{1, 2, 3, 4, 5} + indice := rank(tableau[3],tableau) + fmt.Print(indice) +} + +// renvoie vrai ssi alt1 est préférée à alt2 +func isPref(alt1, alt2 Alternative, prefs []Alternative) bool { + // if (alt1 <0 || alt2 <0){ + // return errors.New("l'une des deux valeurs n'est pas présente dans le tableau") + // }else { + return rank(alt1,prefs)<rank(alt2,prefs) + } +// } +func Test_isPref(){ + tableau := []Alternative{1, 2, 3, 4, 5} + alt1 := Alternative(1) + alt2 := Alternative(2) + response := isPref(alt1, alt2, tableau) + fmt.Print(response) +} + +// renvoie les meilleures alternatives pour un décompte donné +func maxCount(count Count) (bestAlts []Alternative) { + maxPoints:=-1 + for i, points := range count{ + if points > maxPoints { + bestAlts = []Alternative{Alternative(i)} + maxPoints = points + }else if points == maxPoints{ + bestAlts=append(bestAlts,Alternative(i)) + } + } + return bestAlts +} +func Test_maxCount(){ + count := make(map[Alternative]int) + count[Alternative(1)] = 3 + count[Alternative(2)] = 5 + count[Alternative(3)] = 5 + fmt.Println(maxCount(count)) +} + +// // 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 +func checkProfile(prefs []Alternative, alts []Alternative) error { + //vérifier + if (len (prefs) < len(alts)){ + return errors.New("Il manque des alternatives") + }else if(len (prefs)>len(alts)){ + return errors.New("Il y a des alternatives en trop.") + }else{//vérifier complet + for _, element := range alts{ + if rank(element, prefs) == -1 { + return errors.New("au moins une alternative est absente des préférences") + } + } +} +return nil +} + +func Test_checkProfile(){ + alts := []Alternative{1, 2, 3, 4, 5} + //prefs := []Alternative{3, 1, 5, 2, 4} + //prefs := []Alternative{1, 2, 3, 4, 5,6} + prefs := []Alternative{1, 2, 3, 4, 1} + fmt.Println(checkProfile(prefs, alts)) +} + +// 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 +func checkProfileAlternative(prefs Profile, alts []Alternative) error { + for _, row := range prefs{ + e:=checkProfile(row, alts) + if e != nil{ + return e + } + } +return nil +} + +func Test_checkProfileAlternative(){ + alts := []Alternative{1, 2, 3, 4, 5} + pref1 := []Alternative{5, 2, 1, 4, 3} + pref2 := []Alternative{1, 5, 4, 3, 2} + Pref := [][]Alternative{pref1,pref2} + profil := Profile(Pref) + fmt.Println(checkProfileAlternative(profil, alts)) +} \ No newline at end of file diff --git a/comsoc/Majority.go b/comsoc/Majority.go new file mode 100644 index 0000000000000000000000000000000000000000..93b492c81d055e5d84a2de05fc0f4f0c558a561b --- /dev/null +++ b/comsoc/Majority.go @@ -0,0 +1,17 @@ +package comsoc + +//majorité simple +// fonctions de bien-être social (social welfare function, SWF) : +// retournent un décompte à partir d'un profil +func MajoritySWF(p Profile) (count Count, err error){ + //1) checkProfileAlternative(prefs Profile, alts []Alternative) + count := make(map[Alternative]int) + for _, row := range p{ + count[row[0]]+=1 + } + return count +} + +// fonctions de choix social (choix social, social choice function, SCF) +// renvoient uniquement les alternatives préférées. +func MajoritySCF(p Profile) (bestAlts []Alternative, err error){} diff --git a/comsoc/STV.go b/comsoc/STV.go new file mode 100644 index 0000000000000000000000000000000000000000..7076a9b4de20578f2bfe7749ab88012274203de2 --- /dev/null +++ b/comsoc/STV.go @@ -0,0 +1 @@ +package comsoc \ No newline at end of file diff --git a/comsoc/Type.go b/comsoc/Type.go new file mode 100644 index 0000000000000000000000000000000000000000..3c094d57099e752b0e062f2b5612bccc5d000cc7 --- /dev/null +++ b/comsoc/Type.go @@ -0,0 +1,5 @@ +package comsoc + +type Alternative int +type Profile [][]Alternative +type Count map[Alternative]int \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..15c098f84fb864449b9f76723462ccd7fd89d2a9 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitlab.utc.fr/gvandevi/ia04 + +go 1.21.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000000000000000000000000000000000000..3d7eb072fe13e74327fba0209bb1ab9f02edceec --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main +import ( + "gitlab.utc.fr/gvandevi/ia04/comsoc" +) + +func main(){ + //Fonctions utilitaires + //comsoc.Test_rank() + //comsoc.Test_maxCount() + //comsoc.Test_checkProfile() + comsoc.Test_checkProfileAlternative() +} +