Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • fallmoha/IA04_TP3
1 result
Show changes
Commits on Source (2)
......@@ -112,7 +112,7 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
}
log.Println("NEW BALLOT: Number of alternatives: ", req.Alts)
if rba.rule != "majority" && rba.rule != "borda" && rba.rule != "copeland" && rba.rule != "condorcet" && rba.rule != "approval" {
if rba.rule != "majority" && rba.rule != "borda" && rba.rule != "copeland" && rba.rule != "condorcet" && rba.rule != "approval" && rba.rule != "scoring" {
w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("501: vote rule \"'%s'\" is not impemented in the system", req.Rule)
w.Write([]byte(msg))
......@@ -145,6 +145,9 @@ func (rba *RestBallotAgent) doNewBallot(w http.ResponseWriter, r *http.Request)
bestAlts, err = comsoc.MajoritySCF(rba.pfl)
case "approval":
rba.count, err = comsoc.ApprovalSWF(rba.pfl, rba.options)
case "scoring":
rba.count, err = comsoc.ScoringSWF(rba.pfl, rba.options)
bestAlts, err = comsoc.ScoringSCF(rba.pfl, rba.options)
}
// the tie-break is in the order of rba.alts
tb := comsoc.TieBreakFactory(rba.alts)
......@@ -217,7 +220,7 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
// Process options case
if (len(req.Options) > 0 && rba.rule != "approval") || (len(req.Options) > 1 && rba.rule == "approval") { // modify conditions according to options implemented
if (len(req.Options) > 0 && rba.rule != "approval" && rba.rule != "scoring") || (len(req.Options) > 1 && rba.rule == "approval") { // modify conditions according to options implemented
log.Println("VOTE: Error in given options.")
w.WriteHeader(http.StatusNotImplemented)
msg := fmt.Sprintf("501: Approval threshold only accept threshold for approval (in a list, example: 'options: [3]' in JSON).")
......@@ -226,6 +229,8 @@ func (rba *RestBallotAgent) doVote(w http.ResponseWriter, r *http.Request) {
}
if rba.rule == "approval" {
rba.options = append(rba.options, int(req.Options[0]))
} else if rba.rule == "scoring" {
rba.options = req.Options
}
log.Println(rba.pfl)
......
......@@ -20,6 +20,14 @@ func shuffle(n int, pref []comsoc.Alternative) []comsoc.Alternative {
return pref
}
func makeVector(n int) []int {
vec := make([]int, n)
for i, _ := range vec {
vec[i] = 20 + i
}
return vec
}
func main() {
const url1 = ":8080"
const url2 = "http://localhost:8080"
......@@ -37,7 +45,8 @@ func main() {
fmt.Println("1: Majority")
fmt.Println("2: Copeland")
fmt.Println("3: Approval")
fmt.Println("4: Randomly choose between all the latters")
fmt.Println("4: Scoring (default vector is <20,21,22,23,...>)")
fmt.Println("5: Randomly choose between all the latters")
fmt.Println("Enter a number: ")
fmt.Scanln(&choice)
......@@ -53,7 +62,9 @@ func main() {
fmt.Println("Enter an integer number for the threshold : ")
fmt.Scanln(&ts)
case 4:
rules := []string{"borda", "approval", "copeland", "majority"}
rule = "scoring"
case 5:
rules := []string{"borda", "approval", "copeland", "majority", "scoring"}
rule = rules[rand.Intn(4)]
default:
fmt.Println("Incorrect choice...")
......@@ -87,13 +98,16 @@ func main() {
options = append(options, ts)
}
if rule == "scoring" {
options = makeVector(n)
}
deadline := time.Now().Add(time.Second * 10)
server := agt.NewRestBallotAgent(":8080")
//log.Println("MAIN: Server starting...")
go server.Start()
//log.Println("MAIN: Starting voter agents...")
voterAgents := make([]agt2.RestVoterAgent, 0, m)
for i := 0; i < n; i++ {
id := i
......
......@@ -180,3 +180,50 @@ func TestCondorcetWinner(t *testing.T) {
t.Errorf("no best alternative for prefs2")
}
}
func TestScoringSWF(t *testing.T) {
prefs := [][]Alternative{
{3, 1, 2, 4},
{3, 1, 2, 4},
{3, 4, 2, 1},
{4, 3, 2, 1},
{1, 4, 3, 2},
}
scores := []int{20, 21, 22, 23}
res, _ := ScoringSWF(prefs, scores)
if res[1] != 107 {
t.Errorf("error, result for 1 should be 107, %d computed", res[1])
}
if res[2] != 104 {
t.Errorf("error, result for 2 should be 104, %d computed", res[2])
}
if res[3] != 112 {
t.Errorf("error, result for 3 should be 112, %d computed", res[3])
}
if res[4] != 107 {
t.Errorf("error, result for 3 should be 107, %d computed", res[3])
}
}
func TestScoringSCF(t *testing.T) {
prefs := [][]Alternative{
{3, 1, 2, 4},
{3, 1, 2, 4},
{3, 4, 2, 1},
{4, 3, 2, 1},
{1, 4, 3, 2},
}
scores := []int{20, 21, 22, 23}
res, err := ScoringSCF(prefs, scores)
if err != nil {
t.Error(err)
}
if len(res) != 1 || res[0] != 3 {
t.Errorf("error, 1 should be the only best Alternative")
}
}
package comsoc
func ScoringSWF(p Profile, scores []int) (count Count, err error) {
count = make(map[Alternative]int)
for _, profile := range p {
for i, alt := range profile {
count[alt] += scores[len(profile)-1-i]
}
}
return count, nil
}
func ScoringSCF(p Profile, scores []int) (bestAlts []Alternative, err error) {
count, erreur := ScoringSWF(p, scores)
return maxCount(count), erreur
}