-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cpp
106 lines (94 loc) · 3.05 KB
/
tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// Created by kwoodle on 12/5/19.
//
#include "ElColl.h"
#include <boost/range/counting_range.hpp>
#include <boost/range/irange.hpp>
#include <limits>
using namespace std;
map<int, double> multipliers;
// lambda for comparator for set with States as keys
// larger state first
auto StateCompare = [](const State &state1, const State &state2) -> bool {
return state1.pop > state2.pop;
};
void calc_reps(set<State, decltype(StateCompare)> &states) {
// 50 reps have been allocated. 435 left
int imin{51};
int imax{436};
for (int n = imin; n < imax; ++n) {
double max_priority{numeric_limits<double>::min()};
string st;
State topstate;
for (auto &e : states) {
double spri = e.pop * multipliers[e.reps + 1];
if (spri > max_priority) {
max_priority = spri;
topstate = e;
}
}
++topstate.reps;
states.erase(topstate);
states.insert(topstate);
}
}
int main() {
// for apportionment by Equal proportions method
// initialize multipliers [reciprocal of the geometric mean]
// Each state gets one so lowest 'next' rep is 2
// int2 represents highest conceivable number of reps for any individual state.
int int_s1{2};
int int_s2{60};
for (int n : boost::irange(int_s1, int_s2)) {
multipliers[n] = multiplier(n);
}
drk::MySqlOptions opts;
drk::KSql kSql(opts);
kSql.Execute("use census");
const string sql{R"%%(select t1.code, t2.population, t2.reps from state_names as t1
inner join apportion as t2 on t1.state = t2.state order by population desc;
)%%"};
auto res0{kSql.ExecuteQuery(sql)};
set<State, decltype(StateCompare)> states0(StateCompare);
while (res0->next()) {
string c = res0->getString("code");
int ipop{res0->getInt("population")};
State s(c);
s.pop = ipop;
states0.insert(s);
}
calc_reps(states0);
const string sql3{R"%%(select t1.code, t3.TotRes from state_names as t1
inner join apportion as t2 on t1.state = t2.state
inner join over18_2018 as t3 on t2.state = t3.area order by TotRes desc;
)%%"};
auto res{kSql.ExecuteQuery(sql3)};
set<State, decltype(StateCompare)> states(StateCompare);
while (res->next()) {
string c = res->getString("code");
int ipop_2018{res->getInt("TotRes")};
State s(c);
s.pop = ipop_2018;
states.insert(s);
}
// 50 reps have been allocated. 435 left
calc_reps(states);
int reps_tot{0};
cout << "code \tr2010 \tpop2010\t\tr2018\tpop2018\n";
for (auto st:states) {
for (auto st0:states0) {
if (st.code == st0.code) {
cout << st.code << "\t" << st0.reps << "\t" << st0.pop << "\t\t" <<
st.reps << "\t" << st.pop << endl;
}
}
reps_tot += st.reps;
}
cout << "reps_tot " << reps_tot;
return 0;
}
double multiplier(int n) {
if (n < 2)
throw std::range_error("multplier(n) requires n>1");
return 1. / sqrt(n * (n - 1));
}