Skip to content

Commit

Permalink
Correcting tabs.
Browse files Browse the repository at this point in the history
  • Loading branch information
malaggan committed Nov 26, 2015
1 parent 7983ac1 commit d832614
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 244 deletions.
154 changes: 77 additions & 77 deletions abstract_user.hh
Expand Up @@ -35,100 +35,100 @@ public:
using set_base = std::unordered_set<user*,user::hash,user::key_eq>; /* [1] */
class set_t : public set_base
{
using set_base::set_base; // inherit constructors
using set_base::set_base; // inherit constructors
public:
auto random_subset(size_t size) const {
return random_sample<>{}(*this, size);
}
auto random_subset(size_t size) const {
return random_sample<>{}(*this, size);
}
};

explicit user(user_id_t me, set_t &already_joined, all_t &all_peers);
// privacy stuff
void add_item(item_id_t i) { items.insert(i); };
explicit user(user_id_t me, set_t &already_joined, all_t &all_peers);
// privacy stuff
void add_item(item_id_t i) { items.insert(i); };
template <typename Distribution>
void generate_weights(Distribution /*pc*/) {
assert(privacy_weights.empty());
// TODO: try to use generate_n since it is parallelizable using std::parallel gnu extension
for(item_id_t const & item : items)
privacy_weights[item] = rational{1}; //pc(rng); XXX for debugging: no privacy groups TODO we need rational-from-double here
void generate_weights(Distribution /*pc*/) {
assert(privacy_weights.empty());
// TODO: try to use generate_n since it is parallelizable using std::parallel gnu extension
for(item_id_t const & item : items)
privacy_weights[item] = rational{1}; //pc(rng); XXX for debugging: no privacy groups TODO we need rational-from-double here
}

enum class privacy_class : uint8_t { CONCERNED = 0, NORMAL = 1, UNCONCERNED = 2 };
static privacy_class random_privacy_class () {
switch (std::uniform_int_distribution<uint8_t>{0,2}(rng)) {
case 0: return privacy_class::CONCERNED;
case 1: return privacy_class::NORMAL;
case 2: return privacy_class::UNCONCERNED;
default : assert(false);
}
assert(false);
switch (std::uniform_int_distribution<uint8_t>{0,2}(rng)) {
case 0: return privacy_class::CONCERNED;
case 1: return privacy_class::NORMAL;
case 2: return privacy_class::UNCONCERNED;
default : assert(false);
}
assert(false);
}

static std::pair<rational, rational> pc_limits(privacy_class pc) {
switch(pc) {
case privacy_class::CONCERNED: return std::make_pair(rational{0}, rational{1});
case privacy_class::NORMAL: return std::make_pair(rational{1,2}, rational{1});
case privacy_class::UNCONCERNED: return std::make_pair(rational{9,10}, rational{1});
default: assert(false);
}
assert(false);
switch(pc) {
case privacy_class::CONCERNED: return std::make_pair(rational{0}, rational{1});
case privacy_class::NORMAL: return std::make_pair(rational{1,2}, rational{1});
case privacy_class::UNCONCERNED: return std::make_pair(rational{9,10}, rational{1});
default: assert(false);
}
assert(false);
}

std::vector<rational> weights_of(std::vector<item_id_t> const & subset) {
if(privacy_weights.empty()) {
rational min, max;
std::tie(min, max) = pc_limits(random_privacy_class());
assert(0 <= min && min <= 1);
assert(0 <= max && max <= 1);
generate_weights(std::uniform_real_distribution<float>{boost::rational_cast<float>(min), boost::rational_cast<float>(max)});
}

// return only the weigts for items in the `subset'
//
// set<item_id_t> (sorted): 3 76 195 344
// subset : 3 - 195 -
// privacy_weights(vector): 0.5 1.0 0.3 0.24
// return value : 0.5 0.3 (in that order)
// for every subset item, find index in set, append privacy_weight at that index// a map would be better
std::vector<rational> v;
for(auto const &subset_item : subset)
v.push_back(privacy_weights[subset_item]);
return v;
if(privacy_weights.empty()) {
rational min, max;
std::tie(min, max) = pc_limits(random_privacy_class());
assert(0 <= min && min <= 1);
assert(0 <= max && max <= 1);
generate_weights(std::uniform_real_distribution<float>{boost::rational_cast<float>(min), boost::rational_cast<float>(max)});
}

// return only the weigts for items in the `subset'
//
// set<item_id_t> (sorted): 3 76 195 344
// subset : 3 - 195 -
// privacy_weights(vector): 0.5 1.0 0.3 0.24
// return value : 0.5 0.3 (in that order)
// for every subset item, find index in set, append privacy_weight at that index// a map would be better
std::vector<rational> v;
for(auto const &subset_item : subset)
v.push_back(privacy_weights[subset_item]);
return v;
}

rational cached_similarity(user_id_t other) {
// make sure that the pair<2,3> and pair<3,2> are the same by always putting the /larger/ id last
if(this->id > other)
return all_peers[other]->cached_similarity(this->id);

// TODO uncomment this. it was only commented for sanity check.
// if(similarities.count(id))
// return similarities[id];

std::vector<item_id_t> intersection;
boost::set_intersection(items, all_peers[other]->items, std::back_inserter<>(intersection));

ba::accumulator_set<rational, ba::features<ba::tag::sum>> acc;

boost::range::for_each(weights_of(intersection),
all_peers[other]->weights_of(intersection),
std::bind(std::ref(acc),
std::bind(std::multiplies<rational>(),
std::placeholders::_1,
std::placeholders::_2)));
// divide squared inner product by size1 * size2 to get cosine similarity
rational inner_prod{ba::sum(acc)};
inner_prod *= inner_prod;
rational denominator{items.size() * all_peers[other]->items.size()};
if(similarities.count(other)) // sanity check
assert( similarities[other] == inner_prod / denominator);

similarities[other] = inner_prod / denominator;
//std::cout << "squared cossim "
// << std::setfill('0') << std::setw(3) << a << "-"
// << std::setfill('0') << std::setw(3) << b << " = "
// << similarities[id] << std::endl;
return similarities[other];
// make sure that the pair<2,3> and pair<3,2> are the same by always putting the /larger/ id last
if(this->id > other)
return all_peers[other]->cached_similarity(this->id);

// TODO uncomment this. it was only commented for sanity check.
// if(similarities.count(id))
// return similarities[id];

std::vector<item_id_t> intersection;
boost::set_intersection(items, all_peers[other]->items, std::back_inserter<>(intersection));

ba::accumulator_set<rational, ba::features<ba::tag::sum>> acc;

boost::range::for_each(weights_of(intersection),
all_peers[other]->weights_of(intersection),
std::bind(std::ref(acc),
std::bind(std::multiplies<rational>(),
std::placeholders::_1,
std::placeholders::_2)));
// divide squared inner product by size1 * size2 to get cosine similarity
rational inner_prod{ba::sum(acc)};
inner_prod *= inner_prod;
rational denominator{items.size() * all_peers[other]->items.size()};
if(similarities.count(other)) // sanity check
assert( similarities[other] == inner_prod / denominator);

similarities[other] = inner_prod / denominator;
//std::cout << "squared cossim "
// << std::setfill('0') << std::setw(3) << a << "-"
// << std::setfill('0') << std::setw(3) << b << " = "
// << similarities[id] << std::endl;
return similarities[other];
}

// cyclon stuff
Expand Down
22 changes: 11 additions & 11 deletions cyclon.cc
Expand Up @@ -41,7 +41,7 @@ using std::left;
using std::right;

user::user(user_id_t me, set_t &already_joined, all_t &all_peers)
: id{me}, cyclon_view{}, all_peers{all_peers}, vicinity_view{}, items{}, privacy_weights{}, similarities{}
: id{me}, cyclon_view{}, all_peers{all_peers}, vicinity_view{}, items{}, privacy_weights{}, similarities{}
{
auto bootstrapPeers = already_joined.random_subset(viewSize);
for(auto rps_other : bootstrapPeers)
Expand Down Expand Up @@ -92,16 +92,16 @@ void user::cyclon_print_view() {
// );
// cout << endl;

if(this->id == 1) {
extern uint32_t current_cycle;
cout << 'R'
<< setfill('0') << setw(2) << right << current_cycle;
for (auto neighbor : cyclon_view | ::helpers::map_ids)
if( cached_similarity(neighbor) > rational{0} )
cout << setfill(' ') << setw(4) << right << neighbor << ": "
<< setfill(' ') << setw(10) << left << cached_similarity(neighbor);
cout << endl;
}
if(this->id == 1) {
extern uint32_t current_cycle;
cout << 'R'
<< setfill('0') << setw(2) << right << current_cycle;
for (auto neighbor : cyclon_view | ::helpers::map_ids)
if( cached_similarity(neighbor) > rational{0} )
cout << setfill(' ') << setw(4) << right << neighbor << ": "
<< setfill(' ') << setw(10) << left << cached_similarity(neighbor);
cout << endl;
}
}

auto user::random_replace(user_id_t id) -> user_id_t {
Expand Down
20 changes: 10 additions & 10 deletions dataset.cc
Expand Up @@ -11,14 +11,14 @@ std::istream& operator>>(std::istream& in, item& p) { in >> p.first; in >> p.sec

size_t dataset_get_num_users(std::string path)
{
auto f = std::ifstream{ path };
assert(f);
std::string signature;
f >> signature;
assert(signature == "dims");
size_t user_count;
f >> user_count;
return user_count;
auto f = std::ifstream{ path };
assert(f);
std::string signature;
f >> signature;
assert(signature == "dims");
size_t user_count;
f >> user_count;
return user_count;
}

void load_dataset(std::string path, all_t & all_peers)
Expand All @@ -35,7 +35,7 @@ void load_dataset(std::string path, all_t & all_peers)

for_each(boost::istream_range<item>(f),
[&all_peers](item const& item) {
assert(item.first <= all_peers.size());
all_peers[item.first - 1]->add_item(item.second);
assert(item.first <= all_peers.size());
all_peers[item.first - 1]->add_item(item.second);
});
}
48 changes: 24 additions & 24 deletions gossple.cc
Expand Up @@ -19,16 +19,16 @@ all_t all_peers;
namespace ba = boost::accumulators;
uint32_t current_cycle = 0;
int main(int argc, char *argv[]) {
assert(argc == 2);
// TODO check paper: Push-Pull Functional Reactive Programming - Conal Elliott
// for each user: has a test set and a training set
// the test set should not include items which no one else has; this will raise recall)
// similarity computation is one on the two training sets
// recall computation is done on test set vs (test+training)
// TODO: is search (recall) done also on RPS view??
// TODO: implemen laplacian mechanism (check my sources for cc code for Ilya Mironov paper)
assert(argc == 2);
// TODO check paper: Push-Pull Functional Reactive Programming - Conal Elliott
// for each user: has a test set and a training set
// the test set should not include items which no one else has; this will raise recall)
// similarity computation is one on the two training sets
// recall computation is done on test set vs (test+training)
// TODO: is search (recall) done also on RPS view??
// TODO: implemen laplacian mechanism (check my sources for cc code for Ilya Mironov paper)

user::set_t joined_peers;
user::set_t joined_peers;

int last = 0;
std::cout << "Initializing peers:";
Expand All @@ -52,16 +52,16 @@ int main(int argc, char *argv[]) {
std::cout << "Simulating cycles:" << std::endl;
for(auto i : boost::counting_range(0u, cycles))
{
current_cycle++;
// ba::accumulator_set<float, ba::features<ba::tag::sum_kahan>> acc;
// for(auto u : joined_peers)
// {
// auto a = dynamic_cast<vicinity*>(u);
// assert(a != nullptr);
// acc(a->recall());
// //a->print_view();
// }
// std::cout << "average recall = " << ba::sum_kahan(acc) / joined_peers.size() << std::endl;
current_cycle++;
// ba::accumulator_set<float, ba::features<ba::tag::sum_kahan>> acc;
// for(auto u : joined_peers)
// {
// auto a = dynamic_cast<vicinity*>(u);
// assert(a != nullptr);
// acc(a->recall());
// //a->print_view();
// }
// std::cout << "average recall = " << ba::sum_kahan(acc) / joined_peers.size() << std::endl;

boost::for_each(joined_peers, std::mem_fn(&user::vicinity_do_gossip));
// int progress = static_cast<int>(100*i/static_cast<float>(cycles));
Expand All @@ -78,11 +78,11 @@ int main(int argc, char *argv[]) {

for(auto a : joined_peers)
{
auto recall = a->recall();
std::cout << "recall("<<(a->id)<<") = " << boost::rational_cast<float>(recall) << std::endl ;
acc(recall);
acc2(boost::rational_cast<double>(recall));
//a->print_view();
auto recall = a->recall();
std::cout << "recall("<<(a->id)<<") = " << boost::rational_cast<float>(recall) << std::endl ;
acc(recall);
acc2(boost::rational_cast<double>(recall));
//a->print_view();
}
std::cout << "average recall (rational) = " << boost::rational_cast<float>( ba::sum(acc) / rational{joined_peers.size()}) << std::endl ;
std::cout << "average recall = " << (ba::sum_kahan(acc2) / joined_peers.size()) << std::endl ;
Expand Down
4 changes: 2 additions & 2 deletions heap.tcc
Expand Up @@ -30,8 +30,8 @@ template <typename T> using fn = std::function<T>;
template<typename RandomAccessRange, typename Comparator>
void sift_down(RandomAccessRange range, Comparator comp, heap_index root) {
using f1 = fn<fn<maybe<size_t>(size_t)>(maybe<size_t>)>;
using f2 = fn<maybe<size_t>(size_t)>;
f1 if_greater =
using f2 = fn<maybe<size_t>(size_t)>;
f1 if_greater =
[&](maybe<size_t> other) {
return f2{
[&comp,&range,other/*the bug: was capturing other by ref on expired stack frame...*/](size_t idx) -> maybe<size_t> {
Expand Down
56 changes: 28 additions & 28 deletions option.hh
Expand Up @@ -16,7 +16,7 @@ public:
constexpr explicit maybe( const T& value ) : base_maybe<T>{value} {}
constexpr explicit maybe( T&& value ) : base_maybe<T>{std::move(value)} {}

// bind = fmap + join. This is the join: maybe<maybe<B>> -> maybe<B>.
// bind = fmap + join. This is the join: maybe<maybe<B>> -> maybe<B>.
// TODO: test for maybe<maybe<maybe<T>>>
constexpr maybe( maybe<maybe<T>> const& o ) : base_maybe<T>{none} {
if(o)
Expand Down Expand Up @@ -82,35 +82,35 @@ namespace gossple {

// SFINAE to enable only on arithmetic types (or perhaps on any type with operator+ ?)
#include <experimental/type_traits>
#define maybe_lift_arith(op) template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
maybe<decltype(std::declval<T1>() op std::declval<T2>())>> operator op(maybe<T1> a, maybe<T2> b) \
{ \
if(a && b) \
return some(a.value() op b.value()); \
return none; \
} \
template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
#define maybe_lift_arith(op) template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
maybe<decltype(std::declval<T1>() op std::declval<T2>())>> operator op(maybe<T1> a, maybe<T2> b) \
{ \
if(a && b) \
return some(a.value() op b.value()); \
return none; \
} \
template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
maybe<decltype(std::declval<T1>() op std::declval<T2>())>> operator op(maybe<T1> a, T2 b) \
{ \
if(a) \
return some(a.value() op b); \
return none; \
} \
template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
{ \
if(a) \
return some(a.value() op b); \
return none; \
} \
template <typename T1, typename T2> \
std::enable_if_t< \
std::experimental::is_arithmetic_v<T1> and \
std::experimental::is_arithmetic_v<T2>, \
maybe<decltype(std::declval<T1>() op std::declval<T2>())>> operator op(T1 a, maybe<T2> b) \
{ \
if(b) \
return some(a op b.value()); \
return none; \
{ \
if(b) \
return some(a op b.value()); \
return none; \
}
// XXX: Is "a && b" this the correct behaviour? What if the semantics
// would be: if(!a) return b;, as in "a || b"?
Expand Down

0 comments on commit d832614

Please sign in to comment.