From 87aacc97883cefc606743d4561706131008ff83f Mon Sep 17 00:00:00 2001
From: Gabrielle van de Vijver <gabrielle.van-de-vijver@etu.utc.fr>
Date: Sun, 1 Oct 2023 16:55:29 +0200
Subject: [PATCH] =?UTF-8?q?fonctions=20utilitaires=20d=C3=A9velopp=C3=A9es?=
 =?UTF-8?q?=20en=20TD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 _test.go                  |  12 +++++
 comsoc/Approval.go        |   5 ++
 comsoc/Borda.go           |   1 +
 comsoc/CondorcetWinner.go |   1 +
 comsoc/Copeland.go        |   1 +
 comsoc/HelpFunctions.go   | 111 ++++++++++++++++++++++++++++++++++++++
 comsoc/Majority.go        |  17 ++++++
 comsoc/STV.go             |   1 +
 comsoc/Type.go            |   5 ++
 go.mod                    |   3 ++
 main.go                   |  13 +++++
 11 files changed, 170 insertions(+)
 create mode 100644 _test.go
 create mode 100644 comsoc/Approval.go
 create mode 100644 comsoc/Borda.go
 create mode 100644 comsoc/CondorcetWinner.go
 create mode 100644 comsoc/Copeland.go
 create mode 100644 comsoc/HelpFunctions.go
 create mode 100644 comsoc/Majority.go
 create mode 100644 comsoc/STV.go
 create mode 100644 comsoc/Type.go
 create mode 100644 go.mod
 create mode 100644 main.go

diff --git a/_test.go b/_test.go
new file mode 100644
index 0000000..304d580
--- /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 0000000..e1b7639
--- /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 0000000..7076a9b
--- /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 0000000..7076a9b
--- /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 0000000..7076a9b
--- /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 0000000..b7b90d8
--- /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 0000000..93b492c
--- /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 0000000..7076a9b
--- /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 0000000..3c094d5
--- /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 0000000..15c098f
--- /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 0000000..3d7eb07
--- /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()
+}
+
-- 
GitLab