-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimisedVersion.py
More file actions
1591 lines (1514 loc) · 63.7 KB
/
Copy pathoptimisedVersion.py
File metadata and controls
1591 lines (1514 loc) · 63.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
from PlotGant import plot_schedule
class RCPSP:
def __init__(self, num_tasks, durations, resource_requirements, resource_availabilities):
self.num_tasks = num_tasks
self.durations = durations
self.resource_requirements = resource_requirements
self.resource_availabilities = resource_availabilities
self.tasks = {}
def get_successors(self, task_id):
"""Returns a list of successor task IDs for the given task."""
successors = []
for t_id, task in self.tasks.items():
if task_id in task['predecessors']:
successors.append(t_id)
return successors
def detect_circular_dependencies(self):
"""Detects circular dependencies in the task graph using DFS."""
def dfs(task_id, visited, stack):
visited.add(task_id)
stack.add(task_id)
for pred in self.tasks[task_id]['predecessors']:
if pred not in visited:
if dfs(pred, visited, stack):
return True
elif pred in stack:
return True
stack.remove(task_id)
return False
visited = set()
stack = set()
for task_id in self.tasks:
if task_id not in visited:
if dfs(task_id, visited, stack):
return True
return False
def topological_sort(self):
"""Generates a topological sort of tasks using Kahn's algorithm."""
in_degree = {task_id: 0 for task_id in self.tasks}
for task_id, task in self.tasks.items():
for pred in task['predecessors']:
in_degree[task_id] += 1
queue = [task_id for task_id, degree in in_degree.items() if degree == 0]
topo_order = []
while queue:
current = queue.pop(0)
topo_order.append(current)
# For each task, check for tasks that depend on it (i.e., its successors)
for successor_id, successor_task in self.tasks.items():
if current in successor_task['predecessors']:
in_degree[successor_id] -= 1
if in_degree[successor_id] == 0:
queue.append(successor_id)
if len(topo_order) != len(self.tasks):
raise ValueError("Graph contains a cycle; topological sort not possible.")
return topo_order
def serial_schedule_generation_scheme(self, task_order=None):
import heapq
schedule = {}
resource_usage = [0] * (max(self.durations) * self.num_tasks) # Tracks resource usage over time
if task_order is None:
task_order = list(self.tasks.keys())
task_start_times = {}
ready_queue = [] # Tasks ready to be scheduled
# Determine the number of unsatisfied predecessors for each task
unsatisfied_predecessors = {
task_id: len(self.tasks[task_id]['predecessors']) for task_id in self.tasks
}
# Initialize the ready queue with tasks that have no predecessors
for task_id, count in unsatisfied_predecessors.items():
if count == 0:
heapq.heappush(ready_queue, task_id)
current_time = 0
while ready_queue:
# Process ready tasks
task_id = heapq.heappop(ready_queue)
task = self.tasks[task_id]
# Compute the earliest possible start time based on predecessor completion
start_time = max(
(task_start_times[pred] + self.tasks[pred]['duration'] for pred in task['predecessors']),
default=current_time
)
# Ensure resource constraints are met
while True:
can_start = True
for t in range(start_time, start_time + task['duration']):
if t >= len(resource_usage) or any(
resource_usage[t] + req > avail
for req, avail in zip(task['resources'], self.resource_availabilities)
):
can_start = False
break
if can_start:
break
start_time += 1
# Assign the computed start time
task_start_times[task_id] = start_time
# Update resource usage during the task's execution
for t in range(start_time, start_time + task['duration']):
for r, req in enumerate(task['resources']):
resource_usage[t] += req
schedule[task_id] = start_time
# Update unsatisfied predecessors for dependent tasks
for successor_id in self.get_successors(task_id):
unsatisfied_predecessors[successor_id] -= 1
if unsatisfied_predecessors[successor_id] == 0:
heapq.heappush(ready_queue, successor_id)
# Increment current time
current_time = max(current_time, start_time + task['duration'])
return schedule, task_start_times
def compute_makespan(self, task_start_times):
return max(start + self.tasks[task_id]['duration'] for task_id, start in task_start_times.items())
def validate_task_order(self, task_order):
visited = set()
for task_id in task_order:
for pred in self.tasks[task_id]['predecessors']:
if pred not in visited:
return False
visited.add(task_id)
return True
def optimize_parallelism(schedule, task_start_times, project):
"""
Adjusts the start times of tasks in the schedule to allow better parallelism,
ensuring resource and precedence constraints are respected.
"""
# Convert schedule to a structure that includes task IDs and their start times
sorted_tasks = [(task_id, task_start_times[task_id]) for task_id in schedule]
sorted_tasks.sort(key=lambda x: x[1]) # Sort by start time
new_task_start_times = task_start_times.copy()
for task_id, start_time in sorted_tasks:
earliest_start = 0
# Check predecessor constraints
for pred in project.tasks[task_id]['predecessors']:
pred_end_time = new_task_start_times[pred] + project.tasks[pred]['duration']
earliest_start = max(earliest_start, pred_end_time)
# Check resource constraints
task_duration = project.tasks[task_id]['duration']
task_resources = project.tasks[task_id]['resources']
for t in range(earliest_start, start_time):
if all(
project.resource_availabilities[r] >= sum(
project.tasks[other_task]['resources'][r]
for other_task in schedule
if new_task_start_times[other_task] <= t < new_task_start_times[other_task] + project.tasks[other_task]['duration']
) + task_resources[r]
for r in range(len(task_resources))
):
# Update the task start time if all conditions are met
new_task_start_times[task_id] = t
break
# Reconstruct the new schedule
new_schedule = [task_id for task_id in schedule]
return new_schedule, new_task_start_times
def constrained_shuffle(task_order, project):
valid_order = task_order[:]
for _ in range(len(task_order) * 2): # Attempt constrained swaps
i, j = random.sample(range(len(valid_order)), 2)
if project.validate_task_order(valid_order[:i] + [valid_order[j]] + valid_order[i+1:j] + [valid_order[i]] + valid_order[j+1:]):
valid_order[i], valid_order[j] = valid_order[j], valid_order[i]
return valid_order
def abc(population_size, scouts, max_trial, project, max_iterations=100, max_shuffle_attempts=10):
if project.detect_circular_dependencies():
return None, float('inf'), None
#initialization
food_number = population_size // 2
food_sources = []
base_task_order = project.topological_sort()
for _ in range(food_number):
task_order = base_task_order[:]
shuffle_attempts = 0
while not project.validate_task_order(task_order):
task_order = constrained_shuffle(task_order, project)
shuffle_attempts += 1
if shuffle_attempts >= max_shuffle_attempts:
break
food_sources.append({
"task_order": task_order,
"makespan": None,
"task_start_times": None
})
trials = [0] * food_number
best_schedule = None
best_makespan = float('inf')
def evaluate_schedule(source):
if source["makespan"] is None:
schedule, task_start_times = project.serial_schedule_generation_scheme(source["task_order"])
source["makespan"] = project.compute_makespan(schedule)
source["task_start_times"] = task_start_times
return source["makespan"], source["task_start_times"]
iteration = 0
while iteration < max_iterations:
iteration += 1
# send employed bees
for source in food_sources:
makespan, task_start_times = evaluate_schedule(source)
if makespan < best_makespan:
best_schedule = source["task_order"][:]
best_makespan = makespan
# send onlooker bees
for i in range(food_number):
new_task_order = food_sources[i]["task_order"][:]
a, b = random.sample(range(project.num_tasks), 2)
new_task_order[a], new_task_order[b] = new_task_order[b], new_task_order[a]
if project.validate_task_order(new_task_order):
new_source = {
"task_order": new_task_order,
"makespan": None,
"task_start_times": None
}
new_makespan, _ = evaluate_schedule(new_source)
if new_makespan < food_sources[i]["makespan"]:
food_sources[i] = new_source
trials[i] = 0
if new_makespan < best_makespan:
best_schedule = new_task_order
best_makespan = new_makespan
else:
trials[i] += 1
# send scout bees
scout_indices = sorted(range(food_number), key=lambda x: trials[x], reverse=True)[:scouts]
for idx in scout_indices:
task_order = base_task_order[:]
random.shuffle(task_order)
shuffle_attempts = 0
while not project.validate_task_order(task_order):
random.shuffle(task_order)
shuffle_attempts += 1
if shuffle_attempts >= max_shuffle_attempts:
break
food_sources[idx] = {
"task_order": task_order,
"makespan": None,
"task_start_times": None
}
trials[idx] = 0
if all(trial <= max_trial for trial in trials):
break
# check if further parallelism is possible
# Recalculate makespan after optimizing parallelism todo vrati na staro
best_schedule, task_start_times = optimize_parallelism(best_schedule, task_start_times, project)
best_makespan = project.compute_makespan(task_start_times) # Use task_start_times here
return best_schedule, best_makespan, task_start_times
#simple helper function used for testing, checks if predecessors or resource constraints are validated in the final given solution
def validate_solution(project, best_schedule, task_start_times):
# Check precedence constraints
for task, task_data in project.tasks.items():
for predecessor in task_data['predecessors']:
if task_start_times[task] < task_start_times[predecessor] + project.durations[predecessor]:
print(f"Precedence constraint violated: Task {task} starts before Task {predecessor} is completed.")
return False
# Check resource constraints
num_resources = len(project.resource_availabilities)
max_time = max(task_start_times[task] + project.durations[task] for task in project.tasks)
resource_usage = [[0] * max_time for _ in range(num_resources)]
for task, start_time in task_start_times.items():
for resource_idx, resource_required in enumerate(project.resource_requirements[task]):
for t in range(start_time, start_time + project.durations[task]):
resource_usage[resource_idx][t] += resource_required
if resource_usage[resource_idx][t] > project.resource_availabilities[resource_idx]:
print(f"Resource constraint violated: Resource {resource_idx} exceeds availability at time {t}.")
return False
print("The solution is valid: all precedence and resource constraints are fulfilled.")
return True
if __name__ == "__main__":
# # batch3 - COMPLEX, EDGE CASES ETC with more activities
# print("Test 9\n")
# # Description: A project where one task has zero duration.
# num_tasks = 4
# durations = [3, 0, 5, 2]
# resource_requirements = [[1, 2], [0, 0], [2, 1], [1, 1]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [1], # Task 3 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [1, 2]},
# 1: {'duration': 0, 'predecessors': [0], 'resources': [0, 0]},
# 2: {'duration': 5, 'predecessors': [1], 'resources': [2, 1]},
# 3: {'duration': 2, 'predecessors': [1], 'resources': [1, 1]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 10\n")
# # Description: A project where one task requires zero resources.
# num_tasks = 3
# durations = [4, 3, 5]
# resource_requirements = [[1, 1], [0, 0], [2, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 4, 'predecessors': [], 'resources': [1, 1]},
# 1: {'duration': 3, 'predecessors': [0], 'resources': [0, 0]},
# 2: {'duration': 5, 'predecessors': [1], 'resources': [2, 3]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 11\n")
# # Description: A project where resource availability exactly matches demand.
# num_tasks = 4
# durations = [3, 2, 4, 5]
# resource_requirements = [[1, 2], [2, 3], [3, 2], [4, 1]]
# resource_availabilities = [10, 8] # Total demand is exactly matched.
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [1], # Task 3 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [1, 2]},
# 1: {'duration': 2, 'predecessors': [0], 'resources': [2, 3]},
# 2: {'duration': 4, 'predecessors': [1], 'resources': [3, 2]},
# 3: {'duration': 5, 'predecessors': [1], 'resources': [4, 1]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 1: 10 Activities\n")
# # Description: A project with 10 activities and balanced resource availability.
#
# num_tasks = 10
# durations = [2, 3, 1, 4, 5, 2, 3, 4, 2, 1]
# resource_requirements = [
# [2, 1], [1, 2], [1, 1], [2, 3], [1, 1],
# [2, 2], [1, 3], [2, 1], [3, 2], [1, 1]
# ]
# resource_availabilities = [6, 6]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [0], # Task 2 depends on Task 0
# [1, 2], # Task 3 depends on Tasks 1 and 2
# [3], # Task 4 depends on Task 3
# [2], # Task 5 depends on Task 2
# [4, 5], # Task 6 depends on Tasks 4 and 5
# [5], # Task 7 depends on Task 5
# [6], # Task 8 depends on Task 6
# [8, 7], # Task 9 depends on Tasks 8 and 7
# ]
# tasks = {
# i: {'duration': durations[i], 'predecessors': predecessors[i], 'resources': resource_requirements[i]}
# for i in range(num_tasks)
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 2: 14 Activities\n")
# # Description: A project with 14 activities, moderate dependencies, and manageable resources.
#
# num_tasks = 14
# durations = [3, 2, 4, 1, 2, 5, 3, 2, 4, 3, 1, 2, 3, 4]
# resource_requirements = [
# [1, 1], [2, 3], [1, 2], [3, 1], [2, 2],
# [1, 3], [2, 1], [1, 1], [3, 2], [2, 3],
# [1, 2], [2, 1], [1, 1], [3, 2]
# ]
# resource_availabilities = [7, 7]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [0], # Task 2 depends on Task 0
# [1, 2], # Task 3 depends on Tasks 1 and 2
# [3], # Task 4 depends on Task 3
# [4], # Task 5 depends on Task 4
# [2], # Task 6 depends on Task 2
# [5, 6], # Task 7 depends on Tasks 5 and 6
# [7], # Task 8 depends on Task 7
# [8], # Task 9 depends on Task 8
# [8], # Task 10 depends on Task 8
# [9, 10], # Task 11 depends on Tasks 9 and 10
# [11], # Task 12 depends on Task 11
# [12], # Task 13 depends on Task 12
# ]
# tasks = {
# i: {'duration': durations[i], 'predecessors': predecessors[i], 'resources': resource_requirements[i]}
# for i in range(num_tasks)
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 20
# scouts = 5
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 3: 17 Activities\n")
# #Description: A project with 17 activities and interdependent task chains.
#
# num_tasks = 17
# durations = [2, 4, 1, 3, 2, 5, 3, 1, 4, 2, 3, 2, 4, 1, 3, 2, 4]
# resource_requirements = [
# [1, 2], [2, 1], [1, 1], [2, 3], [1, 1],
# [3, 2], [2, 2], [1, 1], [3, 3], [2, 2],
# [1, 2], [2, 1], [1, 3], [2, 1], [3, 2],
# [1, 2], [2, 3]
# ]
# resource_availabilities = [8, 8]
# predecessors = [
# [], [0], [1], [2], [3],
# [4], [2], [5, 6], [7], [8],
# [9], [10], [11], [12], [13],
# [14], [15]
# ]
# tasks = {
# i: {'duration': durations[i], 'predecessors': predecessors[i], 'resources': resource_requirements[i]}
# for i in range(num_tasks)
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# validate_solution(project,best_schedule,start_times_of_activities)
# plot_schedule(best_schedule, project, start_times_of_activities)
# print("Test 4: 20 Activities\n")
# # Description: A project with 20 activities and high complexity.
#
# num_tasks = 20
# durations = [1, 3, 2, 4, 5, 2, 3, 1, 4, 3, 2, 3, 1, 4, 2, 3, 1, 2, 4, 3]
# resource_requirements = [
# [1, 2], [2, 1], [1, 1], [3, 2], [2, 3],
# [1, 1], [3, 2], [2, 1], [1, 3], [2, 2],
# [3, 2], [2, 1], [1, 3], [2, 2], [3, 1],
# [2, 1], [1, 2], [2, 3], [3, 2], [1, 1]
# ]
# resource_availabilities = [10, 10]
# predecessors = [
# [], [0], [0], [1, 2], [3],
# [4], [2], [5, 6], [7], [8],
# [9], [10], [11], [12], [13],
# [14], [15], [16], [17], [18]
# ]
# tasks = {
# i: {'duration': durations[i], 'predecessors': predecessors[i], 'resources': resource_requirements[i]}
# for i in range(num_tasks)
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# # Main code
# print("Test 9\n")
# # Description: A project where one task has zero duration.
# num_tasks = 4
# durations = [3, 0, 5, 2]
# resource_requirements = [[1, 2], [0, 0], [2, 1], [1, 1]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [1], # Task 3 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [1, 2]},
# 1: {'duration': 2, 'predecessors': [0], 'resources': [0, 0]},
# 2: {'duration': 5, 'predecessors': [1], 'resources': [2, 1]},
# 3: {'duration': 2, 'predecessors': [1,2], 'resources': [1, 1]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# # Plot the schedule
# plot_schedule(best_schedule, project, task_start_times)
# print("Test 9\n")
# # Description: A larger project with tight resource constraints.
# num_tasks = 6
# durations = [2, 3, 4, 1, 5, 3]
# resource_requirements = [[2, 3], [3, 1], [2, 2], [1, 1], [3, 2], [2, 3]]
# resource_availabilities = [5, 4]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [0], # Task 2 depends on Task 0
# [1], # Task 3 depends on Task 1
# [2, 3], # Task 4 depends on Tasks 2 and 3
# [4], # Task 5 depends on Task 4
# ]
# tasks = {
# 0: {'duration': 2, 'predecessors': [], 'resources': [2, 3]},
# 1: {'duration': 3, 'predecessors': [0], 'resources': [3, 1]},
# 2: {'duration': 4, 'predecessors': [0], 'resources': [2, 2]},
# 3: {'duration': 1, 'predecessors': [1], 'resources': [1, 1]},
# 4: {'duration': 5, 'predecessors': [2, 3], 'resources': [3, 2]},
# 5: {'duration': 3, 'predecessors': [4], 'resources': [2, 3]}
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 50
# scouts = 5
# max_trial = 10
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"test 9 Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# plot_schedule(best_schedule, project, task_start_times)
# #Test 8: Tight Resource Constraints with Heavy Task Duration Overlap
# print("Test 8\n")
# # Description: A project with tight resource constraints and heavy task duration overlap to test resource usage.
# num_tasks = 8
# durations = [7, 4, 5, 3, 2, 6, 5, 4]
# resource_requirements = [[4, 3], [3, 4], [4, 2], [3, 3], [2, 2], [3, 4], [2, 3], [4, 3]]
# resource_availabilities = [10, 7]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [0, 1], # Task 2 depends on Tasks 0 and 1
# [1], # Task 3 depends on Task 1
# [2], # Task 4 depends on Task 2
# [3], # Task 5 depends on Task 3
# [4], # Task 6 depends on Task 4
# [5], # Task 7 depends on Task 5
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [4, 3]},
# 1: {'duration': 2, 'predecessors': [0], 'resources': [3, 4]},
# 2: {'duration': 4, 'predecessors': [0,1], 'resources': [4, 2]},
# 3: {'duration': 1, 'predecessors': [1], 'resources': [3, 3]},
# 4: {'duration': 5, 'predecessors': [2], 'resources': [2, 2]},
# 5: {'duration': 3, 'predecessors': [3], 'resources': [3, 4]},
# 6: {'duration': 4, 'predecessors': [4], 'resources': [2, 3]},
# 7: {'duration': 2, 'predecessors': [5], 'resources': [4, 3]}
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 60
# scouts = 30
# max_trial = 12
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# print("Start times: ", task_start_times)
#
# # Plot the schedule
# plot_schedule(best_schedule, project, task_start_times)
#TEST 9
# num_tasks = 8
# durations = [7, 4, 5, 3, 2, 6, 5, 4]
# resource_requirements = [[4, 3], [3, 4], [4, 2], [3, 3], [2, 2], [3, 4], [2, 3], [4, 3]]
# resource_availabilities = [10, 7]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [0, 1], # Task 2 depends on Tasks 0 and 1
# [1], # Task 3 depends on Task 1
# [2], # Task 4 depends on Task 2
# [3], # Task 5 depends on Task 3
# [4], # Task 6 depends on Task 4
# [5], # Task 7 depends on Task 5
# ]
# tasks = {
# i: {'duration': durations[i], 'predecessors': predecessors[i], 'resources': resource_requirements[i]} for i in range(num_tasks)
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 50
# scouts = 5
# max_trial = 15
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"ABC Makespan: {best_makespan}")
# print(f"ABC Schedule: {task_start_times}")
# print("Start times: ", task_start_times)
# plot_schedule(best_schedule, project, task_start_times)
# #CIRCULAR TESTS - inf none expected
# print("Test C1\n")
# # Description: Tests a project with task dependencies but no resource conflicts.
# num_tasks = 6
# durations = [3, 2, 4, 5, 3, 6]
# resource_requirements = [[1, 2], [2, 3], [2, 1], [1, 4], [3, 2], [2, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [2], # Task 3 depends on Task 2
# [2], # Task 4 depends on Task 2
# [4, 5], # Task 5 depends on Tasks 4 and 5
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [1, 2]},
# 1: {'duration': 2, 'predecessors': [0], 'resources': [2, 3]},
# 2: {'duration': 4, 'predecessors': [1], 'resources': [2, 1]},
# 3: {'duration': 5, 'predecessors': [2], 'resources': [1, 4]},
# 4: {'duration': 3, 'predecessors': [2], 'resources': [3, 2]},
# 5: {'duration': 6, 'predecessors': [4, 5], 'resources': [2, 3]}
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}")
#
# # Test 7: Circular Precedence Edge Case (Invalid Input)
# print("Test C2\n")
# # Description: This test checks if the system gracefully handles an invalid input with circular dependencies.
# num_tasks = 3
# durations = [3, 2, 4]
# resource_requirements = [[1, 2], [2, 1], [1, 1]]
# resource_availabilities = [3, 3]
# predecessors = [
# [2], # Task 0 depends on Task 2
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1 (circular dependency)
# ]
#
# # Test 5: Circular Dependency (Simple Loop)
# # Description: A project where Task 1 depends on Task 2 and Task 2 depends on Task 1.
# num_tasks = 3
# durations = [4, 3, 5]
# resource_requirements = [[1, 1], [2, 2], [1, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [2], # Task 1 depends on Task 2
# [1], # Task 2 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 4, 'predecessors': [], 'resources': [1, 1]},
# 1: {'duration': 3, 'predecessors': [2], 'resources': [2, 2]},
# 2: {'duration': 5, 'predecessors': [1], 'resources': [1, 3]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# # Test 6: Circular Dependency (Complex)
# print("Test C3\n")
# # Description: A project with a more complex circular dependency among multiple tasks.
# num_tasks = 4
# durations = [3, 6, 2, 4]
# resource_requirements = [[1, 1], [2, 3], [1, 2], [3, 1]]
# resource_availabilities = [5, 5]
# predecessors = [
# [3], # Task 0 depends on Task 3
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [2], # Task 3 depends on Task 2
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [3], 'resources': [1, 1]},
# 1: {'duration': 6, 'predecessors': [0], 'resources': [2, 3]},
# 2: {'duration': 2, 'predecessors': [1], 'resources': [1, 2]},
# 3: {'duration': 4, 'predecessors': [2], 'resources': [3, 1]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# # Test 7: Self-Dependency
# print("Test C4\n")
# # Description: A project where a task depends on itself, creating a circular dependency.
# num_tasks = 2
# durations = [4, 3]
# resource_requirements = [[1, 2], [2, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [0], # Task 0 depends on itself
# [0], # Task 1 depends on Task 0
# ]
# tasks = {
# 0: {'duration': 4, 'predecessors': [0], 'resources': [1, 2]},
# 1: {'duration': 3, 'predecessors': [0], 'resources': [2, 3]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# # Test 8: Disconnected Graph (Valid but Contains Isolated Task)
# print("Test C5\n")
# # Description: A project with a disconnected graph where one task has no predecessors or successors.
# num_tasks = 3
# durations = [2, 5, 3]
# resource_requirements = [[1, 1], [2, 2], [1, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [2], # Task 2 depends on itself (Circular Dependency)
# ]
# tasks = {
# 0: {'duration': 2, 'predecessors': [], 'resources': [1, 1]},
# 1: {'duration': 5, 'predecessors': [0], 'resources': [2, 2]},
# 2: {'duration': 3, 'predecessors': [2], 'resources': [1, 3]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, task_start_times = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
# batch2 tests are in testing-bruteForce.py where the results are compared to the brufeForce algorithm
# NOTE: these tests are consisted usually of under 12 activities for bruteForce permormance reasons
# # batch3 - COMPLEX, EDGE CASES ETC with more activities
# print("Test 9\n")
# # Description: A project where one task has zero duration.
# num_tasks = 4
# durations = [3, 0, 5, 2]
# resource_requirements = [[1, 2], [0, 0], [2, 1], [1, 1]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# [1], # Task 3 depends on Task 1
# ]
# tasks = {
# 0: {'duration': 3, 'predecessors': [], 'resources': [1, 2]},
# 1: {'duration': 0, 'predecessors': [0], 'resources': [0, 0]},
# 2: {'duration': 5, 'predecessors': [1], 'resources': [2, 1]},
# 3: {'duration': 2, 'predecessors': [1], 'resources': [1, 1]},
# }
# project = RCPSP(
# num_tasks=len(tasks),
# durations=[task['duration'] for task in tasks.values()],
# resource_requirements=[task['resources'] for task in tasks.values()],
# resource_availabilities=resource_availabilities
# )
# project.tasks = tasks
#
# # Run the ABC algorithm
# population_size = 10
# scouts = 3
# max_trial = 5
# best_schedule, best_makespan, start_times_of_activities = abc(population_size, scouts, max_trial, project)
#
# print(f"Best schedule: {best_schedule}")
# print(f"Best makespan: {best_makespan}\n")
#
# print("Test 10\n")
# # Description: A project where one task requires zero resources.
# num_tasks = 3
# durations = [4, 3, 5]
# resource_requirements = [[1, 1], [0, 0], [2, 3]]
# resource_availabilities = [5, 5]
# predecessors = [
# [], # Task 0
# [0], # Task 1 depends on Task 0
# [1], # Task 2 depends on Task 1
# ]