-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTown.cpp
executable file
·269 lines (254 loc) · 6.55 KB
/
Town.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "Town.h"
//Default Town Constructor
Town::Town()
{
}
/*
Precondition: The parameters are initialized within the following constraints:
1 <= number_bars <= 255 and 1<= population <=16348 (2^14) and user_cap is the
array for the capacity for each bar ( whose size may exceed the population.)
Postcondition: Barnums array and people array initialized as well as STM array
which keeps track which bar has won.
Description: Town Constructor which creates the Bars and Agents using inputted
parameters by the createBars() and createAgents() functions
*/
Town::Town(int number_bars,int population,int user_cap[],bool isPercent,int grpSize,int avgAge2,int dropValue) {
avgAge=avgAge2;
numbars= number_bars+1;
numpeeps=population;
int i;
STM=new int[3];
for (i=0;i<3;i++) {
STM[i] = rand()%numbars;
}
barnums=(Bar **)malloc(sizeof(Bar)*numbars);
createBars(numbars, user_cap, isPercent);
createGroups(population,grpSize,avgAge,dropValue);
stuff=new struct pass_graph;
topStrats=(Strategy **)malloc(sizeof(Strategy*)*20);
drop=dropValue;
}
/* Precondition: 1 <= numbars <= 255
Postcondition: Bar array barnums[] has been initialized
Description: The Bar array barnums[] is initialized and bar[0] is the
normalizing home location that if the population is greater
than the sum of all “user_cap[]”s, then bar [0] gets the value of population - SUM(user_cap[])
*/
void Town::createBars(int numbars,int user_cap[],bool isPercent) {
int i,j;
int temp=0;
for (i=0;i<(numbars-1);i++) {
j=(isPercent?((numpeeps*user_cap[i])/100):user_cap[i]);
barnums[i+1] = new Bar(j);
temp+=j;
}
barnums[0]= new Bar((numpeeps>temp?numpeeps-temp:0));
}
/* Precondition: 1<= population <= 16348 (2^14)
Postcondition: Agent array people[] is initialized and holds all initialized Agents in the population
Description: Populates Agent array people[] with initialized Agent Objects.
*/
void Town::createGroups(int population,int grpSize,int avgAge,int dropValue) {
int q=(int)(population/grpSize);
int q2=population%grpSize;
int i;
for(i=0;i<q;i++)
{
groups.push_back(new Group(grpSize,numbars,avgAge,dropValue));
}
groups.push_back(new Group(grpSize,numbars));
Group * y=groups.back();
for(i=0;i<q2;i++)
{
y->addAgent(avgAge,dropValue);
}
}
/* Precondition: Input parameters have been passed from the GUI and have initialized all appropriate instances.
Postcondition: One turn has passed, all parameters have been updated and recorded.
Description: One turn is simulated when turn() is run.
*/
graphPtr Town::turn() {
STI = (int)(pow((double)STM[2],3)+pow((double)STM[1],2)+STM[0]);
//cout<<ST<<endl;
int* a;
a=new int[256];
a= goingToBar();
a=getWinners(a);
tellWinners(a);
popcontrol();
return stuff;
}
/* Precondition: All necessary variables are set and a simulation is underway
Postcondition: Returns a pointer to an array which holds counters for how many
Agents are going to a particular bar for a given turn
Description: Polls all the agents of which bar they are going to and populates
an array which keeps track of this.
*/
int* Town::goingToBar() {
int i,j;
j=0;
int * barpeople;
barpeople=new int[numbars];
for(i=0;i<numbars;i++)
{
barpeople[i]=0;
}
for ( it=groups.begin() ; it != groups.end(); it++ )
{
j=(*it)->isGoingToBar(STI);
barpeople[j]+=(*it)->getNumPeeps();
}
return barpeople;
}
int Town::popcontrol()
{
int * g=(int*)malloc(sizeof(int));
(*g)=0;
for ( it=groups.begin() ; it != groups.end(); it++ )
{
if((*it)->isEmpty(g))
{
delete (*it);
it=groups.erase(it);
it--;
}
}
int x=*g;
free(g);
Group * a=groups.back();
while(x)
{
while(a->getNumPeeps()!=grpSize)
{
a->addAgent(new Agent(numbars,avgAge,drop,topStrats[rand()%20],topStrats[rand()%20],topStrats[rand()%20]));
x--;
if(!x)
{
break;
}
}
groups.push_back(new Group(grpSize,numbars));
a=groups.back();
}
}
/* Precondition: Input is an int array which contains 1s's where bars won
and 0's where bars lost
Postcondition: Bars that have won for a particular round have been acknowledged
and updated
Description: Function updates the STM to add current turn data. Also populates
Stuff structure to pass back to the GUI for graph output
*/
int* Town::getWinners(int a[]){
int j;
double k;
int minAt = 0;
double min=1;
int* rtn;
int winBars=0;
double winpeeps=0;
rtn=new int[numbars];
for(j=0;j<numbars;j++)
{
rtn[j]=0;
}
for (j=0;j<numbars;j++)
{
k=barnums[j]->wonThisTurn(a[j]);
if(k<=1)
{
rtn[j]=1;
winBars++;
stuff->barCompare[j]=true;
winpeeps+=a[j];
if(k<min)
{
min=k;
minAt=j;
}
}
else
{
rtn[j]=0;
stuff->barCompare[j]=false;
}
}
STM[2]=STM[1];
STM[1]=STM[0];
STM[0]=minAt;
stuff->numWinBars=winBars;
stuff->numWinners=(int)winpeeps;
return rtn;
}
/* Precondition: The array that holds valid bars for the current simulation has been populated
Postcondition: stuff has been populated
Description: Function goes through the population and tells an agent they have either won or
lost the current round. Also populates Stuff structure to pass back to the GUI for graph output
*/
void Town::tellWinners(int a[256]) {
int i,j;
int* k=(int*)malloc(sizeof(int));
int q=0;
int h=0;
double max=0;
double average=0;
double minmax=200;
int minmaxat=0;
Strategy ** temp;
list<Group*>::iterator it;
for ( it=groups.begin() ; it != groups.end(); it++ )
{
temp=(*it)->tellWins(a,STI,k); //returns array of graph data
for(i=0;i<*k;i++)
{
average+=temp[i]->getScore();
h++;
if(q<20)
{
topStrats[q]=temp[i];
if(temp[i]->getScore()<minmax)
{
minmax=temp[i]->getScore();
minmaxat=q;
}
q++;
}
else
{
if(temp[i]->getScore()<minmax)
{
topStrats[minmaxat]=temp[i];
minmax=200;
minmaxat=0;
for(j=0;j<20;j++)
{
if(topStrats[j]->getScore()<minmax)
{
minmax=temp[i]->getScore();
minmaxat=j;
}
}
}
}
if(temp[i]->getScore()>max)
{
max=temp[i]->getScore();
}
}
}
average/=h;
stuff->avgStratScore=average;
stuff->bestStratScore=max;
}
Town::~Town()
{
int i;
for(i=0;i<numbars;i++)
{
delete barnums[i];
}
free(barnums);
free(topStrats);
}