Source: compute/voteCasters/score/castScoreGrid.js

  1. /** @module */
  2. import castScorePoint from './castScorePoint.js'
  3. import makeGrid1D from '../voteCasters/makeGrid1D.js'
  4. import makeGrid2D from '../voteCasters/makeGrid2D.js'
  5. /**
  6. * Tally votes.
  7. */
  8. export default function castScoreGrid(voterGeom, geometry, castOptions, strategyRngs, voterStrategy) {
  9. const { canPoints, dimensions, information } = geometry
  10. const { verbosity } = castOptions
  11. // just find the vote and count at each grid point
  12. const makeGrid = (dimensions === 1) ? makeGrid1D : makeGrid2D
  13. const grid = makeGrid(voterGeom, castOptions)
  14. const { voteCounts, totalVotes, voterPoints } = grid
  15. const n = canPoints.length
  16. const scoreSumByCan = Array(n).fill(0)
  17. // find vote
  18. const gridLength = grid.x.length
  19. const voteSet = Array(gridLength)
  20. for (let i = 0; i < gridLength; i++) {
  21. const voteCount = voteCounts[i]
  22. const voterPoint = voterPoints[i]
  23. const vote = castScorePoint(canPoints, voterPoint, dimensions, verbosity, information, voterStrategy, strategyRngs)
  24. voteSet[i] = vote
  25. const { scoreVote } = vote
  26. for (let k = 0; k < n; k++) {
  27. scoreSumByCan[k] += scoreVote[k] * voteCount
  28. }
  29. }
  30. if (verbosity < 2) {
  31. return { scoreSumByCan, totalVotes }
  32. }
  33. const votesForGeom = { grid, voteSet, scoreSumByCan, totalVotes, voterGeom }
  34. return votesForGeom
  35. }