-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOne-step-look-ahead.vb
More file actions
1645 lines (1629 loc) · 91.7 KB
/
One-step-look-ahead.vb
File metadata and controls
1645 lines (1629 loc) · 91.7 KB
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
Imports System.Math
Imports System.IO
Imports System.Text
Imports MathNet.Numerics.Distributions
Public Class Form4
Dim filepath As String
Public M_state As New List(Of List(Of Integer))
Dim last_origin() As Integer
'Dim amb_origin_base(3) As Integer
Public c_state As New List(Of List(Of Double))
Dim last_num_of_call As Integer
Dim fel As New List(Of List(Of Double))
Dim clock As Double
'Dim lambda_of_l(5, 5) As Double
Public amb_list() As Double
Public seed As Integer
Public weed As Integer
Public leed As Integer
Public reed As Integer
Public zeed As Integer
Public r1 As New Random(seed)
Public r2 As New Random(weed)
Public r3 As New Random(leed)
Public r4 As New Random(reed)
Public r5 As New Random(zeed)
Dim new_call_num As Integer
Dim current_event As Integer
Dim arrival_clock As Double
Dim locations As Integer
Dim priority As Integer
Dim prob_priority As Double = 0.86
Dim prob_scene_hospital As Double = 0.73
Dim lambda_finish_at_scene As Double = 1 / 17.1
Dim lambda_service_at_hospital As Double = 1 / 56.66
Dim next_action As List(Of Double)
Dim i As Double
Dim row As Integer
Dim closer_amb_distance As New List(Of List(Of Double))
Dim begining_state As String
Dim reps As Integer
Dim last_replication As Integer
Public waiting_time As List(Of Double)
Public waiting_loss_time As List(Of Double)
Dim temp_time As Double
Dim temp_loss_time As Double
Dim response_time As Double
Public response_time_indicator As Integer
Public arrival_change As Double
Dim b_loop As Boolean
Public miss_call_nom As Integer
Public miss_call_num_pr As Integer
Public number_of_missed_calls As New List(Of Double)
Public number_of_missed_calls_pr As New List(Of Double)
Public num_of_total_calls As Integer
Public total_hp_call_nom_list As New List(Of Double)
Public total_lp_call_nom_list As New List(Of Double)
Dim busy_time_start As Double
Dim busy_time_end As Double
Dim busy_time As Double
Public busy_time_fraction As New List(Of Double)
Dim node_lambdas As New List(Of Double)
Dim travel_lambdas As New List(Of List(Of Double))
Dim node_distances As New List(Of List(Of Double))
Dim ambulance_initial_location As New List(Of List(Of Integer))
Public amb_ini_base_list As New List(Of Integer)
Dim int_state As List(Of List(Of Integer))
Dim coverage As Double
Dim time_interval As Double
Public state_coverage As New List(Of Double)
Dim time_passed As New Double
Dim last_clock As Double
Dim arrival_list As New List(Of List(Of List(Of Double)))
Dim miss_call_num_hp As Double
Dim miss_call_num_lp As Double
Dim hp_weight As Double = 1
Dim lp_weight As Double = 0.1
Dim total_hp_call_nom As Double
Dim total_lp_call_nom As Double
Dim dynamic_ambulance_number As Integer = 17
Public Sub input_read()
''''''''''''''''''''''''''''''''''''''''reading call arrival lambdas'''''''''''''''''''''''''''''''''''
Dim sr As StreamReader = New StreamReader("c:\users\snasrol\desktop\input\Node Lambdas for Calls.txt", True)
Dim enumerator As Integer = 1
'node_lambdas.Add(0)
While sr.Peek() <> -1
Dim first_line As String
first_line = sr.ReadLine()
If enumerator = Convert.ToInt32(first_line.Substring(0, first_line.IndexOf(vbTab))) Then
node_lambdas.Add(Convert.ToDouble(first_line.Substring(first_line.IndexOf(vbTab) + 1)))
Else
While enumerator <> Convert.ToInt32(first_line.Substring(0, first_line.IndexOf(vbTab)))
node_lambdas.Add(0)
enumerator += 1
End While
node_lambdas.Add(Convert.ToDouble(first_line.Substring(first_line.IndexOf(vbTab) + 1)))
End If
enumerator += 1
End While
''''''''''''''''''''''''''''''reading travel time lambdas'''''''''''''''''''''''''''''''
sr = New StreamReader("c:\users\snasrol\desktop\input\Travel Table-Fixed.txt", True)
While sr.Peek <> -1
Dim line As String = sr.ReadLine()
Dim first_row As New List(Of Double)
For i As Integer = 1 To 168 Step 1
If i = 168 Then
first_row.Add(Convert.ToDouble(line.Substring(0)))
Else
first_row.Add(Convert.ToDouble(line.Substring(0, line.IndexOf(vbTab))))
line = line.Remove(0, line.IndexOf(vbTab) + 1)
End If
Next
travel_lambdas.Add(first_row)
End While
'''''''''''''''''''''''reading distance between each pair of nodes''''''''''''''''''''''''
sr = New StreamReader("c:\users\snasrol\desktop\input\Node Distances.txt", True)
While sr.Peek <> -1
Dim line As String = sr.ReadLine()
Dim first_row As New List(Of Double)
For i As Integer = 1 To 168 Step 1
If i = 168 Then
first_row.Add(Convert.ToDouble(line.Substring(0)))
Else
first_row.Add(Convert.ToDouble(line.Substring(0, line.IndexOf(vbTab))))
line = line.Remove(0, line.IndexOf(vbTab) + 1)
End If
Next
node_distances.Add(first_row)
End While
''''''''''''''''''''''''''''''''''''''''reading ambulance initial location''''''''''''''''''''''''''''
sr = New StreamReader("c:\users\snasrol\desktop\input\Ambulance Initial Location.txt", True)
While sr.Peek <> -1
Dim line As String = sr.ReadLine()
Dim amb_ini_loc_tmp As New List(Of Integer)
amb_ini_loc_tmp.Add(Convert.ToInt32(line.Substring(0, line.IndexOf(vbTab)))) 'location of base
amb_ini_loc_tmp.Add(Convert.ToInt32(line.Substring(line.IndexOf(vbTab)))) 'number of ambs
ambulance_initial_location.Add(amb_ini_loc_tmp)
End While
For i As Integer = 0 To ambulance_initial_location.Count() - 1 Step 1
For j As Integer = 0 To ambulance_initial_location(i).Item(1) Step 1
amb_ini_base_list.Add(ambulance_initial_location(i).Item(0))
Next
Next
End Sub
Public Sub closer_amb_initialization()
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
Dim a As New List(Of Double)
a.Add(0)
a.Add(0)
closer_amb_distance.Add(a)
Next
End Sub
Private Sub amb_iteration_initialization(ByVal state As String)
Dim amb_state As New List(Of Integer)
Dim amb_number As New Integer
For t As Integer = 0 To state.IndexOf("]") Step 1 'ambulance state area
If state.ElementAt(t) = "(" Then
amb_state = New List(Of Integer)
M_state.Add(amb_state)
amb_number += 1
Dim amb_string As String
amb_string = ""
For q As Integer = t + 1 To state.IndexOf("]") Step 1
If state.ElementAt(q) <> ")" Then
amb_string = amb_string + state.ElementAt(q)
Else
Exit For
End If
Next
M_state(amb_number - 1).Add(Convert.ToInt32(amb_string.Substring(0, amb_string.IndexOf(","))))
M_state(amb_number - 1).Add(Convert.ToInt32(amb_string.Substring(amb_string.IndexOf(",") + 2, amb_string.LastIndexOf(",") - amb_string.IndexOf(",") - 2)))
M_state(amb_number - 1).Add(Convert.ToInt32(amb_string.Substring(amb_string.LastIndexOf(",") + 2, amb_string.Count() - amb_string.LastIndexOf(",") - 2)))
M_state(amb_number - 1).Add(0)
End If
Next
amb_list = New Double(amb_number - 1) {}
last_origin = New Integer(amb_number - 1) {}
For t As Integer = 0 To amb_number - 1
last_origin(t) = M_state(t).Item(1)
Next
End Sub
Private Sub call_iteration_initialization(ByVal state As String)
If c_state.Count() > 0 Then
c_state.RemoveRange(0, c_state.Count() - 1)
End If
For t As Integer = state.IndexOf("]") + 1 To state.Count() - 1 Step 1
Dim call_state As New List(Of Double)
If state.ElementAt(t) = "]" Then
Exit For
Else
If state.ElementAt(t) = "(" Then
Dim call_string As String
call_string = ""
For q As Integer = t + 1 To state.LastIndexOf("]") Step 1
If state.ElementAt(q) <> ")" Then
call_string = call_string + state.ElementAt(q)
Else
Exit For
End If
Next
call_state.Add(Convert.ToDouble(call_string.Substring(0, call_string.IndexOf(","))))
call_state.Add(Convert.ToDouble(call_string.Substring(call_string.IndexOf(",") + 2, call_string.LastIndexOf(",") - call_string.IndexOf(",") - 2)))
call_state.Add(Convert.ToDouble(call_string.Substring(call_string.LastIndexOf(",") + 2, call_string.Count() - call_string.LastIndexOf(",") - 2)))
call_state.Add(0)
c_state.Add(call_state)
End If
End If
Next
num_of_total_calls = c_state.Count()
new_call_num = num_of_total_calls
End Sub
Private Sub amb_state_update(ByVal number_of_amb As Integer, ByVal destination As Integer, ByVal st_time As Double)
If destination = 169 Then '''''''''''''''''''''''''''''''''''''''''''''ambulance(num_of_amb) is going to hospital
M_state(number_of_amb).Item(0) = 3
M_state(number_of_amb).Item(1) = last_origin(number_of_amb)
Dim hospital As Integer = Int(r4.NextDouble() * 3)
If hospital = 0 Then
M_state(number_of_amb).Item(2) = 91
ElseIf hospital = 1 Then
M_state(number_of_amb).Item(2) = 37
Else
M_state(number_of_amb).Item(2) = 66
End If
End If
If destination = 168 Then ''''''''''''''''''''''''''''''''''''''''''''ambulance(num_of_amb) is at the scene
M_state(number_of_amb).Item(0) = 2
M_state(number_of_amb).Item(1) = last_origin(number_of_amb)
M_state(number_of_amb).Item(2) = last_origin(number_of_amb)
End If
If destination <= 167 Then '''''''''''''''''''''''''''''''''''''''''''ambulance(num_of_amb) dispatched
M_state(number_of_amb).Item(0) = 1
M_state(number_of_amb).Item(1) = last_origin(number_of_amb)
M_state(number_of_amb).Item(2) = destination
End If
If destination >= 200 And destination < 400 Then ''''''''''''''''''''''''''''''''''''''''''''ambulance (number_of_amb) going to base
M_state(number_of_amb).Item(0) = 4
M_state(number_of_amb).Item(1) = last_origin(number_of_amb)
M_state(number_of_amb).Item(2) = destination - 200
End If
If destination >= 800 Then
M_state(number_of_amb).Item(0) = 0 '''''''''''''''''''''''''''''ambulance (number_of_amb) is at the base
M_state(number_of_amb).Item(1) = destination - 800
M_state(number_of_amb).Item(2) = 0
End If
If destination >= 400 And destination < 800 Then '''''''''''''''''''''''''ambulance is reallocating
M_state(number_of_amb).Item(0) = 4
M_state(number_of_amb).Item(1) = last_origin(number_of_amb)
M_state(number_of_amb).Item(2) = destination - 400
End If
M_state(number_of_amb).Item(3) = clock
End Sub
Private Sub call_state_update(ByVal status As Integer, ByVal number_of_call As Integer, ByVal location_of_call As Integer, ByVal priority_of_call As Integer, ByVal time_of_creation As Double)
Dim call_state As New List(Of Double)
If number_of_call < c_state.Count() Then
c_state(number_of_call).Item(0) = status
c_state(number_of_call).Item(1) = location_of_call
c_state(number_of_call).Item(2) = priority_of_call
Else
If status = 0 Then
call_state.Add(status)
call_state.Add(location_of_call)
call_state.Add(priority_of_call)
call_state.Add(time_of_creation)
c_state.Add(call_state)
End If
last_num_of_call += 1
End If
'last_num_of_call = number_of_call
End Sub
Public Sub state_FEL_building(ByVal state As String)
''print replication propertise in datagridview
'form3.DataGridView1.Rows.Add(1)
'form3.DataGridView1.Rows(row).DefaultCellStyle.BackColor = Color.Gray
'form3.DataGridView1.Item(2, row).Value = "State: " + state
'row = row + 1
''iteration initialization
amb_iteration_initialization(state)
call_iteration_initialization(state)
Dim best_action As String
Dim wtf_finish As Integer = Convert.ToInt32(state.ElementAt(1).ToString())
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If state.ElementAt(1) = "0" Or state.ElementAt(1) = "3" Or state.ElementAt(1) = "4" Or state.ElementAt(1) = "5" Or state.ElementAt(1) = "6" Then 'if there's need for an action in case of call arrival=0, amb finish at scene=3 and amb finish at hospital=4
If state.ElementAt(1) = "0" Then 'if an action for dispatching is needed
best_action = Form1.fixed_action(state, 0, 0, clock)
End If
If state.ElementAt(1) = "3" Or state.ElementAt(1) = "4" Then 'if an action after a finished amb is neede
best_action = Form1.fixed_action(state, 1, wtf_finish, clock)
End If
If state.ElementAt(1) = "5" Or state.ElementAt(1) = "6" Then
best_action = Form1.fixed_action(state, 1, wtf_finish, clock)
End If
Else
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
Dim loc_amb As New Integer
Dim des_amb As New Integer
Dim number_amb As New Integer
For l As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(l) = "(" Then
number_amb += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, number_amb - 1, loc_amb, des_amb)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
FEL_Builder_iteration(3, number_amb - 1, vbNull, vbNull)
Dim count As Integer = c_state.Count() - 1
For t As Integer = 0 To count Step 1
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = loc_amb Then
c_state.RemoveAt(t)
Exit For
End If
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(4, number_amb - 1, loc_amb, vbNull)
End If
If state.ElementAt(l + 1) = "4" Then ''if amb is going to base
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, number_amb - 1, loc_amb, des_amb)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
FEL_Builder_iteration(6, number_amb - 1, loc_amb, des_amb)
End If
End If
Next
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If best_action = "None" Then
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
Dim loc_amb As New Integer
Dim des_amb As New Integer
Dim number_amb As New Integer
'Dim number_call As New Integer
For l As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(l) = "(" Then
number_amb += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, number_amb - 1, loc_amb, des_amb)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
FEL_Builder_iteration(3, number_amb - 1, vbNull, vbNull)
Dim count As Integer = c_state.Count() - 1
For t As Integer = 0 To count Step 1
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = loc_amb Then
c_state.RemoveAt(t)
Exit For
End If
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(4, number_amb - 1, loc_amb, vbNull)
End If
If state.ElementAt(l + 1) = "4" Then ''if amb is going to base
loc_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
des_amb = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, number_amb - 1, loc_amb, des_amb)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
FEL_Builder_iteration(6, number_amb - 1, loc_amb, des_amb)
End If
End If
Next
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If best_action.Substring(0, best_action.IndexOf(",")) = "r" Then 'if the action is only redeployment
best_action = best_action.Remove(0, best_action.IndexOf(",") + 1) 'remove the "r" from best action string
Dim amb_num As New Integer
Dim base_num As New Integer
Dim achar As String
Dim number_of_comma As Integer = 0
For t As Integer = 0 To best_action.Count() - 1
If best_action.ElementAt(t) = "," Then 'counts number of commas
number_of_comma += 1
If number_of_comma = 2 Then 'marks the area of second comma in best_action
For q As Integer = t + 1 To best_action.Count() - 1
If best_action.ElementAt(q) = "," Then 'marks the area of third comma in best_action
achar = best_action.Substring(t, q - t)
Exit For
End If
Next
End If
Exit For
End If
Next
amb_num = Convert.ToInt32(achar)
Dim schar As String = best_action.Substring(best_action.LastIndexOf(",") + 1) 'marks the start of the third comma in best_action
base_num = Convert.ToInt32(schar)
Dim amb_number As New Integer
Dim amb_loc As Integer
For t As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(t) = "(" Then
amb_number += 1
If amb_number - 1 = amb_num Then
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
End If
End If
Next
FEL_Builder_iteration(6, amb_num, amb_loc, base_num) '' FEL building for the best action
amb_state_update(amb_num, base_num + 200, 0) ''update amb status regarding the best action
''other FEL actions
amb_number = New Integer
amb_loc = New Integer
Dim amb_des As New Integer
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
For l As Integer = 0 To state.IndexOf("]") Step 1 'ambuance state area
If state.ElementAt(l) = "(" Then
amb_number += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(3, amb_number - 1, vbNull, vbNull)
Dim count As Integer = c_state.Count() - 1
For t As Integer = 0 To count Step 1
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = amb_loc Then
c_state.RemoveAt(t)
Exit For
End If
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(4, amb_number - 1, amb_loc, vbNull)
End If
If state.ElementAt(l + 1) = "4" And amb_number - 1 <> amb_num Then ''if amb is going to base and is different from the amb of best action
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(6, amb_number - 1, amb_loc, amb_des)
End If
End If
Next
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If best_action.Substring(0, best_action.IndexOf(",")) = "rd" Then 'if the action is redeployment and dispatching
best_action = best_action.Remove(0, best_action.IndexOf(",") + 1) 'remove the "rd" from best action string
Dim amb_num As New Integer
Dim call_num As New Integer
Dim amb_num_2 As New Integer
Dim base_num As New Integer
Dim schar As String = best_action.Substring(0, best_action.IndexOf(",")) 'marks the area to the first comma
amb_num = Convert.ToInt32(schar)
Dim achar As String = best_action.Substring(best_action.LastIndexOf(",") + 1) 'marks the area from the third comma
base_num = Convert.ToInt32(achar)
Dim bchar As String
Dim dchar As String
Dim number_of_comma As Integer = 0
For t As Integer = 0 To best_action.Count() - 1 Step 1
If best_action.ElementAt(t) = "," Then
number_of_comma += 1
End If
If number_of_comma = 1 Then 'marks the area starting from the first comma
For q As Integer = t + 1 To best_action.Count() - 1 Step 1
If best_action.ElementAt(q) = "," Then
bchar = best_action.Substring(t, q - t)
dchar = best_action.Substring(q + 1, best_action.LastIndexOf(",") - q - 1)
Exit For
End If
Next
End If
Next
call_num = Convert.ToInt32(bchar)
amb_num_2 = Convert.ToInt32(dchar)
Dim amb_number As New Integer
Dim call_number As New Integer
Dim amb_loc As New Integer
Dim amb_des As New Integer
Dim call_loc As New Integer
Dim call_pro As New Integer
Dim amb_loc_2 As New Integer
For t As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(t) = "(" Then
amb_number += 1
If amb_number - 1 = amb_num Then
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
For q As Integer = state.LastIndexOf("[") To state.LastIndexOf("]") Step 1
If state.ElementAt(q) = "(" Then
call_number += 1
If call_number - 1 = call_num Then
call_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", q) + 2, state.IndexOf(",", state.IndexOf(",", q) + 1) - state.IndexOf(",", q) - 2))
call_pro = Convert.ToInt32(state.Substring(state.IndexOf(")", t) - 1, 1))
End If
End If
Next
End If
If amb_number - 1 = amb_num_2 Then
amb_loc_2 = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
End If
End If
Next
''FEL best action development
amb_number = New Integer
FEL_Builder_iteration(2, amb_num, amb_loc, call_loc)
FEL_Builder_iteration(6, amb_num_2, amb_loc_2, base_num)
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
amb_state_update(amb_num, call_loc, 0)
amb_state_update(amb_num_2, base_num + 200, 0)
call_state_update(1, call_num, call_loc, call_pro, 0)
''FEL other developments
For l As Integer = 0 To state.IndexOf("]") Step 1 'ambuance state area
If state.ElementAt(l) = "(" Then
amb_number += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(3, amb_number - 1, vbNull, vbNull)
Dim count As Integer = c_state.Count() - 1
For t As Integer = 0 To count Step 1
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = amb_loc Then
c_state.RemoveAt(t)
Exit For
End If
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(4, amb_number - 1, amb_loc, vbNull)
End If
If state.ElementAt(l + 1) = "4" And amb_number - 1 <> amb_num Then ''if amb is going to base and is different from the amb of best action
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(6, amb_number - 1, amb_loc, amb_des)
End If
End If
Next
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If best_action.Substring(0, best_action.IndexOf(",")) = "d" Then 'if best action is just dispatching
best_action = best_action.Remove(0, best_action.IndexOf(",") + 1) 'remove the "d" from best action string
Dim amb_num As New Integer
Dim call_num As New Integer
Dim schar As String = best_action.Substring(0, best_action.IndexOf(",")) 'marks the area to the first comma in best_action
amb_num = Convert.ToInt32(schar)
Dim achar As String
Dim number_of_comma As Integer = 0
For t As Integer = 0 To best_action.Count() - 1 Step 1
If best_action.ElementAt(t) = "," Then 'marks the area of the first comma
number_of_comma += 1
End If
If number_of_comma = 1 Then
For q As Integer = t + 1 To best_action.Count() - 1 Step 1
If best_action.ElementAt(q) = "," Then 'marks the area of the second comma
achar = best_action.Substring(t, q - t)
Exit For
End If
Next
End If
Next
call_num = Convert.ToInt32(achar)
''Fel building for the best action
Dim amb_number As New Integer
Dim call_number As New Integer
Dim amb_loc As New Integer
Dim amb_des As New Integer
Dim call_loc As New Integer
Dim call_pro As New Integer
For t As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(t) = "(" Then
amb_number += 1
If amb_number - 1 = amb_num Then
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
For q As Integer = state.LastIndexOf("[") To state.LastIndexOf("]") Step 1
If state.ElementAt(q) = "(" Then
call_number += 1
If call_number - 1 = call_num Then
call_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", q) + 2, state.IndexOf(",", state.IndexOf(",", q) + 1) - state.IndexOf(",", q) - 2))
call_pro = Convert.ToInt32(state.Substring(state.IndexOf(")", t) - 1, 1))
End If
End If
Next
End If
End If
Next
FEL_Builder_iteration(2, amb_num, amb_loc, call_loc)
amb_state_update(amb_num, call_loc, 0)
call_state_update(1, call_num, call_loc, call_pro, 0)
''other FEL action
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
amb_number = New Integer
For l As Integer = 0 To state.IndexOf("]") Step 1 'ambuance state area
If state.ElementAt(l) = "(" Then
amb_number += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(3, amb_number - 1, vbNull, vbNull)
For t As Integer = 0 To -1 + c_state.Count()
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = amb_loc Then
c_state.RemoveAt(t)
Exit For
End If
End If
If t = c_state.Count() - 1 Then
Exit For
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(4, amb_number - 1, amb_loc, vbNull)
End If
If state.ElementAt(l + 1) = "4" And amb_number - 1 <> amb_num Then ''if amb is going to base and is different from the amb of best action
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(6, amb_number - 1, amb_loc, amb_des)
End If
End If
Next
Exit Sub
End If
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
If best_action.Substring(0, best_action.IndexOf(",")) = "da" Then 'if best action is dispatching and reallocations
best_action = best_action.Remove(0, best_action.IndexOf(",") + 1) 'remove the "da" from best action string
Dim amb_num As New Integer
Dim call_num As New Integer
Dim amb_num_2 As New Integer
Dim base_num As New Integer
Dim schar As String = best_action.Substring(0, best_action.IndexOf(",")) 'marks the area to the first comma
amb_num = Convert.ToInt32(schar)
Dim achar As String = best_action.Substring(best_action.LastIndexOf(",") + 1) 'marks the area from the third comma
base_num = Convert.ToInt32(achar)
Dim bchar As String
Dim dchar As String
Dim number_of_comma As Integer = 0
For t As Integer = 0 To best_action.Count() - 1 Step 1
If best_action.ElementAt(t) = "," Then
number_of_comma += 1
End If
If number_of_comma = 1 Then 'marks the area starting from the first comma
For q As Integer = t + 1 To best_action.Count() - 1 Step 1
If best_action.ElementAt(q) = "," Then
bchar = best_action.Substring(t, q - t)
dchar = best_action.Substring(q + 1, best_action.LastIndexOf(",") - q - 1)
Exit For
End If
Next
End If
Next
call_num = Convert.ToInt32(bchar)
amb_num_2 = Convert.ToInt32(dchar)
Dim amb_number As New Integer
Dim call_number As New Integer
Dim amb_loc As New Integer
Dim amb_des As New Integer
Dim call_loc As New Integer
Dim call_pro As New Integer
Dim amb_loc_2 As New Integer
For t As Integer = state.IndexOf("[") To state.IndexOf("]") Step 1
If state.ElementAt(t) = "(" Then
amb_number += 1
If amb_number - 1 = amb_num Then
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
For q As Integer = state.LastIndexOf("[") To state.LastIndexOf("]") Step 1
If state.ElementAt(q) = "(" Then
call_number += 1
If call_number - 1 = call_num Then
call_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", q) + 2, state.IndexOf(",", state.IndexOf(",", q) + 1) - state.IndexOf(",", q) - 2))
call_pro = Convert.ToInt32(state.Substring(state.IndexOf(")", t) - 1, 1))
End If
End If
Next
End If
If amb_number - 1 = amb_num_2 Then
amb_loc_2 = Convert.ToInt32(state.Substring(state.IndexOf(",", t) + 2, state.IndexOf(",", state.IndexOf(",", t) + 1) - state.IndexOf(",", t) - 2))
End If
End If
Next
''FEL best action development
amb_number = New Integer
FEL_Builder_iteration(2, amb_num, amb_loc, call_loc)
FEL_Builder_iteration(6, amb_num_2, amb_loc_2, base_num)
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
amb_state_update(amb_num, call_loc, 0)
amb_state_update(amb_num_2, base_num + 200, 0)
call_state_update(1, call_num, call_loc, call_pro, 0)
''FEL other developments
For l As Integer = 0 To state.IndexOf("]") Step 1 'ambuance state area
If state.ElementAt(l) = "(" Then
amb_number += 1
If state.ElementAt(l + 1) = "1" Then ''if that amb is dispatched
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(2, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "2" Then ''if amb is at the scene
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(3, amb_number - 1, vbNull, vbNull)
Dim count As Integer = c_state.Count() - 1
For t As Integer = 0 To count Step 1
If c_state(t).Item(0) = 1 Then
If c_state(t).Item(1) = amb_loc Then
c_state.RemoveAt(t)
Exit For
End If
End If
Next
End If
If state.ElementAt(l + 1) = "3" Then ''if amb is at hospital
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
FEL_Builder_iteration(4, amb_number - 1, amb_loc, vbNull)
End If
If state.ElementAt(l + 1) = "4" And amb_number - 1 <> amb_num Then ''if amb is going to base and is different from the amb of best action
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(5, amb_number - 1, amb_loc, amb_des)
End If
If state.ElementAt(l + 1) = "5" Then ''if amb is reallocating
amb_loc = Convert.ToInt32(state.Substring(state.IndexOf(",", l) + 2, state.IndexOf(",", state.IndexOf(",", l) + 1) - state.IndexOf(",", l) - 2))
amb_des = Convert.ToInt32(state.Substring(state.IndexOf(",", state.IndexOf(" ", l)) + 2, state.IndexOf(")", l) - state.IndexOf(",", state.IndexOf(" ", l)) - 2))
FEL_Builder_iteration(6, amb_number - 1, amb_loc, amb_des)
End If
End If
Next
Exit Sub
End If
End Sub
Public Function expo_random_generator(ByVal lambda As Double)
Dim rnd As Double = r1.NextDouble()
expo_random_generator = (-1 / lambda) * Math.Log(1 - rnd)
End Function
Public Function normal_random_generator(ByVal mean As Double, stddev As Double)
Return MathNet.Numerics.Distributions.Normal.Sample(r2, mean, stddev)
End Function
Private Function call_generator() As Double
Dim discount_factor As Double = 0.99999
Dim call_reigon(167) As Double
Dim t As Integer
new_call_num += 1
current_event = 0
For q As Integer = 0 To 167 Step 1
If node_lambdas(q) <> 0 Then
call_reigon(q) = expo_random_generator((1 / node_lambdas(q))) ' + arrival_change * (1 / node_lambdas(q)))
End If
Next
Dim calls As New List(Of Double)
For q As Integer = 0 To call_reigon.Count() - 1 Step 1
If call_reigon(q) <> 0 Then
calls.Add(call_reigon(q))
End If
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
arrival_clock = calls.Min()
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For t = 0 To 167 Step 1
If call_reigon(t) = arrival_clock Then
locations = t
End If
Next
If r3.NextDouble > prob_priority Then
priority = 1
Else
priority = 0
End If
call_state_update(0, new_call_num, locations, priority, clock + arrival_clock)
Return arrival_clock
End Function
Private Sub FEL_Builder_iteration(ByVal action As Integer, number_of_amb As Integer, ByVal origin As Integer, ByVal destination As Integer)
Dim B As List(Of Double) = New List(Of Double)
B.Add(action)
Select Case action
Case 1
B.Add(clock + call_generator())
Case 2
B.Add(clock + travel_lambdas(origin).Item(destination))
amb_list(number_of_amb) = B(1)
Case 3
B.Add(clock + normal_random_generator(54.18, 15.8))
amb_list(number_of_amb) = B(1)
Case 4
B.Add(clock + normal_random_generator(56.7, 13.6))
amb_list(number_of_amb) = B(1)
Case 5
B.Add(clock + travel_lambdas(origin).Item(destination))
amb_list(number_of_amb) = B(1)
Case 6
B.Add(clock + travel_lambdas(origin).Item(destination))
amb_list(number_of_amb) = B(1)
End Select
fel.Add(B)
End Sub
Private Sub simulation_of_state(ByVal state)
Dim amb_num As Integer
next_action = (From i In fel Order By i(1) Ascending Select i).First()
clock = next_action(1)
''''''''''''''''''''''''''''busy time'''''''''''''''
Dim all_amb_busy As Boolean
For j As Integer = 0 To M_state.Count() - 1 Step 1
If M_state(j).Item(0) = 0 Then
all_amb_busy = False
Exit For
Else
all_amb_busy = True
End If
Next
If all_amb_busy = False And busy_time_start <> 0 Then
busy_time_end = clock
busy_time = busy_time + busy_time_end - busy_time_start
busy_time_start = 0
Else
If all_amb_busy = True And busy_time_start = 0 Then
busy_time_start = clock
End If
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Select Case next_action(i)
Case 1
'sampling_state_iteration(0, 10)
call_arrival()
'trace_current(vbNull)
'trace_fel()
Case 2
Dim rand As New Double
rand = r5.NextDouble
If rand > prob_scene_hospital Then
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
'trace_current(t)
ambulance_served_at_scene(t)
'sampling_state_iteration(2, t)
'trace_fel()
amb_num = t
Exit For
End If
Next
Else
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
'trace_current(t)
amb_to_hospital(t)
'sampling_state_iteration(2, t)
'trace_fel()
amb_num = t
Exit For
End If
Next
End If
If c_state.Count() >= 1 Then
For t As Integer = 0 To c_state.Count() - 1 Step 1
If c_state(t).Item(1) = M_state(amb_num).Item(1) Then
If c_state(t).Item(3) < Form1.TextBox1.Text Then 'And c_state(t).Item(2) = 1 Then
calculating_waiting_time(clock, c_state(t).Item(3), reps, state, c_state(t).Item(2)) ''calculating waiting time for each call
End If
c_state.RemoveAt(t)
Exit For
End If
Next
End If
Case 3
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
finished_at_scene(t)
'trace_current(t)
'sampling_state_iteration(3, t)
'trace_fel()
Exit For
End If
Next
Case 4
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
finished_at_hospital(t)
'trace_current(t)
'sampling_state_iteration(4, t)
'trace_fel()
Exit For
End If
Next
Case 5
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
'trace_current(t)
arrives_at_base(t)
'sampling_state_iteration(5, t)
'trace_fel()
Exit For
End If
Next
Case 6
For t As Integer = 0 To dynamic_ambulance_number - 1 Step 1
If clock = amb_list(t) Then
trace_current(t)
arrives_at_base(t)
'sampling_state_iteration(6, t)
trace_fel()
Exit For
End If
Next
End Select
fel.Remove(next_action)
End Sub
Public Function sampling_state_iteration(ByVal events As Integer, ByVal amb_number As Integer)
'Dim file_path As String = "C:\Users\snasrol\Desktop\output\State_ADP Complete Sample.txt"
Dim sb As StringBuilder = New StringBuilder
sb.Append("{" + events.ToString + ", [")
For t = 0 To dynamic_ambulance_number - 1 Step 1
sb.Append("(" + M_state(t).Item(0).ToString + ", " + M_state(t).Item(1).ToString + ", " + M_state(t).Item(2).ToString + ")")
Next
sb.Append("], [")
For t = 0 To c_state.Count() - 1 Step 1
If c_state(t).Item(3) <= clock Then
sb.Append("(" + c_state(t).Item(0).ToString + ", " + c_state(t).Item(1).ToString + ", " + c_state(t).Item(2).ToString + ")")
End If
Next
sb.Append("]}")
sb.AppendLine("," + amb_number.ToString())
'Using outfile As StreamWriter = New StreamWriter(file_path, True)
'outfile.Write(sb.ToString())
'End Using
Return sb.ToString()
End Function
Private Sub call_arrival() ''mabey there is a best action in the policy
If clock > Form1.TextBox1.Text Then
Exit Sub
End If
num_of_total_calls += 1
Dim state As String = sampling_state_iteration(0, 50)
FEL_Builder_iteration(1, vbNull, vbNull, vbNull)
Dim best_action As String = Form1.fixed_action(state, 0, 0, clock)
If best_action <> "None" Then
''best dispatch and reallocate action
If best_action.Substring(0, best_action.IndexOf(",")) = "da" Then 'if the action is dispatching and reallocation
best_action = best_action.Remove(0, best_action.IndexOf(",") + 1) 'remove the "da" from best action string
Dim amb_num As New Integer
Dim call_num As New Integer
Dim amb_num_2 As New Integer
Dim base_num As New Integer
Dim schar As String = best_action.Substring(0, best_action.IndexOf(",")) 'marks the area to the first comma
amb_num = Convert.ToInt32(schar)
Dim achar As String = best_action.Substring(best_action.LastIndexOf(",") + 1) 'marks the area from the third comma
base_num = Convert.ToInt32(achar)
Dim bchar As String
Dim dchar As String
Dim number_of_comma As Integer = 0
For t As Integer = 0 To best_action.Count() - 1 Step 1
If best_action.ElementAt(t) = "," Then
number_of_comma += 1
End If
If number_of_comma = 1 Then 'marks the area starting from the first comma
For q As Integer = t + 1 To best_action.Count() - 1 Step 1
If best_action.ElementAt(q) = "," Then
bchar = best_action.Substring(t, q - t)
dchar = best_action.Substring(q + 1, best_action.LastIndexOf(",") - q - 1)
Exit For
End If
Next
End If
Next