-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlcoholNetwork.java
More file actions
222 lines (180 loc) · 7.55 KB
/
AlcoholNetwork.java
File metadata and controls
222 lines (180 loc) · 7.55 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Alcohol ABM
*
* This model will compare interventions aimed at reducing racial disparities in alcohol-related
* homicide, using New York City as the place and population of interest.
*
* The network class creates the social network linking agents to each other. Linked agents influence
* each other's drinking behaviors.
*
* Revised May 27, 2014
*
* Revisions: (1) Add consideration of drinking status when forming social network - 5/27/2014
*
*/
package cbtModel;
import java.util.ArrayList;
import uchicago.src.sim.util.Random;
import java.util.Iterator;
public class AlcoholNetwork {
// Start with zero edges
public static int numEdges = 0;
public int numNodes;
// Keep track of the degree of each node
ArrayList<Integer> socialrelationships = new ArrayList<Integer>();
// the Alcohol Social Network Constructor
public AlcoholNetwork() {
// create lists of connected agents;
SocialNetworkPopulate();
} // end of Alcohol Social Network constructor
public void SocialNetworkPopulate() {
System.out.println("Populate Social Network Method called");
// Outer loop i is the node currently being attached to the network
Iterator<AlcoholAgent> iter = AlcoholModel.SocialNetworkList.iterator();
while(iter.hasNext()) {
// "a" is the node being added
AlcoholAgent a = iter.next();
// Cycle through "while" loop as long as agent has less than allotted friends
// NOTE: attempt to find target friends for 10,000 iterations, then give up
int outCycleInt = 0;
while (a.getTotalFriends() < a.finalfriendsize && outCycleInt < 10000) {
outCycleInt++;
// Begin match algorithm
// FIRST CHOICE: Spatial or demographics match
double space_demo_choice = Random.uniform.nextDoubleFromTo(0, 1);
int cycle = 0;
// Spatial match algorithm implemented 25% of the time
if (space_demo_choice <= 0.25) {
while (cycle < 10000) {
// Select an agent at random for a potential match
int nodestart = 0 + (int)(Math.random() * ((AlcoholModel.SocialNetworkList.size()-1 - 0) + 1));
// "b" is the agent under evaluation as a potential match
AlcoholAgent b = (AlcoholAgent)AlcoholModel.SocialNetworkList.get(nodestart);
// Check if (1) trying to add itself, (2) add to maxed-out node, (3) add to a node that has already been added
// If so, do nothing
if (a.getID()==b.getID() || (b.getTotalFriends()>b.finalfriendsize) || a.isFriend(b)) {
// do nothing
}
else {
int xA = a.getX();
int yA = a.getY();
int xB = b.getX();
int yB = b.getY();
if (Math.abs(xA-xB) <= 100 && Math.abs(yA-yB) <= 100) {
boolean Match1 = true, Match2 = true, Match3 = true, Match4 = true, Match5 = true;;
// Age check
double age_choice = Random.uniform.nextDoubleFromTo(0, 1);
double Age_A = a.getAge();
double Age_B = b.getAge();
if (age_choice > 0.185) {
if (Math.abs(Age_A-Age_B)<=10) { Match1 = true; } else { Match1 = false; }
}
// Gender check
double gender_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Gender_A = a.getGender();
int Gender_B = b.getGender();
if (gender_choice > 0.995) {
if (Gender_A == Gender_B) { Match2 = true; } else { Match2 = false; }
}
// Race check
double race_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Race_A = a.getRace();
int Race_B = b.getRace();
if (race_choice > 0.01) {
if (Race_A == Race_B) { Match3 = true; } else { Match3 = false; }
}
// Education check
double education_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Edu_A = a.getEducation();
int Edu_B = b.getEducation();
if (education_choice > 0.25) {
if (Edu_A == Edu_B) { Match4 = true; } else { Match4 = false; }
}
// Drinking status check
double drinking_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Drk_A = a.getDrinkStat();
int Drk_B = b.getDrinkStat();
if (drinking_choice <= 0.15) {
if (Drk_A == Drk_B) { Match5 = true; } else { Match5 = false; }
}
// Match node
if (Match1 && Match2 && Match3 && Match4 && Match5) {
b.setFriend(a);
a.setFriend(b);
numEdges+=1;
break;
}
} // spatial loop
} // end of else loop
cycle += 1;
} // end of while loop
} // end of spatial matching
// Demographic match algorithm implemented 75% of the time
cycle = 0;
if (space_demo_choice > 0.25) {
while (cycle < 10000) {
// Select an agent at random for a potential match
int nodestart = 0 + (int)(Math.random() * ((AlcoholModel.SocialNetworkList.size()-1 - 0) + 1));
// "b" is the agent under evaluation as a potential match
AlcoholAgent b = (AlcoholAgent)AlcoholModel.SocialNetworkList.get(nodestart);
// Check if (1) trying to add itself, (2) add to maxed-out node, (3) add to a node that has already been added
// If so, do nothing
if (a.getID()==b.getID() || (b.getTotalFriends()>b.finalfriendsize) || a.isFriend(b)) {
// do nothing
}
else {
boolean Match1 = true, Match2 = true, Match3 = true, Match4 = true, Match5 = true;
// Age check
double age_choice = Random.uniform.nextDoubleFromTo(0, 1);
double Age_A = a.getAge();
double Age_B = b.getAge();
if (age_choice > 0.185) {
if (Math.abs(Age_A-Age_B)<=10) { Match1 = true; } else { Match1 = false; }
}
// Gender check
double gender_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Gender_A = a.getGender();
int Gender_B = b.getGender();
if (gender_choice > 0.995) {
if (Gender_A == Gender_B) { Match2 = true; } else { Match2 = false; }
}
// Race check
double race_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Race_A = a.getRace();
int Race_B = b.getRace();
if (race_choice > 0.01) {
if (Race_A == Race_B) { Match3 = true; } else { Match3 = false; }
}
// Education check
double education_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Edu_A = a.getEducation();
int Edu_B = b.getEducation();
if (education_choice > 0.25) {
if (Edu_A == Edu_B) { Match4 = true; } else { Match4 = false; }
}
// Drinking status check
double drinking_choice = Random.uniform.nextDoubleFromTo(0, 1);
int Drk_A = a.getDrinkStat();
int Drk_B = b.getDrinkStat();
if (drinking_choice <= 0.15) {
if (Drk_A == Drk_B) { Match5 = true; } else { Match5 = false; }
}
// Match node
if (Match1 && Match2 && Match3 && Match4 && Match5) {
b.setFriend(a);
a.setFriend(b);
numEdges+=1;
break;
}
} // end of else loop
cycle += 1;
} // end of while loop
} // end of demographic matching
} // end of attempt to reach target number of friends (while loop)
// remove this agent ("node") if she/he has enough friends
if (a.getTotalFriends() == a.finalfriendsize) { iter.remove(); }
} // end of iterator
} // end of SocialNetworkPopulate()
///////////////// SETTERS AND GETTERS
public int getEdges() { return numEdges; }
} // end of AlcoholNetwork class