-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consanguinity.cpp
179 lines (153 loc) · 5.74 KB
/
Consanguinity.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//============================================================================
// Name : Consanguinity.cpp
// Author : Willow Black
// Version : 1.0
// Description : Consanguinity calculator
//============================================================================
#include <iostream>
#include <new>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
struct Creature{
string name;
Creature* Mother;
Creature* Father;
vector<Creature*> Children;
float Consanguinity;
Creature(){
Consanguinity = 0;
Mother = nullptr;
Father = nullptr;
Children.reserve(16);
}
};
void findAncestors(Creature* src,set<Creature*>& Ancestors){
if(src->Mother != nullptr && src->Father != nullptr){
Ancestors.insert(src->Mother);
Ancestors.insert(src->Father);
findAncestors(src->Mother, Ancestors);
findAncestors(src->Father, Ancestors);
}
}
set<Creature*> findcommonAncestors(Creature* src, Creature* dst){
set<Creature*> srcAncestors;
findAncestors(src,srcAncestors);
set<Creature*> dstAncestors;
findAncestors(dst,dstAncestors);
set<Creature*> commonAncestors;
for(set<Creature*>::iterator it = srcAncestors.begin(); it != srcAncestors.end(); ++it){
if(dstAncestors.find(*it) != dstAncestors.end())
commonAncestors.insert(*it);
}
return commonAncestors;
}
void directRelation(Creature* src, Creature* dst, vector<int>& distances, int distance){
//Search down the tree until dst is found.
if (src == dst)
distances.push_back(distance);
else
for(vector<Creature*>::iterator it = src->Children.begin(); it != src->Children.end(); ++it)
directRelation(*it,dst,distances,distance+1);
}
void collateralRelation(bool up, Creature* src, Creature* dst, set<Creature*> common, vector<int>& distances, int distance, set<Creature*> visited){
visited.insert(src);
//This function is funky. Basically, it goes up the tree until it finds a common ancestor
//Then it allows searching down the tree from that node, too. Both cases, we do not revisit previously visited nodes.
if(src == dst)
distances.push_back(distance);
else if (up){
if(common.find(src) != common.end())
for(vector<Creature*>::iterator it = src->Children.begin(); it != src->Children.end(); ++it){
if(visited.find(*it) == visited.end())
collateralRelation(false,*it,dst,common,distances,distance+1,visited);
}
else {
if(src->Mother != nullptr)
collateralRelation(up, src->Mother,dst,common,distances,distance+1,visited);
if(src->Father != nullptr)
collateralRelation(up, src->Father,dst,common,distances,distance+1,visited);
}
}else{
for(vector<Creature*>::iterator it = src->Children.begin(); it != src->Children.end(); ++it)
if(true)
collateralRelation(false,*it,dst,common,distances,distance+1,visited);
}
}
vector<int> determineRelatedness(Creature* src, Creature* dst){
vector<int> distances;
set<Creature*> visited;
set<Creature*> srcAncestors;
set<Creature*> dstAncestors;
findAncestors(src,srcAncestors);
//Edge case handling if we want funky ways to handle twins.
if(src == dst)
distances.push_back(0);
//A set is a red-black tree, find() returns the end of the set if the element searched for isn't in the set
//This is to make sure that src and dst are in the correct order for doing direct relation search
if(srcAncestors.find(dst) != srcAncestors.end()){
Creature* temp = src;
src = dst;
dst = temp;
}
findAncestors(src,srcAncestors);
findAncestors(dst,dstAncestors);
set<Creature*> commonAncestors = findcommonAncestors(src,dst);
visited.insert(src);
//If src is an ancestor of dst, perform a direct relation search
if(dstAncestors.find(src) != dstAncestors.end())
directRelation(src,dst,distances,0);
//If there are common ancestors, perform a collateral relation search
if(commonAncestors.size() > 0)
collateralRelation(true,src,dst,commonAncestors,distances,0, visited);
//collateral and direct relations are calculated separately, as per advice
//from the website I read describing how to perform consanguinity calculations
/*
for(vector<int>::iterator it = distances.begin(); it != distances.end(); ++it)
cout << *it << " ";
cout << endl;
*/
return distances;
}
Creature* Mate(Creature* Mom, Creature* Dad){
Creature* Child = new Creature();
//Child->name = "(" + Mom->name + "&" + Dad->name + ")";
Child->Mother = Mom;
Child->Father = Dad;
Mom->Children.push_back(Child);
Dad->Children.push_back(Child);
vector<int> degrees = determineRelatedness(Mom,Dad);
//Calculating the coefficient of relationship, r, and summing the average of the parents with it.
//Coefficient of relationship, without the parental averaging, approaches 1 with an arbitrary
//amount of inbreeding. Perhaps just going to leave out the parental averaging, but it makes
//for an interesting way of more immediately reflecting how inbred a genetic line is.
float r = (Mom->Consanguinity+Dad->Consanguinity)/2.0;
for(vector<int>::iterator it = degrees.begin(); it != degrees.end(); ++it){
r += pow(0.5,*it);
}
Child->Consanguinity=r;
return Child;
}
int main() {
Creature* A = new Creature();
Creature* B = new Creature();
A->name = 'A';
B->name = 'B';
vector<Creature*> creatures;
creatures.push_back(A);
creatures.push_back(B);
for(int i = 0; i < 24; i ++){
creatures.push_back(Mate(creatures.at(creatures.size()-1),creatures.at(creatures.size()-2)));
creatures.back()->name = 'C' + i;
}
cout << "Incestuousness: " << endl;
for(vector<Creature*>::iterator it = creatures.begin(); it != creatures.end(); ++it){
Creature* temp = *it;
if(temp->Mother != nullptr){
cout << temp->Mother->name << "x" << temp->Father->name << " | ";
}
cout << temp->name + ": " << temp->Consanguinity << endl;
}
return 0;
}