Feature ObserversΒΆ

[1]:
import matplotlib.pyplot as plt

from job_shop_lib import JobShopInstance, Operation
from job_shop_lib.dispatching.feature_observers import (
    feature_observer_factory,
    FeatureObserverType,
    CompositeFeatureObserver,
    EarliestStartTimeObserver,
)
from job_shop_lib.dispatching import (
    Dispatcher,
    HistoryObserver,
    ReadyOperationsFilterType,
    create_composite_operation_filter,
)
from job_shop_lib.dispatching.rules import DispatchingRuleSolver
from job_shop_lib.visualization.gantt import (
    plot_gantt_chart,
    get_partial_gantt_chart_plotter,
)
from job_shop_lib.visualization.graphs import plot_disjunctive_graph

plt.style.use("ggplot")

m1 = 0
m2 = 1
m3 = 2

job_1 = [
    Operation(m1, 1),
    Operation(m2, 1),
    Operation(m3, 7),
    Operation(m1, 2),
]
job_2 = [Operation(m2, 5), Operation(m3, 1), Operation(m1, 1)]
job_3 = [Operation(m3, 1), Operation(m1, 3), Operation(m2, 2)]

jobs = [job_1, job_2, job_3]

instance = JobShopInstance(
    jobs,
    name="Irregular",
)
[2]:
_ = plot_disjunctive_graph(instance)
../_images/examples_08-Feature-Observers_2_0.png
[3]:
ready_operations_filter = create_composite_operation_filter(
    [
        ReadyOperationsFilterType.DOMINATED_OPERATIONS,
        # ReadyOperationsFilterType.NON_IMMEDIATE_MACHINES,
    ]
)
dispatcher = Dispatcher(
    instance, ready_operations_filter=ready_operations_filter
)

feature_observers = [
    feature_observer_factory(feature_observer_type, dispatcher=dispatcher)
    for feature_observer_type in FeatureObserverType
]
ealiest_start_time_observer = [
    fo for fo in feature_observers if isinstance(fo, EarliestStartTimeObserver)
][0]
history = HistoryObserver(dispatcher)
composite = CompositeFeatureObserver(
    dispatcher, feature_observers=feature_observers
)

print(ealiest_start_time_observer.earliest_start_times)
print(composite)
[[ 0.  1.  2.  9.]
 [ 0.  5.  6. nan]
 [ 0.  1.  4. nan]]
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      1.0                0.0       1.0          0.0            0.0          0.0
1      0.0                1.0       1.0          0.0            1.0          0.0
2      0.0                2.0       7.0          0.0            2.0          0.0
3      0.0                9.0       2.0          0.0            3.0          0.0
4      1.0                0.0       5.0          0.0            0.0          0.0
5      0.0                5.0       1.0          0.0            1.0          0.0
6      0.0                6.0       1.0          0.0            2.0          0.0
7      1.0                0.0       1.0          0.0            0.0          0.0
8      0.0                1.0       3.0          0.0            1.0          0.0
9      0.0                4.0       2.0          0.0            2.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                0.0       7.0          0.0                  4.0          0.0
1      1.0                0.0       8.0          0.0                  3.0          0.0
2      1.0                0.0       9.0          0.0                  3.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                0.0      11.0          0.0                  4.0          0.0
1      1.0                0.0       7.0          0.0                  3.0          0.0
2      1.0                0.0       6.0          0.0                  3.0          0.0
[4]:
solver = DispatchingRuleSolver("most_work_remaining")
plot_function = get_partial_gantt_chart_plotter(show_available_operations=True)
for i in range(len(dispatcher.unscheduled_operations())):
    solver.step(dispatcher)
    print(f"------------------- Step {i + 1} -------------------")
    scheduled_operation = history.history[-1]
    print(f"Scheduled operation: {scheduled_operation}")
    print(f"Current time: {dispatcher.current_time()}")
    print(composite)
    plot_function(
        dispatcher.schedule,
        makespan=11,
        available_operations=dispatcher.available_operations(),
        current_time=dispatcher.current_time(),
    )
    plt.title(f"Step {i + 1}")
    plt.show()
------------------- Step 1 -------------------
Scheduled operation: S-Op(operation=O(m=0, d=1, j=0, p=0), start_time=0, machine_id=0)
Current time: 0
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0                0.0       1.0          1.0            0.0          0.0
1      1.0                1.0       1.0          0.0            0.0          0.0
2      0.0                2.0       7.0          0.0            1.0          0.0
3      0.0                9.0       2.0          0.0            2.0          0.0
4      1.0                0.0       5.0          0.0            0.0          0.0
5      0.0                5.0       1.0          0.0            1.0          0.0
6      0.0                6.0       1.0          0.0            2.0          0.0
7      1.0                0.0       1.0          0.0            0.0          0.0
8      0.0                1.0       3.0          0.0            1.0          0.0
9      0.0                4.0       2.0          0.0            2.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                1.0       6.0          1.0                  3.0          0.0
1      1.0                0.0       8.0          0.0                  3.0          0.0
2      1.0                0.0       9.0          0.0                  3.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                1.0      10.0          1.0                  3.0          0.0
1      1.0                0.0       7.0          0.0                  3.0          0.0
2      1.0                0.0       6.0          0.0                  3.0          0.0
../_images/examples_08-Feature-Observers_4_1.png
------------------- Step 2 -------------------
Scheduled operation: S-Op(operation=O(m=1, d=1, j=0, p=1), start_time=1, machine_id=1)
Current time: 0
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0                0.0       1.0          1.0            0.0          0.0
1      0.0                1.0       1.0          1.0            0.0          0.0
2      0.0                2.0       7.0          0.0            0.0          0.0
3      0.0                9.0       2.0          0.0            1.0          0.0
4      1.0                2.0       5.0          0.0            0.0          0.0
5      0.0                7.0       1.0          0.0            1.0          0.0
6      0.0                8.0       1.0          0.0            2.0          0.0
7      1.0                0.0       1.0          0.0            0.0          0.0
8      0.0                1.0       3.0          0.0            1.0          0.0
9      0.0                4.0       2.0          0.0            2.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                1.0       6.0          1.0                  3.0          0.0
1      1.0                2.0       7.0          1.0                  2.0          0.0
2      1.0                0.0       9.0          0.0                  3.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       9.0          2.0                  2.0          0.0
1      1.0                2.0       7.0          0.0                  3.0          0.0
2      1.0                0.0       6.0          0.0                  3.0          0.0
../_images/examples_08-Feature-Observers_4_3.png
------------------- Step 3 -------------------
Scheduled operation: S-Op(operation=O(m=1, d=5, j=1, p=0), start_time=2, machine_id=1)
Current time: 0
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0                0.0       1.0          1.0            0.0          0.0
1      0.0                1.0       1.0          1.0            0.0          0.0
2      0.0                2.0       7.0          0.0            0.0          0.0
3      0.0                9.0       2.0          0.0            1.0          0.0
4      0.0                2.0       5.0          1.0            0.0          0.0
5      0.0                7.0       1.0          0.0            0.0          0.0
6      0.0                8.0       1.0          0.0            1.0          0.0
7      1.0                0.0       1.0          0.0            0.0          0.0
8      0.0                1.0       3.0          0.0            1.0          0.0
9      0.0                7.0       2.0          0.0            2.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                1.0       6.0          1.0                  3.0          0.0
1      0.0                7.0       2.0          2.0                  1.0          0.0
2      1.0                0.0       9.0          0.0                  3.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       9.0          2.0                  2.0          0.0
1      0.0                7.0       2.0          1.0                  2.0          0.0
2      1.0                0.0       6.0          0.0                  3.0          0.0
../_images/examples_08-Feature-Observers_4_5.png
------------------- Step 4 -------------------
Scheduled operation: S-Op(operation=O(m=2, d=1, j=2, p=0), start_time=0, machine_id=2)
Current time: 1
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0               -1.0       1.0          1.0            0.0          1.0
1      0.0                0.0       1.0          1.0            0.0          0.0
2      1.0                1.0       7.0          0.0            0.0          0.0
3      0.0                8.0       2.0          0.0            1.0          0.0
4      0.0                1.0       5.0          1.0            0.0          0.0
5      1.0                6.0       1.0          0.0            0.0          0.0
6      0.0                7.0       1.0          0.0            1.0          0.0
7      0.0               -1.0       1.0          1.0            0.0          1.0
8      1.0                0.0       3.0          0.0            0.0          0.0
9      0.0                6.0       2.0          0.0            1.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                0.0       6.0          0.0                  3.0          0.0
1      0.0                6.0       2.0          2.0                  1.0          0.0
2      1.0                1.0       8.0          0.0                  2.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                1.0       9.0          1.0                  2.0          0.0
1      1.0                6.0       2.0          1.0                  2.0          0.0
2      1.0                0.0       5.0          0.0                  2.0          0.0
../_images/examples_08-Feature-Observers_4_7.png
------------------- Step 5 -------------------
Scheduled operation: S-Op(operation=O(m=2, d=7, j=0, p=2), start_time=2, machine_id=2)
Current time: 1
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0               -1.0       1.0          1.0            0.0          1.0
1      0.0                0.0       1.0          1.0            0.0          0.0
2      0.0                1.0       7.0          1.0            0.0          0.0
3      0.0                8.0       2.0          0.0            0.0          0.0
4      0.0                1.0       5.0          1.0            0.0          0.0
5      1.0                8.0       1.0          0.0            0.0          0.0
6      0.0                9.0       1.0          0.0            1.0          0.0
7      0.0               -1.0       1.0          1.0            0.0          1.0
8      1.0                0.0       3.0          0.0            0.0          0.0
9      0.0                6.0       2.0          0.0            1.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                0.0       6.0          0.0                  3.0          0.0
1      0.0                6.0       2.0          2.0                  1.0          0.0
2      1.0                8.0       1.0          1.0                  1.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                8.0       2.0          2.0                  1.0          0.0
1      1.0                8.0       2.0          1.0                  2.0          0.0
2      1.0                0.0       5.0          0.0                  2.0          0.0
../_images/examples_08-Feature-Observers_4_9.png
------------------- Step 6 -------------------
Scheduled operation: S-Op(operation=O(m=0, d=3, j=2, p=1), start_time=1, machine_id=0)
Current time: 7
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0               -7.0       1.0          1.0            0.0          1.0
1      0.0               -6.0       1.0          1.0            0.0          1.0
2      0.0               -5.0       2.0          1.0            0.0          0.0
3      1.0                2.0       2.0          0.0            0.0          0.0
4      0.0               -5.0       5.0          1.0            0.0          1.0
5      1.0                2.0       1.0          0.0            0.0          0.0
6      0.0                3.0       1.0          0.0            1.0          0.0
7      0.0               -7.0       1.0          1.0            0.0          1.0
8      0.0               -6.0       3.0          1.0            0.0          1.0
9      1.0                0.0       2.0          0.0            0.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                2.0       3.0          0.0                  2.0          0.0
1      1.0                0.0       2.0          0.0                  1.0          0.0
2      1.0                2.0       1.0          1.0                  1.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                2.0       2.0          1.0                  1.0          0.0
1      1.0                2.0       2.0          0.0                  2.0          0.0
2      1.0                0.0       2.0          0.0                  1.0          0.0
../_images/examples_08-Feature-Observers_4_11.png
------------------- Step 7 -------------------
Scheduled operation: S-Op(operation=O(m=0, d=2, j=0, p=3), start_time=9, machine_id=0)
Current time: 7
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0               -7.0       1.0          1.0            0.0          1.0
1      0.0               -6.0       1.0          1.0            0.0          1.0
2      0.0               -5.0       2.0          1.0            0.0          0.0
3      0.0                2.0       2.0          1.0            0.0          0.0
4      0.0               -5.0       5.0          1.0            0.0          1.0
5      1.0                2.0       1.0          0.0            0.0          0.0
6      0.0                4.0       1.0          0.0            1.0          0.0
7      0.0               -7.0       1.0          1.0            0.0          1.0
8      0.0               -6.0       3.0          1.0            0.0          1.0
9      1.0                0.0       2.0          0.0            0.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                4.0       1.0          1.0                  1.0          0.0
1      1.0                0.0       2.0          0.0                  1.0          0.0
2      1.0                2.0       1.0          1.0                  1.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       0.0          2.0                  0.0          0.0
1      1.0                2.0       2.0          0.0                  2.0          0.0
2      1.0                0.0       2.0          0.0                  1.0          0.0
../_images/examples_08-Feature-Observers_4_13.png
------------------- Step 8 -------------------
Scheduled operation: S-Op(operation=O(m=2, d=1, j=1, p=1), start_time=9, machine_id=2)
Current time: 7
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0               -7.0       1.0          1.0            0.0          1.0
1      0.0               -6.0       1.0          1.0            0.0          1.0
2      0.0               -5.0       2.0          1.0            0.0          0.0
3      0.0                2.0       2.0          1.0            0.0          0.0
4      0.0               -5.0       5.0          1.0            0.0          1.0
5      0.0                2.0       1.0          1.0            0.0          0.0
6      1.0                4.0       1.0          0.0            0.0          0.0
7      0.0               -7.0       1.0          1.0            0.0          1.0
8      0.0               -6.0       3.0          1.0            0.0          1.0
9      1.0                0.0       2.0          0.0            0.0          0.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                4.0       1.0          1.0                  1.0          0.0
1      1.0                0.0       2.0          0.0                  1.0          0.0
2      0.0               -7.0       0.0          2.0                  0.0          0.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       0.0          2.0                  0.0          0.0
1      1.0                4.0       1.0          1.0                  1.0          0.0
2      1.0                0.0       2.0          0.0                  1.0          0.0
../_images/examples_08-Feature-Observers_4_15.png
------------------- Step 9 -------------------
Scheduled operation: S-Op(operation=O(m=1, d=2, j=2, p=2), start_time=7, machine_id=1)
Current time: 11
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0              -11.0       1.0          1.0            0.0          1.0
1      0.0              -10.0       1.0          1.0            0.0          1.0
2      0.0               -9.0       2.0          1.0            0.0          1.0
3      0.0               -2.0       2.0          1.0            0.0          1.0
4      0.0               -9.0       5.0          1.0            0.0          1.0
5      0.0               -2.0       1.0          1.0            0.0          1.0
6      1.0                0.0       1.0          0.0            0.0          0.0
7      0.0              -11.0       1.0          1.0            0.0          1.0
8      0.0              -10.0       3.0          1.0            0.0          1.0
9      0.0               -4.0       2.0          1.0            0.0          1.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      1.0                0.0       1.0          0.0                  1.0          0.0
1      0.0              -11.0       0.0          0.0                  0.0          1.0
2      0.0              -11.0       0.0          0.0                  0.0          1.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       0.0          0.0                  0.0          1.0
1      1.0                0.0       1.0          0.0                  1.0          0.0
2      0.0                0.0       0.0          0.0                  0.0          1.0
../_images/examples_08-Feature-Observers_4_17.png
------------------- Step 10 -------------------
Scheduled operation: S-Op(operation=O(m=0, d=1, j=1, p=2), start_time=11, machine_id=0)
Current time: 12
CompositeFeatureObserver:
------------------------
operations:
   IsReady  EarliestStartTime  Duration  IsScheduled  PositionInJob  IsCompleted
0      0.0              -12.0       1.0          1.0            0.0          1.0
1      0.0              -11.0       1.0          1.0            0.0          1.0
2      0.0              -10.0       2.0          1.0            0.0          1.0
3      0.0               -3.0       2.0          1.0            0.0          1.0
4      0.0              -10.0       5.0          1.0            0.0          1.0
5      0.0               -3.0       1.0          1.0            0.0          1.0
6      0.0               -1.0       1.0          1.0            0.0          1.0
7      0.0              -12.0       1.0          1.0            0.0          1.0
8      0.0              -11.0       3.0          1.0            0.0          1.0
9      0.0               -5.0       2.0          1.0            0.0          1.0
machines:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0              -12.0       0.0          0.0                  0.0          1.0
1      0.0              -12.0       0.0          0.0                  0.0          1.0
2      0.0              -12.0       0.0          0.0                  0.0          1.0
jobs:
   IsReady  EarliestStartTime  Duration  IsScheduled  RemainingOperations  IsCompleted
0      0.0                2.0       0.0          0.0                  0.0          1.0
1      0.0                0.0       0.0          0.0                  0.0          1.0
2      0.0                0.0       0.0          0.0                  0.0          1.0
../_images/examples_08-Feature-Observers_4_19.png
[5]:
plot_gantt_chart(dispatcher.schedule)
[5]:
(<Figure size 640x480 with 1 Axes>,
 <Axes: title={'center': 'Gantt Chart for Irregular instance'}, xlabel='Time units', ylabel='Machines'>)
../_images/examples_08-Feature-Observers_5_1.png
[ ]: