Repository navigation
Expand file tree
/
Copy pathactor_model.py
More file actions
1427 lines (1160 loc) · 78.2 KB
/
Copy pathactor_model.py
File metadata and controls
1427 lines (1160 loc) · 78.2 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
from abc import ABC, abstractmethod
from enum import Enum, auto
import numpy as np
class ActorStatus(Enum):
"""The (discrete) status of an actor."""
ACTIVATE = auto()
UP = auto()
HIT = auto()
DOWN = auto()
RESET = auto()
READY = auto()
class Rectangle:
"""A rectangle in 2D space.
The rectangle is defined by its lower left (x1, y1) and upper right (x2, y2) corner.
From https://stackoverflow.com/questions/25068538/intersection-and-difference-of-two-rectangles
"""
def __init__(self, x1, y1, x2, y2):
"""Initializes the rectangle.
:param x1: A float, the x-coordinate of the lower left corner.
:param y1: A float, the y-coordinate of the lower left corner.
:param x2: A float, the x-coordinate of the upper right corner.
:param y2: A float, the y-coordinate of the upper right corner.
"""
self._x1, self._y1, self._x2, self._y2 = min(x1, x2), min(y1, y2), max(x1, x2), max(y1, y2)
def __and__(self, other):
"""Calculates the intersection of two rectangles.
:param other: A Rectangle object, the other rectangle.
:returns: None or a Rectangle, the intersection of the two rectangles.
"""
x1, y1, x2, y2 = max(self.x1, other.x1), max(self.y1, other.y1), \
min(self.x2, other.x2), min(self.y2, other.y2)
if x1 < x2 and y1 < y2:
return type(self)(x1, y1, x2, y2)
@property
def x1(self):
"""The x-coordinate of the lower left corner.
:returns: A float, the x-coordinate of the lower left corner.
"""
return self._x1
@property
def y1(self):
"""The y-coordinate of the lower left corner.
:returns: A float, the y-coordinate of the lower left corner.
"""
return self._y1
@property
def x2(self):
"""The x-coordinate of the upper right corner.
:returns: A float, the x-coordinate of the upper right corner.
"""
return self._x2
@property
def y2(self):
"""The y-coordinate of the upper right corner.
:returns: A float, the y-coordinate of the upper right corner.
"""
return self._y2
intersection = __and__
class AbstractActorModel(ABC):
"""Abstract class for all actor models of the controller and simulator (either for 1D or 2D optical sorters).
Along with the particle and the contact model, the actor model forms the system model of the stochastic model
predictive controller.
Assumptions:
- The actor model is deterministic.
- The state is fully observable.
- The actors dynamic behavior is identical for all actors and does not change with time (no abrasion, etc.).
Dynamic model:
There exist different versions depending on whether the model is formulated in discrete or continuous time.
With going back to READY status:
Actor
status
s_{j,k}^act ^
|
HIT | ___________
| | |
UP/DOWN | _____| |_____ slide/belt surface
| ------------|-----------------------|-------------------------------
ACTIVATE | _____| |
READY | ______| | _____________
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
With direct reactivation:
Actor
status
s_{j,k}^act ^
|
HIT | ___________ __
| | | |
UP/DOWN | _____| |_____ slide/belt surface _____|
| ------------|-----------------------|-----------------------|--------
ACTIVATE | _____| | _____|
READY | ______| | |
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
"""
def __init__(self,
pos,
length,
T,
t_activate,
t_up,
t_hit,
t_down,
t_reset,
width=None,
):
"""Initializes the actor model.
:param pos: A np.array of shape [num_actor] or [num_actors, 2] representing the positions of the actors. If
the shape is [num_actors], an 1D optical sorter (groove sorter) is assumed and the positions need to be
sorted from smallest to largest position. If the shape is [num_actors, 2], a 2D optical sorter (belt or
chute) is assumed. In this case, the position format is [x, y], with the x-axis corresponding the transport
direction, and pos needs to be sorted from smallest x-position to largest x-position. Actors with the same
x-position need to be sorted from smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in
x-direction) of the actors.
:param T: A float representing the time interval between two consecutive time steps.
:param t_activate: A float, the time interval after an activation until the actor reaches the surface.
:param t_up: A float, the time interval above the surface until reaching the optimal hitting time. The particle
is not ejected but disturbed or only ejected with a reduced probability when hit during this time period.
:param t_hit: A float, the time interval when a hitting is optimal, i.e., the particles is ejected.
:param t_down: A float, the time interval above the surface after t_hit until reaching the surface again.
The particle is not ejected but disturbed or only ejected with a reduced probability when hit during this
time period.
:param t_reset: A float, the reset time. The time interval after an activation before a new activation is
possible.
:param width: None or a float or a np.array of shape [num_actors] representing the with (operating ranges in
y-direction) of the actors. If None, an 1D optical sorter (groove sorter) is assumed. If given, a 2D
optical sorter (belt or chute) is assumed.
"""
if np.shape(pos) != (np.shape(pos)[0], 2) and np.shape(pos) != (np.shape(pos)[0],):
raise ValueError('Actor positions must be of shape [num_actors] or [num_actors, 2].')
if np.ndim(pos) == 1 and width is not None or np.ndim(pos) != 1 and width is None:
raise ValueError('If width is given, pos must be of shape [num_actors, 2] and vice versa.')
if np.any(np.asarray(length) < 0):
raise ValueError('Actor lengths must be greater equal zero.')
if not np.isscalar(length) and np.shape(pos)[0] != np.shape(length)[0]:
raise ValueError('If length is an array, it needs num_actors elements.')
if width is not None and np.any(np.asarray(width) < 0):
raise ValueError('Actor width must be greater equal zero.')
if width is not None and not np.isscalar(width) and np.shape(pos)[0] != len(width):
raise ValueError('If width is an array, it needs num_actors elements.')
if np.ndim(pos) == 1 and np.any(np.diff(pos) <= 0):
raise ValueError('Actor positions must be ordered so that pos is a strictly increasing array.')
if np.ndim(pos) == 2:
for index in range(np.shape(pos)[0] - 1):
if pos[index + 1][0] < pos[index][0]:
raise ValueError('Actor positions must be ordered from smallest x-position to largest x-position')
elif pos[index + 1][0] == pos[index][0]:
if pos[index + 1][1] < pos[index][1]:
raise ValueError(
'Actor positions must be ordered from smallest y-position to largest y-position'
'when the x-position stays the same')
if not np.isscalar(T) or T <= 0:
raise ValueError('The time interval between two consecutive time steps T must be a positive scalar.')
if np.any(np.array([t_activate, t_up, t_hit, t_down, t_reset]) < 0):
raise ValueError('Actor time intervals must be greater equal zero.')
self._pos = np.asarray(pos) # np.array of length num_actors
self._length = length * np.ones(self._pos.shape[0]) # np.array of length num_actors
self._width = width * np.ones(self._pos.shape[0]) if width is not None else None # None or np.array of
# width num_actors
# check if the actors are non-overlapping
if np.ndim(self._pos) == 1:
self._check_one_dimensional_actor_grid()
else:
self.check_two_dimensional_actor_grid(self._pos, self._length, self._width)
self._T = float(T)
self._t_activate = float(t_activate)
self._t_up = float(t_up)
self._t_hit = float(t_hit)
self._t_down = float(t_down)
self._t_reset = float(t_reset)
@property
def pos(self):
"""The position of the actors.
Format position:
[x] if 1D optical sorter (groove sorter), [x, y] if 2D optical sorter (belt or chute)
If the shape is [num_actors], the positions are sorted from smallest to largest position. If the shape is
[num_actors, 2], it is sorted from smallest x-position to largest x-position. Actors with the same x-position
need to be sorted from smallest y-position to largest y-position.
:returns: A np.array of shape [num_actors] or [num_actors, 2] representing the positions of the actors.
"""
return self._pos
@property
def length(self):
"""The lengths (operating ranges in x-direction) of the actors.
:returns: A np.array of shape [num_actors] representing the lengths (operating ranges in x-direction) of the
actors.
"""
return self._length
@property
def width(self):
"""The widths (operating ranges in y-direction) of the actors.
:returns: None or a np.array of shape [num_actors] representing the widths (operating ranges in y-direction) of
the actors.
"""
return self._width
@property
def num_rows(self):
"""The number of rows of the actors.
:returns: An integer, the number of rows of the actors.
"""
return len(np.unique(self._pos[:, 0] if self._pos.ndim == 2 else self._pos))
@property
def actors_in_row(self):
"""The number of columns of the actors.
:returns: A np.array of shape [num_rows], the number of columns of the actors.
"""
return np.unique(self._pos[:, 0] if self._pos.ndim == 2 else self._pos, return_counts=True)[1]
@property
def actor_grid(self):
"""The positions, lengths (and widths) of the actors in grid format.
Format actor_grid:
[x, length] if 1D optical sorter (groove sorter), [x, y, length, width] if 2D optical sorter (belt or chute)
:returns: A list of length num_rows of np.array of shape [actors_in_row, 2 or 4] representing the positions
and lengths (and widths) of the actors in grid format.
"""
return self._transform_to_actor_grid_data(self._pos, self._length, self._width)
@property
def row_pos(self):
"""The x-positions of the actors in grid format.
:returns: A np.array of shape [num_rows] representing the x-positions of the actors in grid format.
"""
return np.array([pos_row[0, 0] for pos_row in self.actor_grid])
@property
def column_pos(self):
"""The y-positions of the actors in grid format.
:returns: None or a list of length num_rows of np.array of shape [actors_in_row] representing the y-positions of
the actors in grid format.
"""
return [pos_row[:, 1] for pos_row in self.actor_grid] if self._pos.ndim == 2 else None
@property
def T(self):
"""The sampling time difference.
:returns: A float representing the time interval between two consecutive time steps.
"""
return self._T
@property
def t_activate(self):
"""The time interval after an activation until the actor reaches the surface.
:returns: A float, the time interval after an activation until the actor reaches the surface.
"""
return self._t_activate
@property
def t_up(self):
"""The time interval above the surface until reaching the optimal hitting time. The particle is not ejected but
disturbed or only ejected with a reduced probability when hit during this time period.
:returns: A float, the time interval above the surface until reaching the optimal hitting time. The particle
is not ejected but disturbed when hit during this time period.
"""
return self._t_up
@property
def t_hit(self):
"""The time interval when a hitting is optimal, i.e., the particles is ejected.
:returns: A float, the time interval when a hitting is optimal, i.e., the particles is ejected.
"""
return self._t_hit
@property
def t_down(self):
"""The time interval above the surface after t_hit until reaching the surface again. The particle is not ejected
but disturbed or only ejected with a reduced probability when hit during this time period.
:returns: A float, the time interval above the surface after t_hit until reaching the surface again. The particle
is not ejected but disturbed when hit during this time period.
"""
return self._t_down
@property
def t_reset(self):
"""The reset time. The time interval after an activation before a new activation is possible.
:returns: A float, the reset time. The time interval after an activation before a new activation is possible.
"""
return self._t_reset
@property
def t_cycle(self):
"""The time for one actor cycle, i.e., the time that is required by the actor before the next ejection process
can be started.
:returns: A float, the time for one actor cycle.
"""
return self._t_activate + self._t_up + self._t_hit + self._t_down + self._t_reset
@property
def t_lead(self):
"""The smallest possible time interval before time step k at which the actuator must be activated so that it is
able to hit at time step k.
:returns: A float, the smallest possible time interval before time step k for hitting at k.
"""
return self._t_activate + self._t_up + self._t_hit / 2
@property
def end_array(self):
"""The end coordinate of the actuator array, i.e., the position of the last actor in transport direction.
:returns: A float, the end coordinate of the actuator array.
"""
return self.pos[-1] if self._pos.ndim == 1 else float(np.max(self.pos[:, 0]))
@abstractmethod
def predict_actor_state(self, *args):
"""Calculates the actors' internal states at the next time step.
:param args: Additional arguments that are required to calculate the actors' internal states at the next time
step. See the subclasses for more information.
:returns: A np.array of shape [num_actors_to_consider], the updated actor states.
"""
# To be overwritten by subclass
raise NotImplementedError('Call to abstract method.')
def _transform_to_actor_grid_data(self, pos, length, width=None):
"""Transforms the positions, lengths (and widths) of the actors in grid format.
Format actor_grid:
[x, length] if 1D optical sorter (groove sorter), [x, y, length, width] if 2D optical sorter (belt or chute)
:param pos: A np.array of shape [num_actors] or [num_actors, 2] representing the positions of the actors. If
the shape is [num_actors], an 1D optical sorter (groove sorter) is assumed and the positions need to be
sorted from smallest to largest position. If the shape is [num_actors, 2], a 2D optical sorter (belt or
chute) is assumed. In this case, the position format is [x, y], with the x-axis corresponding the transport
direction, and pos needs to be sorted from smallest x-position to largest x-position. Actors with the same
x-position need to be sorted from smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in
x-direction) of the actors.
:param width: None or a float or a np.array of shape [num_actors] representing the with (operating ranges in
y-direction) of the actors. If None, an 1D optical sorter (groove sorter) is assumed. If given, a 2D
optical sorter (belt or chute) is assumed.
:returns: A list of length num_rows of np.array of shape [actors_in_row, 2 or 4] representing the positions
and lengths (and widths) of the actors in grid format.
"""
length_width = np.column_stack((length, width)) if width is not None else length
grid_data = np.column_stack((pos, length_width))
return np.split(grid_data, np.cumsum(self.actors_in_row)[:-1])
def _check_one_dimensional_actor_grid(self):
"""Checks if the actors are not overlapping.
If the actors are overlapping, a ValueError is raised.
"""
for row_idx, (pos, length) in enumerate(zip(self._pos[:-1], self._length[:-1])):
# check if the actors are not overlapping
next_pos, next_length = self._pos[row_idx + 1], self._length[row_idx + 1]
if pos + length / 2 > next_pos - next_length / 2:
raise ValueError(
f'The operating ranges of the actors {row_idx + 1} and {row_idx + 2} must not overlap.')
def check_two_dimensional_actor_grid(self, pos, length, width):
"""Checks if the actors are not overlapping within the rows and between the rows.
Each actor is compared with the next actor in the same row and with all actors in the next row. If the actors
are overlapping (the intersection is not None), a ValueError is raised.
:param pos: A np.array of shape [num_actors, 2] representing the positions of the actors. pos needs to be sorted
from smallest x-position to largest x-position. Actors with the same x-position need to be sorted from
smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in
x-direction) of the actors.
:param width: A float or a np.array of shape [num_actors] representing the with (operating ranges in
y-direction) of the actors.
"""
actor_grid = self._transform_to_actor_grid_data(pos, length, width)
for row_idx, actor_row in enumerate(actor_grid):
# check if the actors are not overlapping within the row
for column_idx in range(len(actor_row) - 1):
actor = actor_row[column_idx]
next_actor = actor_row[column_idx + 1]
actor_rect = Rectangle(actor[0] - actor[2] / 2,
actor[1] - actor[3] / 2,
actor[0] + actor[2] / 2,
actor[1] + actor[3] / 2)
next_actor_rect = Rectangle(next_actor[0] - next_actor[2] / 2,
next_actor[1] - next_actor[3] / 2,
next_actor[0] + next_actor[2] / 2,
next_actor[1] + next_actor[3] / 2)
intersection = actor_rect & next_actor_rect
if intersection is not None:
raise ValueError(
f'The operating ranges of the actors "row {row_idx + 1}, column {column_idx + 1} and '
f'{column_idx + 2}" must not overlap.')
# check if the actors are not overlapping with the actors in the next row
if row_idx + 1 < len(actor_grid):
next_row = actor_grid[row_idx + 1]
for column_idx in range(len(actor_row)):
actor = actor_row[column_idx]
actor_rect = Rectangle(actor[0] - actor[2] / 2,
actor[1] - actor[3] / 2,
actor[0] + actor[2] / 2,
actor[1] + actor[3] / 2)
for next_column_idx in range(len(next_row)):
next_actor = next_row[next_column_idx]
next_actor_rect = Rectangle(next_actor[0] - next_actor[2] / 2,
next_actor[1] - next_actor[3] / 2,
next_actor[0] + next_actor[2] / 2,
next_actor[1] + next_actor[3] / 2)
intersection = actor_rect & next_actor_rect
if intersection is not None:
raise ValueError(
f'The operating ranges of the actors "row {row_idx + 1}, column {column_idx + 1} and '
f'row {row_idx + 2}, column {next_column_idx + 1}" must not overlap.')
class ActorModelVariantB2(AbstractActorModel):
"""Actor model for variant B2 (continuous-time with intermediate time steps) of the controllers and for heuristics
(either for 1D or 2D optical sorters).
Along with the particle and the contact model, the actor model forms the system model of the stochastic model
predictive controller.
Variant B2 of the controller maps particles to activation times, i.e., an actor is activated so that at the time
when a particle is expected to pass the actor, the actor is supposed to hit the particle. The controllers then only
decide on the association of particles to actors.
Assumptions:
- The actor model is deterministic.
- The state is fully observable.
- The actors dynamic behavior is identical for all actors and does not change with time (no abrasion, etc.).
Dynamic model:
This actor model simply predicts the actor movement in continuous time without any notion of time steps.
With going back to READY status:
Actor
status
s_{j,k}^act ^
|
HIT | ___________
| | |
UP/DOWN | _____| |_____ slide/belt surface
| ------------|-----------------------|-------------------------------
ACTIVATE | _____| |
READY | ______| | _____________
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
With direct reactivation:
Actor
status
s_{j,k}^act ^
|
HIT | ___________ __
| | | |
UP/DOWN | _____| |_____ slide/belt surface _____|
| ------------|-----------------------|-----------------------|--------
ACTIVATE | _____| | _____|
READY | ______| | |
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
"""
def __init__(self,
pos,
length,
T,
t_activate,
t_up,
t_hit,
t_down,
t_reset,
width=None,
):
"""Initializes the actor model.
:param pos: A np.array of shape [num_actor] or [num_actors, 2] representing the positions of the actors. If
the shape is [num_actors], an 1D optical sorter (groove sorter) is assumed and the positions need to be
sorted from smallest to largest position. If the shape is [num_actors, 2], a 2D optical sorter (belt or
chute) is assumed. In this case, the position format is [x, y], with the x-axis corresponding the transport
direction, and pos needs to be sorted from smallest x-position to largest x-position. Actors with the same
x-position need to be sorted from smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in
x-direction) of the actors.
:param T: A float representing the time interval between two consecutive time steps.
:param t_activate: A float, the time interval after an activation until the actor reaches the surface.
:param t_up: A float, the time interval above the surface until reaching the optimal hitting time. The particle
is not ejected but disturbed or only ejected with a reduced probability when hit during this time period.
:param t_hit: A float, the time interval when a hitting is optimal, i.e., the particles is ejected.
:param t_down: A float, the time interval above the surface after t_hit until reaching the surface again.
The particle is not ejected but disturbed or only ejected with a reduced probability when hit during this
time period.
:param t_reset: A float, the reset time. The time interval after an activation before a new activation is
possible.
:param width: None or a float or a np.array of shape [num_actors] representing the with (operating ranges in
y-direction) of the actors. If None, an 1D optical sorter (groove sorter) is assumed. If given, a 2D
optical sorter (belt or chute) is assumed.
"""
super().__init__(pos=pos,
length=length,
T=T,
t_activate=t_activate,
t_up=t_up,
t_hit=t_hit,
t_down=t_down,
t_reset=t_reset,
width=width,
)
def predict_actor_state(self, actors_next_time_ready, delta_toa, actor_indices, particle_indices):
"""Predicts the actors' next time in ready status given actor activations.
Here, it is assumed that actors are activated such that they hit the particles at the respective expected
arrival time at the actors.
:param actors_next_time_ready: A np.array of shape [num_actors] containing the time when the actor will be
in READY status for the first time again as delta w.r.t. the current time step.
:param delta_toa: A np.array of shape [num_actors, num_particles], the particles' time of arrival as delta
w.r.t. the current time step.
:param actor_indices: An integer np.array or an integer, index or indices of the actors to be activated.
:param particle_indices: An integer np.array or an integer, index or mask of the particles to be ejected.
:returns: actors_next_time_ready: A np.array of shape [num_actors] containing the time when the actor will be
in READY status for the first time again as delta w.r.t. the current time step.
"""
# we check the type of particle indices since the required type was recently changed
# TODO: Remove this again in future versions
assert isinstance(particle_indices, int) or issubclass(particle_indices.dtype.type, np.integer), \
"particle_indices must be an integer or an integer np.array."
actors_next_time_ready[actor_indices] = delta_toa[actor_indices, particle_indices] + self.t_cycle - self.t_lead
return actors_next_time_ready
class ActorModelVariantA(AbstractActorModel):
"""Actor model for variant A (discrete actor activation time) of the controller (either for 1D or 2D optical
sorters).
Along with the particle and the contact model, the actor model forms the system model of the stochastic model
predictive controller.
Variant A of the controller in each time steps uses the controls
u_k = [ u_{1,k} ... u_{N_A,k} ]^T
with binary components u_{j,k} in {0, 1} where u_{j,k} = 1 ≙ “activate actor 𝑗 at time step 𝑘” and N_A being the
number of actors in the setup.
Assumptions:
- The actor model is deterministic.
- The state is fully observable.
- The actors dynamic behavior is identical for all actors and does not change with time (no abrasion, etc.).
Dynamic model:
With going back to READY status:
Actor
status
s_{j,k}^act ^
|
HIT | ___________
| | |
UP/DOWN | _____| |_____ slide/belt surface
| ------------|-----------------------|-------------------------------
ACTIVATE | _____| |
READY | ______| | _____________
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
Example 0 0 1 2 3 4 5 6 7 8 0 0
(with T=1)
With direct reactivation:
Actor
status
s_{j,k}^act ^
|
HIT | ___________ __
| | | |
UP/DOWN | _____| |_____ slide/belt surface _____|
| ------------|-----------------------|-----------------------|--------
ACTIVATE | _____| | _____|
READY | ______| | |
RESET | |_________________|
--------------------------------------------------------|--------------->
|––> t_{j,k}^act t_cycle^act Time t
Example 0 0 1 2 3 4 5 6 7 8 1 2
(with T=1)
The dynamics of actor j is given by
{ t_{j,k}^act + T , if t_{j,k}^act not in {0, t_cycle^act}
t_{j,k+1}^act = { T , if u_{j,k} = 1 and t_{j,k}^act in {0, t_cycle^act}
{ 0 , else ,
where t_{j,k}^act describes the actor's internal time since its last activation (measured at the right edge of
the discrete time interval) and t_cycle^act is the time for one actor cycle, i.e., the time that is required by
the actor before the next ejection process can be started.
The discrete status of the actor s_{j,k}^act is given by the corresponding output equation that maps
t_{j,k}^act to s_{j,k}^act according to
{ ACTIVATE , if 0 < t_{j,k}^act <= t_activate^act
{ UP , if t_activate^act < t_{j,k}^act <= t_activate^act + t_up^act
s_{j,k}^act = { HIT , if t_activate^act + t_up^act < t_{j,k}^act <= t_activate^act + t_up^act + t_hit^act
{ DOWN , if t_activate^act + t_up^act + t_hit^act < t_{j,k}^act
<= t_activate^act + t_up^act + t_hit^act + t_down^act
{ RESET , if t_activate^act + t_up^act + t_hit^act + t_down^act < t_{j,k}^act
<= t_activate^act + t_up^act + t_hit^act + t_down^act + t_reset^act
{ READY , else .
Here, t_activate^act, t_up^act, t_hit^ac, t_down^act, t_reset^act with t_activate^act + t_up^act + t_hit^act +
t_down^act + t_reset^act = t_cycle^act are time intervals. s_{j,k}^act models the relevant stages in which the
actor interacts with its environment, in particular, with the particles in its operating range. See the
contact model for an explanation.
"""
def __init__(self,
pos,
length,
T,
t_activate,
t_up,
t_hit,
t_down,
t_reset,
width=None,
):
"""Initializes the actor model.
:param pos: A np.array of shape [num_actor] or [num_actors, 2] representing the positions of the actors. If
the shape is [num_actors], an 1D optical sorter (groove sorter) is assumed and the positions need to be
sorted from smallest to largest position. If the shape is [num_actors, 2], a 2D optical sorter (belt or
chute) is assumed. In this case, the position format is [x, y], with the x-axis corresponding the transport
direction, and pos needs to be sorted from smallest x-position to largest x-position. Actors with the same
x-position need to be sorted from smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in
x-direction) of the actors.
:param T: A float representing the time interval between two consecutive time steps.
:param t_activate: A float, the time interval after an activation until the actor reaches the surface.
:param t_up: A float, the time interval above the surface until reaching the optimal hitting time. The particle
is not ejected but disturbed or only ejected with a reduced probability when hit during this time period.
:param t_hit: A float, the time interval when a hitting is optimal, i.e., the particles is ejected.
:param t_down: A float, the time interval above the surface after t_hit until reaching the surface again.
The particle is not ejected but disturbed or only ejected with a reduced probability when hit during this
time period.
:param t_reset: A float, the reset time. The time interval after an activation before a new activation is
possible.
:param width: None or a float or a np.array of shape [num_actors] representing the with (operating ranges in
y-direction) of the actors. If None, an 1D optical sorter (groove sorter) is assumed. If given, a 2D
optical sorter (belt or chute) is assumed.
"""
if np.any(np.mod(np.array([t_activate, t_up, t_hit, t_down, t_reset]), T) != 0):
raise ValueError('Actor time intervals must be multiples of the time interval T.')
super().__init__(pos=pos,
length=length,
T=T,
t_activate=t_activate,
t_up=t_up,
t_hit=t_hit,
t_down=t_down,
t_reset=t_reset,
width=width,
)
def predict_actor_state(self, t_act, u_act):
"""Calculates the actors' internal time and status at the next time step.
:param t_act: A np.array of shape [num_actors] representing the actors' internal time t_k^act at time step
k.
:param u_act: A Boolean np.array of shape [num_actors] representing the control inputs u_k at time step k, where
True indicates that the actor should be activated in the current time step.
:returns:
t_act: A np.array of shape [num_actors] representing the actors' internal time t_{k+1}^act at time step k+1.
s_act: An object np.array of shape [num_actors] representing the actors' status s_{k+1}^act at time step
k+1.
"""
# increment the internal actor time (dynamic equation)
t_act = np.where(np.logical_not(
np.isin(t_act, [0, self._t_activate + self._t_up + self._t_hit + self._t_down + self._t_reset])),
t_act + self._T, 0.0)
t_act[np.logical_and(u_act, t_act == 0)] = self._T # because it was already set to 0 if
# t_act == self.t_cycle - self._T by the previous line
s_act = self.output_actor_status(t_act)
return t_act, s_act
def output_actor_status(self, t_act):
"""Calculates the actors' status given the current actor time.
This function constitutes the output equation of the actor model.
:param t_act: A np.array of shape [num_actors] representing the actors' internal time t_k^act at time step k.
:returns:
s_act: An object np.array of shape [num_actors] representing the actors' status s_k^act at time step k.
"""
activate_mask = np.logical_and(0 < t_act, t_act <= self._t_activate)
up_mask = np.logical_and(self._t_activate < t_act, t_act <= self._t_activate + self._t_up)
hit_mask = np.logical_and(self._t_activate + self._t_up < t_act,
t_act <= self._t_activate + self._t_up + self._t_hit)
down_mask = np.logical_and(self._t_activate + self._t_up + self._t_hit < t_act,
t_act <= self._t_activate + self._t_up + self._t_hit + self._t_down)
reset_mask = np.logical_and(self._t_activate + self._t_up + self._t_hit + self._t_down < t_act,
t_act <= self._t_activate + self._t_up + self._t_hit + self._t_down + self._t_reset)
s_act = np.array(len(t_act) * [ActorStatus.READY], dtype=object)
s_act[activate_mask] = ActorStatus.ACTIVATE
s_act[up_mask] = ActorStatus.UP
s_act[hit_mask] = ActorStatus.HIT
s_act[down_mask] = ActorStatus.DOWN
s_act[reset_mask] = ActorStatus.RESET
return s_act
class ActorModelVariantB(AbstractActorModel):
"""Actor model for variant B (continuous-time with intermediate time steps) of the controller (either for 1D or 2D
optical sorters).
Along with the particle and the contact model, the actor model forms the system model of the stochastic model
predictive controller.
Variant B of the controller in each time steps uses the controls
u_k = [ u_{1,k} ... u_{N_A,k} ]^T
with N_A being the number of actors in the setup and u_{j,k} in R is such that it coincides with the time
t_activate^act + t_up^act + t_hit^act / 2, i.e., it is the center time of the HIT-status.
# TODO: Potentially change the inputs from hitting to activation times (however, no issues are currently known)
Assumptions:
- The actor model is deterministic.
- The state is fully observable.
- The actors dynamic behavior is identical for all actors and does not change with time (no abrasion, etc.).
- The sampling time interval 𝑇 is smaller than or equal to the duration of one actor cycle.
===> At each time step, an actor can be activated at most once.
Additional conventions:
- All times t are given as delta times w.r.t. the current time step k, i.e., t in [0, T].
- We exclude u_k^act from the last convention and allow u_k^act in R to enable the use of unconstrained
optimization (although it should be constrained if possible)
Dynamic model:
^
Internal actor |
time t^act | |-------t_cycle^act--------|
| ...... ............ | ................ | ................. | ......
(t_cycle^act) | | | / |
(t^act_{k}(T)).... .. | ................. |/ |
| | /| | /
RESET | | ____ / | | /
| | 1 | / | | /
DOWN | | | / | |/
| | / | /|
HIT |------- -------------|--------/----------|----------------/--|-------
(t_hit_B) | ...... ............ | ... / | ....... | .......... / .. | ......
UP | | / | / | |
| | / | | / |
ACTIVATE (t^act_{k-1}(T))... ..|/ | / | |
| /| | | ___/ |
READY (0) | _________/ | | | | |
| | | u_k^act | u_{k+1}^act |
-------- ----------|--|--------|----------|------|-------|----|--------------->
c(k-1) |––> t_k^act c(k+1) Time t
k k+1 k+2 Time step
|---------T---------|
I) Forward actor dynamics
The dynamics of an actor j are given by a mapping from time t to internal time t^act. In general, this a
function
t^act = f(t, t^act_{k-1}(T), u_k^act) : [0, T] x [0, t_cycle^act) x R -> [0, t_cycle^act)
of time t, the actor status at the end of the previous time step t^act_{k-1}(T) and the control input u_k^act.
The function itself has a sawtooth profile with linear and constant (only t^act=0) segments. Note that the line has
a slope of 1 since time passes equally for both coordinate systems (time and internal time coordinate system). If
the actor moves the coordinate systems are shifted according to the equation
t^act = t - c . (we use a minus here for convenience.)
The shifting constant c can be determined by looking at the point
t^act_{k-1}(T)) = 0 - c . (see first window of the sketch above.)
or
t_activate^act + t_up^act + t_hit^act / 2 = u_k^act - c(k) , (see second window of the sketch above)
--> c(k) = u_k^act - (t_activate^act + t_up^act + t_hit^act / 2),
(i.e., c(k) = the actor's activation time)
There are three cases to consider:
i) the actor movement is due to a previous activation (due to t^act_{k-1}(T)),
ii) the actor movement is due to an activation in the current time step (due to u_k^act), or
iii) there is no actor movement.
Case i) Actor movement due to a previous activation:
t^act = t + t^act_{k-1}(T) if t^act_{k-1}(T) > 0 , i.e., there is a previous activation
and
t + t^act_{k-1}(T) < t_cycle^act , i.e., t is still influenced by
t^act_{k-1}(T).
Case ii) Actor movement due to an activation in the current time step:
t^act = t - c(k) if 0 <= c(k) < T , i.e., there is an activation
and
c(k) >= t_cycle^act - t^act_{k-1}(T) , i.e., the activation is not within
if t^act_{k-1}(T) > 0 a previous actor movement
and
t >= t_cycle^act - t^act_{k-1}(T) , i.e., t is not influenced by
if t^act_{k-1}(T) > 0 t^act_{k-1}(T)
and
t >= c(k) , i.e., t is influenced by u_k^act.
Note that the first two requirements are independent of t and only check if u_k^act is a valid activation
while the latter two describe the time range in which the line segment is valid.
Furthermore, note that if t^act_{k-1}(T) > 0 and if t >= c(k) >= t_cycle^act - t^act_{k-1}(T) (fourth and
second condition), the third condition, t >= t_cycle^act - t^act_{k-1}(T), is automatically fulfilled.
(Also the fourth condition ensures the part c(k) < T of the first condition if t < T.)
Case iii) There is no actor movement:
t^act = 0 if not in case i) or case ii).
Note that this case results in ambiguities since t^act = 0 for a wide range of t, t^act_{k-1}(T), and u_k^act.
II) Inverse actor dynamics
Additionally to the forward actor dynamics, we require an inverse actor model outputting time given an internal
actor time (e.g., the time when a HIT-phase begins, depicted by t_hit_B in the sketch above), i.e., we need to
invert f(.). This results in a function
t = g(t^act, t^act_{k-1}(T), u_k^act) : (0, t_cycle^act) x [0, t_cycle^act) x R -> {[0, T) , inf}
that maps the internal actor time t^act, the actor status at the end of the previous time step t^act_{k-1}(T) and
the control input u_k^act to the time t.
Note that for t^act = 0, the continuous actor dynamics is not bijective while for all other t^act in
(0, t_cycle^act), the function is (because of the assumption that the sampling time interval 𝑇 is smaller than or
equal to the duration of one actor cycle t_cycle^act). For this reason, we restrict the domain of g(.) to the range
(0, t_cycle^act) where the inversion is properly (without ambiguities) defined.
We again need to consider the three cases defined before:
Case i) Actor movement due to a previous activation:
t = t^act - t^act_{k-1}(T) if t^act_{k-1}(T) > 0 , i.e., there is a previous activation
and
t^act_{k-1}(T) <= t^act , this comes from the requirement that
t^act(0) = t^act_{k-1}(T) + 0
<= t^act(t)
(otherwise t^act(t) cannot be
caused by t^act_{k-1}(T) ).
and
t^act - t^act_{k-1}(T) < T , this comes from the requirement that
t^act(T) = t^act_{k-1}(T) + T
> t^act(t)
(otherwise t^act(t) cannot be
reached within the current time
step).
Case ii) Actor movement due to an activation in the current time step:
t = t^act + c(k) if 0 <= c(k) < T , i.e., there is an activation
and
c(k) >= t_cycle^act - t^act_{k-1}(T) , i.e., the activation is not within
if t^act_{k-1}(T) > 0 a previous actor movement
and
t^act < t^act_{k-1}(T) , i.e., t^act is not influenced by
if t^act_{k-1}(T) > 0 t^act_{k-1}(T)
and
t^act < T - c(k) , this comes from the requirement that
t^act(T) = T - c(k) > t^act(t)
(otherwise t^act(t) cannot be
reached within the current time
step).
Note that the first two requirements are the same as before (check if u_k^act is a valid activation).
Furthermore, note that if t^act_{k-1}(T) > 0 and if t^act < T - c(k) <= t_cycle^act - c(k) <= t^act_{k-1}(T)
(fourth condition, T <= t_cycle^act by the assumptions, and second condition), the third condition
t^act < t^act_{k-1}(T) is automatically satisfied. (Also the fourth condition ensures the part c(k) < T of
the first condition since T - t^act <= T.)
Case iii) There is no actor movement:
t = np.inf if not in case i) or case ii).
"""
def __init__(self,
pos,
length,
T,
t_activate,
t_up,
t_hit,
t_down,
t_reset,
width=None,
):
"""Initializes the actor model.
:param pos: A np.array of shape [num_actor] or [num_actors, 2] representing the positions of the actors. If
the shape is [num_actors], an 1D optical sorter (groove sorter) is assumed and the positions need to be
sorted from smallest to largest position. If the shape is [num_actors, 2], a 2D optical sorter (belt or
chute) is assumed. In this case, the position format is [x, y], with the x-axis corresponding the transport
direction, and pos needs to be sorted from smallest x-position to largest x-position. Actors with the same
x-position need to be sorted from smallest y-position to largest y-position.
:param length: A float or a np.array of shape [num_actors] representing the lengths (operating ranges in