Example #1
0
//Prompt the user for a file, create a voter preference Map, and print it.
//Determine the Set of all the candidates in the election, from this Map.
//Repeatedly evaluate the ballot based on the candidates (still) in the
//  election, printing the vote count (tally) two ways: with the candidates
//  (a) shown alphabetically increasing and (b) shown with the vote count
//  decreasing (candidates with equal vote counts are shown alphabetically
//  increasing); from this tally, compute which candidates remain in the
//  election: all candidates receiving more than the minimum number of votes;
//  continue this process until there are less than 2 candidates.
//Print the final result: there may 1 candidate left (the winner) or 0 left
//   (no winner).
int main() {
  try {
	  std::ifstream text_file;
	  ics::safe_open(text_file,"Enter the name of a file with voter preferences","votepref1.txt");
	  std::cout << std::endl;

	  const Preferences prefs = read_voter_preferences(text_file);
	  print_voter_preferences(prefs);
	  std::cout << std::endl;

	  CandidateSet candidateSet;
	  for (const auto& kv : prefs) {
		  for (const std::string& candidate : kv.second) {
			  candidateSet.insert(candidate);
		  }
	  }

	  CandidateTally tally = evaluate_ballot(prefs, candidateSet);
	  int ballot = 1;

	  while (candidateSet.size() > 1) {
		  std::stringstream ss;
		  ss << ballot++;
		  print_tally("Vote count on ballot #" + ss.str() +
				  " with candidates in alphabetical order", tally, alphabetic_gt);
		  print_tally("Vote count on ballot #" + ss.str() +
				  " with candidates in numerical order", tally, numeric_gt);

		  candidateSet = remaining_candidates(tally);
		  tally = evaluate_ballot(prefs, candidateSet);
	  }

	  if (candidateSet.size() == 0) {
		  std::cout << "No winner" << std::endl;
	  } else {
		  std::string winner;
		  for (const std::string& candidate : candidateSet) {
			  winner += candidate + " and ";
		  }
		  std::cout << "Winner is " << winner.substr(0, winner.length()-5) << std::endl;
	  }

  } catch (ics::IcsError& e) {
    std::cout << e.what() << std::endl;
  }
  return 0;
}
Example #2
0
File: model.C Project: google/refr
void
RandomPairCandidateSetScorer::Score(Model *model,
                                    CandidateSet &candidates, bool training) {
  // First, pick two candidate indices at random.
  size_t idx1 = GetRandomIndex(candidates.size());
  size_t idx2 = GetRandomIndex(candidates.size());
  Candidate &c1 = candidates.Get(idx1);
  Candidate &c2 = candidates.Get(idx2);

  // Next, just score those two candidates.
  model->ScoreCandidate(c1, training);
  model->ScoreCandidate(c2, training);

  // Finally, set indices of best scoring and gold amongst just those two.
  int score_cmp = model->score_comparator()->Compare(*model, c1, c2);
  candidates.set_best_scoring_index(score_cmp > 0 ? c1.index() : c2.index());

  int gold_cmp = model->gold_comparator()->Compare(*model, c1, c2);
  candidates.set_gold_index(gold_cmp > 0 ? c1.index() : c2.index());
}