liquid_feedback_core

view lf_update_issue_order.c @ 394:326dc0c3b859

Calculation of "order_in_admission_state"
author jbe
date Fri Oct 11 12:57:03 2013 +0200 (2013-10-11)
parents
children d93428e4edad
line source
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <libpq-fe.h>
5 #include <search.h>
7 static int logging = 0;
9 static char *escapeLiteral(PGconn *conn, const char *str, size_t len) {
10 // provides compatibility for PostgreSQL versions prior 9.0
11 // in future: return PQescapeLiteral(conn, str, len);
12 char *res;
13 size_t res_len;
14 res = malloc(2*len+3);
15 if (!res) return NULL;
16 res[0] = '\'';
17 res_len = PQescapeStringConn(conn, res+1, str, len, NULL);
18 res[res_len+1] = '\'';
19 res[res_len+2] = 0;
20 return res;
21 }
23 static void freemem(void *ptr) {
24 // to be used for "escapeLiteral" function
25 // provides compatibility for PostgreSQL versions prior 9.0
26 // in future: PQfreemem(ptr);
27 free(ptr);
28 }
30 // column numbers when querying "issue_supporter_in_admission_state" view in function main():
31 #define COL_MEMBER_ID 0
32 #define COL_WEIGHT 1
33 #define COL_ISSUE_ID 2
35 // data structure for a candidate (in this case a suggestion) to the proportional runoff system:
36 struct candidate {
37 char *key; // identifier of the candidate, which is the "suggestion_id" string
38 double score_per_step; // added score per step
39 double score; // current score of candidate; a score of 1.0 is needed to survive a round
40 int seat; // equals 0 for unseated candidates, or contains rank number
41 };
43 // compare two integers stored as strings (invocation like strcmp):
44 static int compare_id(char *id1, char *id2) {
45 int ldiff;
46 ldiff = strlen(id1) - strlen(id2);
47 if (ldiff) return ldiff;
48 else return strcmp(id1, id2);
49 }
51 // compare two candidates by their key (invocation like strcmp):
52 static int compare_candidate(struct candidate *c1, struct candidate *c2) {
53 return compare_id(c1->key, c2->key);
54 }
56 // candidates are stored as global variables due to the constrained twalk() interface:
57 static int candidate_count;
58 static struct candidate *candidates;
60 // function to be passed to twalk() to store candidates ordered in candidates[] array:
61 static void register_candidate(char **candidate_key, VISIT visit, int level) {
62 if (visit == postorder || visit == leaf) {
63 struct candidate *candidate;
64 candidate = candidates + (candidate_count++);
65 candidate->key = *candidate_key;
66 candidate->seat = 0;
67 if (logging) printf("Candidate #%i is suggestion #%s.\n", candidate_count, candidate->key);
68 }
69 }
71 // performs a binary search in candidates[] array to lookup a candidate by its key (which is the suggestion_id):
72 static struct candidate *candidate_by_key(char *candidate_key) {
73 struct candidate *candidate;
74 struct candidate compare;
75 compare.key = candidate_key;
76 candidate = bsearch(&compare, candidates, candidate_count, sizeof(struct candidate), (void *)compare_candidate);
77 if (!candidate) {
78 fprintf(stderr, "Candidate not found (should not happen).\n");
79 abort();
80 }
81 return candidate;
82 }
84 // ballot of the proportional runoff system:
85 struct ballot {
86 int weight; // if weight is greater than 1, then the ballot is counted multiple times
87 int count;
88 struct candidate **candidates;
89 };
91 // determine candidate, which is assigned the next seat (starting with the worst rank):
92 static struct candidate *loser(int round_number, struct ballot *ballots, int ballot_count) {
93 int i, j; // index variables for loops
94 int remaining; // remaining candidates to be seated
95 // reset scores of all candidates:
96 for (i=0; i<candidate_count; i++) {
97 candidates[i].score = 0.0;
98 }
99 // calculate remaining candidates to be seated:
100 remaining = candidate_count - round_number;
101 // repeat following loop, as long as there is more than one remaining candidate:
102 while (remaining > 1) {
103 if (logging) printf("There are %i remaining candidates.\n", remaining);
104 double scale; // factor to be later multiplied with score_per_step:
105 // reset score_per_step for all candidates:
106 for (i=0; i<candidate_count; i++) {
107 candidates[i].score_per_step = 0.0;
108 }
109 // calculate score_per_step for all candidates:
110 for (i=0; i<ballot_count; i++) {
111 int matches = 0;
112 for (j=0; j<ballots[i].count; j++) {
113 struct candidate *candidate;
114 candidate = ballots[i].candidates[j];
115 if (candidate->score < 1.0 && !candidate->seat) matches++;
116 }
117 if (matches) {
118 double score_inc;
119 score_inc = (double)ballots[i].weight / (double)matches;
120 for (j=0; j<ballots[i].count; j++) {
121 struct candidate *candidate;
122 candidate = ballots[i].candidates[j];
123 if (candidate->score < 1.0 && !candidate->seat) {
124 candidate->score_per_step += score_inc;
125 }
126 }
127 }
128 }
129 // calculate scale factor:
130 scale = (double)0.0; // 0.0 is used to indicate that there is no value yet
131 for (i=0; i<candidate_count; i++) {
132 double max_scale;
133 if (candidates[i].score_per_step > 0.0) {
134 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
135 if (scale == 0.0 || max_scale <= scale) {
136 scale = max_scale;
137 }
138 }
139 }
140 // add scale*score_per_step to each candidates score:
141 for (i=0; i<candidate_count; i++) {
142 int log_candidate = 0;
143 if (logging && candidates[i].score < 1.0 && !candidates[i].seat) log_candidate = 1;
144 if (log_candidate) printf("Score for suggestion #%s = %.4f+%.4f*%.4f", candidates[i].key, candidates[i].score, scale, candidates[i].score_per_step);
145 if (candidates[i].score_per_step > 0.0) {
146 double max_scale;
147 max_scale = (1.0-candidates[i].score) / candidates[i].score_per_step;
148 if (max_scale == scale) {
149 // score of 1.0 should be reached, so we set score directly to avoid floating point errors:
150 candidates[i].score = 1.0;
151 remaining--;
152 } else {
153 candidates[i].score += scale * candidates[i].score_per_step;
154 if (candidates[i].score >= 1.0) remaining--;
155 }
156 }
157 if (log_candidate) {
158 if (candidates[i].score >= 1.0) printf("=1\n");
159 else printf("=%.4f\n", candidates[i].score);
160 }
161 // when there is only one candidate remaining, then break inner (and thus outer) loop:
162 if (remaining <= 1) {
163 break;
164 }
165 }
166 }
167 // return remaining candidate:
168 for (i=0; i<candidate_count; i++) {
169 if (candidates[i].score < 1.0 && !candidates[i].seat) return candidates+i;
170 }
171 // if there is no remaining candidate, then something went wrong:
172 fprintf(stderr, "No remaining candidate (should not happen).");
173 abort();
174 }
176 // write results to database:
177 static int write_ranks(PGconn *db, char *escaped_area_id) {
178 PGresult *res;
179 char *cmd;
180 int i;
181 if (asprintf(&cmd, "BEGIN; UPDATE \"issue\" SET \"order_in_admission_state\" = NULL WHERE \"area_id\" = %s AND (\"state\" = 'admission' OR \"order_in_admission_state\" NOTNULL)", escaped_area_id) < 0) {
182 fprintf(stderr, "Could not prepare query string in memory.\n");
183 abort();
184 }
185 res = PQexec(db, cmd);
186 free(cmd);
187 if (!res) {
188 fprintf(stderr, "Error in pqlib while sending SQL command to initiate issue update.\n");
189 return 1;
190 } else if (
191 PQresultStatus(res) != PGRES_COMMAND_OK &&
192 PQresultStatus(res) != PGRES_TUPLES_OK
193 ) {
194 fprintf(stderr, "Error while executing SQL command to initiate issue update:\n%s", PQresultErrorMessage(res));
195 PQclear(res);
196 return 1;
197 } else {
198 PQclear(res);
199 }
200 for (i=0; i<candidate_count; i++) {
201 char *escaped_issue_id;
202 escaped_issue_id = escapeLiteral(db, candidates[i].key, strlen(candidates[i].key));
203 if (!escaped_issue_id) {
204 fprintf(stderr, "Could not escape literal in memory.\n");
205 abort();
206 }
207 if (asprintf(&cmd, "UPDATE \"issue\" SET \"order_in_admission_state\" = %i WHERE \"id\" = %s", candidates[i].seat, escaped_issue_id) < 0) {
208 fprintf(stderr, "Could not prepare query string in memory.\n");
209 abort();
210 }
211 freemem(escaped_issue_id);
212 res = PQexec(db, cmd);
213 free(cmd);
214 if (!res) {
215 fprintf(stderr, "Error in pqlib while sending SQL command to update issue order.\n");
216 } else if (
217 PQresultStatus(res) != PGRES_COMMAND_OK &&
218 PQresultStatus(res) != PGRES_TUPLES_OK
219 ) {
220 fprintf(stderr, "Error while executing SQL command to update issue order:\n%s", PQresultErrorMessage(res));
221 PQclear(res);
222 } else {
223 PQclear(res);
224 continue;
225 }
226 res = PQexec(db, "ROLLBACK");
227 if (res) PQclear(res);
228 return 1;
229 }
230 res = PQexec(db, "COMMIT");
231 if (!res) {
232 fprintf(stderr, "Error in pqlib while sending SQL command to commit transaction.\n");
233 return 1;
234 } else if (
235 PQresultStatus(res) != PGRES_COMMAND_OK &&
236 PQresultStatus(res) != PGRES_TUPLES_OK
237 ) {
238 fprintf(stderr, "Error while executing SQL command to commit transaction:\n%s", PQresultErrorMessage(res));
239 PQclear(res);
240 return 1;
241 } else {
242 PQclear(res);
243 return 0;
244 }
245 }
247 // calculate ordering of issues in admission state for an area and call write_ranks() to write it to database:
248 static int process_area(PGconn *db, PGresult *res, char *escaped_area_id) {
249 int err; // variable to store an error condition (0 = success)
250 int ballot_count = 1; // number of ballots, must be initiatized to 1, due to loop below
251 struct ballot *ballots; // data structure containing the ballots
252 int i; // index variable for loops
253 // create candidates[] and ballots[] arrays:
254 {
255 void *candidate_tree = NULL; // temporary structure to create a sorted unique list of all candidate keys
256 int tuple_count; // number of tuples returned from the database
257 char *old_member_id = NULL; // old member_id to be able to detect a new ballot in loops
258 struct ballot *ballot; // pointer to current ballot
259 int candidates_in_ballot = 0; // number of candidates in ballot
260 // reset candidate count:
261 candidate_count = 0;
262 // determine number of tuples:
263 tuple_count = PQntuples(res);
264 // trivial case, when there are no tuples:
265 if (!tuple_count) {
266 if (logging) printf("Nothing to do.\n");
267 return 0;
268 }
269 // calculate ballot_count and generate set of candidate keys (suggestion_id is used as key):
270 for (i=0; i<tuple_count; i++) {
271 char *member_id, *issue_id;
272 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
273 issue_id = PQgetvalue(res, i, COL_ISSUE_ID);
274 if (!candidate_tree || !tfind(issue_id, &candidate_tree, (void *)compare_id)) {
275 candidate_count++;
276 if (!tsearch(issue_id, &candidate_tree, (void *)compare_id)) {
277 fprintf(stderr, "Insufficient memory while inserting into candidate tree.\n");
278 abort();
279 }
280 }
281 if (old_member_id && strcmp(old_member_id, member_id)) ballot_count++;
282 old_member_id = member_id;
283 }
284 // allocate memory for candidates[] array:
285 candidates = malloc(candidate_count * sizeof(struct candidate));
286 if (!candidates) {
287 fprintf(stderr, "Insufficient memory while creating candidate list.\n");
288 abort();
289 }
290 // transform tree of candidate keys into sorted array:
291 candidate_count = 0; // needed by register_candidate()
292 twalk(candidate_tree, (void *)register_candidate);
293 // free memory of tree structure (tdestroy() is not available on all platforms):
294 while (candidate_tree) tdelete(*(void **)candidate_tree, &candidate_tree, (void *)compare_id);
295 // allocate memory for ballots[] array:
296 ballots = calloc(ballot_count, sizeof(struct ballot));
297 if (!ballots) {
298 fprintf(stderr, "Insufficient memory while creating ballot list.\n");
299 abort();
300 }
301 // set ballot weights, determine ballot section sizes, and verify preference values:
302 ballot = ballots;
303 old_member_id = NULL;
304 for (i=0; i<tuple_count; i++) {
305 char *member_id;
306 int weight;
307 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
308 weight = (int)strtol(PQgetvalue(res, i, COL_WEIGHT), (char **)NULL, 10);
309 if (weight <= 0) {
310 fprintf(stderr, "Unexpected weight value.\n");
311 free(ballots);
312 free(candidates);
313 return 1;
314 }
315 if (old_member_id && strcmp(old_member_id, member_id)) ballot++;
316 ballot->weight = weight;
317 ballot->count++;
318 old_member_id = member_id;
319 }
320 // allocate memory for ballot sections:
321 for (i=0; i<ballot_count; i++) {
322 if (ballots[i].count) {
323 ballots[i].candidates = malloc(ballots[i].count * sizeof(struct candidate *));
324 if (!ballots[i].candidates) {
325 fprintf(stderr, "Insufficient memory while creating ballot section.\n");
326 abort();
327 }
328 }
329 }
330 // fill ballot sections with candidate references:
331 old_member_id = NULL;
332 ballot = ballots;
333 for (i=0; i<tuple_count; i++) {
334 char *member_id, *issue_id;
335 member_id = PQgetvalue(res, i, COL_MEMBER_ID);
336 issue_id = PQgetvalue(res, i, COL_ISSUE_ID);
337 if (old_member_id && strcmp(old_member_id, member_id)) {
338 ballot++;
339 candidates_in_ballot = 0;
340 }
341 ballot->candidates[candidates_in_ballot++] = candidate_by_key(issue_id);
342 old_member_id = member_id;
343 }
344 // print ballots, if logging is enabled:
345 if (logging) {
346 for (i=0; i<ballot_count; i++) {
347 int j;
348 printf("Ballot #%i: ", i+1);
349 for (j=0; j<ballots[i].count; j++) {
350 if (!j) printf("issues ");
351 else printf(", ");
352 printf("#%s", ballots[i].candidates[j]->key);
353 }
354 // if (!j) printf("empty"); // should not happen
355 printf(".\n");
356 }
357 }
358 }
360 // calculate ranks based on constructed data structures:
361 for (i=0; i<candidate_count; i++) {
362 struct candidate *candidate = loser(i, ballots, ballot_count);
363 candidate->seat = candidate_count - i;
364 if (logging) printf("Assigning rank #%i to issue #%s.\n", candidate_count-i, candidate->key);
365 }
367 // free ballots[] array:
368 for (i=0; i<ballot_count; i++) {
369 // if (ballots[i].count) { // count should not be zero
370 free(ballots[i].candidates);
371 // }
372 }
373 free(ballots);
375 // write results to database:
376 if (logging) printf("Writing ranks to database.\n");
377 err = write_ranks(db, escaped_area_id);
378 if (logging) printf("Done.\n");
380 // free candidates[] array:
381 free(candidates);
383 // return error code of write_ranks() call
384 return err;
385 }
387 int main(int argc, char **argv) {
389 // variable declarations:
390 int err = 0;
391 int i, count;
392 char *conninfo;
393 PGconn *db;
394 PGresult *res;
396 // parse command line:
397 if (argc == 0) return 1;
398 if (argc == 1 || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
399 FILE *out;
400 out = argc == 1 ? stderr : stdout;
401 fprintf(out, "\n");
402 fprintf(out, "Usage: %s [-v|--verbose] <conninfo>\n", argv[0]);
403 fprintf(out, "\n");
404 fprintf(out, "<conninfo> is specified by PostgreSQL's libpq,\n");
405 fprintf(out, "see http://www.postgresql.org/docs/9.1/static/libpq-connect.html\n");
406 fprintf(out, "\n");
407 fprintf(out, "Example: %s dbname=liquid_feedback\n", argv[0]);
408 fprintf(out, "\n");
409 return argc == 1 ? 1 : 0;
410 }
411 {
412 size_t len = 0;
413 int argb = 1;
414 if (
415 argc >= 2 &&
416 (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--verbose"))
417 ) {
418 argb = 2;
419 logging = 1;
420 }
421 for (i=argb; i<argc; i++) len += strlen(argv[i]) + 1;
422 conninfo = malloc(len * sizeof(char));
423 if (!conninfo) {
424 fprintf(stderr, "Error: Could not allocate memory for conninfo string.\n");
425 abort();
426 }
427 conninfo[0] = 0;
428 for (i=argb; i<argc; i++) {
429 if (i>argb) strcat(conninfo, " ");
430 strcat(conninfo, argv[i]);
431 }
432 }
434 // connect to database:
435 db = PQconnectdb(conninfo);
436 if (!db) {
437 fprintf(stderr, "Error: Could not create database handle.\n");
438 return 1;
439 }
440 if (PQstatus(db) != CONNECTION_OK) {
441 fprintf(stderr, "Could not open connection:\n%s", PQerrorMessage(db));
442 return 1;
443 }
445 // go through areas:
446 res = PQexec(db, "SELECT \"id\" FROM \"area\"");
447 if (!res) {
448 fprintf(stderr, "Error in pqlib while sending SQL command selecting areas to process.\n");
449 err = 1;
450 } else if (PQresultStatus(res) != PGRES_TUPLES_OK) {
451 fprintf(stderr, "Error while executing SQL command selecting areas to process:\n%s", PQresultErrorMessage(res));
452 err = 1;
453 PQclear(res);
454 } else if (PQnfields(res) < 1) {
455 fprintf(stderr, "Too few columns returned by SQL command selecting areas to process.\n");
456 err = 1;
457 PQclear(res);
458 } else {
459 count = PQntuples(res);
460 if (logging) printf("Number of areas to process: %i\n", count);
461 for (i=0; i<count; i++) {
462 char *area_id, *escaped_area_id;
463 char *cmd;
464 PGresult *res2;
465 area_id = PQgetvalue(res, i, 0);
466 if (logging) printf("Processing area #%s:\n", area_id);
467 escaped_area_id = escapeLiteral(db, area_id, strlen(area_id));
468 if (!escaped_area_id) {
469 fprintf(stderr, "Could not escape literal in memory.\n");
470 abort();
471 }
472 if (asprintf(&cmd, "SELECT \"member_id\", \"weight\", \"issue_id\" FROM \"issue_supporter_in_admission_state\" WHERE \"area_id\" = %s ORDER BY \"member_id\"", escaped_area_id) < 0) {
473 fprintf(stderr, "Could not prepare query string in memory.\n");
474 abort();
475 }
476 res2 = PQexec(db, cmd);
477 free(cmd);
478 if (!res2) {
479 fprintf(stderr, "Error in pqlib while sending SQL command selecting issue supporter in admission state.\n");
480 err = 1;
481 } else if (PQresultStatus(res2) != PGRES_TUPLES_OK) {
482 fprintf(stderr, "Error while executing SQL command selecting issue supporter in admission state:\n%s", PQresultErrorMessage(res));
483 err = 1;
484 PQclear(res2);
485 } else if (PQnfields(res2) < 3) {
486 fprintf(stderr, "Too few columns returned by SQL command selecting issue supporter in admission state.\n");
487 err = 1;
488 PQclear(res2);
489 } else {
490 if (process_area(db, res2, escaped_area_id)) err = 1;
491 PQclear(res2);
492 }
493 freemem(escaped_area_id);
494 }
495 PQclear(res);
496 }
498 // cleanup and exit:
499 PQfinish(db);
500 if (!err) {
501 if (logging) printf("Successfully terminated.\n");
502 } else {
503 fprintf(stderr, "Exiting with error code %i.\n", err);
504 }
505 return err;
507 }

Impressum / About Us