-
Notifications
You must be signed in to change notification settings - Fork 0
/
rice.py
1255 lines (1113 loc) · 43.1 KB
/
rice.py
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
"""
Regional Integrated model of Climate and the Economy (RICE)
"""
import logging
import os
import sys
import numpy as np
from gym.spaces import MultiDiscrete
_PUBLIC_REPO_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path = [_PUBLIC_REPO_DIR] + sys.path
from rice_helpers import (
get_abatement_cost,
get_armington_agg,
get_aux_m,
get_capital,
get_capital_depreciation,
get_carbon_intensity,
get_consumption,
get_damages,
get_exogenous_emissions,
get_global_carbon_mass,
get_global_temperature,
get_gross_output,
get_investment,
get_labor,
get_land_emissions,
get_max_potential_exports,
get_mitigation_cost,
get_production,
get_production_factor,
get_social_welfare,
get_utility,
set_rice_params,
)
# Set logger level e.g., DEBUG, INFO, WARNING, ERROR.
logging.getLogger().setLevel(logging.ERROR)
_FEATURES = "features"
_ACTION_MASK = "action_mask"
class Rice:
"""
TODO : write docstring for RICE
Rice class. Includes all regions, interactions, etc.
Is initialized based on yaml file.
etc...
"""
name = "Rice"
def __init__(
self,
num_discrete_action_levels=10, # the number of discrete levels for actions, > 1
negotiation_on=False, # If True then negotiation is on, else off
):
"""TODO : init docstring"""
assert (
num_discrete_action_levels > 1
), "the number of action levels should be > 1."
self.num_discrete_action_levels = num_discrete_action_levels
self.negotiation_on = negotiation_on
self.float_dtype = np.float32
self.int_dtype = np.int32
# Constants
params, num_regions = set_rice_params(
os.path.join(_PUBLIC_REPO_DIR, "region_yamls"),
)
# TODO : add to yaml
self.balance_interest_rate = 0.1
self.num_regions = num_regions
self.rice_constant = params["_RICE_CONSTANT"]
self.dice_constant = params["_DICE_CONSTANT"]
self.all_constants = self.concatenate_world_and_regional_params(
self.dice_constant, self.rice_constant
)
# TODO: rename constans[0] to dice_constants?
self.start_year = self.all_constants[0]["xt_0"]
self.end_year = (
self.start_year
+ self.all_constants[0]["xDelta"] * self.all_constants[0]["xN"]
)
# These will be set in reset (see below)
self.current_year = None # current year in the simulation
self.timestep = None # episode timestep
self.activity_timestep = None # timestep pertaining to the activity stage
# Parameters for Armington aggregation
# TODO : add to yaml
self.sub_rate = 0.5
self.dom_pref = 0.5
self.for_pref = [0.5 / (self.num_regions - 1)] * self.num_regions
# Typecasting
self.sub_rate = np.array([self.sub_rate]).astype(self.float_dtype)
self.dom_pref = np.array([self.dom_pref]).astype(self.float_dtype)
self.for_pref = np.array(self.for_pref, dtype=self.float_dtype)
# Define env global state
# These will be initialized at reset (see below)
self.global_state = {}
# Define the episode length
self.episode_length = self.dice_constant["xN"]
# Defining observation and action spaces
self.observation_space = None # This will be set via the env_wrapper (in utils)
# Notation nvec: vector of counts of each categorical variable
# Each region sets mitigation and savings rates
self.savings_action_nvec = [self.num_discrete_action_levels]
self.mitigation_rate_action_nvec = [self.num_discrete_action_levels]
# Each region sets max allowed export from own region
self.export_action_nvec = [self.num_discrete_action_levels]
# Each region sets import bids (max desired imports from other countries)
self.import_actions_nvec = [self.num_discrete_action_levels] * self.num_regions
# Each region sets import tariffs imposed on other countries
self.tariff_actions_nvec = [self.num_discrete_action_levels] * self.num_regions
self.actions_nvec = (
self.savings_action_nvec
+ self.mitigation_rate_action_nvec
+ self.export_action_nvec
+ self.import_actions_nvec
+ self.tariff_actions_nvec
)
# Negotiation-related initializations
if self.negotiation_on:
self.stage = 0
self.num_negotiation_stages = 2 # proposal and evaluation steps
self.episode_length += (
self.dice_constant["xN"] * self.num_negotiation_stages
)
# Each region proposes to each other region
# self mitigation and their mitigation values
self.proposal_actions_nvec = (
[self.num_discrete_action_levels] * 2 * self.num_regions
)
# Each region evaluates a proposal from every other region,
# either accept or reject.
self.evaluation_actions_nvec = [2] * self.num_regions
self.actions_nvec += (
self.proposal_actions_nvec + self.evaluation_actions_nvec
)
# Set the env action space
self.action_space = {
region_id: MultiDiscrete(self.actions_nvec)
for region_id in range(self.num_regions)
}
# Set the default action mask (all ones)
self.len_actions = sum(self.actions_nvec)
self.default_agent_action_mask = np.ones(self.len_actions, dtype=self.int_dtype)
# Add num_agents attribute (for use with WarpDrive)
self.num_agents = self.num_regions
def reset(self):
"""
Reset the environment
"""
self.timestep = 0
self.activity_timestep = 0
self.current_year = self.start_year
constants = self.all_constants
self.set_global_state(
key="global_temperature",
value=np.array(
[constants[0]["xT_AT_0"], constants[0]["xT_LO_0"]],
),
timestep=self.timestep,
norm=1e1,
)
self.set_global_state(
key="global_carbon_mass",
value=np.array(
[
constants[0]["xM_AT_0"],
constants[0]["xM_UP_0"],
constants[0]["xM_LO_0"],
],
),
timestep=self.timestep,
norm=1e4,
)
self.set_global_state(
key="capital_all_regions",
value=np.array(
[constants[region_id]["xK_0"] for region_id in range(self.num_regions)]
),
timestep=self.timestep,
norm=1e4,
)
self.set_global_state(
key="labor_all_regions",
value=np.array(
[constants[region_id]["xL_0"] for region_id in range(self.num_regions)]
),
timestep=self.timestep,
norm=1e4,
)
self.set_global_state(
key="production_factor_all_regions",
value=np.array(
[constants[region_id]["xA_0"] for region_id in range(self.num_regions)]
),
timestep=self.timestep,
norm=1e2,
)
self.set_global_state(
key="intensity_all_regions",
value=np.array(
[
constants[region_id]["xsigma_0"]
for region_id in range(self.num_regions)
]
),
timestep=self.timestep,
norm=1e-1,
)
for key in [
"global_exogenous_emissions",
"global_land_emissions",
]:
self.set_global_state(
key=key,
value=np.zeros(
1,
),
timestep=self.timestep,
)
self.set_global_state(
"timestep", self.timestep, self.timestep, dtype=self.int_dtype, norm=1e2
)
self.set_global_state(
"activity_timestep",
self.activity_timestep,
self.timestep,
dtype=self.int_dtype,
)
for key in [
"capital_depreciation_all_regions",
"savings_all_regions",
"mitigation_rate_all_regions",
"max_export_limit_all_regions",
"mitigation_cost_all_regions",
"damages_all_regions",
"abatement_cost_all_regions",
"utility_all_regions",
"social_welfare_all_regions",
"reward_all_regions",
]:
self.set_global_state(
key=key,
value=np.zeros(
self.num_regions,
),
timestep=self.timestep,
)
for key in [
"consumption_all_regions",
"current_balance_all_regions",
"gross_output_all_regions",
"investment_all_regions",
"production_all_regions",
]:
self.set_global_state(
key=key,
value=np.zeros(
self.num_regions,
),
timestep=self.timestep,
norm=1e3,
)
for key in [
"tariffs",
"future_tariffs",
"scaled_imports",
"desired_imports",
"tariffed_imports",
]:
self.set_global_state(
key=key,
value=np.zeros((self.num_regions, self.num_regions)),
timestep=self.timestep,
norm=1e2,
)
# Negotiation-related features
self.set_global_state(
key="stage",
value=np.zeros(1),
timestep=self.timestep,
dtype=self.int_dtype,
)
self.set_global_state(
key="minimum_mitigation_rate_all_regions",
value=np.zeros(self.num_regions),
timestep=self.timestep,
)
for key in [
"promised_mitigation_rate",
"requested_mitigation_rate",
"proposal_decisions",
]:
self.set_global_state(
key=key,
value=np.zeros((self.num_regions, self.num_regions)),
timestep=self.timestep,
)
return self.generate_observation()
def step(self, actions=None):
"""
The environment step function.
If negotiation is enabled, it also comprises
the proposal and evaluation steps.
"""
# Increment timestep
self.timestep += 1
# Carry over the previous global states to the current timestep
for key in self.global_state:
if key != "reward_all_regions":
self.global_state[key]["value"][self.timestep] = self.global_state[key][
"value"
][self.timestep - 1].copy()
self.set_global_state(
"timestep", self.timestep, self.timestep, dtype=self.int_dtype
)
if self.negotiation_on:
# Note: The '+1` below is for the climate_and_economy_simulation_step
self.stage = self.timestep % (self.num_negotiation_stages + 1)
self.set_global_state(
"stage", self.stage, self.timestep, dtype=self.int_dtype
)
if self.stage == 1:
return self.proposal_step(actions)
if self.stage == 2:
return self.evaluation_step(actions)
return self.climate_and_economy_simulation_step(actions)
def generate_observation(self):
"""
Generate observations for each agent by concatenating global, public
and private features.
The observations are returned as a dictionary keyed by region index.
Each dictionary contains the features as well as the action mask.
"""
# Observation array features
# Global features that are observable by all regions
global_features = [
"global_temperature",
"global_carbon_mass",
"global_exogenous_emissions",
"global_land_emissions",
"timestep",
]
# Public features that are observable by all regions
public_features = [
"capital_all_regions",
"capital_depreciation_all_regions",
"labor_all_regions",
"gross_output_all_regions",
"investment_all_regions",
"consumption_all_regions",
"savings_all_regions",
"mitigation_rate_all_regions",
"max_export_limit_all_regions",
"current_balance_all_regions",
"tariffs",
]
# Private features that are private to each region.
private_features = [
"production_factor_all_regions",
"intensity_all_regions",
"mitigation_cost_all_regions",
"damages_all_regions",
"abatement_cost_all_regions",
"production_all_regions",
"utility_all_regions",
"social_welfare_all_regions",
"reward_all_regions",
]
# Features concerning two regions
bilateral_features = []
# Negotiation-specific features
if self.negotiation_on:
global_features += ["stage"]
public_features += []
private_features += [
"minimum_mitigation_rate_all_regions",
]
bilateral_features += [
"promised_mitigation_rate",
"requested_mitigation_rate",
"proposal_decisions",
]
shared_features = np.array([])
for feature in global_features + public_features:
shared_features = np.append(
shared_features,
self.flatten_array(
self.global_state[feature]["value"][self.timestep]
/ self.global_state[feature]["norm"]
),
)
# Form the feature dictionary, keyed by region_id.
features_dict = {}
for region_id in range(self.num_regions):
# Add a region indicator array to the observation
region_indicator = np.zeros(self.num_regions, dtype=self.float_dtype)
region_indicator[region_id] = 1
all_features = np.append(region_indicator, shared_features)
for feature in private_features:
assert self.global_state[feature]["value"].shape[1] == self.num_regions
all_features = np.append(
all_features,
self.flatten_array(
self.global_state[feature]["value"][self.timestep, region_id]
/ self.global_state[feature]["norm"]
),
)
for feature in bilateral_features:
assert self.global_state[feature]["value"].shape[1] == self.num_regions
assert self.global_state[feature]["value"].shape[2] == self.num_regions
all_features = np.append(
all_features,
self.flatten_array(
self.global_state[feature]["value"][self.timestep, region_id]
/ self.global_state[feature]["norm"]
),
)
all_features = np.append(
all_features,
self.flatten_array(
self.global_state[feature]["value"][self.timestep, :, region_id]
/ self.global_state[feature]["norm"]
),
)
features_dict[region_id] = all_features
# Fetch the action mask dictionary, keyed by region_id.
action_mask_dict = self.generate_action_mask()
# Form the observation dictionary keyed by region id.
obs_dict = {}
for region_id in range(self.num_regions):
obs_dict[region_id] = {
_FEATURES: features_dict[region_id],
_ACTION_MASK: action_mask_dict[region_id],
}
return obs_dict
def generate_action_mask(self):
"""
Generate action masks.
"""
mask_dict = {region_id: None for region_id in range(self.num_regions)}
for region_id in range(self.num_regions):
mask = self.default_agent_action_mask.copy()
if self.negotiation_on:
minimum_mitigation_rate = int(round(
self.global_state["minimum_mitigation_rate_all_regions"]["value"][
self.timestep, region_id
]
* self.num_discrete_action_levels
))
mitigation_mask = np.array(
[0 for _ in range(minimum_mitigation_rate)]
+ [
1
for _ in range(
self.num_discrete_action_levels - minimum_mitigation_rate
)
]
)
mask_start = sum(self.savings_action_nvec)
mask_end = mask_start + sum(self.mitigation_rate_action_nvec)
mask[mask_start:mask_end] = mitigation_mask
mask_dict[region_id] = mask
return mask_dict
def proposal_step(self, actions=None):
"""
Update Proposal States and Observations using proposal actions
Update Stage to 1 - Evaluation
"""
assert self.negotiation_on
assert self.stage == 1
assert isinstance(actions, dict)
assert len(actions) == self.num_regions
action_offset_index = len(
self.savings_action_nvec
+ self.mitigation_rate_action_nvec
+ self.export_action_nvec
+ self.import_actions_nvec
+ self.tariff_actions_nvec
)
num_proposal_actions = len(self.proposal_actions_nvec)
m1_all_regions = [
actions[region_id][
action_offset_index : action_offset_index + num_proposal_actions : 2
]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
]
m2_all_regions = [
actions[region_id][
action_offset_index + 1 : action_offset_index + num_proposal_actions : 2
]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
]
self.set_global_state(
"promised_mitigation_rate", np.array(m1_all_regions), self.timestep
)
self.set_global_state(
"requested_mitigation_rate", np.array(m2_all_regions), self.timestep
)
obs = self.generate_observation()
rew = {region_id: 0.0 for region_id in range(self.num_regions)}
done = {"__all__": 0}
info = {}
return obs, rew, done, info
def evaluation_step(self, actions=None):
"""
Update minimum mitigation rates
"""
assert self.negotiation_on
assert self.stage == 2
assert isinstance(actions, dict)
assert len(actions) == self.num_regions
action_offset_index = len(
self.savings_action_nvec
+ self.mitigation_rate_action_nvec
+ self.export_action_nvec
+ self.import_actions_nvec
+ self.tariff_actions_nvec
+ self.proposal_actions_nvec
)
num_evaluation_actions = len(self.evaluation_actions_nvec)
proposal_decisions = np.array(
[
actions[region_id][
action_offset_index : action_offset_index + num_evaluation_actions
]
for region_id in range(self.num_regions)
]
)
# Force set the evaluation for own proposal to reject
for region_id in range(self.num_regions):
proposal_decisions[region_id, region_id] = 0
self.set_global_state("proposal_decisions", proposal_decisions, self.timestep)
for region_id in range(self.num_regions):
outgoing_accepted_mitigation_rates = [
self.global_state["promised_mitigation_rate"]["value"][
self.timestep, region_id, j
]
* self.global_state["proposal_decisions"]["value"][
self.timestep, j, region_id
]
for j in range(self.num_regions)
]
incoming_accepted_mitigation_rates = [
self.global_state["requested_mitigation_rate"]["value"][
self.timestep, j, region_id
]
* self.global_state["proposal_decisions"]["value"][
self.timestep, region_id, j
]
for j in range(self.num_regions)
]
self.global_state["minimum_mitigation_rate_all_regions"]["value"][
self.timestep, region_id
] = max(
outgoing_accepted_mitigation_rates + incoming_accepted_mitigation_rates
)
obs = self.generate_observation()
rew = {region_id: 0.0 for region_id in range(self.num_regions)}
done = {"__all__": 0}
info = {}
return obs, rew, done, info
def climate_and_economy_simulation_step(self, actions=None):
"""
The step function for the climate and economy simulation.
PLEASE DO NOT MODIFY THE CODE BELOW.
These functions dictate the dynamics of the climate and economy simulation,
and should not be altered hence.
"""
self.activity_timestep += 1
self.set_global_state(
key="activity_timestep",
value=self.activity_timestep,
timestep=self.timestep,
dtype=self.int_dtype,
)
if self.negotiation_on:
assert self.stage == 0
else:
assert self.timestep == self.activity_timestep
assert isinstance(actions, dict)
assert len(actions) == self.num_regions
# add actions to global state
savings_action_index = 0
mitigation_rate_action_index = savings_action_index + len(
self.savings_action_nvec
)
export_action_index = mitigation_rate_action_index + len(
self.mitigation_rate_action_nvec
)
tariffs_action_index = export_action_index + len(self.export_action_nvec)
desired_imports_action_index = tariffs_action_index + len(
self.tariff_actions_nvec
)
self.set_global_state(
"savings_all_regions",
[
actions[region_id][savings_action_index]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
],
self.timestep,
)
self.set_global_state(
"mitigation_rate_all_regions",
[
actions[region_id][mitigation_rate_action_index]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
],
self.timestep,
)
self.set_global_state(
"max_export_limit_all_regions",
[
actions[region_id][export_action_index]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
],
self.timestep,
)
self.set_global_state(
"future_tariffs",
[
actions[region_id][
tariffs_action_index : tariffs_action_index + self.num_regions
]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
],
self.timestep,
)
self.set_global_state(
"desired_imports",
[
actions[region_id][
desired_imports_action_index : desired_imports_action_index
+ self.num_regions
]
/ self.num_discrete_action_levels
for region_id in range(self.num_regions)
],
self.timestep,
)
# Constants
constants = self.all_constants
const = constants[0]
aux_m_all_regions = np.zeros(self.num_regions, dtype=self.float_dtype)
prev_global_temperature = self.get_global_state(
"global_temperature", self.timestep - 1
)
t_at = prev_global_temperature[0]
# add emissions to global state
global_exogenous_emissions = get_exogenous_emissions(
const["xf_0"], const["xf_1"], const["xt_f"], self.activity_timestep
)
global_land_emissions = get_land_emissions(
const["xE_L0"], const["xdelta_EL"], self.activity_timestep, self.num_regions
)
self.set_global_state(
"global_exogenous_emissions", global_exogenous_emissions, self.timestep
)
self.set_global_state(
"global_land_emissions", global_land_emissions, self.timestep
)
desired_imports = self.get_global_state("desired_imports")
scaled_imports = self.get_global_state("scaled_imports")
for region_id in range(self.num_regions):
# Actions
savings = self.get_global_state("savings_all_regions", region_id=region_id)
mitigation_rate = self.get_global_state(
"mitigation_rate_all_regions", region_id=region_id
)
# feature values from previous timestep
intensity = self.get_global_state(
"intensity_all_regions", timestep=self.timestep - 1, region_id=region_id
)
production_factor = self.get_global_state(
"production_factor_all_regions",
timestep=self.timestep - 1,
region_id=region_id,
)
capital = self.get_global_state(
"capital_all_regions", timestep=self.timestep - 1, region_id=region_id
)
labor = self.get_global_state(
"labor_all_regions", timestep=self.timestep - 1, region_id=region_id
)
gov_balance_prev = self.get_global_state(
"current_balance_all_regions",
timestep=self.timestep - 1,
region_id=region_id,
)
# constants
const = constants[region_id]
# climate costs and damages
mitigation_cost = get_mitigation_cost(
const["xp_b"],
const["xtheta_2"],
const["xdelta_pb"],
self.activity_timestep,
intensity,
)
damages = get_damages(t_at, const["xa_1"], const["xa_2"], const["xa_3"])
abatement_cost = get_abatement_cost(
mitigation_rate, mitigation_cost, const["xtheta_2"]
)
production = get_production(
production_factor,
capital,
labor,
const["xgamma"],
)
gross_output = get_gross_output(damages, abatement_cost, production)
gov_balance_prev = gov_balance_prev * (1 + self.balance_interest_rate)
investment = get_investment(savings, gross_output)
for j in range(self.num_regions):
scaled_imports[region_id][j] = (
desired_imports[region_id][j] * gross_output
)
# Import bid to self is reset to zero
scaled_imports[region_id][region_id] = 0
total_scaled_imports = np.sum(scaled_imports[region_id])
if total_scaled_imports > gross_output:
for j in range(self.num_regions):
scaled_imports[region_id][j] = (
scaled_imports[region_id][j]
/ total_scaled_imports
* gross_output
)
# Scale imports based on gov balance
init_capital_multiplier = 10.0
debt_ratio = gov_balance_prev / init_capital_multiplier * const["xK_0"]
debt_ratio = min(0.0, debt_ratio)
debt_ratio = max(-1.0, debt_ratio)
debt_ratio = np.array(debt_ratio).astype(self.float_dtype)
scaled_imports[region_id] *= 1 + debt_ratio
self.set_global_state(
"mitigation_cost_all_regions",
mitigation_cost,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"damages_all_regions", damages, self.timestep, region_id=region_id
)
self.set_global_state(
"abatement_cost_all_regions",
abatement_cost,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"production_all_regions", production, self.timestep, region_id=region_id
)
self.set_global_state(
"gross_output_all_regions",
gross_output,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"current_balance_all_regions",
gov_balance_prev,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"investment_all_regions",
investment,
self.timestep,
region_id=region_id,
)
for region_id in range(self.num_regions):
x_max = self.get_global_state(
"max_export_limit_all_regions", region_id=region_id
)
gross_output = self.get_global_state(
"gross_output_all_regions", region_id=region_id
)
investment = self.get_global_state(
"investment_all_regions", region_id=region_id
)
# scale desired imports according to max exports
max_potential_exports = get_max_potential_exports(
x_max, gross_output, investment
)
total_desired_exports = np.sum(scaled_imports[:, region_id])
if total_desired_exports > max_potential_exports:
for j in range(self.num_regions):
scaled_imports[j][region_id] = (
scaled_imports[j][region_id]
/ total_desired_exports
* max_potential_exports
)
self.set_global_state("scaled_imports", scaled_imports, self.timestep)
# countries with negative gross output cannot import
prev_tariffs = self.get_global_state(
"future_tariffs", timestep=self.timestep - 1
)
tariffed_imports = self.get_global_state("tariffed_imports")
scaled_imports = self.get_global_state("scaled_imports")
for region_id in range(self.num_regions):
# constants
const = constants[region_id]
# get variables from global state
savings = self.get_global_state("savings_all_regions", region_id=region_id)
gross_output = self.get_global_state(
"gross_output_all_regions", region_id=region_id
)
investment = get_investment(savings, gross_output)
labor = self.get_global_state(
"labor_all_regions",
timestep=self.timestep - 1,
region_id=region_id,
)
# calculate tariffed imports, tariff revenue and budget balance
for j in range(self.num_regions):
tariffed_imports[region_id, j] = scaled_imports[region_id, j] * (
1 - prev_tariffs[region_id, j]
)
tariff_revenue = np.sum(
scaled_imports[region_id, :] * prev_tariffs[region_id, :]
)
# Aggregate consumption from domestic and foreign goods
# domestic consumption
c_dom = get_consumption(gross_output, investment, exports=scaled_imports[:, region_id])
consumption = get_armington_agg(
c_dom=c_dom,
c_for=tariffed_imports[region_id, :], # np.array
sub_rate=self.sub_rate, # in (0,1) np.array
dom_pref=self.dom_pref, # in [0,1] np.array
for_pref=self.for_pref, # np.array, sums to (1 - dom_pref)
)
utility = get_utility(labor, consumption, const["xalpha"])
social_welfare = get_social_welfare(
utility, const["xrho"], const["xDelta"], self.activity_timestep
)
self.set_global_state(
"tariff_revenue", tariff_revenue, self.timestep, region_id=region_id
)
self.set_global_state(
"consumption_all_regions",
consumption,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"utility_all_regions",
utility,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"social_welfare_all_regions",
social_welfare,
self.timestep,
region_id=region_id,
)
self.set_global_state(
"reward_all_regions",
utility,
self.timestep,
region_id=region_id,
)
# Update gov balance
for region_id in range(self.num_regions):
const = constants[region_id]
gov_balance_prev = self.get_global_state(
"current_balance_all_regions", region_id=region_id
)
scaled_imports = self.get_global_state("scaled_imports")
gov_balance = gov_balance_prev + const["xDelta"] * (
np.sum(scaled_imports[:, region_id])
- np.sum(scaled_imports[region_id, :])
)
self.set_global_state(