-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYuMiScheduler.mzn
1191 lines (945 loc) · 47.9 KB
/
YuMiScheduler.mzn
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
% MIT License
%
% Copyright (c) 2021 Johan Ludde Wessén
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%=============================================================================%
% YuMiScheduler
%
% Johan Ludde Wessén
% Latest Update 2021-02-21
%============================================================================%
include "globals.mzn";
include "gecode.mzn";
% model configuration flags
bool: implied_value_precede = true;
bool: implied_regular = false;
bool: implied_cumulative = false;
bool: implied_diffn = false;
int: no_agents = 2; % number of arms
int: no_locations = card(index_set_1of2(left_arm_travel_times) ); % number of locations
int: no_actual_tasks = card(index_set_2of2(task_durations)); % number of actual tasks, i.e. excluding dummy start & end tasks
int: no_tot_tasks = no_actual_tasks+2*no_agents; % all tasks, including dummy start & end tasks
int: min_duration = 1;
int: max_duration = max([ task_durations[r,n] | r in AGENTS, n in ACTUAL_TASKS]);
int: max_travel_time = max([ left_arm_travel_times[i,j] | i,j in LOCATIONS] ++ [ right_arm_travel_times[i,j] | i,j in LOCATIONS] );
int: time_budget_left = sum (i in ACTUAL_TASKS) (
if -1 < max([left_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[1,i] + max([ left_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: time_budget_right = sum (i in ACTUAL_TASKS) (
if -1 < max([right_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[2,i] + max([ right_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: time_budget = max([time_budget_left,time_budget_right]);
% Calculate the absolute minimum time an arm needs to work + travel
int: min_time_budget_left = sum (i in ACTUAL_TASKS) (
if -1 < min([left_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[1,i] + min([ left_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: min_time_budget_right = sum (i in ACTUAL_TASKS) (
if -1 < min([right_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[2,i] + min([ right_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: min_time_budget = min([min_time_budget_left,min_time_budget_right]);
% Calculate the time if one arm recieves all tasks it can perform, and manages to get minimal travel time
int: minimax_time_budget_left = sum (i in ACTUAL_TASKS) (
if -1 < max([left_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[1,i] + min([ left_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: minimax_time_budget_right = sum (i in ACTUAL_TASKS) (
if -1 < max([right_arm_travel_times[l,l] | l in location_domain(i)]) then
task_durations[2,i] + min([ right_arm_travel_times[l1,l2] | l1 in location_domain(i), l2 in LOCATIONS ])
else
0 % if we can't do the task, we shouldn't count it
endif
);
int: minimax_time_budget = min([minimax_time_budget_left,minimax_time_budget_right]);
int: min_traveltime = 1;
int: min_waittime = 1;
set of int: TOTTIME = 0..time_budget;
set of int: WAITTIME = min_waittime..time_budget;
set of int: DURTIME = min_duration..max_duration; %smaller domain, for task durations. needs to include 0, due to dummy tasks
set of int: TRAVELTIME = min_traveltime..max_travel_time; % smaller domain, for travel times, needs to include 0, due to dummy tasks
% The last tasks represent the start and end task for each vehicle (dummies, not depot)
set of int: AGENTS = 1..no_agents;
set of int: TASKS = 1..no_tot_tasks;
set of int: ACTUAL_TASKS = 1..no_actual_tasks;
set of int: LOCATIONS = 1..no_locations;
% Depot tasks are not actual tasks, but rather a modelling convenience, that separates our
% successors into 2 sequences
set of int: DEPOT_TASKS = no_actual_tasks+1..no_tot_tasks;
set of int: START_DEPOT_TASKS = no_actual_tasks+1..no_actual_tasks+no_agents;
set of int: END_DEPOT_TASKS = no_actual_tasks+no_agents+1..no_tot_tasks;
array[int] of int: DEPOT_TASK_LIST = [no_actual_tasks + a | a in 1..2*no_agents];
array[int] of int: START_DEPOT_TASK_LIST = [no_actual_tasks + a | a in AGENTS];
array[int] of int: END_DEPOT_TASK_LIST = [no_actual_tasks + no_agents + a | a in AGENTS];
%To be defined in datafile:
% TODO: generalize
set of int: TRAY_TASKS;
set of int: CAMERA_TASKS;
set of int: FIXTURE_TASKS = all_tasks(fixture_task_orders);
set of int: GRIPPER_TASKS = all_tasks(gripper_pick_tasks_orders);
set of int: SUCTION_TASKS = all_tasks(suction_pick_tasks_orders);
set of int: OUTPUT_TASKS;
set of int: empty_gripper_tasks; % these are tasks for which the application requires empty gripper (e.g. "peeling of tape")
% We assume these sets have no overlap, and form the full set of locations
set of int: TRAY_LOCATIONS;
set of int: CAMERA_LOCATIONS;
set of int: FIXTURE_LOCATIONS;
set of int: AIRGUN_LOCATIONS;
set of int: OUTPUT_LOCATIONS;
% Data
array[AGENTS, int] of int: task_durations;
array[int,int] of int: left_arm_travel_times;
array[int,int] of int: right_arm_travel_times;
array[int] of int: location_order;
% =================================================%
% Variables
% =================================================%
% These variables each belongs to a task.
% =================================================%
%
% Since we model multiple (2) agents using 1 hamiltonian task circuit
% we add dummy tasks as "breaks" between each agent's task sequence.
% The allover sequence starts with the start task of agent 1, the tasks of agent 1,
% and finishes with the end task of agent1. It then continues with the start task
% of agent 2, the tasks of agent 2, and finishes with the end task of agent2.
% The end task of agent 2 then finalizes the circuit by "pointing to" the
% start task of agent 1.
%
array[TASKS] of var AGENTS: agent; % Which agent performs the task
% Route encodings - each of these encode the same information, but in different ways
array[TASKS] of var TASKS: successor; % Task sequence encoding where successor[i] = j means task j follows task i
array[TASKS] of var TASKS: task; % Task sequence encoding where task[i] = j means task j is i:th to be performed
% Time variables:
array[TASKS] of var TOTTIME: arrival_time; % when agent arrives at task
array[TASKS] of var TOTTIME: start_time; % when agent start working on task
array[TASKS] of var TOTTIME: end_time; % when agent finish working on task
array[TASKS] of var TOTTIME: next_arrival_time; % when agent arrives at next task
array[TASKS] of var WAITTIME: waiting_time; % Time between arrival_time and start_time (needed(?) to ensure collision detection at all times)
array[TASKS] of var DURTIME: duration; % duration of task
array[TASKS] of var TRAVELTIME: travel_time; % travel time of going from task i to successor[i]
% Locations - locations are variable
array[TASKS] of var LOCATIONS: location;
array[TASKS] of var LOCATIONS: next_location; % Used to induce traveltime between tasks
% =================================================%
% Variables not attributet to one task
% =================================================%
% META VARIABLES - used for search & increasing propagation
array[1..1,AGENTS] of var TASKS: agent_counts;
var TASKS: agent_count = 2*length(agent) - sum(agent); % # tasks assigned to left arm
constraint agent_counts[1,1] = 2*card(TRAY_TASKS) - sum(t in TRAY_TASKS)(agent[t]);
constraint agent_counts[1,2] = sum(t in TRAY_TASKS)(agent[t]) - card(TRAY_TASKS);
% =================================================%
% Routing Model Constraints:
%
% Each arm task sequence starts with a (dummy) start task, and a (dummy) end task
% Since it is all modelled as one Hamiltonian Circuit, we need to connect arm sequences to each other
% Thus,
% - After end task of arm n comes start task of arm n+1
% - After end task of arm N comes start task of arm 0
% We use 4 equivalent Encodings:
% (A) successor
% (B) task
% =================================================%
% Constraint related to the dummy tasks:
% [..., START_DEPOT_TASK_LIST[1], START_DEPOT_TASK_LIST[2], END_DEPOT_TASK_LIST[1], END_DEPOT_TASK_LIST[2]]
% =================================================%
% -----------------------------------------------------
% Constraints on dummy tasks' sequence encodings
% **********************
%Constraint 1, row 3:
% **********************
% Encoding A:
constraint successor[END_DEPOT_TASK_LIST[2]] = START_DEPOT_TASK_LIST[1] ;
constraint successor[END_DEPOT_TASK_LIST[1]] = START_DEPOT_TASK_LIST[2] ;
% Encoding B: (only states start & end of full sequence)START_DEPOT_TASK_LIST[1]] = END_DEPOT_TASK_LIST[2];
constraint task[no_tot_tasks] = END_DEPOT_TASK_LIST[2];
constraint task[1] = START_DEPOT_TASK_LIST[1]; % The statements A) There is "c" ones in "agent" and B) agent one's last task is on c:th place in the sequence
constraint task[agent_count] = END_DEPOT_TASK_LIST[1];
constraint task[agent_count+1] = START_DEPOT_TASK_LIST[2];
% Fixing dummy tasks to correct agent:
constraint forall(a in AGENTS)(
agent[START_DEPOT_TASK_LIST[a]] = a
/\
agent[END_DEPOT_TASK_LIST[a]] = a
);
% Making sure we get the full cost of the schedule:
% Make an artificial "depot"
% Return the arm to 1:st position at the end of a schedule
% The last task in the sequence should reflect that it returns to the first task of the cycle
% Thus, we make sure that the end task get the same location as the successor of the start task of the arm
%-------------------------------------------------------
% Constraint 1, row 5-6:
constraint forall(a in AGENTS) (
location[END_DEPOT_TASK_LIST[a]] == location[START_DEPOT_TASK_LIST[a]]
/\
location[END_DEPOT_TASK_LIST[a]] = next_location[END_DEPOT_TASK_LIST[a]] % Missing from Constraint 1, row 5-6 (it should read location_succ_T-2 = location_T-2 and location_succ_T-1 = location_T-1)
/\
location[START_DEPOT_TASK_LIST[a]] = next_location[START_DEPOT_TASK_LIST[a]]
/\
next_location[START_DEPOT_TASK_LIST[a]] == location[successor[START_DEPOT_TASK_LIST[a]]]
);
%-------------------------------------------------------
% ----- Dummy Tasks Time Constraints ------------
%-------------------------------------------------------
% Overlapping cycles: ------------------------------------------
constraint forall(t in START_DEPOT_TASKS) (
next_arrival_time[t] = arrival_time[successor[t]]
);
% At least one agent starts at time 0
constraint min(t in START_DEPOT_TASKS)(next_arrival_time[t]) = 0;
constraint max(t in START_DEPOT_TASKS)(next_arrival_time[t]) = cycle_overlap;
constraint forall(t in DEPOT_TASKS) (
travel_time[t] == min_traveltime
/\
duration[t] == min_duration
/\
waiting_time[t] = min_waittime
);
constraint forall(t in DEPOT_TASKS) (
start_time[t] == arrival_time[t]
/\
start_time[t] == end_time[t]
/\
start_time[t] == next_arrival_time[t]
);
%===========================================
% END DUMMY TASK CONSTRAINTS
%===========================================
%===========================================
% Route Model Constraints
%===========================================
% ---------------------------------------------------------------------
% Hamiltonian Circuit
%--------------------------------
constraint circuit(successor) :: domain;
% ----------------------------------------------------------------------
% Connect Successor Encoding (A) with "task" Encoding (B)
% ----------------------------------------------------------------------
constraint forall(t in 1..(no_tot_tasks-1)) (task[t+1] = successor[task[t]]);
% Redundant
constraint alldifferent(task) :: domain;
%-----------------------------------------------------------------------------------
% Connecting Successor Encoding (A) with Route Assignment:
% Posting that successors & predeccessors has same agent (agent)
%------------------------------------------------------------------------------------
constraint
forall(t in ACTUAL_TASKS) (agent[t] == agent[successor[t]]);
constraint forall(t in START_DEPOT_TASKS) (agent[t] == agent[successor[t]]);
constraint forall(t in END_DEPOT_TASKS) (agent[t] != agent[successor[t]]);
% ==============================================
% Locations Constraints:
% ==============================================
int: no_fixture_locations = card(FIXTURE_LOCATIONS);
% Only allowing locations of correct type
predicate remove_invalids(set of int: tasks, set of int: locs) =
forall(t in tasks) (location[t] in locs) :: domain_change_constraint;
% Pre Constraint 7
constraint remove_invalids(TRAY_TASKS, TRAY_LOCATIONS);
constraint remove_invalids(CAMERA_TASKS, CAMERA_LOCATIONS);
constraint remove_invalids(FIXTURE_TASKS, FIXTURE_LOCATIONS);
constraint remove_invalids(OUTPUT_TASKS, OUTPUT_LOCATIONS);
constraint forall(t in ACTUAL_TASKS) (next_location[t] = location[successor[t]]);
% All tasks of a fixture, must be done at the same fixture
constraint forall(f in 1..no_fixtures)(
all_equal([location[fixture_task_orders[f,t]] | t in 1..fixture_order_lengths where fixture_task_orders[f,t] >= 0 ] )
);
% All tasks of different fixtures, must be done at different locations
constraint alldifferent([location[fixture_task_orders[f,1]] | f in 1..no_fixtures]);
% ----------------------------------------------------
% Some locations are only reachable by some agents
% Using that diagonal elements of travelling matrix is >=0 if feasible, -1 otherwise (currently only0, but might be >0 in the future)
% REDUNDANT wrt constraint below, since it puts exactly the same restrictions on all tasks' agent-location tuples (however, including this constraint seems to make things ever so slightly faster..)
constraint forall(t in TASKS)(
let { array[int] of int: raw_extension =
[ if x = 1 then 1 else l endif | l in location_domain(t) where left_arm_travel_times[l,l] >= 0, x in 1..2]
++
[ if x = 1 then 2 else l endif | l in location_domain(t) where right_arm_travel_times[l,l] >= 0, x in 1..2]
} in table( [agent[t]] ++ [location[t]], array2d(1..(length(raw_extension) div 2), 1..2, raw_extension) )
);
%----------------------------------------------------
%------------------------------------------------------
% Travel time depends on arm, location, next_location,
% Create table for a 4-tuple: [agent[t]] ++ [location[t]] ++ [next_location[t]] ++ [travel_time[t]]
constraint forall(t in ACTUAL_TASKS)(
let { array[int] of int: raw_tt_extension = [
if x = 1 then
1
elseif x = 2 then
l1
elseif x = 3 then
l2
else
if l1 = l2 then
min_traveltime
else
left_arm_travel_times[l1,l2]
endif
endif
| l1 in location_domain(t) where left_arm_travel_times[l1,l1] >= 0, l2 in LOCATIONS where left_arm_travel_times[l2,l2] >= 0, x in 1..4]
++ [
if x = 1 then
2
elseif x = 2 then
l1
elseif x = 3 then
l2
else
if l1 = l2 then
min_traveltime
else
right_arm_travel_times[l1,l2]
endif
endif
| l1 in location_domain(t) where right_arm_travel_times[l1,l1] >= 0, l2 in LOCATIONS where right_arm_travel_times[l2,l2] >= 0, x in 1..4]
} in table( [agent[t]] ++ [location[t]] ++ [next_location[t]] ++ [travel_time[t]], array2d(1..(length(raw_tt_extension) div 4), 1..4, raw_tt_extension)) :: defines_var(travel_time[t])
);
% ======================================
% ----- Core Time Constraints ------------ %
% ======================================
constraint forall(t in ACTUAL_TASKS) (
start_time[t] + duration[t] == end_time[t]
);
constraint forall(t in ACTUAL_TASKS) (
end_time[t] + travel_time[t] == next_arrival_time[t]
);
constraint forall(t in ACTUAL_TASKS) (
next_arrival_time[t] = arrival_time[successor[t]]
);
constraint forall(t in ACTUAL_TASKS)(
arrival_time[t] + waiting_time[t] = start_time[t]
);
% Table: arm dependent task duration
constraint forall(t in ACTUAL_TASKS) (
table( [agent[t]] ++ [duration[t]], array2d(1..2, 1..2,
[ if x = 1 then r else task_durations[r,t] endif | r in 1..2 , x in 1..2])) );
%-----------------------------------------------------
% Constrain first task of fixture to start immiediately - could possibly hinder good _cyclic_ schedules
constraint forall(r in 1..no_fixtures)(
start_time[fixture_task_orders[r,1]] = arrival_time[fixture_task_orders[r,1]] + min_waittime
);
% Constrain tasks to start directly, or as soon as previous task finishes
constraint forall(r in 1..no_fixtures, n in 1..(fixture_order_lengths - 1) where fixture_task_orders[r,n+1] >= 0 )(
start_time[fixture_task_orders[r,n+1]] = max(arrival_time[fixture_task_orders[r,n+1]] + min_waittime,
end_time[fixture_task_orders[r,n]])
);
% Constrain non-fixture tasks to work when arriving at location
% this is valid since non-fixture tasks occupy the same space while waiting as it does while working
constraint forall(t in ACTUAL_TASKS where not(t in FIXTURE_TASKS) ) (
arrival_time[t] + min_waittime = start_time[t]
/\
waiting_time[t] = min_waittime
);
% Ordering imposed by suction pick-n-place
% Since we know these tasks are performed by same arm, we can use separat all times of a task
constraint forall(order_list_id in index_set_1of2(suction_pick_tasks_orders), list_pos in 1..(suction_order_lengths - 1) where suction_pick_tasks_orders[order_list_id,list_pos+1] >= 0 )(
next_arrival_time[suction_pick_tasks_orders[order_list_id,list_pos]] <= arrival_time[suction_pick_tasks_orders[order_list_id,list_pos+1]]
);
% Ordering imposed by gripper pick-n-place
% Since we know these tasks are performed by same arm, we can use separat all times of a task
constraint forall(order_list_id in index_set_1of2(gripper_pick_tasks_orders), list_pos in 1..(gripper_order_lengths - 1) where gripper_pick_tasks_orders[order_list_id,list_pos+1] >= 0 )(
next_arrival_time[gripper_pick_tasks_orders[order_list_id,list_pos]] <= arrival_time[gripper_pick_tasks_orders[order_list_id,list_pos+1]]
);
% -------------------------------------------------------------
% DISJUNCTIVE over arms
% -------------------------------------------------------------
% This is equal to posting one unary per arm
% Stricter disjunction (including duration and travel time) , since an arm task uses arm for both processing and transportation
constraint implied_constraint(
if not implied_diffn then
true
else
diffn(
[arrival_time[t] | t in TASKS],
[ agent[t] | t in TASKS],
[ duration[t] + travel_time[t] + waiting_time[t] | t in TASKS],
[1 | t in TASKS]
)
endif
);
% All tasks
constraint implied_constraint(
if not implied_cumulative then
true
else
cumulative( [arrival_time[t] | t in ACTUAL_TASKS]
, [duration[t] + travel_time[t] + waiting_time[t] | t in ACTUAL_TASKS]
, [1 | t in ACTUAL_TASKS]
, 2
)
endif
);
% Tasks of each arm
constraint implied_constraint(
if not implied_cumulative then
true
else
forall(a in AGENTS)(
cumulative( [arrival_time[t] | t in ACTUAL_TASKS]
, [duration[t] + travel_time[t] + waiting_time[t] | t in ACTUAL_TASKS]
, [agent[t] = a | t in ACTUAL_TASKS]
, 1
)
)
endif
);
% =======================================================
% Suction Tool Task Arm Orderings
% =======================================================
int: no_suction_cups;
array[int,int] of int: suction_pick_tasks_orders;
int: no_suction_picks = max(index_set_1of2(suction_pick_tasks_orders));
int: suction_order_lengths = max(index_set_2of2(suction_pick_tasks_orders));
% -------------------------------------------------------
% Ordering constraints on routing model
% -------------------------------------------------------
% Each pick-n-place belongs to same arm
constraint forall(s in 1..no_suction_picks)(
all_equal([agent[suction_pick_tasks_orders[s,n]] | n in 1..(sequence_length(suction_pick_tasks_orders, s)) ] )
);
% We know the ordering value will be increasing
constraint forall(order_list_id in index_set_1of2(suction_pick_tasks_orders)) (
increasing([ arrival_time[suction_pick_tasks_orders[order_list_id,list_pos]] | list_pos in index_set_2of2(suction_pick_tasks_orders) where suction_pick_tasks_orders[order_list_id,list_pos] >= 0 ] )
);
% RegEx stating that tasks are done in certain order, on same arm
% It _does not_ make sure that the number of components in suction tool does not exceed capacity - that is taken care of in later regex constraint
constraint %implied_constraint(
if not implied_regular then
true
elseif card(index_set_1of2(suction_pick_tasks_orders)) < 1 then % If we do not have suction pick task we don't need to worry about filling suction cups
true
else
forall(order_list_id in 1..no_suction_picks)(
let {
% Tasks that do not affect the component load and cannot be done without holding the tool
string: keep_no_component = "[" ++ set2regex_str(TASKS diff all_tasks_of_sequence(suction_pick_tasks_orders, order_list_id)) ++ "]";
% Tasks that do not affect the component load - except intermediate tasks that are treated separately
string: keep_component_load = keep_no_component ;%"[" ++ set2regex_str(TASKS diff first_task_of_sequence(suction_pick_tasks_orders, order_list_id) diff last_task_of_sequence(suction_pick_tasks_orders, order_list_id) diff DEPOT_TASKS) ++ "]";
string: adding_component_load = "[" ++ set2regex_str( first_task_of_sequence(suction_pick_tasks_orders, order_list_id) ) ++ "]";
string: performing_component_intermediate = "[" ++ set2regex_str( intermediate_tasks_of_sequence(suction_pick_tasks_orders, order_list_id) ) ++ "]";
int: seq_len = sequence_length(suction_pick_tasks_orders, order_list_id) ;
%Create a string of intermediate values - interleaved with the string in "keep_component_load"
% This forces that the tasks in the pick-and-place list are performed in the prescribed order, possibly with tasks in between
array[int] of string: performing_component_intermediate_plus_keep_component_load_star = ["[" ++ set2regex_str( { suction_pick_tasks_orders[order_list_id, seq_ind ] } ) ++ "]" ++ keep_component_load ++ "*" | seq_ind in 2..(seq_len-1)];
string: removing_component_load = "[" ++ set2regex_str( last_task_of_sequence(suction_pick_tasks_orders, order_list_id) ) ++ "]";
string: strSuctionHold =
"(" ++
keep_no_component ++ "*" ++
"(" ++
adding_component_load ++
keep_component_load ++ "*" ++
%performing_component_intermediate ++ %merely forces one intermediate tasks to be done, not all...
str_a2str(performing_component_intermediate_plus_keep_component_load_star,1) ++
%keep_component_load ++ "*" ++
removing_component_load ++
")?" ++
")*";
string: strSuctionHold2 =
"[" ++ set2regex_str({ START_DEPOT_TASK_LIST[1] }) ++ "]" ++
"(" ++
keep_no_component ++ "*" ++
"(" ++
adding_component_load ++
keep_component_load ++ "*" ++
%performing_component_intermediate ++ %merely forces one intermediate tasks to be done, not all...
str_a2str(performing_component_intermediate_plus_keep_component_load_star,1) ++
%keep_component_load ++ "*" ++
removing_component_load ++
")?" ++
")*" ++
"[" ++ set2regex_str({ END_DEPOT_TASK_LIST[1] }) ++ "]" ++
"[" ++ set2regex_str({ START_DEPOT_TASK_LIST[2] }) ++ "]" ++
"(" ++
keep_no_component ++ "*" ++
"(" ++
adding_component_load ++
keep_component_load ++ "*" ++
%performing_component_intermediate ++ %merely forces one intermediate tasks to be done, not all...
str_a2str(performing_component_intermediate_plus_keep_component_load_star,1) ++
%keep_component_load ++ "*" ++
removing_component_load ++
")?" ++
")*" ++
"[" ++ set2regex_str( { END_DEPOT_TASK_LIST[2] } ) ++ "]"
;
} in regular(task, strSuctionHold)
)
endif
%)
;
% Regex - suction cup holding/not holding _any_ component
% Regex - stating suction cups becomes filled when picking up stuff, and emptied when placing them
constraint %implied_constraint(
if not implied_regular then
true
else
let {
string: start_tasks = "[" ++ set2regex_str(START_DEPOT_TASKS) ++ "]";
string: end_tasks = "[" ++ set2regex_str(END_DEPOT_TASKS) ++ "]";
string: keep_tool_empty = "[" ++ set2regex_str(TASKS diff all_tasks(suction_pick_tasks_orders)) ++ "]";
string: keep_tool_load = "[" ++ set2regex_str(TASKS diff all_first_tasks(suction_pick_tasks_orders) diff all_last_tasks(suction_pick_tasks_orders) diff DEPOT_TASKS) ++ "]";
string: adding_tool_load = "[" ++ set2regex_str(all_first_tasks(suction_pick_tasks_orders)) ++ "]";
string: removing_tool_load = "[" ++ set2regex_str(all_last_tasks(suction_pick_tasks_orders)) ++ "]";
bool: uses_tool = adding_tool_load != "[]" /\ removing_tool_load != "[]"; %check that the have equal no of elements
string: strSuctionHold =
"(" ++
start_tasks ++
"(" ++
keep_tool_empty ++ "*" ++
if uses_tool then
"(" ++
adding_tool_load ++
keep_tool_load ++ "*" ++
if no_suction_cups > 1 then
"(" ++
adding_tool_load ++
keep_tool_load ++ "*" ++
removing_tool_load ++
keep_tool_load ++ "*" ++
")*"
else
""
endif ++
removing_tool_load ++
")*"
else
""
endif ++
")*" ++
end_tasks ++
"){" ++ show(no_agents) ++ "}"
;
} in regular(task, strSuctionHold)
endif
%)
;
% ========================================================
% Gripper Tool Tasks Arm Orderings
% ========================================================
array[int,int] of int: gripper_pick_tasks_orders;
int: no_gripper_picks = card(index_set_1of2(gripper_pick_tasks_orders));
int: gripper_order_lengths = max(index_set_2of2(gripper_pick_tasks_orders));
% -------------------------------------------------------
% -------------------------------------------------------
% All pick-n-place belongs to same arm
constraint forall(order_list_id in 1..no_gripper_picks)(
all_equal([agent[gripper_pick_tasks_orders[order_list_id,list_pos]] | list_pos in 1..(sequence_length(gripper_pick_tasks_orders, order_list_id)) ] )
);
% Redundant wrt the pick-n-place
constraint forall(order_list_id in 1..no_gripper_picks) (
increasing([ arrival_time[gripper_pick_tasks_orders[order_list_id,list_pos]] | list_pos in 1..(sequence_length(gripper_pick_tasks_orders, order_list_id)) ] )
);
% -------------------------------------------------------
% Regex on routing model
% -------------------------------------------------------
% All gripper pnp regex:s
constraint %implied_constraint(
if not implied_regular then
true
elseif card(index_set_1of2(gripper_pick_tasks_orders)) < 1 then
true
else
forall(order_list_id in 1..no_gripper_picks)(
let {
string: keep_tool_empty = "[" ++ set2regex_str(TASKS diff all_tasks_of_sequence(gripper_pick_tasks_orders, order_list_id)) ++ "]";
% Tasks that does not affect the tool load, except from intermediate tasks that _must_ happen before we perform place-task
string: keep_tool_load = "[" ++ set2regex_str(TASKS diff all_tasks_of_sequence(gripper_pick_tasks_orders, order_list_id) diff empty_gripper_tasks diff DEPOT_TASKS) ++ "]";
string: adding_tool_load = "[" ++ set2regex_str( first_task_of_sequence(gripper_pick_tasks_orders, order_list_id) ) ++ "]";
string: performing_tool_intermediate = "[" ++ set2regex_str( intermediate_tasks_of_sequence(gripper_pick_tasks_orders, order_list_id) ) ++ "]";
int: seq_len = sequence_length(gripper_pick_tasks_orders, order_list_id) ;
%Create a string of intermediate values - interleaved with the string in "keep_tool_load"
% This forces that the tasks in the pick-and-place list are performed in the prescribed order, possibly with tasks in between
array[int] of string: performing_tool_intermediate_plus_keep_tool_load_star = ["[" ++ set2regex_str( { gripper_pick_tasks_orders[order_list_id, seq_ind ] } ) ++ "]" ++ keep_tool_load ++ "*" | seq_ind in 2..(seq_len-1)];
string: removing_tool_load = "[" ++ set2regex_str( last_task_of_sequence(gripper_pick_tasks_orders, order_list_id) ) ++ "]";
bool: uses_tool = adding_tool_load != "[]" /\ removing_tool_load != "[]"; %check that the have equal no of elements
string: strToolPnP =
"(" ++
keep_tool_empty ++ "*" ++
"(" ++
adding_tool_load ++
keep_tool_load ++ "*" ++
% Below gives all intermediate tasks, interleaved with other tasks that keeps the tool load constant
str_a2str(performing_tool_intermediate_plus_keep_tool_load_star,1) ++
%keep_tool_load ++ "*" ++
removing_tool_load ++
")?"
++
keep_tool_empty ++ "*" ++
")";
} in regular(task, strToolPnP)
)
endif
%)
;
% Ensuring gripper capacity (1) is not exceeded
constraint %implied_constraint(
if not implied_regular then
true
else
let {
string: start_tasks = "[" ++ set2regex_str(START_DEPOT_TASKS) ++ "]";
string: end_tasks = "[" ++ set2regex_str(END_DEPOT_TASKS) ++ "]";
string: inEmptyGripperState = "[" ++ set2regex_str(TASKS diff all_tasks(gripper_pick_tasks_orders) diff DEPOT_TASKS ) ++ "]";
string: inHoldingWithGripperState = "[" ++ set2regex_str(TASKS diff all_first_tasks(gripper_pick_tasks_orders) diff all_last_tasks(gripper_pick_tasks_orders) diff DEPOT_TASKS diff empty_gripper_tasks) ++ "]";
string: fromEmptyToHoldingState = "[" ++ set2regex_str(all_first_tasks(gripper_pick_tasks_orders)) ++ "]";
string: fromHoldingToEmptyState = "[" ++ set2regex_str(all_last_tasks(gripper_pick_tasks_orders)) ++ "]";
string: strGripHold =
"(" ++
start_tasks ++
"(" ++
inEmptyGripperState ++ "*" ++
"(" ++
fromEmptyToHoldingState ++
if inHoldingWithGripperState != "[]" then inHoldingWithGripperState ++ "*" else "" endif ++
fromHoldingToEmptyState ++
")?" ++
")*" ++
end_tasks ++
"){"++show(no_agents)++"}"
;
string: strGripHold2 =
"[" ++ set2regex_str({ START_DEPOT_TASK_LIST[1] }) ++ "]" ++
"(" ++
inEmptyGripperState ++ "*" ++
"(" ++
fromEmptyToHoldingState ++
if inHoldingWithGripperState != "[]" then inHoldingWithGripperState ++ "*" else "" endif ++
fromHoldingToEmptyState ++
")?" ++
")*" ++
"[" ++ set2regex_str({ END_DEPOT_TASK_LIST[1] }) ++ "]" ++
"[" ++ set2regex_str({ START_DEPOT_TASK_LIST[2] }) ++ "]" ++
"(" ++
inEmptyGripperState ++ "*" ++
"(" ++
fromEmptyToHoldingState ++
if inHoldingWithGripperState != "[]" then inHoldingWithGripperState ++ "*" else "" endif ++
fromHoldingToEmptyState ++
")?" ++
")*" ++
"[" ++ set2regex_str({ END_DEPOT_TASK_LIST[2] }) ++ "]"
;
} in regular(task, strGripHold)
endif
%)
;
% redundant wrt above, however as one unified constraint as well as including holding constraint. It seems to be much more efficient
% (ensuring only 1 component at a time)
constraint %implied_constraint(
if not implied_regular then
true
elseif card(index_set_1of2(gripper_pick_tasks_orders)) < 1 then
true
else
let {
string: keep_tool_empty = "[" ++ set2regex_str(TASKS diff all_tasks(gripper_pick_tasks_orders)) ++ "]" ;
string: keep_tool_load = "[" ++ set2regex_str(TASKS diff all_tasks(gripper_pick_tasks_orders) diff empty_gripper_tasks diff DEPOT_TASKS) ++ "]" ;
string: keep_tool_load_star_safe = if keep_tool_load != "[]" then keep_tool_load ++ "*" else "" endif ;
array[int] of string: adding_tool_load = ["[" ++ set2regex_str( first_task_of_sequence(gripper_pick_tasks_orders, order_list_id) ) ++ "]" | order_list_id in 1..no_gripper_picks];
array[int] of string: removing_tool_load = ["[" ++ set2regex_str( last_task_of_sequence(gripper_pick_tasks_orders, order_list_id) ) ++ "]"| order_list_id in 1..no_gripper_picks];
int: max_seq_len = max_sequence_length(gripper_pick_tasks_orders, 1) ;
array[int] of string: performing_tool_intermediate = ["[" ++ if sequence_length(gripper_pick_tasks_orders, order_list_id) - 1 < seq_ind then "" else set2regex_str( { gripper_pick_tasks_orders[order_list_id, seq_ind ] } ) endif ++ "]" | order_list_id in 1..no_gripper_picks, seq_ind in 2..(max_seq_len-1)];
array[int,int] of string: performing_tool_intermediateS_safe = if max_seq_len > 2 then array2d(1..(length(performing_tool_intermediate) div (max_seq_len-2)), 1..(max_seq_len-2), performing_tool_intermediate) else [|""|] endif ;
array[int] of string: strToolPnP = [
"(" ++
keep_tool_empty ++ "*" ++
"(" ++
adding_tool_load[order_list_id] ++
%performing_tool_intermediate[order_list_id] ++ %does not handle multiple intermediate tasks .. hence see below
keep_tool_load_star_safe ++
str_a_slice2str(performing_tool_intermediateS_safe, keep_tool_load_star_safe, order_list_id, 1, sequence_length(gripper_pick_tasks_orders, order_list_id) - 2) ++
removing_tool_load[order_list_id] ++
")?" ++
")*"
| order_list_id in 1..no_gripper_picks];
} in regular(task, "(" ++ str_a2str(strToolPnP,1) ++ ")*" )
endif
%)
;
% =====================================================================
% Fixture orderings - regex for agent model + interval constraints
% =====================================================================
array[int,int] of int: fixture_task_orders;
int: no_fixtures = max(index_set_1of2(fixture_task_orders));
int: fixture_order_lengths = max(index_set_2of2(fixture_task_orders));
% -------------------------------------------------------
% ------------------------------------------------------
% Constraint 4, row 1
% Time constraints are posted with other time constraints (above)
% -------------------------------------------------------
% Regex on routing model
% -------------------------------------------------------
% Constraint 4, row 1
%Regex - fixture order
% Given that an agent performs tasks on a fixture, those tasks are performed in the prescibed order
constraint %implied_constraint(
if not implied_regular then
true
else
forall(order_list_id in 1..no_fixtures)(
let {
string: start_tasks = "[" ++ set2regex_str(START_DEPOT_TASKS) ++ "]";
string: end_tasks = "[" ++ set2regex_str(END_DEPOT_TASKS) ++ "]";
% Tasks that are allowed to be done in between fixture tasks. TODO: This could be made with other constraints in mind - e.g. it is not allowed to pick up an item after droping it on fixture
string: allowed_interspersed = "[" ++ set2regex_str(TASKS diff all_tasks_of_sequence(fixture_task_orders, order_list_id) diff DEPOT_TASKS) ++ "]";
int: seq_len = sequence_length(fixture_task_orders, order_list_id) ;
%Create a string of intermediate values - interleaved with the string in "allowed_interspersed"
% This forces that the tasks in the pick-and-place list are performed in the prescribed order, possibly with tasks in between
array[int] of string: performing_intermediate_interspersed_star = ["[" ++ set2regex_str( { fixture_task_orders[order_list_id, seq_ind ] } ) ++ "]?" ++ allowed_interspersed ++ "*" | seq_ind in 1..seq_len];
string: strFixtureTasks =
"(" ++
start_tasks ++
allowed_interspersed ++ "*" ++
str_a2str(performing_intermediate_interspersed_star,1) ++
end_tasks ++
"){"++show(no_agents)++"}"
;
} in regular(task, strFixtureTasks)
)
endif
%)
;
% -----------------------------------------------------------
% END FIXTURE ORDERS
% -----------------------------------------------------------
% =============================================================
% Constraint Specific to the reality of assembly layout
% =============================================================
% All component trays need to be at different locations
constraint alldifferent([location[t] | t in TRAY_TASKS] );
% =============================================================
% END Assembly Layout Constraint
% =============================================================
% =============================================================
% Collision Coordination
% =============================================================
% Constraint 8, and equivalents - in separate file(s)
array[1..2] of int: FixtureWorkObstruction;
% Making sure fixture is emptied before next assembly:
constraint forall(order_list_id in 1..no_fixtures) (
end_time[fixture_task_orders[order_list_id, sequence_length(fixture_task_orders, order_list_id)]] <= start_time[fixture_task_orders[order_list_id,1]] + period
);
% Making sure no overlap of assembly tasks within an agent (might remove some solutions, but a reasonable simplification as to facilitate pick-up-and-delivery constraints hold):
constraint forall(a in AGENTS)(
start_time[START_DEPOT_TASK_LIST[a]] + period = start_time[END_DEPOT_TASK_LIST[a]]
);
% =============================================================
% END Collision Coordination
% =============================================================
% =============================================================
% START Symmetry Breaking
% =============================================================
% None so far..
% =============================================================
% END Symmetry Breaking
% =============================================================
% =====================================
% Objective - and related constraints
% =====================================
constraint makespan = max( next_arrival_time );
constraint next_arrival_time[END_DEPOT_TASK_LIST[1]] >= (min_time_budget div 2) ;
constraint next_arrival_time[END_DEPOT_TASK_LIST[2]] >= (min_time_budget div 2) ;
% cycle overlap is the time between the finish time of one arm and the finish time of the other arm
% (it is also the time between the start time of one arm and the start time of the other arm)
var TOTTIME: cycle_overlap ;
constraint cycle_overlap = max([start_time[START_DEPOT_TASK_LIST[1]], start_time[START_DEPOT_TASK_LIST[2]] ]);
set of int: FINISH_TIMES = (minimax_time_budget div 2)..time_budget;
var FINISH_TIMES: period ;
var FINISH_TIMES: makespan;
constraint makespan = period + cycle_overlap ;
%constraint makespan <= sum(duration ++ travel_time);
% This follows from the definition of makespan, cycle_overlap and period
% Very useful, since this is a cost function without negative weights
constraint period = min([ next_arrival_time[END_DEPOT_TASK_LIST[1]] ,
next_arrival_time[END_DEPOT_TASK_LIST[2]]
]) ;
% ============================================================================
% ============================================================================
% Enforce sequencing of fixture tasks
constraint
if not implied_value_precede then
true
else
forall( r in 1..no_fixtures,
i in 1..fixture_order_lengths,
j in 1..fixture_order_lengths
where
j > i /\ fixture_task_orders[r,j] >= 1
)(
let {TASKS: ti = fixture_task_orders[r,i],
TASKS: tj = fixture_task_orders[r,j],
var int: enable = (agent[ti] != agent[tj]) * ti,
} in value_precede(ti, tj, [enable] ++ task)
)