Finish social-preference and social-choice functions for borda method.

main
Edward L Platt 2 years ago
parent 5444c0654e
commit 30a1496f31

@ -1,6 +1,6 @@
(define-module (deliberate borda)
#:use-module (deliberate utils)
#:export (borda-score))
#:export (borda-score social-choice-borda social-preference-borda))
(define (borda-score alternatives ranked)
;; Check whether all alternatives have been scored
@ -18,3 +18,30 @@
(cons (cons alt (length remaining))
(borda-score remaining (cdr ranked))))
)))
(define (profile->scores alternatives profile)
(apply append
(map (lambda (pair)
(borda-score alternatives (cdr pair)))
profile)))
(define (groups->totals groups)
(map (lambda (elt)
(cons (car elt)
(reduce + 0 (map cdr (cdr elt)))))
groups))
(define (profile-borda-totals alternatives profile)
(let* ((scores (profile->scores alternatives profile))
(groups (group-by car scores))
(totals (groups->totals groups)))
totals))
;; TODO - handle ties
(define (social-preference-borda alternatives profile)
(let ((totals (profile-borda-totals alternatives profile)))
;; We use > instead of < for to rank in decreasing score order
(map (compose list car) (sort totals (lambda (x y) (> (cdr x) (cdr y)))))))
(define (social-choice-borda alternatives profile)
(car (social-preference-borda alternatives profile)))

@ -13,12 +13,6 @@
(borda-score test-alts test-pref)
(define (profile-borda-scores alternatives profile)
(apply append
(map (lambda (pair)
(borda-score alternatives (cdr pair)))
profile)))
(social-preference-borda test-alts test-profile)
(define test-scores (profile-borda-scores test-alts test-profile))
(define test-score-groups (group-by car test-scores))
(social-choice-borda test-alts test-profile)

Loading…
Cancel
Save