-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluents.py
1402 lines (1174 loc) · 68.3 KB
/
fluents.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 2023 DFKI GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Tuple, Set
from abc import ABC, abstractmethod
from enum import Enum, unique
import math
import warnings
from up_interface import config as conf
from up_interface.types import *
from up_interface.types_helper import *
@unique
class FluentNames(Enum):
""" Enum holding the names of the fluents to be used """
planning_failed = 'planning_failed'
total_harvested_mass = 'total_harvested_mass'
total_harvested_mass_planned = 'total_harvested_mass_planned'
total_yield_mass_in_fields_unharvested = 'total_yield_mass_in_fields_unharvested'
total_yield_mass_in_fields_unreserved = 'total_yield_mass_in_fields_unreserved'
total_yield_mass_potentially_reserved = 'total_yield_mass_potentially_reserved'
total_yield_mass_reserved = 'total_yield_mass_reserved'
total_yield_mass_in_silos = 'total_yield_mass_in_silos'
total_yield_mass_reserved_in_silos = 'total_yield_mass_reserved_in_silos'
default_infield_transit_duration_to_access_point = 'default_infield_transit_duration_to_access_point'
# HARVESTER
harv_free = 'harv_free'
harv_timestamp = 'harv_timestamp'
harv_at_init_loc = 'harv_at_init_loc'
harv_at_field = 'harv_at_field'
harv_at_field_access = 'harv_at_field_access'
harv_transit_time = 'harv_transit_time'
harv_enabled_to_drive_to_loc = 'harv_enabled_to_drive_to_loc'
harv_enabled_to_drive_to_field_exit = 'harv_enabled_to_drive_to_field_exit'
harv_enabled_to_overload = 'harv_enabled_to_overload'
harv_transit_speed_empty = 'harv_transit_speed_empty'
harv_working_time_per_area = 'harv_working_time_per_area'
harv_overload_count = 'harv_overload_count'
harv_overload_id = 'harv_overload_id'
harv_overloading = 'harv_overloading'
harv_waiting_to_harvest = 'harv_waiting_to_harvest'
harv_field_turn = 'harv_field_turn'
harv_count_pre_assigned_field_turns = 'harv_count_pre_assigned_field_turns'
harv_tv_turn = 'harv_tv_turn'
harv_pre_assigned_tv_turns_left = 'harv_pre_assigned_tv_turns_left'
harv_waiting_time = 'harv_waiting_time'
# TRANSPORT VEHICLE
tv_free = 'tv_free'
tv_timestamp = 'tv_timestamp'
tv_at_init_loc = 'tv_at_init_loc'
tv_at_field = 'tv_at_field'
tv_at_field_access = 'tv_at_field_access'
tv_at_silo_access = 'tv_at_silo_access'
tv_transit_time = 'tv_transit_time'
tv_transit_speed_empty = 'tv_transit_speed_empty'
tv_transit_speed_full = 'tv_transit_speed_full'
tvs_all_enabled_to_drive_to_field = 'tvs_all_enabled_to_drive_to_field'
tvs_all_enabled_to_arrive_in_field = 'tvs_all_enabled_to_arrive_in_field'
tv_enabled_to_drive_to_field = 'tv_enabled_to_drive_to_field'
tv_enabled_to_drive_to_silo = 'tv_enabled_to_drive_to_silo'
tv_enabled_to_drive_to_field_exit = 'tv_enabled_to_drive_to_field_exit'
tv_total_capacity_mass = 'tv_total_capacity_mass'
tv_unloading_speed_mass = 'tv_unloading_speed_mass'
tv_bunker_mass = 'tv_bunker_mass'
tv_ready_to_unload = 'tv_ready_to_unload'
tv_overload_id = 'tv_overload_id'
tv_mass_to_overload = 'tv_mass_to_overload'
tv_pre_assigned_harvester = 'tv_pre_assigned_harvester'
tv_pre_assigned_turn = 'tv_pre_assigned_turn'
tvs_waiting_to_overload_ref_count = 'tvs_waiting_to_overload_ref_count'
tv_waiting_to_overload_id = 'tv_waiting_to_overload_id'
tv_ready_to_overload = 'tv_ready_to_overload'
tv_waiting_to_overload = 'tv_waiting_to_overload'
tvs_waiting_to_drive_ref_count = 'tvs_waiting_to_drive_ref_count'
tv_waiting_to_drive_id = 'tv_waiting_to_drive_id'
tv_ready_to_drive = 'tv_ready_to_drive'
tv_waiting_to_drive = 'tv_waiting_to_drive'
tv_waiting_time = 'tv_waiting_time'
tv_can_unload = 'tv_can_unload'
tv_can_load = 'tv_can_load'
# FIELD
field_id = 'field_id'
field_harvested = 'field_harvested'
field_started_harvest_int = 'field_started_harvest_int'
field_timestamp_harvested = 'field_timestamp_harvested'
field_timestamp_started_harvest = 'field_timestamp_started_harvest'
field_timestamp_assigned = 'field_timestamp_assigned'
field_plan_id = 'field_plan_id'
field_harvester = 'field_harvester'
field_pre_assigned_harvester = 'field_pre_assigned_harvester'
field_pre_assigned_turn = 'field_pre_assigned_turn'
field_area_per_yield_mass = 'field_area_per_yield_mass'
field_yield_mass_total = 'field_yield_mass_total'
field_yield_mass_after_reserve = 'field_yield_mass_after_reserve'
field_yield_mass_unharvested = 'field_yield_mass_unharvested'
field_yield_mass_minus_planned = 'field_yield_mass_minus_planned'
# FIELD ACCESS
field_access_field = 'field_access_field'
field_access_field_id = 'field_access_field_id'
field_access_index = 'field_access_index'
# SILO
silo_id = 'silo_id'
silo_available_capacity_mass = 'silo_available_capacity_mass'
# SILO ACCESS
silo_access_silo_id = 'silo_access_silo_id'
silo_access_index = 'silo_access_index'
silo_access_free = 'silo_access_free'
silo_access_total_capacity_mass = 'silo_access_total_capacity_mass'
silo_access_available_capacity_mass = 'silo_access_available_capacity_mass'
silo_access_sweep_duration = 'silo_access_sweep_duration'
silo_access_state_id = 'silo_access_state_id'
silo_access_cleared = 'silo_access_cleared'
silo_access_timestamp = 'silo_access_timestamp'
# COMPACTOR
compactor_silo_id = 'compactor_silo_id'
compactor_mass_per_sweep = 'compactor_mass_per_sweep'
compactor_free = 'compactor_free'
# LOCATIONS
transit_distance_init_fap = 'transit_distance_init_fap'
transit_distance_init_sap = 'transit_distance_init_sap'
transit_distance_fap_sap = 'transit_distance_fap_sap'
transit_distance_fap_fap = 'transit_distance_fap_fap'
transit_distance_sap_fap = 'transit_distance_sap_fap'
class FluentExtended:
""" Class holding a fluent and its initial value """
def __init__(self, fluent: Fluent, default_initial_value=None):
self.fluent = fluent
self.default_initial_value = default_initial_value
class FluentValueRange:
""" Class holding values range (lower and upper bounds) of a variable (x=None: no x limit) """
def __init__(self, _min: Union[int, float, None] = None, _max: Union[int, float, None] = None):
self.min = _min
self.max = _max
class FluentValueRanges:
""" Class holding the value ranges and maximum values of problem-specific variables, which will be used to initialize numeric fluents """
def __init__(self, with_bounds: bool = True):
_def_max_yield_mass = 10000000000
""" Default maximum yield mass [kg] """
_def_max_transit_distance = 10000000000
""" Default maximum transit distance between locations [m] """
_def_max_infield_transit_duration = 10000000000
""" Default maximum duration for transit inside the field [s] """
_def_max_speed = 50
""" Default maximum machine speed [m/s] """
self.with_int_bounds = with_bounds
""" If false, all integer fluents will be added without bounds """
self.with_real_bounds = with_bounds
""" If false, all real fluents will be added without bounds """
self.field_ids = FluentValueRange()
""" Range of values of field ids """
self.silo_ids = FluentValueRange()
""" Range of values of silo ids """
self.tv_mass_capacity = FluentValueRange()
""" Range of values of mass capacity of transport vehicles [kg] """
self.count_field_accesses_in_field = FluentValueRange(0, 1000)
""" Range of amount of field access points in a field """
self.count_silo_accesses_in_silo = FluentValueRange(0, 1000)
""" Range of amount of silo access points in a silo """
self.yield_mass_in_field = FluentValueRange(0, _def_max_yield_mass)
""" Range of yield mass in the fields [kg]"""
self.harv_max_transit_speed_empty = FluentValueRange(-1, _def_max_speed)
""" Range of maximum harvester speeds (empty) [m/s] """
self.tv_max_transit_speed_empty = FluentValueRange(-1, _def_max_speed)
""" Range of maximum transport vehicle speeds (empty) [m/s] """
self.tv_max_transit_speed_full = FluentValueRange(-1, _def_max_speed)
""" Range of maximum transport vehicle speeds (full) [m/s] """
self.harv_working_time_per_area = FluentValueRange(0, 1000)
""" Range of harvesters' working time per area [s/m²] """
self.tv_unloading_speed_mass = FluentValueRange(1, 100000)
""" Range of transport vehicle unloading speeds [kg/s] """
self.field_area_per_yield_mass = FluentValueRange(0, 1000000)
""" Range of field area per yield mass [m²/kg] """
self.silo_access_mass_capacity = FluentValueRange(0, INF_CAPACITY)
""" Range of mass capacities in the silo access points [kg] """
self.silo_access_sweep_duration = FluentValueRange(0, 10000)
""" Range of silo access sweep durations [s] """
self.compactor_mass_per_sweep = FluentValueRange(0, 100000)
""" Range of mass compacted by the compactors in a sweep [kg] """
self.count_fields_to_work: Optional[int] = None
""" Amount of fields to be worked/harvested """
self.count_tvs: Optional[int] = None
""" Amount of transport vehicles """
self.total_yield_mass_in_fields: float = _def_max_yield_mass
""" Total amount of yield-mass in all fields [kg] """
self.total_yield_mass_in_tvs: float = _def_max_yield_mass
""" Total amount of yield-mass in all transport vehicles in the initial state [kg] """
self.infield_transit_duration_to_fap: Optional[float] = None
""" Duration of transit inside the field from/to a field access point [s] """
self.max_harv_transit_time: Optional[float] = None
""" Maximum transit time that a harvester can have in the given problem [s] """
self.max_tv_transit_time: Optional[float] = None
""" Maximum transit time that a transport vehicle can have in the given problem [s] """
self.max_transit_distance_init_fap: float = _def_max_transit_distance
""" Maximum transit transit distance from machines' initial locations to field access points [m] """
self.max_transit_distance_init_sap: float = _def_max_transit_distance
""" Maximum transit transit distance from machines' initial locations to silo access points [m] """
self.max_transit_distance_fap_sap: float = _def_max_transit_distance
""" Maximum transit transit distance from field access points to silo access points [m] """
self.max_transit_distance_fap_fap: float = _def_max_transit_distance
""" Maximum transit transit distance between field access points [m] """
self.max_transit_distance_sap_fap: float = _def_max_transit_distance
""" Maximum transit transit distance from silo access points to field access points [m] """
self.max_silo_mass_capacity: float = INF_CAPACITY
""" Maximum mass capacity in the silos [kg] """
self.max_overloading_activities_all: Optional[int] = None
""" Maximum amount of overloading activities that can exist in the given problem """
self.max_overloading_activities_field: Optional[int] = None
""" Maximum amount of overloading activities that an field can have in the given problem """
self.max_process_duration: Optional[float] = None
""" Maximum possible duration of the process for the given problem """
class FluentsManagerBase(ABC):
""" Base class of the FluentsManager """
def __init__(self):
self._fluents: Dict[str, FluentExtended] = dict()
self._fluents_initial_values: Dict[str, List[Tuple[Union[Tuple, Any, None]], Any]] = dict()
self._name_values = [member.value for member in FluentNames]
self._initialized: bool = False
def initialize(self,
problem_settings: conf.GeneralProblemSettings = conf.default_problem_settings,
fluent_value_ranges: FluentValueRanges = FluentValueRanges()):
""" Initialize the FluentsManager with the given problem settings and fluents' value ranges
Parameters
----------
problem_settings : config.GeneralProblemSettings
Problem settings
fluent_value_ranges : FluentValueRanges
Holds all value ranges and maximum values of problem-specific variables, which will be used to initialize numeric fluents
"""
self._initialize(problem_settings, fluent_value_ranges)
self._initialized = True
@abstractmethod
def _initialize(self,
problem_settings: conf.GeneralProblemSettings = conf.default_problem_settings,
fluent_value_ranges: FluentValueRanges = FluentValueRanges()):
""" (Child implementation) Initialize the FluentsManager with the given problem settings and fluents' value ranges
Parameters
----------
problem_settings : config.GeneralProblemSettings
Problem settings
fluent_value_ranges : FluentValueRanges
Holds all value ranges and maximum values to be set to the fluents during their initialization
"""
pass
@abstractmethod
def fluent_enabled_for_problem_settings(self,
fluent: Union[str, Fluent],
problem_settings: conf.GeneralProblemSettings) \
-> bool:
""" (Child implementation) Check if a fluent is enabled for the given problem settings.
Parameters
----------
fluent : str, Fluent
Fluent or fluent name
problem_settings : config.GeneralProblemSettings
Problem settings
Returns
-------
enabled : bool
True if the fluent is enabled
"""
pass
def add_fluents_to_problem(self, problem: Problem,
fluents_to_add: Optional[Set[Union[str, FluentNames]]] = None,
check_fluents: bool = True):
""" Add all registered fluents to a problem.
Parameters
----------
problem : Problem
Problem
fluents_to_add : Set[str | FluentNames]
Names of the fluents to be added (if None -> all registered fluents will be added)
check_fluents : bool
If True, it will check if values were added for non-registered fluents and print a warning
"""
if not self._initialized:
raise Exception("The fluents manager has not been initialized ")
def get_fluent_value(fluent_: Fluent, value, allow_none: bool = True):
if value is None:
if allow_none:
return None
raise ValueError(f'Invalid value: {value}')
f_type = get_up_type_as_str(fluent_)
if is_up_type_bool(f_type) and isinstance(value, bool):
_value = Bool(value)
elif is_up_type_int(f_type) and isinstance(value, int):
_value = Int(value)
elif is_up_type_real(f_type) and isinstance(value, (int, float)):
_value = get_up_real(value)
else:
_value = value
v_type = get_up_type_as_str(_value)
if v_type != f_type:
raise ValueError(f'Missmatch in value ({v_type}) and fluent ({f_type}) types')
return _value
if fluents_to_add is None:
fluents_dict = self._fluents
else:
fluents_dict = dict()
for name in fluents_to_add:
fluent_ext = self.get_fluent_ext(name)
assert fluent_ext is not None, f"Fluent {name} is not registered"
fluents_dict[name] = fluent_ext
for name, fluent_ext in fluents_dict.items():
fluent = fluent_ext.fluent
problem.add_fluent(fluent,
default_initial_value=get_fluent_value(fluent, fluent_ext.default_initial_value, True))
fluent_vals = self._fluents_initial_values.get(name)
if fluent_vals is None:
continue
for params, init_val in fluent_vals:
_init_val = get_fluent_value(fluent, init_val, False)
if params is None:
problem.set_initial_value(fluent(), _init_val)
elif isinstance(params, tuple) or isinstance(params, list):
problem.set_initial_value(fluent(*params), _init_val)
else:
problem.set_initial_value(fluent(params), _init_val)
if check_fluents:
for name in self._fluents_initial_values.keys():
if name not in self._fluents.keys():
warnings.warn(f'Initial values were added for non-registered fluent {name}')
def add_fluent_initial_value(self,
fluent_name: Union[str, FluentNames],
params: Union[Tuple, Any, None],
value: Any):
""" Register (internally) the initial value of a fluent.
Parameters
----------
fluent_name : str, FluentNames
Fluent name (or enum)
params : Tuple, Any, None
Fluent parameters
value: Any
Fluent initial value
"""
if isinstance(fluent_name, FluentNames):
_fluent_name = fluent_name.value
else:
_fluent_name = fluent_name
if _fluent_name not in self._name_values:
raise ValueError(f'The fluent name {fluent_name} does not correspond to any of the supported fluent names')
fluent_vals = self._fluents_initial_values.get(_fluent_name)
if fluent_vals is None:
fluent_vals = list()
self._fluents_initial_values[_fluent_name] = fluent_vals
fluent_vals.append( (params, value) )
@property
def fluents(self) -> Dict[str, FluentExtended]:
""" Get the (extended) fluents (fluents + initial values)
Returns
-------
fluents : Dict[str, FluentExtended]
Fluents dictionary: key: fluent name ; value: extended fluent (fluent + initial value)
"""
return self._fluents
def get_fluent(self, name: Union[FluentNames, str]) -> Union[Fluent, None]:
""" Get a specific fluent
Parameters
----------
name : str, FluentNames
Fluent name (or enum)
Returns
-------
fluent : Fluent, None
The respective fluent or None if a fluent with the given name is not registered
"""
f_ext = self.get_fluent_ext(name)
if f_ext is None:
return None
# raise ValueError(f'The fluent {name} does not exist')
return f_ext.fluent
def get_fluent_ext(self, name: Union[FluentNames, str]) -> Optional[FluentExtended]:
""" Get a specific (extended) fluent
Parameters
----------
name : str, FluentNames
Fluent name (or enum)
Returns
-------
fluent : Fluent, None
The respective (extended) fluent or None if a fluent with the given name is not registered
"""
if isinstance(name, str):
f_ext = self._fluents.get(name)
elif isinstance(name, FluentNames):
f_ext = self._fluents.get(name.value)
else:
raise TypeError(f'Invalid name type')
if f_ext is None:
return None
# raise ValueError(f'The fluent {name} does not exist')
return f_ext
def _add_fluent(self,
problem_settings: conf.GeneralProblemSettings,
fluent: Fluent,
default_initial_value: Any = None):
""" Register (internally) a fluent and its default initial value
Parameters
----------
problem_settings : conf.GeneralProblemSettings
Problem settings
fluent : Fluent
Fluent
default_initial_value :
Default fluent initial value
"""
if fluent.name not in self._name_values:
raise ValueError(f'The fluent name {fluent.name} does not correspond to any of the supported fluent names')
if fluent.name in self._fluents.keys():
print(f'[WARN] Fluent {fluent.name} was already added and will be overwritten')
if not self.fluent_enabled_for_problem_settings(fluent, problem_settings):
print(f'[WARN] Fluent {fluent.name} is not enabled for the given problem settings')
return
self._fluents[fluent.name] = FluentExtended(fluent, default_initial_value)
class FluentsManager(FluentsManagerBase):
""" Default FluentsManager """
def __init__(self):
super(FluentsManager, self).__init__()
fn = FluentNames
# Establish which fluents are exclusive for temporal or sequential planning
self.__fluents_only_temporal = {
fn.total_harvested_mass_planned.value,
fn.total_yield_mass_in_fields_unreserved.value,
fn.total_yield_mass_potentially_reserved.value,
fn.total_yield_mass_reserved.value,
fn.harv_free.value,
fn.harv_enabled_to_drive_to_loc.value,
fn.harv_enabled_to_drive_to_field_exit.value,
fn.harv_enabled_to_overload.value,
fn.harv_overload_count.value,
fn.harv_overload_id.value,
fn.harv_overloading.value,
fn.harv_waiting_to_harvest.value,
fn.tv_free.value,
fn.tvs_all_enabled_to_drive_to_field.value,
fn.tvs_all_enabled_to_arrive_in_field.value,
fn.tv_enabled_to_drive_to_field.value,
fn.tv_enabled_to_drive_to_silo.value,
fn.tv_enabled_to_drive_to_field_exit.value,
fn.tv_overload_id.value,
fn.tv_mass_to_overload.value,
fn.tvs_waiting_to_overload_ref_count.value,
fn.tv_waiting_to_overload_id.value,
fn.tv_ready_to_overload.value,
fn.tv_waiting_to_overload.value,
fn.tvs_waiting_to_drive_ref_count.value,
fn.tv_waiting_to_drive_id.value,
fn.tv_ready_to_drive.value,
fn.tv_waiting_to_drive.value,
fn.field_yield_mass_after_reserve.value,
fn.field_yield_mass_minus_planned.value
}
self.__fluents_only_sequential = {
fn.total_yield_mass_in_fields_unharvested.value,
fn.field_started_harvest_int.value,
fn.field_timestamp_harvested.value,
fn.field_timestamp_started_harvest.value,
fn.field_timestamp_assigned.value,
fn.harv_timestamp.value,
fn.harv_waiting_time.value,
fn.tv_timestamp.value,
fn.tv_waiting_time.value,
fn.silo_access_timestamp.value
}
def fluent_enabled_for_problem_settings(self,
fluent: Union[str, Fluent],
problem_settings: conf.GeneralProblemSettings) -> bool:
""" Check if a fluent is enabled for the given problem settings.
Parameters
----------
fluent : str, Fluent
Fluent or fluent name
problem_settings : config.GeneralProblemSettings
Problem settings
Returns
-------
enabled : bool
True if the fluent is enabled
"""
if isinstance(fluent, Fluent):
fluent_name = fluent.name
else:
fluent_name = fluent
if problem_settings.planning_type is conf.PlanningType.TEMPORAL \
and fluent_name in self.__fluents_only_sequential:
return False
elif problem_settings.planning_type is conf.PlanningType.SEQUENTIAL \
and fluent_name in self.__fluents_only_temporal:
return False
fn = FluentNames
if problem_settings.control_windows.enable_driving_opening_time is None \
or problem_settings.control_windows.enable_driving_opening_time <= 0.0:
if fluent_name == fn.harv_enabled_to_drive_to_loc.value \
or fluent_name == fn.harv_enabled_to_drive_to_field_exit.value \
or fluent_name == fn.tv_enabled_to_drive_to_silo.value \
or fluent_name == fn.tv_enabled_to_drive_to_field_exit.value:
return False
if problem_settings.control_windows.enable_overload_opening_time is None \
or problem_settings.control_windows.enable_overload_opening_time <= 0.0:
if fluent_name == fn.harv_enabled_to_overload.value:
return False
if problem_settings.cost_windows.waiting_harvest_opening_time is None \
or problem_settings.cost_windows.waiting_harvest_opening_time <= 0.0:
if fluent_name == fn.harv_waiting_to_harvest.value:
return False
if problem_settings.control_windows.enable_driving_tvs_to_field_opening_time is None \
or problem_settings.control_windows.enable_driving_tvs_to_field_opening_time <= 0.0:
if fluent_name == fn.tvs_all_enabled_to_drive_to_field.value \
or fluent_name == fn.tv_enabled_to_drive_to_field.value:
return False
if problem_settings.control_windows.enable_arriving_tvs_in_field_opening_time is None \
or problem_settings.control_windows.enable_arriving_tvs_in_field_opening_time <= 0.0:
if fluent_name == fn.tvs_all_enabled_to_arrive_in_field.value:
return False
if problem_settings.cost_windows.waiting_overload_opening_time is None \
or problem_settings.cost_windows.waiting_overload_opening_time <= 0.0:
if problem_settings.cost_windows.use_old_implementation_waiting_overload:
if fluent_name == fn.tvs_waiting_to_overload_ref_count.value \
or fluent_name == fn.tv_waiting_to_overload_id.value \
or fluent_name == fn.tv_ready_to_overload.value \
or fluent_name == fn.tv_waiting_to_overload.value:
return False
else:
if problem_settings.cost_windows.use_old_implementation_waiting_overload:
if fluent_name == fn.tvs_waiting_to_overload_ref_count.value \
or fluent_name == fn.tv_waiting_to_overload_id.value \
or fluent_name == fn.tv_ready_to_overload.value:
return False
else:
if fluent_name == fn.tv_waiting_to_overload.value:
return False
if ( ( problem_settings.cost_windows.waiting_drive_opening_time is None
or problem_settings.cost_windows.waiting_drive_opening_time <= 0.0 )
and ( problem_settings.cost_windows.waiting_drive_from_silo_opening_time is None
or problem_settings.cost_windows.waiting_drive_from_silo_opening_time <= 0.0 ) ):
if (fluent_name == fn.tvs_waiting_to_drive_ref_count.value
or fluent_name == fn.tv_waiting_to_drive_id.value
or fluent_name == fn.tv_ready_to_drive.value
or fluent_name == fn.tv_waiting_to_drive.value):
return False
else:
if problem_settings.cost_windows.use_old_implementation_waiting_drive:
if fluent_name == fn.tvs_waiting_to_drive_ref_count.value \
or fluent_name == fn.tv_waiting_to_drive_id.value \
or fluent_name == fn.tv_ready_to_drive.value:
return False
else:
if fluent_name == fn.tv_waiting_to_drive.value:
return False
if problem_settings.silo_planning_type is not conf.SiloPlanningType.WITH_SILO_ACCESS_CAPACITY_AND_COMPACTION:
if fluent_name == fn.compactor_silo_id.value \
or fluent_name == fn.compactor_mass_per_sweep.value \
or fluent_name == fn.compactor_free.value:
return False
if problem_settings.silo_planning_type is conf.SiloPlanningType.WITHOUT_SILO_ACCESS_AVAILABILITY:
if fluent_name == fn.tv_ready_to_unload.value \
or fluent_name == fn.silo_access_timestamp.value:
return False
return True
def _initialize(self,
problem_settings: conf.GeneralProblemSettings = conf.default_problem_settings,
fluent_value_ranges: FluentValueRanges = FluentValueRanges()):
""" Initialize the FluentsManager with the given problem settings and fluents' value ranges
Parameters
----------
problem_settings : config.GeneralProblemSettings
Problem settings
fluent_value_ranges : FluentValueRanges
Holds all value ranges and maximum values of problem-specific variables, which will be used to initialize numeric fluents
"""
fvr = fluent_value_ranges
fn = FluentNames
_max_mass_factor = 2 # to be sure the limits will not be exceeded (otherwise the planner sometimes fails to yield a plan)
# --------------PROCESS FLUENTS--------------
# [BOOL] Has the plan failed?
# Workaround to be used as precondition for all actions
self._add_fluent(problem_settings,
Fluent(fn.planning_failed.value, BoolType()),
default_initial_value=False)
# [REAL] Total yield mass harvested in all fields
self._add_fluent(problem_settings,
Fluent(fn.total_harvested_mass.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=0.0)
# [Real] Total remaining (unharvested) yield mass in all fields
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_in_fields_unharvested.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=fvr.total_yield_mass_in_fields)
# [REAL] Total yield mass planned to be harvested in all fields (i.e., the harvester started the harvesting window)
self._add_fluent(problem_settings,
Fluent(fn.total_harvested_mass_planned.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=0.0)
# [Real/CONST] Total remaining (unreserved) yield mass in all fields
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_in_fields_unreserved.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=fvr.total_yield_mass_in_fields)
# [Real] Total potentially reserved yield mass (planned) - Before we actually know how much will be reserved
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_potentially_reserved.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=0.0)
# [Real] Total reserved yield mass (planned)
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_reserved.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.total_yield_mass_in_fields * _max_mass_factor)),
default_initial_value=0.0)
_max_mass_to_store = None if fvr.total_yield_mass_in_fields is None or fvr.total_yield_mass_in_tvs is None \
else math.ceil( fvr.total_yield_mass_in_fields + fvr.total_yield_mass_in_tvs ) * _max_mass_factor
# [REAL] Total mass stored at the silos
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_in_silos.value,
self._get_real_type(fvr.with_real_bounds, 0, _max_mass_to_store)),
default_initial_value=0.0)
# [REAL] Total mass reserved to be stored at the silos
self._add_fluent(problem_settings,
Fluent(fn.total_yield_mass_reserved_in_silos.value,
self._get_real_type(fvr.with_real_bounds, 0, _max_mass_to_store)),
default_initial_value=0.0)
# [Real/CONST] Default infield transit duration from/to an access point
# @todo Used to access the value from the problem object
self._add_fluent(problem_settings,
Fluent(fn.default_infield_transit_duration_to_access_point.value,
self._get_real_type(fvr.with_real_bounds, fvr.infield_transit_duration_to_fap, fvr.infield_transit_duration_to_fap)),
default_initial_value=self._get_fraction(fvr.infield_transit_duration_to_fap))
# --------------MACHINE FLUENTS--------------
# [BOOL] Is the machine available
self._add_fluent(problem_settings,
Fluent(fn.harv_free.value, BoolType(), machine=Harvester),
default_initial_value=True)
self._add_fluent(problem_settings,
Fluent(fn.tv_free.value, BoolType(), machine=TransportVehicle),
default_initial_value=True)
# [Real] The machine timestamp
self._add_fluent(problem_settings,
Fluent(fn.harv_timestamp.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.max_process_duration),
machine=Harvester),
default_initial_value=0)
self._add_fluent(problem_settings,
Fluent(fn.tv_timestamp.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.max_process_duration),
machine=TransportVehicle),
default_initial_value=0)
# [MachineLocation] Location of a specified machine
self._add_fluent(problem_settings,
Fluent(fn.harv_at_init_loc.value, MachineInitLoc, machine=Harvester),
default_initial_value=None)
self._add_fluent(problem_settings,
Fluent(fn.harv_at_field.value, Field, machine=Harvester), None)
self._add_fluent(problem_settings,
Fluent(fn.harv_at_field_access.value, FieldAccess, machine=Harvester),
default_initial_value=None)
self._add_fluent(problem_settings,
Fluent(fn.tv_at_init_loc.value, MachineInitLoc, machine=TransportVehicle),
default_initial_value=None)
self._add_fluent(problem_settings,
Fluent(fn.tv_at_field.value, Field, machine=TransportVehicle),
default_initial_value=None)
self._add_fluent(problem_settings,
Fluent(fn.tv_at_field_access.value, FieldAccess, machine=TransportVehicle),
default_initial_value=None)
self._add_fluent(problem_settings,
Fluent(fn.tv_at_silo_access.value, SiloAccess, machine=TransportVehicle),
default_initial_value=None)
# [REAL] Machine transit time (off field)
self._add_fluent(problem_settings,
Fluent(fn.harv_transit_time.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.max_harv_transit_time),
machine=Harvester),
default_initial_value=0)
self._add_fluent(problem_settings,
Fluent(fn.tv_transit_time.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.max_tv_transit_time),
machine=TransportVehicle),
default_initial_value=0)
# [REAL] Machine transit speeds (off field)
min_val = -1 if fvr.harv_max_transit_speed_empty.min is None else fvr.harv_max_transit_speed_empty.min
self._add_fluent(problem_settings,
Fluent(fn.harv_transit_speed_empty.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.harv_max_transit_speed_empty.max),
machine=Harvester),
default_initial_value=self._get_fraction(min_val))
min_val = self._get_fraction( -1 if fvr.tv_max_transit_speed_empty.min is None else fvr.tv_max_transit_speed_empty.min )
self._add_fluent(problem_settings,
Fluent(fn.tv_transit_speed_empty.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.tv_max_transit_speed_empty.max),
machine=TransportVehicle),
default_initial_value=self._get_fraction(min_val))
min_val = self._get_fraction( -1 if fvr.tv_max_transit_speed_full.min is None else fvr.tv_max_transit_speed_full.min )
self._add_fluent(problem_settings,
Fluent(fn.tv_transit_speed_full.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.tv_max_transit_speed_full.max),
machine=TransportVehicle),
default_initial_value=self._get_fraction(min_val))
# [BOOL] Workaround to enable/force planning of driving to a field or silo in a window
self._add_fluent(problem_settings,
Fluent(fn.harv_enabled_to_drive_to_loc.value, BoolType(), machine=Harvester),
default_initial_value=True)
self._add_fluent(problem_settings,
Fluent(fn.tvs_all_enabled_to_drive_to_field.value, BoolType()),
default_initial_value=True)
self._add_fluent(problem_settings,
Fluent(fn.tvs_all_enabled_to_arrive_in_field.value, BoolType(), field=Field),
default_initial_value=False)
self._add_fluent(problem_settings,
Fluent(fn.tv_enabled_to_drive_to_field.value, BoolType(), machine=TransportVehicle),
default_initial_value=True)
self._add_fluent(problem_settings,
Fluent(fn.tv_enabled_to_drive_to_silo.value, BoolType(), machine=TransportVehicle),
default_initial_value=True)
# [BOOL] Workaround to enable/force planning of driving to a field exit in a window
self._add_fluent(problem_settings,
Fluent(fn.harv_enabled_to_drive_to_field_exit.value, BoolType(), machine=Harvester),
default_initial_value=True)
self._add_fluent(problem_settings,
Fluent(fn.tv_enabled_to_drive_to_field_exit.value, BoolType(), machine=TransportVehicle),
default_initial_value=True)
# [BOOL] Workaround to enable/force planning of harvesting+overload in a window
# 0 := disable overloading for harvester in cases where it is acceptable (e.g. it arrived in the field or finished the previous overload and must wait for a TV)
# -1 := disable in critical cases (e.g., the corresponding TV is in the field and should start overloading right ahead)
# >0 := overloading id of the next TV
self._add_fluent(problem_settings,
Fluent(fn.harv_enabled_to_overload.value,
self._get_int_type(fvr.with_int_bounds, -1, fvr.max_overloading_activities_field),
machine=Harvester),
default_initial_value=0)
# [REAL] Time the machine spends waiting to overload or drive [s]
self._add_fluent(problem_settings,
Fluent(fn.harv_waiting_time.value,
self._get_real_type(fvr.with_real_bounds, 0, None),
machine=Harvester),
default_initial_value=0)
self._add_fluent(problem_settings,
Fluent(fn.tv_waiting_time.value,
self._get_real_type(fvr.with_real_bounds, 0, None),
machine=TransportVehicle),
default_initial_value=0)
# --------------HARVESTER FLUENTS--------------
# [REAL/CONST] Time needed by a harvester to work one square meter [s/m2]
min_val = 0 if fvr.harv_working_time_per_area.min is None else fvr.harv_working_time_per_area.min
self._add_fluent(problem_settings,
Fluent(fn.harv_working_time_per_area.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.harv_working_time_per_area.max),
machine=Harvester),
default_initial_value=self._get_fraction(min_val))
# [INT] Number of planned overloads
self._add_fluent(problem_settings,
Fluent(fn.harv_overload_count.value,
self._get_int_type(fvr.with_int_bounds, -1, fvr.max_overloading_activities_field),
machine=Harvester),
default_initial_value=-1)
# [INT] Current overload
self._add_fluent(problem_settings,
Fluent(fn.harv_overload_id.value,
self._get_int_type(fvr.with_int_bounds, -1, fvr.max_overloading_activities_field),
machine=Harvester),
default_initial_value=-1)
# [BOOL] The harvester is overloading
self._add_fluent(problem_settings,
Fluent(fn.harv_overloading.value, BoolType(), machine=Harvester),
default_initial_value=False)
# [BOOL] The harvester is waiting to harvest
self._add_fluent(problem_settings,
Fluent(fn.harv_waiting_to_harvest.value, BoolType(), machine=Harvester),
default_initial_value=False)
# [INT] Current field turn
self._add_fluent(problem_settings,
Fluent(fn.harv_field_turn.value,
self._get_int_type(fvr.with_int_bounds, 0, fvr.count_fields_to_work),
# self._get_int_type(fvr.with_int_bounds, 0, None if fvr.count_fields_to_work is None else 10*fvr.count_fields_to_work), #@todo apparently tamer tries to plan invalid actions that try to set this fluent over the limit
machine=Harvester),
default_initial_value=0)
# [INT] Amount of (field) turns assigned to the harvester
self._add_fluent(problem_settings,
Fluent(fn.harv_count_pre_assigned_field_turns.value,
self._get_int_type(fvr.with_int_bounds, 0, fvr.count_fields_to_work),
machine=Harvester),
default_initial_value=0)
# [INT] Current TV turn
self._add_fluent(problem_settings,
Fluent(fn.harv_tv_turn.value,
self._get_int_type(fvr.with_int_bounds, 0, fvr.max_overloading_activities_all),
# self._get_int_type(fvr.with_int_bounds, 0, None if fvr.max_overloading_activities_all is None else 10*fvr.max_overloading_activities_all), #@todo apparently tamer tries to plan invalid actions that try to set this fluent over the limit
machine=Harvester),
default_initial_value=0)
# [INT] Amount of (tv) overloading turns left assigned to the harvester
self._add_fluent(problem_settings,
Fluent(fn.harv_pre_assigned_tv_turns_left.value,
self._get_int_type(fvr.with_int_bounds, -fvr.max_overloading_activities_all if fvr.max_overloading_activities_all is not None else None,
fvr.count_tvs),
machine=Harvester),
default_initial_value=0)
# --------------TV FLUENTS--------------
# [REAL] Machine total mass capacity
min_val = 0 if fvr.tv_mass_capacity.min is None else fvr.tv_mass_capacity.min
self._add_fluent(problem_settings,
Fluent(fn.tv_total_capacity_mass.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.tv_mass_capacity.max),
machine=TransportVehicle),
default_initial_value=self._get_fraction(min_val))
# [REAL/CONST] Machine unloading speed [kg/s] / [m³/s]
min_val = 1 if fvr.tv_unloading_speed_mass.min is None else fvr.tv_unloading_speed_mass.min
self._add_fluent(problem_settings,
Fluent(fn.tv_unloading_speed_mass.value,
self._get_real_type(fvr.with_real_bounds, min_val, fvr.tv_unloading_speed_mass.max),
machine=TransportVehicle),
default_initial_value=self._get_fraction(min_val))
# [REAL] Machine current bunker mass and volume (volume not supported at the moment)
self._add_fluent(problem_settings,
Fluent(fn.tv_bunker_mass.value,
self._get_real_type(fvr.with_real_bounds, 0, fvr.tv_mass_capacity.max),
machine=TransportVehicle),
default_initial_value=0)
# [BOOL] Is the transport vehicle at a silo access ready to unload
self._add_fluent(problem_settings,
Fluent(fn.tv_ready_to_unload.value, BoolType(), machine=TransportVehicle),
default_initial_value=False)
# [INT] Reserved overload
self._add_fluent(problem_settings,
Fluent(fn.tv_overload_id.value,
self._get_int_type(fvr.with_int_bounds, -1, fvr.max_overloading_activities_field),
machine=TransportVehicle),
default_initial_value=-1)
# [REAL] Mass to be overloaded to the TV
self._add_fluent(problem_settings, Fluent(fn.tv_mass_to_overload.value,