# HG changeset patch # User jbe # Date 1363508919 -3600 # Node ID 4698b68e0942396ff74b7fd779bd50c101fd0ce8 # Parent accffcefeff7c2dec91a7188fb93c215e5039359 Calculation of ranks in "lf_update_suggestion_order" diff -r accffcefeff7 -r 4698b68e0942 lf_update_suggestion_order.c --- a/lf_update_suggestion_order.c Sat Mar 16 18:29:55 2013 +0100 +++ b/lf_update_suggestion_order.c Sun Mar 17 09:28:39 2013 +0100 @@ -31,6 +31,8 @@ struct candidate { char *key; + double score_per_step; + int reaches_score; double score; int seat; }; @@ -47,7 +49,6 @@ struct candidate *candidate; candidate = candidates + (candidate_count++); candidate->key = *candidate_key; - candidate->score = 0.0; candidate->seat = 0; } } @@ -58,7 +59,7 @@ compare.key = candidate_key; candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate); if (!candidate) { - fprintf(stderr, "Candidate not found (should not happen)\n"); + fprintf(stderr, "Candidate not found (should not happen).\n"); abort(); } return candidate; @@ -74,10 +75,77 @@ struct ballot_section sections[4]; }; +static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) { + int i, j, k; + int remaining; + for (i=0; iscore < 1.0 && !candidate->seat) matches++; + } + if (matches) { + double score_inc; + score_inc = 1.0 / (double)matches; + for (k=0; kscore < 1.0 && !candidate->seat) { + candidate->score_per_step += score_inc; + } + } + break; + } + } + } + scale = (double)candidate_count; + for (i=0; i 0.0) { + max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step; + if (max_scale <= scale) { + scale = max_scale; + candidates[i].reaches_score = 1; + } + } + } + for (i=0; i 0.0) { + if (candidates[i].reaches_score) { + candidates[i].score = 1.0; + remaining--; + } else { + candidates[i].score += scale * candidates[i].score_per_step; + if (candidates[i].score >= 1.0) remaining--; + } + if (remaining <= 1) break; + } + } + } + for (i=candidate_count-1; i>=0; i--) { + if (candidates[i].score < 1.0 && !candidates[i].seat) return candidates+i; + } + fprintf(stderr, "No remaining candidate (should not happen)."); + abort(); +} + static void process_initiative(PGresult *res) { int ballot_count = 0; + struct ballot *ballots; int i; - struct ballot *ballots; { void *candidate_tree = NULL; int tuple_count; @@ -94,7 +162,7 @@ if (!candidate_tree || !tfind(suggestion_id, &candidate_tree, (void *)strcmp)) { candidate_count++; if (!tsearch(suggestion_id, &candidate_tree, (void *)strcmp)) { - fprintf(stderr, "Insufficient memory\n"); + fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n"); abort(); } } @@ -107,7 +175,7 @@ printf("Candidate count: %i\n", candidate_count); candidates = malloc(candidate_count * sizeof(struct candidate)); if (!candidates) { - fprintf(stderr, "Insufficient memory\n"); + fprintf(stderr, "Insufficient memory while creating candidate list.\n"); abort(); } candidate_count = 0; @@ -116,7 +184,7 @@ printf("Ballot count: %i\n", ballot_count); ballots = calloc(ballot_count, sizeof(struct ballot)); if (!ballots) { - fprintf(stderr, "Insufficient memory\n"); + fprintf(stderr, "Insufficient memory while creating ballot list.\n"); abort(); } ballot = ballots; @@ -127,12 +195,12 @@ suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID); weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10); if (weight <= 0) { - fprintf(stderr, "Unexpected weight value\n"); + fprintf(stderr, "Unexpected weight value.\n"); abort(); } preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10); if (preference < 1 || preference > 4) { - fprintf(stderr, "Unexpected preference value\n"); + fprintf(stderr, "Unexpected preference value.\n"); abort(); } preference--; @@ -147,7 +215,7 @@ if (ballots[i].sections[j].count) { ballots[i].sections[j].candidates = malloc(ballots[i].sections[j].count * sizeof(struct candidate *)); if (!ballots[i].sections[j].candidates) { - fprintf(stderr, "Insufficient memory\n"); + fprintf(stderr, "Insufficient memory while creating ballot section.\n"); abort(); } } @@ -161,10 +229,6 @@ member_id = PQgetvalue(res, i, COL_MEMBER_ID); suggestion_id = PQgetvalue(res, i, COL_SUGGESTION_ID); preference = (int)strtol(PQgetvalue(res, i, COL_PREFERENCE), (char **)NULL, 10); - if (preference < 1 || preference > 4) { - fprintf(stderr, "Unexpected preference value\n"); - abort(); - } preference--; ballot->sections[preference].candidates[candidates_in_sections[preference]++] = candidate_by_key(suggestion_id); } @@ -179,6 +243,11 @@ } } + for (i=0; iseat = candidate_count - i; + } + free(candidates); for (i=0; i