-
Notifications
You must be signed in to change notification settings - Fork 27
/
rice_step.cu
2410 lines (2239 loc) · 86.8 KB
/
rice_step.cu
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2022, salesforce.com, inc and MILA.
// All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
// For full license text, see the LICENSE file in the repo root
// or https://opensource.org/licenses/BSD-3-Clause
__constant__ float kSmallNum = 1.0e-0;
extern "C"
{
// Device helper functions for the environment dynamics
__device__ float get_mitigation_cost(
float p_b,
float theta_2,
float delta_pb,
int timestep,
float intensity)
{
return p_b /
(1000 * theta_2) *
pow(1 - delta_pb, timestep - 1) *
intensity;
}
__device__ float get_exogenous_emissions(
float f_0,
float f_1,
int t_f,
int timestep)
{
return f_0 + min(f_1 - f_0, (f_1 - f_0) / t_f * (timestep - 1));
}
__device__ float get_land_emissions(
float e_l0,
float delta_el,
int timestep,
int kNumAgents)
{
return e_l0 * pow(1 - delta_el, timestep - 1) / float(kNumAgents);
}
__device__ float get_production(
float production_factor_all_regions,
float capital_all_regions,
float labor_all_regions,
float gamma)
{
return production_factor_all_regions *
pow(capital_all_regions, gamma) *
pow(labor_all_regions / 1000.0, 1 - gamma);
}
__device__ float get_damages(
float t_at,
float a_1,
float a_2,
int a_3)
{
return 1 / (1 + a_1 * t_at + a_2 * pow(t_at, a_3));
}
__device__ float get_abatement_cost(
float mitigation_rate,
float mitigation_cost,
float theta_2)
{
return mitigation_cost * pow(mitigation_rate, theta_2);
}
__device__ float get_gross_output(
float damages,
float abatement_cost,
float production)
{
return damages * (1 - abatement_cost) * production;
}
__device__ float get_investment(
float savings,
float gross_output)
{
return savings * gross_output;
}
__device__ float get_consumption(
float savings,
float gross_output,
float *exports,
const int kAgentId,
const int kNumAgents)
{
float exports_total = 0.0;
for (int region_id = 0; region_id < kNumAgents; region_id++)
{
exports_total += exports[kAgentId + region_id * kNumAgents];
}
float consumption = gross_output * (1 - savings) - exports_total;
if (consumption >= 0.0)
{
return consumption;
}
else
{
return 0.0;
}
}
__device__ float get_max_potential_exports(
float x_max,
float gross_output,
float investment)
{
if (x_max * gross_output <= gross_output - investment)
{
return x_max * gross_output;
}
else
{
return gross_output - investment;
}
}
__device__ void update_global_temperature(
const float *phi_t,
float *global_temperature,
const float *b_t,
float f_2x,
float m_at,
int m_at_1750,
float global_exogenous_emissions,
const int global_temperture_len,
const int kEnvId,
const int kAgentId,
const int kNumAgents)
{
// Update global_temperature in place.
// Shapes:
// phi_t: (global_temperture_len, global_temperture_len)
// global_temperature: (num_envs, global_temperture_len)
// b_t: (global_temperture_len,)
// f_i: (1,)
const float f_i = f_2x *
logf(m_at / m_at_1750) / logf(2) +
global_exogenous_emissions;
for (int i_idx = 0; i_idx < global_temperture_len; i_idx++)
{
float intermediate_dot_prod = 0.0;
for (int j_idx = 0; j_idx < global_temperture_len; j_idx++)
{
intermediate_dot_prod +=
phi_t[i_idx * global_temperture_len + j_idx] *
global_temperature[kEnvId * global_temperture_len + j_idx];
}
const int global_temperature_idx =
kEnvId * global_temperture_len + i_idx;
global_temperature[global_temperature_idx] =
intermediate_dot_prod + b_t[i_idx] * f_i;
}
}
__device__ float get_aux_m(
float intensity,
float mitigation_rate,
float production,
float land_emissions)
{
return intensity *
(1 - mitigation_rate) *
production +
land_emissions;
}
__device__ void update_global_carbon_mass(
const float *phi_m,
float *global_carbon_mass,
const float *b_m,
float sum_aux_ms,
const int carbon_mass_array_len,
const int kEnvId,
const int kAgentId,
const int kNumAgents)
{
// Update global_carbon_mass in-place.
// Shapes:
// phi_m: carbon_mass_array_len, carbon_mass_array_len)
// global_carbon_mass: (num_envs, num_agents, carbon_mass_array_len)
// b_t: carbon_mass_array_len,)
// sum_aux_ms: (1,)
// global_carbon_mass: (num_envs, num_agents, carbon_mass_array_len)
for (int i_idx = 0; i_idx < carbon_mass_array_len; i_idx++)
{
float intermediate_dot_prod = 0.0;
for (int j_idx = 0; j_idx < carbon_mass_array_len; j_idx++)
{
intermediate_dot_prod +=
phi_m[i_idx * carbon_mass_array_len + j_idx] *
global_carbon_mass[kEnvId * carbon_mass_array_len + j_idx];
}
const int global_carbon_mass_idx =
kEnvId * carbon_mass_array_len + i_idx;
global_carbon_mass[global_carbon_mass_idx] =
intermediate_dot_prod +
b_m[i_idx] * sum_aux_ms;
}
}
__device__ float get_capital(
float capital_depreciation,
float capital,
float delta,
float investment)
{
return capital_depreciation * capital +
delta * investment;
}
__device__ float get_labor(
float labor,
float l_a,
float l_g)
{
return labor * pow((1 + l_a) / (1 + labor), l_g);
}
__device__ float get_production_factor(
float production_factor,
float g_a,
float delta_a,
float delta,
int timestep)
{
return production_factor * (exp(0.0033) +
g_a * exp(-delta_a * delta * (timestep - 1)));
}
__device__ float get_carbon_intensity(
float intensity,
float g_sigma,
float delta_sigma,
float delta,
int timestep)
{
return intensity *
exp(-g_sigma * pow(1 - delta_sigma, delta * (timestep - 1)) * delta);
}
__device__ float get_utility(
float labor,
float consumption,
float alpha)
{
return (labor / 1000.0) * (pow(consumption / (labor / 1000.0) + kSmallNum, 1 - alpha) - 1) /
(1 - alpha);
}
__device__ float get_social_welfare(
float utility,
float rho,
float delta,
int timestep)
{
return utility / pow(1 + rho, delta * timestep);
}
// Consumption aggregation for trade
__device__ float get_armington_agg(
float c_dom,
float *c_for,
const float *sub_rate,
const float *dom_pref,
const float *for_pref,
const int kEnvId,
const int kAgentId,
const int kNumAgents)
{
// assert(0 < sub_rate[0] && sub_rate[0] < 1);
// assert(0 <= dom_pref[0] && dom_pref[0] <= 1);
int c_for_index_offset = kEnvId * kNumAgents * kNumAgents +
kAgentId * kNumAgents;
float c_dom_pref = dom_pref[0] * pow(c_dom, sub_rate[0]);
float c_for_pref = 0.0;
for (int region_id = 0; region_id < kNumAgents; region_id++)
{
c_for_pref += for_pref[region_id] *
pow(
c_for[c_for_index_offset + region_id],
sub_rate[0]);
}
float c_agg = pow(c_dom_pref + c_for_pref, 1.0 / sub_rate[0]);
return c_agg;
}
// // Generate action masks
__device__ void CudaGenerateActionMasks(
const int kEnvId,
const int kAgentId,
const int kNumAgents,
const int kAgentArrayIdx,
const int kActionLen,
const int kNumSavingsactions,
const int kNumDiscreteActionLevels,
const bool kNegotiationOn,
int *action_masks_arr,
float *minimum_mitigation_rate_all_regions)
{
if (kNegotiationOn)
{
// Currently, the masks are only updated
// when negotiation is turned on.
// Otherwise, we use the default (all-ones) mask.
const int mask_arr_idx_offset = kEnvId * kNumAgents * kActionLen +
kAgentId * kActionLen +
kNumSavingsactions *
kNumDiscreteActionLevels;
int minimum_mitigation_rate =
lroundf(minimum_mitigation_rate_all_regions[kAgentArrayIdx] * kNumDiscreteActionLevels);
for (int idx = 0; idx < kNumDiscreteActionLevels; idx++)
{
action_masks_arr[mask_arr_idx_offset + idx] = (idx < minimum_mitigation_rate) ? 0 : 1;
}
}
}
// Generate observation
__device__ void CudaGenerateObservation(
float *abatement_cost_all_regions,
int *activity_timestep,
float *capital_all_regions,
float *capital_depreciation_all_regions,
float *consumption_all_regions,
float *current_balance_all_regions,
float *damages_all_regions,
float *global_carbon_mass,
float *global_exogenous_emissions,
float *global_land_emissions,
float *global_temperature,
float *gross_output_all_regions,
float *intensity_all_regions,
float *investment_all_regions,
float *labor_all_regions,
float *max_export_limit_all_regions,
float *minimum_mitigation_rate_all_regions,
float *mitigation_cost_all_regions,
float *mitigation_rate_all_regions,
float *production_all_regions,
float *production_factor_all_regions,
float *promised_mitigation_rate,
float *requested_mitigation_rate,
float *proposal_decisions,
float *reward_all_regions,
float *savings_all_regions,
float *social_welfare_all_regions,
int *stage,
int *timestep,
float *tariffs,
float *utility_all_regions,
float abatement_cost_all_regions_norm,
float activity_timestep_norm,
float capital_all_regions_norm,
float capital_depreciation_all_regions_norm,
float consumption_all_regions_norm,
float current_balance_all_regions_norm,
float damages_all_regions_norm,
float global_carbon_mass_norm,
float global_exogenous_emissions_norm,
float global_land_emissions_norm,
float global_temperature_norm,
float gross_output_all_regions_norm,
float intensity_all_regions_norm,
float investment_all_regions_norm,
float labor_all_regions_norm,
float max_export_limit_all_regions_norm,
float minimum_mitigation_rate_all_regions_norm,
float mitigation_cost_all_regions_norm,
float mitigation_rate_all_regions_norm,
float production_all_regions_norm,
float production_factor_all_regions_norm,
float promised_mitigation_rate_norm,
float proposal_decisions_norm,
float requested_mitigation_rate_norm,
float reward_all_regions_norm,
float savings_all_regions_norm,
float social_welfare_all_regions_norm,
float stage_norm,
float tariffs_norm,
float timestep_norm,
float utility_all_regions_norm,
const int kNumSavingsActions,
const int kNumDiscreteActionLevels,
const bool kNegotiationOn,
float *observations_arr,
int *action_masks_arr,
const int global_temperature_len,
const int global_carbon_mass_len,
const int global_exogenous_emissions_len,
const int global_land_emissions_len,
const int kEnvId,
const int kAgentId,
const int kNumAgents,
const int kAgentArrayIdx,
const int kNumFeatures,
const int kActionLen)
{
int obs_arr_idx_offset = kEnvId * kNumAgents * kNumFeatures +
kAgentId * kNumFeatures;
// region indicator
observations_arr[obs_arr_idx_offset + kAgentId] = 1.0;
obs_arr_idx_offset += kNumAgents;
// global features
for (int idx = 0; idx < global_temperature_len; idx++)
{
observations_arr[obs_arr_idx_offset + idx] =
global_temperature[kEnvId * global_temperature_len + idx] /
global_temperature_norm;
}
obs_arr_idx_offset += global_temperature_len;
for (int idx = 0; idx < global_carbon_mass_len; idx++)
{
observations_arr[obs_arr_idx_offset + idx] =
global_carbon_mass[kEnvId * global_carbon_mass_len + idx] /
global_carbon_mass_norm;
}
obs_arr_idx_offset += global_carbon_mass_len;
for (int idx = 0; idx < global_exogenous_emissions_len; idx++)
{
observations_arr[obs_arr_idx_offset + idx] =
global_exogenous_emissions[kEnvId * global_exogenous_emissions_len + idx] /
global_exogenous_emissions_norm;
}
obs_arr_idx_offset += global_exogenous_emissions_len;
for (int idx = 0; idx < global_land_emissions_len; idx++)
{
observations_arr[obs_arr_idx_offset + idx] =
global_land_emissions[kEnvId * global_land_emissions_len + idx] /
global_land_emissions_norm;
}
obs_arr_idx_offset += global_land_emissions_len;
observations_arr[obs_arr_idx_offset] = timestep[kEnvId] /
timestep_norm;
obs_arr_idx_offset += 1;
if (kNegotiationOn)
{
observations_arr[obs_arr_idx_offset] = stage[kEnvId] / stage_norm;
obs_arr_idx_offset += 1;
}
// public features
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
capital_all_regions[feature_idx] / capital_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
capital_depreciation_all_regions[feature_idx] /
capital_depreciation_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
labor_all_regions[feature_idx] / labor_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
gross_output_all_regions[feature_idx] /
gross_output_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
investment_all_regions[feature_idx] /
investment_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
consumption_all_regions[feature_idx] /
consumption_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
savings_all_regions[feature_idx] /
savings_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
mitigation_rate_all_regions[feature_idx] /
mitigation_rate_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
max_export_limit_all_regions[feature_idx] /
max_export_limit_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
current_balance_all_regions[feature_idx] /
current_balance_all_regions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int i = 0; i < kNumAgents; i++)
{
for (int j = 0; j < kNumAgents; j++)
{
int tariff_idx = i * kNumAgents + j;
observations_arr[obs_arr_idx_offset + tariff_idx] =
tariffs[kEnvId * kNumAgents * kNumAgents + tariff_idx] /
tariffs_norm;
}
}
obs_arr_idx_offset += kNumAgents * kNumAgents;
// private features
observations_arr[obs_arr_idx_offset] =
production_factor_all_regions[kAgentArrayIdx] /
production_factor_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
intensity_all_regions[kAgentArrayIdx] /
intensity_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
mitigation_cost_all_regions[kAgentArrayIdx] /
mitigation_cost_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
damages_all_regions[kAgentArrayIdx] /
damages_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
abatement_cost_all_regions[kAgentArrayIdx] /
abatement_cost_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
production_all_regions[kAgentArrayIdx] /
production_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
utility_all_regions[kAgentArrayIdx] /
utility_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
social_welfare_all_regions[kAgentArrayIdx] /
social_welfare_all_regions_norm;
obs_arr_idx_offset += 1;
observations_arr[obs_arr_idx_offset] =
reward_all_regions[kAgentArrayIdx] /
reward_all_regions_norm;
obs_arr_idx_offset += 1;
if (kNegotiationOn)
{
// private features
observations_arr[obs_arr_idx_offset] =
minimum_mitigation_rate_all_regions[kAgentArrayIdx] /
minimum_mitigation_rate_all_regions_norm;
obs_arr_idx_offset += 1;
// bilateral features
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
kAgentId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
promised_mitigation_rate[feature_idx] /
promised_mitigation_rate_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
idx * kNumAgents + kAgentId;
observations_arr[obs_arr_idx_offset + idx] =
promised_mitigation_rate[feature_idx] /
promised_mitigation_rate_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
kAgentId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
requested_mitigation_rate[feature_idx] /
requested_mitigation_rate_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
idx * kNumAgents + kAgentId;
observations_arr[obs_arr_idx_offset + idx] =
requested_mitigation_rate[feature_idx] /
requested_mitigation_rate_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
kAgentId * kNumAgents + idx;
observations_arr[obs_arr_idx_offset + idx] =
proposal_decisions[feature_idx] /
proposal_decisions_norm;
}
obs_arr_idx_offset += kNumAgents;
for (int idx = 0; idx < kNumAgents; idx++)
{
int feature_idx = kEnvId * kNumAgents * kNumAgents +
idx * kNumAgents + kAgentId;
observations_arr[obs_arr_idx_offset + idx] =
proposal_decisions[feature_idx] /
proposal_decisions_norm;
}
obs_arr_idx_offset += kNumAgents;
}
CudaGenerateActionMasks(
kEnvId,
kAgentId,
kNumAgents,
kAgentArrayIdx,
kActionLen,
kNumSavingsActions,
kNumDiscreteActionLevels,
kNegotiationOn,
action_masks_arr,
minimum_mitigation_rate_all_regions);
}
__device__ void CudaProposalStep(
float *abatement_cost_all_regions,
int *activity_timestep,
float *capital_all_regions,
float *capital_depreciation_all_regions,
float *consumption_all_regions,
float *current_balance_all_regions,
float *damages_all_regions,
float *global_carbon_mass,
float *global_exogenous_emissions,
float *global_land_emissions,
float *global_temperature,
float *gross_output_all_regions,
float *intensity_all_regions,
float *investment_all_regions,
float *labor_all_regions,
float *max_export_limit_all_regions,
float *minimum_mitigation_rate_all_regions,
float *mitigation_cost_all_regions,
float *mitigation_rate_all_regions,
float *production_all_regions,
float *production_factor_all_regions,
float *promised_mitigation_rate,
float *requested_mitigation_rate,
float *proposal_decisions,
float *reward_all_regions,
float *savings_all_regions,
float *social_welfare_all_regions,
int *stage,
int *timestep,
float *tariffs,
float *utility_all_regions,
float abatement_cost_all_regions_norm,
float activity_timestep_norm,
float capital_all_regions_norm,
float capital_depreciation_all_regions_norm,
float consumption_all_regions_norm,
float current_balance_all_regions_norm,
float damages_all_regions_norm,
float global_carbon_mass_norm,
float global_exogenous_emissions_norm,
float global_land_emissions_norm,
float global_temperature_norm,
float gross_output_all_regions_norm,
float intensity_all_regions_norm,
float investment_all_regions_norm,
float labor_all_regions_norm,
float max_export_limit_all_regions_norm,
float minimum_mitigation_rate_all_regions_norm,
float mitigation_cost_all_regions_norm,
float mitigation_rate_all_regions_norm,
float production_all_regions_norm,
float production_factor_all_regions_norm,
float promised_mitigation_rate_norm,
float proposal_decisions_norm,
float requested_mitigation_rate_norm,
float reward_all_regions_norm,
float savings_all_regions_norm,
float social_welfare_all_regions_norm,
float stage_norm,
float tariffs_norm,
float timestep_norm,
float utility_all_regions_norm,
float *observations_arr,
int *action_masks_arr,
int *actions_arr,
float *rewards_arr,
int *done_arr,
int *env_timestep_arr,
const int kNumDiscreteActionLevels,
const bool kNegotiationOn,
const int global_temperature_len,
const int global_carbon_mass_len,
const int global_exogenous_emissions_len,
const int global_land_emissions_len,
const int kNumMitigationRateActions,
const int kNumSavingsActions,
const int kNumExportActions,
const int kNumImportActions,
const int kNumTariffActions,
const int kEnvId,
const int kAgentId,
const int kNumAgents,
const int kAgentArrayIdx,
const int kNumFeatures,
const int kActionLen,
const int kNumActions,
const int kEpisodeLength)
{
if (kAgentId < kNumAgents)
{
assert(kNegotiationOn);
assert(stage[kEnvId] == 1);
// Update proposal values corresponding to this Agents proposals
const int kPropArrIdxOffset = kEnvId * kNumAgents * kNumAgents +
kAgentId * kNumAgents;
const int kActionIdxOffset = kNumSavingsActions +
kNumMitigationRateActions +
kNumExportActions +
kNumImportActions +
kNumTariffActions;
const int kActionArrIdxOffset =
kEnvId * kNumAgents * kNumActions +
kAgentId * kNumActions + kActionIdxOffset;
for (int idx = 0; idx < kNumAgents; idx++)
{
promised_mitigation_rate[kPropArrIdxOffset + idx] = static_cast<float>(
actions_arr[kActionArrIdxOffset + 2 * idx + 0]) /
kNumDiscreteActionLevels;
}
for (int idx = 0; idx < kNumAgents; idx++)
{
requested_mitigation_rate[kPropArrIdxOffset + idx] = static_cast<float>(
actions_arr[kActionArrIdxOffset + 2 * idx + 1]) /
kNumDiscreteActionLevels;
}
// Wait here for all agents to update promised_mitigation_rate
// and requested_mitigation_rate.
__syncthreads();
// -------------------------------
// Generate observation
// -------------------------------
CudaGenerateObservation(
abatement_cost_all_regions,
activity_timestep,
capital_all_regions,
capital_depreciation_all_regions,
consumption_all_regions,
current_balance_all_regions,
damages_all_regions,
global_carbon_mass,
global_exogenous_emissions,
global_land_emissions,
global_temperature,
gross_output_all_regions,
intensity_all_regions,
investment_all_regions,
labor_all_regions,
max_export_limit_all_regions,
minimum_mitigation_rate_all_regions,
mitigation_cost_all_regions,
mitigation_rate_all_regions,
production_all_regions,
production_factor_all_regions,
promised_mitigation_rate,
requested_mitigation_rate,
proposal_decisions,
reward_all_regions,
savings_all_regions,
social_welfare_all_regions,
stage,
timestep,
tariffs,
utility_all_regions,
abatement_cost_all_regions_norm,
activity_timestep_norm,
capital_all_regions_norm,
capital_depreciation_all_regions_norm,
consumption_all_regions_norm,
current_balance_all_regions_norm,
damages_all_regions_norm,
global_carbon_mass_norm,
global_exogenous_emissions_norm,
global_land_emissions_norm,
global_temperature_norm,
gross_output_all_regions_norm,
intensity_all_regions_norm,
investment_all_regions_norm,
labor_all_regions_norm,
max_export_limit_all_regions_norm,
minimum_mitigation_rate_all_regions_norm,
mitigation_cost_all_regions_norm,
mitigation_rate_all_regions_norm,
production_all_regions_norm,
production_factor_all_regions_norm,
promised_mitigation_rate_norm,
proposal_decisions_norm,
requested_mitigation_rate_norm,
reward_all_regions_norm,
savings_all_regions_norm,
social_welfare_all_regions_norm,
stage_norm,
tariffs_norm,
timestep_norm,
utility_all_regions_norm,
kNumSavingsActions,
kNumDiscreteActionLevels,
kNegotiationOn,
observations_arr,
action_masks_arr,
global_temperature_len,
global_carbon_mass_len,
global_exogenous_emissions_len,
global_land_emissions_len,
kEnvId,
kAgentId,
kNumAgents,
kAgentArrayIdx,
kNumFeatures,
kActionLen);
// Update rewards array
rewards_arr[kAgentArrayIdx] = 0.0;
// Wait here for all agents before checking for the done condition
__syncthreads();
}
// Use only agent 0's thread to set done_arr
if (kAgentId == 0)
{
done_arr[kEnvId] = 0;
}
}
__device__ void CudaEvaluationStep(
float *abatement_cost_all_regions,
int *activity_timestep,
float *capital_all_regions,
float *capital_depreciation_all_regions,
float *consumption_all_regions,
float *current_balance_all_regions,
float *damages_all_regions,
float *global_carbon_mass,
float *global_exogenous_emissions,
float *global_land_emissions,
float *global_temperature,
float *gross_output_all_regions,
float *intensity_all_regions,
float *investment_all_regions,
float *labor_all_regions,
float *max_export_limit_all_regions,
float *minimum_mitigation_rate_all_regions,
float *mitigation_cost_all_regions,
float *mitigation_rate_all_regions,
float *production_all_regions,
float *production_factor_all_regions,
float *promised_mitigation_rate,
float *requested_mitigation_rate,
float *proposal_decisions,
float *reward_all_regions,
float *savings_all_regions,
float *social_welfare_all_regions,
int *stage,
int *timestep,
float *tariffs,
float *utility_all_regions,
float abatement_cost_all_regions_norm,
float activity_timestep_norm,
float capital_all_regions_norm,
float capital_depreciation_all_regions_norm,
float consumption_all_regions_norm,
float current_balance_all_regions_norm,
float damages_all_regions_norm,
float global_carbon_mass_norm,
float global_exogenous_emissions_norm,
float global_land_emissions_norm,
float global_temperature_norm,
float gross_output_all_regions_norm,
float intensity_all_regions_norm,
float investment_all_regions_norm,
float labor_all_regions_norm,
float max_export_limit_all_regions_norm,
float minimum_mitigation_rate_all_regions_norm,
float mitigation_cost_all_regions_norm,
float mitigation_rate_all_regions_norm,
float production_all_regions_norm,
float production_factor_all_regions_norm,
float promised_mitigation_rate_norm,
float proposal_decisions_norm,
float requested_mitigation_rate_norm,
float reward_all_regions_norm,
float savings_all_regions_norm,
float social_welfare_all_regions_norm,
float stage_norm,
float tariffs_norm,
float timestep_norm,
float utility_all_regions_norm,
float *observations_arr,
int *action_masks_arr,
int *actions_arr,
float *rewards_arr,
int *done_arr,
int *env_timestep_arr,
const int kNumDiscreteActionLevels,
const bool kNegotiationOn,
const int global_temperature_len,
const int global_carbon_mass_len,
const int global_exogenous_emissions_len,
const int global_land_emissions_len,
const int kNumMitigationRateActions,
const int kNumSavingsActions,
const int kNumExportActions,
const int kNumImportActions,
const int kNumTariffActions,
const int kNumProposalActions,
const int kNumEvaluationActions,
const int kEnvId,
const int kAgentId,
const int kNumAgents,
const int kAgentArrayIdx,
const int kNumFeatures,
const int kActionLen,
const int kNumActions,
const int kEpisodeLength)
{
if (kAgentId < kNumAgents)