-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
211 lines (136 loc) · 6.96 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include "steering_behaviors.h"
#include "window.h"
#include <omp.h>
#include <sys/time.h>
// Main class for running the parallel flocking sim
#define WINDOWSIZE 1000
int main(int argc, char **argv) {
long num_agents;
long num_steps;
int thread_count;
char* filename;
long timestep;
if (argc != 5) {
printf("Usage: num_agents num_steps filename\n");
exit(1);
}
thread_count = atoi(argv[1]);
num_agents = atol(argv[2]);
num_steps = atol(argv[3]);
filename = argv[4];
printf("Running a simulation with %lu agents for %lu steps and saving the data to the file %s...\n",
num_agents, num_steps, filename);
// here's the simulation skeleton
// create the agents, with random headings, maybe random locations or all in a bunch
unsigned int i = 1;
int j;
Agent **agent_lists_at_timesteps;
agent_lists_at_timesteps = malloc(num_steps * sizeof(Agent*));
int a;
for(a=0; a<num_steps; a++)
{
agent_lists_at_timesteps[a] = malloc(num_agents * sizeof(Agent));
}
// Agent *agent_list = malloc(num_agents * sizeof(Agent));
struct timeval tv;
gettimeofday(&tv, NULL);
srand(tv.tv_usec);
for(j = 0; j < num_agents - 1; j++){
//printf("Creating agent %d\n", j);
Vector2D heading = init_vector(-(rand_r(&i) % 5), -(rand_r(&i) % 5));
//printf("Done vector creation\n");
Point2D start_loc = init_point((rand_r(&i) % WINDOWSIZE), (rand_r(&i) % WINDOWSIZE));
double speed = 5;
Agent_Type prey_type = prey;
//printf("initing agent\n");
Agent new_agent = init_agent(&heading, &start_loc, speed, prey_type);
//printf("Adding to list\n");
agent_lists_at_timesteps[0][j] = new_agent;
//printf("Done creating agent %d\n", j);
}
Vector2D heading = init_vector((rand_r(&i) % 5), (rand_r(&i) % 5));
Point2D start_loc = init_point((rand_r(&i) % WINDOWSIZE), (rand_r(&i) % WINDOWSIZE));
double speed = 5;
Agent_Type predator_type = predator;
agent_lists_at_timesteps[0][j] = init_agent(&heading, &start_loc, speed, predator_type);
int agent_close;
double ts, te;
// for each numstep
ts = omp_get_wtime();
for(timestep = 1; timestep < num_steps; timestep++){
int k;
Vector2D s,c,a, combo, seek_vector;
# pragma omp parallel for num_threads(thread_count) \
private(s, c, a, combo, k)
for(k = 0; k< num_agents; k++){
if(agent_lists_at_timesteps[timestep-1][k].type == prey) {
s = separation(&agent_lists_at_timesteps[timestep-1][k], k, agent_lists_at_timesteps[timestep-1], num_agents);
c = cohesion(&agent_lists_at_timesteps[timestep-1][k], k, agent_lists_at_timesteps[timestep-1], num_agents);
a = alignment(&agent_lists_at_timesteps[timestep-1][k], k, agent_lists_at_timesteps[timestep-1], num_agents);
combo = plus(&agent_lists_at_timesteps[timestep-1][k].heading, &s);
plus_equals(&combo, &c);
plus_equals(&combo, &a);
divide_equals(&combo, 1.1);
agent_lists_at_timesteps[timestep][k].heading.x = combo.x;
agent_lists_at_timesteps[timestep][k].heading.y = combo.y;
agent_lists_at_timesteps[timestep][k].position.x = agent_lists_at_timesteps[timestep-1][k].position.x;
agent_lists_at_timesteps[timestep][k].position.y = agent_lists_at_timesteps[timestep-1][k].position.y;
agent_lists_at_timesteps[timestep][k].speed = agent_lists_at_timesteps[timestep-1][k].speed;
agent_lists_at_timesteps[timestep][k].type = agent_lists_at_timesteps[timestep-1][k].type;
}
else {
/*
if(((timestep - 1) % 50) == 0) {
agent_close = find_closest(&agent_lists_at_timesteps[timestep-1][k], k, agent_lists_at_timesteps[timestep-1], num_agents);
}
*/
//seek_vector = seek(&agent_lists_at_timesteps[timestep-1][k], &agent_lists_at_timesteps[timestep-1][agent_close]);
seek_vector = init_vector(5.0, 0.0);
agent_lists_at_timesteps[timestep][k].heading.x = seek_vector.x;
agent_lists_at_timesteps[timestep][k].heading.y = seek_vector.y;
agent_lists_at_timesteps[timestep][k].position.x = agent_lists_at_timesteps[timestep-1][k].position.x;
agent_lists_at_timesteps[timestep][k].position.y = agent_lists_at_timesteps[timestep-1][k].position.y;
agent_lists_at_timesteps[timestep][k].speed = agent_lists_at_timesteps[timestep-1][k].speed;
agent_lists_at_timesteps[timestep][k].type = agent_lists_at_timesteps[timestep-1][k].type;
}
}
printf("just changed headings\n");
# pragma omp parallel for num_threads(thread_count)
for(k = 0; k< num_agents; k++){
agent_update(&agent_lists_at_timesteps[timestep][k], 1);
//printf("TRYING TO MOD %f \n", agent_list[k].position.x);
agent_lists_at_timesteps[timestep][k].position.x = ((int)agent_lists_at_timesteps[timestep][k].position.x % WINDOWSIZE);
agent_lists_at_timesteps[timestep][k].position.y = ((int)agent_lists_at_timesteps[timestep][k].position.y % WINDOWSIZE);
//printf("AND I GOT %f \n", agent_list[k].position.x);
}
/* for(k = 0; k < num_agents; k++){
add_point_to_file(agent_lists_at_timesteps[timestep][k].position.x, agent_lists_at_timesteps[timestep][k].position.y, 0, filename);
}
if(timestep != num_steps -1)
add_terminator_to_file(-2, filename); */
//printf("timestep is %d\n", timestep);
}
/* add_terminator_to_file(-1, filename); */
te = omp_get_wtime();
printf("Time to do all computation and updating %f\n", te-ts);
// for each agent
// do cohesion, alignment, and seperation
// update new heading
//
// for each agent
// update the agent location
// check their position within boundaries, fix it with mod
// update old heading by setting it = to new heading
// write position of agent to file
//
// (if it's not the last step)
// write -2 to file
//
// write -1 to file
create_window(WINDOWSIZE, WINDOWSIZE, "Test");
// run_file(filename);
run_sim(agent_lists_at_timesteps, num_steps, num_agents);
return 0;
}