-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto1.cpp
407 lines (306 loc) · 11.7 KB
/
proto1.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <vector>
#include "random.c"
#define NUM_EVENTS 5
#define NUM_VALUES_MANAGEMENT 4
#define BUSY 1
#define IDLE 2
#define BUYING_EVENT 2
#define SOWING_EVENT 3
#define MANAGEMENT_EVENT 4
#define HARVEST_EVENT 0
#define END_EVENT 1
#define TIME_LIMIT 1.0e+30
#define NONE_MANAGEMENT 1
#define REMOVE_WORM 2
#define LOOSEN_SOIL 3
#define BOTH_MANAGEMENT 4
#define MAX_FARMLAND 1000
#define MAX_SEED 5000
#define BUYING_INTERVAL 10.0
using namespace std;
class MyComp {
public:
bool operator() (const pair<float, int> &lhs, const pair<float, int> &rhs) {
return lhs.first > rhs.first;
}
};
using event_queue = priority_queue<pair<float, int>, vector<pair<float, int> >, MyComp>;
struct land_info {
int status;
int production;
};
land_info land_infos[MAX_FARMLAND];
int initial_inv_level, end_time, num_policies, initial_seed, initial_num_land, seed, num_land, land_cost,
growing_period, initial_production, seed_cost, sell_price,
remove_worm_cost, loosen_soil_cost, remove_worm_production, loosen_soil_production;
float sim_time, time_last_event, prob_distrib_management[NUM_VALUES_MANAGEMENT + 1],
smalls, bigs, initial_money, money, total_income;
event_queue buying_event_queue;
event_queue sowing_event_queue;
event_queue management_event_queue;
event_queue harvest_event_queue;
event_queue end_event_queue;
event_queue event_list[NUM_EVENTS] = {
harvest_event_queue,
end_event_queue,
buying_event_queue,
sowing_event_queue,
management_event_queue
};
FILE *infile, *outfile;
void initialize(void);
pair<int,int> timing(void);
void buying(void);
void sowing(int index);
void management(int index);
void harvest(int index);
void report(void);
float expon(float mean);
int random_integer(float prob_distrib []);
float uniform(float a, float b);
float lcgrand(int stream);
void lcgrandst (long zset, int stream);
long lcgrandgt (int stream);
int main() /* Main function. */
{
int i, next_event_type, index, j;
/* Open input and output files. */
infile = fopen("input.txt", "r");
outfile = fopen("output.txt", "w");
/* Read input parameters. */
fscanf(infile, "%f%d%d%d%d%d%d%d%d%d%d%d%d",
&initial_money, &initial_seed, &initial_num_land, &end_time,
&growing_period, &initial_production, &land_cost, &seed_cost, &sell_price,
&remove_worm_cost, &loosen_soil_cost, &remove_worm_production, &loosen_soil_production);
for (i = 1; i <= NUM_VALUES_MANAGEMENT; ++i)
fscanf(infile, "%f", &prob_distrib_management[i]);
fscanf(infile, "%d", &num_policies);
fprintf(outfile, " policy money seed land income\n");
for (i = 1; i <= num_policies; ++i) {
/* Read the inventory policy, and initialize the simulation. */
//fscanf(infile, "%f %f", &smalls, &bigs);
fscanf(infile, "%f", &smalls);
for (j = 1; j <= 40; j += 3) {
bigs = j;
initialize();
/* Run the simulation until it terminates after an end-simulation event
(type 3) occurs. */
do {
/* Determine the next event. */
pair<int, int> timing_ret = timing();
next_event_type = timing_ret.first;
index = timing_ret.second;
/* Update time-average statistical accumulators. */
/* Invoke the appropriate event function. */
switch (next_event_type) {
case BUYING_EVENT:
buying();
break;
case SOWING_EVENT:
sowing(index);
break;
case MANAGEMENT_EVENT:
management(index);
break;
case HARVEST_EVENT:
harvest(index);
break;
case END_EVENT:
report();
break;
default :
//printf("\nError: next_event_type has not acceptable value\n");
exit(1);
}
/* If the event just executed was not the end-simulation event (type 3),
continue simulating. Otherwise, end the simulation for the current
(s,S) pair and go on to the next pair (if any). */
} while (next_event_type != END_EVENT);
}
}
/* End the simulations. */
fclose(infile);
fclose(outfile);
return 0;
}
void initialize(void) /* Initialization function. */
{
int i;
/* Initialize the simulation clock. */
sim_time = 0.0;
/* Initialize the state variables. */
seed = initial_seed;
money = initial_money;
num_land = initial_num_land;
total_income = 0.0;
for (i=0; i<MAX_FARMLAND; ++i) {
land_infos[i].status = IDLE;
land_infos[i].production = initial_production;
}
time_last_event = 0.0;
event_list[BUYING_EVENT].push(make_pair(0.0, -1)); // -1 means "no index"
event_list[END_EVENT].push(make_pair(end_time, -1)); // -1 means "no index"
}
pair<int, int> timing(void) /* Timing function. */
{
const int no_event = -1;
const int first_event = 0;
int event_type;
int index;
int next_event_type;
float min_time_next_event = TIME_LIMIT / 10;
next_event_type = no_event;
/* Determine the event type of the next event to occur. */
for (event_type = first_event; event_type < NUM_EVENTS; ++event_type) {
event_queue cur_queue = event_list[event_type];
if (!cur_queue.empty() && cur_queue.top().first < min_time_next_event) {
min_time_next_event = cur_queue.top().first;
next_event_type = event_type;
}
}
/* Check to see whether the event list is empty. */
if (next_event_type == no_event) {
/* The event list is empty, so stop the simulation */
fprintf(outfile, "\nEvent list empty at time %f", sim_time);
exit(1);
}
/* The event list is not empty, so advance the simulation clock. */
pair<float, int> next_event = event_list[next_event_type].top(); // pair<float time, int index>
event_list[next_event_type].pop();
sim_time = next_event.first;
return make_pair(next_event_type, next_event.second);
}
void buying(void) {
float policy; //policy is #ofseeds/#oflands
float origin_money;
int index;
//while (money > 0 && origin_money != money) { // buy only one!
origin_money = money;
policy = seed/(float)num_land;
if (policy < smalls) {
int possible_max = (int)money / seed_cost;
int num_buy = bigs*num_land - seed; // number of seeds to buy
if (possible_max < num_buy) {
num_buy = possible_max;
}
seed += num_buy;
money -= seed_cost*num_buy;
if (num_buy > 0) {
//printf("sim_time: %f, Buy %d seed(s). Current money is %f\n", sim_time, num_buy, money);
}
}
else if (money > land_cost && num_land < MAX_FARMLAND) {
int num_buy_land = (int)money / land_cost;
if (num_buy_land + num_land > MAX_FARMLAND) {
num_buy_land = MAX_FARMLAND - num_land;
}
money -= land_cost * num_buy_land;
num_land += num_buy_land;
//printf("sim_time: %f, Buy a land. Current money is %f\n", sim_time, money);
}
//}
for (index=0; index<num_land; ++index) {
if (land_infos[index].status == IDLE && seed > 0) {
sowing(index);
}
}
event_list[BUYING_EVENT].push(make_pair(sim_time + BUYING_INTERVAL, -1));
return;
}
void sowing(int index) {
if (land_infos[index].status == IDLE && seed > 0) { // actually this is an duplication
//printf("sim_time: %f, sow 1 seed\n", sim_time);
land_infos[index].status = BUSY;
seed -= 1;
management(index);
event_list[HARVEST_EVENT].push(make_pair(sim_time + growing_period, index));
}
}
void management(int index) {
int management_type = random_integer(prob_distrib_management);
switch(management_type){
case NONE_MANAGEMENT: //ran is 1, nothing
//printf("sim_time: %f, Do nothing.\n", sim_time);
break;
case REMOVE_WORM: //ran is 2, remove worms
if (money >= remove_worm_cost) {
land_infos[index].production += remove_worm_production;
money -= remove_worm_cost;
//printf("sim_time: %f, Remove worms. money: %f\n", sim_time, money);
} else {
//printf("sim_time: %f, Remove worms failded. money: %f\n", sim_time, money);
}
break;
case LOOSEN_SOIL: //ran is 3, loosen soil
if (money >= loosen_soil_cost) {
land_infos[index].production += loosen_soil_production;
money -= loosen_soil_cost;
//printf("sim_time: %f, Loosen soil. money: %f\n", sim_time, money);
} else {
//printf("sim_time: %f, Loosen soil failed. money: %f\n", sim_time, money);
}
break;
case BOTH_MANAGEMENT: //ran is 4, remove worms & loosen soil
if (money >= remove_worm_cost + loosen_soil_cost) {
land_infos[index].production += remove_worm_production + loosen_soil_production;
money -= remove_worm_cost + loosen_soil_cost;
//printf("sim_time: %f, Remove worms and lossen soil. money: %f\n", sim_time, money);
} else {
//printf("sim_time: %f, Remove worms and lossen soil failed. money: %f\n", sim_time, money);
}
break;
default:
//printf("Error: Management type is invalid. mangement type: %d\n", management_type);
exit(1);
}
}
void harvest(int index) {
float income = sell_price * land_infos[index].production;
money += income;
total_income += income;
//printf("sim_time: %f, harvest. income : %d money: %f\n", sim_time, sell_price * land_infos[index].production, money);
// initialize
land_infos[index].status = IDLE;
land_infos[index].production = initial_production;
if (seed > 0) {
sowing(index);
}
}
void report(void) /* Report generator function. */
{
/* Compute and write estimates of desired measures of performance. */
fprintf(outfile, "(%5.2f, %5.2f)%15.2f%17d%17d%15.2f\n",
smalls, bigs,
money,
seed, num_land, total_income);
}
float expon(float mean) /* Exponential variate generation function. */
{
/* Return an exponential random variate with mean "mean". */
return -mean * log(lcgrand(1));
// return mean;
}
int random_integer(float prob_distrib[]) /* Random integer generation
function. */
{
int i;
float u;
/* Generate a U(0,1) random variate. */
u = lcgrand(1);
/* Return a random integer in accordance with the (cumulative) distribution
function prob_distrib. */
for (i = 1; u > prob_distrib[i]; ++i)
;
//return 2; // use no random
return i;
}
float uniform(float a, float b) /* Uniform variate generation function. */
{
/* Return a U(a,b) random variate. */
//return a + lcgrand(1) * (b - a);
return (a + b) / 2;
}