-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra_planner.py
2218 lines (2019 loc) · 104 KB
/
infra_planner.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
""" Infra Planner ########################################################################################################
Infra Planner is a custom gym environment to plan interventions on infrastrucutre.
Developed by: Zachary Hamida
Email: [email protected]
Webpage: https://zachamida.github.io
"""
from decision_makers import decision_maker
from gym import spaces
import numpy as np
import math as mt
import copy
import scipy.linalg as sp
import scipy.io as sio
import scipy.stats as stats
import scipy.special as sc
import scipy.interpolate as interpolate
import matplotlib.pyplot as plt
from os.path import join as pjoin
class infra_planner:
def __init__(self, fixed_seed=0, infra_lvl =0):
# Analyses level
self.infra_lvl = infra_lvl
if self.infra_lvl == 0:
self.element_lvl = 1
self.category_lvl = 0
self.bridge_lvl = 0
self.network_lvl = 0 # (under construction)
elif self.infra_lvl == 1:
self.element_lvl = 0
self.category_lvl = 1
self.bridge_lvl = 0
self.network_lvl = 0 # (under construction)
elif self.infra_lvl == 2:
self.element_lvl = 0
self.category_lvl = 0
self.bridge_lvl = 1
self.network_lvl = 0 # (under construction)
else:
self.element_lvl = 1
self.category_lvl = 0
self.bridge_lvl = 0
self.network_lvl = 0 # (under construction)
# State type
self.deterministic_model = 0
# action type
self.discrete_actions = 1
# budget
self.include_budget = 0
self.max_budget = 10
self.min_budget = 0.5
# actions and observations
if self.discrete_actions == 0 :
self.action_space = spaces.Discrete(5)
elif self.discrete_actions :
self.action_space = spaces.Discrete(2)
# condition | speed | time since last action |or| variablity
if self.element_lvl:
high = np.array([100, 0] , dtype=np.float32)
low = np.array([0, -20] , dtype=np.float32)
elif self.category_lvl:
high = np.array([100, 0, 100, 100] , dtype=np.float32)
low = np.array([0, -20, 0 ,0] , dtype=np.float32)
elif self.bridge_lvl:
high = np.array([100, 0, 100] , dtype=np.float32)
low = np.array([0, -20, 0] , dtype=np.float32)
elif self.network_lvl:
# Network of bridges
high = np.array([100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 50], dtype=np.float32)
low = np.array([0, -20, 0, -20, 0, -20, 0, -20, 0, -20, 0, 0], dtype=np.float32)
if self.include_budget:
high = np.append(high, 1)
high = np.append(high, self.max_budget)
low = np.append(low, 1)
low = np.append(low, self.min_budget)
self.observation_space = spaces.Box(low, high, dtype=np.float32)
self.metadata = 'Infrastructures deterioration environment'
# functions
self.inv = np.linalg.pinv
self.svd = np.linalg.svd
# time props
self.dt = 1 # time step size
self.total_years = 100 # total time steps
# network props
# Network[birdge #1(Category #1(#Elements), Category #2(#Elements)),
# bridge #2(Category #1(#Elements), Category #2(#Elements))
# . . . . . . . . . . .
# bridge #B(Category #1(#Elements), Category #2(#Elements)) ]
self.net_data = np.array([
[[15],[2],[3],[2],[4],[3]],
[[2],[2],[2],[2],[2],[2]],
[[2],[2],[2],[2],[2],[2]],
[[2],[2],[2],[2],[2],[2]],
[[2],[2],[2],[2],[2],[2]],
])
# plotting
self.plotting = 0
# Environment Seed
self.fixed_seed = fixed_seed
self.seed = 1
# Env. Reset
# self.reset()
""" Initial Environment Run ########################################################################################################
reset : reset the env
"""
def reset(self):
#seeds = np.random.randint(0,1000)
if self.fixed_seed != 0:
np.random.seed(self.seed)
else:
np.random.seed(None)
self.budget = np.random.uniform(self.min_budget, self.max_budget)
# reset number of bridges, structural categories and elements
self.num_c = [ len(listElem) for listElem in self.net_data] # structural categories
self.num_b = self.net_data.shape[0] # number of bridges
self.num_e = self.net_data # number of structural elements
self.initial_state = np.zeros([self.num_b, np.max(self.num_c), np.max(np.max(self.num_e)),3])
# indicators
self.cb = np.array(0) # current bridge
self.cc = np.array(0) # current structural category
self.ci = np.array(0) # current structural element
self.ec = np.array(0) # element index tracker can be utlized in the state vector, also faciltates generateing a new inspector
self.struc_tracker = 0 # track the structures that have been maintained
self.worst_cat_ind = 1 # to track if the worst structural category in the bridge has been maintained or not
# inspection data
self.max_cond = np.array(100) # dynamic max health condition u_t
self.max_cond_original = copy.copy(self.max_cond) # fixed max health condition u_t
self.max_cond_decay = 0.999 # decay factor for max health condition
self.min_cond = np.array(25) # fixed min health condition
self.b_min_functional_state = [85, 15] # min. accpetable functional state of the bridge [condition, variablity in structural categories]
self.min_high_cond = 0 # track the ID of the sorted condition from min. condition to high condition
self.y = np.nan * np.zeros([self.num_c[self.cb], np.max(self.num_e[self.cb,:, 0])]) # inspection data y
self.inspection_frq = np.random.randint(2,5,self.num_b) # inspection frequency
self.inspector_std = np.array(range(0,223)) # inspector ID
self.inspector_std = np.c_[self.inspector_std, np.random.uniform(1,6,223)] # inspectors' error std.
# kenimatic model
self.F = np.array([1, 0, 0]) # observation matrix
self.A = np.array([[1, self.dt, (self.dt ** 2) / 2], [0, 1, self.dt], [0, 0, 1]]) # transition model
sigma_w = 0.005 # process error std. for the kenimatic model
self.Q = sigma_w** 2 * np.array([[(self.dt ** 5) / 20, (self.dt ** 4) / 8, (self.dt ** 3) / 6],
[(self.dt ** 4) / 8, (self.dt ** 3) / 3, (self.dt ** 2) / 2],
[(self.dt ** 3) / 6, (self.dt ** 2) / 2, self.dt]]) # Process error (covariance)
# budget model
self.A_budget = np.array([[0, 1, 1], [0, 1, 0], [0, 0, 1]]) # transition model for the budget
self.x_budget = np.array([0, 0, 0]) # vector of available budget, cost, income
self.Q_budget = 0.5 * np.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) # process error for the transition model of the budget
self.bridge_priority = np.random.uniform(0.1,0.3) # bridge priority index
# prior initial state
self.x_init = np.array([[np.random.randint(35,90), np.random.uniform(-1.5,-0.15), -0.005],
[10**2, 0.05**2, 0.001**2]]) # initial state for the structural elements in the network
self.init_var = np.array([3**2, 0.1**2, 0.005**2, 1, 1, 1]) # intiial variance for the state
# true states
self.cs = np.empty((1,6)) # elements
self.c_cs = np.empty((self.num_b, np.max(self.num_c), 3)) # structural categories
self.b_cs = np.empty((self.num_b, 3)) # bridges
self.net_cs = np.empty((1,3)) # network
# transformation function
self.n_tr = 4
self.ST = SpaceTransformation(self.n_tr,self.max_cond,self.min_cond)
# State constraits
self.SC = StateConstraints()
# State aggregation functions
self.ME = MixtureEstimate()
# estimated states
self.e_Ex = np.empty((1,6)) # elements
self.e_Var = np.empty((1,6,6)) # elements
self.c_Ex = np.empty((self.num_b,np.max(self.num_c),3)) # category
self.c_Var = np.empty((self.num_b,np.max(self.num_c),3,3)) # category
self.b_Ex = np.empty((self.num_b,3)) # bridge
self.b_Var = np.empty((self.num_b,3,3)) # bridge
self.net_Ex = np.empty((1,6)) # network
self.net_Var = np.empty((1,6,6)) # network
# initilizing the network
self.inspector = np.nan * np.zeros_like(self.num_e[self.cb, self.cc, 0])
self.current_year = 0
# initilizing the deterioration model
self.Am = sp.block_diag(self.A, np.eye(3))
self.Qm = sp.block_diag(self.Q, np.zeros(3))
self.Fm = np.append(self.F, np.zeros([1,3]))
# SSM model estimated parameters
self.sigma_v = np.random.uniform(0.85,1.1)*self.inspector_std
# interventions for Beams | Front Wall | Slabs | gaurdrail | Wing Wall | Pavement
self.int_Ex = np.array([ [[0.5,0.169,1e-2],
[8.023,0.189,1e-2],
[18.117,0.179,1e-2]],
[[0.1,0.169,1e-2],
[18.407,0.199,1e-3],
[21.28,0.495,1e-4]],
[[1.894,0.532,3.1e-4],
[11.358,0.4,1e-4],
[20.632,0.802,0.004]],
[[0.2,0.169,1e-2],
[9.354,0.499,1e-4],
[13.683,0.054,1e-4]],
[[0.3,0.169,1e-2],
[8.023,0.189,1e-2],
[18.663,0.263,1e-4]],
[[8.023,0.189,1e-2],
[20.933,0.187,2e-2],
[27.071,0.505,2e-2]]
])
self.int_var = np.array([[[8.28e-05,2.65e-07,-5.29e-08],
[2.65e-07,0.00030,1.75e-05],
[-5.29e-08,1.75e-05,0.00012]],
[[0.37,0.00087,-8.52e-05],
[0.00087,0.0048,2.62e-05],
[-8.52e-05,2.62e-05,0.00013]],
[[0.18,0.00027,-0.0017],
[0.00027,0.00029,1.45e-05],
[-0.0018,1.45e-05,0.00052]]])
# interventions true state
self.int_true_var = np.array([[10**-8, 0.05**2, 10**-10],
[2**2, 0.1**2, 10**-8],
[4**2, 0.15**2, 10**-8]])
self.int_true = np.array([ [[0.5, 0.2, 1e-2],
[7.5, 0.3, 1e-2],
[18.75, 0.4, 1e-2]],
[[0.1,0.2,1e-2],
[19,0.2,1e-3],
[20.5,0.4,1e-4]],
[[1,0.5,3.1e-4],
[12,0.4,1e-4],
[20,0.8,0.004]],
[[0.25,0.169,1e-2],
[9.0,0.5,1e-4],
[14.0,0.1,1e-4]],
[[0.25,0.18,1e-2],
[8,0.17,1e-2],
[17,0.27,1e-4]],
[[8,0.19,1e-2],
[20,0.18,2e-2],
[28,0.50,2e-2]]
])
self.int_Q = np.square(np.array([[ [1.39,0,0,0,0,0],
[0,0.01,0,0,0,0],
[0,0,0.045,0,0,0],
[0,0,0,1.39,0,0],
[0,0,0,0,0.01,0],
[0,0,0,0,0,0.045]],
[[3.533,0,0,0,0,0],
[0,0.0747,0,0,0,0],
[0,0,0.047,0,0,0],
[0,0,0,3.533,0,0],
[0,0,0,0,0.0747,0],
[0,0,0,0,0,0.047]],
[[3.768,0,0,0,0,0],
[0,0.0227,0,0,0,0],
[0,0,0.0499,0,0,0],
[0,0,0,3.768,0,0],
[0,0,0,0,0.0227,0],
[0,0,0,0,0,0.0499]]]))
data_dir ='./data'
filename = pjoin(data_dir, 'service_life.mat')
self.CDF = sio.loadmat(filename)
# RL model
# actions on elements
self.actions = np.array([0,1,2,3,4])
self.actionCardinality = self.actions.shape[0]
# actions on category
self.action_h_c = np.array([0, 1])
self.actionCardinality_h_c = self.action_h_c.shape[0]
# actions on bridge
self.action_h_b = np.array([0, 1])
self.actionCardinality_h_b = self.action_h_b.shape[0]
# solve an interval problem
self.find_interval = 1
# actions tracking
self.act_timer = 0 # tracking the frequency of the policy's actions
self.actions_count = np.zeros([self.num_b, np.max(self.num_c), self.num_e.max(), self.actionCardinality])
self.actions_hist = self.total_years * np.ones([self.num_b, np.max(self.num_c), self.num_e.max(), self.actionCardinality])
# metrics related to the agent's actions
self.cat_act_ratio = np.zeros((self.actionCardinality,np.max(self.num_c)))
self.act_timer_on = 1
# Rewards
self.shutdown_cond = 35
self.shutdown_speed = -2
self.functional_cond = 70
self.element_critical_cond = 55
self.element_critical_speed = -1.5
self.action_costs = np.array([
[0, -0.025, -0.075, -0.15, -0.40],[0, -0.075, -0.2, -0.3, -0.45],[0, -0.2, -0.3, -0.45, -0.50],
[0, -0.01, -0.05, -0.075, -0.1],[0, -0.07, -0.15, -0.3, -0.50],[0, -0.15, -0.35, -0.45, -1]
])
self.action_costs_fixed = np.array([
[0, -0.1, -0.8, -0.8, -2],[0, -0.2, -0.5, -0.8, -2],[0, -0.3, -0.6, -0.8, -2],
[0, -0.05, -0.15, -0.2, -0.4],[0, -0.2, -0.5, -0.9, -2.5],[0, -0.5, -0.5, -0.7, -1.5]
])
self.action_costs_const = np.array([350, 200, 100, 100, 100, 100])
# maitenance fund priority
self.fund_priority = np.random.rand(self.total_years+1)
self.shutdown_cost = 1 # track the maintenance shutdown of the bridge
self.prev_shutdown = 0 # track if the bridge has been previously shutdown
self.prev_action = 0 # track the type of actions
self.rewards = 0 # rewards
self.elem_rewards = 0 # rewards: elemetns
self.cat_rewards = 0 # rewards: structural category
self.bridge_rewards = 0 # rewards: bridge
self.penalty = 0 # penalty for no maitenance
self.penalty_cat = 0 # penalty for no maitenance: structural category
self.penalty_bridge = 0 # penalty for no maitenance: bridge
self.max_cost = 0
# decaying factors
self.alpha1 = 1 # condition
self.alpha2 = 1 # speed
# Performance Measures
self.compatiblity = 0
self.criticality = 0
self.compatibility_cat = 0
self.compatibility_bridge = 0
self.compatibility_net = 0
self.variablity = 0
self.e_variablity = 0
self.c_variablity = 0
self.b_variablity = 0
# plotting
self.color_ex = 'bo'
self.color_std = 'b'
self.plt_y = []
self.plt_true_c = []
self.plt_true_s = []
self.plt_Ex = []
self.plt_var = []
self.plt_Ex_dot = []
self.plt_var_dot = []
self.plt_goal = []
self.plt_R = []
self.plt_t = []
# initiation
# Agnets
DM = decision_maker(self.int_Ex, self.include_budget, self.shutdown_cond, self.discrete_actions, self.b_min_functional_state)
self.agent_element_discrete = DM.agent_element_lvl_discrete
self.agent_high_discrete = DM.agent_high_lvl_discrete
self.agent_one_element = DM.agent_one_element_lvl
self.agent_element = DM.agent_element_lvl
self.agent_category = DM.agent_high_lvl
self.agent_bridge = DM.agent_high_lvl_bridge
self.agent_network = DM.agent_high_lvl_bridge
self.plot_policymap = DM.plot_policy
self.get_initial_state()
self.initial_run()
# analyses level: network, bridge, category, element
action = 0
if self.network_lvl:
while self.current_year == 0:
if self.discrete_actions:
_, _, _, observation = self.network_action_discrete_par(action)
else:
_, _, _, observation = self.network_action_par(action)
elif self.bridge_lvl:
if self.discrete_actions:
_, _, _, observation = self.bridge_action_discrete(action)
else:
_, _, _, observation = self.bridge_action(action)
elif self.category_lvl:
if self.discrete_actions:
_, _, _, observation = self.cat_action_discrete(action)
else:
_, _, _, observation = self.cat_action(action)
elif self.element_lvl:
if self.discrete_actions:
_, _, _, observation = self.elem_action_discrete(action)
else:
_, _, _, observation = self.elem_action(action)
return observation
""" Initial Environment Run ########################################################################################################
get_initial_state : generate the initial state for structural elements
initial_run : Propagate the initial state from t = 0, to t = 1 without an intervention
"""
def get_initial_state(self):
for i in range(self.num_b):
for j in range(self.num_c[i]):
for k in range(0,self.num_e[i,j,0]):
sample_state = np.random.multivariate_normal(self.x_init[0,:],np.diag(self.x_init[1,:]))
self.initial_state[i,j,k,:] = sample_state
# initilize budget
self.x_budget[0] = self.budget
def initial_run(self):
self.cs = self.initial_state
self.e_Ex = np.concatenate([np.zeros(self.cs.shape), np.zeros(self.cs.shape)],axis = 3)
self.e_Ex[:,:,:,0:3] = self.cs * np.random.uniform(0.85,1.05)
self.e_Ex[self.e_Ex[:,:,:,1]>0, 1] = 0
self.e_Ex[:,:,:,2] = 0
e_var = np.diag(np.array(self.init_var))
self.e_Var = np.zeros([self.num_b, np.max(self.num_c), self.num_e.max(),6,6])
self.e_Var[:, :, :, :, :] = e_var
for i in range(self.num_b):
self.cb = i
for j in range(self.num_c[i]):
self.cc = j
for k in range(self.num_e[i,j,0]):
self.ci = k
self.true_action(0)
if self.deterministic_model == 0:
self.estimate_step(self.e_Ex[self.cb,self.cc,self.ci,:], self.e_Var[self.cb, self.cc, self.ci, :, :], 0)
self.category_state_update()
self.bridge_state_update()
self.network_state_update()
self.cb = 0
self.cc = 0
self.ci = 0
self.ec = self.num_e[self.cb,self.cc,0]
def step(self, action): # action is a scalar\
# automatic restart when the last year is reached
if self.current_year == self.total_years:
observation = self.reset()
reward = 0
else:
# action
if self.network_lvl:
if self.discrete_actions:
_, _, reward, observation = self.network_action_discrete(action)
else:
_, _, reward, observation = self.network_action(action)
elif self.bridge_lvl:
if self.discrete_actions:
_, _, reward, observation = self.bridge_action_discrete(action)
else:
_, _, reward, observation = self.bridge_action(action)
elif self.category_lvl:
if self.discrete_actions:
_, _, reward, observation = self.cat_action_discrete(action)
else:
_, _, reward, observation = self.cat_action(action)
elif self.element_lvl:
if self.discrete_actions:
_, _, reward, observation = self.elem_action_discrete(action)
else:
_, _, reward, observation = self.elem_action(action)
# done
done = 0
# info
info = {'e_compatibility': 1 - self.compatiblity/self.current_year, 'e_criticality': self.criticality/self.current_year,\
'c_compatibility': 1 - self.compatibility_cat/self.current_year, 'b_compatibility': 1 - self.compatibility_bridge/self.current_year,\
'n_compatibility': 1 - self.compatibility_net/self.current_year}
return observation, reward, done, info
""" Environment Actions ########################################################################################################
network_action
network_action_discrete
bridge_action
bridge_action_discrete
cat_action
cat_action_discrete
elem_action
elem_action_discrete
"""
def network_action(self, goal):
reward = 0
# prepare bridge state
state = self.state_net_prep(goal, 1)
new_goal = copy.copy(goal)
if self.deterministic_model:
state_sum = np.sum(self.b_cs[:, 0 ])
else:
state_sum = np.sum(self.b_Ex[:, 0 ])
# sort structures once
if self.deterministic_model:
min_bridge_cond = np.argsort(self.b_cs[:, 0])
else:
min_bridge_cond = np.argsort(self.b_Ex[:, 0])
for i in range(self.num_b):
# state on goal
goal_dim = 0
# element in category
self.cb = min_bridge_cond[i]
# get category goal from bridge goal
goal_bridge = self.goal_network_to_bridge(new_goal, state_sum)
self.bridge_action(goal_bridge)
#
self.network_state_update()
# collect rewards
self.get_rewards_bridge(state)
reward += self.bridge_rewards
# prepare states to check on if the goal is acheived
state_one_bridge_update = self.state_prep_3()
# transition the goal
new_goal = self.goal_transition_high(state, goal, state_one_bridge_update, goal_dim)
# after step
# budget transition
fund_pr = self.fund_priority[self.current_year]
self.update_budget(fund_pr, reward)
self.current_year += 1
self.max_cond = self.max_cond_decay * self.max_cond
self.act_timer_on = 1
self.shutdown_cost = 1
next_state = self.state_net_prep(goal, 0)
# update plotting functions
if self.plotting == 1 and self.current_year > self.inspection_frq[self.cb]:
self.plt_goal.append(goal)
self.render()
return state, goal, reward, next_state
def network_action_discrete(self, action):
if action == 0:
state, goal, reward, next_state = self.network_action(0)
else:
# prepare category state
state_ = self.state_net_prep(0, 0)
goal = self.agent_network(state_)
state, goal, reward, next_state = self.network_action(goal)
return state, goal, reward, next_state
def network_action_par(self, goal):
# fund priority
fund_pr = self.fund_priority[self.current_year]
if self.struc_tracker == 0:
if self.deterministic_model:
self.min_high_cond = np.argsort(self.b_cs[:, 0])
else:
self.min_high_cond = np.argsort(self.b_Ex[:, 0])
self.cb = self.min_high_cond[self.struc_tracker]
# reset the element tracker for each category
self.cc = 0
self.ec = self.num_e[self.cb,self.cc,0]
# prepare bridge - category state
state = self.state_net_prep(goal, 1)
self.bridge_action(goal)
#
self.bridge_state_update()
# collect rewards
reward = self.bridge_rewards
# update budget
if self.include_budget:
self.update_budget(fund_pr, reward)
# transition tracker
self.struc_tracker += 1
# move to the next structure
if self.struc_tracker < self.num_b:
self.cb = self.min_high_cond[self.struc_tracker]
else:
if self.deterministic_model:
self.min_high_cond = np.argsort(self.b_cs[:, 0])
else:
self.min_high_cond = np.argsort(self.b_Ex[:, 0])
self.cb = self.min_high_cond[0]
# next state
next_state = self.state_net_prep(None, 0)
# after step
# budget transition
if self.network_lvl and self.struc_tracker == self.num_b:
self.current_year += 1
fund_pr = self.fund_priority[self.current_year]
self.max_cond = self.max_cond_decay * self.max_cond
self.act_timer_on = 1
self.struc_tracker = 0
# update plotting functions
if self.network_lvl and self.plotting == 1 and self.current_year > self.inspection_frq[self.cb]:
self.plt_goal.append(goal)
self.render()
if self.network_lvl:
return state, goal, reward, next_state
def network_action_discrete_par(self, action):
if action == 0:
state, goal, reward, next_state = self.network_action_par(0)
else:
# prepare category state
state_ = self.state_bridge_prep_single(0, 0)
goal = self.agent_bridge(state_)
state, goal, reward, next_state = self.network_action_par(goal)
return state, goal, reward, next_state
def bridge_action(self, goal):
reward = 0
# prepare bridge state
state = self.state_bridge_prep(goal, 1)
new_goal = copy.copy(goal)
if self.deterministic_model:
state_sum = np.sum(self.c_cs[self.cb, :, 0 ])
min_cat_cond = np.argsort(self.c_cs[self.cb, :, 0])
else:
state_sum = np.sum(self.c_Ex[self.cb, :, 0 ])
min_cat_cond = np.argsort(self.c_Ex[self.cb, :, 0])
# this tracker is set to avoid any mismatch between the high-level action and the low-level action
# self.worst_cat_ind = 1
# vec_act = self.act_to_vec(goal)
for i in range(self.num_c[self.cb]):
# state on goal
goal_dim = 0
# element in category
self.cc = min_cat_cond[i]
# reset the element tracker for each category
self.ec = self.num_e[self.cb,self.cc,0]
# get category goal from bridge goal
goal_cat = self.goal_bridge_to_category(new_goal, state_sum)
self.cat_action(goal_cat)
#
self.bridge_state_update()
# collect rewards
# self.get_rewards_cat(state)
reward += self.cat_rewards
# prepare states to check on if the goal is acheived
state_one_cat_update = self.state_prep_2()
# transition the goal
new_goal = self.goal_transition_high_bridge(state, goal, state_one_cat_update, goal_dim)
# this tracker is set to avoid any mismatch between the high-level action and the low-level action
# self.worst_cat_ind = 0
# after step
# budget transition
if self.bridge_lvl:
fund_pr = self.fund_priority[self.current_year]
self.update_budget(fund_pr, reward)
self.current_year += 1
self.max_cond = self.max_cond_decay * self.max_cond
self.act_timer_on = 1
self.shutdown_cost = 1
next_state = self.state_bridge_prep(goal, 0)
# update plotting functions
if self.bridge_lvl and self.plotting == 1 and self.current_year > self.inspection_frq[self.cb]:
self.plt_goal.append(goal)
self.render()
if self.bridge_lvl:
return state, goal, reward, next_state
else:
self.bridge_rewards = reward
def bridge_action_discrete(self, action):
if action == 0:
state, goal, reward, next_state = self.bridge_action(0)
else:
# prepare category state
state_ = self.state_bridge_prep(0, 0)
goal = self.agent_bridge(state_)
state, goal, reward, next_state = self.bridge_action(goal)
return state, goal, reward, next_state
def cat_action(self, goal):
reward = 0
# prepare category state
state = self.state_category_prep(goal, 1)
goal_dim = 0
# target_cond = state[goal_dim] + goal * 75
new_goal = copy.copy(goal)
# sort elements once
if self.deterministic_model:
state_sum = np.sum(self.e_Ex[self.cb, self.cc, 0:self.num_e[self.cb,self.cc, 0], 0])
min_elem_cond = np.argsort(self.cs[self.cb, self.cc, 0:self.num_e[self.cb,self.cc, 0], 0])
else:
state_sum = np.sum(self.e_Ex[self.cb, self.cc, 0:self.num_e[self.cb,self.cc, 0], 0])
min_elem_cond = np.argsort(self.e_Ex[self.cb, self.cc, 0:self.num_e[self.cb,self.cc, 0], 0])
for i in range(self.num_e[self.cb, self.cc, 0]):
# element in category
self.ci = min_elem_cond[i]
# element prep
state_element = self.state_prep_0()
# get action from goal
#goal_elem = self.goal_category_to_element(new_goal, state_sum)
if new_goal == 0:
action = 0
else:
action = self.agent_element(state_element, self.cc)
if action == 0:
action = 1
#action = self.goal_to_action_index(goal_elem)
#if goal == 0:
# action = 0
#else:
# action = self.agent_one_element(state_element, self.cc)
# if action == 0:
# action = 1
# apply action
self.elem_action(action)
# state after action
# get updated state of the category
self.category_state_update()
# collect rewards
self.get_rewards_cat(state)
reward += self.cat_rewards
# get the category condition after one element intervention
# cat_state_update = self.state_prep_1()
state_one_elem_update = self.state_prep_1()
# transition the goal
# goal = self.goal_transition(goal_elem, state_element)
new_goal = self.goal_transition_elem(state, goal, state_one_elem_update, goal_dim)
state[0:state_one_elem_update.__len__()] = state_one_elem_update
# after step
# budget transition
if self.category_lvl:
fund_pr = self.fund_priority[self.current_year]
self.update_budget(fund_pr, reward)
self.current_year += 1
self.max_cond = self.max_cond_decay * self.max_cond
self.act_timer_on = 1
self.shutdown_cost = 1
next_state = self.state_category_prep(goal, 0)
# update plotting functions
if self.category_lvl and self.plotting == 1 and self.current_year > self.inspection_frq[self.cb]:
self.plt_goal.append(goal)
self.render()
if self.category_lvl:
return state, goal, reward, next_state
else:
self.cat_rewards = copy.copy(reward)
def cat_action_discrete(self, action):
if action == 0:
state, goal, reward, next_state = self.cat_action(action)
else:
# prepare category state
state_ = self.state_category_prep(0, 0)
goal = self.agent_category(state_, self.cc)
state, goal, reward, next_state = self.cat_action(goal)
return state, goal, reward, next_state
def elem_action(self, action):
reward = 0
# prepare category state
if self.element_lvl:
state = self.state_element_prep()
# check element compatibility
self.compatibility_action(action, 0)
# perform element action
self.true_action(action)
if self.deterministic_model == 0:
self.estimate_step(self.e_Ex[self.cb,self.cc,self.ci,:], self.e_Var[self.cb, self.cc, self.ci, :, :], action)
self.ec = self.ec - 1
# update action count
self.count_action(action)
# check element compatibility
self.compatibility_action(action, 1)
# advance time
if self.element_lvl:
fund_pr = self.fund_priority[self.current_year]
self.update_budget(fund_pr, reward)
self.current_year += 1
self.max_cond = self.max_cond_decay * self.max_cond
self.act_timer_on = 1
self.shutdown_cost = 1
# rewards
self.get_rewards_elem(action)
reward = self.elem_rewards
# update plotting functions
if self.element_lvl and self.plotting == 1 and self.current_year > self.inspection_frq[self.cb]:
self.plt_goal.append(action)
self.render()
if self.element_lvl:
next_state = self.state_element_prep()
return state, action, reward, next_state
def elem_action_discrete(self, action):
if action == 0: # no intervention
state, action, reward, next_state = self.elem_action(action)
else:
state_ = self.state_element_prep()
goal = self.agent_one_element(state_, self.cc)
action = self.goal_to_action_index(goal)
state, action, reward, next_state = self.elem_action(action)
return state, action, reward, next_state
""" - Rewards Section ########################################################################################################
get_rewards_elem : get the costs/rewards from actions on elements
get_rewards_cat : get the costs/rewards from actions on categories
get_rewards_bridge : get the costs/rewards from actions on bridges
get_rewards_sparse : obtain the final reward wind/lose
check_action_limits : determine if a penalty exist due to repeating the same action
"""
def get_rewards_elem(self,action):
self.penalty = 0
sc_act = 0
act_check = self.check_action_limits()
# shutdown cost
if self.shutdown_cost:
if action == 4:
sc_act = self.action_costs_fixed[self.cc][action] + self.action_costs[self.cc][action]
elif action == 3:
sc_act = self.action_costs_fixed[self.cc][action] + self.action_costs[self.cc][action]
elif action == 2:
sc_act = self.action_costs_fixed[self.cc][action] + self.action_costs[self.cc][action]
elif action == 1:
sc_act = self.action_costs_fixed[self.cc][action] + self.action_costs[self.cc][action]
self.shutdown_cost = 0
self.prev_action = action
else:
if self.prev_action >= action:
sc_act = 0
else:
sc_act = -1 + (self.action_costs[self.cc][action] - self.action_costs[self.cc][self.prev_action])
self.prev_action = action
if action>0 :
if self.deterministic_model:
if action == 4:
elem_cost = sc_act #+ self.action_costs[self.cc][action]
elif action == 3:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.cs[self.cb, self.cc, self.ci,0] + sc_act # 350: beams, 200: Front Wall
elif action ==2:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.cs[self.cb, self.cc, self.ci,0] + sc_act
elif action ==1:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.cs[self.cb, self.cc, self.ci,0] + sc_act
else:
if action == 4:
elem_cost = sc_act #+ self.action_costs[self.cc][action]
elif action == 3:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.e_Ex[self.cb, self.cc, self.ci,0] + sc_act # 350: beams, 200: Front Wall
elif action ==2:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.e_Ex[self.cb, self.cc, self.ci,0] + sc_act
elif action ==1:
elem_cost = self.action_costs[self.cc][action]*self.action_costs_const[self.cc]/self.e_Ex[self.cb, self.cc, self.ci,0] + sc_act
else:
elem_cost = 0
if self.deterministic_model:
if (self.cs[self.cb, self.cc, self.ci,0] < self.element_critical_cond or \
self.cs[self.cb, self.cc, self.ci,1] < self.element_critical_speed or \
(self.x_budget[0]<0 and action>0 and self.include_budget) or \
act_check or self.prev_shutdown) :
self.penalty = 1
else:
if (self.e_Ex[self.cb, self.cc, self.ci,0]<self.element_critical_cond or \
self.e_Ex[self.cb, self.cc, self.ci,1]< self.element_critical_speed or \
(self.x_budget[0]<0 and action>0 and self.include_budget) or \
act_check or self.prev_shutdown) :
self.penalty = 1
if self.penalty:
elem_cost = -1 + elem_cost + self.action_costs[self.cc][3]
self.penalty = 0
self.elem_rewards = elem_cost
def get_rewards_cat(self, state):
if self.penalty_cat != 1:
if (state[0]<=self.shutdown_cond or state[1]<=self.shutdown_speed):
self.penalty_cat = 1
# check penalty
if self.penalty_cat:
cat_cost = -1 + self.action_costs[self.cc][4]
self.penalty_cat = 0
else:
cat_cost = 0
self.cat_rewards = self.elem_rewards + cat_cost
def get_rewards_bridge(self, state):
if self.penalty_bridge ==0:
if (state[0]<=self.shutdown_cond or state[1]<-self.shutdown_speed):
self.penalty_bridge = 1
# check penalty
if self.penalty_bridge:
bridge_cost = -1 + self.action_costs[self.cc][3]
self.penalty_bridge = 0
else:
bridge_cost = 0
self.bridge_rewards = self.cat_rewards + bridge_cost
def get_rewards_sparse(self, reward_agent, reward_expert):
if reward_agent > reward_expert:
sparse_reward = 1
else:
sparse_reward = -1
return sparse_reward
def check_action_limits(self):
act_penalty = 0
if self.network_lvl:
if self.actions_count[self.cb, self.cc, self.ci, 4] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * self.num_b * 2 or \
self.actions_count[self.cb, self.cc, self.ci, 3] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * self.num_b * 4 or \
self.actions_count[self.cb, self.cc, self.ci, 2] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * self.num_b * 8 or \
self.actions_count[self.cb, self.cc, self.ci, 1] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * self.num_b * 16:
act_penalty = 1
elif self.bridge_lvl:
if self.actions_count[self.cb, self.cc, self.ci, 4] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * 2 or \
self.actions_count[self.cb, self.cc, self.ci, 3] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * 4 or \
self.actions_count[self.cb, self.cc, self.ci, 2] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * 8 or \
self.actions_count[self.cb, self.cc, self.ci, 1] > self.num_e[self.cb, self.cc, 0] * self.num_c[self.cb] * 16:
act_penalty = 1
elif self.category_lvl:
if self.actions_count[self.cb, self.cc, self.ci, 4] > self.num_e[self.cb, self.cc, 0] * 2 or \
self.actions_count[self.cb, self.cc, self.ci, 3] > self.num_e[self.cb, self.cc, 0] * 4 or \
self.actions_count[self.cb, self.cc, self.ci, 2] > self.num_e[self.cb, self.cc, 0] * 8 or \
self.actions_count[self.cb, self.cc, self.ci, 1] > self.num_e[self.cb, self.cc, 0] * 16:
act_penalty = 1
elif self.element_lvl:
if self.actions_count[self.cb, self.cc, self.ci, 4] > 2 * self.total_years/100 or \
self.actions_count[self.cb, self.cc, self.ci, 3] > 4 * self.total_years/100 or \
self.actions_count[self.cb, self.cc, self.ci, 2] > 8 * self.total_years/100 or \
self.actions_count[self.cb, self.cc, self.ci, 1] > 16 * self.total_years/100:
act_penalty = 1
return act_penalty
""" - State Propagation in Time ########################################################################################################
true_action : applies an action on the true state (relies on true_step)
true_step : advances the true state from (t) to (t+1)
estimate_step : applies an action on the deterioration state and advance the state from (t) to (t+1)
update_budget : changes the budget over time
gen_observations : generates observations from the true state
count_action : counter for time since the last action over time + actions counter
"""
# true_action : applies an action on the true state (relies on true_step)
def true_action(self, action):
stall_counter = 0
max_stall = 50
# no action
if action == 0: # do nothing
# space transformation to know min value in the transformed space
min_cond,_ = self.ST.original_to_transformed(self.min_cond)
cond_1 = self.cs[self.cb, self.cc, 0:self.num_e[self.cb,self.cc, 0], 0] <= min_cond
if self.num_e[self.cb,self.cc, 0] < self.num_e.max():
check_1 = self.cs[self.cb, self.cc, :, 0] != 0
cond_1 = np.concatenate((cond_1, check_1[self.num_e[self.cb,self.cc, 0]:]))
if cond_1[self.ci]:
self.cs[self.cb, self.cc, cond_1, 0]=min_cond
next_state = self.cs[self.cb, self.cc, self.ci, :]
else:
next_state = self.true_step(self.cs[self.cb, self.cc, self.ci, :], action)