13 | | We'll use the following replication policy: |
14 | | * A job is considered finished if either |
15 | | * Two volunteers reported "no ellipse found"; |
16 | | * Two volunteers found ellipses, and their centers are within 20 pixels; |
17 | | * If ten instances have been performed and neither condition holds, the job is marked as "inconclusive" and no further instances are issued. |
| 19 | The callback functions follow: |
| 20 | {{{ |
| 21 | 30 function job_issued($job, $inst, $user) { |
| 22 | 31 $insts = $job->get_instances(); |
| 23 | 32 if (count($insts) == 1) { |
| 24 | 33 $job->set_priority(2); |
| 25 | 34 } else { |
| 26 | 35 $job->set_priority(0); |
| 27 | 36 } |
| 28 | 37 } |
37 | | function job_finished($job, $inst) { |
38 | | $response = null; |
39 | | if (get_str('submit', true)) { |
40 | | $response->have_ellipse = 0; |
41 | | } else { |
42 | | $response->have_ellipse = 1; |
43 | | $response->cx = get_int('pic_x'); |
44 | | $response->cy = get_int('pic_y'); |
45 | | } |
46 | | $inst->update_info($response); |
47 | | |
48 | | // see if job is done |
49 | | // |
50 | | $insts = $job->get_finished_instances(); |
51 | | $n = count($insts); |
52 | | |
53 | | $results = null; |
54 | | foreach ($insts as $inst) { |
55 | | $results[] = $inst->get_info(); |
56 | | } |
57 | | for ($i=0; $i<$n-1; $i++) { |
58 | | $r1 = $results[$i]; |
59 | | for ($j=$i+1; $j<$n; $j++) { |
60 | | $r2 = $results[$j]; |
61 | | if (compatible($r1, $r2)) { |
62 | | $job->update_state(BOSSA_JOB_DONE); |
63 | | return; |
64 | | } |
65 | | } |
66 | | } |
67 | | if ($n >= 10) { |
68 | | $job->update_state(BOSSA_JOB_INCONCLUSIVE); |
69 | | return; |
70 | | } |
71 | | } |
72 | | |
73 | | // two results are compatible if neither found an ellipse, |
74 | | // or they both did and centers are within 20 pixels |
75 | | // |
76 | | function compatible($r1, $r2) { |
77 | | if ($r1->have_ellipse) { |
78 | | if ($r2->have_ellipse) { |
79 | | $dx = ($r1->cx - $r2->cx); |
80 | | $dy = ($r1->cy - $r2->cy); |
81 | | $dsq = $dx*$dx + $dy*$dy; |
82 | | return ($dsq < 400); |
83 | | } else return false; |
84 | | } else { |
85 | | return !$r2->have_ellipse; |
86 | | } |
87 | | } |
| 38 | 39 function job_finished($job, $inst) { |
| 39 | 40 // record the user's response |
| 40 | 41 // |
| 41 | 42 $response = null; |
| 42 | 43 if (get_str('submit', true)) { |
| 43 | 44 $response->have_ellipse = 0; |
| 44 | 45 } else { |
| 45 | 46 $response->have_ellipse = 1; |
| 46 | 47 $response->cx = get_int('pic_x'); |
| 47 | 48 $response->cy = get_int('pic_y'); |
| 48 | 49 } |
| 49 | 50 $inst->set_opaque_data($response); |
| 50 | 51 |
| 51 | 52 // see whether we have a consensus |
| 52 | 53 // |
| 53 | 54 $insts = $job->get_finished_instances(); |
| 54 | 55 $n = count($insts); |
| 55 | 56 |
| 56 | 57 $results = null; |
| 57 | 58 foreach ($insts as $inst) { |
| 58 | 59 $results[] = $inst->get_opaque_data(); |
| 59 | 60 } |
| 60 | 61 for ($i=0; $i<$n-1; $i++) { |
| 61 | 62 $r1 = $results[$i]; |
| 62 | 63 for ($j=$i+1; $j<$n; $j++) { |
| 63 | 64 $r2 = $results[$j]; |
| 64 | 65 if (compatible($r1, $r2)) { |
| 65 | 66 $job->set_state(BOSSA_JOB_DONE); |
| 66 | 67 return; |
| 67 | 68 } |
| 68 | 69 } |
| 69 | 70 } |
| 70 | 71 |
| 71 | 72 // no consensus - see whether we've reached replication limit |
| 72 | 73 // |
| 73 | 74 if ($n >= 10) { |
| 74 | 75 $job->set_state(BOSSA_JOB_INCONCLUSIVE); |
| 75 | 76 return; |
| 76 | 77 } |
| 77 | 78 |
| 78 | 79 // still looking for consensus - allow another instance to be issued |
| 79 | 80 // |
| 80 | 81 $job->set_priority(2); |
| 81 | 82 } |
| 82 | 83 |
| 83 | 84 // two results are compatible if neither found an ellipse, |
| 84 | 85 // or they both did and centers are within 20 pixels |
| 85 | 86 // |
| 86 | 87 function compatible($r1, $r2) { |
| 87 | 88 if ($r1->have_ellipse) { |
| 88 | 89 if ($r2->have_ellipse) { |
| 89 | 90 $dx = ($r1->cx - $r2->cx); |
| 90 | 91 $dy = ($r1->cy - $r2->cy); |
| 91 | 92 $dsq = $dx*$dx + $dy*$dy; |
| 92 | 93 return ($dsq < 400); |
| 93 | 94 } else return false; |
| 94 | 95 } else { |
| 95 | 96 return !$r2->have_ellipse; |
| 96 | 97 } |
| 97 | 98 } |